blob: 7f351020b9567925e7044bfb342bfd6770d4e5c0 [file] [log] [blame]
Gian Marcoe75a02b2017-11-08 12:24:09 +00001/*
2 * Copyright (c) 2017 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#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{
68 constexpr unsigned int num_elems_processed_per_iteration = 16;
69
70 // Configure kernel window
71 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
72
73 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000074
75 bool window_changed = update_window_and_padding(win,
Chunosov5124be52017-11-22 20:42:13 +070076 input_access);
77
78 if(output->total_size() != 0)
79 {
80 AccessWindowHorizontal output_result_access(output, 0, num_elems_processed_per_iteration);
81 window_changed = window_changed || update_window_and_padding(win, output_result_access);
82
83 output_result_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
84 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000085
86 if(bias != nullptr)
87 {
88 AccessWindowStatic bias_access(bias, 0, 0, ceil_to_multiple(bias->dimension(0), num_elems_processed_per_iteration), bias->tensor_shape()[1]);
89 window_changed = window_changed || update_window_and_padding(win, bias_access);
90 }
91
Georgios Pinitas631c41a2017-12-06 11:53:03 +000092 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000093 return std::make_pair(err, win);
94}
95
Gian Marco6b77e912017-11-17 09:27:57 +000096inline void scale_input(int32x4x4_t &in_s32, int32x4_t result_offset_s32, int32_t result_mult_int)
97{
98 // Add the offset terms to GEMM's result
99 in_s32.val[0] = vaddq_s32(in_s32.val[0], result_offset_s32);
100 in_s32.val[1] = vaddq_s32(in_s32.val[1], result_offset_s32);
101 in_s32.val[2] = vaddq_s32(in_s32.val[2], result_offset_s32);
102 in_s32.val[3] = vaddq_s32(in_s32.val[3], result_offset_s32);
103
104 // Multiply by result_mult_int
105 in_s32.val[0] = vmulq_n_s32(in_s32.val[0], result_mult_int);
106 in_s32.val[1] = vmulq_n_s32(in_s32.val[1], result_mult_int);
107 in_s32.val[2] = vmulq_n_s32(in_s32.val[2], result_mult_int);
108 in_s32.val[3] = vmulq_n_s32(in_s32.val[3], result_mult_int);
109}
110
111template <bool is_bounded_relu>
112inline uint8x16_t finalize_quantization(int32x4x4_t &in_s32, int32x4_t result_shift_s32, uint8x16_t min_u8, uint8x16_t max_u8)
113{
114 const static int32x4_t zero_s32 = vdupq_n_s32(0);
115
116 // Shift final result (negative value shift right)
117 in_s32.val[0] = vshlq_s32(in_s32.val[0], result_shift_s32);
118 in_s32.val[1] = vshlq_s32(in_s32.val[1], result_shift_s32);
119 in_s32.val[2] = vshlq_s32(in_s32.val[2], result_shift_s32);
120 in_s32.val[3] = vshlq_s32(in_s32.val[3], result_shift_s32);
121
122 // Saturate negative values
123 in_s32.val[0] = vmaxq_s32(in_s32.val[0], zero_s32);
124 in_s32.val[1] = vmaxq_s32(in_s32.val[1], zero_s32);
125 in_s32.val[2] = vmaxq_s32(in_s32.val[2], zero_s32);
126 in_s32.val[3] = vmaxq_s32(in_s32.val[3], zero_s32);
127
128 // Convert S32 to S16
129 const int16x8x2_t in_s16 =
130 {
131 {
132 vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])),
133 vcombine_s16(vqmovn_s32(in_s32.val[2]), vqmovn_s32(in_s32.val[3]))
134 }
135 };
136
137 // Convert S16 to U8
138 uint8x16_t out_u8 = vcombine_u8(vqmovun_s16(in_s16.val[0]), vqmovun_s16(in_s16.val[1]));
139
140 if(is_bounded_relu)
141 {
142 out_u8 = vmaxq_u8(out_u8, min_u8);
143 out_u8 = vminq_u8(out_u8, max_u8);
144 }
145
146 return out_u8;
147}
148} // namespace
149
Gian Marcoe75a02b2017-11-08 12:24:09 +0000150namespace arm_compute
151{
152class Coordinates;
153} // namespace arm_compute
154
Gian Marco6b77e912017-11-17 09:27:57 +0000155template <bool is_bounded_relu>
156void NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::run(const Window &window)
157{
158 const int32x4_t result_offset_s32 = vdupq_n_s32(_result_offset);
159 const int32x4_t result_shift_s32 = vdupq_n_s32(-_result_shift);
160 const uint8x16_t min_u8 = vdupq_n_u8(static_cast<uint8_t>(_min));
161 const uint8x16_t max_u8 = vdupq_n_u8(static_cast<uint8_t>(_max));
162
163 ARM_COMPUTE_UNUSED(min_u8);
164 ARM_COMPUTE_UNUSED(max_u8);
165
166 Iterator in(_input, window);
167 Iterator out(_output, window);
168
169 if(_bias != nullptr)
170 {
171 Window win_biases;
172 win_biases.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), window.x().step()));
173 win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
174
175 Iterator bias(_bias, win_biases);
176 execute_window_loop(window, [&](const Coordinates & id)
177 {
178 int32x4x4_t in_s32 =
179 {
180 {
181 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 0),
182 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 4),
183 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 8),
184 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 12)
185 }
186 };
187
188 const int32x4x4_t bias_s32 =
189 {
190 {
191 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + 0),
192 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + 4),
193 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + 8),
194 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + 12)
195 }
196 };
197
Gian Marco6b77e912017-11-17 09:27:57 +0000198 // Add the bias to GEMM's result
199 in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]);
200 in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]);
201 in_s32.val[2] = vaddq_s32(in_s32.val[2], bias_s32.val[2]);
202 in_s32.val[3] = vaddq_s32(in_s32.val[3], bias_s32.val[3]);
203
Gian Marco58c57942017-11-28 09:10:03 +0000204 // Add the offset terms to GEMM's result and multiply by result_mult_int
205 scale_input(in_s32, result_offset_s32, _result_mult_int);
206
Gian Marco6b77e912017-11-17 09:27:57 +0000207 vst1q_u8(out.ptr(), finalize_quantization<is_bounded_relu>(in_s32, result_shift_s32, min_u8, max_u8));
208 },
209 in, bias, out);
210 }
211 else
212 {
213 execute_window_loop(window, [&](const Coordinates & id)
214 {
215 int32x4x4_t in_s32 =
216 {
217 {
218 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 0),
219 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 4),
220 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 8),
221 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 12)
222 }
223 };
224
225 // Add the offset terms to GEMM's result and multiply by result_mult_int
226 scale_input(in_s32, result_offset_s32, _result_mult_int);
227
228 vst1q_u8(out.ptr(), finalize_quantization<is_bounded_relu>(in_s32, result_shift_s32, min_u8, max_u8));
229 },
230 in, out);
231 }
232}
233
Gian Marcoe75a02b2017-11-08 12:24:09 +0000234NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel()
Gian Marco6b77e912017-11-17 09:27:57 +0000235 : _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 +0000236{
237}
238
Gian Marco6b77e912017-11-17 09:27:57 +0000239void 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 +0000240{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000241 // Perform validate step
242 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Gian Marco58c57942017-11-28 09:10:03 +0000243
244 // Output auto inizialitation if not yet initialized
245 auto_init_if_empty(*output->info(), input->info()->clone()->set_data_type(DataType::QASYMM8));
246
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000247 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(),
248 (bias != nullptr) ? bias->info() : nullptr,
249 output->info(),
250 min,
251 max));
Gian Marcoe75a02b2017-11-08 12:24:09 +0000252
253 _input = input;
Gian Marco6b77e912017-11-17 09:27:57 +0000254 _bias = bias;
Gian Marcoe75a02b2017-11-08 12:24:09 +0000255 _output = output;
256 _result_offset = result_offset;
257 _result_mult_int = result_mult_int;
258 _result_shift = result_shift;
Gian Marco6b77e912017-11-17 09:27:57 +0000259 _min = min;
260 _max = max;
Gian Marcoe75a02b2017-11-08 12:24:09 +0000261
Gian Marcoe75a02b2017-11-08 12:24:09 +0000262 // Configure kernel window
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000263 auto win_config = validate_and_configure_window(input->info(), (bias != nullptr) ? bias->info() : nullptr, output->info());
264 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
265 INEKernel::configure(win_config.second);
Gian Marco6b77e912017-11-17 09:27:57 +0000266
267 // Check if we need to clamp the result using min and max
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000268 const bool is_bounded_relu = ((min != max) && !(min == 0 && max == 255));
269 _func = is_bounded_relu ? &NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::run<true> : &NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::run<false>;
270}
271
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000272Status NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000273{
Chunosov5124be52017-11-22 20:42:13 +0700274 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000275 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, min, max));
276 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(),
277 (bias != nullptr) ? bias->clone().get() : nullptr,
278 output->clone().get())
279 .first);
280
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000281 return Status{};
Gian Marcoe75a02b2017-11-08 12:24:09 +0000282}
283
284void NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::run(const Window &window, const ThreadInfo &info)
285{
286 ARM_COMPUTE_UNUSED(info);
287 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
288 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
289
Gian Marco6b77e912017-11-17 09:27:57 +0000290 (this->*_func)(window);
Gian Marcoe75a02b2017-11-08 12:24:09 +0000291}