blob: 62e329e691728351bd345519ae9e397e2351dedd [file] [log] [blame]
Michalis Spyrouc4d45552020-10-19 12:41:30 +01001/*
Georgios Pinitas70eb53b2021-01-06 19:42:21 +00002 * Copyright (c) 2020-2021 Arm Limited.
Michalis Spyrouc4d45552020-10-19 12:41:30 +01003 *
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"
Michalis Spyrouc4d45552020-10-19 12:41:30 +010030
31#include <arm_neon.h>
32#include <cmath>
33#include <cstddef>
34
35namespace arm_compute
36{
37namespace cpu
38{
Dana Zlotnik32291712021-11-25 09:58:27 +020039void neon_qasymm8_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
Michalis Spyrouc4d45552020-10-19 12:41:30 +010040{
41 constexpr int window_step_x = 16;
42 const auto window_start_x = static_cast<int>(window.x().start());
43 const auto window_end_x = static_cast<int>(window.x().end());
44 const ActivationLayerInfo::ActivationFunction act = act_info.activation();
45
46 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
47 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
48
49 Iterator input(src, win_collapsed);
50 Iterator output(dst, win_collapsed);
51
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +000052 const UniformQuantizationInfo qi_in = src->info()->quantization_info().uniform();
53 const UniformQuantizationInfo qi_out = dst->info()->quantization_info().uniform();
54 const qasymm8x16_t va = vdupq_n_u8(quantize_qasymm8(act_info.a(), qi_in));
55 const qasymm8x16_t vb = vdupq_n_u8(quantize_qasymm8(act_info.b(), qi_in));
56 const qasymm8_t a = quantize_qasymm8(act_info.a(), qi_in);
57 const qasymm8_t b = quantize_qasymm8(act_info.b(), qi_in);
58 const qasymm8_t const_0 = quantize_qasymm8(0.f, qi_in);
59 const qasymm8x16_t vconst_0 = vdupq_n_u8(const_0);
60 const auto vconst_1 = vdupq_n_f32(1.f);
61#ifndef __aarch64__
62 const auto vconst_0_f32 = vdupq_n_f32(0);
63#endif // __aarch64__
64 const float32x4_t va_f32 = vdupq_n_f32(act_info.a());
65 const float32x4_t vb_f32 = vdupq_n_f32(act_info.b());
66 const float a_f32 = act_info.a();
67 const float b_f32 = act_info.b();
68 const auto const_6_f32 = vdupq_n_f32(6.f);
69 const auto const_0_f32 = vdupq_n_f32(0.f);
70 const auto const_3_f32 = vdupq_n_f32(3.f);
71 const auto const_inv_6_f32 = vdupq_n_f32(0.166666667f);
Michalis Spyrouc4d45552020-10-19 12:41:30 +010072
73 // Initialise scale/offset for re-quantization
74 float s = qi_in.scale / qi_out.scale;
75 float o = -qi_in.offset * s + qi_out.offset;
76 float32x4_t vs = vdupq_n_f32(s);
77 float32x4_t vo = vdupq_n_f32(o);
78
79 execute_window_loop(win_collapsed, [&](const Coordinates &)
80 {
81 const auto input_ptr = reinterpret_cast<const qasymm8_t *>(input.ptr());
82 const auto output_ptr = reinterpret_cast<qasymm8_t *>(output.ptr());
83
84 wrapper::traits::neon_bitvector_t<qasymm8_t, wrapper::traits::BitWidth::W128> tmp;
85
86 // Compute S elements per iteration
87 int x = window_start_x;
88 for(; x <= (window_end_x - window_step_x); x += window_step_x)
89 {
90 const auto vin = wrapper::vloadq(input_ptr + x);
91 if(act == ActivationLayerInfo::ActivationFunction::RELU)
92 {
93 // Perform activation
94 tmp = vmaxq_u8(vconst_0, vin);
95 // Re-quantize to new output space
96 tmp = vmlaq_qasymm8(tmp, vs, vo);
97 }
98 else if(act == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU)
99 {
100 // Perform activation
101 tmp = vminq_u8(va, vmaxq_u8(vconst_0, vin));
102 // Re-quantize to new output space
103 tmp = vmlaq_qasymm8(tmp, vs, vo);
104 }
105 else if(act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
106 {
107 // Perform activation
108 tmp = vminq_u8(va, vmaxq_u8(vb, vin));
109 // Re-quantize to new output space
110 tmp = vmlaq_qasymm8(tmp, vs, vo);
111 }
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(tmp_dep, qi_out);
128 }
129 else if(act == ActivationLayerInfo::ActivationFunction::TANH)
130 {
131 // De-quantize
132 const auto vin_deq = vdequantize(vin, qi_in);
133 // Perform activation
134 const float32x4x4_t tmp_dep =
135 {
136 {
137 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[0], vb_f32))),
138 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[1], vb_f32))),
139 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[2], vb_f32))),
140 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[3], vb_f32))),
141 }
142 };
143 // Re-quantize to new output space
144 tmp = vquantize(tmp_dep, qi_out);
145 }
146 else if(act == ActivationLayerInfo::ActivationFunction::HARD_SWISH)
147 {
148 // De-quantize
149 const auto vin_deq = vdequantize(vin, qi_in);
150 // Perform activation
151 const float32x4x4_t tmp_dep =
152 {
153 {
154 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))))),
155 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))))),
156 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))))),
157 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))))),
158 }
159 };
160 // Re-quantize to new output space
161 tmp = vquantize(tmp_dep, qi_out);
162 }
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000163 else if(act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU)
164 {
165 const auto vin_deq = vdequantize(vin, qi_in);
166
167#ifdef __aarch64__
168 const uint32x4x4_t pos_mask =
169 {
170 {
171 wrapper::vcgtz(vin_deq.val[0]),
172 wrapper::vcgtz(vin_deq.val[1]),
173 wrapper::vcgtz(vin_deq.val[2]),
174 wrapper::vcgtz(vin_deq.val[3]),
175 }
176 };
177#else // __aarch64__
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#endif // __aarch64__
188
189 const float32x4x4_t tmp_dep =
190 {
191 {
192 wrapper::vbsl(pos_mask.val[0], vin_deq.val[0], wrapper::vmul(va_f32, vin_deq.val[0])),
193 wrapper::vbsl(pos_mask.val[1], vin_deq.val[1], wrapper::vmul(va_f32, vin_deq.val[1])),
194 wrapper::vbsl(pos_mask.val[2], vin_deq.val[2], wrapper::vmul(va_f32, vin_deq.val[2])),
195 wrapper::vbsl(pos_mask.val[3], vin_deq.val[3], wrapper::vmul(va_f32, vin_deq.val[3])),
196 }
197 };
198
199 tmp = vquantize(tmp_dep, qi_out);
200 }
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100201 else
202 {
203 ARM_COMPUTE_ERROR("Unsupported activation function");
204 }
205 wrapper::vstore(output_ptr + x, tmp);
206 }
207
208 // Compute left-over elements
209 for(; x < window_end_x; ++x)
210 {
211 qasymm8_t in = *(reinterpret_cast<const qasymm8_t *>(input_ptr + x));
212 qasymm8_t tmp = 0;
213 if(act == ActivationLayerInfo::ActivationFunction::RELU)
214 {
215 tmp = std::max(const_0, in);
216 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
217 }
218 else if(act == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU)
219 {
220 tmp = std::min(a, std::max(const_0, in));
221 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
222 }
223 else if(act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
224 {
225 tmp = std::min(a, std::max(b, in));
226 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
227 }
228 else if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
229 {
230 float tmp_f = dequantize_qasymm8(in, qi_in);
231 tmp_f = 1.f / (1.f + std::exp(-tmp_f));
232 tmp = quantize_qasymm8(tmp_f, qi_out);
233 }
234 else if(act == ActivationLayerInfo::ActivationFunction::TANH)
235 {
236 float tmp_f = dequantize_qasymm8(in, qi_in);
237 tmp_f = a_f32 * std::tanh(b_f32 * tmp_f);
238 tmp = quantize_qasymm8(tmp_f, qi_out);
239 }
240 else if(act == ActivationLayerInfo::ActivationFunction::HARD_SWISH)
241 {
242 float tmp_f = dequantize_qasymm8(in, qi_in);
243 tmp_f = tmp_f * ((std::min(std::max((tmp_f + 3), 0.0f), 6.0f)) * 0.166666667f);
244 tmp = quantize_qasymm8(tmp_f, qi_out);
245 }
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000246 else if(act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU)
247 {
248 float tmp_f = dequantize_qasymm8(in, qi_in);
249 tmp_f = tmp_f > 0 ? tmp_f : tmp_f * a_f32;
250 tmp = quantize_qasymm8(tmp_f, qi_out);
251 }
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100252 else
253 {
254 ARM_COMPUTE_ERROR("Unsupported activation function");
255 }
256 *(output_ptr + x) = tmp;
257 }
258 },
259 input, output);
260}
261} // namespace cpu
262} // namespace arm_compute