blob: ec246efc8c4984a33534a8aca539a10ddc3f3f9a [file] [log] [blame]
Gian Marco Iodicebc415af2019-06-13 15:58:32 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 Arm Limited.
Gian Marco Iodicebc415af2019-06-13 15:58:32 +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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_NESYMM_H
25#define ARM_COMPUTE_NESYMM_H
Gian Marco Iodicebc415af2019-06-13 15:58:32 +010026
Sang-Hoon Park396cb952020-03-26 14:02:37 +000027#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010029#include "src/core/NEON/NEMath.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030
Gian Marco Iodicebc415af2019-06-13 15:58:32 +010031#include <arm_neon.h>
32
33namespace arm_compute
34{
Manuel Bottini7bb56c62019-06-26 15:17:09 +010035using qsymm8_t = int8_t; /**< 8 bit quantized symmetric scalar value */
36using qsymm16_t = int16_t; /**< 16 bit quantized symmetric scalar value */
37
38using qsymm16x8_t = int16x8_t; /**< 16 bit quantized symmetric vector with 8 elements */
39using qsymm16x8x2_t = int16x8x2_t; /**< 16 bit quantized symmetric vector with 16 elements */
40
Gian Marco Iodicebc415af2019-06-13 15:58:32 +010041/** 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 */
53template <bool is_bounded_relu>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054int16x8_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 Iodicebc415af2019-06-13 15:58:32 +010056{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 if (result_shift < 0)
Manuel Bottini07263982019-10-17 18:37:26 +010058 {
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 Iodicebc415af2019-06-13 15:58:32 +010061
Manuel Bottini07263982019-10-17 18:37:26 +010062 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 Iodicebc415af2019-06-13 15:58:32 +010074
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010078 if (is_bounded_relu)
Gian Marco Iodicebc415af2019-06-13 15:58:32 +010079 {
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 */
99template <bool is_bounded_relu>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100100inline 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 Iodicebc415af2019-06-13 15:58:32 +0100102{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 if (result_shift < 0)
Manuel Bottini07263982019-10-17 18:37:26 +0100104 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100105 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 Bottini07263982019-10-17 18:37:26 +0100108 }
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 Iodicebc415af2019-06-13 15:58:32 +0100116
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100120 if (is_bounded_relu)
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100121 {
122 out_s16 = static_cast<int16_t>(std::max(min_s16, std::min(max_s16, out_s16)));
123 }
124
125 return out_s16;
126}
giuros01c9573f32019-06-20 10:30:17 +0100127
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 */
135inline float32x4x2_t vdequantize_int16(const int16x8_t &qv, float scale)
136{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100137 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)}};
giuros01c9573f32019-06-20 10:30:17 +0100140 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 */
150inline int16x8_t vquantize_int16(const float32x4x2_t &qv, float scale)
151{
152 const float32x4_t vinvscale = vdupq_n_f32(1.f / scale);
153
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100154 const int32x4x2_t rf = {{
giuros01c9573f32019-06-20 10:30:17 +0100155#ifdef __aarch64__
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100156 vcvtnq_s32_f32(vmulq_f32(qv.val[0], vinvscale)), vcvtnq_s32_f32(vmulq_f32(qv.val[1], vinvscale))
giuros01c9573f32019-06-20 10:30:17 +0100157#else //__aarch64__
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100158 vcvtq_s32_f32(vmulq_f32(qv.val[0], vinvscale)), vcvtq_s32_f32(vmulq_f32(qv.val[1], vinvscale))
giuros01c9573f32019-06-20 10:30:17 +0100159#endif //__aarch64__
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100160 }};
giuros01c9573f32019-06-20 10:30:17 +0100161 return vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1]));
162}
163
Manuel Bottini7bb56c62019-06-26 15:17:09 +0100164/** 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 */
171inline float32x4x4_t vdequantize(const int16x8x2_t &qv, const UniformQuantizationInfo &qi)
172{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100173 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 Bottini7bb56c62019-06-26 15:17:09 +0100181 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 */
191inline 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100196 const int32x4x4_t rf = {{
Manuel Bottini7bb56c62019-06-26 15:17:09 +0100197#ifdef __aarch64__
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100198 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 Bottini7bb56c62019-06-26 15:17:09 +0100202#else //__aarch64__
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100203 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 Bottini7bb56c62019-06-26 15:17:09 +0100207#endif //__aarch64__
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100208 }};
209 const qsymm16x8x2_t res = {
Manuel Bottini7bb56c62019-06-26 15:17:09 +0100210 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 Park396cb952020-03-26 14:02:37 +0000217/** 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 Park0d008f72020-03-13 14:56:05 +0000225inline int32x4x2_t multiply_by_quantized_multiplier_2row(int32x4x2_t input, int32_t qmul, int32_t shift)
Sang-Hoon Park396cb952020-03-26 14:02:37 +0000226{
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 Iodicebc415af2019-06-13 15:58:32 +0100238} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000239#endif // ARM_COMPUTE_NESYMM_H