blob: 83ca6f944da316f3d07fcc0f7497cb391654d968 [file] [log] [blame]
Gian Marco58c57942017-11-28 09:10:03 +00001/*
SiCongLib88272e2021-02-24 15:40:57 +00002 * Copyright (c) 2017-2021 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 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel.h"
Gian Marco58c57942017-11-28 09:10:03 +000025
Gian Marco58c57942017-11-28 09:10:03 +000026#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Georgios Pinitas932491f2018-09-21 16:33:15 +010029#include "arm_compute/core/TensorInfo.h"
Gian Marco58c57942017-11-28 09:10:03 +000030#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Utils.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
Georgios Pinitas041f36d2018-09-18 18:38:37 +010034#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010035#include "src/core/NEON/NEAsymm.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010036#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
Gian Marco58c57942017-11-28 09:10:03 +000038
39#include <arm_neon.h>
40#include <cstddef>
41#include <cstdint>
42
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +000043namespace arm_compute
44{
Gian Marco58c57942017-11-28 09:10:03 +000045namespace
46{
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000047Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +000048{
49 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S32);
Giorgio Arena1856ff72020-02-07 13:46:45 +000050 ARM_COMPUTE_RETURN_ERROR_ON(min > max);
Gian Marco58c57942017-11-28 09:10:03 +000051
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
Gian Marco58c57942017-11-28 09:10:03 +000078
Georgios Pinitas5a594532018-12-03 14:30:05 +000079 return std::make_pair(Status{}, win);
Gian Marco58c57942017-11-28 09:10:03 +000080}
Georgios Pinitasf72f9362018-01-12 16:29:45 +000081} // namespace
Gian Marco58c57942017-11-28 09:10:03 +000082
Georgios Pinitasf72f9362018-01-12 16:29:45 +000083namespace arm_compute
Gian Marco58c57942017-11-28 09:10:03 +000084{
Georgios Pinitasf72f9362018-01-12 16:29:45 +000085class Coordinates;
Gian Marco58c57942017-11-28 09:10:03 +000086} // namespace arm_compute
87
88template <bool is_bounded_relu>
89void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run(const Window &window)
90{
91 const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(_result_offset_after_shift);
92 const uint8x16_t min_u8 = vdupq_n_u8(static_cast<uint8_t>(_min));
93 const uint8x16_t max_u8 = vdupq_n_u8(static_cast<uint8_t>(_max));
94
95 ARM_COMPUTE_UNUSED(min_u8);
96 ARM_COMPUTE_UNUSED(max_u8);
97
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000098 const int window_step_x = 16;
99 const auto window_start_x = static_cast<int>(window.x().start());
100 const auto window_end_x = static_cast<int>(window.x().end());
Gian Marco7f0f7902017-12-07 09:26:56 +0000101
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100102 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
103 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
Gian Marco7f0f7902017-12-07 09:26:56 +0000104
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100105 Iterator in(_input, win_collapsed);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000106 Iterator out(_output, win_collapsed);
Gian Marco58c57942017-11-28 09:10:03 +0000107 if(_bias != nullptr)
108 {
109 Window win_biases;
Gian Marco7f0f7902017-12-07 09:26:56 +0000110 win_biases.set(Window::DimX, Window::Dimension(0, 1, 1));
Gian Marco58c57942017-11-28 09:10:03 +0000111 win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
112
113 Iterator bias(_bias, win_biases);
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100114 execute_window_loop(win_collapsed, [&](const Coordinates &)
Gian Marco58c57942017-11-28 09:10:03 +0000115 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000116 // Compute 16 elements per iteration
117 int x = window_start_x;
118 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Gian Marco58c57942017-11-28 09:10:03 +0000119 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000120 int32x4x4_t in_s32 =
Gian Marco58c57942017-11-28 09:10:03 +0000121 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000122 {
123 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
124 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
125 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
126 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
127 }
128 };
Gian Marco58c57942017-11-28 09:10:03 +0000129
Gian Marco7f0f7902017-12-07 09:26:56 +0000130 const int32x4x4_t bias_s32 =
131 {
132 {
133 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 0),
134 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 4),
135 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 8),
136 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 12)
137 }
138 };
139
140 // Add the bias to GEMM's result
141 in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]);
142 in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]);
143 in_s32.val[2] = vaddq_s32(in_s32.val[2], bias_s32.val[2]);
144 in_s32.val[3] = vaddq_s32(in_s32.val[3], bias_s32.val[3]);
145
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100146 vst1q_u8(out.ptr() + x, finalize_quantization(in_s32, _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, min_u8, max_u8, is_bounded_relu));
Gian Marco7f0f7902017-12-07 09:26:56 +0000147 }
148
149 // Compute left-over elements
150 for(; x < window_end_x; ++x)
Gian Marco58c57942017-11-28 09:10:03 +0000151 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000152 const int32_t bias_value = *(reinterpret_cast<const int32_t *>(bias.ptr()) + x);
153 int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
Gian Marco58c57942017-11-28 09:10:03 +0000154
Gian Marco7f0f7902017-12-07 09:26:56 +0000155 // Add bias
156 in_value += bias_value;
Gian Marco7f0f7902017-12-07 09:26:56 +0000157 // Finalize and store the result
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100158 *(out.ptr() + x) = finalize_quantization(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift, static_cast<uint8_t>(_min), static_cast<uint8_t>(_max), is_bounded_relu);
Gian Marco7f0f7902017-12-07 09:26:56 +0000159 }
Gian Marco58c57942017-11-28 09:10:03 +0000160 },
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000161 in, out, bias);
Gian Marco58c57942017-11-28 09:10:03 +0000162 }
163 else
164 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100165 execute_window_loop(win_collapsed, [&](const Coordinates &)
Gian Marco58c57942017-11-28 09:10:03 +0000166 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000167 // Compute 16 elements per iteration
168 int x = window_start_x;
169 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Gian Marco58c57942017-11-28 09:10:03 +0000170 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000171 int32x4x4_t in_s32 =
Gian Marco58c57942017-11-28 09:10:03 +0000172 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000173 {
174 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
175 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
176 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
177 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
178 }
179 };
Gian Marco58c57942017-11-28 09:10:03 +0000180
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100181 vst1q_u8(out.ptr() + x, finalize_quantization(in_s32, _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, min_u8, max_u8, is_bounded_relu));
Gian Marco7f0f7902017-12-07 09:26:56 +0000182 }
183
184 // Compute left-over elements
185 for(; x < window_end_x; ++x)
186 {
George Wort2d7e6832019-02-22 16:37:41 +0000187 const int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
Gian Marco7f0f7902017-12-07 09:26:56 +0000188
189 // Finalize and store the result
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100190 *(out.ptr() + x) = finalize_quantization(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift, static_cast<uint8_t>(_min), static_cast<uint8_t>(_max), is_bounded_relu);
Gian Marco7f0f7902017-12-07 09:26:56 +0000191 }
Gian Marco58c57942017-11-28 09:10:03 +0000192 },
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000193 in, out);
Gian Marco58c57942017-11-28 09:10:03 +0000194 }
195}
196
197NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel()
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000198 : _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 +0000199{
200}
201
202void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift,
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000203 int result_offset_after_shift, int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +0000204{
205 // Perform validate step
206 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000207 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 +0000208
209 _input = input;
210 _bias = bias;
211 _output = output;
212 _result_fixedpoint_multiplier = result_fixedpoint_multiplier;
213 _result_shift = result_shift;
214 _result_offset_after_shift = result_offset_after_shift;
215 _min = min;
216 _max = max;
217
218 // Configure kernel window
Georgios Pinitas5a594532018-12-03 14:30:05 +0000219 auto win_config = validate_and_configure_window(input->info(), output->info());
Gian Marco58c57942017-11-28 09:10:03 +0000220 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
221 INEKernel::configure(win_config.second);
222
223 // Check if we need to clamp the result using min and max
Giorgio Arena1856ff72020-02-07 13:46:45 +0000224 const bool is_bounded_relu = !(min <= 0 && max >= 255);
Gian Marco58c57942017-11-28 09:10:03 +0000225 _func = is_bounded_relu ? &NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run<true> : &NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run<false>;
226}
227
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000228Status NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +0000229{
Chunosov5124be52017-11-22 20:42:13 +0700230 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000231 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, min, max));
Georgios Pinitas5a594532018-12-03 14:30:05 +0000232 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
Gian Marco58c57942017-11-28 09:10:03 +0000233
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000234 return Status{};
Gian Marco58c57942017-11-28 09:10:03 +0000235}
236
237void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run(const Window &window, const ThreadInfo &info)
238{
239 ARM_COMPUTE_UNUSED(info);
240 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
241 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
242
243 (this->*_func)(window);
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000244}
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100245} // namespace arm_compute