blob: 9ed85e62aaf50c9e19e078548910899b25b9b4dd [file] [log] [blame]
Georgios Pinitas448a81f2019-11-21 14:10:25 +00001/*
SiCongLib88272e2021-02-24 15:40:57 +00002 * Copyright (c) 2019-2021 Arm Limited.
Georgios Pinitas448a81f2019-11-21 14:10:25 +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/NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel.h"
Georgios Pinitas448a81f2019-11-21 14:10:25 +000025
Georgios Pinitas448a81f2019-11-21 14:10:25 +000026#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Georgios Pinitas448a81f2019-11-21 14:10:25 +000029#include "arm_compute/core/TensorInfo.h"
30#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"
34#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"
Georgios Pinitas448a81f2019-11-21 14:10:25 +000038
39#include <arm_neon.h>
40#include <cstddef>
41#include <cstdint>
42
43namespace arm_compute
44{
45namespace
46{
47Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
48{
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);
Georgios Pinitas448a81f2019-11-21 14:10:25 +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 }
59
60 if(output->total_size() != 0)
61 {
62 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8_SIGNED);
63 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, input);
64 }
65
66 return Status{};
67}
68
69std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
70{
71 // Output auto initialization if not yet initialized
72 auto_init_if_empty(*output, input->clone()->set_data_type(DataType::QASYMM8_SIGNED));
73
74 // Configure kernel window
75 Window win = calculate_max_window(*input, Steps());
76
77 // NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel doesn't need padding so update_window_and_padding() can be skipped
Georgios Pinitas448a81f2019-11-21 14:10:25 +000078
79 return std::make_pair(Status{}, win);
80}
81} // namespace
82
83template <bool is_bounded_relu>
84void NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::run(const Window &window)
85{
86 const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(_result_offset_after_shift);
87 const int8x16_t min_s8 = vdupq_n_s8(static_cast<int8_t>(_min));
88 const int8x16_t max_s8 = vdupq_n_s8(static_cast<int8_t>(_max));
89
90 ARM_COMPUTE_UNUSED(min_s8, max_s8);
91
92 const int window_step_x = 16;
93 const auto window_start_x = static_cast<int>(window.x().start());
94 const auto window_end_x = static_cast<int>(window.x().end());
95
96 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
97 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
98
99 Iterator in(_input, win_collapsed);
100 Iterator out(_output, win_collapsed);
101 if(_bias != nullptr)
102 {
103 Window win_biases;
104 win_biases.set(Window::DimX, Window::Dimension(0, 1, 1));
105 win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
106
107 Iterator bias(_bias, win_biases);
108 execute_window_loop(win_collapsed, [&](const Coordinates &)
109 {
110 // Compute 16 elements per iteration
111 int x = window_start_x;
112 for(; x <= (window_end_x - window_step_x); x += window_step_x)
113 {
114 int32x4x4_t in_s32 =
115 {
116 {
117 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
118 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
119 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
120 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
121 }
122 };
123
124 const int32x4x4_t bias_s32 =
125 {
126 {
127 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 0),
128 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 4),
129 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 8),
130 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 12)
131 }
132 };
133
134 // Add the bias to GEMM's result
135 in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]);
136 in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]);
137 in_s32.val[2] = vaddq_s32(in_s32.val[2], bias_s32.val[2]);
138 in_s32.val[3] = vaddq_s32(in_s32.val[3], bias_s32.val[3]);
139
140 vst1q_s8(reinterpret_cast<int8_t *>(out.ptr() + x),
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100141 finalize_quantization(in_s32, _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, min_s8, max_s8, is_bounded_relu));
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000142 }
143
144 // Compute left-over elements
145 for(; x < window_end_x; ++x)
146 {
147 const int32_t bias_value = *(reinterpret_cast<const int32_t *>(bias.ptr()) + x);
148 int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
149
150 // Add bias
151 in_value += bias_value;
152 // Finalize and store the result
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100153 *reinterpret_cast<int8_t *>(out.ptr() + x) = finalize_quantization(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift,
154 static_cast<int8_t>(_min), static_cast<int8_t>(_max), is_bounded_relu);
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000155 }
156 },
157 in, out, bias);
158 }
159 else
160 {
161 execute_window_loop(win_collapsed, [&](const Coordinates &)
162 {
163 // Compute 16 elements per iteration
164 int x = window_start_x;
165 for(; x <= (window_end_x - window_step_x); x += window_step_x)
166 {
167 int32x4x4_t in_s32 =
168 {
169 {
170 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
171 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
172 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
173 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
174 }
175 };
176
177 vst1q_s8(reinterpret_cast<int8_t *>(out.ptr() + x),
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100178 finalize_quantization(in_s32, _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, min_s8, max_s8, is_bounded_relu));
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000179 }
180
181 // Compute left-over elements
182 for(; x < window_end_x; ++x)
183 {
184 const int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
185
186 // Finalize and store the result
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100187 *reinterpret_cast<int8_t *>(out.ptr() + x) = finalize_quantization(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift,
188 static_cast<int8_t>(_min), static_cast<int8_t>(_max), is_bounded_relu);
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000189 }
190 },
191 in, out);
192 }
193}
194
195NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel()
196 : _func(nullptr), _input(nullptr), _bias(nullptr), _output(nullptr), _result_fixedpoint_multiplier(0), _result_shift(0), _result_offset_after_shift(0), _min(0), _max(0)
197{
198}
199
200void NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift,
201 int result_offset_after_shift, int min, int max)
202{
203 // Perform validate step
204 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
205 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias != nullptr) ? bias->info() : nullptr, output->info(), min, max));
206
207 _input = input;
208 _bias = bias;
209 _output = output;
210 _result_fixedpoint_multiplier = result_fixedpoint_multiplier;
211 _result_shift = result_shift;
212 _result_offset_after_shift = result_offset_after_shift;
213 _min = min;
214 _max = max;
215
216 // Configure kernel window
217 auto win_config = validate_and_configure_window(input->info(), output->info());
218 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
219 INEKernel::configure(win_config.second);
220
221 // Check if we need to clamp the result using min and max
Giorgio Arena1856ff72020-02-07 13:46:45 +0000222 const bool is_bounded_relu = !(min <= -128 && max >= 127);
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000223 _func = is_bounded_relu ? &NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::run<true> : &NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::run<false>;
224}
225
226Status NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
227{
228 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
229 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, min, max));
230 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
231
232 return Status{};
233}
234
235void NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::run(const Window &window, const ThreadInfo &info)
236{
237 ARM_COMPUTE_UNUSED(info);
238 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
239 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
240
241 (this->*_func)(window);
242}
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100243} // namespace arm_compute