blob: a221bd7925e1aa2ea49d638c6636ab8250b93898 [file] [log] [blame]
Gian Marcoe75a02b2017-11-08 12:24:09 +00001/*
Michalis Spyroua4f378d2019-04-26 14:54:54 +01002 * Copyright (c) 2017-2019 ARM Limited.
Gian Marcoe75a02b2017-11-08 12:24:09 +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 */
24#include "arm_compute/core/NEON/kernels/NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel.h"
25
Gian Marco6b77e912017-11-17 09:27:57 +000026#include "arm_compute/core/AccessWindowStatic.h"
Gian Marcoe75a02b2017-11-08 12:24:09 +000027#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/ITensor.h"
30#include "arm_compute/core/Types.h"
Gian Marco6b77e912017-11-17 09:27:57 +000031#include "arm_compute/core/Utils.h"
Gian Marcoe75a02b2017-11-08 12:24:09 +000032#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
34
35#include <arm_neon.h>
36#include <cstddef>
37#include <cstdint>
38
39using namespace arm_compute;
40
Gian Marco6b77e912017-11-17 09:27:57 +000041namespace
42{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000043Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000044{
45 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S32);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000046 ARM_COMPUTE_RETURN_ERROR_ON(max > 255);
47 ARM_COMPUTE_RETURN_ERROR_ON(min < 0 || min > max);
48
49 // Check biases if exist
50 if(bias != nullptr)
51 {
52 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias);
53 ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1);
54 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != bias->dimension(0));
55 }
Chunosov5124be52017-11-22 20:42:13 +070056
57 if(output->total_size() != 0)
58 {
59 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8);
60 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
61 }
62
Georgios Pinitas631c41a2017-12-06 11:53:03 +000063 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000064}
65
Georgios Pinitas631c41a2017-12-06 11:53:03 +000066std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *bias, ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000067{
Gian Marco7f0f7902017-12-07 09:26:56 +000068 // Note: This kernel performs 16 elements per iteration.
69 // However, since we use a left-over for loop, we cannot have any read or write out of memory
70 // For this reason num_elems_processed_per_iteration is set to 1
71 constexpr unsigned int num_elems_processed_per_iteration = 1;
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000072
73 // Configure kernel window
74 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
75
76 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000077
78 bool window_changed = update_window_and_padding(win,
Chunosov5124be52017-11-22 20:42:13 +070079 input_access);
80
81 if(output->total_size() != 0)
82 {
83 AccessWindowHorizontal output_result_access(output, 0, num_elems_processed_per_iteration);
84 window_changed = window_changed || update_window_and_padding(win, output_result_access);
85
86 output_result_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
87 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000088
89 if(bias != nullptr)
90 {
Gian Marco7f0f7902017-12-07 09:26:56 +000091 AccessWindowStatic bias_access(bias, 0, 0, bias->dimension(0), bias->dimension(1));
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000092 window_changed = window_changed || update_window_and_padding(win, bias_access);
93 }
94
Georgios Pinitas631c41a2017-12-06 11:53:03 +000095 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000096 return std::make_pair(err, win);
97}
98
Gian Marco6b77e912017-11-17 09:27:57 +000099inline void scale_input(int32x4x4_t &in_s32, int32x4_t result_offset_s32, int32_t result_mult_int)
100{
101 // Add the offset terms to GEMM's result
102 in_s32.val[0] = vaddq_s32(in_s32.val[0], result_offset_s32);
103 in_s32.val[1] = vaddq_s32(in_s32.val[1], result_offset_s32);
104 in_s32.val[2] = vaddq_s32(in_s32.val[2], result_offset_s32);
105 in_s32.val[3] = vaddq_s32(in_s32.val[3], result_offset_s32);
106
107 // Multiply by result_mult_int
108 in_s32.val[0] = vmulq_n_s32(in_s32.val[0], result_mult_int);
109 in_s32.val[1] = vmulq_n_s32(in_s32.val[1], result_mult_int);
110 in_s32.val[2] = vmulq_n_s32(in_s32.val[2], result_mult_int);
111 in_s32.val[3] = vmulq_n_s32(in_s32.val[3], result_mult_int);
112}
113
114template <bool is_bounded_relu>
115inline uint8x16_t finalize_quantization(int32x4x4_t &in_s32, int32x4_t result_shift_s32, uint8x16_t min_u8, uint8x16_t max_u8)
116{
117 const static int32x4_t zero_s32 = vdupq_n_s32(0);
118
119 // Shift final result (negative value shift right)
120 in_s32.val[0] = vshlq_s32(in_s32.val[0], result_shift_s32);
121 in_s32.val[1] = vshlq_s32(in_s32.val[1], result_shift_s32);
122 in_s32.val[2] = vshlq_s32(in_s32.val[2], result_shift_s32);
123 in_s32.val[3] = vshlq_s32(in_s32.val[3], result_shift_s32);
124
125 // Saturate negative values
126 in_s32.val[0] = vmaxq_s32(in_s32.val[0], zero_s32);
127 in_s32.val[1] = vmaxq_s32(in_s32.val[1], zero_s32);
128 in_s32.val[2] = vmaxq_s32(in_s32.val[2], zero_s32);
129 in_s32.val[3] = vmaxq_s32(in_s32.val[3], zero_s32);
130
131 // Convert S32 to S16
132 const int16x8x2_t in_s16 =
133 {
134 {
135 vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])),
136 vcombine_s16(vqmovn_s32(in_s32.val[2]), vqmovn_s32(in_s32.val[3]))
137 }
138 };
139
140 // Convert S16 to U8
141 uint8x16_t out_u8 = vcombine_u8(vqmovun_s16(in_s16.val[0]), vqmovun_s16(in_s16.val[1]));
142
143 if(is_bounded_relu)
144 {
145 out_u8 = vmaxq_u8(out_u8, min_u8);
146 out_u8 = vminq_u8(out_u8, max_u8);
147 }
148
149 return out_u8;
150}
151} // namespace
152
Gian Marcoe75a02b2017-11-08 12:24:09 +0000153namespace arm_compute
154{
155class Coordinates;
156} // namespace arm_compute
157
Gian Marco6b77e912017-11-17 09:27:57 +0000158template <bool is_bounded_relu>
159void NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::run(const Window &window)
160{
161 const int32x4_t result_offset_s32 = vdupq_n_s32(_result_offset);
162 const int32x4_t result_shift_s32 = vdupq_n_s32(-_result_shift);
163 const uint8x16_t min_u8 = vdupq_n_u8(static_cast<uint8_t>(_min));
164 const uint8x16_t max_u8 = vdupq_n_u8(static_cast<uint8_t>(_max));
165
166 ARM_COMPUTE_UNUSED(min_u8);
167 ARM_COMPUTE_UNUSED(max_u8);
168
Gian Marco7f0f7902017-12-07 09:26:56 +0000169 const int window_step_x = 16;
170 const auto window_start_x = static_cast<int>(window.x().start());
171 const auto window_end_x = static_cast<int>(window.x().end());
172
173 Window win(window);
174 win.set(Window::DimX, Window::Dimension(0, 1, 1));
175
176 Iterator in(_input, win);
177 Iterator out(_output, win);
Gian Marco6b77e912017-11-17 09:27:57 +0000178
179 if(_bias != nullptr)
180 {
181 Window win_biases;
Gian Marco7f0f7902017-12-07 09:26:56 +0000182 win_biases.set(Window::DimX, Window::Dimension(0, 1, 1));
Gian Marco6b77e912017-11-17 09:27:57 +0000183 win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
184
185 Iterator bias(_bias, win_biases);
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100186 execute_window_loop(win, [&](const Coordinates &)
Gian Marco6b77e912017-11-17 09:27:57 +0000187 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000188 // Compute 16 elements per iteration
189 int x = window_start_x;
190 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Gian Marco6b77e912017-11-17 09:27:57 +0000191 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000192 int32x4x4_t in_s32 =
Gian Marco6b77e912017-11-17 09:27:57 +0000193 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000194 {
195 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
196 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
197 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
198 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
199 }
200 };
Gian Marco6b77e912017-11-17 09:27:57 +0000201
Gian Marco7f0f7902017-12-07 09:26:56 +0000202 const int32x4x4_t bias_s32 =
203 {
204 {
205 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 0),
206 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 4),
207 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 8),
208 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 12)
209 }
210 };
211
212 // Add the bias to GEMM's result
213 in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]);
214 in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]);
215 in_s32.val[2] = vaddq_s32(in_s32.val[2], bias_s32.val[2]);
216 in_s32.val[3] = vaddq_s32(in_s32.val[3], bias_s32.val[3]);
217
218 // Add the offset terms to GEMM's result and multiply by result_mult_int
219 scale_input(in_s32, result_offset_s32, _result_mult_int);
220
221 vst1q_u8(out.ptr() + x, finalize_quantization<is_bounded_relu>(in_s32, result_shift_s32, min_u8, max_u8));
222 }
223
224 // Compute left-over elements
225 for(; x < window_end_x; ++x)
Gian Marco6b77e912017-11-17 09:27:57 +0000226 {
Anthony Barbierc2708ec2017-12-08 22:24:32 +0000227 const int bias_value = *(reinterpret_cast<const int *>(bias.ptr()) + x);
228 int in_value = *(reinterpret_cast<const int *>(in.ptr()) + x);
Gian Marco7f0f7902017-12-07 09:26:56 +0000229
230 // Quantize
231 in_value = ((in_value + bias_value + _result_offset) * _result_mult_int) >> _result_shift;
232
233 // Finalize and store the result
234 if(is_bounded_relu)
Gian Marco6b77e912017-11-17 09:27:57 +0000235 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000236 *(out.ptr() + x) = static_cast<uint8_t>(std::max(_min, std::min(_max, in_value)));
Gian Marco6b77e912017-11-17 09:27:57 +0000237 }
Gian Marco7f0f7902017-12-07 09:26:56 +0000238 else
239 {
240 *(out.ptr() + x) = static_cast<uint8_t>(std::max(0, std::min(255, in_value)));
241 }
242 }
Gian Marco6b77e912017-11-17 09:27:57 +0000243 },
244 in, bias, out);
245 }
246 else
247 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100248 execute_window_loop(win, [&](const Coordinates &)
Gian Marco6b77e912017-11-17 09:27:57 +0000249 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000250 // Compute 16 elements per iteration
251 int x = window_start_x;
252 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Gian Marco6b77e912017-11-17 09:27:57 +0000253 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000254 int32x4x4_t in_s32 =
Gian Marco6b77e912017-11-17 09:27:57 +0000255 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000256 {
257 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
258 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
259 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
260 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
261 }
262 };
263
264 // Add the offset terms to GEMM's result and multiply by result_mult_int
265 scale_input(in_s32, result_offset_s32, _result_mult_int);
266
267 vst1q_u8(out.ptr() + x, finalize_quantization<is_bounded_relu>(in_s32, result_shift_s32, min_u8, max_u8));
268 }
269
270 // Compute left-over elements
271 for(; x < window_end_x; ++x)
272 {
Anthony Barbierc2708ec2017-12-08 22:24:32 +0000273 int in_value = *(reinterpret_cast<const int *>(in.ptr()) + x);
Gian Marco7f0f7902017-12-07 09:26:56 +0000274
275 // Quantize
276 in_value = ((in_value + _result_offset) * _result_mult_int) >> _result_shift;
277
278 // Finalize and store the result
279 if(is_bounded_relu)
280 {
281 *(out.ptr() + x) = static_cast<uint8_t>(std::max(_min, std::min(_max, in_value)));
Gian Marco6b77e912017-11-17 09:27:57 +0000282 }
Gian Marco7f0f7902017-12-07 09:26:56 +0000283 else
284 {
285 *(out.ptr() + x) = static_cast<uint8_t>(std::max(0, std::min(255, in_value)));
286 }
287 }
Gian Marco6b77e912017-11-17 09:27:57 +0000288 },
289 in, out);
290 }
291}
292
Gian Marcoe75a02b2017-11-08 12:24:09 +0000293NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel()
Gian Marco6b77e912017-11-17 09:27:57 +0000294 : _func(nullptr), _input(nullptr), _bias(nullptr), _output(nullptr), _result_offset(0), _result_mult_int(0), _result_shift(0), _min(0), _max(0)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000295{
296}
297
Gian Marco6b77e912017-11-17 09:27:57 +0000298void NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_offset, int result_mult_int, int result_shift, int min, int max)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000299{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000300 // Perform validate step
301 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Gian Marco58c57942017-11-28 09:10:03 +0000302
303 // Output auto inizialitation if not yet initialized
304 auto_init_if_empty(*output->info(), input->info()->clone()->set_data_type(DataType::QASYMM8));
305
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000306 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(),
307 (bias != nullptr) ? bias->info() : nullptr,
308 output->info(),
309 min,
310 max));
Gian Marcoe75a02b2017-11-08 12:24:09 +0000311
312 _input = input;
Gian Marco6b77e912017-11-17 09:27:57 +0000313 _bias = bias;
Gian Marcoe75a02b2017-11-08 12:24:09 +0000314 _output = output;
315 _result_offset = result_offset;
316 _result_mult_int = result_mult_int;
317 _result_shift = result_shift;
Gian Marco6b77e912017-11-17 09:27:57 +0000318 _min = min;
319 _max = max;
Gian Marcoe75a02b2017-11-08 12:24:09 +0000320
Gian Marcoe75a02b2017-11-08 12:24:09 +0000321 // Configure kernel window
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000322 auto win_config = validate_and_configure_window(input->info(), (bias != nullptr) ? bias->info() : nullptr, output->info());
323 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
324 INEKernel::configure(win_config.second);
Gian Marco6b77e912017-11-17 09:27:57 +0000325
326 // Check if we need to clamp the result using min and max
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000327 const bool is_bounded_relu = ((min != max) && !(min == 0 && max == 255));
328 _func = is_bounded_relu ? &NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::run<true> : &NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::run<false>;
329}
330
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000331Status NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000332{
Chunosov5124be52017-11-22 20:42:13 +0700333 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000334 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, min, max));
335 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(),
336 (bias != nullptr) ? bias->clone().get() : nullptr,
337 output->clone().get())
338 .first);
339
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000340 return Status{};
Gian Marcoe75a02b2017-11-08 12:24:09 +0000341}
342
343void NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::run(const Window &window, const ThreadInfo &info)
344{
345 ARM_COMPUTE_UNUSED(info);
346 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
347 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
348
Gian Marco6b77e912017-11-17 09:27:57 +0000349 (this->*_func)(window);
Anthony Barbierc2708ec2017-12-08 22:24:32 +0000350}