blob: 2c00a76305ee4c1c6aa3b967af60eda044469287 [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
Georgios Pinitas1fd2c802020-06-16 17:44:46 +010098std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input, ITensorInfo *output)
Michalis Spyrouafa5d812017-11-30 14:25:57 +000099{
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 Pinitas1fd2c802020-06-16 17:44:46 +0100119 : _func(nullptr), _act_info()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120{
121}
122
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100123void NEActivationLayerKernel::configure(const ITensorInfo *input, ITensorInfo *output, ActivationLayerInfo activation_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124{
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100125 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100127 _act_info = activation_info;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100129 // Disabled activation, thus no operation needed
130 if(!activation_info.enabled())
131 {
132 _func = nullptr;
133 }
134
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100135 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, output, activation_info));
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000136
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137 // Activation functions : FP32
138 static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_f32 =
139 {
140 { ActivationFunction::ABS, &NEActivationLayerKernel::activation<ActivationFunction::ABS, float> },
141 { ActivationFunction::LINEAR, &NEActivationLayerKernel::activation<ActivationFunction::LINEAR, float> },
142 { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, float> },
143 { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, float> },
144 { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, float> },
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100145 { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, float> },
Georgios Pinitas579c0492017-07-12 16:12:12 +0100146 { ActivationFunction::LEAKY_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LEAKY_RELU, float> },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147 { ActivationFunction::SOFT_RELU, &NEActivationLayerKernel::activation<ActivationFunction::SOFT_RELU, float> },
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +0100148 { ActivationFunction::ELU, &NEActivationLayerKernel::activation<ActivationFunction::ELU, float> },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 { ActivationFunction::SQRT, &NEActivationLayerKernel::activation<ActivationFunction::SQRT, float> },
150 { ActivationFunction::SQUARE, &NEActivationLayerKernel::activation<ActivationFunction::SQUARE, float> },
151 { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, float> },
Usama Arif80e55db2019-05-14 17:48:47 +0100152 { ActivationFunction::IDENTITY, &NEActivationLayerKernel::activation<ActivationFunction::IDENTITY, float> },
morgolock07df3d42020-02-27 11:46:28 +0000153 { ActivationFunction::HARD_SWISH, &NEActivationLayerKernel::activation<ActivationFunction::HARD_SWISH, float> },
154
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 };
Pablo Tello91654c42017-07-05 11:32:17 +0100156
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000157#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello91654c42017-07-05 11:32:17 +0100158 // Activation functions : FP16
159 static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_f16 =
160 {
161 { ActivationFunction::ABS, &NEActivationLayerKernel::activation<ActivationFunction::ABS, float16_t> },
162 { ActivationFunction::LINEAR, &NEActivationLayerKernel::activation<ActivationFunction::LINEAR, float16_t> },
163 { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, float16_t> },
164 { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, float16_t> },
165 { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, float16_t> },
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100166 { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, float16_t> },
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100167 { ActivationFunction::LEAKY_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LEAKY_RELU, float16_t> },
Pablo Tello91654c42017-07-05 11:32:17 +0100168 { ActivationFunction::SOFT_RELU, &NEActivationLayerKernel::activation<ActivationFunction::SOFT_RELU, float16_t> },
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +0100169 { ActivationFunction::ELU, &NEActivationLayerKernel::activation<ActivationFunction::ELU, float16_t> },
Pablo Tello91654c42017-07-05 11:32:17 +0100170 { ActivationFunction::SQRT, &NEActivationLayerKernel::activation<ActivationFunction::SQRT, float16_t> },
171 { ActivationFunction::SQUARE, &NEActivationLayerKernel::activation<ActivationFunction::SQUARE, float16_t> },
172 { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, float16_t> },
Usama Arif80e55db2019-05-14 17:48:47 +0100173 { ActivationFunction::IDENTITY, &NEActivationLayerKernel::activation<ActivationFunction::IDENTITY, float16_t> },
morgolock07df3d42020-02-27 11:46:28 +0000174 { ActivationFunction::HARD_SWISH, &NEActivationLayerKernel::activation<ActivationFunction::HARD_SWISH, float16_t> },
175
Pablo Tello91654c42017-07-05 11:32:17 +0100176 };
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000177#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC*/
Pablo Tello91654c42017-07-05 11:32:17 +0100178
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000179 // Activation functions : QASYMM8_SIGNED
180 static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_qasymm8_signed =
181 {
182 { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, qasymm8_signed_t> },
183 { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, qasymm8_signed_t> },
184 { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, qasymm8_signed_t> },
185 { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, qasymm8_signed_t> },
186 { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, qasymm8_signed_t> },
187 { ActivationFunction::IDENTITY, &NEActivationLayerKernel::activation<ActivationFunction::IDENTITY, qasymm8_signed_t> },
morgolockaa85cdf2020-02-28 15:38:28 +0000188 { ActivationFunction::HARD_SWISH, &NEActivationLayerKernel::activation<ActivationFunction::HARD_SWISH, qasymm8_signed_t> },
189
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000190 };
191
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000192 // Activation functions : QASYMM8
193 static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_qasymm8 =
194 {
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000195 { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, qasymm8_t> },
Isabella Gottardi5d62c012019-01-29 15:05:41 +0000196 { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, qasymm8_t> },
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000197 { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, qasymm8_t> },
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000198 { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, qasymm8_t> },
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100199 { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, qasymm8_t> },
Usama Arif80e55db2019-05-14 17:48:47 +0100200 { ActivationFunction::IDENTITY, &NEActivationLayerKernel::activation<ActivationFunction::IDENTITY, qasymm8_t> },
morgolockaa85cdf2020-02-28 15:38:28 +0000201 { ActivationFunction::HARD_SWISH, &NEActivationLayerKernel::activation<ActivationFunction::HARD_SWISH, qasymm8_t> },
202
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000203 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204
giuros01c9573f32019-06-20 10:30:17 +0100205 // Activation functions : QSYMM16
206 static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_qsymm16 =
207 {
208 { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, qsymm16_t> },
209 { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, qsymm16_t> },
morgolockaa85cdf2020-02-28 15:38:28 +0000210
giuros01c9573f32019-06-20 10:30:17 +0100211 };
212
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100213 switch(input->data_type())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100214 {
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000215 case DataType::QASYMM8_SIGNED:
216 _func = act_map_qasymm8_signed[activation_info.activation()];
217 break;
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000218 case DataType::QASYMM8:
219 _func = act_map_qasymm8[activation_info.activation()];
220 break;
giuros01c9573f32019-06-20 10:30:17 +0100221 case DataType::QSYMM16:
222 _func = act_map_qsymm16[activation_info.activation()];
223 break;
Georgios Pinitasccc65d42017-06-27 17:39:11 +0100224 case DataType::F32:
225 _func = act_map_f32[activation_info.activation()];
226 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000227#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello91654c42017-07-05 11:32:17 +0100228 case DataType::F16:
229 _func = act_map_f16[activation_info.activation()];
230 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000231#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100232 default:
233 ARM_COMPUTE_ERROR("Unsupported data type.");
234 }
235
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100236 // Configure kernel window
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100237 auto win_config = validate_and_configure_window(input, output);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000238 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
239 ICPPKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100240}
241
Pablo Tello91654c42017-07-05 11:32:17 +0100242template <ActivationLayerInfo::ActivationFunction F, typename T>
Georgios Pinitas5a594532018-12-03 14:30:05 +0000243typename std::enable_if<arm_compute::utils::traits::is_floating_point<T>::value, void>::type
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100244NEActivationLayerKernel::activation(const ITensor *src, ITensor *dst, const Window &window)
Pablo Tello91654c42017-07-05 11:32:17 +0100245{
Georgios Pinitas5a594532018-12-03 14:30:05 +0000246 /** NEON vector tag type. */
247 using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<T, wrapper::traits::BitWidth::W128>;
Pablo Tello91654c42017-07-05 11:32:17 +0100248
Georgios Pinitas5a594532018-12-03 14:30:05 +0000249 const int window_step_x = 16 / sizeof(T);
250 const auto window_start_x = static_cast<int>(window.x().start());
251 const auto window_end_x = static_cast<int>(window.x().end());
252 const ActivationFunction act = F;
Pablo Tello91654c42017-07-05 11:32:17 +0100253
Georgios Pinitas5a594532018-12-03 14:30:05 +0000254 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
255 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100256
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100257 Iterator input(src, win_collapsed);
258 Iterator output(dst, win_collapsed);
Pablo Tello91654c42017-07-05 11:32:17 +0100259
SiCong Lia32e2ae2020-06-08 17:30:51 +0100260 // A small delta added to the input to prevent NAN values caused by zeros in inputs to SQRT
261#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100262 const auto delta = wrapper::vdup_n(static_cast<T>(1e-7), ExactTagType {});
SiCong Lia32e2ae2020-06-08 17:30:51 +0100263#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100264 const auto delta = wrapper::vdup_n(static_cast<T>(1e-24), ExactTagType {});
SiCong Lia32e2ae2020-06-08 17:30:51 +0100265#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100266 const auto const_1 = wrapper::vdup_n(static_cast<T>(1.f), ExactTagType {});
morgolock07df3d42020-02-27 11:46:28 +0000267 const auto const_0 = wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{});
268 const auto const_6 = wrapper::vdup_n(static_cast<T>(6.f), ExactTagType{});
269 const auto const_3 = wrapper::vdup_n(static_cast<T>(3.f), ExactTagType{});
270 const auto const_inv_6 = wrapper::vdup_n(static_cast<T>(0.166666667f), ExactTagType{});
Georgios Pinitas5a594532018-12-03 14:30:05 +0000271
morgolock07df3d42020-02-27 11:46:28 +0000272 const auto va = wrapper::vdup_n(static_cast<T>(_act_info.a()), ExactTagType{});
273 const auto vb = wrapper::vdup_n(static_cast<T>(_act_info.b()), ExactTagType{});
274 const auto a = static_cast<T>(_act_info.a());
275 const auto b = static_cast<T>(_act_info.b());
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100276 execute_window_loop(win_collapsed, [&](const Coordinates &)
Pablo Tello91654c42017-07-05 11:32:17 +0100277 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000278 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
279 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
Pablo Tello91654c42017-07-05 11:32:17 +0100280
Georgios Pinitas5a594532018-12-03 14:30:05 +0000281 wrapper::traits::neon_bitvector_t<T, wrapper::traits::BitWidth::W128> tmp;
Pablo Tello91654c42017-07-05 11:32:17 +0100282
Georgios Pinitas5a594532018-12-03 14:30:05 +0000283 // Compute S elements per iteration
284 int x = window_start_x;
285 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Pablo Tello91654c42017-07-05 11:32:17 +0100286 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000287 const auto vin = wrapper::vloadq(input_ptr + x);
288 switch(act)
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100289 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000290 case ActivationFunction::ABS:
291 tmp = wrapper::vabs(vin);
292 break;
293 case ActivationFunction::LINEAR:
294 tmp = wrapper::vmla(vb, va, vin);
295 break;
296 case ActivationFunction::LOGISTIC:
297 tmp = wrapper::vinv(wrapper::vadd(const_1, wrapper::vexpq(wrapper::vneg(vin))));
298 break;
299 case ActivationFunction::RELU:
300 tmp = wrapper::vmax(const_0, vin);
301 break;
302 case ActivationFunction::BOUNDED_RELU:
303 tmp = wrapper::vmin(va, wrapper::vmax(const_0, vin));
304 break;
305 case ActivationFunction::LU_BOUNDED_RELU:
306 tmp = wrapper::vmin(va, wrapper::vmax(vb, vin));
307 break;
308 case ActivationFunction::LEAKY_RELU:
309 tmp = wrapper::vbsl(wrapper::vcgt(vin, const_0), vin, wrapper::vmul(va, vin));
310 break;
311 case ActivationFunction::SOFT_RELU:
312 tmp = wrapper::vlog(wrapper::vadd(const_1, wrapper::vexpq(vin)));
313 break;
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +0100314 case ActivationFunction::ELU:
315 tmp = wrapper::vbsl(wrapper::vcge(vin, const_0), vin, wrapper::vmul(va, wrapper::vsub(wrapper::vexpq(vin), const_1)));
316 break;
Georgios Pinitas5a594532018-12-03 14:30:05 +0000317 case ActivationFunction::SQRT:
SiCong Lia32e2ae2020-06-08 17:30:51 +0100318 tmp = wrapper::vinv(wrapper::vinvsqrt(wrapper::vadd(vin, delta)));
Georgios Pinitas5a594532018-12-03 14:30:05 +0000319 break;
320 case ActivationFunction::SQUARE:
321 tmp = wrapper::vmul(vin, vin);
322 break;
323 case ActivationFunction::TANH:
324 tmp = wrapper::vmul(va, wrapper::vtanh(wrapper::vmul(vb, vin)));
325 break;
Usama Arif80e55db2019-05-14 17:48:47 +0100326 case ActivationFunction::IDENTITY:
327 tmp = vin;
328 break;
morgolock07df3d42020-02-27 11:46:28 +0000329 case ActivationFunction::HARD_SWISH:
330 tmp = wrapper::vmul(vin, wrapper::vmul(const_inv_6, wrapper::vmin(const_6, wrapper::vmax(const_0, wrapper::vadd(vin, const_3)))));
331 break;
Georgios Pinitas5a594532018-12-03 14:30:05 +0000332 default:
333 ARM_COMPUTE_ERROR("Unsupported activation function");
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100334 }
Georgios Pinitas5a594532018-12-03 14:30:05 +0000335 wrapper::vstore(output_ptr + x, tmp);
Pablo Tello91654c42017-07-05 11:32:17 +0100336 }
337
Georgios Pinitas5a594532018-12-03 14:30:05 +0000338 // Compute left-over elements
339 for(; x < window_end_x; ++x)
Georgios Pinitasf525eab2018-01-30 14:47:39 +0000340 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000341 const T in = *(reinterpret_cast<const T *>(input_ptr + x));
342 T tmp;
343 switch(act)
Georgios Pinitasf525eab2018-01-30 14:47:39 +0000344 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000345 case ActivationFunction::ABS:
346 tmp = std::abs(in);
347 break;
348 case ActivationFunction::LINEAR:
349 tmp = a * in + b;
350 break;
351 case ActivationFunction::LOGISTIC:
352 tmp = static_cast<T>(1) / (static_cast<T>(1) + std::exp(-in));
353 break;
354 case ActivationFunction::RELU:
355 tmp = std::max<T>(static_cast<T>(0), in);
356 break;
357 case ActivationFunction::BOUNDED_RELU:
358 tmp = std::min<T>(a, std::max(static_cast<T>(0), in));
359 break;
360 case ActivationFunction::LU_BOUNDED_RELU:
361 tmp = std::min<T>(a, std::max<T>(b, in));
362 break;
363 case ActivationFunction::LEAKY_RELU:
364 tmp = (in > 0) ? in : a * in;
365 break;
366 case ActivationFunction::SOFT_RELU:
367 tmp = std::log(static_cast<T>(1) + std::exp(in));
368 break;
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +0100369 case ActivationFunction::ELU:
370 tmp = (in >= 0) ? in : a * (std::exp(in) - 1);
371 break;
Georgios Pinitas5a594532018-12-03 14:30:05 +0000372 case ActivationFunction::SQRT:
373 tmp = std::sqrt(in);
374 break;
375 case ActivationFunction::SQUARE:
376 tmp = in * in;
377 break;
378 case ActivationFunction::TANH:
379 tmp = a * std::tanh(b * in);
380 break;
Usama Arif80e55db2019-05-14 17:48:47 +0100381 case ActivationFunction::IDENTITY:
382 tmp = in;
383 break;
morgolock07df3d42020-02-27 11:46:28 +0000384 case ActivationFunction::HARD_SWISH:
385 tmp = in * ((std::min(std::max((in + 3), 0.0f), 6.0f)) * 0.166666667f);
386 break;
Georgios Pinitas5a594532018-12-03 14:30:05 +0000387 default:
388 ARM_COMPUTE_ERROR("Unsupported activation function");
Georgios Pinitasf525eab2018-01-30 14:47:39 +0000389 }
Georgios Pinitas5a594532018-12-03 14:30:05 +0000390 *(output_ptr + x) = tmp;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100391 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100392 },
393 input, output);
394}
395
396template <ActivationLayerInfo::ActivationFunction F, typename T>
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100397typename std::enable_if<std::is_same<T, qasymm8_t>::value, void>::type NEActivationLayerKernel::activation(const ITensor *src, ITensor *dst, const Window &window)
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000398{
Georgios Pinitas5a594532018-12-03 14:30:05 +0000399 const int window_step_x = 16 / sizeof(T);
400 const auto window_start_x = static_cast<int>(window.x().start());
401 const auto window_end_x = static_cast<int>(window.x().end());
402 const ActivationFunction act = F;
403
404 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
405 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
406
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100407 Iterator input(src, win_collapsed);
408 Iterator output(dst, win_collapsed);
Georgios Pinitas5a594532018-12-03 14:30:05 +0000409
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100410 const UniformQuantizationInfo qi_in = src->info()->quantization_info().uniform();
411 const UniformQuantizationInfo qi_out = dst->info()->quantization_info().uniform();
morgolockaa85cdf2020-02-28 15:38:28 +0000412 const qasymm8x16_t va = vdupq_n_u8(quantize_qasymm8(_act_info.a(), qi_in));
413 const qasymm8x16_t vb = vdupq_n_u8(quantize_qasymm8(_act_info.b(), qi_in));
414 const qasymm8_t a = quantize_qasymm8(_act_info.a(), qi_in);
415 const qasymm8_t b = quantize_qasymm8(_act_info.b(), qi_in);
416 const qasymm8_t const_0 = quantize_qasymm8(0.f, qi_in);
417 const qasymm8x16_t vconst_0 = vdupq_n_u8(const_0);
418 const auto vconst_1 = vdupq_n_f32(1.f);
419 const float32x4_t va_f32 = vdupq_n_f32(_act_info.a());
420 const float32x4_t vb_f32 = vdupq_n_f32(_act_info.b());
421 const float a_f32 = _act_info.a();
422 const float b_f32 = _act_info.b();
423 const auto const_6_f32 = vdupq_n_f32(6.f);
424 const auto const_0_f32 = vdupq_n_f32(0.f);
425 const auto const_3_f32 = vdupq_n_f32(3.f);
426 const auto const_inv_6_f32 = vdupq_n_f32(0.166666667f);
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000427
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000428 // Initialise scale/offset for re-quantization
429 float s = qi_in.scale / qi_out.scale;
430 float o = -qi_in.offset * s + qi_out.offset;
431 float32x4_t vs = vdupq_n_f32(s);
432 float32x4_t vo = vdupq_n_f32(o);
433
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100434 execute_window_loop(win_collapsed, [&](const Coordinates &)
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000435 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000436 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
437 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000438
Georgios Pinitas5a594532018-12-03 14:30:05 +0000439 wrapper::traits::neon_bitvector_t<T, wrapper::traits::BitWidth::W128> tmp;
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000440
Georgios Pinitas5a594532018-12-03 14:30:05 +0000441 // Compute S elements per iteration
442 int x = window_start_x;
443 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000444 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000445 const auto vin = wrapper::vloadq(input_ptr + x);
446 if(act == ActivationFunction::RELU)
447 {
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000448 // Perform activation
Georgios Pinitas5a594532018-12-03 14:30:05 +0000449 tmp = vmaxq_u8(vconst_0, vin);
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000450 // Re-quantize to new output space
451 tmp = vmlaq_qasymm8(tmp, vs, vo);
Georgios Pinitas5a594532018-12-03 14:30:05 +0000452 }
453 else if(act == ActivationFunction::BOUNDED_RELU)
454 {
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000455 // Perform activation
Georgios Pinitas5a594532018-12-03 14:30:05 +0000456 tmp = vminq_u8(va, vmaxq_u8(vconst_0, vin));
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000457 // Re-quantize to new output space
458 tmp = vmlaq_qasymm8(tmp, vs, vo);
Georgios Pinitas5a594532018-12-03 14:30:05 +0000459 }
460 else if(act == ActivationFunction::LU_BOUNDED_RELU)
461 {
462 // Perform activation
463 tmp = vminq_u8(va, vmaxq_u8(vb, vin));
464 // Re-quantize to new output space
465 tmp = vmlaq_qasymm8(tmp, vs, vo);
466 }
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000467 else if(act == ActivationFunction::LOGISTIC)
468 {
Isabella Gottardif7a3bf22019-03-15 14:58:24 +0000469 // De-quantize
470 const auto vin_deq = vdequantize(vin, qi_in);
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000471 // Perform activation
Isabella Gottardif7a3bf22019-03-15 14:58:24 +0000472 const float32x4x4_t tmp_dep =
473 {
474 {
475 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[0])))),
476 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[1])))),
477 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[2])))),
478 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[3])))),
479 }
480 };
481 // Re-quantize to new output space
482 tmp = vquantize(tmp_dep, qi_out);
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000483 }
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100484 else if(act == ActivationFunction::TANH)
485 {
486 // De-quantize
487 const auto vin_deq = vdequantize(vin, qi_in);
488 // Perform activation
489 const float32x4x4_t tmp_dep =
490 {
491 {
492 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[0], vb_f32))),
493 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[1], vb_f32))),
494 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[2], vb_f32))),
495 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[3], vb_f32))),
496 }
497 };
498 // Re-quantize to new output space
499 tmp = vquantize(tmp_dep, qi_out);
500 }
morgolockaa85cdf2020-02-28 15:38:28 +0000501 else if(act == ActivationFunction::HARD_SWISH)
502 {
503 // De-quantize
504 const auto vin_deq = vdequantize(vin, qi_in);
505 // Perform activation
506 const float32x4x4_t tmp_dep =
507 {
508 {
509 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))))),
510 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))))),
511 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))))),
512 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))))),
513 }
514 };
515 // Re-quantize to new output space
516 tmp = vquantize(tmp_dep, qi_out);
517 }
Georgios Pinitas5a594532018-12-03 14:30:05 +0000518 else
519 {
520 ARM_COMPUTE_ERROR("Unsupported activation function");
521 }
522 wrapper::vstore(output_ptr + x, tmp);
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000523 }
524
Georgios Pinitas5a594532018-12-03 14:30:05 +0000525 // Compute left-over elements
526 for(; x < window_end_x; ++x)
527 {
528 T in = *(reinterpret_cast<const T *>(input_ptr + x));
529 T tmp;
530 if(act == ActivationFunction::RELU)
531 {
532 tmp = std::max(const_0, in);
Michele Di Giorgiob70770e2020-04-22 12:26:10 +0100533 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
Georgios Pinitas5a594532018-12-03 14:30:05 +0000534 }
535 else if(act == ActivationFunction::BOUNDED_RELU)
536 {
537 tmp = std::min(a, std::max(const_0, in));
Michele Di Giorgiob70770e2020-04-22 12:26:10 +0100538 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
Georgios Pinitas5a594532018-12-03 14:30:05 +0000539 }
540 else if(act == ActivationFunction::LU_BOUNDED_RELU)
541 {
542 tmp = std::min(a, std::max(b, in));
Michele Di Giorgiob70770e2020-04-22 12:26:10 +0100543 tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o);
Georgios Pinitas5a594532018-12-03 14:30:05 +0000544 }
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000545 else if(act == ActivationFunction::LOGISTIC)
546 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100547 float tmp_f = dequantize_qasymm8(in, qi_in);
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000548 tmp_f = 1.f / (1.f + std::exp(-tmp_f));
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100549 tmp = quantize_qasymm8(tmp_f, qi_out);
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000550 }
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100551 else if(act == ActivationFunction::TANH)
552 {
553 float tmp_f = dequantize_qasymm8(in, qi_in);
554 tmp_f = a_f32 * std::tanh(b_f32 * tmp_f);
555 tmp = quantize_qasymm8(tmp_f, qi_out);
556 }
morgolockaa85cdf2020-02-28 15:38:28 +0000557 else if(act == ActivationFunction::HARD_SWISH)
558 {
559 float tmp_f = dequantize_qasymm8(in, qi_in);
560 tmp_f = tmp_f * ((std::min(std::max((tmp_f + 3), 0.0f), 6.0f)) * 0.166666667f);
561 tmp = quantize_qasymm8(tmp_f, qi_out);
562 }
Georgios Pinitas5a594532018-12-03 14:30:05 +0000563 else
564 {
565 ARM_COMPUTE_ERROR("Unsupported activation function");
566 }
567 *(output_ptr + x) = tmp;
568 }
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000569 },
570 input, output);
571}
572
giuros01c9573f32019-06-20 10:30:17 +0100573template <ActivationLayerInfo::ActivationFunction F, typename T>
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100574typename std::enable_if<std::is_same<T, qasymm8_signed_t>::value, void>::type NEActivationLayerKernel::activation(const ITensor *src, ITensor *dst, const Window &window)
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000575{
576 const int window_step_x = 16 / sizeof(T);
577 const auto window_start_x = static_cast<int>(window.x().start());
578 const auto window_end_x = static_cast<int>(window.x().end());
579 const ActivationFunction act = F;
580
581 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
582 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
583
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100584 Iterator input(src, win_collapsed);
585 Iterator output(dst, win_collapsed);
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000586
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100587 const UniformQuantizationInfo qi_in = src->info()->quantization_info().uniform();
588 const UniformQuantizationInfo qi_out = dst->info()->quantization_info().uniform();
morgolockaa85cdf2020-02-28 15:38:28 +0000589 const qasymm8x16_signed_t va = vdupq_n_s8(quantize_qasymm8_signed(_act_info.a(), qi_in));
590 const qasymm8x16_signed_t vb = vdupq_n_s8(quantize_qasymm8_signed(_act_info.b(), qi_in));
591 const qasymm8_signed_t a = quantize_qasymm8_signed(_act_info.a(), qi_in);
592 const qasymm8_signed_t b = quantize_qasymm8_signed(_act_info.b(), qi_in);
593 const qasymm8_signed_t const_0 = quantize_qasymm8_signed(0.f, qi_in);
594 const qasymm8x16_signed_t vconst_0 = vdupq_n_s8(const_0);
595 const auto vconst_1 = vdupq_n_f32(1.f);
596 const float32x4_t va_f32 = vdupq_n_f32(_act_info.a());
597 const float32x4_t vb_f32 = vdupq_n_f32(_act_info.b());
598 const float a_f32 = _act_info.a();
599 const float b_f32 = _act_info.b();
600 const auto const_6_f32 = vdupq_n_f32(6.f);
601 const auto const_0_f32 = vdupq_n_f32(0.f);
602 const auto const_3_f32 = vdupq_n_f32(3.f);
603 const auto const_inv_6_f32 = vdupq_n_f32(0.166666667f);
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000604
605 // Initialise scale/offset for re-quantization
606 float s = qi_in.scale / qi_out.scale;
607 float o = -qi_in.offset * s + qi_out.offset;
608 float32x4_t vs = vdupq_n_f32(s);
609 float32x4_t vo = vdupq_n_f32(o);
610
611 execute_window_loop(win_collapsed, [&](const Coordinates &)
612 {
613 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
614 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
615
616 wrapper::traits::neon_bitvector_t<T, wrapper::traits::BitWidth::W128> tmp;
617
618 // Compute S elements per iteration
619 int x = window_start_x;
620 for(; x <= (window_end_x - window_step_x); x += window_step_x)
621 {
622 const auto vin = wrapper::vloadq(input_ptr + x);
623 if(act == ActivationFunction::RELU)
624 {
625 // Perform activation
626 tmp = vmaxq_s8(vconst_0, vin);
627 // Re-quantize to new output space
628 tmp = vmlaq_qasymm8_signed(tmp, vs, vo);
629 }
630 else if(act == ActivationFunction::BOUNDED_RELU)
631 {
632 // Perform activation
633 tmp = vminq_s8(va, vmaxq_s8(vconst_0, vin));
634 // Re-quantize to new output space
635 tmp = vmlaq_qasymm8_signed(tmp, vs, vo);
636 }
637 else if(act == ActivationFunction::LU_BOUNDED_RELU)
638 {
639 // Perform activation
640 tmp = vminq_s8(va, vmaxq_s8(vb, vin));
641 // Re-quantize to new output space
642 tmp = vmlaq_qasymm8_signed(tmp, vs, vo);
643 }
644 else if(act == ActivationFunction::LOGISTIC)
645 {
646 // De-quantize
647 const auto vin_deq = vdequantize(vin, qi_in);
648 // Perform activation
649 const float32x4x4_t tmp_dep =
650 {
651 {
652 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[0])))),
653 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[1])))),
654 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[2])))),
655 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[3])))),
656 }
657 };
658 // Re-quantize to new output space
659 tmp = vquantize_signed(tmp_dep, qi_out);
660 }
661 else if(act == ActivationFunction::TANH)
662 {
663 // De-quantize
664 const auto vin_deq = vdequantize(vin, qi_in);
665 // Perform activation
666 const float32x4x4_t tmp_dep =
667 {
668 {
669 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[0], vb_f32))),
670 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[1], vb_f32))),
671 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[2], vb_f32))),
672 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[3], vb_f32))),
673 }
674 };
675 // Re-quantize to new output space
676 tmp = vquantize_signed(tmp_dep, qi_out);
677 }
morgolockaa85cdf2020-02-28 15:38:28 +0000678 else if(act == ActivationFunction::HARD_SWISH)
679 {
680 // De-quantize
681 const auto vin_deq = vdequantize(vin, qi_in);
682 // Perform activation
683 const float32x4x4_t tmp_dep =
684 {
685 {
686 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))))),
687 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))))),
688 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))))),
689 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))))),
690 }
691 };
692 // Re-quantize to new output space
693 tmp = vquantize_signed(tmp_dep, qi_out);
694 }
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000695 else
696 {
697 ARM_COMPUTE_ERROR("Unsupported activation function");
698 }
699 wrapper::vstore(output_ptr + x, tmp);
700 }
701
702 // Compute left-over elements
703 for(; x < window_end_x; ++x)
704 {
705 T in = *(reinterpret_cast<const T *>(input_ptr + x));
706 T tmp;
707 if(act == ActivationFunction::RELU)
708 {
709 tmp = std::max(const_0, in);
Michele Di Giorgiob70770e2020-04-22 12:26:10 +0100710 tmp = utility::clamp<int32_t, qasymm8_signed_t>(tmp * s + o);
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000711 }
712 else if(act == ActivationFunction::BOUNDED_RELU)
713 {
714 tmp = std::min(a, std::max(const_0, in));
Michele Di Giorgiob70770e2020-04-22 12:26:10 +0100715 tmp = utility::clamp<int32_t, qasymm8_signed_t>(tmp * s + o);
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000716 }
717 else if(act == ActivationFunction::LU_BOUNDED_RELU)
718 {
719 tmp = std::min(a, std::max(b, in));
Michele Di Giorgiob70770e2020-04-22 12:26:10 +0100720 tmp = utility::clamp<int32_t, qasymm8_signed_t>(tmp * s + o);
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000721 }
722 else if(act == ActivationFunction::LOGISTIC)
723 {
724 float tmp_f = dequantize_qasymm8_signed(in, qi_in);
725 tmp_f = 1.f / (1.f + std::exp(-tmp_f));
726 tmp = quantize_qasymm8_signed(tmp_f, qi_out);
727 }
728 else if(act == ActivationFunction::TANH)
729 {
730 float tmp_f = dequantize_qasymm8_signed(in, qi_in);
731 tmp_f = a_f32 * std::tanh(b_f32 * tmp_f);
732 tmp = quantize_qasymm8_signed(tmp_f, qi_out);
733 }
morgolockaa85cdf2020-02-28 15:38:28 +0000734 else if(act == ActivationFunction::HARD_SWISH)
735 {
736 float tmp_f = dequantize_qasymm8_signed(in, qi_in);
737 tmp_f = tmp_f * ((std::min(std::max((tmp_f + 3), 0.0f), 6.0f)) * 0.166666667f);
738 tmp = quantize_qasymm8_signed(tmp_f, qi_out);
739 }
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000740 else
741 {
742 ARM_COMPUTE_ERROR("Unsupported activation function");
743 }
744 *(output_ptr + x) = tmp;
745 }
746 },
747 input, output);
748}
749
750template <ActivationLayerInfo::ActivationFunction F, typename T>
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100751typename std::enable_if<std::is_same<T, qsymm16_t>::value, void>::type NEActivationLayerKernel::activation(const ITensor *src, ITensor *dst, const Window &window)
giuros01c9573f32019-06-20 10:30:17 +0100752{
753 const int window_step_x = 16 / sizeof(T);
754 const auto window_start_x = static_cast<int>(window.x().start());
755 const auto window_end_x = static_cast<int>(window.x().end());
756 const ActivationFunction act = F;
757
758 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
759 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
760
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100761 Iterator input(src, win_collapsed);
762 Iterator output(dst, win_collapsed);
giuros01c9573f32019-06-20 10:30:17 +0100763
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100764 const UniformQuantizationInfo qi_in = src->info()->quantization_info().uniform();
765 const UniformQuantizationInfo qi_out = dst->info()->quantization_info().uniform();
giuros01c9573f32019-06-20 10:30:17 +0100766 const auto vconst_1 = vdupq_n_f32(1.f);
767 const float32x4_t va_f32 = vdupq_n_f32(_act_info.a());
768 const float32x4_t vb_f32 = vdupq_n_f32(_act_info.b());
769 const float a_f32 = _act_info.a();
770 const float b_f32 = _act_info.b();
771
772 execute_window_loop(win_collapsed, [&](const Coordinates &)
773 {
774 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
775 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
776
777 wrapper::traits::neon_bitvector_t<T, wrapper::traits::BitWidth::W128> tmp;
778 ARM_COMPUTE_UNUSED(tmp);
779
780 // Compute S elements per iteration
781 int x = window_start_x;
782 for(; x <= (window_end_x - window_step_x); x += window_step_x)
783 {
784 const auto vin = wrapper::vloadq(input_ptr + x);
785 if(act == ActivationFunction::LOGISTIC)
786 {
787 // De-quantize
788 const auto vin_deq = vdequantize_int16(vin, qi_in.scale);
789 // Perform activation
790 const float32x4x2_t tmp_dep =
791 {
792 {
793 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[0])))),
794 wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[1])))),
795 }
796 };
797 // Re-quantize to new output space
798 tmp = vquantize_int16(tmp_dep, qi_out.scale);
799 }
800 else if(act == ActivationFunction::TANH)
801 {
802 // De-quantize
803 const auto vin_deq = vdequantize_int16(vin, qi_in.scale);
804 // Perform activation
805 const float32x4x2_t tmp_dep =
806 {
807 {
808 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[0], vb_f32))),
809 wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[1], vb_f32))),
810 }
811 };
812 // Re-quantize to new output space
813 tmp = vquantize_int16(tmp_dep, qi_out.scale);
814 }
815 else
816 {
817 ARM_COMPUTE_ERROR("Unsupported activation function");
818 }
819 wrapper::vstore(output_ptr + x, tmp);
820 }
821
822 // Compute left-over elements
823 for(; x < window_end_x; ++x)
824 {
825 T in = *(reinterpret_cast<const T *>(input_ptr + x));
826 T tmp;
827 if(act == ActivationFunction::LOGISTIC)
828 {
829 float tmp_f = dequantize_qsymm16(in, qi_in.scale);
830 tmp_f = 1.f / (1.f + std::exp(-tmp_f));
831 tmp = quantize_qsymm16(tmp_f, qi_out);
832 }
833 else if(act == ActivationFunction::TANH)
834 {
835 float tmp_f = dequantize_qsymm16(in, qi_in.scale);
836 tmp_f = a_f32 * std::tanh(b_f32 * tmp_f);
837 tmp = quantize_qsymm16(tmp_f, qi_out);
838 }
839 else
840 {
841 ARM_COMPUTE_ERROR("Unsupported activation function");
842 }
843 *(output_ptr + x) = tmp;
844 }
845 },
846 input, output);
847}
848
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000849Status NEActivationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
850{
851 ARM_COMPUTE_UNUSED(act_info);
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100852 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, act_info));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000853 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (output != nullptr) ? output->clone().get() : nullptr).first);
854
855 return Status{};
856}
857
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100858void NEActivationLayerKernel::run_op(const std::vector<InputTensor> &inputs,
859 const std::vector<OutputTensor> &outputs,
860 const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100861{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100862 // Early exit on disabled activation
863 if(!_act_info.enabled())
864 {
865 return;
866 }
867
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100868 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100869 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100870 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100871 ARM_COMPUTE_ERROR_ON(_func == nullptr);
872
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100873 ARM_COMPUTE_ERROR_ON(inputs.empty() || outputs.empty());
874
875 (this->*_func)(inputs[0].tensor, outputs[0].tensor, window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100876}