blob: 5ce79f1007f5782b06ddfa9b1e95e31406e8d28e [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);
Michele Di Giorgio1c948d42018-11-20 16:03:01 +0000187 static const float16x8_t CONST_1_H = vdupq_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 {
Pablo Tello91654c42017-07-05 11:32:17 +0100243 tmp =
244 {
245 {
Michele Di Giorgio1c948d42018-11-20 16:03:01 +0000246 vinvq_f16(vaddq_f16(CONST_1_H, vexpq_f16(vnegq_f16(in.val[0])))),
247 vinvq_f16(vaddq_f16(CONST_1_H, vexpq_f16(vnegq_f16(in.val[1]))))
Pablo Tello91654c42017-07-05 11:32:17 +0100248 }
249 };
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100250 }
251 break;
Pablo Tello91654c42017-07-05 11:32:17 +0100252 case ActivationFunction::RELU:
253 tmp =
254 {
255 {
256 vmaxq_f16(CONST_0, in.val[0]),
257 vmaxq_f16(CONST_0, in.val[1])
258 }
259 };
260 break;
261 case ActivationFunction::LEAKY_RELU:
262 tmp =
263 {
264 {
265 vbslq_f16(vcgtq_f16(in.val[0], CONST_0), in.val[0], vmulq_f16(a, in.val[0])),
266 vbslq_f16(vcgtq_f16(in.val[1], CONST_0), in.val[1], vmulq_f16(a, in.val[1]))
267 }
268 };
269 break;
270 case ActivationFunction::SOFT_RELU:
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100271 {
272 // TODO (COMPMID-1535) : Revisit FP16 approximations
273 const float16x4x2_t in0 =
274 {
275 vcvt_f16_f32(vlogq_f32(vaddq_f32(CONST_1_F32, vexpq_f32(vcvt_f32_f16(vget_low_f16(in.val[0])))))),
276 vcvt_f16_f32(vlogq_f32(vaddq_f32(CONST_1_F32, vexpq_f32(vcvt_f32_f16(vget_high_f16(in.val[0])))))),
277 };
278
279 const float16x4x2_t in1 =
280 {
281 vcvt_f16_f32(vlogq_f32(vaddq_f32(CONST_1_F32, vexpq_f32(vcvt_f32_f16(vget_low_f16(in.val[1])))))),
282 vcvt_f16_f32(vlogq_f32(vaddq_f32(CONST_1_F32, vexpq_f32(vcvt_f32_f16(vget_high_f16(in.val[1])))))),
283 };
284
Pablo Tello91654c42017-07-05 11:32:17 +0100285 tmp =
286 {
287 {
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100288 vcombine_f16(in0.val[0], in0.val[1]),
289 vcombine_f16(in1.val[0], in1.val[1]),
Pablo Tello91654c42017-07-05 11:32:17 +0100290 }
291 };
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100292 }
293 break;
Pablo Tello91654c42017-07-05 11:32:17 +0100294 case ActivationFunction::SQRT:
295 tmp =
296 {
297 {
298 vinvq_f16(vinvsqrtq_f16(in.val[0])),
299 vinvq_f16(vinvsqrtq_f16(in.val[1])),
300 }
301 };
302 break;
303 case ActivationFunction::SQUARE:
304 tmp =
305 {
306 {
307 vmulq_f16(in.val[0], in.val[0]),
308 vmulq_f16(in.val[1], in.val[1])
309 }
310 };
311 break;
312 case ActivationFunction::TANH:
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100313 {
314 // TODO (COMPMID-1535) : Revisit FP16 approximations
315 const float16x8x2_t mul =
316 {
317 vmulq_f16(b, in.val[0]),
318 vmulq_f16(b, in.val[1])
319 };
320 const float16x4x2_t in0 =
321 {
322 vmul_f16(a_h, vcvt_f16_f32(vtanhq_f32(vcvt_f32_f16(vget_low_f16(mul.val[0]))))),
323 vmul_f16(a_h, vcvt_f16_f32(vtanhq_f32(vcvt_f32_f16(vget_high_f16(mul.val[0]))))),
324 };
325
326 const float16x4x2_t in1 =
327 {
328 vmul_f16(a_h, vcvt_f16_f32(vtanhq_f32(vcvt_f32_f16(vget_low_f16(mul.val[1]))))),
329 vmul_f16(a_h, vcvt_f16_f32(vtanhq_f32(vcvt_f32_f16(vget_high_f16(mul.val[1]))))),
330 };
331
Pablo Tello91654c42017-07-05 11:32:17 +0100332 tmp =
333 {
334 {
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100335 vcombine_f16(in0.val[0], in0.val[1]),
336 vcombine_f16(in1.val[0], in1.val[1]),
Pablo Tello91654c42017-07-05 11:32:17 +0100337 }
338 };
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100339 }
340 break;
Pablo Tello91654c42017-07-05 11:32:17 +0100341 default:
342 ARM_COMPUTE_ERROR("Not implemented");
343 break;
344 }
345
346 vst2q_f16(output_ptr, tmp);
347 },
348 input, output);
349}
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000350#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello91654c42017-07-05 11:32:17 +0100351
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100352template <ActivationLayerInfo::ActivationFunction F, typename T>
353typename std::enable_if<std::is_same<T, float>::value, void>::type NEActivationLayerKernel::activation(const Window &window)
354{
355 Iterator input(_input, window);
356 Iterator output(_output, window);
357
358 static const float32x4_t CONST_1 = vdupq_n_f32(1.f);
359 static const float32x4_t CONST_0 = vdupq_n_f32(0.f);
360 const float32x4_t a = vdupq_n_f32(_act_info.a());
361 const float32x4_t b = vdupq_n_f32(_act_info.b());
362
363 execute_window_loop(window, [&](const Coordinates & id)
364 {
365 const auto input_ptr = reinterpret_cast<const float *>(input.ptr());
366 const auto output_ptr = reinterpret_cast<float *>(output.ptr());
367
Georgios Pinitasf525eab2018-01-30 14:47:39 +0000368 const float32x4x4_t in =
369 {
370 {
371 vld1q_f32(input_ptr),
372 vld1q_f32(input_ptr + 4),
373 vld1q_f32(input_ptr + 8),
374 vld1q_f32(input_ptr + 12)
375 }
376 };
377 float32x4x4_t tmp = { {} };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100378
379 switch(F)
380 {
381 case ActivationFunction::ABS:
382 tmp =
383 {
384 {
385 vabsq_f32(in.val[0]),
386 vabsq_f32(in.val[1]),
387 vabsq_f32(in.val[2]),
388 vabsq_f32(in.val[3]),
389 }
390 };
391 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100392 case ActivationFunction::LINEAR:
393 tmp =
394 {
395 {
396 vmlaq_f32(b, a, in.val[0]),
397 vmlaq_f32(b, a, in.val[1]),
398 vmlaq_f32(b, a, in.val[2]),
399 vmlaq_f32(b, a, in.val[3]),
400 }
401 };
402 break;
403 case ActivationFunction::LOGISTIC:
404 tmp =
405 {
406 {
407 vinvq_f32(vaddq_f32(CONST_1, vexpq_f32(vnegq_f32(in.val[0])))),
408 vinvq_f32(vaddq_f32(CONST_1, vexpq_f32(vnegq_f32(in.val[1])))),
409 vinvq_f32(vaddq_f32(CONST_1, vexpq_f32(vnegq_f32(in.val[2])))),
410 vinvq_f32(vaddq_f32(CONST_1, vexpq_f32(vnegq_f32(in.val[3])))),
411 }
412 };
413 break;
414 case ActivationFunction::RELU:
415 tmp =
416 {
417 {
418 vmaxq_f32(CONST_0, in.val[0]),
419 vmaxq_f32(CONST_0, in.val[1]),
420 vmaxq_f32(CONST_0, in.val[2]),
421 vmaxq_f32(CONST_0, in.val[3]),
422 }
423 };
424 break;
Georgios Pinitas579c0492017-07-12 16:12:12 +0100425 case ActivationFunction::BOUNDED_RELU:
426 tmp =
427 {
428 {
429 vminq_f32(a, vmaxq_f32(CONST_0, in.val[0])),
430 vminq_f32(a, vmaxq_f32(CONST_0, in.val[1])),
431 vminq_f32(a, vmaxq_f32(CONST_0, in.val[2])),
432 vminq_f32(a, vmaxq_f32(CONST_0, in.val[3])),
433 }
434 };
435 break;
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100436 case ActivationFunction::LU_BOUNDED_RELU:
437 tmp =
438 {
439 {
440 vminq_f32(a, vmaxq_f32(b, in.val[0])),
441 vminq_f32(a, vmaxq_f32(b, in.val[1])),
442 vminq_f32(a, vmaxq_f32(b, in.val[2])),
443 vminq_f32(a, vmaxq_f32(b, in.val[3])),
444 }
445 };
446 break;
Georgios Pinitas579c0492017-07-12 16:12:12 +0100447 case ActivationFunction::LEAKY_RELU:
448 tmp =
449 {
450 {
451 vbslq_f32(vcgtq_f32(in.val[0], CONST_0), in.val[0], vmulq_f32(a, in.val[0])),
452 vbslq_f32(vcgtq_f32(in.val[1], CONST_0), in.val[1], vmulq_f32(a, in.val[1])),
453 vbslq_f32(vcgtq_f32(in.val[2], CONST_0), in.val[2], vmulq_f32(a, in.val[2])),
454 vbslq_f32(vcgtq_f32(in.val[3], CONST_0), in.val[3], vmulq_f32(a, in.val[3])),
455 }
456 };
457 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100458 case ActivationFunction::SOFT_RELU:
459 tmp =
460 {
461 {
462 vlogq_f32(vaddq_f32(CONST_1, vexpq_f32(in.val[0]))),
463 vlogq_f32(vaddq_f32(CONST_1, vexpq_f32(in.val[1]))),
464 vlogq_f32(vaddq_f32(CONST_1, vexpq_f32(in.val[2]))),
465 vlogq_f32(vaddq_f32(CONST_1, vexpq_f32(in.val[3]))),
466 }
467 };
468 break;
469 case ActivationFunction::SQRT:
470 tmp =
471 {
472 {
473 vinvq_f32(vinvsqrtq_f32(in.val[0])),
474 vinvq_f32(vinvsqrtq_f32(in.val[1])),
475 vinvq_f32(vinvsqrtq_f32(in.val[2])),
476 vinvq_f32(vinvsqrtq_f32(in.val[3])),
477 }
478 };
479 break;
480 case ActivationFunction::SQUARE:
481 tmp =
482 {
483 {
484 vmulq_f32(in.val[0], in.val[0]),
485 vmulq_f32(in.val[1], in.val[1]),
486 vmulq_f32(in.val[2], in.val[2]),
487 vmulq_f32(in.val[3], in.val[3]),
488 }
489 };
490 break;
491 case ActivationFunction::TANH:
492 tmp =
493 {
494 {
495 vmulq_f32(a, vtanhq_f32(vmulq_f32(b, in.val[0]))),
496 vmulq_f32(a, vtanhq_f32(vmulq_f32(b, in.val[1]))),
497 vmulq_f32(a, vtanhq_f32(vmulq_f32(b, in.val[2]))),
498 vmulq_f32(a, vtanhq_f32(vmulq_f32(b, in.val[3]))),
499 }
500 };
501 break;
502 default:
503 break;
504 }
505
Georgios Pinitasf525eab2018-01-30 14:47:39 +0000506 vst1q_f32(output_ptr, tmp.val[0]);
507 vst1q_f32(output_ptr + 4, tmp.val[1]);
508 vst1q_f32(output_ptr + 8, tmp.val[2]);
509 vst1q_f32(output_ptr + 12, tmp.val[3]);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100510 },
511 input, output);
512}
513
514template <ActivationLayerInfo::ActivationFunction F, typename T>
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000515typename std::enable_if<std::is_same<T, qasymm8_t>::value, void>::type NEActivationLayerKernel::activation(const Window &window)
516{
Isabella Gottardiabca1c92018-02-26 16:06:06 +0000517 Iterator input(_input, window);
518 Iterator output(_output, window);
519 const QuantizationInfo qi_in = _input->info()->quantization_info();
520 const QuantizationInfo qi_out = _output->info()->quantization_info();
521 const qasymm8x16_t a = vdupq_n_u8(sqcvt_qasymm8_f32(_act_info.a(), qi_in.scale, qi_in.offset));
522 const qasymm8x16_t b = vdupq_n_u8(sqcvt_qasymm8_f32(_act_info.b(), qi_in.scale, qi_in.offset));
523 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 +0000524
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000525 // Initialise scale/offset for re-quantization
526 float s = qi_in.scale / qi_out.scale;
527 float o = -qi_in.offset * s + qi_out.offset;
528 float32x4_t vs = vdupq_n_f32(s);
529 float32x4_t vo = vdupq_n_f32(o);
530
531 execute_window_loop(window, [&](const Coordinates & id)
532 {
533 const auto input_ptr = reinterpret_cast<const qasymm8_t *>(input.ptr());
534 const auto output_ptr = reinterpret_cast<qasymm8_t *>(output.ptr());
535
536 const qasymm8x16_t in = vld1q_u8(input_ptr);
537 qasymm8x16_t tmp = {};
538
539 switch(F)
540 {
541 case ActivationFunction::LU_BOUNDED_RELU:
542 // Perform activation
543 tmp = vminq_u8(a, vmaxq_u8(b, in));
544 // Re-quantize to new output space
545 tmp = vmlaq_qasymm8(tmp, vs, vo);
546 break;
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000547 case ActivationFunction::RELU:
548 // Perform activation
549 tmp = vmaxq_u8(CONST_0, in);
550 // Re-quantize to new output space
551 tmp = vmlaq_qasymm8(tmp, vs, vo);
552 break;
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000553 default:
554 ARM_COMPUTE_ERROR("Function not implemented");
555 break;
556 }
557
558 vst1q_u8(output_ptr, tmp);
559 },
560 input, output);
561}
562
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000563Status NEActivationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
564{
565 ARM_COMPUTE_UNUSED(act_info);
566 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
567 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (output != nullptr) ? output->clone().get() : nullptr).first);
568
569 return Status{};
570}
571
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100572void NEActivationLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100573{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100574 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100575 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100576 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100577 ARM_COMPUTE_ERROR_ON(_func == nullptr);
578
579 (this->*_func)(window);
580}