xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/kernels/activation/generic/neon/qasymm8.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2020-2022 Arm Limited.
3  *
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  */
24 
25 #include "arm_compute/core/Helpers.h"
26 #include "arm_compute/core/Window.h"
27 #include "src/core/NEON/NEAsymm.h"
28 #include "src/core/NEON/NEMath.h"
29 #include "src/core/NEON/wrapper/wrapper.h"
30 
31 #include <arm_neon.h>
32 #include <cmath>
33 #include <cstddef>
34 #include <cstdint>
35 
36 namespace arm_compute
37 {
38 namespace cpu
39 {
neon_qasymm8_activation(const ITensor * src,ITensor * dst,const ActivationLayerInfo & act_info,const Window & window)40 void neon_qasymm8_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
41 {
42     constexpr int                                 window_step_x  = 16;
43     const auto                                    window_start_x = static_cast<int>(window.x().start());
44     const auto                                    window_end_x   = static_cast<int>(window.x().end());
45     const ActivationLayerInfo::ActivationFunction act            = act_info.activation();
46 
47     Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
48     win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
49 
50     Iterator input(src, win_collapsed);
51     Iterator output(dst, win_collapsed);
52 
53     const UniformQuantizationInfo qi_in    = src->info()->quantization_info().uniform();
54     const UniformQuantizationInfo qi_out   = dst->info()->quantization_info().uniform();
55     const qasymm8x16_t            va       = vdupq_n_u8(quantize_qasymm8(act_info.a(), qi_in));
56     const qasymm8x16_t            vb       = vdupq_n_u8(quantize_qasymm8(act_info.b(), qi_in));
57     const qasymm8_t               a        = quantize_qasymm8(act_info.a(), qi_in);
58     const qasymm8_t               b        = quantize_qasymm8(act_info.b(), qi_in);
59     const qasymm8_t               const_0  = quantize_qasymm8(0.f, qi_in);
60     const qasymm8x16_t            vconst_0 = vdupq_n_u8(const_0);
61     const auto                    vconst_1 = vdupq_n_f32(1.f);
62 
63 #ifndef __aarch64__
64     const auto vconst_0_f32 = vdupq_n_f32(0);
65 #else  // #ifndef __aarch64__
66     const auto const_inv_2      = vdupq_n_f32(0.5f);
67     const auto const_inv_sqrt_2 = vdupq_n_f32(0.70710678118f);
68 #endif // __aarch64__
69     const float32x4_t va_f32 = vdupq_n_f32(act_info.a());
70     const float32x4_t vb_f32 = vdupq_n_f32(act_info.b());
71     const float       a_f32  = act_info.a();
72     const float       b_f32  = act_info.b();
73 
74 #ifndef __aarch64__
75     const auto const_6_f32     = vdupq_n_f32(6.f);
76     const auto const_0_f32     = vdupq_n_f32(0.f);
77     const auto const_3_f32     = vdupq_n_f32(3.f);
78     const auto const_inv_6_f32 = vdupq_n_f32(0.166666667f);
79 #endif // __aarch64__
80 
81     // Initialise scale/offset for re-quantization
82     float       s  = qi_in.scale / qi_out.scale;
83     float       o  = -qi_in.offset * s + qi_out.offset;
84     float32x4_t vs = vdupq_n_f32(s);
85     float32x4_t vo = vdupq_n_f32(o);
86 
87     execute_window_loop(win_collapsed, [&](const Coordinates &)
88     {
89         const auto input_ptr  = reinterpret_cast<const qasymm8_t *>(input.ptr());
90         const auto output_ptr = reinterpret_cast<qasymm8_t *>(output.ptr());
91 
92         wrapper::traits::neon_bitvector_t<qasymm8_t, wrapper::traits::BitWidth::W128> tmp;
93 
94         // Compute S elements per iteration
95         int x = window_start_x;
96         for(; x <= (window_end_x - window_step_x); x += window_step_x)
97         {
98             const auto vin = wrapper::vloadq(input_ptr + x);
99             if(act == ActivationLayerInfo::ActivationFunction::RELU)
100             {
101                 // Perform activation
102                 tmp = vmaxq_u8(vconst_0, vin);
103                 // Re-quantize to new output space
104                 tmp = vmlaq_qasymm8(tmp, vs, vo);
105             }
106             else if(act == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU)
107             {
108                 // Perform activation
109                 tmp = vminq_u8(va, vmaxq_u8(vconst_0, vin));
110                 // Re-quantize to new output space
111                 tmp = vmlaq_qasymm8(tmp, vs, vo);
112             }
113             else if(act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
114             {
115                 // Perform activation
116                 tmp = vminq_u8(va, vmaxq_u8(vb, vin));
117                 // Re-quantize to new output space
118                 tmp = vmlaq_qasymm8(tmp, vs, vo);
119             }
120 #ifndef __aarch64__ // LUT-based implementation is used for aarch64 instead.
121             else if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
122             {
123                 // De-quantize
124                 const auto vin_deq = vdequantize(vin, qi_in);
125                 // Perform activation
126                 const float32x4x4_t tmp_dep =
127                 {
128                     {
129                         wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[0])))),
130                         wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[1])))),
131                         wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[2])))),
132                         wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[3])))),
133                     }
134                 };
135                 // Re-quantize to new output space
136                 tmp = vquantize(tmp_dep, qi_out);
137             }
138 #endif // __aarch64__
139             else if(act == ActivationLayerInfo::ActivationFunction::TANH)
140             {
141                 // De-quantize
142                 const auto vin_deq = vdequantize(vin, qi_in);
143                 // Perform activation
144                 const float32x4x4_t tmp_dep =
145                 {
146                     {
147                         wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[0], vb_f32))),
148                         wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[1], vb_f32))),
149                         wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[2], vb_f32))),
150                         wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[3], vb_f32))),
151                     }
152                 };
153                 // Re-quantize to new output space
154                 tmp = vquantize(tmp_dep, qi_out);
155             }
156 #ifndef __aarch64__ // LUT-based implementation is used for aarch64 instead.
157             else if(act == ActivationLayerInfo::ActivationFunction::HARD_SWISH)
158             {
159                 // De-quantize
160                 const auto vin_deq = vdequantize(vin, qi_in);
161                 // Perform activation
162                 const float32x4x4_t tmp_dep =
163                 {
164                     {
165                         wrapper::vmul(vin_deq.val[0], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[0], const_3_f32))))),
166                         wrapper::vmul(vin_deq.val[1], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[1], const_3_f32))))),
167                         wrapper::vmul(vin_deq.val[2], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[2], const_3_f32))))),
168                         wrapper::vmul(vin_deq.val[3], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[3], const_3_f32))))),
169                     }
170                 };
171                 // Re-quantize to new output space
172                 tmp = vquantize(tmp_dep, qi_out);
173             }
174             else if(act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU)
175             {
176                 const auto vin_deq = vdequantize(vin, qi_in);
177 
178                 const uint32x4x4_t pos_mask =
179                 {
180                     {
181                         wrapper::vcgt(vin_deq.val[0], vconst_0_f32),
182                         wrapper::vcgt(vin_deq.val[1], vconst_0_f32),
183                         wrapper::vcgt(vin_deq.val[2], vconst_0_f32),
184                         wrapper::vcgt(vin_deq.val[3], vconst_0_f32),
185                     }
186                 };
187 
188                 const float32x4x4_t tmp_dep =
189                 {
190                     {
191                         wrapper::vbsl(pos_mask.val[0], vin_deq.val[0], wrapper::vmul(va_f32, vin_deq.val[0])),
192                         wrapper::vbsl(pos_mask.val[1], vin_deq.val[1], wrapper::vmul(va_f32, vin_deq.val[1])),
193                         wrapper::vbsl(pos_mask.val[2], vin_deq.val[2], wrapper::vmul(va_f32, vin_deq.val[2])),
194                         wrapper::vbsl(pos_mask.val[3], vin_deq.val[3], wrapper::vmul(va_f32, vin_deq.val[3])),
195                     }
196                 };
197 
198                 tmp = vquantize(tmp_dep, qi_out);
199             }
200 #else  // #ifndef __aarch64__
201             else if (act == ActivationLayerInfo::ActivationFunction::GELU)
202             {
203                 const auto vin_deq = vdequantize(vin, qi_in);
204                 // Perform activation
205                 const float32x4x4_t tmp_dep =
206                 {
207                     {
208                         wrapper::vmul(vin_deq.val[0], wrapper::vmul(const_inv_2, wrapper::vadd(vconst_1, wrapper::verf(wrapper::vmul(vin_deq.val[0], const_inv_sqrt_2))))),
209                         wrapper::vmul(vin_deq.val[1], wrapper::vmul(const_inv_2, wrapper::vadd(vconst_1, wrapper::verf(wrapper::vmul(vin_deq.val[1], const_inv_sqrt_2))))),
210                         wrapper::vmul(vin_deq.val[2], wrapper::vmul(const_inv_2, wrapper::vadd(vconst_1, wrapper::verf(wrapper::vmul(vin_deq.val[2], const_inv_sqrt_2))))),
211                         wrapper::vmul(vin_deq.val[3], wrapper::vmul(const_inv_2, wrapper::vadd(vconst_1, wrapper::verf(wrapper::vmul(vin_deq.val[3], const_inv_sqrt_2))))),
212                     }
213                 };
214                 // Re-quantize to new output space
215                 tmp = vquantize(tmp_dep, qi_out);
216             }
217 #endif // __aarch64__
218             else
219             {
220                 ARM_COMPUTE_ERROR("Unsupported activation function");
221             }
222             wrapper::vstore(output_ptr + x, tmp);
223         }
224 
225         // Compute left-over elements
226         for(; x < window_end_x; ++x)
227         {
228             qasymm8_t in  = *(reinterpret_cast<const qasymm8_t *>(input_ptr + x));
229             qasymm8_t tmp = 0;
230             if(act == ActivationLayerInfo::ActivationFunction::RELU)
231             {
232                 tmp = std::max(const_0, in);
233                 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
234             }
235             else if(act == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU)
236             {
237                 tmp = std::min(a, std::max(const_0, in));
238                 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
239             }
240             else if(act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
241             {
242                 tmp = std::min(a, std::max(b, in));
243                 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
244             }
245 #ifndef __aarch64__ // LUT-based implementation is used for aarch64 instead.
246             else if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
247             {
248                 float tmp_f = dequantize_qasymm8(in, qi_in);
249                 tmp_f       = 1.f / (1.f + std::exp(-tmp_f));
250                 tmp         = quantize_qasymm8(tmp_f, qi_out);
251             }
252 #endif // __aarch64__
253             else if(act == ActivationLayerInfo::ActivationFunction::TANH)
254             {
255                 float tmp_f = dequantize_qasymm8(in, qi_in);
256                 tmp_f       = a_f32 * std::tanh(b_f32 * tmp_f);
257                 tmp         = quantize_qasymm8(tmp_f, qi_out);
258             }
259 #ifndef __aarch64__ // LUT-based implementation is used for aarch64 instead.
260             else if(act == ActivationLayerInfo::ActivationFunction::HARD_SWISH)
261             {
262                 float tmp_f = dequantize_qasymm8(in, qi_in);
263                 tmp_f       = tmp_f * ((std::min(std::max((tmp_f + 3), 0.0f), 6.0f)) * 0.166666667f);
264                 tmp         = quantize_qasymm8(tmp_f, qi_out);
265             }
266             else if(act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU)
267             {
268                 float tmp_f = dequantize_qasymm8(in, qi_in);
269                 tmp_f       = tmp_f > 0 ? tmp_f : tmp_f * a_f32;
270                 tmp         = quantize_qasymm8(tmp_f, qi_out);
271             }
272             else if(act == ActivationLayerInfo::ActivationFunction::GELU)
273             {
274                 float tmp_f = dequantize_qasymm8(in, qi_in);
275                 tmp         = tmp_f * 0.5f * (1.0f + std::erff(in / 1.41421356237f));
276                 tmp         = quantize_qasymm8(tmp_f, qi_out);
277             }
278 #endif // __aarch64__
279             else
280             {
281                 ARM_COMPUTE_ERROR("Unsupported activation function");
282             }
283             *(output_ptr + x) = tmp;
284         }
285     },
286     input, output);
287 }
288 } // namespace cpu
289 } // namespace arm_compute
290