Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 1 | /* |
Pablo Marquez Tello | 20cfa45 | 2023-03-20 16:29:21 +0000 | [diff] [blame] | 2 | * Copyright (c) 2020-2023 Arm Limited. |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 3 | * |
| 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/Helpers.h" |
| 25 | #include "arm_compute/core/Window.h" |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame^] | 26 | #include "arm_compute/function_info/ActivationLayerInfo.h" |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 27 | #include "src/core/NEON/NEAsymm.h" |
| 28 | #include "src/core/NEON/NEMath.h" |
| 29 | #include "src/core/NEON/wrapper/wrapper.h" |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 30 | |
| 31 | #include <arm_neon.h> |
| 32 | #include <cmath> |
| 33 | #include <cstddef> |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | namespace cpu |
| 38 | { |
Dana Zlotnik | 3229171 | 2021-11-25 09:58:27 +0200 | [diff] [blame] | 39 | void neon_qasymm8_signed_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window) |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 40 | { |
| 41 | constexpr int window_step_x = 16; |
| 42 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 43 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 44 | const ActivationLayerInfo::ActivationFunction act = act_info.activation(); |
| 45 | |
| 46 | Window win_collapsed = window.collapse_if_possible(window, Window::DimZ); |
| 47 | win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 48 | |
| 49 | Iterator input(src, win_collapsed); |
| 50 | Iterator output(dst, win_collapsed); |
| 51 | |
Sang-Hoon Park | add8e81 | 2020-11-25 11:46:03 +0000 | [diff] [blame] | 52 | const UniformQuantizationInfo qi_in = src->info()->quantization_info().uniform(); |
| 53 | const UniformQuantizationInfo qi_out = dst->info()->quantization_info().uniform(); |
| 54 | const qasymm8x16_signed_t va = vdupq_n_s8(quantize_qasymm8_signed(act_info.a(), qi_in)); |
| 55 | const qasymm8x16_signed_t vb = vdupq_n_s8(quantize_qasymm8_signed(act_info.b(), qi_in)); |
| 56 | const qasymm8_signed_t a = quantize_qasymm8_signed(act_info.a(), qi_in); |
| 57 | const qasymm8_signed_t b = quantize_qasymm8_signed(act_info.b(), qi_in); |
| 58 | const qasymm8_signed_t const_0 = quantize_qasymm8_signed(0.f, qi_in); |
| 59 | const qasymm8x16_signed_t vconst_0 = vdupq_n_s8(const_0); |
Sang-Hoon Park | add8e81 | 2020-11-25 11:46:03 +0000 | [diff] [blame] | 60 | #ifndef __aarch64__ |
Viet-Hoa Do | 29db3d2 | 2022-08-10 11:56:49 +0100 | [diff] [blame] | 61 | const auto vconst_1 = vdupq_n_f32(1.f); |
| 62 | const auto vconst_0_f32 = vdupq_n_f32(0.f); |
Sang-Hoon Park | add8e81 | 2020-11-25 11:46:03 +0000 | [diff] [blame] | 63 | #endif // __aarch64__ |
| 64 | const float32x4_t va_f32 = vdupq_n_f32(act_info.a()); |
| 65 | const float32x4_t vb_f32 = vdupq_n_f32(act_info.b()); |
| 66 | const float a_f32 = act_info.a(); |
| 67 | const float b_f32 = act_info.b(); |
| 68 | const auto const_6_f32 = vdupq_n_f32(6.f); |
| 69 | const auto const_0_f32 = vdupq_n_f32(0.f); |
| 70 | const auto const_3_f32 = vdupq_n_f32(3.f); |
| 71 | const auto const_inv_6_f32 = vdupq_n_f32(0.166666667f); |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 72 | |
| 73 | // Initialise scale/offset for re-quantization |
| 74 | float s = qi_in.scale / qi_out.scale; |
| 75 | float o = -qi_in.offset * s + qi_out.offset; |
| 76 | float32x4_t vs = vdupq_n_f32(s); |
| 77 | float32x4_t vo = vdupq_n_f32(o); |
| 78 | |
| 79 | execute_window_loop(win_collapsed, [&](const Coordinates &) |
| 80 | { |
| 81 | const auto input_ptr = reinterpret_cast<const qasymm8_signed_t *>(input.ptr()); |
| 82 | const auto output_ptr = reinterpret_cast<qasymm8_signed_t *>(output.ptr()); |
| 83 | |
| 84 | wrapper::traits::neon_bitvector_t<qasymm8_signed_t, wrapper::traits::BitWidth::W128> tmp; |
| 85 | |
| 86 | // Compute S elements per iteration |
| 87 | int x = window_start_x; |
| 88 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 89 | { |
| 90 | const auto vin = wrapper::vloadq(input_ptr + x); |
| 91 | if(act == ActivationLayerInfo::ActivationFunction::RELU) |
| 92 | { |
| 93 | // Perform activation |
| 94 | tmp = vmaxq_s8(vconst_0, vin); |
| 95 | // Re-quantize to new output space |
Pablo Marquez Tello | 20cfa45 | 2023-03-20 16:29:21 +0000 | [diff] [blame] | 96 | tmp = vmlaq_qasymm8_signed<RoundingPolicy::TO_NEAREST_UP>(tmp, vs, vo); |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 97 | } |
| 98 | else if(act == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU) |
| 99 | { |
| 100 | // Perform activation |
| 101 | tmp = vminq_s8(va, vmaxq_s8(vconst_0, vin)); |
| 102 | // Re-quantize to new output space |
Pablo Marquez Tello | 20cfa45 | 2023-03-20 16:29:21 +0000 | [diff] [blame] | 103 | tmp = vmlaq_qasymm8_signed<RoundingPolicy::TO_NEAREST_UP>(tmp, vs, vo); |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 104 | } |
| 105 | else if(act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU) |
| 106 | { |
| 107 | // Perform activation |
| 108 | tmp = vminq_s8(va, vmaxq_s8(vb, vin)); |
| 109 | // Re-quantize to new output space |
Pablo Marquez Tello | 20cfa45 | 2023-03-20 16:29:21 +0000 | [diff] [blame] | 110 | tmp = vmlaq_qasymm8_signed<RoundingPolicy::TO_NEAREST_UP>(tmp, vs, vo); |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 111 | } |
Viet-Hoa Do | 29db3d2 | 2022-08-10 11:56:49 +0100 | [diff] [blame] | 112 | #ifndef __aarch64__ // LUT-based implementation is used for aarch64 instead. |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 113 | else if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC) |
| 114 | { |
| 115 | // De-quantize |
| 116 | const auto vin_deq = vdequantize(vin, qi_in); |
| 117 | // Perform activation |
| 118 | const float32x4x4_t tmp_dep = |
| 119 | { |
| 120 | { |
| 121 | wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[0])))), |
| 122 | wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[1])))), |
| 123 | wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[2])))), |
| 124 | wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[3])))), |
| 125 | } |
| 126 | }; |
| 127 | // Re-quantize to new output space |
| 128 | tmp = vquantize_signed(tmp_dep, qi_out); |
| 129 | } |
Viet-Hoa Do | 29db3d2 | 2022-08-10 11:56:49 +0100 | [diff] [blame] | 130 | #endif // __aarch64__ |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 131 | else if(act == ActivationLayerInfo::ActivationFunction::TANH) |
| 132 | { |
| 133 | // De-quantize |
| 134 | const auto vin_deq = vdequantize(vin, qi_in); |
| 135 | // Perform activation |
| 136 | const float32x4x4_t tmp_dep = |
| 137 | { |
| 138 | { |
| 139 | wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[0], vb_f32))), |
| 140 | wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[1], vb_f32))), |
| 141 | wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[2], vb_f32))), |
| 142 | wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[3], vb_f32))), |
| 143 | } |
| 144 | }; |
| 145 | // Re-quantize to new output space |
| 146 | tmp = vquantize_signed(tmp_dep, qi_out); |
| 147 | } |
| 148 | else if(act == ActivationLayerInfo::ActivationFunction::HARD_SWISH) |
| 149 | { |
| 150 | // De-quantize |
| 151 | const auto vin_deq = vdequantize(vin, qi_in); |
| 152 | // Perform activation |
| 153 | const float32x4x4_t tmp_dep = |
| 154 | { |
| 155 | { |
| 156 | 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))))), |
| 157 | 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))))), |
| 158 | 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))))), |
| 159 | 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))))), |
| 160 | } |
| 161 | }; |
| 162 | // Re-quantize to new output space |
| 163 | tmp = vquantize_signed(tmp_dep, qi_out); |
| 164 | } |
Sang-Hoon Park | add8e81 | 2020-11-25 11:46:03 +0000 | [diff] [blame] | 165 | else if(act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU) |
| 166 | { |
| 167 | const auto vin_deq = vdequantize(vin, qi_in); |
| 168 | |
| 169 | #ifdef __aarch64__ |
| 170 | const uint32x4x4_t pos_mask = |
| 171 | { |
| 172 | { |
| 173 | wrapper::vcgtz(vin_deq.val[0]), |
| 174 | wrapper::vcgtz(vin_deq.val[1]), |
| 175 | wrapper::vcgtz(vin_deq.val[2]), |
| 176 | wrapper::vcgtz(vin_deq.val[3]), |
| 177 | } |
| 178 | }; |
| 179 | #else // __aarch64__ |
| 180 | const uint32x4x4_t pos_mask = |
| 181 | { |
| 182 | { |
| 183 | wrapper::vcgt(vin_deq.val[0], vconst_0_f32), |
| 184 | wrapper::vcgt(vin_deq.val[1], vconst_0_f32), |
| 185 | wrapper::vcgt(vin_deq.val[2], vconst_0_f32), |
| 186 | wrapper::vcgt(vin_deq.val[3], vconst_0_f32), |
| 187 | } |
| 188 | }; |
| 189 | #endif // __aarch64__ |
| 190 | |
| 191 | const float32x4x4_t tmp_dep = |
| 192 | { |
| 193 | { |
| 194 | wrapper::vbsl(pos_mask.val[0], vin_deq.val[0], wrapper::vmul(va_f32, vin_deq.val[0])), |
| 195 | wrapper::vbsl(pos_mask.val[1], vin_deq.val[1], wrapper::vmul(va_f32, vin_deq.val[1])), |
| 196 | wrapper::vbsl(pos_mask.val[2], vin_deq.val[2], wrapper::vmul(va_f32, vin_deq.val[2])), |
| 197 | wrapper::vbsl(pos_mask.val[3], vin_deq.val[3], wrapper::vmul(va_f32, vin_deq.val[3])), |
| 198 | } |
| 199 | }; |
| 200 | |
| 201 | tmp = vquantize_signed(tmp_dep, qi_out); |
| 202 | } |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 203 | else |
| 204 | { |
| 205 | ARM_COMPUTE_ERROR("Unsupported activation function"); |
| 206 | } |
| 207 | wrapper::vstore(output_ptr + x, tmp); |
| 208 | } |
| 209 | |
| 210 | // Compute left-over elements |
| 211 | for(; x < window_end_x; ++x) |
| 212 | { |
| 213 | qasymm8_signed_t in = *(reinterpret_cast<const qasymm8_signed_t *>(input_ptr + x)); |
| 214 | qasymm8_signed_t tmp = 0; |
| 215 | if(act == ActivationLayerInfo::ActivationFunction::RELU) |
| 216 | { |
| 217 | tmp = std::max(const_0, in); |
Pablo Marquez Tello | 20cfa45 | 2023-03-20 16:29:21 +0000 | [diff] [blame] | 218 | tmp = utility::clamp<int32_t, qasymm8_signed_t>(support::cpp11::lround(tmp * s + o)); |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 219 | } |
| 220 | else if(act == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU) |
| 221 | { |
| 222 | tmp = std::min(a, std::max(const_0, in)); |
Pablo Marquez Tello | 20cfa45 | 2023-03-20 16:29:21 +0000 | [diff] [blame] | 223 | tmp = utility::clamp<int32_t, qasymm8_signed_t>(support::cpp11::lround(tmp * s + o)); |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 224 | } |
| 225 | else if(act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU) |
| 226 | { |
| 227 | tmp = std::min(a, std::max(b, in)); |
Pablo Marquez Tello | 20cfa45 | 2023-03-20 16:29:21 +0000 | [diff] [blame] | 228 | tmp = utility::clamp<int32_t, qasymm8_signed_t>(support::cpp11::lround(tmp * s + o)); |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 229 | } |
Viet-Hoa Do | 29db3d2 | 2022-08-10 11:56:49 +0100 | [diff] [blame] | 230 | #ifndef __aarch64__ // LUT-based implementation is used for aarch64 instead. |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 231 | else if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC) |
| 232 | { |
| 233 | float tmp_f = dequantize_qasymm8_signed(in, qi_in); |
| 234 | tmp_f = 1.f / (1.f + std::exp(-tmp_f)); |
| 235 | tmp = quantize_qasymm8_signed(tmp_f, qi_out); |
| 236 | } |
Viet-Hoa Do | 29db3d2 | 2022-08-10 11:56:49 +0100 | [diff] [blame] | 237 | #endif // __aarch64__ |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 238 | else if(act == ActivationLayerInfo::ActivationFunction::TANH) |
| 239 | { |
| 240 | float tmp_f = dequantize_qasymm8_signed(in, qi_in); |
| 241 | tmp_f = a_f32 * std::tanh(b_f32 * tmp_f); |
| 242 | tmp = quantize_qasymm8_signed(tmp_f, qi_out); |
| 243 | } |
| 244 | else if(act == ActivationLayerInfo::ActivationFunction::HARD_SWISH) |
| 245 | { |
| 246 | float tmp_f = dequantize_qasymm8_signed(in, qi_in); |
| 247 | tmp_f = tmp_f * ((std::min(std::max((tmp_f + 3), 0.0f), 6.0f)) * 0.166666667f); |
| 248 | tmp = quantize_qasymm8_signed(tmp_f, qi_out); |
| 249 | } |
Sang-Hoon Park | add8e81 | 2020-11-25 11:46:03 +0000 | [diff] [blame] | 250 | else if(act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU) |
| 251 | { |
| 252 | float tmp_f = dequantize_qasymm8_signed(in, qi_in); |
| 253 | tmp_f = tmp_f > 0 ? tmp_f : tmp_f * a_f32; |
| 254 | tmp = quantize_qasymm8_signed(tmp_f, qi_out); |
| 255 | } |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 256 | else |
| 257 | { |
| 258 | ARM_COMPUTE_ERROR("Unsupported activation function"); |
| 259 | } |
| 260 | *(output_ptr + x) = tmp; |
| 261 | } |
| 262 | }, |
| 263 | input, output); |
| 264 | } |
| 265 | } // namespace cpu |
| 266 | } // namespace arm_compute |