blob: c61facc9b08592f85b1809d6548397a3c1c996a1 [file] [log] [blame]
Michalis Spyrouc4d45552020-10-19 12:41:30 +01001/*
Pablo Marquez Tello20cfa452023-03-20 16:29:21 +00002 * Copyright (c) 2020-2023 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
Matthew Benthamf1aeab92023-05-30 13:35:34 +000025#include "arm_compute/core/ActivationLayerInfo.h"
Michalis Spyrouc4d45552020-10-19 12:41:30 +010026#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/Window.h"
28#include "src/core/NEON/NEAsymm.h"
29#include "src/core/NEON/NEMath.h"
30#include "src/core/NEON/wrapper/wrapper.h"
Michalis Spyrouc4d45552020-10-19 12:41:30 +010031
32#include <arm_neon.h>
33#include <cmath>
34#include <cstddef>
Pablo Marquez Tellod75cd8a2022-05-26 14:19:39 +010035#include <cstdint>
Michalis Spyrouc4d45552020-10-19 12:41:30 +010036
37namespace arm_compute
38{
39namespace cpu
40{
Dana Zlotnik32291712021-11-25 09:58:27 +020041void neon_qasymm8_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
Michalis Spyrouc4d45552020-10-19 12:41:30 +010042{
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
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +000054 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);
Murray Kornelsen926f5022022-07-13 21:22:39 -040062 const auto vconst_1 = vdupq_n_f32(1.f);
63
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +000064#ifndef __aarch64__
65 const auto vconst_0_f32 = vdupq_n_f32(0);
Murray Kornelsen926f5022022-07-13 21:22:39 -040066#else // #ifndef __aarch64__
67 const auto const_inv_2 = vdupq_n_f32(0.5f);
68 const auto const_inv_sqrt_2 = vdupq_n_f32(0.70710678118f);
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +000069#endif // __aarch64__
Pablo Marquez Tellod75cd8a2022-05-26 14:19:39 +010070 const float32x4_t va_f32 = vdupq_n_f32(act_info.a());
71 const float32x4_t vb_f32 = vdupq_n_f32(act_info.b());
72 const float a_f32 = act_info.a();
73 const float b_f32 = act_info.b();
Michalis Spyrouc4d45552020-10-19 12:41:30 +010074
Viet-Hoa Dob042e392022-06-21 15:56:15 +010075#ifndef __aarch64__
76 const auto const_6_f32 = vdupq_n_f32(6.f);
77 const auto const_0_f32 = vdupq_n_f32(0.f);
78 const auto const_3_f32 = vdupq_n_f32(3.f);
79 const auto const_inv_6_f32 = vdupq_n_f32(0.166666667f);
80#endif // __aarch64__
81
Michalis Spyrouc4d45552020-10-19 12:41:30 +010082 // Initialise scale/offset for re-quantization
83 float s = qi_in.scale / qi_out.scale;
84 float o = -qi_in.offset * s + qi_out.offset;
85 float32x4_t vs = vdupq_n_f32(s);
86 float32x4_t vo = vdupq_n_f32(o);
87
88 execute_window_loop(win_collapsed, [&](const Coordinates &)
89 {
90 const auto input_ptr = reinterpret_cast<const qasymm8_t *>(input.ptr());
91 const auto output_ptr = reinterpret_cast<qasymm8_t *>(output.ptr());
92
93 wrapper::traits::neon_bitvector_t<qasymm8_t, wrapper::traits::BitWidth::W128> tmp;
94
95 // Compute S elements per iteration
96 int x = window_start_x;
97 for(; x <= (window_end_x - window_step_x); x += window_step_x)
98 {
99 const auto vin = wrapper::vloadq(input_ptr + x);
100 if(act == ActivationLayerInfo::ActivationFunction::RELU)
101 {
102 // Perform activation
103 tmp = vmaxq_u8(vconst_0, vin);
104 // Re-quantize to new output space
Pablo Marquez Tello20cfa452023-03-20 16:29:21 +0000105 tmp = vmlaq_qasymm8<RoundingPolicy::TO_NEAREST_UP>(tmp, vs, vo);
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100106 }
107 else if(act == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU)
108 {
109 // Perform activation
110 tmp = vminq_u8(va, vmaxq_u8(vconst_0, vin));
111 // Re-quantize to new output space
Pablo Marquez Tello20cfa452023-03-20 16:29:21 +0000112 tmp = vmlaq_qasymm8<RoundingPolicy::TO_NEAREST_UP>(tmp, vs, vo);
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100113 }
114 else if(act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
115 {
116 // Perform activation
117 tmp = vminq_u8(va, vmaxq_u8(vb, vin));
118 // Re-quantize to new output space
Pablo Marquez Tello20cfa452023-03-20 16:29:21 +0000119 tmp = vmlaq_qasymm8<RoundingPolicy::TO_NEAREST_UP>(tmp, vs, vo);
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100120 }
Viet-Hoa Do29db3d22022-08-10 11:56:49 +0100121#ifndef __aarch64__ // LUT-based implementation is used for aarch64 instead.
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100122 else if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
123 {
124 // De-quantize
125 const auto vin_deq = vdequantize(vin, qi_in);
126 // Perform activation
127 const float32x4x4_t tmp_dep =
128 {
129 {
130 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[0])))),
131 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[1])))),
132 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[2])))),
133 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[3])))),
134 }
135 };
136 // Re-quantize to new output space
137 tmp = vquantize(tmp_dep, qi_out);
138 }
Viet-Hoa Do29db3d22022-08-10 11:56:49 +0100139#endif // __aarch64__
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100140 else if(act == ActivationLayerInfo::ActivationFunction::TANH)
141 {
142 // De-quantize
143 const auto vin_deq = vdequantize(vin, qi_in);
144 // Perform activation
145 const float32x4x4_t tmp_dep =
146 {
147 {
148 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[0], vb_f32))),
149 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[1], vb_f32))),
150 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[2], vb_f32))),
151 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[3], vb_f32))),
152 }
153 };
154 // Re-quantize to new output space
155 tmp = vquantize(tmp_dep, qi_out);
156 }
Viet-Hoa Dob042e392022-06-21 15:56:15 +0100157#ifndef __aarch64__ // LUT-based implementation is used for aarch64 instead.
158 else if(act == ActivationLayerInfo::ActivationFunction::HARD_SWISH)
159 {
160 // De-quantize
161 const auto vin_deq = vdequantize(vin, qi_in);
162 // Perform activation
163 const float32x4x4_t tmp_dep =
164 {
165 {
166 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))))),
167 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))))),
168 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))))),
169 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))))),
170 }
171 };
172 // Re-quantize to new output space
173 tmp = vquantize(tmp_dep, qi_out);
174 }
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000175 else if(act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU)
176 {
177 const auto vin_deq = vdequantize(vin, qi_in);
178
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000179 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 };
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000188
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 }
Murray Kornelsen926f5022022-07-13 21:22:39 -0400201#else // #ifndef __aarch64__
202 else if (act == ActivationLayerInfo::ActivationFunction::GELU)
203 {
204 const auto vin_deq = vdequantize(vin, qi_in);
205 // Perform activation
206 const float32x4x4_t tmp_dep =
207 {
208 {
209 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))))),
210 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))))),
211 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))))),
212 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))))),
213 }
214 };
215 // Re-quantize to new output space
216 tmp = vquantize(tmp_dep, qi_out);
217 }
Viet-Hoa Dob042e392022-06-21 15:56:15 +0100218#endif // __aarch64__
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100219 else
220 {
221 ARM_COMPUTE_ERROR("Unsupported activation function");
222 }
223 wrapper::vstore(output_ptr + x, tmp);
224 }
225
226 // Compute left-over elements
227 for(; x < window_end_x; ++x)
228 {
229 qasymm8_t in = *(reinterpret_cast<const qasymm8_t *>(input_ptr + x));
230 qasymm8_t tmp = 0;
231 if(act == ActivationLayerInfo::ActivationFunction::RELU)
232 {
233 tmp = std::max(const_0, in);
Pablo Marquez Tello20cfa452023-03-20 16:29:21 +0000234 tmp = utility::clamp<int32_t, qasymm8_t>(support::cpp11::lround(tmp * s + o));
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100235 }
236 else if(act == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU)
237 {
238 tmp = std::min(a, std::max(const_0, in));
Pablo Marquez Tello20cfa452023-03-20 16:29:21 +0000239 tmp = utility::clamp<int32_t, qasymm8_t>(support::cpp11::lround(tmp * s + o));
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100240 }
241 else if(act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
242 {
243 tmp = std::min(a, std::max(b, in));
Pablo Marquez Tello20cfa452023-03-20 16:29:21 +0000244 tmp = utility::clamp<int32_t, qasymm8_t>(support::cpp11::lround(tmp * s + o));
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100245 }
Viet-Hoa Do29db3d22022-08-10 11:56:49 +0100246#ifndef __aarch64__ // LUT-based implementation is used for aarch64 instead.
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100247 else if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
248 {
249 float tmp_f = dequantize_qasymm8(in, qi_in);
250 tmp_f = 1.f / (1.f + std::exp(-tmp_f));
251 tmp = quantize_qasymm8(tmp_f, qi_out);
252 }
Viet-Hoa Do29db3d22022-08-10 11:56:49 +0100253#endif // __aarch64__
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100254 else if(act == ActivationLayerInfo::ActivationFunction::TANH)
255 {
256 float tmp_f = dequantize_qasymm8(in, qi_in);
257 tmp_f = a_f32 * std::tanh(b_f32 * tmp_f);
258 tmp = quantize_qasymm8(tmp_f, qi_out);
259 }
Viet-Hoa Dob042e392022-06-21 15:56:15 +0100260#ifndef __aarch64__ // LUT-based implementation is used for aarch64 instead.
261 else if(act == ActivationLayerInfo::ActivationFunction::HARD_SWISH)
262 {
263 float tmp_f = dequantize_qasymm8(in, qi_in);
264 tmp_f = tmp_f * ((std::min(std::max((tmp_f + 3), 0.0f), 6.0f)) * 0.166666667f);
265 tmp = quantize_qasymm8(tmp_f, qi_out);
266 }
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000267 else if(act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU)
268 {
269 float tmp_f = dequantize_qasymm8(in, qi_in);
270 tmp_f = tmp_f > 0 ? tmp_f : tmp_f * a_f32;
271 tmp = quantize_qasymm8(tmp_f, qi_out);
272 }
Murray Kornelsen926f5022022-07-13 21:22:39 -0400273 else if(act == ActivationLayerInfo::ActivationFunction::GELU)
274 {
275 float tmp_f = dequantize_qasymm8(in, qi_in);
276 tmp = tmp_f * 0.5f * (1.0f + std::erff(in / 1.41421356237f));
277 tmp = quantize_qasymm8(tmp_f, qi_out);
278 }
Viet-Hoa Dob042e392022-06-21 15:56:15 +0100279#endif // __aarch64__
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100280 else
281 {
282 ARM_COMPUTE_ERROR("Unsupported activation function");
283 }
284 *(output_ptr + x) = tmp;
285 }
286 },
287 input, output);
288}
289} // namespace cpu
290} // namespace arm_compute