blob: d3cfc7a8fac0d227d54a851602b948dbf092146f [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 Marco58c57942017-11-28 09:10:03 +000089} // namespace arm_compute
90
91template <bool is_bounded_relu>
92void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run(const Window &window)
93{
94 const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(_result_offset_after_shift);
95 const uint8x16_t min_u8 = vdupq_n_u8(static_cast<uint8_t>(_min));
96 const uint8x16_t max_u8 = vdupq_n_u8(static_cast<uint8_t>(_max));
97
98 ARM_COMPUTE_UNUSED(min_u8);
99 ARM_COMPUTE_UNUSED(max_u8);
100
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000101 const int window_step_x = 16;
102 const auto window_start_x = static_cast<int>(window.x().start());
103 const auto window_end_x = static_cast<int>(window.x().end());
Gian Marco7f0f7902017-12-07 09:26:56 +0000104
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100105 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
106 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
Gian Marco7f0f7902017-12-07 09:26:56 +0000107
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100108 Iterator in(_input, win_collapsed);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000109 Iterator out(_output, win_collapsed);
Gian Marco58c57942017-11-28 09:10:03 +0000110 if(_bias != nullptr)
111 {
112 Window win_biases;
Gian Marco7f0f7902017-12-07 09:26:56 +0000113 win_biases.set(Window::DimX, Window::Dimension(0, 1, 1));
Gian Marco58c57942017-11-28 09:10:03 +0000114 win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
115
116 Iterator bias(_bias, win_biases);
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100117 execute_window_loop(win_collapsed, [&](const Coordinates & id)
Gian Marco58c57942017-11-28 09:10:03 +0000118 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000119 // Compute 16 elements per iteration
120 int x = window_start_x;
121 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Gian Marco58c57942017-11-28 09:10:03 +0000122 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000123 int32x4x4_t in_s32 =
Gian Marco58c57942017-11-28 09:10:03 +0000124 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000125 {
126 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
127 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
128 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
129 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
130 }
131 };
Gian Marco58c57942017-11-28 09:10:03 +0000132
Gian Marco7f0f7902017-12-07 09:26:56 +0000133 const int32x4x4_t bias_s32 =
134 {
135 {
136 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 0),
137 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 4),
138 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 8),
139 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 12)
140 }
141 };
142
143 // Add the bias to GEMM's result
144 in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]);
145 in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]);
146 in_s32.val[2] = vaddq_s32(in_s32.val[2], bias_s32.val[2]);
147 in_s32.val[3] = vaddq_s32(in_s32.val[3], bias_s32.val[3]);
148
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000149 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 +0000150 }
151
152 // Compute left-over elements
153 for(; x < window_end_x; ++x)
Gian Marco58c57942017-11-28 09:10:03 +0000154 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000155 const int32_t bias_value = *(reinterpret_cast<const int32_t *>(bias.ptr()) + x);
156 int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
Gian Marco58c57942017-11-28 09:10:03 +0000157
Gian Marco7f0f7902017-12-07 09:26:56 +0000158 // Add bias
159 in_value += bias_value;
Gian Marco7f0f7902017-12-07 09:26:56 +0000160 // Finalize and store the result
George Wort2d7e6832019-02-22 16:37:41 +0000161 *(out.ptr() + x) = finalize_quantization<is_bounded_relu>(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift, static_cast<uint8_t>(_min), static_cast<uint8_t>(_max));
Gian Marco7f0f7902017-12-07 09:26:56 +0000162 }
Gian Marco58c57942017-11-28 09:10:03 +0000163 },
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000164 in, out, bias);
Gian Marco58c57942017-11-28 09:10:03 +0000165 }
166 else
167 {
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100168 execute_window_loop(win_collapsed, [&](const Coordinates & id)
Gian Marco58c57942017-11-28 09:10:03 +0000169 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000170 // Compute 16 elements per iteration
171 int x = window_start_x;
172 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Gian Marco58c57942017-11-28 09:10:03 +0000173 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000174 int32x4x4_t in_s32 =
Gian Marco58c57942017-11-28 09:10:03 +0000175 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000176 {
177 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
178 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
179 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
180 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
181 }
182 };
Gian Marco58c57942017-11-28 09:10:03 +0000183
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000184 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 +0000185 }
186
187 // Compute left-over elements
188 for(; x < window_end_x; ++x)
189 {
George Wort2d7e6832019-02-22 16:37:41 +0000190 const int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
Gian Marco7f0f7902017-12-07 09:26:56 +0000191
192 // Finalize and store the result
George Wort2d7e6832019-02-22 16:37:41 +0000193 *(out.ptr() + x) = finalize_quantization<is_bounded_relu>(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift, static_cast<uint8_t>(_min), static_cast<uint8_t>(_max));
Gian Marco7f0f7902017-12-07 09:26:56 +0000194 }
Gian Marco58c57942017-11-28 09:10:03 +0000195 },
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000196 in, out);
Gian Marco58c57942017-11-28 09:10:03 +0000197 }
198}
199
200NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel()
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000201 : _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 +0000202{
203}
204
205void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift,
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000206 int result_offset_after_shift, int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +0000207{
208 // Perform validate step
209 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000210 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 +0000211
212 _input = input;
213 _bias = bias;
214 _output = output;
215 _result_fixedpoint_multiplier = result_fixedpoint_multiplier;
216 _result_shift = result_shift;
217 _result_offset_after_shift = result_offset_after_shift;
218 _min = min;
219 _max = max;
220
221 // Configure kernel window
Georgios Pinitas5a594532018-12-03 14:30:05 +0000222 auto win_config = validate_and_configure_window(input->info(), output->info());
Gian Marco58c57942017-11-28 09:10:03 +0000223 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
224 INEKernel::configure(win_config.second);
225
226 // Check if we need to clamp the result using min and max
227 const bool is_bounded_relu = ((min != max) && !(min == 0 && max == 255));
228 _func = is_bounded_relu ? &NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run<true> : &NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run<false>;
229}
230
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000231Status NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +0000232{
Chunosov5124be52017-11-22 20:42:13 +0700233 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000234 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, min, max));
Georgios Pinitas5a594532018-12-03 14:30:05 +0000235 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
Gian Marco58c57942017-11-28 09:10:03 +0000236
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000237 return Status{};
Gian Marco58c57942017-11-28 09:10:03 +0000238}
239
240void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run(const Window &window, const ThreadInfo &info)
241{
242 ARM_COMPUTE_UNUSED(info);
243 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
244 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
245
246 (this->*_func)(window);
247}