blob: a60d5d0fde96f3bb99d0044a7b1dabc094b5dd07 [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 */
24#ifndef __ARM_COMPUTE_NESYMM_H__
25#define __ARM_COMPUTE_NESYMM_H__
26
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{
57 // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar
58 in_s32.val[0] = vqrdmulhq_n_s32(in_s32.val[0], result_fixedpoint_multiplier);
59 in_s32.val[1] = vqrdmulhq_n_s32(in_s32.val[1], result_fixedpoint_multiplier);
60
61 // Round to the nearest division by a power-of-two using result_shift_s32
62 in_s32.val[0] = rounding_divide_by_pow2(in_s32.val[0], result_shift);
63 in_s32.val[1] = rounding_divide_by_pow2(in_s32.val[1], result_shift);
64
65 // Convert S32 to S16
66 int16x8_t out_s16 = vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1]));
67
68 if(is_bounded_relu)
69 {
70 out_s16 = vmaxq_s16(out_s16, min_s16);
71 out_s16 = vminq_s16(out_s16, max_s16);
72 }
73
74 return out_s16;
75}
76
77/** Performs final quantization step on single signed 16-bit element
78 *
79 * @tparam is_bounded_relu Specified if a fused bounded relu should be applied
80 *
81 * @param[in] in_value Input to be quantized.
82 * @param[in] result_fixedpoint_multiplier Result multiplier parameter
83 * @param[in] result_shift Result shift parameter
84 * @param[in] min_s16 Relu lower bound
85 * @param[in] max_s16 Relu upper bound
86 *
87 * @return Quantized values
88 */
89template <bool is_bounded_relu>
90inline int16_t finalize_quantization_int16(int32_t in_value, int result_fixedpoint_multiplier,
91 int32_t result_shift, int16_t min_s16, int16_t max_s16)
92{
93 int32x4_t in_s32 = vdupq_n_s32(in_value);
94
95 // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar
96 in_value = vgetq_lane_s32(vqrdmulhq_n_s32(in_s32, result_fixedpoint_multiplier), 0);
97
98 // Shift value by result_shift_s32
99 in_value = rounding_divide_by_pow2(in_value, result_shift);
100
101 // Bound the result
102 int16_t out_s16 = static_cast<int16_t>(std::max<int32_t>(-32768, std::min<int32_t>(32767, in_value)));
103
104 if(is_bounded_relu)
105 {
106 out_s16 = static_cast<int16_t>(std::max(min_s16, std::min(max_s16, out_s16)));
107 }
108
109 return out_s16;
110}
giuros01c9573f32019-06-20 10:30:17 +0100111
112/** Dequantize a neon vector holding 8 16-bit quantized values.
113 *
114 * @param[in] qv Input values to be dequantized.
115 * @param[in] scale Quantization scale
116 *
117 * @return Dequantized values in a neon vector
118 */
119inline float32x4x2_t vdequantize_int16(const int16x8_t &qv, float scale)
120{
121 const float32x4_t vscale = vdupq_n_f32(scale);
122 const float32x4x2_t vdequantized_input =
123 {
124 {
125 vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(qv))), vscale),
126 vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(qv))), vscale)
127 }
128 };
129 return vdequantized_input;
130}
131
132/** Quantize a neon vector holding 8 floating point values.
133 *
134 * @param[in] qv Input values to be quantized.
135 * @param[in] scale Quantization scale
136 *
137 * @return A neon vector holding the quantized values
138 */
139inline int16x8_t vquantize_int16(const float32x4x2_t &qv, float scale)
140{
141 const float32x4_t vinvscale = vdupq_n_f32(1.f / scale);
142
143 const int32x4x2_t rf =
144 {
145 {
146#ifdef __aarch64__
147 vcvtnq_s32_f32(vmulq_f32(qv.val[0], vinvscale)),
148 vcvtnq_s32_f32(vmulq_f32(qv.val[1], vinvscale))
149#else //__aarch64__
150 vcvtq_s32_f32(vmulq_f32(qv.val[0], vinvscale)),
151 vcvtq_s32_f32(vmulq_f32(qv.val[1], vinvscale))
152#endif //__aarch64__
153 }
154 };
155 return vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1]));
156}
157
Manuel Bottini7bb56c62019-06-26 15:17:09 +0100158/** Dequantize a neon vector holding 16 16-bit quantized values.
159 *
160 * @param[in] qv Input values to be dequantized.
161 * @param[in] qi Quantization information to be used in the computation.
162 *
163 * @return Dequantized values in a neon vector
164 */
165inline float32x4x4_t vdequantize(const int16x8x2_t &qv, const UniformQuantizationInfo &qi)
166{
167 const float scale = qi.scale;
168 const float32x4_t vscale = vdupq_n_f32(scale);
169 const float32x4x4_t vdequantized_input =
170 {
171 {
172 vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(qv.val[0]))), vscale),
173 vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(qv.val[0]))), vscale),
174 vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(qv.val[1]))), vscale),
175 vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(qv.val[1]))), vscale),
176 }
177 };
178 return vdequantized_input;
179}
180
181/** Quantize a neon vector holding 16 floating point values.
182 *
183 * @param[in] qv Input values to be quantized.
184 * @param[in] qi Quantization information to be used in the computation.
185 *
186 * @return A neon vector holding the quantized values
187 */
188inline qsymm16x8x2_t vquantize_qsymm16(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
189{
190 const float scale = qi.scale;
191 ARM_COMPUTE_ERROR_ON(scale == 0.f);
192 const float32x4_t vinvscale = vdupq_n_f32(1.f / scale);
193 const int32x4x4_t rf =
194 {
195 {
196#ifdef __aarch64__
197 vcvtnq_s32_f32(vmulq_f32(qv.val[0], vinvscale)),
198 vcvtnq_s32_f32(vmulq_f32(qv.val[1], vinvscale)),
199 vcvtnq_s32_f32(vmulq_f32(qv.val[2], vinvscale)),
200 vcvtnq_s32_f32(vmulq_f32(qv.val[3], vinvscale)),
201#else //__aarch64__
202 vcvtq_s32_f32(vmulq_f32(qv.val[0], vinvscale)),
203 vcvtq_s32_f32(vmulq_f32(qv.val[1], vinvscale)),
204 vcvtq_s32_f32(vmulq_f32(qv.val[2], vinvscale)),
205 vcvtq_s32_f32(vmulq_f32(qv.val[3], vinvscale)),
206#endif //__aarch64__
207 }
208 };
209 const qsymm16x8x2_t res =
210 {
211 vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1])),
212 vcombine_s16(vqmovn_s32(rf.val[2]), vqmovn_s32(rf.val[3])),
213 };
214
215 return res;
216}
217
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100218} // namespace arm_compute
219#endif // __ARM_COMPUTE_NESYMM_H__