blob: 3023d93113bef35b279987d15567f116c901a9c3 [file] [log] [blame]
Luca Foschiani4b869532020-02-13 15:07:36 +00001/*
SiCongLib88272e2021-02-24 15:40:57 +00002 * Copyright (c) 2020-2021 Arm Limited.
Luca Foschiani4b869532020-02-13 15:07:36 +00003 *
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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/kernels/CpuGemmLowpQuantizeDownInt32ScaleKernel.h"
Luca Foschiani4b869532020-02-13 15:07:36 +000025
Luca Foschiani4b869532020-02-13 15:07:36 +000026#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Luca Foschiani4b869532020-02-13 15:07:36 +000029#include "arm_compute/core/Types.h"
30#include "arm_compute/core/Utils.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/core/Window.h"
33#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/AccessWindowStatic.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010035#include "src/core/NEON/wrapper/wrapper.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010036#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
Luca Foschiani4b869532020-02-13 15:07:36 +000038
39#include <arm_neon.h>
Luca Foschiani4b869532020-02-13 15:07:36 +000040
41namespace arm_compute
42{
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010043namespace cpu
Luca Foschiani4b869532020-02-13 15:07:36 +000044{
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010045namespace kernels
46{
47namespace
48{
49Status validate_arguments(const ITensorInfo *src, const ITensorInfo *bias, const ITensorInfo *dst, const GEMMLowpOutputStageInfo *output_stage)
50{
51 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
52 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::S32);
Luca Foschiani4b869532020-02-13 15:07:36 +000053
54 ARM_COMPUTE_RETURN_ERROR_ON(output_stage->gemmlowp_max_bound > std::get<1>(quantization::get_min_max_values_from_quantized_data_type(output_stage->output_data_type)));
55 ARM_COMPUTE_RETURN_ERROR_ON(output_stage->gemmlowp_min_bound < std::get<0>(quantization::get_min_max_values_from_quantized_data_type(output_stage->output_data_type))
56 || output_stage->gemmlowp_min_bound > output_stage->gemmlowp_max_bound);
57
58 // Check biases if exist
59 if(bias != nullptr)
60 {
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010061 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, bias);
Luca Foschiani4b869532020-02-13 15:07:36 +000062 ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1);
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010063 ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(0) != bias->dimension(0));
Luca Foschiani4b869532020-02-13 15:07:36 +000064 }
65
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010066 if(dst->total_size() != 0)
Luca Foschiani4b869532020-02-13 15:07:36 +000067 {
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010068 if(dst->data_type() != output_stage->output_data_type && (output_stage->output_data_type == DataType::QASYMM8 || output_stage->output_data_type == DataType::QASYMM8_SIGNED))
Luca Foschiani4b869532020-02-13 15:07:36 +000069 {
70 ARM_COMPUTE_RETURN_ERROR_MSG("Mismatching data types");
71 }
72
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010073 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst);
Luca Foschiani4b869532020-02-13 15:07:36 +000074 }
75
76 return Status{};
77}
78
79inline void scale_input(int32x4x4_t &in_s32, int32x4_t result_offset_s32, int32_t result_mult_int)
80{
81 // Add the offset terms to GEMM's result
82 in_s32.val[0] = vaddq_s32(in_s32.val[0], result_offset_s32);
83 in_s32.val[1] = vaddq_s32(in_s32.val[1], result_offset_s32);
84 in_s32.val[2] = vaddq_s32(in_s32.val[2], result_offset_s32);
85 in_s32.val[3] = vaddq_s32(in_s32.val[3], result_offset_s32);
86
87 // Multiply by result_mult_int
88 in_s32.val[0] = vmulq_n_s32(in_s32.val[0], result_mult_int);
89 in_s32.val[1] = vmulq_n_s32(in_s32.val[1], result_mult_int);
90 in_s32.val[2] = vmulq_n_s32(in_s32.val[2], result_mult_int);
91 in_s32.val[3] = vmulq_n_s32(in_s32.val[3], result_mult_int);
92}
93
94template <typename T>
95inline typename std::enable_if<std::is_same<T, uint8_t>::value,
96 typename wrapper::traits::neon_vector<T, 16>::type>::type
97 convert_to_8bit(const int16x8x2_t in_s16)
98{
99 return wrapper::vcombine(wrapper::vqmovun(in_s16.val[0]), wrapper::vqmovun(in_s16.val[1]));
100}
101
102template <typename T>
103inline typename std::enable_if<std::is_same<T, int8_t>::value,
104 typename wrapper::traits::neon_vector<T, 16>::type>::type
105 convert_to_8bit(const int16x8x2_t in_s16)
106{
107 return wrapper::vcombine(wrapper::vqmovn(in_s16.val[0]), wrapper::vqmovn(in_s16.val[1]));
108}
109
110template <typename T>
111inline typename wrapper::traits::neon_vector<T, 16>::type finalize_quantization(int32x4x4_t &in_s32, int32x4_t result_shift_s32, typename wrapper::traits::neon_vector<T, 16>::type min,
112 typename wrapper::traits::neon_vector<T, 16>::type max)
113{
114 // Shift final result (negative value shift right)
115 in_s32.val[0] = vshlq_s32(in_s32.val[0], result_shift_s32);
116 in_s32.val[1] = vshlq_s32(in_s32.val[1], result_shift_s32);
117 in_s32.val[2] = vshlq_s32(in_s32.val[2], result_shift_s32);
118 in_s32.val[3] = vshlq_s32(in_s32.val[3], result_shift_s32);
119
120 // Convert S32 to S16
121 const int16x8x2_t in_s16 =
122 {
123 {
124 vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])),
125 vcombine_s16(vqmovn_s32(in_s32.val[2]), vqmovn_s32(in_s32.val[3]))
126 }
127 };
128
129 // Convert S16 to S8 or U8
130 typename wrapper::traits::neon_vector<T, 16>::type out = convert_to_8bit<T>(in_s16);
131
132 out = wrapper::vmax(out, min);
133 out = wrapper::vmin(out, max);
134
135 return out;
136}
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100137} // namespace
Luca Foschiani4b869532020-02-13 15:07:36 +0000138
139template <typename T>
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100140void CpuGemmLowpQuantizeDownInt32ScaleKernel::run_internal(const ITensor *src, const ITensor *bias, ITensor *dst, const Window &window)
Luca Foschiani4b869532020-02-13 15:07:36 +0000141{
142 using VectorType = typename wrapper::traits::neon_vector<T, 16>::type;
143
144 const int32x4_t result_offset_s32 = vdupq_n_s32(_output_stage->gemmlowp_offset);
145 const int32x4_t result_shift_s32 = vdupq_n_s32(-_output_stage->gemmlowp_shift);
146 const int window_step_x = 16;
147 const auto window_start_x = static_cast<int>(window.x().start());
148 const auto window_end_x = static_cast<int>(window.x().end());
149
150 const int clamp_min = (_is_bounded_relu) ? _output_stage->gemmlowp_min_bound : std::numeric_limits<T>::lowest();
151 const int clamp_max = (_is_bounded_relu) ? _output_stage->gemmlowp_max_bound : std::numeric_limits<T>::max();
152
153 VectorType min = wrapper::vdup_n(static_cast<T>(clamp_min), wrapper::traits::vector_128_tag{});
154 VectorType max = wrapper::vdup_n(static_cast<T>(clamp_max), wrapper::traits::vector_128_tag{});
155
156 Window win(window);
157 win.set(Window::DimX, Window::Dimension(0, 1, 1));
158
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100159 Iterator in(src, win);
160 Iterator out(dst, win);
Luca Foschiani4b869532020-02-13 15:07:36 +0000161
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100162 if(bias != nullptr)
Luca Foschiani4b869532020-02-13 15:07:36 +0000163 {
164 Window win_biases;
165 win_biases.set(Window::DimX, Window::Dimension(0, 1, 1));
166 win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
167
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100168 Iterator bias_i(bias, win_biases);
Luca Foschiani4b869532020-02-13 15:07:36 +0000169 execute_window_loop(win, [&](const Coordinates &)
170 {
171 // Compute 16 elements per iteration
172 int x = window_start_x;
173 for(; x <= (window_end_x - window_step_x); x += window_step_x)
174 {
175 int32x4x4_t in_s32 =
176 {
177 {
178 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
179 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
180 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
181 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
182 }
183 };
184
185 const int32x4x4_t bias_s32 =
186 {
187 {
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100188 vld1q_s32(reinterpret_cast<const int32_t *>(bias_i.ptr()) + x + 0),
189 vld1q_s32(reinterpret_cast<const int32_t *>(bias_i.ptr()) + x + 4),
190 vld1q_s32(reinterpret_cast<const int32_t *>(bias_i.ptr()) + x + 8),
191 vld1q_s32(reinterpret_cast<const int32_t *>(bias_i.ptr()) + x + 12)
Luca Foschiani4b869532020-02-13 15:07:36 +0000192 }
193 };
194
195 // Add the bias to GEMM's result
196 in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]);
197 in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]);
198 in_s32.val[2] = vaddq_s32(in_s32.val[2], bias_s32.val[2]);
199 in_s32.val[3] = vaddq_s32(in_s32.val[3], bias_s32.val[3]);
200
201 // Add the offset terms to GEMM's result and multiply by result_mult_int
202 scale_input(in_s32, result_offset_s32, _output_stage->gemmlowp_multiplier);
203
204 wrapper::vstore(reinterpret_cast<T *>(out.ptr() + x), finalize_quantization<T>(in_s32, result_shift_s32, min, max));
205 }
206
207 // Compute left-over elements
208 for(; x < window_end_x; ++x)
209 {
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100210 const int bias_value = *(reinterpret_cast<const int *>(bias_i.ptr()) + x);
Luca Foschiani4b869532020-02-13 15:07:36 +0000211 int in_value = *(reinterpret_cast<const int *>(in.ptr()) + x);
212
213 // Quantize
214 in_value = ((in_value + bias_value + _output_stage->gemmlowp_offset) * _output_stage->gemmlowp_multiplier) >> _output_stage->gemmlowp_shift;
215
216 // Store the result
217 *(out.ptr() + x) = static_cast<T>(utility::clamp<int>(in_value, clamp_min, clamp_max));
218 }
219 },
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100220 in, bias_i, out);
Luca Foschiani4b869532020-02-13 15:07:36 +0000221 }
222 else
223 {
224 execute_window_loop(win, [&](const Coordinates &)
225 {
226 // Compute 16 elements per iteration
227 int x = window_start_x;
228 for(; x <= (window_end_x - window_step_x); x += window_step_x)
229 {
230 int32x4x4_t in_s32 =
231 {
232 {
233 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
234 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
235 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
236 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
237 }
238 };
239
240 // Add the offset terms to GEMM's result and multiply by result_mult_int
241 scale_input(in_s32, result_offset_s32, _output_stage->gemmlowp_multiplier);
242
243 wrapper::vstore(reinterpret_cast<T *>(out.ptr() + x), finalize_quantization<T>(in_s32, result_shift_s32, min, max));
244 }
245
246 // Compute left-over elements
247 for(; x < window_end_x; ++x)
248 {
249 int in_value = *(reinterpret_cast<const int *>(in.ptr()) + x);
250
251 // Quantize
252 in_value = ((in_value + _output_stage->gemmlowp_offset) * _output_stage->gemmlowp_multiplier) >> _output_stage->gemmlowp_shift;
253
254 // Store the result
255 *(out.ptr() + x) = static_cast<T>(utility::clamp<int>(in_value, clamp_min, clamp_max));
256 }
257 },
258 in, out);
259 }
260}
261
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100262void CpuGemmLowpQuantizeDownInt32ScaleKernel::configure(ITensorInfo *src, ITensorInfo *bias, ITensorInfo *dst, const GEMMLowpOutputStageInfo *output_stage)
Luca Foschiani4b869532020-02-13 15:07:36 +0000263{
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100264 ARM_COMPUTE_UNUSED(bias);
Luca Foschiani4b869532020-02-13 15:07:36 +0000265 // Perform validate step
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100266 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst, output_stage);
Luca Foschiani4b869532020-02-13 15:07:36 +0000267
268 // Output auto inizialitation if not yet initialized
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100269 auto_init_if_empty(*dst, src->clone()->set_data_type(output_stage->output_data_type));
Luca Foschiani4b869532020-02-13 15:07:36 +0000270
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100271 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src,
272 bias,
273 dst,
Luca Foschiani4b869532020-02-13 15:07:36 +0000274 output_stage));
275
Luca Foschiani4b869532020-02-13 15:07:36 +0000276 _output_stage = output_stage;
277
278 // Configure kernel window
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100279 Window win = calculate_max_window(*src, Steps());
Luca Foschiani4b869532020-02-13 15:07:36 +0000280
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100281 ICpuKernel::configure(win);
Luca Foschiani4b869532020-02-13 15:07:36 +0000282
283 // Check if we need to clamp the result using min and max
284 _is_bounded_relu = ((_output_stage->gemmlowp_min_bound != _output_stage->gemmlowp_max_bound)
285 && !(_output_stage->gemmlowp_min_bound == std::get<0>(quantization::get_min_max_values_from_quantized_data_type(output_stage->output_data_type))
286 && _output_stage->gemmlowp_max_bound == std::get<1>(quantization::get_min_max_values_from_quantized_data_type(output_stage->output_data_type))));
287 if(_output_stage->output_data_type == DataType::QASYMM8)
288 {
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100289 _func = &CpuGemmLowpQuantizeDownInt32ScaleKernel::run_internal<uint8_t>;
Luca Foschiani4b869532020-02-13 15:07:36 +0000290 }
291 else if(_output_stage->output_data_type == DataType::QASYMM8_SIGNED)
292 {
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100293 _func = &CpuGemmLowpQuantizeDownInt32ScaleKernel::run_internal<int8_t>;
Luca Foschiani4b869532020-02-13 15:07:36 +0000294 }
295 else
296 {
297 ARM_COMPUTE_ERROR("Data type not supported");
298 }
299}
300
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100301Status CpuGemmLowpQuantizeDownInt32ScaleKernel::validate(const ITensorInfo *src, const ITensorInfo *bias, const ITensorInfo *dst, const GEMMLowpOutputStageInfo *output_stage)
Luca Foschiani4b869532020-02-13 15:07:36 +0000302{
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100303 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, bias, dst, output_stage));
Luca Foschiani4b869532020-02-13 15:07:36 +0000304 return Status{};
305}
306
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100307void CpuGemmLowpQuantizeDownInt32ScaleKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Luca Foschiani4b869532020-02-13 15:07:36 +0000308{
309 ARM_COMPUTE_UNUSED(info);
310 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100311 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
312 ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No inputs provided");
Luca Foschiani4b869532020-02-13 15:07:36 +0000313
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100314 auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
315 auto bias = tensors.get_const_tensor(TensorType::ACL_BIAS);
316 auto dst = tensors.get_tensor(TensorType::ACL_DST);
317 (this->*_func)(src, bias, dst, window);
Luca Foschiani4b869532020-02-13 15:07:36 +0000318}
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100319
320const char *CpuGemmLowpQuantizeDownInt32ScaleKernel::name() const
321{
322 return "CpuGemmLowpQuantizeDownInt32ScaleKernel";
323}
324} // namespace kernels
325} // namespace cpu
Luca Foschiani4b869532020-02-13 15:07:36 +0000326} // namespace arm_compute