blob: 7b26441824fa6016c1ea37f9c552426fa9ea2e4d [file] [log] [blame]
Michalis Spyrouc4d45552020-10-19 12:41:30 +01001/*
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
37namespace arm_compute
38{
39namespace cpu
40{
41void 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
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);
62 const auto vconst_1 = vdupq_n_f32(1.f);
63#ifndef __aarch64__
64 const auto vconst_0_f32 = vdupq_n_f32(0);
65#endif // __aarch64__
66 const float32x4_t va_f32 = vdupq_n_f32(act_info.a());
67 const float32x4_t vb_f32 = vdupq_n_f32(act_info.b());
68 const float a_f32 = act_info.a();
69 const float b_f32 = act_info.b();
70 const auto const_6_f32 = vdupq_n_f32(6.f);
71 const auto const_0_f32 = vdupq_n_f32(0.f);
72 const auto const_3_f32 = vdupq_n_f32(3.f);
73 const auto const_inv_6_f32 = vdupq_n_f32(0.166666667f);
Michalis Spyrouc4d45552020-10-19 12:41:30 +010074
75 // Initialise scale/offset for re-quantization
76 float s = qi_in.scale / qi_out.scale;
77 float o = -qi_in.offset * s + qi_out.offset;
78 float32x4_t vs = vdupq_n_f32(s);
79 float32x4_t vo = vdupq_n_f32(o);
80
81 execute_window_loop(win_collapsed, [&](const Coordinates &)
82 {
83 const auto input_ptr = reinterpret_cast<const qasymm8_t *>(input.ptr());
84 const auto output_ptr = reinterpret_cast<qasymm8_t *>(output.ptr());
85
86 wrapper::traits::neon_bitvector_t<qasymm8_t, wrapper::traits::BitWidth::W128> tmp;
87
88 // Compute S elements per iteration
89 int x = window_start_x;
90 for(; x <= (window_end_x - window_step_x); x += window_step_x)
91 {
92 const auto vin = wrapper::vloadq(input_ptr + x);
93 if(act == ActivationLayerInfo::ActivationFunction::RELU)
94 {
95 // Perform activation
96 tmp = vmaxq_u8(vconst_0, vin);
97 // Re-quantize to new output space
98 tmp = vmlaq_qasymm8(tmp, vs, vo);
99 }
100 else if(act == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU)
101 {
102 // Perform activation
103 tmp = vminq_u8(va, vmaxq_u8(vconst_0, vin));
104 // Re-quantize to new output space
105 tmp = vmlaq_qasymm8(tmp, vs, vo);
106 }
107 else if(act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
108 {
109 // Perform activation
110 tmp = vminq_u8(va, vmaxq_u8(vb, vin));
111 // Re-quantize to new output space
112 tmp = vmlaq_qasymm8(tmp, vs, vo);
113 }
114 else if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
115 {
116 // De-quantize
117 const auto vin_deq = vdequantize(vin, qi_in);
118 // Perform activation
119 const float32x4x4_t tmp_dep =
120 {
121 {
122 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[0])))),
123 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[1])))),
124 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[2])))),
125 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[3])))),
126 }
127 };
128 // Re-quantize to new output space
129 tmp = vquantize(tmp_dep, qi_out);
130 }
131 else if(act == ActivationLayerInfo::ActivationFunction::TANH)
132 {
133 // De-quantize
134 const auto vin_deq = vdequantize(vin, qi_in);
135 // Perform activation
136 const float32x4x4_t tmp_dep =
137 {
138 {
139 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[0], vb_f32))),
140 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[1], vb_f32))),
141 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[2], vb_f32))),
142 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[3], vb_f32))),
143 }
144 };
145 // Re-quantize to new output space
146 tmp = vquantize(tmp_dep, qi_out);
147 }
148 else if(act == ActivationLayerInfo::ActivationFunction::HARD_SWISH)
149 {
150 // De-quantize
151 const auto vin_deq = vdequantize(vin, qi_in);
152 // Perform activation
153 const float32x4x4_t tmp_dep =
154 {
155 {
156 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))))),
157 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))))),
158 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))))),
159 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))))),
160 }
161 };
162 // Re-quantize to new output space
163 tmp = vquantize(tmp_dep, qi_out);
164 }
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000165 else if(act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU)
166 {
167 const auto vin_deq = vdequantize(vin, qi_in);
168
169#ifdef __aarch64__
170 const uint32x4x4_t pos_mask =
171 {
172 {
173 wrapper::vcgtz(vin_deq.val[0]),
174 wrapper::vcgtz(vin_deq.val[1]),
175 wrapper::vcgtz(vin_deq.val[2]),
176 wrapper::vcgtz(vin_deq.val[3]),
177 }
178 };
179#else // __aarch64__
180 const uint32x4x4_t pos_mask =
181 {
182 {
183 wrapper::vcgt(vin_deq.val[0], vconst_0_f32),
184 wrapper::vcgt(vin_deq.val[1], vconst_0_f32),
185 wrapper::vcgt(vin_deq.val[2], vconst_0_f32),
186 wrapper::vcgt(vin_deq.val[3], vconst_0_f32),
187 }
188 };
189#endif // __aarch64__
190
191 const float32x4x4_t tmp_dep =
192 {
193 {
194 wrapper::vbsl(pos_mask.val[0], vin_deq.val[0], wrapper::vmul(va_f32, vin_deq.val[0])),
195 wrapper::vbsl(pos_mask.val[1], vin_deq.val[1], wrapper::vmul(va_f32, vin_deq.val[1])),
196 wrapper::vbsl(pos_mask.val[2], vin_deq.val[2], wrapper::vmul(va_f32, vin_deq.val[2])),
197 wrapper::vbsl(pos_mask.val[3], vin_deq.val[3], wrapper::vmul(va_f32, vin_deq.val[3])),
198 }
199 };
200
201 tmp = vquantize(tmp_dep, qi_out);
202 }
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100203 else
204 {
205 ARM_COMPUTE_ERROR("Unsupported activation function");
206 }
207 wrapper::vstore(output_ptr + x, tmp);
208 }
209
210 // Compute left-over elements
211 for(; x < window_end_x; ++x)
212 {
213 qasymm8_t in = *(reinterpret_cast<const qasymm8_t *>(input_ptr + x));
214 qasymm8_t tmp = 0;
215 if(act == ActivationLayerInfo::ActivationFunction::RELU)
216 {
217 tmp = std::max(const_0, in);
218 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
219 }
220 else if(act == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU)
221 {
222 tmp = std::min(a, std::max(const_0, in));
223 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
224 }
225 else if(act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
226 {
227 tmp = std::min(a, std::max(b, in));
228 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
229 }
230 else if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
231 {
232 float tmp_f = dequantize_qasymm8(in, qi_in);
233 tmp_f = 1.f / (1.f + std::exp(-tmp_f));
234 tmp = quantize_qasymm8(tmp_f, qi_out);
235 }
236 else if(act == ActivationLayerInfo::ActivationFunction::TANH)
237 {
238 float tmp_f = dequantize_qasymm8(in, qi_in);
239 tmp_f = a_f32 * std::tanh(b_f32 * tmp_f);
240 tmp = quantize_qasymm8(tmp_f, qi_out);
241 }
242 else if(act == ActivationLayerInfo::ActivationFunction::HARD_SWISH)
243 {
244 float tmp_f = dequantize_qasymm8(in, qi_in);
245 tmp_f = tmp_f * ((std::min(std::max((tmp_f + 3), 0.0f), 6.0f)) * 0.166666667f);
246 tmp = quantize_qasymm8(tmp_f, qi_out);
247 }
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000248 else if(act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU)
249 {
250 float tmp_f = dequantize_qasymm8(in, qi_in);
251 tmp_f = tmp_f > 0 ? tmp_f : tmp_f * a_f32;
252 tmp = quantize_qasymm8(tmp_f, qi_out);
253 }
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100254 else
255 {
256 ARM_COMPUTE_ERROR("Unsupported activation function");
257 }
258 *(output_ptr + x) = tmp;
259 }
260 },
261 input, output);
262}
263} // namespace cpu
264} // namespace arm_compute