xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/kernels/CpuActivationKernel.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2017-2022 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #include "src/cpu/kernels/CpuActivationKernel.h"
25*c217d954SCole Faust 
26*c217d954SCole Faust #include "arm_compute/core/ITensor.h"
27*c217d954SCole Faust #include "arm_compute/core/TensorInfo.h"
28*c217d954SCole Faust #include "arm_compute/core/Utils.h"
29*c217d954SCole Faust #include "src/core/CPP/Validate.h"
30*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
31*c217d954SCole Faust #include "src/core/helpers/WindowHelpers.h"
32*c217d954SCole Faust 
33*c217d954SCole Faust #include "src/core/common/Registrars.h"
34*c217d954SCole Faust #include "src/cpu/kernels/activation/list.h"
35*c217d954SCole Faust 
36*c217d954SCole Faust #include <array>
37*c217d954SCole Faust 
38*c217d954SCole Faust namespace arm_compute
39*c217d954SCole Faust {
40*c217d954SCole Faust namespace cpu
41*c217d954SCole Faust {
42*c217d954SCole Faust namespace kernels
43*c217d954SCole Faust {
44*c217d954SCole Faust namespace
45*c217d954SCole Faust {
46*c217d954SCole Faust static const std::vector<CpuActivationKernel::ActivationKernel> available_kernels =
47*c217d954SCole Faust {
48*c217d954SCole Faust #ifdef ARM_COMPUTE_ENABLE_SVE
49*c217d954SCole Faust     {
50*c217d954SCole Faust         "sve_q8_activation_lut",
__anon86be97850202() 51*c217d954SCole Faust         [](const ActivationDataTypeISASelectorData & data) { return ActivationLayerInfo::is_lut_supported(data.f, data.dt) && data.cpumodel == CPUModel::A510 && data.isa.sve; },
52*c217d954SCole Faust         REGISTER_QASYMM8_SVE(arm_compute::cpu::sve_q8_activation_lut)
53*c217d954SCole Faust     },
54*c217d954SCole Faust #endif // ARM_COMPUTE_ENABLE_SVE
55*c217d954SCole Faust #ifdef __aarch64__
56*c217d954SCole Faust     {
57*c217d954SCole Faust         // Neon LUT implementantion takes precedence
58*c217d954SCole Faust         "neon_q8_activation_lut",
__anon86be97850302() 59*c217d954SCole Faust         [](const ActivationDataTypeISASelectorData & data) { return ActivationLayerInfo::is_lut_supported(data.f, data.dt); },
60*c217d954SCole Faust         REGISTER_Q8_NEON(arm_compute::cpu::neon_q8_activation_lut)
61*c217d954SCole Faust     },
62*c217d954SCole Faust #endif // __aarch64__
63*c217d954SCole Faust     {
64*c217d954SCole Faust         "sve2_qu8_activation",
__anon86be97850402() 65*c217d954SCole Faust         [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8 && data.isa.sve2 && data.f != ActivationLayerInfo::ActivationFunction::GELU; },
66*c217d954SCole Faust         REGISTER_QASYMM8_SVE2(arm_compute::cpu::sve2_qasymm8_activation)
67*c217d954SCole Faust     },
68*c217d954SCole Faust     {
69*c217d954SCole Faust         "sve2_qs8_activation",
__anon86be97850502() 70*c217d954SCole Faust         [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED && data.isa.sve2 && data.f != ActivationLayerInfo::ActivationFunction::GELU; },
71*c217d954SCole Faust         REGISTER_QASYMM8_SIGNED_SVE2(arm_compute::cpu::sve2_qasymm8_signed_activation)
72*c217d954SCole Faust     },
73*c217d954SCole Faust     {
74*c217d954SCole Faust         "sve2_qs16_activation",
__anon86be97850602() 75*c217d954SCole Faust         [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::QSYMM16 && data.isa.sve2 && data.f != ActivationLayerInfo::ActivationFunction::GELU; },
76*c217d954SCole Faust         REGISTER_QSYMM16_SVE2(arm_compute::cpu::sve2_qsymm16_activation)
77*c217d954SCole Faust     },
78*c217d954SCole Faust     {
79*c217d954SCole Faust         "sve_fp16_activation",
__anon86be97850702() 80*c217d954SCole Faust         [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::F16 && data.isa.sve && data.isa.fp16 && data.f != ActivationLayerInfo::ActivationFunction::GELU; },
81*c217d954SCole Faust         REGISTER_FP16_SVE(arm_compute::cpu::sve_fp16_activation)
82*c217d954SCole Faust     },
83*c217d954SCole Faust     {
84*c217d954SCole Faust         "sve_fp32_activation",
__anon86be97850802() 85*c217d954SCole Faust         [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::F32 && data.isa.sve && data.f != ActivationLayerInfo::ActivationFunction::GELU; },
86*c217d954SCole Faust         REGISTER_FP32_SVE(arm_compute::cpu::sve_fp32_activation)
87*c217d954SCole Faust     },
88*c217d954SCole Faust     {
89*c217d954SCole Faust         "neon_fp16_activation",
__anon86be97850902() 90*c217d954SCole Faust         [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::F16 && data.isa.fp16; },
91*c217d954SCole Faust         REGISTER_FP16_NEON(arm_compute::cpu::neon_fp16_activation)
92*c217d954SCole Faust     },
93*c217d954SCole Faust     {
94*c217d954SCole Faust         "neon_fp32_activation",
__anon86be97850a02() 95*c217d954SCole Faust         [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::F32; },
96*c217d954SCole Faust         REGISTER_FP32_NEON(arm_compute::cpu::neon_fp32_activation)
97*c217d954SCole Faust     },
98*c217d954SCole Faust     {
99*c217d954SCole Faust         "neon_qu8_activation",
__anon86be97850b02() 100*c217d954SCole Faust         [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8; },
101*c217d954SCole Faust         REGISTER_QASYMM8_NEON(arm_compute::cpu::neon_qasymm8_activation)
102*c217d954SCole Faust     },
103*c217d954SCole Faust     {
104*c217d954SCole Faust         "neon_qs8_activation",
__anon86be97850c02() 105*c217d954SCole Faust         [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED; },
106*c217d954SCole Faust         REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::neon_qasymm8_signed_activation)
107*c217d954SCole Faust     },
108*c217d954SCole Faust     {
109*c217d954SCole Faust         "neon_qs16_activation",
__anon86be97850d02() 110*c217d954SCole Faust         [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::QSYMM16; },
111*c217d954SCole Faust         REGISTER_QSYMM16_NEON(arm_compute::cpu::neon_qsymm16_activation)
112*c217d954SCole Faust     },
113*c217d954SCole Faust };
114*c217d954SCole Faust 
115*c217d954SCole Faust /* Supported activation in the 8-bit integer domain */
116*c217d954SCole Faust static const std::array<ActivationLayerInfo::ActivationFunction, 8> qasymm8_activations =
117*c217d954SCole Faust {
118*c217d954SCole Faust     ActivationLayerInfo::ActivationFunction::RELU,
119*c217d954SCole Faust     ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
120*c217d954SCole Faust     ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
121*c217d954SCole Faust     ActivationLayerInfo::ActivationFunction::LOGISTIC,
122*c217d954SCole Faust     ActivationLayerInfo::ActivationFunction::TANH,
123*c217d954SCole Faust     ActivationLayerInfo::ActivationFunction::HARD_SWISH,
124*c217d954SCole Faust     ActivationLayerInfo::ActivationFunction::LEAKY_RELU,
125*c217d954SCole Faust     ActivationLayerInfo::ActivationFunction::GELU,
126*c217d954SCole Faust };
127*c217d954SCole Faust /* Supported activation in the 16-bit integer domain */
128*c217d954SCole Faust static const std::array<ActivationLayerInfo::ActivationFunction, 4> qsymm16_activations =
129*c217d954SCole Faust {
130*c217d954SCole Faust     ActivationLayerInfo::ActivationFunction::LOGISTIC,
131*c217d954SCole Faust     ActivationLayerInfo::ActivationFunction::TANH,
132*c217d954SCole Faust     ActivationLayerInfo::ActivationFunction::HARD_SWISH,
133*c217d954SCole Faust     ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
134*c217d954SCole Faust };
135*c217d954SCole Faust 
validate_arguments(const ITensorInfo * src,const ITensorInfo * dst,const ActivationLayerInfo & activation_info)136*c217d954SCole Faust Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const ActivationLayerInfo &activation_info)
137*c217d954SCole Faust {
138*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src);
139*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::QSYMM16, DataType::F16, DataType::F32);
140*c217d954SCole Faust 
141*c217d954SCole Faust     const auto *uk = CpuActivationKernel::get_implementation(ActivationDataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_cpu_model(), CPUInfo::get().get_isa(), activation_info.activation() });
142*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
143*c217d954SCole Faust 
144*c217d954SCole Faust     const DataType                                data_type = src->data_type();
145*c217d954SCole Faust     const QuantizationInfo                       &oq_info   = (dst != nullptr) ? dst->quantization_info() : src->quantization_info();
146*c217d954SCole Faust     const ActivationLayerInfo::ActivationFunction f_act     = activation_info.activation();
147*c217d954SCole Faust 
148*c217d954SCole Faust     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)),
149*c217d954SCole Faust                                     "For QASYMM8 only hard swish, leaky relu, tanh, logistic, relu and lower/upper bounded relu are supported");
150*c217d954SCole Faust 
151*c217d954SCole Faust     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)),
152*c217d954SCole Faust                                     "For QSYMM16 only tanh and logistic are supported");
153*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON((data_type == DataType::QASYMM8 || data_type == DataType::QASYMM16) && (f_act == ActivationLayerInfo::ActivationFunction::TANH)
154*c217d954SCole Faust                                 && (oq_info != QuantizationInfo(1.f / 128.f, 128)));
155*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON((data_type == DataType::QASYMM8 || data_type == DataType::QASYMM16) && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
156*c217d954SCole Faust                                 && (oq_info != QuantizationInfo(1.f / 256.f, 0)));
157*c217d954SCole Faust 
158*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 128.f, 0)));
159*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 256.f, -128)));
160*c217d954SCole Faust 
161*c217d954SCole Faust     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)));
162*c217d954SCole Faust     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)));
163*c217d954SCole Faust 
164*c217d954SCole Faust     // Checks performed when dst is configured
165*c217d954SCole Faust     if((dst != nullptr) && (dst->total_size() != 0))
166*c217d954SCole Faust     {
167*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst);
168*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
169*c217d954SCole Faust     }
170*c217d954SCole Faust 
171*c217d954SCole Faust     return Status{};
172*c217d954SCole Faust }
173*c217d954SCole Faust 
validate_and_configure_window(const ITensorInfo * src,ITensorInfo * dst)174*c217d954SCole Faust std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *src, ITensorInfo *dst)
175*c217d954SCole Faust {
176*c217d954SCole Faust     // Configure kernel window
177*c217d954SCole Faust     Window win = calculate_max_window(*src, Steps());
178*c217d954SCole Faust 
179*c217d954SCole Faust     if(dst != nullptr)
180*c217d954SCole Faust     {
181*c217d954SCole Faust         // dst auto inizialitation if not yet initialized
182*c217d954SCole Faust         auto_init_if_empty(*dst, *src->clone());
183*c217d954SCole Faust     }
184*c217d954SCole Faust 
185*c217d954SCole Faust     return std::make_pair(Status{}, win);
186*c217d954SCole Faust }
187*c217d954SCole Faust } // namespace
188*c217d954SCole Faust 
configure(const ITensorInfo * src,ITensorInfo * dst,ActivationLayerInfo activation_info)189*c217d954SCole Faust void CpuActivationKernel::configure(const ITensorInfo *src, ITensorInfo *dst, ActivationLayerInfo activation_info)
190*c217d954SCole Faust {
191*c217d954SCole Faust     ARM_COMPUTE_UNUSED(dst);
192*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_NULLPTR(src);
193*c217d954SCole Faust     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, activation_info));
194*c217d954SCole Faust 
195*c217d954SCole Faust     const auto uk = CpuActivationKernel::get_implementation(ActivationDataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_cpu_model(), CPUInfo::get().get_isa(), activation_info.activation() });
196*c217d954SCole Faust     if(dst != nullptr)
197*c217d954SCole Faust     {
198*c217d954SCole Faust         // dst auto inizialitation if not yet initialized
199*c217d954SCole Faust         auto_init_if_empty(*dst, *src->clone());
200*c217d954SCole Faust     }
201*c217d954SCole Faust 
202*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_NULLPTR(uk);
203*c217d954SCole Faust 
204*c217d954SCole Faust     _run_method = uk->ukernel;
205*c217d954SCole Faust     _name       = std::string("CpuActivationKernel").append("/").append(uk->name);
206*c217d954SCole Faust 
207*c217d954SCole Faust #ifdef __aarch64__
208*c217d954SCole Faust     if(ActivationLayerInfo::is_lut_supported(activation_info.activation(), src->data_type()))
209*c217d954SCole Faust     {
210*c217d954SCole Faust         activation_info.init_lut(src->data_type(), src->quantization_info().uniform(), (dst) ? dst->quantization_info().uniform() : src->quantization_info().uniform());
211*c217d954SCole Faust     }
212*c217d954SCole Faust #endif // __aarch64__
213*c217d954SCole Faust     _act_info = activation_info;
214*c217d954SCole Faust 
215*c217d954SCole Faust     Window win;
216*c217d954SCole Faust 
217*c217d954SCole Faust     // Use squashed window
218*c217d954SCole Faust     std::tie(win, _split_dimension) = calculate_squashed_or_max_window(*src);
219*c217d954SCole Faust     ICPPKernel::configure(win);
220*c217d954SCole Faust }
221*c217d954SCole Faust 
validate(const ITensorInfo * src,const ITensorInfo * dst,const ActivationLayerInfo & act_info)222*c217d954SCole Faust Status CpuActivationKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
223*c217d954SCole Faust {
224*c217d954SCole Faust     ARM_COMPUTE_UNUSED(act_info);
225*c217d954SCole Faust     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, act_info));
226*c217d954SCole Faust     ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src->clone().get(), (dst != nullptr) ? dst->clone().get() : nullptr).first);
227*c217d954SCole Faust 
228*c217d954SCole Faust     return Status{};
229*c217d954SCole Faust }
230*c217d954SCole Faust 
get_mws(const CPUInfo & platform,size_t thread_count) const231*c217d954SCole Faust size_t CpuActivationKernel::get_mws(const CPUInfo &platform, size_t thread_count) const
232*c217d954SCole Faust {
233*c217d954SCole Faust     ARM_COMPUTE_UNUSED(thread_count);
234*c217d954SCole Faust     ARM_COMPUTE_UNUSED(platform);
235*c217d954SCole Faust 
236*c217d954SCole Faust     if(_split_dimension == Window::DimX)
237*c217d954SCole Faust     {
238*c217d954SCole Faust         // Don't split the work load too small if the tensor has been reinterpreted as 1D.
239*c217d954SCole Faust         // This number is loosely chosen as threading overhead in each platform varies wildly.
240*c217d954SCole Faust         return 1536;
241*c217d954SCole Faust     }
242*c217d954SCole Faust     return default_mws;
243*c217d954SCole Faust }
244*c217d954SCole Faust 
run_op(ITensorPack & tensors,const Window & window,const ThreadInfo & info)245*c217d954SCole Faust void CpuActivationKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
246*c217d954SCole Faust {
247*c217d954SCole Faust     // Early exit on disabled activation
248*c217d954SCole Faust     if(!_act_info.enabled())
249*c217d954SCole Faust     {
250*c217d954SCole Faust         return;
251*c217d954SCole Faust     }
252*c217d954SCole Faust 
253*c217d954SCole Faust     ARM_COMPUTE_UNUSED(info);
254*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
255*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
256*c217d954SCole Faust 
257*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON(tensors.empty());
258*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON(_run_method == nullptr);
259*c217d954SCole Faust 
260*c217d954SCole Faust     const ITensor *src = tensors.get_const_tensor(TensorType::ACL_SRC);
261*c217d954SCole Faust     ITensor       *dst = tensors.get_tensor(TensorType::ACL_DST);
262*c217d954SCole Faust 
263*c217d954SCole Faust     _run_method(src, dst, _act_info, window);
264*c217d954SCole Faust }
265*c217d954SCole Faust 
name() const266*c217d954SCole Faust const char *CpuActivationKernel::name() const
267*c217d954SCole Faust {
268*c217d954SCole Faust     return _name.c_str();
269*c217d954SCole Faust }
270*c217d954SCole Faust 
get_available_kernels()271*c217d954SCole Faust const std::vector<CpuActivationKernel::ActivationKernel> &CpuActivationKernel::get_available_kernels()
272*c217d954SCole Faust {
273*c217d954SCole Faust     return available_kernels;
274*c217d954SCole Faust }
275*c217d954SCole Faust } // namespace kernels
276*c217d954SCole Faust } // namespace cpu
277*c217d954SCole Faust } // namespace arm_compute
278