blob: 8fa7e9525eb7e9c5704cd617051b8e2814fcfa0b [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitasf8f04422021-01-08 17:25:55 +00002 * Copyright (c) 2017-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/kernels/CpuActivationKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#include "arm_compute/core/ITensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/TensorInfo.h"
28#include "arm_compute/core/Utils.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010029#include "src/core/CPP/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010030#include "src/core/helpers/AutoConfiguration.h"
31#include "src/core/helpers/WindowHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032
Michalis Spyrouc4d45552020-10-19 12:41:30 +010033#include "src/core/common/Registrars.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010034#include "src/cpu/kernels/activation/list.h"
Michalis Spyrouc4d45552020-10-19 12:41:30 +010035
Georgios Pinitasf8f04422021-01-08 17:25:55 +000036#include <array>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +010038namespace arm_compute
39{
Georgios Pinitasf8f04422021-01-08 17:25:55 +000040namespace cpu
41{
42namespace kernels
43{
Michalis Spyrouafa5d812017-11-30 14:25:57 +000044namespace
45{
Michalis Spyrouc4d45552020-10-19 12:41:30 +010046struct ActivationSelectorData
47{
Michalis Spyrou20fca522021-06-07 14:23:57 +010048 DataType dt;
49 const CPUInfo &ci;
Michalis Spyrouc4d45552020-10-19 12:41:30 +010050};
51
52using ActivationSelectorPtr = std::add_pointer<bool(const ActivationSelectorData &data)>::type;
53using ActivationKernelPtr = std::add_pointer<void(const ITensor *, ITensor *, const ActivationLayerInfo &, const Window &)>::type;
54
55struct ActivationKernel
56{
57 const char *name;
58 const ActivationSelectorPtr is_selected;
59 ActivationKernelPtr ukernel;
60};
61
62static const ActivationKernel available_kernels[] =
63{
Michalis Spyrou20fca522021-06-07 14:23:57 +010064#if defined(ARM_COMPUTE_ENABLE_SVE)
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +000065 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010066 "sve_fp16_activation",
Michalis Spyrou20fca522021-06-07 14:23:57 +010067 [](const ActivationSelectorData & data) { return data.dt == DataType::F16 && data.ci.has_sve(); },
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +000068 REGISTER_FP16_SVE(arm_compute::cpu::fp16_sve_activation)
69 },
70 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010071 "sve_fp32_activation",
Michalis Spyrou20fca522021-06-07 14:23:57 +010072 [](const ActivationSelectorData & data) { return data.dt == DataType::F32 && data.ci.has_sve(); },
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +000073 REGISTER_FP32_SVE(arm_compute::cpu::fp32_sve_activation)
74 },
Michalis Spyrou20fca522021-06-07 14:23:57 +010075#endif /* defined(ARM_COMPUTE_ENABLE_SVE) */
76#if defined(ARM_COMPUTE_ENABLE_NEON)
Michalis Spyrouc4d45552020-10-19 12:41:30 +010077 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010078 "neon_fp16_activation",
Michalis Spyrouc4d45552020-10-19 12:41:30 +010079 [](const ActivationSelectorData & data) { return data.dt == DataType::F16; },
80 REGISTER_FP16_NEON(arm_compute::cpu::fp16_neon_activation)
81 },
82 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010083 "neon_fp32_activation",
Michalis Spyrouc4d45552020-10-19 12:41:30 +010084 [](const ActivationSelectorData & data) { return data.dt == DataType::F32; },
85 REGISTER_FP32_NEON(arm_compute::cpu::fp32_neon_activation)
86 },
Michalis Spyrou20fca522021-06-07 14:23:57 +010087#endif /* defined(ARM_COMPUTE_ENABLE_NEON) */
88#if defined(ARM_COMPUTE_ENABLE_SVE2)
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +000089 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010090 "sve_qu8_activation",
Michalis Spyrou20fca522021-06-07 14:23:57 +010091 [](const ActivationSelectorData & data) { return data.dt == DataType::QASYMM8 && data.ci.has_sve2(); },
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +000092 REGISTER_QASYMM8_SVE(arm_compute::cpu::qasymm8_sve_activation)
93 },
94 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010095 "sve_qs8_activation",
Michalis Spyrou20fca522021-06-07 14:23:57 +010096 [](const ActivationSelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED && data.ci.has_sve2(); },
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +000097 REGISTER_QASYMM8_SIGNED_SVE(arm_compute::cpu::qasymm8_signed_sve_activation)
98 },
99 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100100 "sve_qs16_activation",
Michalis Spyrou20fca522021-06-07 14:23:57 +0100101 [](const ActivationSelectorData & data) { return data.dt == DataType::QSYMM16 && data.ci.has_sve2(); },
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +0000102 REGISTER_QSYMM16_SVE(arm_compute::cpu::qsymm16_sve_activation)
103 },
Michalis Spyrou20fca522021-06-07 14:23:57 +0100104#endif /* defined(ARM_COMPUTE_ENABLE_SVE2) */
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100105 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100106 "neon_qu8_activation",
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100107 [](const ActivationSelectorData & data) { return data.dt == DataType::QASYMM8; },
108 REGISTER_QASYMM8_NEON(arm_compute::cpu::qasymm8_neon_activation)
109 },
110 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100111 "neon_qs8_activation",
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100112 [](const ActivationSelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED; },
113 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::qasymm8_signed_neon_activation)
114 },
115 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100116 "neon_qs16_activation",
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100117 [](const ActivationSelectorData & data) { return data.dt == DataType::QSYMM16; },
118 REGISTER_QSYMM16_NEON(arm_compute::cpu::qsymm16_neon_activation)
119 },
120};
121
122const ActivationKernel *get_implementation(const ActivationSelectorData &data)
123{
124 for(const auto &uk : available_kernels)
125 {
126 if(uk.is_selected(data))
127 {
128 return &uk;
129 }
130 }
131 return nullptr;
132}
133
Georgios Pinitasf8f04422021-01-08 17:25:55 +0000134/* Supported activation in the 8-bit integer domain */
135static const std::array<ActivationLayerInfo::ActivationFunction, 7> qasymm8_activations =
136{
137 ActivationLayerInfo::ActivationFunction::RELU,
138 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
139 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
140 ActivationLayerInfo::ActivationFunction::LOGISTIC,
141 ActivationLayerInfo::ActivationFunction::TANH,
142 ActivationLayerInfo::ActivationFunction::HARD_SWISH,
143 ActivationLayerInfo::ActivationFunction::LEAKY_RELU,
144};
145/* Supported activation in the 16-bit integer domain */
146static const std::array<ActivationLayerInfo::ActivationFunction, 3> qsymm16_activations =
147{
148 ActivationLayerInfo::ActivationFunction::LOGISTIC,
149 ActivationLayerInfo::ActivationFunction::TANH,
150 ActivationLayerInfo::ActivationFunction::HARD_SWISH
151};
152
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000153Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const ActivationLayerInfo &activation_info)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000154{
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000155 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src);
156 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::QSYMM16, DataType::F16, DataType::F32);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000157
Michalis Spyrou20fca522021-06-07 14:23:57 +0100158 const auto *uk = get_implementation(ActivationSelectorData{ src->data_type(), CPUInfo::get() });
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100159 ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
160
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000161 const DataType data_type = src->data_type();
162 const QuantizationInfo &oq_info = (dst != nullptr) ? dst->quantization_info() : src->quantization_info();
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100163 const ActivationLayerInfo::ActivationFunction f_act = activation_info.activation();
164
Georgios Pinitasf8f04422021-01-08 17:25:55 +0000165 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized_asymmetric(data_type) && (std::find(std::begin(qasymm8_activations), std::end(qasymm8_activations), f_act) == std::end(qasymm8_activations)),
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000166 "For QASYMM8 only hard swish, leaky relu, tanh, logistic, relu and lower/upper bounded relu are supported");
giuros01c9573f32019-06-20 10:30:17 +0100167
Georgios Pinitasf8f04422021-01-08 17:25:55 +0000168 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized_symmetric(data_type) && (std::find(std::begin(qsymm16_activations), std::end(qsymm16_activations), f_act) == std::end(qsymm16_activations)),
giuros01c9573f32019-06-20 10:30:17 +0100169 "For QSYMM16 only tanh and logistic are supported");
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000170 ARM_COMPUTE_RETURN_ERROR_ON((data_type == DataType::QASYMM8 || data_type == DataType::QASYMM16) && (f_act == ActivationLayerInfo::ActivationFunction::TANH)
171 && (oq_info != QuantizationInfo(1.f / 128.f, 128)));
172 ARM_COMPUTE_RETURN_ERROR_ON((data_type == DataType::QASYMM8 || data_type == DataType::QASYMM16) && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
173 && (oq_info != QuantizationInfo(1.f / 256.f, 0)));
174
175 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 128.f, 0)));
176 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 256.f, -128)));
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100177
giuros01c9573f32019-06-20 10:30:17 +0100178 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_symmetric(data_type) && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 32768.f, 0)));
179 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_symmetric(data_type) && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 32768.f, 0)));
180
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000181 // Checks performed when dst is configured
182 if((dst != nullptr) && (dst->total_size() != 0))
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000183 {
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000184 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst);
185 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000186 }
187
188 return Status{};
189}
190
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000191std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *src, ITensorInfo *dst)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000192{
Georgios Pinitas5a594532018-12-03 14:30:05 +0000193 // Configure kernel window
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000194 Window win = calculate_max_window(*src, Steps());
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000195
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000196 if(dst != nullptr)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000197 {
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000198 // dst auto inizialitation if not yet initialized
199 auto_init_if_empty(*dst, *src->clone());
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000200 }
201
Georgios Pinitas5a594532018-12-03 14:30:05 +0000202 return std::make_pair(Status{}, win);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000203}
204} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100205
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000206void CpuActivationKernel::configure(const ITensorInfo *src, ITensorInfo *dst, ActivationLayerInfo activation_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100207{
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100208 ARM_COMPUTE_ERROR_ON_NULLPTR(src);
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000209 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, activation_info));
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000210
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100211 const auto uk = get_implementation(ActivationSelectorData{ src->data_type(), CPUInfo::get() });
212 ARM_COMPUTE_ERROR_ON_NULLPTR(uk);
213
214 _act_info = activation_info;
215 _run_method = uk->ukernel;
216 _name = std::string("CpuActivationKernel").append("/").append(uk->name);
217
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100218 // Configure kernel window
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000219 auto win_config = validate_and_configure_window(src, dst);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000220 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
221 ICPPKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100222}
223
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000224Status CpuActivationKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000225{
226 ARM_COMPUTE_UNUSED(act_info);
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000227 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, act_info));
228 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src->clone().get(), (dst != nullptr) ? dst->clone().get() : nullptr).first);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000229
230 return Status{};
231}
232
Georgios Pinitasf8f04422021-01-08 17:25:55 +0000233void CpuActivationKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100234{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100235 // Early exit on disabled activation
236 if(!_act_info.enabled())
237 {
238 return;
239 }
240
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100241 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100242 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100243 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100244
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100245 ARM_COMPUTE_ERROR_ON(tensors.empty());
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100246 ARM_COMPUTE_ERROR_ON(_run_method == nullptr);
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100247
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100248 const ITensor *src = tensors.get_const_tensor(TensorType::ACL_SRC);
249 ITensor *dst = tensors.get_tensor(TensorType::ACL_DST);
250
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100251 _run_method(src, dst, _act_info, window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100252}
Georgios Pinitasf8f04422021-01-08 17:25:55 +0000253
254const char *CpuActivationKernel::name() const
255{
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100256 return _name.c_str();
Georgios Pinitasf8f04422021-01-08 17:25:55 +0000257}
258} // namespace kernels
259} // namespace cpu
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +0100260} // namespace arm_compute