• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 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 #include "src/core/common/StdTypes.h"
31 #include "src/core/common/Validate.h"
32 
33 #include <arm_neon.h>
34 #include <cmath>
35 #include <cstddef>
36 
37 namespace arm_compute
38 {
39 namespace cpu
40 {
qasymm8_neon_activation(const ITensor * src,ITensor * dst,const ActivationLayerInfo & act_info,const Window & window)41 void qasymm8_neon_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
42 {
43     constexpr int                                 window_step_x  = 16;
44     const auto                                    window_start_x = static_cast<int>(window.x().start());
45     const auto                                    window_end_x   = static_cast<int>(window.x().end());
46     const ActivationLayerInfo::ActivationFunction act            = act_info.activation();
47 
48     Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
49     win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
50 
51     Iterator input(src, win_collapsed);
52     Iterator output(dst, win_collapsed);
53 
54     const UniformQuantizationInfo qi_in           = src->info()->quantization_info().uniform();
55     const UniformQuantizationInfo qi_out          = dst->info()->quantization_info().uniform();
56     const qasymm8x16_t            va              = vdupq_n_u8(quantize_qasymm8(act_info.a(), qi_in));
57     const qasymm8x16_t            vb              = vdupq_n_u8(quantize_qasymm8(act_info.b(), qi_in));
58     const qasymm8_t               a               = quantize_qasymm8(act_info.a(), qi_in);
59     const qasymm8_t               b               = quantize_qasymm8(act_info.b(), qi_in);
60     const qasymm8_t               const_0         = quantize_qasymm8(0.f, qi_in);
61     const qasymm8x16_t            vconst_0        = vdupq_n_u8(const_0);
62     const auto                    vconst_1        = vdupq_n_f32(1.f);
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_t *>(input.ptr());
81         const auto output_ptr = reinterpret_cast<qasymm8_t *>(output.ptr());
82 
83         wrapper::traits::neon_bitvector_t<qasymm8_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_u8(vconst_0, vin);
94                 // Re-quantize to new output space
95                 tmp = vmlaq_qasymm8(tmp, vs, vo);
96             }
97             else if(act == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU)
98             {
99                 // Perform activation
100                 tmp = vminq_u8(va, vmaxq_u8(vconst_0, vin));
101                 // Re-quantize to new output space
102                 tmp = vmlaq_qasymm8(tmp, vs, vo);
103             }
104             else if(act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
105             {
106                 // Perform activation
107                 tmp = vminq_u8(va, vmaxq_u8(vb, vin));
108                 // Re-quantize to new output space
109                 tmp = vmlaq_qasymm8(tmp, vs, vo);
110             }
111             else if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
112             {
113                 // De-quantize
114                 const auto vin_deq = vdequantize(vin, qi_in);
115                 // Perform activation
116                 const float32x4x4_t tmp_dep =
117                 {
118                     {
119                         wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[0])))),
120                         wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[1])))),
121                         wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[2])))),
122                         wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[3])))),
123                     }
124                 };
125                 // Re-quantize to new output space
126                 tmp = vquantize(tmp_dep, qi_out);
127             }
128             else if(act == ActivationLayerInfo::ActivationFunction::TANH)
129             {
130                 // De-quantize
131                 const auto vin_deq = vdequantize(vin, qi_in);
132                 // Perform activation
133                 const float32x4x4_t tmp_dep =
134                 {
135                     {
136                         wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[0], vb_f32))),
137                         wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[1], vb_f32))),
138                         wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[2], vb_f32))),
139                         wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[3], vb_f32))),
140                     }
141                 };
142                 // Re-quantize to new output space
143                 tmp = vquantize(tmp_dep, qi_out);
144             }
145             else if(act == ActivationLayerInfo::ActivationFunction::HARD_SWISH)
146             {
147                 // De-quantize
148                 const auto vin_deq = vdequantize(vin, qi_in);
149                 // Perform activation
150                 const float32x4x4_t tmp_dep =
151                 {
152                     {
153                         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))))),
154                         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))))),
155                         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))))),
156                         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))))),
157                     }
158                 };
159                 // Re-quantize to new output space
160                 tmp = vquantize(tmp_dep, qi_out);
161             }
162             else
163             {
164                 ARM_COMPUTE_ERROR("Unsupported activation function");
165             }
166             wrapper::vstore(output_ptr + x, tmp);
167         }
168 
169         // Compute left-over elements
170         for(; x < window_end_x; ++x)
171         {
172             qasymm8_t in  = *(reinterpret_cast<const qasymm8_t *>(input_ptr + x));
173             qasymm8_t tmp = 0;
174             if(act == ActivationLayerInfo::ActivationFunction::RELU)
175             {
176                 tmp = std::max(const_0, in);
177                 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
178             }
179             else if(act == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU)
180             {
181                 tmp = std::min(a, std::max(const_0, in));
182                 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
183             }
184             else if(act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
185             {
186                 tmp = std::min(a, std::max(b, in));
187                 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
188             }
189             else if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
190             {
191                 float tmp_f = dequantize_qasymm8(in, qi_in);
192                 tmp_f       = 1.f / (1.f + std::exp(-tmp_f));
193                 tmp         = quantize_qasymm8(tmp_f, qi_out);
194             }
195             else if(act == ActivationLayerInfo::ActivationFunction::TANH)
196             {
197                 float tmp_f = dequantize_qasymm8(in, qi_in);
198                 tmp_f       = a_f32 * std::tanh(b_f32 * tmp_f);
199                 tmp         = quantize_qasymm8(tmp_f, qi_out);
200             }
201             else if(act == ActivationLayerInfo::ActivationFunction::HARD_SWISH)
202             {
203                 float tmp_f = dequantize_qasymm8(in, qi_in);
204                 tmp_f       = tmp_f * ((std::min(std::max((tmp_f + 3), 0.0f), 6.0f)) * 0.166666667f);
205                 tmp         = quantize_qasymm8(tmp_f, qi_out);
206             }
207             else
208             {
209                 ARM_COMPUTE_ERROR("Unsupported activation function");
210             }
211             *(output_ptr + x) = tmp;
212         }
213     },
214     input, output);
215 }
216 } // namespace cpu
217 } // namespace arm_compute
218