1 /*
2 * Copyright (c) 2019-2020 Arm Limited.
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 #ifndef ARM_COMPUTE_NESYMM_H
25 #define ARM_COMPUTE_NESYMM_H
26
27 #include "arm_compute/core/utils/quantization/AsymmHelpers.h"
28 #include "src/core/NEON/NEMath.h"
29 #include <arm_neon.h>
30
31 namespace arm_compute
32 {
33 using qsymm8_t = int8_t; /**< 8 bit quantized symmetric scalar value */
34 using qsymm16_t = int16_t; /**< 16 bit quantized symmetric scalar value */
35
36 using qsymm16x8_t = int16x8_t; /**< 16 bit quantized symmetric vector with 8 elements */
37 using qsymm16x8x2_t = int16x8x2_t; /**< 16 bit quantized symmetric vector with 16 elements */
38
39 /** Performs final quantization step on 8 signed 16-bit elements
40 *
41 * @tparam is_bounded_relu Specified if a fused bounded relu should be applied
42 *
43 * @param[in] in_s32 Input to be quantized.
44 * @param[in] result_fixedpoint_multiplier Result multiplier parameter
45 * @param[in] result_shift Result shift parameter
46 * @param[in] min_s16 Relu lower bound
47 * @param[in] max_s16 Relu upper bound
48 *
49 * @return Quantized values
50 */
51 template <bool is_bounded_relu>
finalize_quantization_int16(int32x4x2_t & in_s32,int result_fixedpoint_multiplier,int32_t result_shift,int16x8_t min_s16,int16x8_t max_s16)52 int16x8_t finalize_quantization_int16(int32x4x2_t &in_s32,
53 int result_fixedpoint_multiplier,
54 int32_t result_shift,
55 int16x8_t min_s16,
56 int16x8_t max_s16)
57 {
58 if(result_shift < 0)
59 {
60 in_s32.val[0] = vmulq_n_s32(in_s32.val[0], (1 << -result_shift));
61 in_s32.val[1] = vmulq_n_s32(in_s32.val[1], (1 << -result_shift));
62
63 in_s32.val[0] = vqrdmulhq_n_s32(in_s32.val[0], result_fixedpoint_multiplier);
64 in_s32.val[1] = vqrdmulhq_n_s32(in_s32.val[1], result_fixedpoint_multiplier);
65 }
66 else
67 {
68 // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar
69 in_s32.val[0] = vqrdmulhq_n_s32(in_s32.val[0], result_fixedpoint_multiplier);
70 in_s32.val[1] = vqrdmulhq_n_s32(in_s32.val[1], result_fixedpoint_multiplier);
71 // Round to the nearest division by a power-of-two using result_shift_s32
72 in_s32.val[0] = rounding_divide_by_pow2(in_s32.val[0], result_shift);
73 in_s32.val[1] = rounding_divide_by_pow2(in_s32.val[1], result_shift);
74 }
75
76 // Convert S32 to S16
77 int16x8_t out_s16 = vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1]));
78
79 if(is_bounded_relu)
80 {
81 out_s16 = vmaxq_s16(out_s16, min_s16);
82 out_s16 = vminq_s16(out_s16, max_s16);
83 }
84
85 return out_s16;
86 }
87
88 /** Performs final quantization step on single signed 16-bit element
89 *
90 * @tparam is_bounded_relu Specified if a fused bounded relu should be applied
91 *
92 * @param[in] in_value Input to be quantized.
93 * @param[in] result_fixedpoint_multiplier Result multiplier parameter
94 * @param[in] result_shift Result shift parameter
95 * @param[in] min_s16 Relu lower bound
96 * @param[in] max_s16 Relu upper bound
97 *
98 * @return Quantized values
99 */
100 template <bool is_bounded_relu>
finalize_quantization_int16(int32_t in_value,int result_fixedpoint_multiplier,int32_t result_shift,int16_t min_s16,int16_t max_s16)101 inline int16_t finalize_quantization_int16(int32_t in_value, int result_fixedpoint_multiplier,
102 int32_t result_shift, int16_t min_s16, int16_t max_s16)
103 {
104 if(result_shift < 0)
105 {
106 const int64_t in_64 = static_cast<int64_t>(in_value) * (1 << (-result_shift)) * static_cast<int64_t>(result_fixedpoint_multiplier);
107 in_value = static_cast<int32_t>((in_64 + (1 << 30)) >> 31);
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 }
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
120 if(is_bounded_relu)
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 }
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 */
vdequantize_int16(const int16x8_t & qv,float scale)135 inline float32x4x2_t vdequantize_int16(const int16x8_t &qv, float scale)
136 {
137 const float32x4_t vscale = vdupq_n_f32(scale);
138 const float32x4x2_t vdequantized_input =
139 {
140 {
141 vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(qv))), vscale),
142 vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(qv))), vscale)
143 }
144 };
145 return vdequantized_input;
146 }
147
148 /** Quantize a neon vector holding 8 floating point values.
149 *
150 * @param[in] qv Input values to be quantized.
151 * @param[in] scale Quantization scale
152 *
153 * @return A neon vector holding the quantized values
154 */
vquantize_int16(const float32x4x2_t & qv,float scale)155 inline int16x8_t vquantize_int16(const float32x4x2_t &qv, float scale)
156 {
157 const float32x4_t vinvscale = vdupq_n_f32(1.f / scale);
158
159 const int32x4x2_t rf =
160 {
161 {
162 #ifdef __aarch64__
163 vcvtnq_s32_f32(vmulq_f32(qv.val[0], vinvscale)),
164 vcvtnq_s32_f32(vmulq_f32(qv.val[1], vinvscale))
165 #else //__aarch64__
166 vcvtq_s32_f32(vmulq_f32(qv.val[0], vinvscale)),
167 vcvtq_s32_f32(vmulq_f32(qv.val[1], vinvscale))
168 #endif //__aarch64__
169 }
170 };
171 return vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1]));
172 }
173
174 /** Dequantize a neon vector holding 16 16-bit quantized values.
175 *
176 * @param[in] qv Input values to be dequantized.
177 * @param[in] qi Quantization information to be used in the computation.
178 *
179 * @return Dequantized values in a neon vector
180 */
vdequantize(const int16x8x2_t & qv,const UniformQuantizationInfo & qi)181 inline float32x4x4_t vdequantize(const int16x8x2_t &qv, const UniformQuantizationInfo &qi)
182 {
183 const float scale = qi.scale;
184 const float32x4_t vscale = vdupq_n_f32(scale);
185 const float32x4x4_t vdequantized_input =
186 {
187 {
188 vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(qv.val[0]))), vscale),
189 vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(qv.val[0]))), vscale),
190 vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(qv.val[1]))), vscale),
191 vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(qv.val[1]))), vscale),
192 }
193 };
194 return vdequantized_input;
195 }
196
197 /** Quantize a neon vector holding 16 floating point values.
198 *
199 * @param[in] qv Input values to be quantized.
200 * @param[in] qi Quantization information to be used in the computation.
201 *
202 * @return A neon vector holding the quantized values
203 */
vquantize_qsymm16(const float32x4x4_t & qv,const UniformQuantizationInfo & qi)204 inline qsymm16x8x2_t vquantize_qsymm16(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
205 {
206 const float scale = qi.scale;
207 ARM_COMPUTE_ERROR_ON(scale == 0.f);
208 const float32x4_t vinvscale = vdupq_n_f32(1.f / scale);
209 const int32x4x4_t rf =
210 {
211 {
212 #ifdef __aarch64__
213 vcvtnq_s32_f32(vmulq_f32(qv.val[0], vinvscale)),
214 vcvtnq_s32_f32(vmulq_f32(qv.val[1], vinvscale)),
215 vcvtnq_s32_f32(vmulq_f32(qv.val[2], vinvscale)),
216 vcvtnq_s32_f32(vmulq_f32(qv.val[3], vinvscale)),
217 #else //__aarch64__
218 vcvtq_s32_f32(vmulq_f32(qv.val[0], vinvscale)),
219 vcvtq_s32_f32(vmulq_f32(qv.val[1], vinvscale)),
220 vcvtq_s32_f32(vmulq_f32(qv.val[2], vinvscale)),
221 vcvtq_s32_f32(vmulq_f32(qv.val[3], vinvscale)),
222 #endif //__aarch64__
223 }
224 };
225 const qsymm16x8x2_t res =
226 {
227 vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1])),
228 vcombine_s16(vqmovn_s32(rf.val[2]), vqmovn_s32(rf.val[3])),
229 };
230
231 return res;
232 }
233
234 /** Multiply a neon vector using quantized multiplier and shift
235 *
236 * @param[in] input Input vector to mutiply values to be quantized.
237 * @param[in] qmul Quantized multipler
238 * @param[in] shift Left bit shift
239 *
240 * @return A neon vector holding the multiplied value
241 */
multiply_by_quantized_multiplier_2row(int32x4x2_t input,int32_t qmul,int32_t shift)242 inline int32x4x2_t multiply_by_quantized_multiplier_2row(int32x4x2_t input, int32_t qmul, int32_t shift)
243 {
244 const auto left_shift = shift > 0 ? shift : 0;
245 const auto right_shift = shift > 0 ? 0 : -shift;
246 const auto one_shifted = 1 << left_shift;
247
248 int32x4x2_t result;
249 result.val[0] = rounding_divide_by_pow2(vqrdmulhq_n_s32(vmulq_n_s32(input.val[0], one_shifted), qmul), right_shift);
250 result.val[1] = rounding_divide_by_pow2(vqrdmulhq_n_s32(vmulq_n_s32(input.val[1], one_shifted), qmul), right_shift);
251
252 return result;
253 }
254
255 } // namespace arm_compute
256 #endif // ARM_COMPUTE_NESYMM_H
257