blob: 761258941dbdf18dc80ab73a188baaa14724ca38 [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 Pinitasf8f04422021-01-08 17:25:55 +000024#include "src/core/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 Pinitasf8f04422021-01-08 17:25:55 +000034#include "src/core/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{
48 DataType dt;
49};
50
51using ActivationSelectorPtr = std::add_pointer<bool(const ActivationSelectorData &data)>::type;
52using ActivationKernelPtr = std::add_pointer<void(const ITensor *, ITensor *, const ActivationLayerInfo &, const Window &)>::type;
53
54struct ActivationKernel
55{
56 const char *name;
57 const ActivationSelectorPtr is_selected;
58 ActivationKernelPtr ukernel;
59};
60
61static const ActivationKernel available_kernels[] =
62{
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +000063#if defined(__ARM_FEATURE_SVE)
64 {
65 "fp16_sve_activation",
66 [](const ActivationSelectorData & data) { return data.dt == DataType::F16; },
67 REGISTER_FP16_SVE(arm_compute::cpu::fp16_sve_activation)
68 },
69 {
70 "fp32_sve_activation",
71 [](const ActivationSelectorData & data) { return data.dt == DataType::F32; },
72 REGISTER_FP32_SVE(arm_compute::cpu::fp32_sve_activation)
73 },
74#else /* !defined(__ARM_FEATURE_SVE) */
Michalis Spyrouc4d45552020-10-19 12:41:30 +010075 {
76 "fp16_neon_activation",
77 [](const ActivationSelectorData & data) { return data.dt == DataType::F16; },
78 REGISTER_FP16_NEON(arm_compute::cpu::fp16_neon_activation)
79 },
80 {
81 "fp32_neon_activation",
82 [](const ActivationSelectorData & data) { return data.dt == DataType::F32; },
83 REGISTER_FP32_NEON(arm_compute::cpu::fp32_neon_activation)
84 },
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +000085#endif /* defined(__ARM_FEATURE_SVE) */
86
87#if defined(__ARM_FEATURE_SVE2) /* defined(__ARM_FEATURE_SVE2) */
88 {
89 "qasymm8_sve_activation",
90 [](const ActivationSelectorData & data) { return data.dt == DataType::QASYMM8; },
91 REGISTER_QASYMM8_SVE(arm_compute::cpu::qasymm8_sve_activation)
92 },
93 {
94 "qasymm8_signed_sve_activation",
95 [](const ActivationSelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED; },
96 REGISTER_QASYMM8_SIGNED_SVE(arm_compute::cpu::qasymm8_signed_sve_activation)
97 },
98 {
99 "qsymm16_sve_activation",
100 [](const ActivationSelectorData & data) { return data.dt == DataType::QSYMM16; },
101 REGISTER_QSYMM16_SVE(arm_compute::cpu::qsymm16_sve_activation)
102 },
103#else /* !defined(__ARM_FEATURE_SVE2) */
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100104 {
105 "qasymm8_neon_activation",
106 [](const ActivationSelectorData & data) { return data.dt == DataType::QASYMM8; },
107 REGISTER_QASYMM8_NEON(arm_compute::cpu::qasymm8_neon_activation)
108 },
109 {
110 "qasymm8_signed_neon_activation",
111 [](const ActivationSelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED; },
112 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::qasymm8_signed_neon_activation)
113 },
114 {
115 "qsymm16_neon_activation",
116 [](const ActivationSelectorData & data) { return data.dt == DataType::QSYMM16; },
117 REGISTER_QSYMM16_NEON(arm_compute::cpu::qsymm16_neon_activation)
118 },
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +0000119#endif /* defined(__ARM_FEATURE_SVE2) */
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100120};
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
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000158 const auto *uk = get_implementation(ActivationSelectorData{ src->data_type() });
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{
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000208 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100210 _act_info = activation_info;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000212 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, activation_info));
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000213
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100214 // Configure kernel window
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000215 auto win_config = validate_and_configure_window(src, dst);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000216 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
217 ICPPKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218}
219
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000220Status CpuActivationKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000221{
222 ARM_COMPUTE_UNUSED(act_info);
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000223 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, act_info));
224 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 +0000225
226 return Status{};
227}
228
Georgios Pinitasf8f04422021-01-08 17:25:55 +0000229void CpuActivationKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100230{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100231 // Early exit on disabled activation
232 if(!_act_info.enabled())
233 {
234 return;
235 }
236
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100237 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100238 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100239 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100240
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100241 ARM_COMPUTE_ERROR_ON(tensors.empty());
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100242
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100243 const ITensor *src = tensors.get_const_tensor(TensorType::ACL_SRC);
244 ITensor *dst = tensors.get_tensor(TensorType::ACL_DST);
245
246 const auto *uk = get_implementation(ActivationSelectorData{ src->info()->data_type() });
247
248 uk->ukernel(src, dst, _act_info, window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100249}
Georgios Pinitasf8f04422021-01-08 17:25:55 +0000250
251const char *CpuActivationKernel::name() const
252{
253 return "CpuActivationKernel";
254}
255} // namespace kernels
256} // namespace cpu
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +0100257} // namespace arm_compute