xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/kernels/activation/generic/sve2/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 
28 #include <cmath>
29 #include <cstddef>
30 
31 #include "src/core/NEON/SVEAsymm.h"
32 #include "src/core/NEON/SVEMath.h"
33 #include <arm_sve.h>
34 
35 namespace arm_compute
36 {
37 namespace cpu
38 {
sve2_qasymm8_activation(const ITensor * src,ITensor * dst,const ActivationLayerInfo & act_info,const Window & window)39 void sve2_qasymm8_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
40 {
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 auto                    va              = svdup_n_u8(quantize_qasymm8(act_info.a(), qi_in));
54     const auto                    vb              = svdup_n_u8(quantize_qasymm8(act_info.b(), qi_in));
55     const auto                    const_0         = quantize_qasymm8(0.f, qi_in);
56     const auto                    vconst_0        = svdup_n_u8(const_0);
57     const auto                    vconst_1        = svdup_n_f32(1.f);
58     const auto                    va_f32          = svdup_n_f32(act_info.a());
59     const auto                    vb_f32          = svdup_n_f32(act_info.b());
60 
61 
62     // Initialise scale/offset for re-quantization
63     bool requant = true;
64     if(qi_in.scale == qi_out.scale && qi_in.offset == qi_out.offset)
65     {
66         requant = false;
67     }
68     float s  = qi_in.scale / qi_out.scale;
69     float o  = -qi_in.offset * s + qi_out.offset;
70     auto  vs = svdup_n_f32(s);
71     auto  vo = svdup_n_f32(o);
72 
73     // Initialise scale/offset for re-quantization with int32_t
74     const auto voffset_in = svdup_n_s32(qi_in.offset);
75     int32_t    s_s32      = round(s * (1 << 8), arm_compute::RoundingPolicy::TO_NEAREST_EVEN);
76     int32_t    o_s32      = round(o * (1 << 8), arm_compute::RoundingPolicy::TO_NEAREST_EVEN);
77     const auto vs_s32     = svdup_n_s32(s_s32);
78     const auto vo_s32     = svdup_n_s32(o_s32);
79 
80     // Initialise scale/offset for re-quantization for leaky relu
81     int32_t s_leaky_s32 = round(s * act_info.a() * (1 << 8), arm_compute::RoundingPolicy::TO_NEAREST_EVEN);
82     int32_t o_leaky_s32 = round((-qi_in.offset * s * act_info.a() + qi_out.offset) * (1 << 8),
83                                 arm_compute::RoundingPolicy::TO_NEAREST_EVEN);
84     const auto vs_leaky_s32 = svdup_n_s32(s_leaky_s32);
85     const auto vo_leaky_s32 = svdup_n_s32(o_leaky_s32);
86 
87     execute_window_loop(win_collapsed, [&](const Coordinates &)
88     {
89         const auto input_ptr  = reinterpret_cast<const uint8_t *>(input.ptr());
90         const auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr());
91 
92         svuint8_t tmp;
93 
94         int      x  = window_start_x;
95         svbool_t pg = svwhilelt_b8(x, window_end_x);
96         do
97         {
98             const auto vin = svld1_u8(pg, input_ptr + x);
99             if(act == ActivationLayerInfo::ActivationFunction::RELU)
100             {
101                 // Perform activation
102                 tmp = svmax_u8_z(pg, vconst_0, vin);
103                 // Re-quantize to new output space
104                 tmp = requant ? svmla_qasymm8_z(pg, tmp, vs, vo) : tmp;
105             }
106             else if(act == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU)
107             {
108                 // Perform activation
109                 tmp = svmin_u8_z(pg, va, svmax_u8_z(pg, vconst_0, vin));
110                 // Re-quantize to new output space
111                 tmp = requant ? svmla_qasymm8_z(pg, tmp, vs, vo) : tmp;
112             }
113             else if(act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
114             {
115                 // Perform activation
116                 tmp = svmin_u8_z(pg, va, svmax_u8_z(pg, vb, vin));
117                 // Re-quantize to new output space
118                 tmp = svmla_qasymm8_z(pg, tmp, vs, vo);
119             }
120             else if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
121             {
122                 // De-quantize
123                 const auto vin_deq = svdequantize_z(pg, vin, qi_in);
124                 // Perform activation
125                 const svfloat32x4_t tmp_dep = svcreate4_f32(svdiv_f32_z(pg, vconst_1, svadd_f32_z(pg, vconst_1, svexp_f32_z(pg, svneg_f32_z(pg, svget4_f32(vin_deq, 0))))),
126                                                             svdiv_f32_z(pg, vconst_1, svadd_f32_z(pg, vconst_1, svexp_f32_z(pg, svneg_f32_z(pg, svget4_f32(vin_deq, 1))))),
127                                                             svdiv_f32_z(pg, vconst_1, svadd_f32_z(pg, vconst_1, svexp_f32_z(pg, svneg_f32_z(pg, svget4_f32(vin_deq, 2))))),
128                                                             svdiv_f32_z(pg, vconst_1, svadd_f32_z(pg, vconst_1, svexp_f32_z(pg, svneg_f32_z(pg, svget4_f32(vin_deq, 3))))));
129 
130                 // Re-quantize to new output space
131                 tmp = svquantize_z(pg, tmp_dep, qi_out);
132             }
133             else if(act == ActivationLayerInfo::ActivationFunction::TANH)
134             {
135                 // De-quantize
136                 const auto vin_deq = svdequantize_z(pg, vin, qi_in);
137                 // Perform activation
138                 const svfloat32x4_t tmp_dep = svcreate4_f32(svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget4_f32(vin_deq, 0), vb_f32))),
139                                                             svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget4_f32(vin_deq, 1), vb_f32))),
140                                                             svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget4_f32(vin_deq, 2), vb_f32))),
141                                                             svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget4_f32(vin_deq, 3), vb_f32))));
142 
143                 // Re-quantize to new output space
144                 tmp = svquantize_z(pg, tmp_dep, qi_out);
145             }
146             else if(act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU)
147             {
148                 svbool_t    p0, p1, p2, p3;
149                 svint32x4_t tmp_dep;
150 
151                 // Expand to int32
152                 const svint32x4_t vin_s32 = svcreate4_s32(
153                                                 svreinterpret_s32_u32(svmovlb_u32(svmovlb_u16(vin))),
154                                                 svreinterpret_s32_u32(svmovlt_u32(svmovlb_u16(vin))),
155                                                 svreinterpret_s32_u32(svmovlb_u32(svmovlt_u16(vin))),
156                                                 svreinterpret_s32_u32(svmovlt_u32(svmovlt_u16(vin))));
157 
158                 // Compare elements to input offset
159                 if(qi_in.scale >= 0)
160                 {
161                     p0 = svcmplt_s32(pg, svget4_s32(vin_s32, 0), voffset_in);
162                     p1 = svcmplt_s32(pg, svget4_s32(vin_s32, 1), voffset_in);
163                     p2 = svcmplt_s32(pg, svget4_s32(vin_s32, 2), voffset_in);
164                     p3 = svcmplt_s32(pg, svget4_s32(vin_s32, 3), voffset_in);
165                 }
166                 else
167                 {
168                     p0 = svcmpgt_s32(pg, svget4_s32(vin_s32, 0), voffset_in);
169                     p1 = svcmpgt_s32(pg, svget4_s32(vin_s32, 1), voffset_in);
170                     p2 = svcmpgt_s32(pg, svget4_s32(vin_s32, 2), voffset_in);
171                     p3 = svcmpgt_s32(pg, svget4_s32(vin_s32, 3), voffset_in);
172                 }
173 
174                 // Multiply negative elements and requantize if necessary
175                 if(requant)
176                 {
177                     tmp_dep = svcreate4_s32(
178                                   svasr_n_s32_m(pg, svmla_s32_m(pg, svsel(p0, vo_leaky_s32, vo_s32), svget4_s32(vin_s32, 0), svsel(p0, vs_leaky_s32, vs_s32)), 8),
179                                   svasr_n_s32_m(pg, svmla_s32_m(pg, svsel(p1, vo_leaky_s32, vo_s32), svget4_s32(vin_s32, 1), svsel(p1, vs_leaky_s32, vs_s32)), 8),
180                                   svasr_n_s32_m(pg, svmla_s32_m(pg, svsel(p2, vo_leaky_s32, vo_s32), svget4_s32(vin_s32, 2), svsel(p2, vs_leaky_s32, vs_s32)), 8),
181                                   svasr_n_s32_m(pg, svmla_s32_m(pg, svsel(p3, vo_leaky_s32, vo_s32), svget4_s32(vin_s32, 3), svsel(p3, vs_leaky_s32, vs_s32)), 8));
182                 }
183                 else
184                 {
185                     tmp_dep = svcreate4_s32(
186                                   svasr_n_s32_m(p0, svmad_s32_m(p0, svget4_s32(vin_s32, 0), vs_leaky_s32, vo_leaky_s32), 8),
187                                   svasr_n_s32_m(p1, svmad_s32_m(p1, svget4_s32(vin_s32, 1), vs_leaky_s32, vo_leaky_s32), 8),
188                                   svasr_n_s32_m(p2, svmad_s32_m(p2, svget4_s32(vin_s32, 2), vs_leaky_s32, vo_leaky_s32), 8),
189                                   svasr_n_s32_m(p3, svmad_s32_m(p3, svget4_s32(vin_s32, 3), vs_leaky_s32, vo_leaky_s32), 8));
190                 }
191 
192                 // Convert uint32 vectors to uint16 vectors (with saturation)
193                 const auto v_low_u16  = svqxtunt_s32(svqxtunb_s32(svget4_s32(tmp_dep, 0)), svget4_s32(tmp_dep, 1));
194                 const auto v_high_u16 = svqxtunt_s32(svqxtunb_s32(svget4_s32(tmp_dep, 2)), svget4_s32(tmp_dep, 3));
195 
196                 // convert uint16 vectors to uint8 vectors (with saturation)
197                 tmp = svqxtnt_u16(svqxtnb_u16(v_low_u16), v_high_u16);
198             }
199             else
200             {
201                 ARM_COMPUTE_ERROR("Unsupported activation function");
202             }
203 
204             svst1_u8(pg, output_ptr + x, tmp);
205 
206             x += svcntb();
207             pg = svwhilelt_b8(x, window_end_x);
208 
209         }
210         while(svptest_any(svptrue_b8(), pg));
211 
212     },
213     input, output);
214 }
215 } // namespace cpu
216 } // namespace arm_compute
217