blob: cac326da0a53eef2dee2108eb3f9088a779f53bb [file] [log] [blame]
Michalis Spyrouc4d45552020-10-19 12:41:30 +01001/*
Matthew Benthamf1aeab92023-05-30 13:35:34 +00002 * Copyright (c) 2020-2023 Arm Limited.
Michalis Spyrouc4d45552020-10-19 12:41:30 +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 */
Matthew Benthamf1aeab92023-05-30 13:35:34 +000024#include "arm_compute/core/ActivationLayerInfo.h"
Michalis Spyrouc4d45552020-10-19 12:41:30 +010025#include "arm_compute/core/Helpers.h"
Michalis Spyrouc4d45552020-10-19 12:41:30 +010026#include "arm_compute/core/Window.h"
Michalis Spyrouc4d45552020-10-19 12:41:30 +010027#include "src/core/NEON/wrapper/wrapper.h"
Michalis Spyrouc4d45552020-10-19 12:41:30 +010028namespace arm_compute
29{
30namespace cpu
31{
Dana Zlotnik32291712021-11-25 09:58:27 +020032/** Constant parameters needed by the activation implementation.
33 * These parameters differ for each floating type
34 *
35 * @note This are passed as a struct as C++ does not allow float as a template parameter until C++20
36 **/
37struct ActFpImplParams
Michalis Spyrouc4d45552020-10-19 12:41:30 +010038{
Dana Zlotnik32291712021-11-25 09:58:27 +020039 float delta; /**< Minimum delta needed to avoid NaN on corner-cases of elementary functions */
40 int step_x; /**< Window step at the x dimension */
41};
42
Michalis Spyrouc4d45552020-10-19 12:41:30 +010043#ifndef __aarch64__
44inline float32x4_t mask_float_vector(const float32x4_t &in, const uint32x4_t &mask)
45{
46 auto int_in = vreinterpretq_u32_f32(in);
47 return vreinterpretq_f32_u32(wrapper::vand(int_in, mask));
48}
Dana Zlotnik32291712021-11-25 09:58:27 +020049#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
50inline float16x8_t mask_float_vector(const float16x8_t &in, const uint16x8_t &mask)
51{
52 auto int_in = vreinterpretq_u16_f16(in);
53 return vreinterpretq_f16_u16(wrapper::vand(int_in, mask));
54}
55#endif //defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
Michele Di Giorgio6c25aad2021-01-14 16:17:48 +000056#endif /* __aarch64__ */
Michalis Spyrouc4d45552020-10-19 12:41:30 +010057
Dana Zlotnik32291712021-11-25 09:58:27 +020058template <typename T, const ActFpImplParams &P>
59void fp_neon_activation_impl(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
Michalis Spyrouc4d45552020-10-19 12:41:30 +010060{
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +000061 /** SIMD vector tag type. */
Dana Zlotnik32291712021-11-25 09:58:27 +020062 using ExactTagType = typename arm_compute::wrapper::traits::neon_bitvector_tag_t<T, wrapper::traits::BitWidth::W128>;
63 constexpr int window_step_x = P.step_x;
Michalis Spyrouc4d45552020-10-19 12:41:30 +010064 const auto window_start_x = static_cast<int>(window.x().start());
65 const auto window_end_x = static_cast<int>(window.x().end());
66 const ActivationLayerInfo::ActivationFunction act = act_info.activation();
Dana Zlotnik32291712021-11-25 09:58:27 +020067 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
Michalis Spyrouc4d45552020-10-19 12:41:30 +010068 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
Michalis Spyrouc4d45552020-10-19 12:41:30 +010069 Iterator input(src, win_collapsed);
70 Iterator output(dst, win_collapsed);
Michalis Spyrouc4d45552020-10-19 12:41:30 +010071 // In case of non-aarch64, a small delta value is added to the input
72 // to prevent NAN values caused by zeros in inputs to SQRT.
73 // In case of aarh64, we call vsqrt directly, so we don't use delta.
74#ifndef __aarch64__
Dana Zlotnik32291712021-11-25 09:58:27 +020075 const auto delta = wrapper::vdup_n(static_cast<T>(P.delta), ExactTagType {});
Pablo Marquez Telloead4d112022-09-21 11:01:50 +010076#else /* #ifndef __aarch64__ */
77 const auto const_inv_2 = wrapper::vdup_n(static_cast<T>(0.5f), ExactTagType {});
78 const auto const_inv_sqrt_2 = wrapper::vdup_n(static_cast<T>(0.70710678118f), ExactTagType{});
Michele Di Giorgio6c25aad2021-01-14 16:17:48 +000079#endif /* __aarch64__ */
Dana Zlotnik32291712021-11-25 09:58:27 +020080 const auto const_1 = wrapper::vdup_n(static_cast<T>(1.f), ExactTagType {});
81 const auto const_0 = wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{});
82 const auto const_6 = wrapper::vdup_n(static_cast<T>(6.f), ExactTagType{});
83 const auto const_3 = wrapper::vdup_n(static_cast<T>(3.f), ExactTagType{});
84 const auto const_inv_6 = wrapper::vdup_n(static_cast<T>(0.166666667f), ExactTagType{});
Michele Di Giorgio6c25aad2021-01-14 16:17:48 +000085 constexpr float soft_relu_thresh = 12.f;
Dana Zlotnik32291712021-11-25 09:58:27 +020086 const auto vsoft_relu_thresh = wrapper::vdup_n(static_cast<T>(soft_relu_thresh), ExactTagType{});
87 const auto va = wrapper::vdup_n(static_cast<T>(act_info.a()), ExactTagType{});
88 const auto vb = wrapper::vdup_n(static_cast<T>(act_info.b()), ExactTagType{});
89 const auto a = static_cast<T>(act_info.a());
90 const auto b = static_cast<T>(act_info.b());
Michalis Spyrouc4d45552020-10-19 12:41:30 +010091 execute_window_loop(win_collapsed, [&](const Coordinates &)
92 {
Dana Zlotnik32291712021-11-25 09:58:27 +020093 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
94 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
95 wrapper::traits::neon_bitvector_t<T, wrapper::traits::BitWidth::W128> tmp;
Michalis Spyrouc4d45552020-10-19 12:41:30 +010096 // Compute S elements per iteration
97 int x = window_start_x;
98 for(; x <= (window_end_x - window_step_x); x += window_step_x)
99 {
100 const auto vin = wrapper::vloadq(input_ptr + x);
101 switch(act)
102 {
103 case ActivationLayerInfo::ActivationFunction::ABS:
104 tmp = wrapper::vabs(vin);
105 break;
106 case ActivationLayerInfo::ActivationFunction::LINEAR:
107 tmp = wrapper::vmla(vb, va, vin);
108 break;
109 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
110 tmp = wrapper::vinv(wrapper::vadd(const_1, wrapper::vexpq(wrapper::vneg(vin))));
111 break;
112 case ActivationLayerInfo::ActivationFunction::RELU:
113 tmp = wrapper::vmax(const_0, vin);
114 break;
115 case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU:
116 tmp = wrapper::vmin(va, wrapper::vmax(const_0, vin));
117 break;
118 case ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU:
119 tmp = wrapper::vmin(va, wrapper::vmax(vb, vin));
120 break;
121 case ActivationLayerInfo::ActivationFunction::LEAKY_RELU:
122 tmp = wrapper::vbsl(wrapper::vcgt(vin, const_0), vin, wrapper::vmul(va, vin));
123 break;
124 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
Michele Di Giorgio6c25aad2021-01-14 16:17:48 +0000125 tmp = wrapper::vbsl(wrapper::vcgt(vin, vsoft_relu_thresh), vin, wrapper::vlog(wrapper::vadd(const_1, wrapper::vexpq(vin))));
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100126 break;
127 case ActivationLayerInfo::ActivationFunction::ELU:
128 tmp = wrapper::vbsl(wrapper::vcge(vin, const_0), vin, wrapper::vmul(va, wrapper::vsub(wrapper::vexpq(vin), const_1)));
129 break;
130 case ActivationLayerInfo::ActivationFunction::SQRT:
131#ifdef __aarch64__
132 tmp = wrapper::vsqrt(vin);
Michele Di Giorgio6c25aad2021-01-14 16:17:48 +0000133#else /* __aarch64__ */
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100134 {
135 const auto bitmask = wrapper::vceq(vin, wrapper::vdup_n(0.f, ExactTagType{}));
136 tmp = wrapper::vinv(wrapper::vinvsqrt(wrapper::vadd(vin, mask_float_vector(delta, bitmask))));
137 tmp = mask_float_vector(tmp, wrapper::vnot(bitmask));
138 }
Michele Di Giorgio6c25aad2021-01-14 16:17:48 +0000139#endif /* __aarch64__ */
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100140 break;
141 case ActivationLayerInfo::ActivationFunction::SQUARE:
142 tmp = wrapper::vmul(vin, vin);
143 break;
144 case ActivationLayerInfo::ActivationFunction::TANH:
145 tmp = wrapper::vmul(va, wrapper::vtanh(wrapper::vmul(vb, vin)));
146 break;
147 case ActivationLayerInfo::ActivationFunction::IDENTITY:
148 tmp = vin;
149 break;
150 case ActivationLayerInfo::ActivationFunction::HARD_SWISH:
151 tmp = wrapper::vmul(vin, wrapper::vmul(const_inv_6, wrapper::vmin(const_6, wrapper::vmax(const_0, wrapper::vadd(vin, const_3)))));
152 break;
Jonathan Deakind6b8a712022-08-23 11:44:18 +0100153 case ActivationLayerInfo::ActivationFunction::SWISH:
154 tmp = wrapper::vmul(vin, wrapper::vinv(wrapper::vadd(const_1, wrapper::vexpq(wrapper::vneg(wrapper::vmul(va, vin))))));
155 break;
Pablo Marquez Telloead4d112022-09-21 11:01:50 +0100156#ifdef __aarch64__
Murray Kornelsen926f5022022-07-13 21:22:39 -0400157 case ActivationLayerInfo::ActivationFunction::GELU:
158 tmp = wrapper::vmul(vin, wrapper::vmul(const_inv_2, wrapper::vadd(const_1, wrapper::verf(wrapper::vmul(vin, const_inv_sqrt_2)))));
159 break;
Pablo Marquez Telloead4d112022-09-21 11:01:50 +0100160#endif /* __aarch64__ */
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100161 default:
162 ARM_COMPUTE_ERROR("Unsupported activation function");
163 }
164 wrapper::vstore(output_ptr + x, tmp);
165 }
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100166 // Compute left-over elements
167 for(; x < window_end_x; ++x)
168 {
Dana Zlotnik32291712021-11-25 09:58:27 +0200169 const T in = *(reinterpret_cast<const T *>(input_ptr + x));
170 T tmp;
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100171 switch(act)
172 {
173 case ActivationLayerInfo::ActivationFunction::ABS:
174 tmp = std::abs(in);
175 break;
176 case ActivationLayerInfo::ActivationFunction::LINEAR:
177 tmp = a * in + b;
178 break;
179 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
Dana Zlotnik32291712021-11-25 09:58:27 +0200180 tmp = static_cast<T>(1) / (static_cast<T>(1) + std::exp(-in));
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100181 break;
182 case ActivationLayerInfo::ActivationFunction::RELU:
Dana Zlotnik32291712021-11-25 09:58:27 +0200183 tmp = std::max<T>(static_cast<T>(0), in);
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100184 break;
185 case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU:
Dana Zlotnik32291712021-11-25 09:58:27 +0200186 tmp = std::min<T>(a, std::max(static_cast<T>(0), in));
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100187 break;
188 case ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU:
Dana Zlotnik32291712021-11-25 09:58:27 +0200189 tmp = std::min<T>(a, std::max<T>(b, in));
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100190 break;
191 case ActivationLayerInfo::ActivationFunction::LEAKY_RELU:
192 tmp = (in > 0) ? in : a * in;
193 break;
194 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
Dana Zlotnik32291712021-11-25 09:58:27 +0200195 tmp = (in > soft_relu_thresh) ? in : std::log(static_cast<T>(1) + std::exp(in));
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100196 break;
197 case ActivationLayerInfo::ActivationFunction::ELU:
198 tmp = (in >= 0) ? in : a * (std::exp(in) - 1);
199 break;
200 case ActivationLayerInfo::ActivationFunction::SQRT:
201 tmp = std::sqrt(in);
202 break;
203 case ActivationLayerInfo::ActivationFunction::SQUARE:
204 tmp = in * in;
205 break;
206 case ActivationLayerInfo::ActivationFunction::TANH:
207 tmp = a * std::tanh(b * in);
208 break;
209 case ActivationLayerInfo::ActivationFunction::IDENTITY:
210 tmp = in;
211 break;
212 case ActivationLayerInfo::ActivationFunction::HARD_SWISH:
213 tmp = in * ((std::min(std::max((in + 3), 0.0f), 6.0f)) * 0.166666667f);
214 break;
Jonathan Deakind6b8a712022-08-23 11:44:18 +0100215 case ActivationLayerInfo::ActivationFunction::SWISH:
216 tmp = in / (static_cast<T>(1) + std::exp(-a*in));
217 break;
Murray Kornelsen926f5022022-07-13 21:22:39 -0400218 case ActivationLayerInfo::ActivationFunction::GELU:
219 tmp = in * static_cast<T>(0.5f * (1.0f + erff(static_cast<float>(in) / 1.41421356237f)));
220 break;
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100221 default:
222 ARM_COMPUTE_ERROR("Unsupported activation function");
223 }
224 *(output_ptr + x) = tmp;
225 }
226 },
227 input, output);
228}
229} // namespace cpu
230} // namespace arm_compute