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