blob: 7cd50cc5a0efe5690ffb1db80f280cc6421277ea [file] [log] [blame]
Gian Marco58c57942017-11-28 09:10:03 +00001/*
Georgios Pinitasf72f9362018-01-12 16:29:45 +00002 * Copyright (c) 2017-2018 ARM Limited.
Gian Marco58c57942017-11-28 09:10:03 +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/NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#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/NEON/NEAsymm.h"
Georgios Pinitas932491f2018-09-21 16:33:15 +010031#include "arm_compute/core/TensorInfo.h"
Gian Marco58c57942017-11-28 09:10:03 +000032#include "arm_compute/core/Types.h"
33#include "arm_compute/core/Utils.h"
34#include "arm_compute/core/Validate.h"
35#include "arm_compute/core/Window.h"
Georgios Pinitas041f36d2018-09-18 18:38:37 +010036#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Gian Marco58c57942017-11-28 09:10:03 +000037
38#include <arm_neon.h>
39#include <cstddef>
40#include <cstdint>
41
42using namespace arm_compute;
43
44namespace
45{
Georgios Pinitas041f36d2018-09-18 18:38:37 +010046Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output,
Georgios Pinitas932491f2018-09-21 16:33:15 +010047 int min, int max, unsigned int output_3d_depth)
Gian Marco58c57942017-11-28 09:10:03 +000048{
49 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S32);
Gian Marco58c57942017-11-28 09:10:03 +000050 ARM_COMPUTE_RETURN_ERROR_ON(max > 255);
51 ARM_COMPUTE_RETURN_ERROR_ON(min < 0 || min > max);
52
53 // Check biases if exist
54 if(bias != nullptr)
55 {
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias);
57 ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1);
58 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != bias->dimension(0));
59 }
Chunosov5124be52017-11-22 20:42:13 +070060
61 if(output->total_size() != 0)
62 {
Georgios Pinitas932491f2018-09-21 16:33:15 +010063 const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_output_stage_shape(*input, output_3d_depth);
64 const TensorInfo tensor_info_output = output->clone()->set_tensor_shape(output_shape);
Chunosov5124be52017-11-22 20:42:13 +070065 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8);
Georgios Pinitas932491f2018-09-21 16:33:15 +010066 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
Chunosov5124be52017-11-22 20:42:13 +070067 }
68
Georgios Pinitas631c41a2017-12-06 11:53:03 +000069 return Status{};
Gian Marco58c57942017-11-28 09:10:03 +000070}
71
Georgios Pinitas631c41a2017-12-06 11:53:03 +000072std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *bias, ITensorInfo *output)
Gian Marco58c57942017-11-28 09:10:03 +000073{
Gian Marco7f0f7902017-12-07 09:26:56 +000074 // Note: This kernel performs 16 elements per iteration.
75 // However, since we use a left-over for loop, we cannot have any read or write out of memory
76 // For this reason num_elems_processed_per_iteration is set to 1
77 constexpr unsigned int num_elems_processed_per_iteration = 1;
Gian Marco58c57942017-11-28 09:10:03 +000078
79 // Configure kernel window
Georgios Pinitas041f36d2018-09-18 18:38:37 +010080 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
Gian Marco58c57942017-11-28 09:10:03 +000081
82 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
Gian Marco58c57942017-11-28 09:10:03 +000083
84 bool window_changed = update_window_and_padding(win,
Chunosov5124be52017-11-22 20:42:13 +070085 input_access);
86
87 if(output->total_size() != 0)
88 {
Georgios Pinitas041f36d2018-09-18 18:38:37 +010089 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
Chunosov5124be52017-11-22 20:42:13 +070090 }
Gian Marco58c57942017-11-28 09:10:03 +000091
92 if(bias != nullptr)
93 {
Gian Marco7f0f7902017-12-07 09:26:56 +000094 AccessWindowStatic bias_access(bias, 0, 0, bias->dimension(0), bias->dimension(1));
Gian Marco58c57942017-11-28 09:10:03 +000095 window_changed = window_changed || update_window_and_padding(win, bias_access);
96 }
97
Georgios Pinitas631c41a2017-12-06 11:53:03 +000098 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Gian Marco58c57942017-11-28 09:10:03 +000099 return std::make_pair(err, win);
100}
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000101} // namespace
Gian Marco58c57942017-11-28 09:10:03 +0000102
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000103namespace arm_compute
Gian Marco58c57942017-11-28 09:10:03 +0000104{
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000105class Coordinates;
Gian Marco7f0f7902017-12-07 09:26:56 +0000106
107/* Function used by the left-over for loop to perform the quantization */
108template <bool is_bounded_relu>
109inline uint8_t finalize_quantization(int32x4_t in_s32, int result_fixedpoint_multiplier, int32_t result_shift, int32x4_t result_offset_after_shift_s32, uint8_t min_u8, uint8_t max_u8)
110{
111 const static int32x4_t zero_s32 = vdupq_n_s32(0);
112 const static int32x4_t sat_value_s32 = vdupq_n_s32(255);
113
114 // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar
115 in_s32 = vqrdmulhq_n_s32(in_s32, result_fixedpoint_multiplier);
116
117 // Round to the nearest division by a power-of-two using result_shift_s32
118 in_s32 = rounding_divide_by_pow2(in_s32, result_shift);
119
120 // Add the offset terms
121 in_s32 = vaddq_s32(in_s32, result_offset_after_shift_s32);
122
123 // Saturate negative values
124 in_s32 = vmaxq_s32(in_s32, zero_s32);
125 in_s32 = vminq_s32(in_s32, sat_value_s32);
126
127 auto out_u8 = static_cast<uint8_t>(vgetq_lane_s32(in_s32, 0));
128
129 if(is_bounded_relu)
130 {
131 out_u8 = std::max(out_u8, min_u8);
132 out_u8 = std::min(out_u8, max_u8);
133 }
134
135 return out_u8;
136}
Gian Marco58c57942017-11-28 09:10:03 +0000137} // namespace arm_compute
138
139template <bool is_bounded_relu>
140void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run(const Window &window)
141{
142 const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(_result_offset_after_shift);
143 const uint8x16_t min_u8 = vdupq_n_u8(static_cast<uint8_t>(_min));
144 const uint8x16_t max_u8 = vdupq_n_u8(static_cast<uint8_t>(_max));
145
146 ARM_COMPUTE_UNUSED(min_u8);
147 ARM_COMPUTE_UNUSED(max_u8);
148
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100149 const int window_step_x = 16;
150 const auto window_start_x = static_cast<int>(window.x().start());
151 const auto window_end_x = static_cast<int>(window.x().end());
Georgios Pinitas932491f2018-09-21 16:33:15 +0100152 const unsigned int gemm_3d_height = _input->info()->tensor_shape().y() / _output_3d_depth;
Gian Marco7f0f7902017-12-07 09:26:56 +0000153
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100154 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
155 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
Gian Marco7f0f7902017-12-07 09:26:56 +0000156
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100157 Iterator in(_input, win_collapsed);
Gian Marco58c57942017-11-28 09:10:03 +0000158 if(_bias != nullptr)
159 {
160 Window win_biases;
Gian Marco7f0f7902017-12-07 09:26:56 +0000161 win_biases.set(Window::DimX, Window::Dimension(0, 1, 1));
Gian Marco58c57942017-11-28 09:10:03 +0000162 win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
163
164 Iterator bias(_bias, win_biases);
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100165 execute_window_loop(win_collapsed, [&](const Coordinates & id)
Gian Marco58c57942017-11-28 09:10:03 +0000166 {
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100167 // Calculate output coordinates
168 Coordinates out_coords = id;
Georgios Pinitas932491f2018-09-21 16:33:15 +0100169 if(_output_3d_depth != 1)
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100170 {
171 out_coords.set(Window::DimY, id.y() % gemm_3d_height);
172 out_coords.set(Window::DimZ, id.y() / gemm_3d_height);
173 out_coords.set(3, id.z());
174 }
175 uint8_t *out_ptr = _output->ptr_to_element(out_coords);
176
Gian Marco7f0f7902017-12-07 09:26:56 +0000177 // Compute 16 elements per iteration
178 int x = window_start_x;
179 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Gian Marco58c57942017-11-28 09:10:03 +0000180 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000181 int32x4x4_t in_s32 =
Gian Marco58c57942017-11-28 09:10:03 +0000182 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000183 {
184 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
185 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
186 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
187 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
188 }
189 };
Gian Marco58c57942017-11-28 09:10:03 +0000190
Gian Marco7f0f7902017-12-07 09:26:56 +0000191 const int32x4x4_t bias_s32 =
192 {
193 {
194 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 0),
195 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 4),
196 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 8),
197 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 12)
198 }
199 };
200
201 // Add the bias to GEMM's result
202 in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]);
203 in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]);
204 in_s32.val[2] = vaddq_s32(in_s32.val[2], bias_s32.val[2]);
205 in_s32.val[3] = vaddq_s32(in_s32.val[3], bias_s32.val[3]);
206
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100207 vst1q_u8(out_ptr + x, finalize_quantization<is_bounded_relu>(in_s32, _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, min_u8, max_u8));
Gian Marco7f0f7902017-12-07 09:26:56 +0000208 }
209
210 // Compute left-over elements
211 for(; x < window_end_x; ++x)
Gian Marco58c57942017-11-28 09:10:03 +0000212 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000213 const int32_t bias_value = *(reinterpret_cast<const int32_t *>(bias.ptr()) + x);
214 int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
Gian Marco58c57942017-11-28 09:10:03 +0000215
Gian Marco7f0f7902017-12-07 09:26:56 +0000216 // Add bias
217 in_value += bias_value;
Gian Marco58c57942017-11-28 09:10:03 +0000218
Gian Marco7f0f7902017-12-07 09:26:56 +0000219 // Finalize and store the result
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100220 *(out_ptr + x) = finalize_quantization<is_bounded_relu>(vdupq_n_s32(in_value), _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, static_cast<uint8_t>(_min),
221 static_cast<uint8_t>(_max));
Gian Marco7f0f7902017-12-07 09:26:56 +0000222 }
Gian Marco58c57942017-11-28 09:10:03 +0000223 },
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100224 in, bias);
Gian Marco58c57942017-11-28 09:10:03 +0000225 }
226 else
227 {
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100228 execute_window_loop(win_collapsed, [&](const Coordinates & id)
Gian Marco58c57942017-11-28 09:10:03 +0000229 {
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100230 // Calculate output coordinates
231 Coordinates out_coords = id;
Georgios Pinitas932491f2018-09-21 16:33:15 +0100232 if(_output_3d_depth != 1)
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100233 {
Georgios Pinitas932491f2018-09-21 16:33:15 +0100234 out_coords.set(Window::DimY, id.y() % _output_3d_depth);
235 out_coords.set(Window::DimZ, id.y() / _output_3d_depth);
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100236 out_coords.set(3, id.z());
237 }
238 uint8_t *out_ptr = _output->ptr_to_element(out_coords);
239
Gian Marco7f0f7902017-12-07 09:26:56 +0000240 // Compute 16 elements per iteration
241 int x = window_start_x;
242 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Gian Marco58c57942017-11-28 09:10:03 +0000243 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000244 int32x4x4_t in_s32 =
Gian Marco58c57942017-11-28 09:10:03 +0000245 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000246 {
247 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
248 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
249 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
250 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
251 }
252 };
Gian Marco58c57942017-11-28 09:10:03 +0000253
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100254 vst1q_u8(out_ptr + x, finalize_quantization<is_bounded_relu>(in_s32, _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, min_u8, max_u8));
Gian Marco7f0f7902017-12-07 09:26:56 +0000255 }
256
257 // Compute left-over elements
258 for(; x < window_end_x; ++x)
259 {
260 const int32x4_t in_s32 = vld1q_dup_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x);
261
262 // Finalize and store the result
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100263 *(out_ptr + x) = finalize_quantization<is_bounded_relu>(in_s32, _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, static_cast<uint8_t>(_min), static_cast<uint8_t>(_max));
Gian Marco7f0f7902017-12-07 09:26:56 +0000264 }
Gian Marco58c57942017-11-28 09:10:03 +0000265 },
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100266 in);
Gian Marco58c57942017-11-28 09:10:03 +0000267 }
268}
269
270NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel()
Georgios Pinitas932491f2018-09-21 16:33:15 +0100271 : _func(nullptr), _input(nullptr), _bias(nullptr), _output(nullptr), _result_fixedpoint_multiplier(0), _result_shift(0), _result_offset_after_shift(0), _min(0), _max(0), _output_3d_depth(1)
Gian Marco58c57942017-11-28 09:10:03 +0000272{
273}
274
275void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift,
Georgios Pinitas932491f2018-09-21 16:33:15 +0100276 int result_offset_after_shift, int min, int max, unsigned int output_3d_depth)
Gian Marco58c57942017-11-28 09:10:03 +0000277{
278 // Perform validate step
279 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
280
281 // Output auto inizialitation if not yet initialized
Georgios Pinitas932491f2018-09-21 16:33:15 +0100282 const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_output_stage_shape(*input->info(), output_3d_depth);
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100283 auto_init_if_empty(*output->info(), input->info()->clone()->set_data_type(DataType::QASYMM8).set_tensor_shape(output_shape));
Gian Marco58c57942017-11-28 09:10:03 +0000284
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100285 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias != nullptr) ? bias->info() : nullptr, output->info(),
Georgios Pinitas932491f2018-09-21 16:33:15 +0100286 min, max, output_3d_depth));
Gian Marco58c57942017-11-28 09:10:03 +0000287
288 _input = input;
289 _bias = bias;
290 _output = output;
291 _result_fixedpoint_multiplier = result_fixedpoint_multiplier;
292 _result_shift = result_shift;
293 _result_offset_after_shift = result_offset_after_shift;
294 _min = min;
295 _max = max;
Georgios Pinitas932491f2018-09-21 16:33:15 +0100296 _output_3d_depth = output_3d_depth;
Gian Marco58c57942017-11-28 09:10:03 +0000297
298 // Configure kernel window
299 auto win_config = validate_and_configure_window(input->info(), (bias != nullptr) ? bias->info() : nullptr, output->info());
300 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
301 INEKernel::configure(win_config.second);
302
303 // Check if we need to clamp the result using min and max
304 const bool is_bounded_relu = ((min != max) && !(min == 0 && max == 255));
305 _func = is_bounded_relu ? &NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run<true> : &NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run<false>;
306}
307
Georgios Pinitas932491f2018-09-21 16:33:15 +0100308Status NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max, unsigned int output_3d_depth)
Gian Marco58c57942017-11-28 09:10:03 +0000309{
Chunosov5124be52017-11-22 20:42:13 +0700310 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas932491f2018-09-21 16:33:15 +0100311 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, min, max, output_3d_depth));
Gian Marco58c57942017-11-28 09:10:03 +0000312 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(),
313 (bias != nullptr) ? bias->clone().get() : nullptr,
314 output->clone().get())
315 .first);
316
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000317 return Status{};
Gian Marco58c57942017-11-28 09:10:03 +0000318}
319
320void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run(const Window &window, const ThreadInfo &info)
321{
322 ARM_COMPUTE_UNUSED(info);
323 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
324 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
325
326 (this->*_func)(window);
327}