Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020 Arm Limited. |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_NESYMM_H |
| 25 | #define ARM_COMPUTE_NESYMM_H |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 26 | |
Sang-Hoon Park | 396cb95 | 2020-03-26 14:02:37 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 28 | |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 29 | #include "src/core/NEON/NEMath.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 30 | |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 31 | #include <arm_neon.h> |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 35 | using qsymm8_t = int8_t; /**< 8 bit quantized symmetric scalar value */ |
| 36 | using qsymm16_t = int16_t; /**< 16 bit quantized symmetric scalar value */ |
| 37 | |
| 38 | using qsymm16x8_t = int16x8_t; /**< 16 bit quantized symmetric vector with 8 elements */ |
| 39 | using qsymm16x8x2_t = int16x8x2_t; /**< 16 bit quantized symmetric vector with 16 elements */ |
| 40 | |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 41 | /** Performs final quantization step on 8 signed 16-bit elements |
| 42 | * |
| 43 | * @tparam is_bounded_relu Specified if a fused bounded relu should be applied |
| 44 | * |
| 45 | * @param[in] in_s32 Input to be quantized. |
| 46 | * @param[in] result_fixedpoint_multiplier Result multiplier parameter |
| 47 | * @param[in] result_shift Result shift parameter |
| 48 | * @param[in] min_s16 Relu lower bound |
| 49 | * @param[in] max_s16 Relu upper bound |
| 50 | * |
| 51 | * @return Quantized values |
| 52 | */ |
| 53 | template <bool is_bounded_relu> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 54 | int16x8_t finalize_quantization_int16( |
| 55 | int32x4x2_t &in_s32, int result_fixedpoint_multiplier, int32_t result_shift, int16x8_t min_s16, int16x8_t max_s16) |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 56 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 57 | if (result_shift < 0) |
Manuel Bottini | 0726398 | 2019-10-17 18:37:26 +0100 | [diff] [blame] | 58 | { |
| 59 | in_s32.val[0] = vmulq_n_s32(in_s32.val[0], (1 << -result_shift)); |
| 60 | in_s32.val[1] = vmulq_n_s32(in_s32.val[1], (1 << -result_shift)); |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 61 | |
Manuel Bottini | 0726398 | 2019-10-17 18:37:26 +0100 | [diff] [blame] | 62 | in_s32.val[0] = vqrdmulhq_n_s32(in_s32.val[0], result_fixedpoint_multiplier); |
| 63 | in_s32.val[1] = vqrdmulhq_n_s32(in_s32.val[1], result_fixedpoint_multiplier); |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar |
| 68 | in_s32.val[0] = vqrdmulhq_n_s32(in_s32.val[0], result_fixedpoint_multiplier); |
| 69 | in_s32.val[1] = vqrdmulhq_n_s32(in_s32.val[1], result_fixedpoint_multiplier); |
| 70 | // Round to the nearest division by a power-of-two using result_shift_s32 |
| 71 | in_s32.val[0] = rounding_divide_by_pow2(in_s32.val[0], result_shift); |
| 72 | in_s32.val[1] = rounding_divide_by_pow2(in_s32.val[1], result_shift); |
| 73 | } |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 74 | |
| 75 | // Convert S32 to S16 |
| 76 | int16x8_t out_s16 = vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])); |
| 77 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 78 | if (is_bounded_relu) |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 79 | { |
| 80 | out_s16 = vmaxq_s16(out_s16, min_s16); |
| 81 | out_s16 = vminq_s16(out_s16, max_s16); |
| 82 | } |
| 83 | |
| 84 | return out_s16; |
| 85 | } |
| 86 | |
| 87 | /** Performs final quantization step on single signed 16-bit element |
| 88 | * |
| 89 | * @tparam is_bounded_relu Specified if a fused bounded relu should be applied |
| 90 | * |
| 91 | * @param[in] in_value Input to be quantized. |
| 92 | * @param[in] result_fixedpoint_multiplier Result multiplier parameter |
| 93 | * @param[in] result_shift Result shift parameter |
| 94 | * @param[in] min_s16 Relu lower bound |
| 95 | * @param[in] max_s16 Relu upper bound |
| 96 | * |
| 97 | * @return Quantized values |
| 98 | */ |
| 99 | template <bool is_bounded_relu> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 100 | inline int16_t finalize_quantization_int16( |
| 101 | int32_t in_value, int result_fixedpoint_multiplier, int32_t result_shift, int16_t min_s16, int16_t max_s16) |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 102 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 103 | if (result_shift < 0) |
Manuel Bottini | 0726398 | 2019-10-17 18:37:26 +0100 | [diff] [blame] | 104 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 105 | const int64_t in_64 = static_cast<int64_t>(in_value) * (1 << (-result_shift)) * |
| 106 | static_cast<int64_t>(result_fixedpoint_multiplier); |
| 107 | in_value = static_cast<int32_t>((in_64 + (1 << 30)) >> 31); |
Manuel Bottini | 0726398 | 2019-10-17 18:37:26 +0100 | [diff] [blame] | 108 | } |
| 109 | else |
| 110 | { |
| 111 | // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar |
| 112 | const int64_t in_64 = static_cast<int64_t>(in_value) * static_cast<int64_t>(result_fixedpoint_multiplier); |
| 113 | // Shift value by result_shift_s32 |
| 114 | in_value = rounding_divide_by_pow2(static_cast<int32_t>((in_64 + (1 << 30)) >> 31), result_shift); |
| 115 | } |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 116 | |
| 117 | // Bound the result |
| 118 | int16_t out_s16 = static_cast<int16_t>(std::max<int32_t>(-32768, std::min<int32_t>(32767, in_value))); |
| 119 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 120 | if (is_bounded_relu) |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 121 | { |
| 122 | out_s16 = static_cast<int16_t>(std::max(min_s16, std::min(max_s16, out_s16))); |
| 123 | } |
| 124 | |
| 125 | return out_s16; |
| 126 | } |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 127 | |
| 128 | /** Dequantize a neon vector holding 8 16-bit quantized values. |
| 129 | * |
| 130 | * @param[in] qv Input values to be dequantized. |
| 131 | * @param[in] scale Quantization scale |
| 132 | * |
| 133 | * @return Dequantized values in a neon vector |
| 134 | */ |
| 135 | inline float32x4x2_t vdequantize_int16(const int16x8_t &qv, float scale) |
| 136 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 137 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 138 | const float32x4x2_t vdequantized_input = {{vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(qv))), vscale), |
| 139 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(qv))), vscale)}}; |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 140 | return vdequantized_input; |
| 141 | } |
| 142 | |
| 143 | /** Quantize a neon vector holding 8 floating point values. |
| 144 | * |
| 145 | * @param[in] qv Input values to be quantized. |
| 146 | * @param[in] scale Quantization scale |
| 147 | * |
| 148 | * @return A neon vector holding the quantized values |
| 149 | */ |
| 150 | inline int16x8_t vquantize_int16(const float32x4x2_t &qv, float scale) |
| 151 | { |
| 152 | const float32x4_t vinvscale = vdupq_n_f32(1.f / scale); |
| 153 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 154 | const int32x4x2_t rf = {{ |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 155 | #ifdef __aarch64__ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 156 | vcvtnq_s32_f32(vmulq_f32(qv.val[0], vinvscale)), vcvtnq_s32_f32(vmulq_f32(qv.val[1], vinvscale)) |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 157 | #else //__aarch64__ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 158 | vcvtq_s32_f32(vmulq_f32(qv.val[0], vinvscale)), vcvtq_s32_f32(vmulq_f32(qv.val[1], vinvscale)) |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 159 | #endif //__aarch64__ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 160 | }}; |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 161 | return vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1])); |
| 162 | } |
| 163 | |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 164 | /** Dequantize a neon vector holding 16 16-bit quantized values. |
| 165 | * |
| 166 | * @param[in] qv Input values to be dequantized. |
| 167 | * @param[in] qi Quantization information to be used in the computation. |
| 168 | * |
| 169 | * @return Dequantized values in a neon vector |
| 170 | */ |
| 171 | inline float32x4x4_t vdequantize(const int16x8x2_t &qv, const UniformQuantizationInfo &qi) |
| 172 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 173 | const float scale = qi.scale; |
| 174 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 175 | const float32x4x4_t vdequantized_input = {{ |
| 176 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(qv.val[0]))), vscale), |
| 177 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(qv.val[0]))), vscale), |
| 178 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(qv.val[1]))), vscale), |
| 179 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(qv.val[1]))), vscale), |
| 180 | }}; |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 181 | return vdequantized_input; |
| 182 | } |
| 183 | |
| 184 | /** Quantize a neon vector holding 16 floating point values. |
| 185 | * |
| 186 | * @param[in] qv Input values to be quantized. |
| 187 | * @param[in] qi Quantization information to be used in the computation. |
| 188 | * |
| 189 | * @return A neon vector holding the quantized values |
| 190 | */ |
| 191 | inline qsymm16x8x2_t vquantize_qsymm16(const float32x4x4_t &qv, const UniformQuantizationInfo &qi) |
| 192 | { |
| 193 | const float scale = qi.scale; |
| 194 | ARM_COMPUTE_ERROR_ON(scale == 0.f); |
| 195 | const float32x4_t vinvscale = vdupq_n_f32(1.f / scale); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 196 | const int32x4x4_t rf = {{ |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 197 | #ifdef __aarch64__ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 198 | vcvtnq_s32_f32(vmulq_f32(qv.val[0], vinvscale)), |
| 199 | vcvtnq_s32_f32(vmulq_f32(qv.val[1], vinvscale)), |
| 200 | vcvtnq_s32_f32(vmulq_f32(qv.val[2], vinvscale)), |
| 201 | vcvtnq_s32_f32(vmulq_f32(qv.val[3], vinvscale)), |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 202 | #else //__aarch64__ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 203 | vcvtq_s32_f32(vmulq_f32(qv.val[0], vinvscale)), |
| 204 | vcvtq_s32_f32(vmulq_f32(qv.val[1], vinvscale)), |
| 205 | vcvtq_s32_f32(vmulq_f32(qv.val[2], vinvscale)), |
| 206 | vcvtq_s32_f32(vmulq_f32(qv.val[3], vinvscale)), |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 207 | #endif //__aarch64__ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 208 | }}; |
| 209 | const qsymm16x8x2_t res = { |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 210 | vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1])), |
| 211 | vcombine_s16(vqmovn_s32(rf.val[2]), vqmovn_s32(rf.val[3])), |
| 212 | }; |
| 213 | |
| 214 | return res; |
| 215 | } |
| 216 | |
Sang-Hoon Park | 396cb95 | 2020-03-26 14:02:37 +0000 | [diff] [blame] | 217 | /** Multiply a neon vector using quantized multiplier and shift |
| 218 | * |
| 219 | * @param[in] input Input vector to mutiply values to be quantized. |
| 220 | * @param[in] qmul Quantized multipler |
| 221 | * @param[in] shift Left bit shift |
| 222 | * |
| 223 | * @return A neon vector holding the multiplied value |
| 224 | */ |
Sang-Hoon Park | 0d008f7 | 2020-03-13 14:56:05 +0000 | [diff] [blame] | 225 | inline int32x4x2_t multiply_by_quantized_multiplier_2row(int32x4x2_t input, int32_t qmul, int32_t shift) |
Sang-Hoon Park | 396cb95 | 2020-03-26 14:02:37 +0000 | [diff] [blame] | 226 | { |
| 227 | const auto left_shift = shift > 0 ? shift : 0; |
| 228 | const auto right_shift = shift > 0 ? 0 : -shift; |
| 229 | const auto one_shifted = 1 << left_shift; |
| 230 | |
| 231 | int32x4x2_t result; |
| 232 | result.val[0] = rounding_divide_by_pow2(vqrdmulhq_n_s32(vmulq_n_s32(input.val[0], one_shifted), qmul), right_shift); |
| 233 | result.val[1] = rounding_divide_by_pow2(vqrdmulhq_n_s32(vmulq_n_s32(input.val[1], one_shifted), qmul), right_shift); |
| 234 | |
| 235 | return result; |
| 236 | } |
| 237 | |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 238 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 239 | #endif // ARM_COMPUTE_NESYMM_H |