blob: 6dee8705f42a950d7896a94d209a1b8a119af950 [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
Manuel Bottini7bb56c62019-06-26 15:17:09 +010027#include "arm_compute/core/NEON/NEMath.h"
Sang-Hoon Park396cb952020-03-26 14:02:37 +000028#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Gian Marco Iodicebc415af2019-06-13 15:58:32 +010029#include <arm_neon.h>
30
31namespace arm_compute
32{
Manuel Bottini7bb56c62019-06-26 15:17:09 +010033using qsymm8_t = int8_t; /**< 8 bit quantized symmetric scalar value */
34using qsymm16_t = int16_t; /**< 16 bit quantized symmetric scalar value */
35
36using qsymm16x8_t = int16x8_t; /**< 16 bit quantized symmetric vector with 8 elements */
37using qsymm16x8x2_t = int16x8x2_t; /**< 16 bit quantized symmetric vector with 16 elements */
38
Gian Marco Iodicebc415af2019-06-13 15:58:32 +010039/** 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 */
51template <bool is_bounded_relu>
52int16x8_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{
Manuel Bottini07263982019-10-17 18:37:26 +010058 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));
Gian Marco Iodicebc415af2019-06-13 15:58:32 +010062
Manuel Bottini07263982019-10-17 18:37:26 +010063 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 }
Gian Marco Iodicebc415af2019-06-13 15:58:32 +010075
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 */
100template <bool is_bounded_relu>
101inline 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{
Manuel Bottini07263982019-10-17 18:37:26 +0100104 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);
Michalis Spyrouf4643372019-11-29 16:17:13 +0000107 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
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}
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{
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 */
155inline 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
Manuel Bottini7bb56c62019-06-26 15:17:09 +0100174/** 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 */
181inline 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 */
204inline 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
Sang-Hoon Park396cb952020-03-26 14:02:37 +0000234/** 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 */
Sang-Hoon Park0d008f72020-03-13 14:56:05 +0000242inline int32x4x2_t multiply_by_quantized_multiplier_2row(int32x4x2_t input, int32_t qmul, int32_t shift)
Sang-Hoon Park396cb952020-03-26 14:02:37 +0000243{
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
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100255} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000256#endif // ARM_COMPUTE_NESYMM_H