blob: 924840930a79b5dc5bb9029b4e88bb4e4dec926b [file] [log] [blame]
Gian Marco Iodicebc415af2019-06-13 15:58:32 +01001/*
2 * Copyright (c) 2019 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 */
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"
Gian Marco Iodicebc415af2019-06-13 15:58:32 +010028#include <arm_neon.h>
29
30namespace arm_compute
31{
Manuel Bottini7bb56c62019-06-26 15:17:09 +010032using qsymm8_t = int8_t; /**< 8 bit quantized symmetric scalar value */
33using qsymm16_t = int16_t; /**< 16 bit quantized symmetric scalar value */
34
35using qsymm16x8_t = int16x8_t; /**< 16 bit quantized symmetric vector with 8 elements */
36using qsymm16x8x2_t = int16x8x2_t; /**< 16 bit quantized symmetric vector with 16 elements */
37
Gian Marco Iodicebc415af2019-06-13 15:58:32 +010038/** Performs final quantization step on 8 signed 16-bit elements
39 *
40 * @tparam is_bounded_relu Specified if a fused bounded relu should be applied
41 *
42 * @param[in] in_s32 Input to be quantized.
43 * @param[in] result_fixedpoint_multiplier Result multiplier parameter
44 * @param[in] result_shift Result shift parameter
45 * @param[in] min_s16 Relu lower bound
46 * @param[in] max_s16 Relu upper bound
47 *
48 * @return Quantized values
49 */
50template <bool is_bounded_relu>
51int16x8_t finalize_quantization_int16(int32x4x2_t &in_s32,
52 int result_fixedpoint_multiplier,
53 int32_t result_shift,
54 int16x8_t min_s16,
55 int16x8_t max_s16)
56{
Manuel Bottini07263982019-10-17 18:37:26 +010057 if(result_shift < 0)
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 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
78 if(is_bounded_relu)
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 */
99template <bool is_bounded_relu>
100inline int16_t finalize_quantization_int16(int32_t in_value, int result_fixedpoint_multiplier,
101 int32_t result_shift, int16_t min_s16, int16_t max_s16)
102{
Manuel Bottini07263982019-10-17 18:37:26 +0100103 if(result_shift < 0)
104 {
105 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 +0000106 in_value = static_cast<int32_t>((in_64 + (1 << 30)) >> 31);
Manuel Bottini07263982019-10-17 18:37:26 +0100107 }
108 else
109 {
110 // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar
111 const int64_t in_64 = static_cast<int64_t>(in_value) * static_cast<int64_t>(result_fixedpoint_multiplier);
112 // Shift value by result_shift_s32
113 in_value = rounding_divide_by_pow2(static_cast<int32_t>((in_64 + (1 << 30)) >> 31), result_shift);
114 }
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100115
116 // Bound the result
117 int16_t out_s16 = static_cast<int16_t>(std::max<int32_t>(-32768, std::min<int32_t>(32767, in_value)));
118
119 if(is_bounded_relu)
120 {
121 out_s16 = static_cast<int16_t>(std::max(min_s16, std::min(max_s16, out_s16)));
122 }
123
124 return out_s16;
125}
giuros01c9573f32019-06-20 10:30:17 +0100126
127/** Dequantize a neon vector holding 8 16-bit quantized values.
128 *
129 * @param[in] qv Input values to be dequantized.
130 * @param[in] scale Quantization scale
131 *
132 * @return Dequantized values in a neon vector
133 */
134inline float32x4x2_t vdequantize_int16(const int16x8_t &qv, float scale)
135{
136 const float32x4_t vscale = vdupq_n_f32(scale);
137 const float32x4x2_t vdequantized_input =
138 {
139 {
140 vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(qv))), vscale),
141 vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(qv))), vscale)
142 }
143 };
144 return vdequantized_input;
145}
146
147/** Quantize a neon vector holding 8 floating point values.
148 *
149 * @param[in] qv Input values to be quantized.
150 * @param[in] scale Quantization scale
151 *
152 * @return A neon vector holding the quantized values
153 */
154inline int16x8_t vquantize_int16(const float32x4x2_t &qv, float scale)
155{
156 const float32x4_t vinvscale = vdupq_n_f32(1.f / scale);
157
158 const int32x4x2_t rf =
159 {
160 {
161#ifdef __aarch64__
162 vcvtnq_s32_f32(vmulq_f32(qv.val[0], vinvscale)),
163 vcvtnq_s32_f32(vmulq_f32(qv.val[1], vinvscale))
164#else //__aarch64__
165 vcvtq_s32_f32(vmulq_f32(qv.val[0], vinvscale)),
166 vcvtq_s32_f32(vmulq_f32(qv.val[1], vinvscale))
167#endif //__aarch64__
168 }
169 };
170 return vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1]));
171}
172
Manuel Bottini7bb56c62019-06-26 15:17:09 +0100173/** Dequantize a neon vector holding 16 16-bit quantized values.
174 *
175 * @param[in] qv Input values to be dequantized.
176 * @param[in] qi Quantization information to be used in the computation.
177 *
178 * @return Dequantized values in a neon vector
179 */
180inline float32x4x4_t vdequantize(const int16x8x2_t &qv, const UniformQuantizationInfo &qi)
181{
182 const float scale = qi.scale;
183 const float32x4_t vscale = vdupq_n_f32(scale);
184 const float32x4x4_t vdequantized_input =
185 {
186 {
187 vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(qv.val[0]))), vscale),
188 vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(qv.val[0]))), vscale),
189 vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(qv.val[1]))), vscale),
190 vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(qv.val[1]))), vscale),
191 }
192 };
193 return vdequantized_input;
194}
195
196/** Quantize a neon vector holding 16 floating point values.
197 *
198 * @param[in] qv Input values to be quantized.
199 * @param[in] qi Quantization information to be used in the computation.
200 *
201 * @return A neon vector holding the quantized values
202 */
203inline qsymm16x8x2_t vquantize_qsymm16(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
204{
205 const float scale = qi.scale;
206 ARM_COMPUTE_ERROR_ON(scale == 0.f);
207 const float32x4_t vinvscale = vdupq_n_f32(1.f / scale);
208 const int32x4x4_t rf =
209 {
210 {
211#ifdef __aarch64__
212 vcvtnq_s32_f32(vmulq_f32(qv.val[0], vinvscale)),
213 vcvtnq_s32_f32(vmulq_f32(qv.val[1], vinvscale)),
214 vcvtnq_s32_f32(vmulq_f32(qv.val[2], vinvscale)),
215 vcvtnq_s32_f32(vmulq_f32(qv.val[3], vinvscale)),
216#else //__aarch64__
217 vcvtq_s32_f32(vmulq_f32(qv.val[0], vinvscale)),
218 vcvtq_s32_f32(vmulq_f32(qv.val[1], vinvscale)),
219 vcvtq_s32_f32(vmulq_f32(qv.val[2], vinvscale)),
220 vcvtq_s32_f32(vmulq_f32(qv.val[3], vinvscale)),
221#endif //__aarch64__
222 }
223 };
224 const qsymm16x8x2_t res =
225 {
226 vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1])),
227 vcombine_s16(vqmovn_s32(rf.val[2]), vqmovn_s32(rf.val[3])),
228 };
229
230 return res;
231}
232
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100233} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000234#endif // ARM_COMPUTE_NESYMM_H