Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 1 | /* |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2020-2023 Arm Limited. |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [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 | */ |
alerah01 | 4cbcb84 | 2022-02-28 06:38:08 +0200 | [diff] [blame] | 24 | |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 25 | #include "arm_compute/core/Helpers.h" |
| 26 | #include "arm_compute/core/Window.h" |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 27 | #include "arm_compute/function_info/ActivationLayerInfo.h" |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 28 | |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 29 | #include <cmath> |
| 30 | #include <cstddef> |
| 31 | |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 32 | #include "src/core/NEON/SVEAsymm.h" |
| 33 | #include "src/core/NEON/SVEMath.h" |
| 34 | #include <arm_sve.h> |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace cpu |
| 39 | { |
Dana Zlotnik | 3229171 | 2021-11-25 09:58:27 +0200 | [diff] [blame] | 40 | void sve2_qasymm8_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window) |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 41 | { |
| 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 | |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 52 | const UniformQuantizationInfo qi_in = src->info()->quantization_info().uniform(); |
| 53 | const UniformQuantizationInfo qi_out = dst->info()->quantization_info().uniform(); |
| 54 | const auto va = svdup_n_u8(quantize_qasymm8(act_info.a(), qi_in)); |
| 55 | const auto vb = svdup_n_u8(quantize_qasymm8(act_info.b(), qi_in)); |
| 56 | const auto const_0 = quantize_qasymm8(0.f, qi_in); |
| 57 | const auto vconst_0 = svdup_n_u8(const_0); |
| 58 | const auto vconst_1 = svdup_n_f32(1.f); |
| 59 | const auto va_f32 = svdup_n_f32(act_info.a()); |
| 60 | const auto vb_f32 = svdup_n_f32(act_info.b()); |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 61 | |
| 62 | // Initialise scale/offset for re-quantization |
| 63 | bool requant = true; |
| 64 | if(qi_in.scale == qi_out.scale && qi_in.offset == qi_out.offset) |
| 65 | { |
| 66 | requant = false; |
| 67 | } |
| 68 | float s = qi_in.scale / qi_out.scale; |
| 69 | float o = -qi_in.offset * s + qi_out.offset; |
| 70 | auto vs = svdup_n_f32(s); |
| 71 | auto vo = svdup_n_f32(o); |
| 72 | |
arngra01 | 805145d | 2021-01-04 14:28:40 +0000 | [diff] [blame] | 73 | // Initialise scale/offset for re-quantization with int32_t |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 74 | const auto voffset_in = svdup_n_s32(qi_in.offset); |
| 75 | int32_t s_s32 = round(s * (1 << 8), arm_compute::RoundingPolicy::TO_NEAREST_EVEN); |
| 76 | int32_t o_s32 = round(o * (1 << 8), arm_compute::RoundingPolicy::TO_NEAREST_EVEN); |
| 77 | const auto vs_s32 = svdup_n_s32(s_s32); |
| 78 | const auto vo_s32 = svdup_n_s32(o_s32); |
arngra01 | 805145d | 2021-01-04 14:28:40 +0000 | [diff] [blame] | 79 | |
| 80 | // Initialise scale/offset for re-quantization for leaky relu |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 81 | int32_t s_leaky_s32 = round(s * act_info.a() * (1 << 8), arm_compute::RoundingPolicy::TO_NEAREST_EVEN); |
| 82 | int32_t o_leaky_s32 = round((-qi_in.offset * s * act_info.a() + qi_out.offset) * (1 << 8), |
| 83 | arm_compute::RoundingPolicy::TO_NEAREST_EVEN); |
| 84 | const auto vs_leaky_s32 = svdup_n_s32(s_leaky_s32); |
| 85 | const auto vo_leaky_s32 = svdup_n_s32(o_leaky_s32); |
arngra01 | 805145d | 2021-01-04 14:28:40 +0000 | [diff] [blame] | 86 | |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 87 | execute_window_loop(win_collapsed, [&](const Coordinates &) |
| 88 | { |
| 89 | const auto input_ptr = reinterpret_cast<const uint8_t *>(input.ptr()); |
| 90 | const auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr()); |
| 91 | |
| 92 | svuint8_t tmp; |
| 93 | |
| 94 | int x = window_start_x; |
| 95 | svbool_t pg = svwhilelt_b8(x, window_end_x); |
| 96 | do |
| 97 | { |
| 98 | const auto vin = svld1_u8(pg, input_ptr + x); |
| 99 | if(act == ActivationLayerInfo::ActivationFunction::RELU) |
| 100 | { |
| 101 | // Perform activation |
| 102 | tmp = svmax_u8_z(pg, vconst_0, vin); |
| 103 | // Re-quantize to new output space |
| 104 | tmp = requant ? svmla_qasymm8_z(pg, tmp, vs, vo) : tmp; |
| 105 | } |
| 106 | else if(act == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU) |
| 107 | { |
| 108 | // Perform activation |
| 109 | tmp = svmin_u8_z(pg, va, svmax_u8_z(pg, vconst_0, vin)); |
| 110 | // Re-quantize to new output space |
| 111 | tmp = requant ? svmla_qasymm8_z(pg, tmp, vs, vo) : tmp; |
| 112 | } |
| 113 | else if(act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU) |
| 114 | { |
| 115 | // Perform activation |
| 116 | tmp = svmin_u8_z(pg, va, svmax_u8_z(pg, vb, vin)); |
| 117 | // Re-quantize to new output space |
| 118 | tmp = svmla_qasymm8_z(pg, tmp, vs, vo); |
| 119 | } |
| 120 | else if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC) |
| 121 | { |
| 122 | // De-quantize |
| 123 | const auto vin_deq = svdequantize_z(pg, vin, qi_in); |
| 124 | // Perform activation |
Michalis Spyrou | 168d6a8 | 2022-05-03 17:15:42 +0100 | [diff] [blame] | 125 | const svfloat32x4_t tmp_dep = svcreate4_f32(svdiv_f32_z(pg, vconst_1, svadd_f32_z(pg, vconst_1, svexp_f32_z(pg, svneg_f32_z(pg, svget4_f32(vin_deq, 0))))), |
| 126 | svdiv_f32_z(pg, vconst_1, svadd_f32_z(pg, vconst_1, svexp_f32_z(pg, svneg_f32_z(pg, svget4_f32(vin_deq, 1))))), |
| 127 | svdiv_f32_z(pg, vconst_1, svadd_f32_z(pg, vconst_1, svexp_f32_z(pg, svneg_f32_z(pg, svget4_f32(vin_deq, 2))))), |
| 128 | svdiv_f32_z(pg, vconst_1, svadd_f32_z(pg, vconst_1, svexp_f32_z(pg, svneg_f32_z(pg, svget4_f32(vin_deq, 3)))))); |
| 129 | |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 130 | // Re-quantize to new output space |
| 131 | tmp = svquantize_z(pg, tmp_dep, qi_out); |
| 132 | } |
| 133 | else if(act == ActivationLayerInfo::ActivationFunction::TANH) |
| 134 | { |
| 135 | // De-quantize |
| 136 | const auto vin_deq = svdequantize_z(pg, vin, qi_in); |
| 137 | // Perform activation |
Michalis Spyrou | 168d6a8 | 2022-05-03 17:15:42 +0100 | [diff] [blame] | 138 | const svfloat32x4_t tmp_dep = svcreate4_f32(svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget4_f32(vin_deq, 0), vb_f32))), |
| 139 | svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget4_f32(vin_deq, 1), vb_f32))), |
| 140 | svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget4_f32(vin_deq, 2), vb_f32))), |
| 141 | svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget4_f32(vin_deq, 3), vb_f32)))); |
| 142 | |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 143 | // Re-quantize to new output space |
| 144 | tmp = svquantize_z(pg, tmp_dep, qi_out); |
| 145 | } |
arngra01 | 805145d | 2021-01-04 14:28:40 +0000 | [diff] [blame] | 146 | else if(act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU) |
| 147 | { |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 148 | svbool_t p0, p1, p2, p3; |
arngra01 | 805145d | 2021-01-04 14:28:40 +0000 | [diff] [blame] | 149 | svint32x4_t tmp_dep; |
| 150 | |
| 151 | // Expand to int32 |
Michalis Spyrou | 168d6a8 | 2022-05-03 17:15:42 +0100 | [diff] [blame] | 152 | const svint32x4_t vin_s32 = svcreate4_s32( |
| 153 | svreinterpret_s32_u32(svmovlb_u32(svmovlb_u16(vin))), |
| 154 | svreinterpret_s32_u32(svmovlt_u32(svmovlb_u16(vin))), |
| 155 | svreinterpret_s32_u32(svmovlb_u32(svmovlt_u16(vin))), |
| 156 | svreinterpret_s32_u32(svmovlt_u32(svmovlt_u16(vin)))); |
arngra01 | 805145d | 2021-01-04 14:28:40 +0000 | [diff] [blame] | 157 | |
| 158 | // Compare elements to input offset |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 159 | if(qi_in.scale >= 0) |
arngra01 | 805145d | 2021-01-04 14:28:40 +0000 | [diff] [blame] | 160 | { |
| 161 | p0 = svcmplt_s32(pg, svget4_s32(vin_s32, 0), voffset_in); |
| 162 | p1 = svcmplt_s32(pg, svget4_s32(vin_s32, 1), voffset_in); |
| 163 | p2 = svcmplt_s32(pg, svget4_s32(vin_s32, 2), voffset_in); |
| 164 | p3 = svcmplt_s32(pg, svget4_s32(vin_s32, 3), voffset_in); |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | p0 = svcmpgt_s32(pg, svget4_s32(vin_s32, 0), voffset_in); |
| 169 | p1 = svcmpgt_s32(pg, svget4_s32(vin_s32, 1), voffset_in); |
| 170 | p2 = svcmpgt_s32(pg, svget4_s32(vin_s32, 2), voffset_in); |
| 171 | p3 = svcmpgt_s32(pg, svget4_s32(vin_s32, 3), voffset_in); |
| 172 | } |
| 173 | |
| 174 | // Multiply negative elements and requantize if necessary |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 175 | if(requant) |
arngra01 | 805145d | 2021-01-04 14:28:40 +0000 | [diff] [blame] | 176 | { |
| 177 | tmp_dep = svcreate4_s32( |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 178 | svasr_n_s32_m(pg, svmla_s32_m(pg, svsel(p0, vo_leaky_s32, vo_s32), svget4_s32(vin_s32, 0), svsel(p0, vs_leaky_s32, vs_s32)), 8), |
| 179 | svasr_n_s32_m(pg, svmla_s32_m(pg, svsel(p1, vo_leaky_s32, vo_s32), svget4_s32(vin_s32, 1), svsel(p1, vs_leaky_s32, vs_s32)), 8), |
| 180 | svasr_n_s32_m(pg, svmla_s32_m(pg, svsel(p2, vo_leaky_s32, vo_s32), svget4_s32(vin_s32, 2), svsel(p2, vs_leaky_s32, vs_s32)), 8), |
| 181 | svasr_n_s32_m(pg, svmla_s32_m(pg, svsel(p3, vo_leaky_s32, vo_s32), svget4_s32(vin_s32, 3), svsel(p3, vs_leaky_s32, vs_s32)), 8)); |
arngra01 | 805145d | 2021-01-04 14:28:40 +0000 | [diff] [blame] | 182 | } |
| 183 | else |
| 184 | { |
| 185 | tmp_dep = svcreate4_s32( |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 186 | svasr_n_s32_m(p0, svmad_s32_m(p0, svget4_s32(vin_s32, 0), vs_leaky_s32, vo_leaky_s32), 8), |
| 187 | svasr_n_s32_m(p1, svmad_s32_m(p1, svget4_s32(vin_s32, 1), vs_leaky_s32, vo_leaky_s32), 8), |
| 188 | svasr_n_s32_m(p2, svmad_s32_m(p2, svget4_s32(vin_s32, 2), vs_leaky_s32, vo_leaky_s32), 8), |
| 189 | svasr_n_s32_m(p3, svmad_s32_m(p3, svget4_s32(vin_s32, 3), vs_leaky_s32, vo_leaky_s32), 8)); |
arngra01 | 805145d | 2021-01-04 14:28:40 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // Convert uint32 vectors to uint16 vectors (with saturation) |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 193 | const auto v_low_u16 = svqxtunt_s32(svqxtunb_s32(svget4_s32(tmp_dep, 0)), svget4_s32(tmp_dep, 1)); |
arngra01 | 805145d | 2021-01-04 14:28:40 +0000 | [diff] [blame] | 194 | const auto v_high_u16 = svqxtunt_s32(svqxtunb_s32(svget4_s32(tmp_dep, 2)), svget4_s32(tmp_dep, 3)); |
| 195 | |
| 196 | // convert uint16 vectors to uint8 vectors (with saturation) |
| 197 | tmp = svqxtnt_u16(svqxtnb_u16(v_low_u16), v_high_u16); |
| 198 | } |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 199 | else |
| 200 | { |
| 201 | ARM_COMPUTE_ERROR("Unsupported activation function"); |
| 202 | } |
| 203 | |
| 204 | svst1_u8(pg, output_ptr + x, tmp); |
| 205 | |
| 206 | x += svcntb(); |
| 207 | pg = svwhilelt_b8(x, window_end_x); |
| 208 | |
| 209 | } |
| 210 | while(svptest_any(svptrue_b8(), pg)); |
| 211 | |
| 212 | }, |
| 213 | input, output); |
| 214 | } |
| 215 | } // namespace cpu |
| 216 | } // namespace arm_compute |