blob: bdc93ed1b8c3d497f4e2d52f27acea57ce4f13e5 [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 Barbier6ff3b192017-09-04 18:44:23 +010026#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/ITensor.h"
Michel Iwaniec5dfeae62017-11-29 10:48:23 +000028#include "arm_compute/core/NEON/NEAsymm.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/NEON/NEFixedPoint.h"
30#include "arm_compute/core/NEON/NEMath.h"
Michel Iwaniec5dfeae62017-11-29 10:48:23 +000031#include "arm_compute/core/QAsymm8.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Utils.h"
34#include "arm_compute/core/Validate.h"
35#include "arm_compute/core/Window.h"
36
37#include <arm_neon.h>
38#include <array>
39#include <cmath>
40#include <map>
41
42using namespace arm_compute;
Michalis Spyrouafa5d812017-11-30 14:25:57 +000043namespace
44{
45Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
46{
47 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010048 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 +000049
50 // Checks performed when output is configured
51 if((output != nullptr) && (output->total_size() != 0))
52 {
53 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
54 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000055 }
56
57 return Status{};
58}
59
60std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
61{
62 constexpr unsigned int num_elems_processed_per_iteration = 16;
63 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
64 bool window_changed = false;
65
66 if(output != nullptr && (output->total_size() != 0))
67 {
68 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
69
70 window_changed = update_window_and_padding(win,
71 AccessWindowHorizontal(input, 0, num_elems_processed_per_iteration),
72 output_access);
73
74 output_access.set_valid_region(win, input->valid_region());
75 }
76 else
77 {
78 // In-place computation
79 window_changed = update_window_and_padding(win,
80 AccessWindowHorizontal(input, 0, num_elems_processed_per_iteration));
81 }
82
83 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
84 return std::make_pair(err, win);
85}
86} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087
88NEActivationLayerKernel::NEActivationLayerKernel()
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010089 : _input(nullptr), _output(nullptr), _func(nullptr), _act_info(ActivationFunction::LOGISTIC)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090{
91}
92
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010093void NEActivationLayerKernel::configure(ITensor *input, ITensor *output, ActivationLayerInfo activation_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094{
Michalis Spyrouafa5d812017-11-30 14:25:57 +000095 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010097 _input = input;
98 _act_info = activation_info;
99 _output = input;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100101 if(output != nullptr)
102 {
103 // Output auto inizialitation if not yet initialized
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000104 auto_init_if_empty(*output->info(), *input->info()->clone());
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100105 _output = output;
106 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000108 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr));
109
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000110 ARM_COMPUTE_ERROR_ON_MSG((input->info()->data_type() == DataType::QASYMM8) && (activation_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
111 && (activation_info.activation() != ActivationLayerInfo::ActivationFunction::RELU),
112 "For QASYMM8 only relu and lower/upper bounded relu are supported");
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000113
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114 // Activation functions : FP32
115 static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_f32 =
116 {
117 { ActivationFunction::ABS, &NEActivationLayerKernel::activation<ActivationFunction::ABS, float> },
118 { ActivationFunction::LINEAR, &NEActivationLayerKernel::activation<ActivationFunction::LINEAR, float> },
119 { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, float> },
120 { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, float> },
121 { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, float> },
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100122 { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, float> },
Georgios Pinitas579c0492017-07-12 16:12:12 +0100123 { ActivationFunction::LEAKY_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LEAKY_RELU, float> },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124 { ActivationFunction::SOFT_RELU, &NEActivationLayerKernel::activation<ActivationFunction::SOFT_RELU, float> },
125 { ActivationFunction::SQRT, &NEActivationLayerKernel::activation<ActivationFunction::SQRT, float> },
126 { ActivationFunction::SQUARE, &NEActivationLayerKernel::activation<ActivationFunction::SQUARE, float> },
127 { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, float> },
128 };
Pablo Tello91654c42017-07-05 11:32:17 +0100129
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000130#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello91654c42017-07-05 11:32:17 +0100131 // Activation functions : FP16
132 static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_f16 =
133 {
134 { ActivationFunction::ABS, &NEActivationLayerKernel::activation<ActivationFunction::ABS, float16_t> },
135 { ActivationFunction::LINEAR, &NEActivationLayerKernel::activation<ActivationFunction::LINEAR, float16_t> },
136 { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, float16_t> },
137 { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, float16_t> },
138 { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, float16_t> },
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100139 { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, float16_t> },
Pablo Tello91654c42017-07-05 11:32:17 +0100140 { ActivationFunction::SOFT_RELU, &NEActivationLayerKernel::activation<ActivationFunction::SOFT_RELU, float16_t> },
141 { ActivationFunction::SQRT, &NEActivationLayerKernel::activation<ActivationFunction::SQRT, float16_t> },
142 { ActivationFunction::SQUARE, &NEActivationLayerKernel::activation<ActivationFunction::SQUARE, float16_t> },
143 { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, float16_t> },
144 };
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000145#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC*/
Pablo Tello91654c42017-07-05 11:32:17 +0100146
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000147 // Activation functions : QASYMM8
148 static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_qasymm8 =
149 {
150 { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, qasymm8_t> },
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000151 { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, qasymm8_t> },
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000152 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154 switch(input->info()->data_type())
155 {
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000156 case DataType::QASYMM8:
157 _func = act_map_qasymm8[activation_info.activation()];
158 break;
Georgios Pinitasccc65d42017-06-27 17:39:11 +0100159 case DataType::F32:
160 _func = act_map_f32[activation_info.activation()];
161 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000162#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello91654c42017-07-05 11:32:17 +0100163 case DataType::F16:
164 _func = act_map_f16[activation_info.activation()];
165 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000166#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167 default:
168 ARM_COMPUTE_ERROR("Unsupported data type.");
169 }
170
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100171 // Configure kernel window
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000172 auto win_config = validate_and_configure_window(input->info(), (output != nullptr) ? output->info() : nullptr);
173 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
174 ICPPKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100175}
176
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000177#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello91654c42017-07-05 11:32:17 +0100178template <ActivationLayerInfo::ActivationFunction F, typename T>
179typename std::enable_if<std::is_same<T, float16_t>::value, void>::type NEActivationLayerKernel::activation(const Window &window)
180{
181 Iterator input(_input, window);
182 Iterator output(_output, window);
183
184 static const float16x8_t CONST_0 = vdupq_n_f16(0.f);
185 static const float16x8_t CONST_1 = vdupq_n_f16(1.f);
186
187 const float16x8_t a = vdupq_n_f16(_act_info.a());
188 const float16x8_t b = vdupq_n_f16(_act_info.b());
189
190 execute_window_loop(window, [&](const Coordinates &)
191 {
192 const auto input_ptr = reinterpret_cast<const float16_t *>(input.ptr());
193 const auto output_ptr = reinterpret_cast<float16_t *>(output.ptr());
194
195 const float16x8x2_t in = vld2q_f16(input_ptr);
196 float16x8x2_t tmp = { {} };
197
198 switch(F)
199 {
200 case ActivationFunction::ABS:
201 tmp =
202 {
203 {
204 vabsq_f16(in.val[0]),
205 vabsq_f16(in.val[1]),
206 }
207 };
208 break;
209 case ActivationFunction::BOUNDED_RELU:
210 tmp =
211 {
212 {
213 vminq_f16(a, vmaxq_f16(CONST_0, in.val[0])),
214 vminq_f16(a, vmaxq_f16(CONST_0, in.val[1]))
215 }
216 };
217 break;
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100218 case ActivationFunction::LU_BOUNDED_RELU:
219 tmp =
220 {
221 {
222 vminq_f16(a, vmaxq_f16(b, in.val[0])),
223 vminq_f16(a, vmaxq_f16(b, in.val[1]))
224 }
225 };
226 break;
Pablo Tello91654c42017-07-05 11:32:17 +0100227 case ActivationFunction::LINEAR:
228 tmp =
229 {
230 {
231 vaddq_f16(b, vmulq_f16(a, in.val[0])),
232 vaddq_f16(b, vmulq_f16(a, in.val[1]))
233 }
234 };
235 break;
236 case ActivationFunction::LOGISTIC:
237 tmp =
238 {
239 {
240 vinvq_f16(vaddq_f16(CONST_1, vexpq_f16(vnegq_f16(in.val[0])))),
241 vinvq_f16(vaddq_f16(CONST_1, vexpq_f16(vnegq_f16(in.val[1])))),
242 }
243 };
244 break;
245 case ActivationFunction::RELU:
246 tmp =
247 {
248 {
249 vmaxq_f16(CONST_0, in.val[0]),
250 vmaxq_f16(CONST_0, in.val[1])
251 }
252 };
253 break;
254 case ActivationFunction::LEAKY_RELU:
255 tmp =
256 {
257 {
258 vbslq_f16(vcgtq_f16(in.val[0], CONST_0), in.val[0], vmulq_f16(a, in.val[0])),
259 vbslq_f16(vcgtq_f16(in.val[1], CONST_0), in.val[1], vmulq_f16(a, in.val[1]))
260 }
261 };
262 break;
263 case ActivationFunction::SOFT_RELU:
264 tmp =
265 {
266 {
267 vlogq_f16(vaddq_f16(CONST_1, vexpq_f16(in.val[0]))),
268 vlogq_f16(vaddq_f16(CONST_1, vexpq_f16(in.val[1]))),
269 }
270 };
271 break;
272 case ActivationFunction::SQRT:
273 tmp =
274 {
275 {
276 vinvq_f16(vinvsqrtq_f16(in.val[0])),
277 vinvq_f16(vinvsqrtq_f16(in.val[1])),
278 }
279 };
280 break;
281 case ActivationFunction::SQUARE:
282 tmp =
283 {
284 {
285 vmulq_f16(in.val[0], in.val[0]),
286 vmulq_f16(in.val[1], in.val[1])
287 }
288 };
289 break;
290 case ActivationFunction::TANH:
291 tmp =
292 {
293 {
294 vmulq_f16(a, vtanhq_f16(vmulq_f16(b, in.val[0]))),
295 vmulq_f16(a, vtanhq_f16(vmulq_f16(b, in.val[1]))),
296 }
297 };
298 break;
299 default:
300 ARM_COMPUTE_ERROR("Not implemented");
301 break;
302 }
303
304 vst2q_f16(output_ptr, tmp);
305 },
306 input, output);
307}
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000308#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello91654c42017-07-05 11:32:17 +0100309
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100310template <ActivationLayerInfo::ActivationFunction F, typename T>
311typename std::enable_if<std::is_same<T, float>::value, void>::type NEActivationLayerKernel::activation(const Window &window)
312{
313 Iterator input(_input, window);
314 Iterator output(_output, window);
315
316 static const float32x4_t CONST_1 = vdupq_n_f32(1.f);
317 static const float32x4_t CONST_0 = vdupq_n_f32(0.f);
318 const float32x4_t a = vdupq_n_f32(_act_info.a());
319 const float32x4_t b = vdupq_n_f32(_act_info.b());
320
321 execute_window_loop(window, [&](const Coordinates & id)
322 {
323 const auto input_ptr = reinterpret_cast<const float *>(input.ptr());
324 const auto output_ptr = reinterpret_cast<float *>(output.ptr());
325
Georgios Pinitasf525eab2018-01-30 14:47:39 +0000326 const float32x4x4_t in =
327 {
328 {
329 vld1q_f32(input_ptr),
330 vld1q_f32(input_ptr + 4),
331 vld1q_f32(input_ptr + 8),
332 vld1q_f32(input_ptr + 12)
333 }
334 };
335 float32x4x4_t tmp = { {} };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100336
337 switch(F)
338 {
339 case ActivationFunction::ABS:
340 tmp =
341 {
342 {
343 vabsq_f32(in.val[0]),
344 vabsq_f32(in.val[1]),
345 vabsq_f32(in.val[2]),
346 vabsq_f32(in.val[3]),
347 }
348 };
349 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100350 case ActivationFunction::LINEAR:
351 tmp =
352 {
353 {
354 vmlaq_f32(b, a, in.val[0]),
355 vmlaq_f32(b, a, in.val[1]),
356 vmlaq_f32(b, a, in.val[2]),
357 vmlaq_f32(b, a, in.val[3]),
358 }
359 };
360 break;
361 case ActivationFunction::LOGISTIC:
362 tmp =
363 {
364 {
365 vinvq_f32(vaddq_f32(CONST_1, vexpq_f32(vnegq_f32(in.val[0])))),
366 vinvq_f32(vaddq_f32(CONST_1, vexpq_f32(vnegq_f32(in.val[1])))),
367 vinvq_f32(vaddq_f32(CONST_1, vexpq_f32(vnegq_f32(in.val[2])))),
368 vinvq_f32(vaddq_f32(CONST_1, vexpq_f32(vnegq_f32(in.val[3])))),
369 }
370 };
371 break;
372 case ActivationFunction::RELU:
373 tmp =
374 {
375 {
376 vmaxq_f32(CONST_0, in.val[0]),
377 vmaxq_f32(CONST_0, in.val[1]),
378 vmaxq_f32(CONST_0, in.val[2]),
379 vmaxq_f32(CONST_0, in.val[3]),
380 }
381 };
382 break;
Georgios Pinitas579c0492017-07-12 16:12:12 +0100383 case ActivationFunction::BOUNDED_RELU:
384 tmp =
385 {
386 {
387 vminq_f32(a, vmaxq_f32(CONST_0, in.val[0])),
388 vminq_f32(a, vmaxq_f32(CONST_0, in.val[1])),
389 vminq_f32(a, vmaxq_f32(CONST_0, in.val[2])),
390 vminq_f32(a, vmaxq_f32(CONST_0, in.val[3])),
391 }
392 };
393 break;
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100394 case ActivationFunction::LU_BOUNDED_RELU:
395 tmp =
396 {
397 {
398 vminq_f32(a, vmaxq_f32(b, in.val[0])),
399 vminq_f32(a, vmaxq_f32(b, in.val[1])),
400 vminq_f32(a, vmaxq_f32(b, in.val[2])),
401 vminq_f32(a, vmaxq_f32(b, in.val[3])),
402 }
403 };
404 break;
Georgios Pinitas579c0492017-07-12 16:12:12 +0100405 case ActivationFunction::LEAKY_RELU:
406 tmp =
407 {
408 {
409 vbslq_f32(vcgtq_f32(in.val[0], CONST_0), in.val[0], vmulq_f32(a, in.val[0])),
410 vbslq_f32(vcgtq_f32(in.val[1], CONST_0), in.val[1], vmulq_f32(a, in.val[1])),
411 vbslq_f32(vcgtq_f32(in.val[2], CONST_0), in.val[2], vmulq_f32(a, in.val[2])),
412 vbslq_f32(vcgtq_f32(in.val[3], CONST_0), in.val[3], vmulq_f32(a, in.val[3])),
413 }
414 };
415 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100416 case ActivationFunction::SOFT_RELU:
417 tmp =
418 {
419 {
420 vlogq_f32(vaddq_f32(CONST_1, vexpq_f32(in.val[0]))),
421 vlogq_f32(vaddq_f32(CONST_1, vexpq_f32(in.val[1]))),
422 vlogq_f32(vaddq_f32(CONST_1, vexpq_f32(in.val[2]))),
423 vlogq_f32(vaddq_f32(CONST_1, vexpq_f32(in.val[3]))),
424 }
425 };
426 break;
427 case ActivationFunction::SQRT:
428 tmp =
429 {
430 {
431 vinvq_f32(vinvsqrtq_f32(in.val[0])),
432 vinvq_f32(vinvsqrtq_f32(in.val[1])),
433 vinvq_f32(vinvsqrtq_f32(in.val[2])),
434 vinvq_f32(vinvsqrtq_f32(in.val[3])),
435 }
436 };
437 break;
438 case ActivationFunction::SQUARE:
439 tmp =
440 {
441 {
442 vmulq_f32(in.val[0], in.val[0]),
443 vmulq_f32(in.val[1], in.val[1]),
444 vmulq_f32(in.val[2], in.val[2]),
445 vmulq_f32(in.val[3], in.val[3]),
446 }
447 };
448 break;
449 case ActivationFunction::TANH:
450 tmp =
451 {
452 {
453 vmulq_f32(a, vtanhq_f32(vmulq_f32(b, in.val[0]))),
454 vmulq_f32(a, vtanhq_f32(vmulq_f32(b, in.val[1]))),
455 vmulq_f32(a, vtanhq_f32(vmulq_f32(b, in.val[2]))),
456 vmulq_f32(a, vtanhq_f32(vmulq_f32(b, in.val[3]))),
457 }
458 };
459 break;
460 default:
461 break;
462 }
463
Georgios Pinitasf525eab2018-01-30 14:47:39 +0000464 vst1q_f32(output_ptr, tmp.val[0]);
465 vst1q_f32(output_ptr + 4, tmp.val[1]);
466 vst1q_f32(output_ptr + 8, tmp.val[2]);
467 vst1q_f32(output_ptr + 12, tmp.val[3]);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100468 },
469 input, output);
470}
471
472template <ActivationLayerInfo::ActivationFunction F, typename T>
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000473typename std::enable_if<std::is_same<T, qasymm8_t>::value, void>::type NEActivationLayerKernel::activation(const Window &window)
474{
Isabella Gottardiabca1c92018-02-26 16:06:06 +0000475 Iterator input(_input, window);
476 Iterator output(_output, window);
477 const QuantizationInfo qi_in = _input->info()->quantization_info();
478 const QuantizationInfo qi_out = _output->info()->quantization_info();
479 const qasymm8x16_t a = vdupq_n_u8(sqcvt_qasymm8_f32(_act_info.a(), qi_in.scale, qi_in.offset));
480 const qasymm8x16_t b = vdupq_n_u8(sqcvt_qasymm8_f32(_act_info.b(), qi_in.scale, qi_in.offset));
481 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 +0000482
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000483 // Initialise scale/offset for re-quantization
484 float s = qi_in.scale / qi_out.scale;
485 float o = -qi_in.offset * s + qi_out.offset;
486 float32x4_t vs = vdupq_n_f32(s);
487 float32x4_t vo = vdupq_n_f32(o);
488
489 execute_window_loop(window, [&](const Coordinates & id)
490 {
491 const auto input_ptr = reinterpret_cast<const qasymm8_t *>(input.ptr());
492 const auto output_ptr = reinterpret_cast<qasymm8_t *>(output.ptr());
493
494 const qasymm8x16_t in = vld1q_u8(input_ptr);
495 qasymm8x16_t tmp = {};
496
497 switch(F)
498 {
499 case ActivationFunction::LU_BOUNDED_RELU:
500 // Perform activation
501 tmp = vminq_u8(a, vmaxq_u8(b, in));
502 // Re-quantize to new output space
503 tmp = vmlaq_qasymm8(tmp, vs, vo);
504 break;
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000505 case ActivationFunction::RELU:
506 // Perform activation
507 tmp = vmaxq_u8(CONST_0, in);
508 // Re-quantize to new output space
509 tmp = vmlaq_qasymm8(tmp, vs, vo);
510 break;
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000511 default:
512 ARM_COMPUTE_ERROR("Function not implemented");
513 break;
514 }
515
516 vst1q_u8(output_ptr, tmp);
517 },
518 input, output);
519}
520
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000521Status NEActivationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
522{
523 ARM_COMPUTE_UNUSED(act_info);
524 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
525 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (output != nullptr) ? output->clone().get() : nullptr).first);
526
527 return Status{};
528}
529
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100530void NEActivationLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100531{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100532 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100533 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100534 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100535 ARM_COMPUTE_ERROR_ON(_func == nullptr);
536
537 (this->*_func)(window);
538}