blob: 67d9e0a8ca15e590dbfd059069220ecd6ba108e0 [file] [log] [blame]
Michalis Spyrouc4d45552020-10-19 12:41:30 +01001/*
Pablo Marquez Tellod75cd8a2022-05-26 14:19:39 +01002 * Copyright (c) 2020-2022 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>
Pablo Marquez Tellod75cd8a2022-05-26 14:19:39 +010034#include <cstdint>
Michalis Spyrouc4d45552020-10-19 12:41:30 +010035
36namespace arm_compute
37{
38namespace cpu
39{
Dana Zlotnik32291712021-11-25 09:58:27 +020040void neon_qasymm8_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
Michalis Spyrouc4d45552020-10-19 12:41:30 +010041{
42 constexpr int window_step_x = 16;
43 const auto window_start_x = static_cast<int>(window.x().start());
44 const auto window_end_x = static_cast<int>(window.x().end());
45 const ActivationLayerInfo::ActivationFunction act = act_info.activation();
46
47 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
48 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
49
50 Iterator input(src, win_collapsed);
51 Iterator output(dst, win_collapsed);
52
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +000053 const UniformQuantizationInfo qi_in = src->info()->quantization_info().uniform();
54 const UniformQuantizationInfo qi_out = dst->info()->quantization_info().uniform();
55 const qasymm8x16_t va = vdupq_n_u8(quantize_qasymm8(act_info.a(), qi_in));
56 const qasymm8x16_t vb = vdupq_n_u8(quantize_qasymm8(act_info.b(), qi_in));
57 const qasymm8_t a = quantize_qasymm8(act_info.a(), qi_in);
58 const qasymm8_t b = quantize_qasymm8(act_info.b(), qi_in);
59 const qasymm8_t const_0 = quantize_qasymm8(0.f, qi_in);
60 const qasymm8x16_t vconst_0 = vdupq_n_u8(const_0);
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +000061#ifndef __aarch64__
Viet-Hoa Do29db3d22022-08-10 11:56:49 +010062 const auto vconst_1 = vdupq_n_f32(1.f);
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +000063 const auto vconst_0_f32 = vdupq_n_f32(0);
64#endif // __aarch64__
Pablo Marquez Tellod75cd8a2022-05-26 14:19:39 +010065 const float32x4_t va_f32 = vdupq_n_f32(act_info.a());
66 const float32x4_t vb_f32 = vdupq_n_f32(act_info.b());
67 const float a_f32 = act_info.a();
68 const float b_f32 = act_info.b();
Michalis Spyrouc4d45552020-10-19 12:41:30 +010069
Viet-Hoa Dob042e392022-06-21 15:56:15 +010070#ifndef __aarch64__
71 const auto const_6_f32 = vdupq_n_f32(6.f);
72 const auto const_0_f32 = vdupq_n_f32(0.f);
73 const auto const_3_f32 = vdupq_n_f32(3.f);
74 const auto const_inv_6_f32 = vdupq_n_f32(0.166666667f);
75#endif // __aarch64__
76
Michalis Spyrouc4d45552020-10-19 12:41:30 +010077 // Initialise scale/offset for re-quantization
78 float s = qi_in.scale / qi_out.scale;
79 float o = -qi_in.offset * s + qi_out.offset;
80 float32x4_t vs = vdupq_n_f32(s);
81 float32x4_t vo = vdupq_n_f32(o);
82
83 execute_window_loop(win_collapsed, [&](const Coordinates &)
84 {
85 const auto input_ptr = reinterpret_cast<const qasymm8_t *>(input.ptr());
86 const auto output_ptr = reinterpret_cast<qasymm8_t *>(output.ptr());
87
88 wrapper::traits::neon_bitvector_t<qasymm8_t, wrapper::traits::BitWidth::W128> tmp;
89
90 // Compute S elements per iteration
91 int x = window_start_x;
92 for(; x <= (window_end_x - window_step_x); x += window_step_x)
93 {
94 const auto vin = wrapper::vloadq(input_ptr + x);
95 if(act == ActivationLayerInfo::ActivationFunction::RELU)
96 {
97 // Perform activation
98 tmp = vmaxq_u8(vconst_0, vin);
99 // Re-quantize to new output space
100 tmp = vmlaq_qasymm8(tmp, vs, vo);
101 }
102 else if(act == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU)
103 {
104 // Perform activation
105 tmp = vminq_u8(va, vmaxq_u8(vconst_0, vin));
106 // Re-quantize to new output space
107 tmp = vmlaq_qasymm8(tmp, vs, vo);
108 }
109 else if(act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
110 {
111 // Perform activation
112 tmp = vminq_u8(va, vmaxq_u8(vb, vin));
113 // Re-quantize to new output space
114 tmp = vmlaq_qasymm8(tmp, vs, vo);
115 }
Viet-Hoa Do29db3d22022-08-10 11:56:49 +0100116#ifndef __aarch64__ // LUT-based implementation is used for aarch64 instead.
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100117 else if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
118 {
119 // De-quantize
120 const auto vin_deq = vdequantize(vin, qi_in);
121 // Perform activation
122 const float32x4x4_t tmp_dep =
123 {
124 {
125 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[0])))),
126 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[1])))),
127 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[2])))),
128 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[3])))),
129 }
130 };
131 // Re-quantize to new output space
132 tmp = vquantize(tmp_dep, qi_out);
133 }
Viet-Hoa Do29db3d22022-08-10 11:56:49 +0100134#endif // __aarch64__
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100135 else if(act == ActivationLayerInfo::ActivationFunction::TANH)
136 {
137 // De-quantize
138 const auto vin_deq = vdequantize(vin, qi_in);
139 // Perform activation
140 const float32x4x4_t tmp_dep =
141 {
142 {
143 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[0], vb_f32))),
144 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[1], vb_f32))),
145 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[2], vb_f32))),
146 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[3], vb_f32))),
147 }
148 };
149 // Re-quantize to new output space
150 tmp = vquantize(tmp_dep, qi_out);
151 }
Viet-Hoa Dob042e392022-06-21 15:56:15 +0100152#ifndef __aarch64__ // LUT-based implementation is used for aarch64 instead.
153 else if(act == ActivationLayerInfo::ActivationFunction::HARD_SWISH)
154 {
155 // De-quantize
156 const auto vin_deq = vdequantize(vin, qi_in);
157 // Perform activation
158 const float32x4x4_t tmp_dep =
159 {
160 {
161 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))))),
162 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))))),
163 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))))),
164 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))))),
165 }
166 };
167 // Re-quantize to new output space
168 tmp = vquantize(tmp_dep, qi_out);
169 }
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000170 else if(act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU)
171 {
172 const auto vin_deq = vdequantize(vin, qi_in);
173
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000174 const uint32x4x4_t pos_mask =
175 {
176 {
177 wrapper::vcgt(vin_deq.val[0], vconst_0_f32),
178 wrapper::vcgt(vin_deq.val[1], vconst_0_f32),
179 wrapper::vcgt(vin_deq.val[2], vconst_0_f32),
180 wrapper::vcgt(vin_deq.val[3], vconst_0_f32),
181 }
182 };
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000183
184 const float32x4x4_t tmp_dep =
185 {
186 {
187 wrapper::vbsl(pos_mask.val[0], vin_deq.val[0], wrapper::vmul(va_f32, vin_deq.val[0])),
188 wrapper::vbsl(pos_mask.val[1], vin_deq.val[1], wrapper::vmul(va_f32, vin_deq.val[1])),
189 wrapper::vbsl(pos_mask.val[2], vin_deq.val[2], wrapper::vmul(va_f32, vin_deq.val[2])),
190 wrapper::vbsl(pos_mask.val[3], vin_deq.val[3], wrapper::vmul(va_f32, vin_deq.val[3])),
191 }
192 };
193
194 tmp = vquantize(tmp_dep, qi_out);
195 }
Viet-Hoa Dob042e392022-06-21 15:56:15 +0100196#endif // __aarch64__
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100197 else
198 {
199 ARM_COMPUTE_ERROR("Unsupported activation function");
200 }
201 wrapper::vstore(output_ptr + x, tmp);
202 }
203
204 // Compute left-over elements
205 for(; x < window_end_x; ++x)
206 {
207 qasymm8_t in = *(reinterpret_cast<const qasymm8_t *>(input_ptr + x));
208 qasymm8_t tmp = 0;
209 if(act == ActivationLayerInfo::ActivationFunction::RELU)
210 {
211 tmp = std::max(const_0, in);
212 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
213 }
214 else if(act == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU)
215 {
216 tmp = std::min(a, std::max(const_0, in));
217 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
218 }
219 else if(act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
220 {
221 tmp = std::min(a, std::max(b, in));
222 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
223 }
Viet-Hoa Do29db3d22022-08-10 11:56:49 +0100224#ifndef __aarch64__ // LUT-based implementation is used for aarch64 instead.
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100225 else if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
226 {
227 float tmp_f = dequantize_qasymm8(in, qi_in);
228 tmp_f = 1.f / (1.f + std::exp(-tmp_f));
229 tmp = quantize_qasymm8(tmp_f, qi_out);
230 }
Viet-Hoa Do29db3d22022-08-10 11:56:49 +0100231#endif // __aarch64__
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100232 else if(act == ActivationLayerInfo::ActivationFunction::TANH)
233 {
234 float tmp_f = dequantize_qasymm8(in, qi_in);
235 tmp_f = a_f32 * std::tanh(b_f32 * tmp_f);
236 tmp = quantize_qasymm8(tmp_f, qi_out);
237 }
Viet-Hoa Dob042e392022-06-21 15:56:15 +0100238#ifndef __aarch64__ // LUT-based implementation is used for aarch64 instead.
239 else if(act == ActivationLayerInfo::ActivationFunction::HARD_SWISH)
240 {
241 float tmp_f = dequantize_qasymm8(in, qi_in);
242 tmp_f = tmp_f * ((std::min(std::max((tmp_f + 3), 0.0f), 6.0f)) * 0.166666667f);
243 tmp = quantize_qasymm8(tmp_f, qi_out);
244 }
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000245 else if(act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU)
246 {
247 float tmp_f = dequantize_qasymm8(in, qi_in);
248 tmp_f = tmp_f > 0 ? tmp_f : tmp_f * a_f32;
249 tmp = quantize_qasymm8(tmp_f, qi_out);
250 }
Viet-Hoa Dob042e392022-06-21 15:56:15 +0100251#endif // __aarch64__
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