blob: 8e91e6b4d1ba723b9a8057771312bc3d69cd6862 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
morgolock07df3d42020-02-27 11:46:28 +00002 * Copyright (c) 2017-2020 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"
giuros01c9573f32019-06-20 10:30:17 +010032#include "arm_compute/core/NEON/NESymm.h"
Georgios Pinitas5a594532018-12-03 14:30:05 +000033#include "arm_compute/core/NEON/wrapper/wrapper.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>
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010043#include <set>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044
45using namespace arm_compute;
Michalis Spyrouafa5d812017-11-30 14:25:57 +000046namespace
47{
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010048Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &activation_info)
Michalis Spyrouafa5d812017-11-30 14:25:57 +000049{
Anthony Barbiereaefd002018-07-20 17:49:35 +010050 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +000051 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::QSYMM16, DataType::F16, DataType::F32);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000052
morgolockaa85cdf2020-02-28 15:38:28 +000053 const static std::set<ActivationLayerInfo::ActivationFunction> qasymm8_supported_activations =
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010054 {
55 ActivationLayerInfo::ActivationFunction::RELU,
56 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
57 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
58 ActivationLayerInfo::ActivationFunction::LOGISTIC,
morgolockaa85cdf2020-02-28 15:38:28 +000059 ActivationLayerInfo::ActivationFunction::TANH,
60 ActivationLayerInfo::ActivationFunction::HARD_SWISH
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010061 };
morgolockaa85cdf2020-02-28 15:38:28 +000062 const static std::set<ActivationLayerInfo::ActivationFunction> qsymm16_supported_activations =
giuros01c9573f32019-06-20 10:30:17 +010063 {
64 ActivationLayerInfo::ActivationFunction::LOGISTIC,
morgolockaa85cdf2020-02-28 15:38:28 +000065 ActivationLayerInfo::ActivationFunction::TANH,
66 ActivationLayerInfo::ActivationFunction::HARD_SWISH
giuros01c9573f32019-06-20 10:30:17 +010067 };
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010068 const DataType data_type = input->data_type();
69 const QuantizationInfo &oq_info = (output != nullptr) ? output->quantization_info() : input->quantization_info();
70 const ActivationLayerInfo::ActivationFunction f_act = activation_info.activation();
71
giuros01c9573f32019-06-20 10:30:17 +010072 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized_asymmetric(data_type) && (qasymm8_supported_activations.count(f_act) == 0),
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010073 "For QASYMM8 only tanh, logistic, relu and lower/upper bounded relu are supported");
giuros01c9573f32019-06-20 10:30:17 +010074
75 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized_symmetric(data_type) && (qsymm16_supported_activations.count(f_act) == 0),
76 "For QSYMM16 only tanh and logistic are supported");
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +000077 ARM_COMPUTE_RETURN_ERROR_ON((data_type == DataType::QASYMM8 || data_type == DataType::QASYMM16) && (f_act == ActivationLayerInfo::ActivationFunction::TANH)
78 && (oq_info != QuantizationInfo(1.f / 128.f, 128)));
79 ARM_COMPUTE_RETURN_ERROR_ON((data_type == DataType::QASYMM8 || data_type == DataType::QASYMM16) && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
80 && (oq_info != QuantizationInfo(1.f / 256.f, 0)));
81
82 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 128.f, 0)));
83 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 256.f, -128)));
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010084
giuros01c9573f32019-06-20 10:30:17 +010085 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_symmetric(data_type) && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 32768.f, 0)));
86 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_symmetric(data_type) && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 32768.f, 0)));
87
Michalis Spyrouafa5d812017-11-30 14:25:57 +000088 // Checks performed when output is configured
89 if((output != nullptr) && (output->total_size() != 0))
90 {
91 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
92 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000093 }
94
95 return Status{};
96}
97
98std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
99{
Georgios Pinitas5a594532018-12-03 14:30:05 +0000100 // Configure kernel window
101 Window win = calculate_max_window(*input, Steps());
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000102
Georgios Pinitas5a594532018-12-03 14:30:05 +0000103 if(output != nullptr)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000104 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000105 // Output auto inizialitation if not yet initialized
106 auto_init_if_empty(*output, *input->clone());
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000107
Georgios Pinitas5a594532018-12-03 14:30:05 +0000108 // NEActivationLayerKernel doesn't need padding so update_window_and_padding() can be skipped
109 Coordinates coord;
110 coord.set_num_dimensions(output->num_dimensions());
111 output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000112 }
113
Georgios Pinitas5a594532018-12-03 14:30:05 +0000114 return std::make_pair(Status{}, win);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000115}
116} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117
118NEActivationLayerKernel::NEActivationLayerKernel()
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100119 : _input(nullptr), _output(nullptr), _func(nullptr), _act_info()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120{
121}
122
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100123void NEActivationLayerKernel::configure(ITensor *input, ITensor *output, ActivationLayerInfo activation_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124{
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000125 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100127 _input = input;
128 _act_info = activation_info;
129 _output = input;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100131 // Out-of-place calculation
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100132 if(output != nullptr)
133 {
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100134 _output = output;
135 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100137 // Disabled activation, thus no operation needed
138 if(!activation_info.enabled())
139 {
140 _func = nullptr;
141 }
142
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100143 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr, activation_info));
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000144
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 // Activation functions : FP32
146 static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_f32 =
147 {
148 { ActivationFunction::ABS, &NEActivationLayerKernel::activation<ActivationFunction::ABS, float> },
149 { ActivationFunction::LINEAR, &NEActivationLayerKernel::activation<ActivationFunction::LINEAR, float> },
150 { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, float> },
151 { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, float> },
152 { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, float> },
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100153 { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, float> },
Georgios Pinitas579c0492017-07-12 16:12:12 +0100154 { ActivationFunction::LEAKY_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LEAKY_RELU, float> },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 { ActivationFunction::SOFT_RELU, &NEActivationLayerKernel::activation<ActivationFunction::SOFT_RELU, float> },
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +0100156 { ActivationFunction::ELU, &NEActivationLayerKernel::activation<ActivationFunction::ELU, float> },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157 { ActivationFunction::SQRT, &NEActivationLayerKernel::activation<ActivationFunction::SQRT, float> },
158 { ActivationFunction::SQUARE, &NEActivationLayerKernel::activation<ActivationFunction::SQUARE, float> },
159 { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, float> },
Usama Arif80e55db2019-05-14 17:48:47 +0100160 { ActivationFunction::IDENTITY, &NEActivationLayerKernel::activation<ActivationFunction::IDENTITY, float> },
morgolock07df3d42020-02-27 11:46:28 +0000161 { ActivationFunction::HARD_SWISH, &NEActivationLayerKernel::activation<ActivationFunction::HARD_SWISH, float> },
162
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163 };
Pablo Tello91654c42017-07-05 11:32:17 +0100164
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000165#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello91654c42017-07-05 11:32:17 +0100166 // Activation functions : FP16
167 static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_f16 =
168 {
169 { ActivationFunction::ABS, &NEActivationLayerKernel::activation<ActivationFunction::ABS, float16_t> },
170 { ActivationFunction::LINEAR, &NEActivationLayerKernel::activation<ActivationFunction::LINEAR, float16_t> },
171 { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, float16_t> },
172 { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, float16_t> },
173 { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, float16_t> },
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100174 { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, float16_t> },
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100175 { ActivationFunction::LEAKY_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LEAKY_RELU, float16_t> },
Pablo Tello91654c42017-07-05 11:32:17 +0100176 { ActivationFunction::SOFT_RELU, &NEActivationLayerKernel::activation<ActivationFunction::SOFT_RELU, float16_t> },
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +0100177 { ActivationFunction::ELU, &NEActivationLayerKernel::activation<ActivationFunction::ELU, float16_t> },
Pablo Tello91654c42017-07-05 11:32:17 +0100178 { ActivationFunction::SQRT, &NEActivationLayerKernel::activation<ActivationFunction::SQRT, float16_t> },
179 { ActivationFunction::SQUARE, &NEActivationLayerKernel::activation<ActivationFunction::SQUARE, float16_t> },
180 { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, float16_t> },
Usama Arif80e55db2019-05-14 17:48:47 +0100181 { ActivationFunction::IDENTITY, &NEActivationLayerKernel::activation<ActivationFunction::IDENTITY, float16_t> },
morgolock07df3d42020-02-27 11:46:28 +0000182 { ActivationFunction::HARD_SWISH, &NEActivationLayerKernel::activation<ActivationFunction::HARD_SWISH, float16_t> },
183
Pablo Tello91654c42017-07-05 11:32:17 +0100184 };
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000185#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC*/
Pablo Tello91654c42017-07-05 11:32:17 +0100186
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000187 // Activation functions : QASYMM8_SIGNED
188 static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_qasymm8_signed =
189 {
190 { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, qasymm8_signed_t> },
191 { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, qasymm8_signed_t> },
192 { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, qasymm8_signed_t> },
193 { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, qasymm8_signed_t> },
194 { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, qasymm8_signed_t> },
195 { ActivationFunction::IDENTITY, &NEActivationLayerKernel::activation<ActivationFunction::IDENTITY, qasymm8_signed_t> },
morgolockaa85cdf2020-02-28 15:38:28 +0000196 { ActivationFunction::HARD_SWISH, &NEActivationLayerKernel::activation<ActivationFunction::HARD_SWISH, qasymm8_signed_t> },
197
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000198 };
199
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000200 // Activation functions : QASYMM8
201 static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_qasymm8 =
202 {
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000203 { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, qasymm8_t> },
Isabella Gottardi5d62c012019-01-29 15:05:41 +0000204 { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, qasymm8_t> },
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000205 { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, qasymm8_t> },
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000206 { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, qasymm8_t> },
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100207 { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, qasymm8_t> },
Usama Arif80e55db2019-05-14 17:48:47 +0100208 { ActivationFunction::IDENTITY, &NEActivationLayerKernel::activation<ActivationFunction::IDENTITY, qasymm8_t> },
morgolockaa85cdf2020-02-28 15:38:28 +0000209 { ActivationFunction::HARD_SWISH, &NEActivationLayerKernel::activation<ActivationFunction::HARD_SWISH, qasymm8_t> },
210
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000211 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100212
giuros01c9573f32019-06-20 10:30:17 +0100213 // Activation functions : QSYMM16
214 static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_qsymm16 =
215 {
216 { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, qsymm16_t> },
217 { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, qsymm16_t> },
morgolockaa85cdf2020-02-28 15:38:28 +0000218
giuros01c9573f32019-06-20 10:30:17 +0100219 };
220
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221 switch(input->info()->data_type())
222 {
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000223 case DataType::QASYMM8_SIGNED:
224 _func = act_map_qasymm8_signed[activation_info.activation()];
225 break;
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000226 case DataType::QASYMM8:
227 _func = act_map_qasymm8[activation_info.activation()];
228 break;
giuros01c9573f32019-06-20 10:30:17 +0100229 case DataType::QSYMM16:
230 _func = act_map_qsymm16[activation_info.activation()];
231 break;
Georgios Pinitasccc65d42017-06-27 17:39:11 +0100232 case DataType::F32:
233 _func = act_map_f32[activation_info.activation()];
234 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000235#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello91654c42017-07-05 11:32:17 +0100236 case DataType::F16:
237 _func = act_map_f16[activation_info.activation()];
238 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000239#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100240 default:
241 ARM_COMPUTE_ERROR("Unsupported data type.");
242 }
243
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100244 // Configure kernel window
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000245 auto win_config = validate_and_configure_window(input->info(), (output != nullptr) ? output->info() : nullptr);
246 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
247 ICPPKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100248}
249
Pablo Tello91654c42017-07-05 11:32:17 +0100250template <ActivationLayerInfo::ActivationFunction F, typename T>
Georgios Pinitas5a594532018-12-03 14:30:05 +0000251typename std::enable_if<arm_compute::utils::traits::is_floating_point<T>::value, void>::type
252NEActivationLayerKernel::activation(const Window &window)
Pablo Tello91654c42017-07-05 11:32:17 +0100253{
Georgios Pinitas5a594532018-12-03 14:30:05 +0000254 /** NEON vector tag type. */
255 using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<T, wrapper::traits::BitWidth::W128>;
Pablo Tello91654c42017-07-05 11:32:17 +0100256
Georgios Pinitas5a594532018-12-03 14:30:05 +0000257 const int window_step_x = 16 / sizeof(T);
258 const auto window_start_x = static_cast<int>(window.x().start());
259 const auto window_end_x = static_cast<int>(window.x().end());
260 const ActivationFunction act = F;
Pablo Tello91654c42017-07-05 11:32:17 +0100261
Georgios Pinitas5a594532018-12-03 14:30:05 +0000262 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
263 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100264
Georgios Pinitas5a594532018-12-03 14:30:05 +0000265 Iterator input(_input, win_collapsed);
266 Iterator output(_output, win_collapsed);
Pablo Tello91654c42017-07-05 11:32:17 +0100267
morgolock07df3d42020-02-27 11:46:28 +0000268 const auto epsilon = wrapper::vdup_n(static_cast<T>(1e-24), ExactTagType{});
269 const auto const_1 = wrapper::vdup_n(static_cast<T>(1.f), ExactTagType{});
270 const auto const_0 = wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{});
271 const auto const_6 = wrapper::vdup_n(static_cast<T>(6.f), ExactTagType{});
272 const auto const_3 = wrapper::vdup_n(static_cast<T>(3.f), ExactTagType{});
273 const auto const_inv_6 = wrapper::vdup_n(static_cast<T>(0.166666667f), ExactTagType{});
Georgios Pinitas5a594532018-12-03 14:30:05 +0000274
morgolock07df3d42020-02-27 11:46:28 +0000275 const auto va = wrapper::vdup_n(static_cast<T>(_act_info.a()), ExactTagType{});
276 const auto vb = wrapper::vdup_n(static_cast<T>(_act_info.b()), ExactTagType{});
277 const auto a = static_cast<T>(_act_info.a());
278 const auto b = static_cast<T>(_act_info.b());
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100279 execute_window_loop(win_collapsed, [&](const Coordinates &)
Pablo Tello91654c42017-07-05 11:32:17 +0100280 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000281 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
282 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
Pablo Tello91654c42017-07-05 11:32:17 +0100283
Georgios Pinitas5a594532018-12-03 14:30:05 +0000284 wrapper::traits::neon_bitvector_t<T, wrapper::traits::BitWidth::W128> tmp;
Pablo Tello91654c42017-07-05 11:32:17 +0100285
Georgios Pinitas5a594532018-12-03 14:30:05 +0000286 // Compute S elements per iteration
287 int x = window_start_x;
288 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Pablo Tello91654c42017-07-05 11:32:17 +0100289 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000290 const auto vin = wrapper::vloadq(input_ptr + x);
291 switch(act)
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100292 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000293 case ActivationFunction::ABS:
294 tmp = wrapper::vabs(vin);
295 break;
296 case ActivationFunction::LINEAR:
297 tmp = wrapper::vmla(vb, va, vin);
298 break;
299 case ActivationFunction::LOGISTIC:
300 tmp = wrapper::vinv(wrapper::vadd(const_1, wrapper::vexpq(wrapper::vneg(vin))));
301 break;
302 case ActivationFunction::RELU:
303 tmp = wrapper::vmax(const_0, vin);
304 break;
305 case ActivationFunction::BOUNDED_RELU:
306 tmp = wrapper::vmin(va, wrapper::vmax(const_0, vin));
307 break;
308 case ActivationFunction::LU_BOUNDED_RELU:
309 tmp = wrapper::vmin(va, wrapper::vmax(vb, vin));
310 break;
311 case ActivationFunction::LEAKY_RELU:
312 tmp = wrapper::vbsl(wrapper::vcgt(vin, const_0), vin, wrapper::vmul(va, vin));
313 break;
314 case ActivationFunction::SOFT_RELU:
315 tmp = wrapper::vlog(wrapper::vadd(const_1, wrapper::vexpq(vin)));
316 break;
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +0100317 case ActivationFunction::ELU:
318 tmp = wrapper::vbsl(wrapper::vcge(vin, const_0), vin, wrapper::vmul(va, wrapper::vsub(wrapper::vexpq(vin), const_1)));
319 break;
Georgios Pinitas5a594532018-12-03 14:30:05 +0000320 case ActivationFunction::SQRT:
Michele Di Giorgio8d5dd862019-09-13 12:23:46 +0100321 tmp = wrapper::vinv(wrapper::vinvsqrt(vin + epsilon));
Georgios Pinitas5a594532018-12-03 14:30:05 +0000322 break;
323 case ActivationFunction::SQUARE:
324 tmp = wrapper::vmul(vin, vin);
325 break;
326 case ActivationFunction::TANH:
327 tmp = wrapper::vmul(va, wrapper::vtanh(wrapper::vmul(vb, vin)));
328 break;
Usama Arif80e55db2019-05-14 17:48:47 +0100329 case ActivationFunction::IDENTITY:
330 tmp = vin;
331 break;
morgolock07df3d42020-02-27 11:46:28 +0000332 case ActivationFunction::HARD_SWISH:
333 tmp = wrapper::vmul(vin, wrapper::vmul(const_inv_6, wrapper::vmin(const_6, wrapper::vmax(const_0, wrapper::vadd(vin, const_3)))));
334 break;
Georgios Pinitas5a594532018-12-03 14:30:05 +0000335 default:
336 ARM_COMPUTE_ERROR("Unsupported activation function");
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100337 }
Georgios Pinitas5a594532018-12-03 14:30:05 +0000338 wrapper::vstore(output_ptr + x, tmp);
Pablo Tello91654c42017-07-05 11:32:17 +0100339 }
340
Georgios Pinitas5a594532018-12-03 14:30:05 +0000341 // Compute left-over elements
342 for(; x < window_end_x; ++x)
Georgios Pinitasf525eab2018-01-30 14:47:39 +0000343 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000344 const T in = *(reinterpret_cast<const T *>(input_ptr + x));
345 T tmp;
346 switch(act)
Georgios Pinitasf525eab2018-01-30 14:47:39 +0000347 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000348 case ActivationFunction::ABS:
349 tmp = std::abs(in);
350 break;
351 case ActivationFunction::LINEAR:
352 tmp = a * in + b;
353 break;
354 case ActivationFunction::LOGISTIC:
355 tmp = static_cast<T>(1) / (static_cast<T>(1) + std::exp(-in));
356 break;
357 case ActivationFunction::RELU:
358 tmp = std::max<T>(static_cast<T>(0), in);
359 break;
360 case ActivationFunction::BOUNDED_RELU:
361 tmp = std::min<T>(a, std::max(static_cast<T>(0), in));
362 break;
363 case ActivationFunction::LU_BOUNDED_RELU:
364 tmp = std::min<T>(a, std::max<T>(b, in));
365 break;
366 case ActivationFunction::LEAKY_RELU:
367 tmp = (in > 0) ? in : a * in;
368 break;
369 case ActivationFunction::SOFT_RELU:
370 tmp = std::log(static_cast<T>(1) + std::exp(in));
371 break;
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +0100372 case ActivationFunction::ELU:
373 tmp = (in >= 0) ? in : a * (std::exp(in) - 1);
374 break;
Georgios Pinitas5a594532018-12-03 14:30:05 +0000375 case ActivationFunction::SQRT:
376 tmp = std::sqrt(in);
377 break;
378 case ActivationFunction::SQUARE:
379 tmp = in * in;
380 break;
381 case ActivationFunction::TANH:
382 tmp = a * std::tanh(b * in);
383 break;
Usama Arif80e55db2019-05-14 17:48:47 +0100384 case ActivationFunction::IDENTITY:
385 tmp = in;
386 break;
morgolock07df3d42020-02-27 11:46:28 +0000387 case ActivationFunction::HARD_SWISH:
388 tmp = in * ((std::min(std::max((in + 3), 0.0f), 6.0f)) * 0.166666667f);
389 break;
Georgios Pinitas5a594532018-12-03 14:30:05 +0000390 default:
391 ARM_COMPUTE_ERROR("Unsupported activation function");
Georgios Pinitasf525eab2018-01-30 14:47:39 +0000392 }
Georgios Pinitas5a594532018-12-03 14:30:05 +0000393 *(output_ptr + x) = tmp;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100394 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100395 },
396 input, output);
397}
398
399template <ActivationLayerInfo::ActivationFunction F, typename T>
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000400typename std::enable_if<std::is_same<T, qasymm8_t>::value, void>::type NEActivationLayerKernel::activation(const Window &window)
401{
Georgios Pinitas5a594532018-12-03 14:30:05 +0000402 const int window_step_x = 16 / sizeof(T);
403 const auto window_start_x = static_cast<int>(window.x().start());
404 const auto window_end_x = static_cast<int>(window.x().end());
405 const ActivationFunction act = F;
406
407 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
408 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
409
410 Iterator input(_input, win_collapsed);
411 Iterator output(_output, win_collapsed);
412
morgolockaa85cdf2020-02-28 15:38:28 +0000413 const UniformQuantizationInfo qi_in = _input->info()->quantization_info().uniform();
414 const UniformQuantizationInfo qi_out = _output->info()->quantization_info().uniform();
415 const qasymm8x16_t va = vdupq_n_u8(quantize_qasymm8(_act_info.a(), qi_in));
416 const qasymm8x16_t vb = vdupq_n_u8(quantize_qasymm8(_act_info.b(), qi_in));
417 const qasymm8_t a = quantize_qasymm8(_act_info.a(), qi_in);
418 const qasymm8_t b = quantize_qasymm8(_act_info.b(), qi_in);
419 const qasymm8_t const_0 = quantize_qasymm8(0.f, qi_in);
420 const qasymm8x16_t vconst_0 = vdupq_n_u8(const_0);
421 const auto vconst_1 = vdupq_n_f32(1.f);
422 const float32x4_t va_f32 = vdupq_n_f32(_act_info.a());
423 const float32x4_t vb_f32 = vdupq_n_f32(_act_info.b());
424 const float a_f32 = _act_info.a();
425 const float b_f32 = _act_info.b();
426 const auto const_6_f32 = vdupq_n_f32(6.f);
427 const auto const_0_f32 = vdupq_n_f32(0.f);
428 const auto const_3_f32 = vdupq_n_f32(3.f);
429 const auto const_inv_6_f32 = vdupq_n_f32(0.166666667f);
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000430
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000431 // Initialise scale/offset for re-quantization
432 float s = qi_in.scale / qi_out.scale;
433 float o = -qi_in.offset * s + qi_out.offset;
434 float32x4_t vs = vdupq_n_f32(s);
435 float32x4_t vo = vdupq_n_f32(o);
436
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100437 execute_window_loop(win_collapsed, [&](const Coordinates &)
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000438 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000439 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
440 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000441
Georgios Pinitas5a594532018-12-03 14:30:05 +0000442 wrapper::traits::neon_bitvector_t<T, wrapper::traits::BitWidth::W128> tmp;
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000443
Georgios Pinitas5a594532018-12-03 14:30:05 +0000444 // Compute S elements per iteration
445 int x = window_start_x;
446 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000447 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000448 const auto vin = wrapper::vloadq(input_ptr + x);
449 if(act == ActivationFunction::RELU)
450 {
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000451 // Perform activation
Georgios Pinitas5a594532018-12-03 14:30:05 +0000452 tmp = vmaxq_u8(vconst_0, vin);
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000453 // Re-quantize to new output space
454 tmp = vmlaq_qasymm8(tmp, vs, vo);
Georgios Pinitas5a594532018-12-03 14:30:05 +0000455 }
456 else if(act == ActivationFunction::BOUNDED_RELU)
457 {
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000458 // Perform activation
Georgios Pinitas5a594532018-12-03 14:30:05 +0000459 tmp = vminq_u8(va, vmaxq_u8(vconst_0, vin));
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000460 // Re-quantize to new output space
461 tmp = vmlaq_qasymm8(tmp, vs, vo);
Georgios Pinitas5a594532018-12-03 14:30:05 +0000462 }
463 else if(act == ActivationFunction::LU_BOUNDED_RELU)
464 {
465 // Perform activation
466 tmp = vminq_u8(va, vmaxq_u8(vb, vin));
467 // Re-quantize to new output space
468 tmp = vmlaq_qasymm8(tmp, vs, vo);
469 }
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000470 else if(act == ActivationFunction::LOGISTIC)
471 {
Isabella Gottardif7a3bf22019-03-15 14:58:24 +0000472 // De-quantize
473 const auto vin_deq = vdequantize(vin, qi_in);
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000474 // Perform activation
Isabella Gottardif7a3bf22019-03-15 14:58:24 +0000475 const float32x4x4_t tmp_dep =
476 {
477 {
478 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[0])))),
479 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[1])))),
480 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[2])))),
481 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[3])))),
482 }
483 };
484 // Re-quantize to new output space
485 tmp = vquantize(tmp_dep, qi_out);
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000486 }
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100487 else if(act == ActivationFunction::TANH)
488 {
489 // De-quantize
490 const auto vin_deq = vdequantize(vin, qi_in);
491 // Perform activation
492 const float32x4x4_t tmp_dep =
493 {
494 {
495 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[0], vb_f32))),
496 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[1], vb_f32))),
497 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[2], vb_f32))),
498 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[3], vb_f32))),
499 }
500 };
501 // Re-quantize to new output space
502 tmp = vquantize(tmp_dep, qi_out);
503 }
morgolockaa85cdf2020-02-28 15:38:28 +0000504 else if(act == ActivationFunction::HARD_SWISH)
505 {
506 // De-quantize
507 const auto vin_deq = vdequantize(vin, qi_in);
508 // Perform activation
509 const float32x4x4_t tmp_dep =
510 {
511 {
512 wrapper::vmul(vin_deq.val[0], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[0], const_3_f32))))),
513 wrapper::vmul(vin_deq.val[1], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[1], const_3_f32))))),
514 wrapper::vmul(vin_deq.val[2], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[2], const_3_f32))))),
515 wrapper::vmul(vin_deq.val[3], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[3], const_3_f32))))),
516 }
517 };
518 // Re-quantize to new output space
519 tmp = vquantize(tmp_dep, qi_out);
520 }
Georgios Pinitas5a594532018-12-03 14:30:05 +0000521 else
522 {
523 ARM_COMPUTE_ERROR("Unsupported activation function");
524 }
525 wrapper::vstore(output_ptr + x, tmp);
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000526 }
527
Georgios Pinitas5a594532018-12-03 14:30:05 +0000528 // Compute left-over elements
529 for(; x < window_end_x; ++x)
530 {
531 T in = *(reinterpret_cast<const T *>(input_ptr + x));
532 T tmp;
533 if(act == ActivationFunction::RELU)
534 {
535 tmp = std::max(const_0, in);
Michele Di Giorgiob70770e2020-04-22 12:26:10 +0100536 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
Georgios Pinitas5a594532018-12-03 14:30:05 +0000537 }
538 else if(act == ActivationFunction::BOUNDED_RELU)
539 {
540 tmp = std::min(a, std::max(const_0, in));
Michele Di Giorgiob70770e2020-04-22 12:26:10 +0100541 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
Georgios Pinitas5a594532018-12-03 14:30:05 +0000542 }
543 else if(act == ActivationFunction::LU_BOUNDED_RELU)
544 {
545 tmp = std::min(a, std::max(b, in));
Michele Di Giorgiob70770e2020-04-22 12:26:10 +0100546 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
Georgios Pinitas5a594532018-12-03 14:30:05 +0000547 }
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000548 else if(act == ActivationFunction::LOGISTIC)
549 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100550 float tmp_f = dequantize_qasymm8(in, qi_in);
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000551 tmp_f = 1.f / (1.f + std::exp(-tmp_f));
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100552 tmp = quantize_qasymm8(tmp_f, qi_out);
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000553 }
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100554 else if(act == ActivationFunction::TANH)
555 {
556 float tmp_f = dequantize_qasymm8(in, qi_in);
557 tmp_f = a_f32 * std::tanh(b_f32 * tmp_f);
558 tmp = quantize_qasymm8(tmp_f, qi_out);
559 }
morgolockaa85cdf2020-02-28 15:38:28 +0000560 else if(act == ActivationFunction::HARD_SWISH)
561 {
562 float tmp_f = dequantize_qasymm8(in, qi_in);
563 tmp_f = tmp_f * ((std::min(std::max((tmp_f + 3), 0.0f), 6.0f)) * 0.166666667f);
564 tmp = quantize_qasymm8(tmp_f, qi_out);
565 }
Georgios Pinitas5a594532018-12-03 14:30:05 +0000566 else
567 {
568 ARM_COMPUTE_ERROR("Unsupported activation function");
569 }
570 *(output_ptr + x) = tmp;
571 }
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000572 },
573 input, output);
574}
575
giuros01c9573f32019-06-20 10:30:17 +0100576template <ActivationLayerInfo::ActivationFunction F, typename T>
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000577typename std::enable_if<std::is_same<T, qasymm8_signed_t>::value, void>::type NEActivationLayerKernel::activation(const Window &window)
578{
579 const int window_step_x = 16 / sizeof(T);
580 const auto window_start_x = static_cast<int>(window.x().start());
581 const auto window_end_x = static_cast<int>(window.x().end());
582 const ActivationFunction act = F;
583
584 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
585 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
586
587 Iterator input(_input, win_collapsed);
588 Iterator output(_output, win_collapsed);
589
morgolockaa85cdf2020-02-28 15:38:28 +0000590 const UniformQuantizationInfo qi_in = _input->info()->quantization_info().uniform();
591 const UniformQuantizationInfo qi_out = _output->info()->quantization_info().uniform();
592 const qasymm8x16_signed_t va = vdupq_n_s8(quantize_qasymm8_signed(_act_info.a(), qi_in));
593 const qasymm8x16_signed_t vb = vdupq_n_s8(quantize_qasymm8_signed(_act_info.b(), qi_in));
594 const qasymm8_signed_t a = quantize_qasymm8_signed(_act_info.a(), qi_in);
595 const qasymm8_signed_t b = quantize_qasymm8_signed(_act_info.b(), qi_in);
596 const qasymm8_signed_t const_0 = quantize_qasymm8_signed(0.f, qi_in);
597 const qasymm8x16_signed_t vconst_0 = vdupq_n_s8(const_0);
598 const auto vconst_1 = vdupq_n_f32(1.f);
599 const float32x4_t va_f32 = vdupq_n_f32(_act_info.a());
600 const float32x4_t vb_f32 = vdupq_n_f32(_act_info.b());
601 const float a_f32 = _act_info.a();
602 const float b_f32 = _act_info.b();
603 const auto const_6_f32 = vdupq_n_f32(6.f);
604 const auto const_0_f32 = vdupq_n_f32(0.f);
605 const auto const_3_f32 = vdupq_n_f32(3.f);
606 const auto const_inv_6_f32 = vdupq_n_f32(0.166666667f);
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000607
608 // Initialise scale/offset for re-quantization
609 float s = qi_in.scale / qi_out.scale;
610 float o = -qi_in.offset * s + qi_out.offset;
611 float32x4_t vs = vdupq_n_f32(s);
612 float32x4_t vo = vdupq_n_f32(o);
613
614 execute_window_loop(win_collapsed, [&](const Coordinates &)
615 {
616 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
617 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
618
619 wrapper::traits::neon_bitvector_t<T, wrapper::traits::BitWidth::W128> tmp;
620
621 // Compute S elements per iteration
622 int x = window_start_x;
623 for(; x <= (window_end_x - window_step_x); x += window_step_x)
624 {
625 const auto vin = wrapper::vloadq(input_ptr + x);
626 if(act == ActivationFunction::RELU)
627 {
628 // Perform activation
629 tmp = vmaxq_s8(vconst_0, vin);
630 // Re-quantize to new output space
631 tmp = vmlaq_qasymm8_signed(tmp, vs, vo);
632 }
633 else if(act == ActivationFunction::BOUNDED_RELU)
634 {
635 // Perform activation
636 tmp = vminq_s8(va, vmaxq_s8(vconst_0, vin));
637 // Re-quantize to new output space
638 tmp = vmlaq_qasymm8_signed(tmp, vs, vo);
639 }
640 else if(act == ActivationFunction::LU_BOUNDED_RELU)
641 {
642 // Perform activation
643 tmp = vminq_s8(va, vmaxq_s8(vb, vin));
644 // Re-quantize to new output space
645 tmp = vmlaq_qasymm8_signed(tmp, vs, vo);
646 }
647 else if(act == ActivationFunction::LOGISTIC)
648 {
649 // De-quantize
650 const auto vin_deq = vdequantize(vin, qi_in);
651 // Perform activation
652 const float32x4x4_t tmp_dep =
653 {
654 {
655 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[0])))),
656 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[1])))),
657 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[2])))),
658 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[3])))),
659 }
660 };
661 // Re-quantize to new output space
662 tmp = vquantize_signed(tmp_dep, qi_out);
663 }
664 else if(act == ActivationFunction::TANH)
665 {
666 // De-quantize
667 const auto vin_deq = vdequantize(vin, qi_in);
668 // Perform activation
669 const float32x4x4_t tmp_dep =
670 {
671 {
672 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[0], vb_f32))),
673 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[1], vb_f32))),
674 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[2], vb_f32))),
675 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[3], vb_f32))),
676 }
677 };
678 // Re-quantize to new output space
679 tmp = vquantize_signed(tmp_dep, qi_out);
680 }
morgolockaa85cdf2020-02-28 15:38:28 +0000681 else if(act == ActivationFunction::HARD_SWISH)
682 {
683 // De-quantize
684 const auto vin_deq = vdequantize(vin, qi_in);
685 // Perform activation
686 const float32x4x4_t tmp_dep =
687 {
688 {
689 wrapper::vmul(vin_deq.val[0], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[0], const_3_f32))))),
690 wrapper::vmul(vin_deq.val[1], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[1], const_3_f32))))),
691 wrapper::vmul(vin_deq.val[2], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[2], const_3_f32))))),
692 wrapper::vmul(vin_deq.val[3], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[3], const_3_f32))))),
693 }
694 };
695 // Re-quantize to new output space
696 tmp = vquantize_signed(tmp_dep, qi_out);
697 }
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000698 else
699 {
700 ARM_COMPUTE_ERROR("Unsupported activation function");
701 }
702 wrapper::vstore(output_ptr + x, tmp);
703 }
704
705 // Compute left-over elements
706 for(; x < window_end_x; ++x)
707 {
708 T in = *(reinterpret_cast<const T *>(input_ptr + x));
709 T tmp;
710 if(act == ActivationFunction::RELU)
711 {
712 tmp = std::max(const_0, in);
Michele Di Giorgiob70770e2020-04-22 12:26:10 +0100713 tmp = utility::clamp<int32_t, qasymm8_signed_t>(tmp * s + o);
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000714 }
715 else if(act == ActivationFunction::BOUNDED_RELU)
716 {
717 tmp = std::min(a, std::max(const_0, in));
Michele Di Giorgiob70770e2020-04-22 12:26:10 +0100718 tmp = utility::clamp<int32_t, qasymm8_signed_t>(tmp * s + o);
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000719 }
720 else if(act == ActivationFunction::LU_BOUNDED_RELU)
721 {
722 tmp = std::min(a, std::max(b, in));
Michele Di Giorgiob70770e2020-04-22 12:26:10 +0100723 tmp = utility::clamp<int32_t, qasymm8_signed_t>(tmp * s + o);
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000724 }
725 else if(act == ActivationFunction::LOGISTIC)
726 {
727 float tmp_f = dequantize_qasymm8_signed(in, qi_in);
728 tmp_f = 1.f / (1.f + std::exp(-tmp_f));
729 tmp = quantize_qasymm8_signed(tmp_f, qi_out);
730 }
731 else if(act == ActivationFunction::TANH)
732 {
733 float tmp_f = dequantize_qasymm8_signed(in, qi_in);
734 tmp_f = a_f32 * std::tanh(b_f32 * tmp_f);
735 tmp = quantize_qasymm8_signed(tmp_f, qi_out);
736 }
morgolockaa85cdf2020-02-28 15:38:28 +0000737 else if(act == ActivationFunction::HARD_SWISH)
738 {
739 float tmp_f = dequantize_qasymm8_signed(in, qi_in);
740 tmp_f = tmp_f * ((std::min(std::max((tmp_f + 3), 0.0f), 6.0f)) * 0.166666667f);
741 tmp = quantize_qasymm8_signed(tmp_f, qi_out);
742 }
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000743 else
744 {
745 ARM_COMPUTE_ERROR("Unsupported activation function");
746 }
747 *(output_ptr + x) = tmp;
748 }
749 },
750 input, output);
751}
752
753template <ActivationLayerInfo::ActivationFunction F, typename T>
giuros01c9573f32019-06-20 10:30:17 +0100754typename std::enable_if<std::is_same<T, qsymm16_t>::value, void>::type NEActivationLayerKernel::activation(const Window &window)
755{
756 const int window_step_x = 16 / sizeof(T);
757 const auto window_start_x = static_cast<int>(window.x().start());
758 const auto window_end_x = static_cast<int>(window.x().end());
759 const ActivationFunction act = F;
760
761 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
762 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
763
764 Iterator input(_input, win_collapsed);
765 Iterator output(_output, win_collapsed);
766
767 const UniformQuantizationInfo qi_in = _input->info()->quantization_info().uniform();
768 const UniformQuantizationInfo qi_out = _output->info()->quantization_info().uniform();
769 const auto vconst_1 = vdupq_n_f32(1.f);
770 const float32x4_t va_f32 = vdupq_n_f32(_act_info.a());
771 const float32x4_t vb_f32 = vdupq_n_f32(_act_info.b());
772 const float a_f32 = _act_info.a();
773 const float b_f32 = _act_info.b();
774
775 execute_window_loop(win_collapsed, [&](const Coordinates &)
776 {
777 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
778 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
779
780 wrapper::traits::neon_bitvector_t<T, wrapper::traits::BitWidth::W128> tmp;
781 ARM_COMPUTE_UNUSED(tmp);
782
783 // Compute S elements per iteration
784 int x = window_start_x;
785 for(; x <= (window_end_x - window_step_x); x += window_step_x)
786 {
787 const auto vin = wrapper::vloadq(input_ptr + x);
788 if(act == ActivationFunction::LOGISTIC)
789 {
790 // De-quantize
791 const auto vin_deq = vdequantize_int16(vin, qi_in.scale);
792 // Perform activation
793 const float32x4x2_t tmp_dep =
794 {
795 {
796 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[0])))),
797 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[1])))),
798 }
799 };
800 // Re-quantize to new output space
801 tmp = vquantize_int16(tmp_dep, qi_out.scale);
802 }
803 else if(act == ActivationFunction::TANH)
804 {
805 // De-quantize
806 const auto vin_deq = vdequantize_int16(vin, qi_in.scale);
807 // Perform activation
808 const float32x4x2_t tmp_dep =
809 {
810 {
811 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[0], vb_f32))),
812 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[1], vb_f32))),
813 }
814 };
815 // Re-quantize to new output space
816 tmp = vquantize_int16(tmp_dep, qi_out.scale);
817 }
818 else
819 {
820 ARM_COMPUTE_ERROR("Unsupported activation function");
821 }
822 wrapper::vstore(output_ptr + x, tmp);
823 }
824
825 // Compute left-over elements
826 for(; x < window_end_x; ++x)
827 {
828 T in = *(reinterpret_cast<const T *>(input_ptr + x));
829 T tmp;
830 if(act == ActivationFunction::LOGISTIC)
831 {
832 float tmp_f = dequantize_qsymm16(in, qi_in.scale);
833 tmp_f = 1.f / (1.f + std::exp(-tmp_f));
834 tmp = quantize_qsymm16(tmp_f, qi_out);
835 }
836 else if(act == ActivationFunction::TANH)
837 {
838 float tmp_f = dequantize_qsymm16(in, qi_in.scale);
839 tmp_f = a_f32 * std::tanh(b_f32 * tmp_f);
840 tmp = quantize_qsymm16(tmp_f, qi_out);
841 }
842 else
843 {
844 ARM_COMPUTE_ERROR("Unsupported activation function");
845 }
846 *(output_ptr + x) = tmp;
847 }
848 },
849 input, output);
850}
851
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000852Status NEActivationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
853{
854 ARM_COMPUTE_UNUSED(act_info);
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100855 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, act_info));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000856 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (output != nullptr) ? output->clone().get() : nullptr).first);
857
858 return Status{};
859}
860
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100861void NEActivationLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100862{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100863 // Early exit on disabled activation
864 if(!_act_info.enabled())
865 {
866 return;
867 }
868
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100869 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100870 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100871 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100872 ARM_COMPUTE_ERROR_ON(_func == nullptr);
873
874 (this->*_func)(window);
875}