blob: 2163f7bb63d4d7f2e5ed90d53155454403cf691c [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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#include "arm_compute/core/NEON/kernels/NEActivationLayerKernel.h"
25
Anthony Barbiereaefd002018-07-20 17:49:35 +010026#include "arm_compute/core/CPP/Validate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Michel Iwaniec5dfeae62017-11-29 10:48:23 +000029#include "arm_compute/core/NEON/NEAsymm.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/NEON/NEFixedPoint.h"
31#include "arm_compute/core/NEON/NEMath.h"
Michel Iwaniec5dfeae62017-11-29 10:48:23 +000032#include "arm_compute/core/QAsymm8.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Utils.h"
35#include "arm_compute/core/Validate.h"
36#include "arm_compute/core/Window.h"
37
38#include <arm_neon.h>
39#include <array>
40#include <cmath>
41#include <map>
42
43using namespace arm_compute;
Michalis Spyrouafa5d812017-11-30 14:25:57 +000044namespace
45{
46Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
47{
Anthony Barbiereaefd002018-07-20 17:49:35 +010048 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010049 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::QASYMM8, DataType::F16, DataType::F32);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000050
51 // Checks performed when output is configured
52 if((output != nullptr) && (output->total_size() != 0))
53 {
54 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000056 }
57
58 return Status{};
59}
60
61std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
62{
63 constexpr unsigned int num_elems_processed_per_iteration = 16;
64 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
65 bool window_changed = false;
66
67 if(output != nullptr && (output->total_size() != 0))
68 {
69 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
70
71 window_changed = update_window_and_padding(win,
72 AccessWindowHorizontal(input, 0, num_elems_processed_per_iteration),
73 output_access);
74
75 output_access.set_valid_region(win, input->valid_region());
76 }
77 else
78 {
79 // In-place computation
80 window_changed = update_window_and_padding(win,
81 AccessWindowHorizontal(input, 0, num_elems_processed_per_iteration));
82 }
83
84 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
85 return std::make_pair(err, win);
86}
87} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088
89NEActivationLayerKernel::NEActivationLayerKernel()
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010090 : _input(nullptr), _output(nullptr), _func(nullptr), _act_info(ActivationFunction::LOGISTIC)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091{
92}
93
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010094void NEActivationLayerKernel::configure(ITensor *input, ITensor *output, ActivationLayerInfo activation_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095{
Michalis Spyrouafa5d812017-11-30 14:25:57 +000096 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010098 _input = input;
99 _act_info = activation_info;
100 _output = input;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100102 if(output != nullptr)
103 {
104 // Output auto inizialitation if not yet initialized
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000105 auto_init_if_empty(*output->info(), *input->info()->clone());
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100106 _output = output;
107 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000109 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr));
110
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000111 ARM_COMPUTE_ERROR_ON_MSG((input->info()->data_type() == DataType::QASYMM8) && (activation_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
112 && (activation_info.activation() != ActivationLayerInfo::ActivationFunction::RELU),
113 "For QASYMM8 only relu and lower/upper bounded relu are supported");
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000114
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115 // Activation functions : FP32
116 static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_f32 =
117 {
118 { ActivationFunction::ABS, &NEActivationLayerKernel::activation<ActivationFunction::ABS, float> },
119 { ActivationFunction::LINEAR, &NEActivationLayerKernel::activation<ActivationFunction::LINEAR, float> },
120 { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, float> },
121 { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, float> },
122 { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, float> },
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100123 { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, float> },
Georgios Pinitas579c0492017-07-12 16:12:12 +0100124 { ActivationFunction::LEAKY_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LEAKY_RELU, float> },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125 { ActivationFunction::SOFT_RELU, &NEActivationLayerKernel::activation<ActivationFunction::SOFT_RELU, float> },
126 { ActivationFunction::SQRT, &NEActivationLayerKernel::activation<ActivationFunction::SQRT, float> },
127 { ActivationFunction::SQUARE, &NEActivationLayerKernel::activation<ActivationFunction::SQUARE, float> },
128 { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, float> },
129 };
Pablo Tello91654c42017-07-05 11:32:17 +0100130
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000131#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello91654c42017-07-05 11:32:17 +0100132 // Activation functions : FP16
133 static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_f16 =
134 {
135 { ActivationFunction::ABS, &NEActivationLayerKernel::activation<ActivationFunction::ABS, float16_t> },
136 { ActivationFunction::LINEAR, &NEActivationLayerKernel::activation<ActivationFunction::LINEAR, float16_t> },
137 { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, float16_t> },
138 { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, float16_t> },
139 { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, float16_t> },
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100140 { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, float16_t> },
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100141 { ActivationFunction::LEAKY_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LEAKY_RELU, float16_t> },
Pablo Tello91654c42017-07-05 11:32:17 +0100142 { ActivationFunction::SOFT_RELU, &NEActivationLayerKernel::activation<ActivationFunction::SOFT_RELU, float16_t> },
143 { ActivationFunction::SQRT, &NEActivationLayerKernel::activation<ActivationFunction::SQRT, float16_t> },
144 { ActivationFunction::SQUARE, &NEActivationLayerKernel::activation<ActivationFunction::SQUARE, float16_t> },
145 { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, float16_t> },
146 };
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000147#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC*/
Pablo Tello91654c42017-07-05 11:32:17 +0100148
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000149 // Activation functions : QASYMM8
150 static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_qasymm8 =
151 {
152 { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, qasymm8_t> },
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000153 { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, qasymm8_t> },
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000154 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156 switch(input->info()->data_type())
157 {
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000158 case DataType::QASYMM8:
159 _func = act_map_qasymm8[activation_info.activation()];
160 break;
Georgios Pinitasccc65d42017-06-27 17:39:11 +0100161 case DataType::F32:
162 _func = act_map_f32[activation_info.activation()];
163 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000164#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello91654c42017-07-05 11:32:17 +0100165 case DataType::F16:
166 _func = act_map_f16[activation_info.activation()];
167 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000168#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169 default:
170 ARM_COMPUTE_ERROR("Unsupported data type.");
171 }
172
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100173 // Configure kernel window
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000174 auto win_config = validate_and_configure_window(input->info(), (output != nullptr) ? output->info() : nullptr);
175 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
176 ICPPKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177}
178
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000179#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello91654c42017-07-05 11:32:17 +0100180template <ActivationLayerInfo::ActivationFunction F, typename T>
181typename std::enable_if<std::is_same<T, float16_t>::value, void>::type NEActivationLayerKernel::activation(const Window &window)
182{
183 Iterator input(_input, window);
184 Iterator output(_output, window);
185
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100186 static const float16x8_t CONST_0 = vdupq_n_f16(0.f);
187 static const float16x4_t CONST_1_H = vdup_n_f16(1.f);
Pablo Tello91654c42017-07-05 11:32:17 +0100188
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100189 static const float32x4_t CONST_1_F32 = vdupq_n_f32(1.f);
190
191 const float16x8_t a = vdupq_n_f16(_act_info.a());
192 const float16x4_t a_h = vdup_n_f16(_act_info.a());
193 const float16x8_t b = vdupq_n_f16(_act_info.b());
Pablo Tello91654c42017-07-05 11:32:17 +0100194
195 execute_window_loop(window, [&](const Coordinates &)
196 {
197 const auto input_ptr = reinterpret_cast<const float16_t *>(input.ptr());
198 const auto output_ptr = reinterpret_cast<float16_t *>(output.ptr());
199
200 const float16x8x2_t in = vld2q_f16(input_ptr);
201 float16x8x2_t tmp = { {} };
202
203 switch(F)
204 {
205 case ActivationFunction::ABS:
206 tmp =
207 {
208 {
209 vabsq_f16(in.val[0]),
210 vabsq_f16(in.val[1]),
211 }
212 };
213 break;
214 case ActivationFunction::BOUNDED_RELU:
215 tmp =
216 {
217 {
218 vminq_f16(a, vmaxq_f16(CONST_0, in.val[0])),
219 vminq_f16(a, vmaxq_f16(CONST_0, in.val[1]))
220 }
221 };
222 break;
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100223 case ActivationFunction::LU_BOUNDED_RELU:
224 tmp =
225 {
226 {
227 vminq_f16(a, vmaxq_f16(b, in.val[0])),
228 vminq_f16(a, vmaxq_f16(b, in.val[1]))
229 }
230 };
231 break;
Pablo Tello91654c42017-07-05 11:32:17 +0100232 case ActivationFunction::LINEAR:
233 tmp =
234 {
235 {
236 vaddq_f16(b, vmulq_f16(a, in.val[0])),
237 vaddq_f16(b, vmulq_f16(a, in.val[1]))
238 }
239 };
240 break;
241 case ActivationFunction::LOGISTIC:
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100242 {
243 // TODO (COMPMID-1535) : Revisit FP16 approximations
244 const float16x4x2_t in0 =
245 {
246 vinv_f16(vadd_f16(CONST_1_H, vcvt_f16_f32(vexpq_f32(vcvt_f32_f16(vneg_f16(vget_low_f16(in.val[0]))))))),
247 vinv_f16(vadd_f16(CONST_1_H, vcvt_f16_f32(vexpq_f32(vcvt_f32_f16(vneg_f16(vget_high_f16(in.val[0]))))))),
248 };
249
250 const float16x4x2_t in1 =
251 {
252 vinv_f16(vadd_f16(CONST_1_H, vcvt_f16_f32(vexpq_f32(vcvt_f32_f16(vneg_f16(vget_low_f16(in.val[1]))))))),
253 vinv_f16(vadd_f16(CONST_1_H, vcvt_f16_f32(vexpq_f32(vcvt_f32_f16(vneg_f16(vget_high_f16(in.val[1]))))))),
254 };
255
Pablo Tello91654c42017-07-05 11:32:17 +0100256 tmp =
257 {
258 {
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100259 vcombine_f16(in0.val[0], in0.val[1]),
260 vcombine_f16(in1.val[0], in1.val[1]),
Pablo Tello91654c42017-07-05 11:32:17 +0100261 }
262 };
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100263 }
264 break;
Pablo Tello91654c42017-07-05 11:32:17 +0100265 case ActivationFunction::RELU:
266 tmp =
267 {
268 {
269 vmaxq_f16(CONST_0, in.val[0]),
270 vmaxq_f16(CONST_0, in.val[1])
271 }
272 };
273 break;
274 case ActivationFunction::LEAKY_RELU:
275 tmp =
276 {
277 {
278 vbslq_f16(vcgtq_f16(in.val[0], CONST_0), in.val[0], vmulq_f16(a, in.val[0])),
279 vbslq_f16(vcgtq_f16(in.val[1], CONST_0), in.val[1], vmulq_f16(a, in.val[1]))
280 }
281 };
282 break;
283 case ActivationFunction::SOFT_RELU:
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100284 {
285 // TODO (COMPMID-1535) : Revisit FP16 approximations
286 const float16x4x2_t in0 =
287 {
288 vcvt_f16_f32(vlogq_f32(vaddq_f32(CONST_1_F32, vexpq_f32(vcvt_f32_f16(vget_low_f16(in.val[0])))))),
289 vcvt_f16_f32(vlogq_f32(vaddq_f32(CONST_1_F32, vexpq_f32(vcvt_f32_f16(vget_high_f16(in.val[0])))))),
290 };
291
292 const float16x4x2_t in1 =
293 {
294 vcvt_f16_f32(vlogq_f32(vaddq_f32(CONST_1_F32, vexpq_f32(vcvt_f32_f16(vget_low_f16(in.val[1])))))),
295 vcvt_f16_f32(vlogq_f32(vaddq_f32(CONST_1_F32, vexpq_f32(vcvt_f32_f16(vget_high_f16(in.val[1])))))),
296 };
297
Pablo Tello91654c42017-07-05 11:32:17 +0100298 tmp =
299 {
300 {
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100301 vcombine_f16(in0.val[0], in0.val[1]),
302 vcombine_f16(in1.val[0], in1.val[1]),
Pablo Tello91654c42017-07-05 11:32:17 +0100303 }
304 };
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100305 }
306 break;
Pablo Tello91654c42017-07-05 11:32:17 +0100307 case ActivationFunction::SQRT:
308 tmp =
309 {
310 {
311 vinvq_f16(vinvsqrtq_f16(in.val[0])),
312 vinvq_f16(vinvsqrtq_f16(in.val[1])),
313 }
314 };
315 break;
316 case ActivationFunction::SQUARE:
317 tmp =
318 {
319 {
320 vmulq_f16(in.val[0], in.val[0]),
321 vmulq_f16(in.val[1], in.val[1])
322 }
323 };
324 break;
325 case ActivationFunction::TANH:
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100326 {
327 // TODO (COMPMID-1535) : Revisit FP16 approximations
328 const float16x8x2_t mul =
329 {
330 vmulq_f16(b, in.val[0]),
331 vmulq_f16(b, in.val[1])
332 };
333 const float16x4x2_t in0 =
334 {
335 vmul_f16(a_h, vcvt_f16_f32(vtanhq_f32(vcvt_f32_f16(vget_low_f16(mul.val[0]))))),
336 vmul_f16(a_h, vcvt_f16_f32(vtanhq_f32(vcvt_f32_f16(vget_high_f16(mul.val[0]))))),
337 };
338
339 const float16x4x2_t in1 =
340 {
341 vmul_f16(a_h, vcvt_f16_f32(vtanhq_f32(vcvt_f32_f16(vget_low_f16(mul.val[1]))))),
342 vmul_f16(a_h, vcvt_f16_f32(vtanhq_f32(vcvt_f32_f16(vget_high_f16(mul.val[1]))))),
343 };
344
Pablo Tello91654c42017-07-05 11:32:17 +0100345 tmp =
346 {
347 {
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100348 vcombine_f16(in0.val[0], in0.val[1]),
349 vcombine_f16(in1.val[0], in1.val[1]),
Pablo Tello91654c42017-07-05 11:32:17 +0100350 }
351 };
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100352 }
353 break;
Pablo Tello91654c42017-07-05 11:32:17 +0100354 default:
355 ARM_COMPUTE_ERROR("Not implemented");
356 break;
357 }
358
359 vst2q_f16(output_ptr, tmp);
360 },
361 input, output);
362}
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000363#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello91654c42017-07-05 11:32:17 +0100364
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100365template <ActivationLayerInfo::ActivationFunction F, typename T>
366typename std::enable_if<std::is_same<T, float>::value, void>::type NEActivationLayerKernel::activation(const Window &window)
367{
368 Iterator input(_input, window);
369 Iterator output(_output, window);
370
371 static const float32x4_t CONST_1 = vdupq_n_f32(1.f);
372 static const float32x4_t CONST_0 = vdupq_n_f32(0.f);
373 const float32x4_t a = vdupq_n_f32(_act_info.a());
374 const float32x4_t b = vdupq_n_f32(_act_info.b());
375
376 execute_window_loop(window, [&](const Coordinates & id)
377 {
378 const auto input_ptr = reinterpret_cast<const float *>(input.ptr());
379 const auto output_ptr = reinterpret_cast<float *>(output.ptr());
380
Georgios Pinitasf525eab2018-01-30 14:47:39 +0000381 const float32x4x4_t in =
382 {
383 {
384 vld1q_f32(input_ptr),
385 vld1q_f32(input_ptr + 4),
386 vld1q_f32(input_ptr + 8),
387 vld1q_f32(input_ptr + 12)
388 }
389 };
390 float32x4x4_t tmp = { {} };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100391
392 switch(F)
393 {
394 case ActivationFunction::ABS:
395 tmp =
396 {
397 {
398 vabsq_f32(in.val[0]),
399 vabsq_f32(in.val[1]),
400 vabsq_f32(in.val[2]),
401 vabsq_f32(in.val[3]),
402 }
403 };
404 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100405 case ActivationFunction::LINEAR:
406 tmp =
407 {
408 {
409 vmlaq_f32(b, a, in.val[0]),
410 vmlaq_f32(b, a, in.val[1]),
411 vmlaq_f32(b, a, in.val[2]),
412 vmlaq_f32(b, a, in.val[3]),
413 }
414 };
415 break;
416 case ActivationFunction::LOGISTIC:
417 tmp =
418 {
419 {
420 vinvq_f32(vaddq_f32(CONST_1, vexpq_f32(vnegq_f32(in.val[0])))),
421 vinvq_f32(vaddq_f32(CONST_1, vexpq_f32(vnegq_f32(in.val[1])))),
422 vinvq_f32(vaddq_f32(CONST_1, vexpq_f32(vnegq_f32(in.val[2])))),
423 vinvq_f32(vaddq_f32(CONST_1, vexpq_f32(vnegq_f32(in.val[3])))),
424 }
425 };
426 break;
427 case ActivationFunction::RELU:
428 tmp =
429 {
430 {
431 vmaxq_f32(CONST_0, in.val[0]),
432 vmaxq_f32(CONST_0, in.val[1]),
433 vmaxq_f32(CONST_0, in.val[2]),
434 vmaxq_f32(CONST_0, in.val[3]),
435 }
436 };
437 break;
Georgios Pinitas579c0492017-07-12 16:12:12 +0100438 case ActivationFunction::BOUNDED_RELU:
439 tmp =
440 {
441 {
442 vminq_f32(a, vmaxq_f32(CONST_0, in.val[0])),
443 vminq_f32(a, vmaxq_f32(CONST_0, in.val[1])),
444 vminq_f32(a, vmaxq_f32(CONST_0, in.val[2])),
445 vminq_f32(a, vmaxq_f32(CONST_0, in.val[3])),
446 }
447 };
448 break;
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100449 case ActivationFunction::LU_BOUNDED_RELU:
450 tmp =
451 {
452 {
453 vminq_f32(a, vmaxq_f32(b, in.val[0])),
454 vminq_f32(a, vmaxq_f32(b, in.val[1])),
455 vminq_f32(a, vmaxq_f32(b, in.val[2])),
456 vminq_f32(a, vmaxq_f32(b, in.val[3])),
457 }
458 };
459 break;
Georgios Pinitas579c0492017-07-12 16:12:12 +0100460 case ActivationFunction::LEAKY_RELU:
461 tmp =
462 {
463 {
464 vbslq_f32(vcgtq_f32(in.val[0], CONST_0), in.val[0], vmulq_f32(a, in.val[0])),
465 vbslq_f32(vcgtq_f32(in.val[1], CONST_0), in.val[1], vmulq_f32(a, in.val[1])),
466 vbslq_f32(vcgtq_f32(in.val[2], CONST_0), in.val[2], vmulq_f32(a, in.val[2])),
467 vbslq_f32(vcgtq_f32(in.val[3], CONST_0), in.val[3], vmulq_f32(a, in.val[3])),
468 }
469 };
470 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100471 case ActivationFunction::SOFT_RELU:
472 tmp =
473 {
474 {
475 vlogq_f32(vaddq_f32(CONST_1, vexpq_f32(in.val[0]))),
476 vlogq_f32(vaddq_f32(CONST_1, vexpq_f32(in.val[1]))),
477 vlogq_f32(vaddq_f32(CONST_1, vexpq_f32(in.val[2]))),
478 vlogq_f32(vaddq_f32(CONST_1, vexpq_f32(in.val[3]))),
479 }
480 };
481 break;
482 case ActivationFunction::SQRT:
483 tmp =
484 {
485 {
486 vinvq_f32(vinvsqrtq_f32(in.val[0])),
487 vinvq_f32(vinvsqrtq_f32(in.val[1])),
488 vinvq_f32(vinvsqrtq_f32(in.val[2])),
489 vinvq_f32(vinvsqrtq_f32(in.val[3])),
490 }
491 };
492 break;
493 case ActivationFunction::SQUARE:
494 tmp =
495 {
496 {
497 vmulq_f32(in.val[0], in.val[0]),
498 vmulq_f32(in.val[1], in.val[1]),
499 vmulq_f32(in.val[2], in.val[2]),
500 vmulq_f32(in.val[3], in.val[3]),
501 }
502 };
503 break;
504 case ActivationFunction::TANH:
505 tmp =
506 {
507 {
508 vmulq_f32(a, vtanhq_f32(vmulq_f32(b, in.val[0]))),
509 vmulq_f32(a, vtanhq_f32(vmulq_f32(b, in.val[1]))),
510 vmulq_f32(a, vtanhq_f32(vmulq_f32(b, in.val[2]))),
511 vmulq_f32(a, vtanhq_f32(vmulq_f32(b, in.val[3]))),
512 }
513 };
514 break;
515 default:
516 break;
517 }
518
Georgios Pinitasf525eab2018-01-30 14:47:39 +0000519 vst1q_f32(output_ptr, tmp.val[0]);
520 vst1q_f32(output_ptr + 4, tmp.val[1]);
521 vst1q_f32(output_ptr + 8, tmp.val[2]);
522 vst1q_f32(output_ptr + 12, tmp.val[3]);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100523 },
524 input, output);
525}
526
527template <ActivationLayerInfo::ActivationFunction F, typename T>
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000528typename std::enable_if<std::is_same<T, qasymm8_t>::value, void>::type NEActivationLayerKernel::activation(const Window &window)
529{
Isabella Gottardiabca1c92018-02-26 16:06:06 +0000530 Iterator input(_input, window);
531 Iterator output(_output, window);
532 const QuantizationInfo qi_in = _input->info()->quantization_info();
533 const QuantizationInfo qi_out = _output->info()->quantization_info();
534 const qasymm8x16_t a = vdupq_n_u8(sqcvt_qasymm8_f32(_act_info.a(), qi_in.scale, qi_in.offset));
535 const qasymm8x16_t b = vdupq_n_u8(sqcvt_qasymm8_f32(_act_info.b(), qi_in.scale, qi_in.offset));
536 const qasymm8x16_t CONST_0 = vdupq_n_u8(sqcvt_qasymm8_f32(0.f, qi_in.scale, qi_in.offset));
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000537
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000538 // Initialise scale/offset for re-quantization
539 float s = qi_in.scale / qi_out.scale;
540 float o = -qi_in.offset * s + qi_out.offset;
541 float32x4_t vs = vdupq_n_f32(s);
542 float32x4_t vo = vdupq_n_f32(o);
543
544 execute_window_loop(window, [&](const Coordinates & id)
545 {
546 const auto input_ptr = reinterpret_cast<const qasymm8_t *>(input.ptr());
547 const auto output_ptr = reinterpret_cast<qasymm8_t *>(output.ptr());
548
549 const qasymm8x16_t in = vld1q_u8(input_ptr);
550 qasymm8x16_t tmp = {};
551
552 switch(F)
553 {
554 case ActivationFunction::LU_BOUNDED_RELU:
555 // Perform activation
556 tmp = vminq_u8(a, vmaxq_u8(b, in));
557 // Re-quantize to new output space
558 tmp = vmlaq_qasymm8(tmp, vs, vo);
559 break;
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000560 case ActivationFunction::RELU:
561 // Perform activation
562 tmp = vmaxq_u8(CONST_0, in);
563 // Re-quantize to new output space
564 tmp = vmlaq_qasymm8(tmp, vs, vo);
565 break;
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000566 default:
567 ARM_COMPUTE_ERROR("Function not implemented");
568 break;
569 }
570
571 vst1q_u8(output_ptr, tmp);
572 },
573 input, output);
574}
575
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000576Status NEActivationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
577{
578 ARM_COMPUTE_UNUSED(act_info);
579 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
580 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (output != nullptr) ? output->clone().get() : nullptr).first);
581
582 return Status{};
583}
584
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100585void NEActivationLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100586{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100587 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100588 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100589 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100590 ARM_COMPUTE_ERROR_ON(_func == nullptr);
591
592 (this->*_func)(window);
593}