blob: 4a9d2f748148971584a07cc063785421671e9dfe [file] [log] [blame]
Gian Marco58c57942017-11-28 09:10:03 +00001/*
Giorgio Arena1856ff72020-02-07 13:46:45 +00002 * Copyright (c) 2017-2020 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
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +000042namespace arm_compute
43{
Gian Marco58c57942017-11-28 09:10:03 +000044namespace
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);
Giorgio Arena1856ff72020-02-07 13:46:45 +000049 ARM_COMPUTE_RETURN_ERROR_ON(min > max);
Gian Marco58c57942017-11-28 09:10:03 +000050
51 // Check biases if exist
52 if(bias != nullptr)
53 {
54 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias);
55 ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1);
56 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != bias->dimension(0));
57 }
Chunosov5124be52017-11-22 20:42:13 +070058
59 if(output->total_size() != 0)
60 {
61 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000062 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, input);
Chunosov5124be52017-11-22 20:42:13 +070063 }
64
Georgios Pinitas631c41a2017-12-06 11:53:03 +000065 return Status{};
Gian Marco58c57942017-11-28 09:10:03 +000066}
67
Georgios Pinitas5a594532018-12-03 14:30:05 +000068std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
Gian Marco58c57942017-11-28 09:10:03 +000069{
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000070 // Output auto inizialitation if not yet initialized
71 auto_init_if_empty(*output, input->clone()->set_data_type(DataType::QASYMM8));
72
Gian Marco58c57942017-11-28 09:10:03 +000073 // Configure kernel window
Georgios Pinitas5a594532018-12-03 14:30:05 +000074 Window win = calculate_max_window(*input, Steps());
Gian Marco58c57942017-11-28 09:10:03 +000075
Georgios Pinitas5a594532018-12-03 14:30:05 +000076 // NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel doesn't need padding so update_window_and_padding() can be skipped
77 Coordinates coord;
78 coord.set_num_dimensions(output->num_dimensions());
79 output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
Gian Marco58c57942017-11-28 09:10:03 +000080
Georgios Pinitas5a594532018-12-03 14:30:05 +000081 return std::make_pair(Status{}, win);
Gian Marco58c57942017-11-28 09:10:03 +000082}
Georgios Pinitasf72f9362018-01-12 16:29:45 +000083} // namespace
Gian Marco58c57942017-11-28 09:10:03 +000084
Georgios Pinitasf72f9362018-01-12 16:29:45 +000085namespace arm_compute
Gian Marco58c57942017-11-28 09:10:03 +000086{
Georgios Pinitasf72f9362018-01-12 16:29:45 +000087class Coordinates;
Gian Marco58c57942017-11-28 09:10:03 +000088} // namespace arm_compute
89
90template <bool is_bounded_relu>
91void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run(const Window &window)
92{
93 const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(_result_offset_after_shift);
94 const uint8x16_t min_u8 = vdupq_n_u8(static_cast<uint8_t>(_min));
95 const uint8x16_t max_u8 = vdupq_n_u8(static_cast<uint8_t>(_max));
96
97 ARM_COMPUTE_UNUSED(min_u8);
98 ARM_COMPUTE_UNUSED(max_u8);
99
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000100 const int window_step_x = 16;
101 const auto window_start_x = static_cast<int>(window.x().start());
102 const auto window_end_x = static_cast<int>(window.x().end());
Gian Marco7f0f7902017-12-07 09:26:56 +0000103
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100104 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
105 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
Gian Marco7f0f7902017-12-07 09:26:56 +0000106
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100107 Iterator in(_input, win_collapsed);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000108 Iterator out(_output, win_collapsed);
Gian Marco58c57942017-11-28 09:10:03 +0000109 if(_bias != nullptr)
110 {
111 Window win_biases;
Gian Marco7f0f7902017-12-07 09:26:56 +0000112 win_biases.set(Window::DimX, Window::Dimension(0, 1, 1));
Gian Marco58c57942017-11-28 09:10:03 +0000113 win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
114
115 Iterator bias(_bias, win_biases);
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100116 execute_window_loop(win_collapsed, [&](const Coordinates &)
Gian Marco58c57942017-11-28 09:10:03 +0000117 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000118 // Compute 16 elements per iteration
119 int x = window_start_x;
120 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Gian Marco58c57942017-11-28 09:10:03 +0000121 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000122 int32x4x4_t in_s32 =
Gian Marco58c57942017-11-28 09:10:03 +0000123 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000124 {
125 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
126 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
127 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
128 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
129 }
130 };
Gian Marco58c57942017-11-28 09:10:03 +0000131
Gian Marco7f0f7902017-12-07 09:26:56 +0000132 const int32x4x4_t bias_s32 =
133 {
134 {
135 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 0),
136 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 4),
137 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 8),
138 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 12)
139 }
140 };
141
142 // Add the bias to GEMM's result
143 in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]);
144 in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]);
145 in_s32.val[2] = vaddq_s32(in_s32.val[2], bias_s32.val[2]);
146 in_s32.val[3] = vaddq_s32(in_s32.val[3], bias_s32.val[3]);
147
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000148 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 +0000149 }
150
151 // Compute left-over elements
152 for(; x < window_end_x; ++x)
Gian Marco58c57942017-11-28 09:10:03 +0000153 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000154 const int32_t bias_value = *(reinterpret_cast<const int32_t *>(bias.ptr()) + x);
155 int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
Gian Marco58c57942017-11-28 09:10:03 +0000156
Gian Marco7f0f7902017-12-07 09:26:56 +0000157 // Add bias
158 in_value += bias_value;
Gian Marco7f0f7902017-12-07 09:26:56 +0000159 // Finalize and store the result
George Wort2d7e6832019-02-22 16:37:41 +0000160 *(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 +0000161 }
Gian Marco58c57942017-11-28 09:10:03 +0000162 },
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000163 in, out, bias);
Gian Marco58c57942017-11-28 09:10:03 +0000164 }
165 else
166 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100167 execute_window_loop(win_collapsed, [&](const Coordinates &)
Gian Marco58c57942017-11-28 09:10:03 +0000168 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000169 // Compute 16 elements per iteration
170 int x = window_start_x;
171 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Gian Marco58c57942017-11-28 09:10:03 +0000172 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000173 int32x4x4_t in_s32 =
Gian Marco58c57942017-11-28 09:10:03 +0000174 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000175 {
176 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
177 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
178 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
179 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
180 }
181 };
Gian Marco58c57942017-11-28 09:10:03 +0000182
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000183 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 +0000184 }
185
186 // Compute left-over elements
187 for(; x < window_end_x; ++x)
188 {
George Wort2d7e6832019-02-22 16:37:41 +0000189 const int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
Gian Marco7f0f7902017-12-07 09:26:56 +0000190
191 // Finalize and store the result
George Wort2d7e6832019-02-22 16:37:41 +0000192 *(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 +0000193 }
Gian Marco58c57942017-11-28 09:10:03 +0000194 },
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000195 in, out);
Gian Marco58c57942017-11-28 09:10:03 +0000196 }
197}
198
199NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel()
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000200 : _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 +0000201{
202}
203
204void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift,
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000205 int result_offset_after_shift, int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +0000206{
207 // Perform validate step
208 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000209 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 +0000210
211 _input = input;
212 _bias = bias;
213 _output = output;
214 _result_fixedpoint_multiplier = result_fixedpoint_multiplier;
215 _result_shift = result_shift;
216 _result_offset_after_shift = result_offset_after_shift;
217 _min = min;
218 _max = max;
219
220 // Configure kernel window
Georgios Pinitas5a594532018-12-03 14:30:05 +0000221 auto win_config = validate_and_configure_window(input->info(), output->info());
Gian Marco58c57942017-11-28 09:10:03 +0000222 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
223 INEKernel::configure(win_config.second);
224
225 // Check if we need to clamp the result using min and max
Giorgio Arena1856ff72020-02-07 13:46:45 +0000226 const bool is_bounded_relu = !(min <= 0 && max >= 255);
Gian Marco58c57942017-11-28 09:10:03 +0000227 _func = is_bounded_relu ? &NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run<true> : &NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run<false>;
228}
229
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000230Status NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +0000231{
Chunosov5124be52017-11-22 20:42:13 +0700232 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000233 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, min, max));
Georgios Pinitas5a594532018-12-03 14:30:05 +0000234 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
Gian Marco58c57942017-11-28 09:10:03 +0000235
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000236 return Status{};
Gian Marco58c57942017-11-28 09:10:03 +0000237}
238
239void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run(const Window &window, const ThreadInfo &info)
240{
241 ARM_COMPUTE_UNUSED(info);
242 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
243 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
244
245 (this->*_func)(window);
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000246}
247} // namespace arm_compute