blob: bc6a2813539bd1c80b0bcabda45a15dcfaa5c719 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas5a594532018-12-03 14:30:05 +00002 * Copyright (c) 2017-2019 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"
Georgios Pinitas5a594532018-12-03 14:30:05 +000032#include "arm_compute/core/NEON/wrapper/wrapper.h"
Michel Iwaniec5dfeae62017-11-29 10:48:23 +000033#include "arm_compute/core/QAsymm8.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Utils.h"
36#include "arm_compute/core/Validate.h"
37#include "arm_compute/core/Window.h"
38
39#include <arm_neon.h>
40#include <array>
41#include <cmath>
42#include <map>
43
44using namespace arm_compute;
Michalis Spyrouafa5d812017-11-30 14:25:57 +000045namespace
46{
47Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
48{
Anthony Barbiereaefd002018-07-20 17:49:35 +010049 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010050 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 +000051
52 // Checks performed when output is configured
53 if((output != nullptr) && (output->total_size() != 0))
54 {
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000057 }
58
59 return Status{};
60}
61
62std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
63{
Georgios Pinitas5a594532018-12-03 14:30:05 +000064 // Configure kernel window
65 Window win = calculate_max_window(*input, Steps());
Michalis Spyrouafa5d812017-11-30 14:25:57 +000066
Georgios Pinitas5a594532018-12-03 14:30:05 +000067 if(output != nullptr)
Michalis Spyrouafa5d812017-11-30 14:25:57 +000068 {
Georgios Pinitas5a594532018-12-03 14:30:05 +000069 // Output auto inizialitation if not yet initialized
70 auto_init_if_empty(*output, *input->clone());
Michalis Spyrouafa5d812017-11-30 14:25:57 +000071
Georgios Pinitas5a594532018-12-03 14:30:05 +000072 // NEActivationLayerKernel doesn't need padding so update_window_and_padding() can be skipped
73 Coordinates coord;
74 coord.set_num_dimensions(output->num_dimensions());
75 output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
Michalis Spyrouafa5d812017-11-30 14:25:57 +000076 }
77
Georgios Pinitas5a594532018-12-03 14:30:05 +000078 return std::make_pair(Status{}, win);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000079}
80} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081
82NEActivationLayerKernel::NEActivationLayerKernel()
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010083 : _input(nullptr), _output(nullptr), _func(nullptr), _act_info(ActivationFunction::LOGISTIC)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084{
85}
86
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010087void NEActivationLayerKernel::configure(ITensor *input, ITensor *output, ActivationLayerInfo activation_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088{
Michalis Spyrouafa5d812017-11-30 14:25:57 +000089 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010091 _input = input;
92 _act_info = activation_info;
93 _output = input;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010095 if(output != nullptr)
96 {
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010097 _output = output;
98 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000100 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr));
101
Georgios Pinitas5a594532018-12-03 14:30:05 +0000102 ARM_COMPUTE_ERROR_ON_MSG((input->info()->data_type() == DataType::QASYMM8) && (activation_info.activation() != ActivationLayerInfo::ActivationFunction::RELU)
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000103 && (activation_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU) && (activation_info.activation() != ActivationLayerInfo::ActivationFunction::BOUNDED_RELU)
104 && (activation_info.activation() != ActivationLayerInfo::ActivationFunction::LOGISTIC),
105 "For QASYMM8 only logistic, relu and lower/upper bounded relu are supported");
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000106
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 // Activation functions : FP32
108 static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_f32 =
109 {
110 { ActivationFunction::ABS, &NEActivationLayerKernel::activation<ActivationFunction::ABS, float> },
111 { ActivationFunction::LINEAR, &NEActivationLayerKernel::activation<ActivationFunction::LINEAR, float> },
112 { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, float> },
113 { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, float> },
114 { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, float> },
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100115 { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, float> },
Georgios Pinitas579c0492017-07-12 16:12:12 +0100116 { ActivationFunction::LEAKY_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LEAKY_RELU, float> },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117 { ActivationFunction::SOFT_RELU, &NEActivationLayerKernel::activation<ActivationFunction::SOFT_RELU, float> },
118 { ActivationFunction::SQRT, &NEActivationLayerKernel::activation<ActivationFunction::SQRT, float> },
119 { ActivationFunction::SQUARE, &NEActivationLayerKernel::activation<ActivationFunction::SQUARE, float> },
120 { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, float> },
Usama Arif80e55db2019-05-14 17:48:47 +0100121 { ActivationFunction::IDENTITY, &NEActivationLayerKernel::activation<ActivationFunction::IDENTITY, float> },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122 };
Pablo Tello91654c42017-07-05 11:32:17 +0100123
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000124#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello91654c42017-07-05 11:32:17 +0100125 // Activation functions : FP16
126 static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_f16 =
127 {
128 { ActivationFunction::ABS, &NEActivationLayerKernel::activation<ActivationFunction::ABS, float16_t> },
129 { ActivationFunction::LINEAR, &NEActivationLayerKernel::activation<ActivationFunction::LINEAR, float16_t> },
130 { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, float16_t> },
131 { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, float16_t> },
132 { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, float16_t> },
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100133 { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, float16_t> },
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100134 { ActivationFunction::LEAKY_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LEAKY_RELU, float16_t> },
Pablo Tello91654c42017-07-05 11:32:17 +0100135 { ActivationFunction::SOFT_RELU, &NEActivationLayerKernel::activation<ActivationFunction::SOFT_RELU, float16_t> },
136 { ActivationFunction::SQRT, &NEActivationLayerKernel::activation<ActivationFunction::SQRT, float16_t> },
137 { ActivationFunction::SQUARE, &NEActivationLayerKernel::activation<ActivationFunction::SQUARE, float16_t> },
138 { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, float16_t> },
Usama Arif80e55db2019-05-14 17:48:47 +0100139 { ActivationFunction::IDENTITY, &NEActivationLayerKernel::activation<ActivationFunction::IDENTITY, float16_t> },
Pablo Tello91654c42017-07-05 11:32:17 +0100140 };
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000141#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC*/
Pablo Tello91654c42017-07-05 11:32:17 +0100142
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000143 // Activation functions : QASYMM8
144 static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_qasymm8 =
145 {
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000146 { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, qasymm8_t> },
Isabella Gottardi5d62c012019-01-29 15:05:41 +0000147 { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, qasymm8_t> },
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000148 { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, qasymm8_t> },
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000149 { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, qasymm8_t> },
Usama Arif80e55db2019-05-14 17:48:47 +0100150 { ActivationFunction::IDENTITY, &NEActivationLayerKernel::activation<ActivationFunction::IDENTITY, qasymm8_t> },
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000151 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153 switch(input->info()->data_type())
154 {
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000155 case DataType::QASYMM8:
156 _func = act_map_qasymm8[activation_info.activation()];
157 break;
Georgios Pinitasccc65d42017-06-27 17:39:11 +0100158 case DataType::F32:
159 _func = act_map_f32[activation_info.activation()];
160 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000161#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello91654c42017-07-05 11:32:17 +0100162 case DataType::F16:
163 _func = act_map_f16[activation_info.activation()];
164 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000165#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166 default:
167 ARM_COMPUTE_ERROR("Unsupported data type.");
168 }
169
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100170 // Configure kernel window
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000171 auto win_config = validate_and_configure_window(input->info(), (output != nullptr) ? output->info() : nullptr);
172 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
173 ICPPKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174}
175
Pablo Tello91654c42017-07-05 11:32:17 +0100176template <ActivationLayerInfo::ActivationFunction F, typename T>
Georgios Pinitas5a594532018-12-03 14:30:05 +0000177typename std::enable_if<arm_compute::utils::traits::is_floating_point<T>::value, void>::type
178NEActivationLayerKernel::activation(const Window &window)
Pablo Tello91654c42017-07-05 11:32:17 +0100179{
Georgios Pinitas5a594532018-12-03 14:30:05 +0000180 /** NEON vector tag type. */
181 using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<T, wrapper::traits::BitWidth::W128>;
Pablo Tello91654c42017-07-05 11:32:17 +0100182
Georgios Pinitas5a594532018-12-03 14:30:05 +0000183 const int window_step_x = 16 / sizeof(T);
184 const auto window_start_x = static_cast<int>(window.x().start());
185 const auto window_end_x = static_cast<int>(window.x().end());
186 const ActivationFunction act = F;
Pablo Tello91654c42017-07-05 11:32:17 +0100187
Georgios Pinitas5a594532018-12-03 14:30:05 +0000188 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
189 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100190
Georgios Pinitas5a594532018-12-03 14:30:05 +0000191 Iterator input(_input, win_collapsed);
192 Iterator output(_output, win_collapsed);
Pablo Tello91654c42017-07-05 11:32:17 +0100193
Georgios Pinitas5a594532018-12-03 14:30:05 +0000194 const auto const_1 = wrapper::vdup_n(static_cast<T>(1.f), ExactTagType{});
195 const auto const_0 = wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{});
196 const auto va = wrapper::vdup_n(static_cast<T>(_act_info.a()), ExactTagType{});
197 const auto vb = wrapper::vdup_n(static_cast<T>(_act_info.b()), ExactTagType{});
198 const auto a = static_cast<T>(_act_info.a());
199 const auto b = static_cast<T>(_act_info.b());
200
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100201 execute_window_loop(win_collapsed, [&](const Coordinates &)
Pablo Tello91654c42017-07-05 11:32:17 +0100202 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000203 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
204 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
Pablo Tello91654c42017-07-05 11:32:17 +0100205
Georgios Pinitas5a594532018-12-03 14:30:05 +0000206 wrapper::traits::neon_bitvector_t<T, wrapper::traits::BitWidth::W128> tmp;
Pablo Tello91654c42017-07-05 11:32:17 +0100207
Georgios Pinitas5a594532018-12-03 14:30:05 +0000208 // Compute S elements per iteration
209 int x = window_start_x;
210 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Pablo Tello91654c42017-07-05 11:32:17 +0100211 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000212 const auto vin = wrapper::vloadq(input_ptr + x);
213 switch(act)
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100214 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000215 case ActivationFunction::ABS:
216 tmp = wrapper::vabs(vin);
217 break;
218 case ActivationFunction::LINEAR:
219 tmp = wrapper::vmla(vb, va, vin);
220 break;
221 case ActivationFunction::LOGISTIC:
222 tmp = wrapper::vinv(wrapper::vadd(const_1, wrapper::vexpq(wrapper::vneg(vin))));
223 break;
224 case ActivationFunction::RELU:
225 tmp = wrapper::vmax(const_0, vin);
226 break;
227 case ActivationFunction::BOUNDED_RELU:
228 tmp = wrapper::vmin(va, wrapper::vmax(const_0, vin));
229 break;
230 case ActivationFunction::LU_BOUNDED_RELU:
231 tmp = wrapper::vmin(va, wrapper::vmax(vb, vin));
232 break;
233 case ActivationFunction::LEAKY_RELU:
234 tmp = wrapper::vbsl(wrapper::vcgt(vin, const_0), vin, wrapper::vmul(va, vin));
235 break;
236 case ActivationFunction::SOFT_RELU:
237 tmp = wrapper::vlog(wrapper::vadd(const_1, wrapper::vexpq(vin)));
238 break;
239 case ActivationFunction::SQRT:
240 tmp = wrapper::vinv(wrapper::vinvsqrt(vin));
241 break;
242 case ActivationFunction::SQUARE:
243 tmp = wrapper::vmul(vin, vin);
244 break;
245 case ActivationFunction::TANH:
246 tmp = wrapper::vmul(va, wrapper::vtanh(wrapper::vmul(vb, vin)));
247 break;
Usama Arif80e55db2019-05-14 17:48:47 +0100248 case ActivationFunction::IDENTITY:
249 tmp = vin;
250 break;
Georgios Pinitas5a594532018-12-03 14:30:05 +0000251 default:
252 ARM_COMPUTE_ERROR("Unsupported activation function");
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100253 }
Georgios Pinitas5a594532018-12-03 14:30:05 +0000254 wrapper::vstore(output_ptr + x, tmp);
Pablo Tello91654c42017-07-05 11:32:17 +0100255 }
256
Georgios Pinitas5a594532018-12-03 14:30:05 +0000257 // Compute left-over elements
258 for(; x < window_end_x; ++x)
Georgios Pinitasf525eab2018-01-30 14:47:39 +0000259 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000260 const T in = *(reinterpret_cast<const T *>(input_ptr + x));
261 T tmp;
262 switch(act)
Georgios Pinitasf525eab2018-01-30 14:47:39 +0000263 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000264 case ActivationFunction::ABS:
265 tmp = std::abs(in);
266 break;
267 case ActivationFunction::LINEAR:
268 tmp = a * in + b;
269 break;
270 case ActivationFunction::LOGISTIC:
271 tmp = static_cast<T>(1) / (static_cast<T>(1) + std::exp(-in));
272 break;
273 case ActivationFunction::RELU:
274 tmp = std::max<T>(static_cast<T>(0), in);
275 break;
276 case ActivationFunction::BOUNDED_RELU:
277 tmp = std::min<T>(a, std::max(static_cast<T>(0), in));
278 break;
279 case ActivationFunction::LU_BOUNDED_RELU:
280 tmp = std::min<T>(a, std::max<T>(b, in));
281 break;
282 case ActivationFunction::LEAKY_RELU:
283 tmp = (in > 0) ? in : a * in;
284 break;
285 case ActivationFunction::SOFT_RELU:
286 tmp = std::log(static_cast<T>(1) + std::exp(in));
287 break;
288 case ActivationFunction::SQRT:
289 tmp = std::sqrt(in);
290 break;
291 case ActivationFunction::SQUARE:
292 tmp = in * in;
293 break;
294 case ActivationFunction::TANH:
295 tmp = a * std::tanh(b * in);
296 break;
Usama Arif80e55db2019-05-14 17:48:47 +0100297 case ActivationFunction::IDENTITY:
298 tmp = in;
299 break;
Georgios Pinitas5a594532018-12-03 14:30:05 +0000300 default:
301 ARM_COMPUTE_ERROR("Unsupported activation function");
Georgios Pinitasf525eab2018-01-30 14:47:39 +0000302 }
Georgios Pinitas5a594532018-12-03 14:30:05 +0000303 *(output_ptr + x) = tmp;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100304 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100305 },
306 input, output);
307}
308
309template <ActivationLayerInfo::ActivationFunction F, typename T>
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000310typename std::enable_if<std::is_same<T, qasymm8_t>::value, void>::type NEActivationLayerKernel::activation(const Window &window)
311{
Georgios Pinitas5a594532018-12-03 14:30:05 +0000312 const int window_step_x = 16 / sizeof(T);
313 const auto window_start_x = static_cast<int>(window.x().start());
314 const auto window_end_x = static_cast<int>(window.x().end());
315 const ActivationFunction act = F;
316
317 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
318 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
319
320 Iterator input(_input, win_collapsed);
321 Iterator output(_output, win_collapsed);
322
323 const QuantizationInfo qi_in = _input->info()->quantization_info();
324 const QuantizationInfo qi_out = _output->info()->quantization_info();
325 const qasymm8x16_t va = vdupq_n_u8(sqcvt_qasymm8_f32(_act_info.a(), qi_in.scale, qi_in.offset));
326 const qasymm8x16_t vb = vdupq_n_u8(sqcvt_qasymm8_f32(_act_info.b(), qi_in.scale, qi_in.offset));
327 const qasymm8_t a = sqcvt_qasymm8_f32(_act_info.a(), qi_in.scale, qi_in.offset);
328 const qasymm8_t b = sqcvt_qasymm8_f32(_act_info.b(), qi_in.scale, qi_in.offset);
329 const qasymm8_t const_0 = sqcvt_qasymm8_f32(0.f, qi_in.scale, qi_in.offset);
330 const qasymm8x16_t vconst_0 = vdupq_n_u8(const_0);
Isabella Gottardif7a3bf22019-03-15 14:58:24 +0000331 const auto vconst_1 = vdupq_n_f32(1.f);
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000332
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000333 // Initialise scale/offset for re-quantization
334 float s = qi_in.scale / qi_out.scale;
335 float o = -qi_in.offset * s + qi_out.offset;
336 float32x4_t vs = vdupq_n_f32(s);
337 float32x4_t vo = vdupq_n_f32(o);
338
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100339 execute_window_loop(win_collapsed, [&](const Coordinates &)
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000340 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000341 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
342 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000343
Georgios Pinitas5a594532018-12-03 14:30:05 +0000344 wrapper::traits::neon_bitvector_t<T, wrapper::traits::BitWidth::W128> tmp;
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000345
Georgios Pinitas5a594532018-12-03 14:30:05 +0000346 // Compute S elements per iteration
347 int x = window_start_x;
348 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000349 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000350 const auto vin = wrapper::vloadq(input_ptr + x);
351 if(act == ActivationFunction::RELU)
352 {
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000353 // Perform activation
Georgios Pinitas5a594532018-12-03 14:30:05 +0000354 tmp = vmaxq_u8(vconst_0, vin);
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000355 // Re-quantize to new output space
356 tmp = vmlaq_qasymm8(tmp, vs, vo);
Georgios Pinitas5a594532018-12-03 14:30:05 +0000357 }
358 else if(act == ActivationFunction::BOUNDED_RELU)
359 {
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000360 // Perform activation
Georgios Pinitas5a594532018-12-03 14:30:05 +0000361 tmp = vminq_u8(va, vmaxq_u8(vconst_0, vin));
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000362 // Re-quantize to new output space
363 tmp = vmlaq_qasymm8(tmp, vs, vo);
Georgios Pinitas5a594532018-12-03 14:30:05 +0000364 }
365 else if(act == ActivationFunction::LU_BOUNDED_RELU)
366 {
367 // Perform activation
368 tmp = vminq_u8(va, vmaxq_u8(vb, vin));
369 // Re-quantize to new output space
370 tmp = vmlaq_qasymm8(tmp, vs, vo);
371 }
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000372 else if(act == ActivationFunction::LOGISTIC)
373 {
Isabella Gottardif7a3bf22019-03-15 14:58:24 +0000374 // De-quantize
375 const auto vin_deq = vdequantize(vin, qi_in);
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000376 // Perform activation
Isabella Gottardif7a3bf22019-03-15 14:58:24 +0000377 const float32x4x4_t tmp_dep =
378 {
379 {
380 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[0])))),
381 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[1])))),
382 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[2])))),
383 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[3])))),
384 }
385 };
386 // Re-quantize to new output space
387 tmp = vquantize(tmp_dep, qi_out);
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000388 }
Georgios Pinitas5a594532018-12-03 14:30:05 +0000389 else
390 {
391 ARM_COMPUTE_ERROR("Unsupported activation function");
392 }
393 wrapper::vstore(output_ptr + x, tmp);
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000394 }
395
Georgios Pinitas5a594532018-12-03 14:30:05 +0000396 // Compute left-over elements
397 for(; x < window_end_x; ++x)
398 {
399 T in = *(reinterpret_cast<const T *>(input_ptr + x));
400 T tmp;
401 if(act == ActivationFunction::RELU)
402 {
403 tmp = std::max(const_0, in);
Georgios Pinitas57016a42019-01-16 12:54:29 +0000404 tmp = std::max<int32_t>(0, std::min<int32_t>(tmp * s + o, 255));
Georgios Pinitas5a594532018-12-03 14:30:05 +0000405 }
406 else if(act == ActivationFunction::BOUNDED_RELU)
407 {
408 tmp = std::min(a, std::max(const_0, in));
Georgios Pinitas57016a42019-01-16 12:54:29 +0000409 tmp = std::max<int32_t>(0, std::min<int32_t>(tmp * s + o, 255));
Georgios Pinitas5a594532018-12-03 14:30:05 +0000410 }
411 else if(act == ActivationFunction::LU_BOUNDED_RELU)
412 {
413 tmp = std::min(a, std::max(b, in));
Georgios Pinitas57016a42019-01-16 12:54:29 +0000414 tmp = std::max<int32_t>(0, std::min<int32_t>(tmp * s + o, 255));
Georgios Pinitas5a594532018-12-03 14:30:05 +0000415 }
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000416 else if(act == ActivationFunction::LOGISTIC)
417 {
418 float tmp_f = scvt_f32_qasymm8(in, qi_in.scale, qi_in.offset);
419 tmp_f = 1.f / (1.f + std::exp(-tmp_f));
420 tmp = sqcvt_qasymm8_f32(tmp_f, qi_out.scale, qi_out.offset);
421 }
Georgios Pinitas5a594532018-12-03 14:30:05 +0000422 else
423 {
424 ARM_COMPUTE_ERROR("Unsupported activation function");
425 }
426 *(output_ptr + x) = tmp;
427 }
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000428 },
429 input, output);
430}
431
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000432Status NEActivationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
433{
434 ARM_COMPUTE_UNUSED(act_info);
435 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
436 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (output != nullptr) ? output->clone().get() : nullptr).first);
437
438 return Status{};
439}
440
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100441void NEActivationLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100442{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100443 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100444 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100445 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100446 ARM_COMPUTE_ERROR_ON(_func == nullptr);
447
448 (this->*_func)(window);
449}