blob: f0ac695b20adc57462c65a2b4d6e8ad863d54061 [file] [log] [blame]
Gian Marco58c57942017-11-28 09:10:03 +00001/*
Georgios Pinitas5a594532018-12-03 14:30:05 +00002 * Copyright (c) 2017-2019 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 Pinitasbb081ca2018-11-08 10:22:01 +000046Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +000047{
48 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S32);
Gian Marco58c57942017-11-28 09:10:03 +000049 ARM_COMPUTE_RETURN_ERROR_ON(max > 255);
50 ARM_COMPUTE_RETURN_ERROR_ON(min < 0 || min > max);
51
52 // Check biases if exist
53 if(bias != nullptr)
54 {
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias);
56 ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1);
57 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != bias->dimension(0));
58 }
Chunosov5124be52017-11-22 20:42:13 +070059
60 if(output->total_size() != 0)
61 {
62 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000063 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, input);
Chunosov5124be52017-11-22 20:42:13 +070064 }
65
Georgios Pinitas631c41a2017-12-06 11:53:03 +000066 return Status{};
Gian Marco58c57942017-11-28 09:10:03 +000067}
68
Georgios Pinitas5a594532018-12-03 14:30:05 +000069std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
Gian Marco58c57942017-11-28 09:10:03 +000070{
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000071 // Output auto inizialitation if not yet initialized
72 auto_init_if_empty(*output, input->clone()->set_data_type(DataType::QASYMM8));
73
Gian Marco58c57942017-11-28 09:10:03 +000074 // Configure kernel window
Georgios Pinitas5a594532018-12-03 14:30:05 +000075 Window win = calculate_max_window(*input, Steps());
Gian Marco58c57942017-11-28 09:10:03 +000076
Georgios Pinitas5a594532018-12-03 14:30:05 +000077 // NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel doesn't need padding so update_window_and_padding() can be skipped
78 Coordinates coord;
79 coord.set_num_dimensions(output->num_dimensions());
80 output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
Gian Marco58c57942017-11-28 09:10:03 +000081
Georgios Pinitas5a594532018-12-03 14:30:05 +000082 return std::make_pair(Status{}, win);
Gian Marco58c57942017-11-28 09:10:03 +000083}
Georgios Pinitasf72f9362018-01-12 16:29:45 +000084} // namespace
Gian Marco58c57942017-11-28 09:10:03 +000085
Georgios Pinitasf72f9362018-01-12 16:29:45 +000086namespace arm_compute
Gian Marco58c57942017-11-28 09:10:03 +000087{
Georgios Pinitasf72f9362018-01-12 16:29:45 +000088class Coordinates;
Gian Marco7f0f7902017-12-07 09:26:56 +000089
90/* Function used by the left-over for loop to perform the quantization */
91template <bool is_bounded_relu>
92inline 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)
93{
94 const static int32x4_t zero_s32 = vdupq_n_s32(0);
95 const static int32x4_t sat_value_s32 = vdupq_n_s32(255);
96
97 // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar
98 in_s32 = vqrdmulhq_n_s32(in_s32, result_fixedpoint_multiplier);
99
100 // Round to the nearest division by a power-of-two using result_shift_s32
101 in_s32 = rounding_divide_by_pow2(in_s32, result_shift);
102
103 // Add the offset terms
104 in_s32 = vaddq_s32(in_s32, result_offset_after_shift_s32);
105
106 // Saturate negative values
107 in_s32 = vmaxq_s32(in_s32, zero_s32);
108 in_s32 = vminq_s32(in_s32, sat_value_s32);
109
110 auto out_u8 = static_cast<uint8_t>(vgetq_lane_s32(in_s32, 0));
111
112 if(is_bounded_relu)
113 {
114 out_u8 = std::max(out_u8, min_u8);
115 out_u8 = std::min(out_u8, max_u8);
116 }
117
118 return out_u8;
119}
Gian Marco58c57942017-11-28 09:10:03 +0000120} // namespace arm_compute
121
122template <bool is_bounded_relu>
123void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run(const Window &window)
124{
125 const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(_result_offset_after_shift);
126 const uint8x16_t min_u8 = vdupq_n_u8(static_cast<uint8_t>(_min));
127 const uint8x16_t max_u8 = vdupq_n_u8(static_cast<uint8_t>(_max));
128
129 ARM_COMPUTE_UNUSED(min_u8);
130 ARM_COMPUTE_UNUSED(max_u8);
131
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000132 const int window_step_x = 16;
133 const auto window_start_x = static_cast<int>(window.x().start());
134 const auto window_end_x = static_cast<int>(window.x().end());
Gian Marco7f0f7902017-12-07 09:26:56 +0000135
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100136 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
137 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
Gian Marco7f0f7902017-12-07 09:26:56 +0000138
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100139 Iterator in(_input, win_collapsed);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000140 Iterator out(_output, win_collapsed);
Gian Marco58c57942017-11-28 09:10:03 +0000141 if(_bias != nullptr)
142 {
143 Window win_biases;
Gian Marco7f0f7902017-12-07 09:26:56 +0000144 win_biases.set(Window::DimX, Window::Dimension(0, 1, 1));
Gian Marco58c57942017-11-28 09:10:03 +0000145 win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
146
147 Iterator bias(_bias, win_biases);
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100148 execute_window_loop(win_collapsed, [&](const Coordinates & id)
Gian Marco58c57942017-11-28 09:10:03 +0000149 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000150 // Compute 16 elements per iteration
151 int x = window_start_x;
152 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Gian Marco58c57942017-11-28 09:10:03 +0000153 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000154 int32x4x4_t in_s32 =
Gian Marco58c57942017-11-28 09:10:03 +0000155 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000156 {
157 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
158 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
159 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
160 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
161 }
162 };
Gian Marco58c57942017-11-28 09:10:03 +0000163
Gian Marco7f0f7902017-12-07 09:26:56 +0000164 const int32x4x4_t bias_s32 =
165 {
166 {
167 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 0),
168 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 4),
169 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 8),
170 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 12)
171 }
172 };
173
174 // Add the bias to GEMM's result
175 in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]);
176 in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]);
177 in_s32.val[2] = vaddq_s32(in_s32.val[2], bias_s32.val[2]);
178 in_s32.val[3] = vaddq_s32(in_s32.val[3], bias_s32.val[3]);
179
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000180 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 +0000181 }
182
183 // Compute left-over elements
184 for(; x < window_end_x; ++x)
Gian Marco58c57942017-11-28 09:10:03 +0000185 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000186 const int32_t bias_value = *(reinterpret_cast<const int32_t *>(bias.ptr()) + x);
187 int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
Gian Marco58c57942017-11-28 09:10:03 +0000188
Gian Marco7f0f7902017-12-07 09:26:56 +0000189 // Add bias
190 in_value += bias_value;
Gian Marco58c57942017-11-28 09:10:03 +0000191
Gian Marco7f0f7902017-12-07 09:26:56 +0000192 // Finalize and store the result
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000193 *(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),
194 static_cast<uint8_t>(_max));
Gian Marco7f0f7902017-12-07 09:26:56 +0000195 }
Gian Marco58c57942017-11-28 09:10:03 +0000196 },
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000197 in, out, bias);
Gian Marco58c57942017-11-28 09:10:03 +0000198 }
199 else
200 {
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100201 execute_window_loop(win_collapsed, [&](const Coordinates & id)
Gian Marco58c57942017-11-28 09:10:03 +0000202 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000203 // Compute 16 elements per iteration
204 int x = window_start_x;
205 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Gian Marco58c57942017-11-28 09:10:03 +0000206 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000207 int32x4x4_t in_s32 =
Gian Marco58c57942017-11-28 09:10:03 +0000208 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000209 {
210 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
211 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
212 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
213 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
214 }
215 };
Gian Marco58c57942017-11-28 09:10:03 +0000216
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000217 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 +0000218 }
219
220 // Compute left-over elements
221 for(; x < window_end_x; ++x)
222 {
223 const int32x4_t in_s32 = vld1q_dup_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x);
224
225 // Finalize and store the result
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000226 *(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 +0000227 }
Gian Marco58c57942017-11-28 09:10:03 +0000228 },
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000229 in, out);
Gian Marco58c57942017-11-28 09:10:03 +0000230 }
231}
232
233NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel()
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000234 : _func(nullptr), _input(nullptr), _bias(nullptr), _output(nullptr), _result_fixedpoint_multiplier(0), _result_shift(0), _result_offset_after_shift(0), _min(0), _max(0)
Gian Marco58c57942017-11-28 09:10:03 +0000235{
236}
237
238void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift,
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000239 int result_offset_after_shift, int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +0000240{
241 // Perform validate step
242 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000243 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias != nullptr) ? bias->info() : nullptr, output->info(), min, max));
Gian Marco58c57942017-11-28 09:10:03 +0000244
245 _input = input;
246 _bias = bias;
247 _output = output;
248 _result_fixedpoint_multiplier = result_fixedpoint_multiplier;
249 _result_shift = result_shift;
250 _result_offset_after_shift = result_offset_after_shift;
251 _min = min;
252 _max = max;
253
254 // Configure kernel window
Georgios Pinitas5a594532018-12-03 14:30:05 +0000255 auto win_config = validate_and_configure_window(input->info(), output->info());
Gian Marco58c57942017-11-28 09:10:03 +0000256 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
257 INEKernel::configure(win_config.second);
258
259 // Check if we need to clamp the result using min and max
260 const bool is_bounded_relu = ((min != max) && !(min == 0 && max == 255));
261 _func = is_bounded_relu ? &NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run<true> : &NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run<false>;
262}
263
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000264Status NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +0000265{
Chunosov5124be52017-11-22 20:42:13 +0700266 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000267 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, min, max));
Georgios Pinitas5a594532018-12-03 14:30:05 +0000268 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
Gian Marco58c57942017-11-28 09:10:03 +0000269
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000270 return Status{};
Gian Marco58c57942017-11-28 09:10:03 +0000271}
272
273void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run(const Window &window, const ThreadInfo &info)
274{
275 ARM_COMPUTE_UNUSED(info);
276 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
277 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
278
279 (this->*_func)(window);
280}