blob: a0a5c5d79f524ac14a4e7aeca1b43b878146e1d3 [file] [log] [blame]
Georgios Pinitas448a81f2019-11-21 14:10:25 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 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 */
24#include "arm_compute/core/NEON/kernels/NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel.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"
31#include "arm_compute/core/TensorInfo.h"
32#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"
36#include "arm_compute/core/utils/misc/ShapeCalculator.h"
37
38#include <arm_neon.h>
39#include <cstddef>
40#include <cstdint>
41
42namespace arm_compute
43{
44namespace
45{
46Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
47{
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);
Georgios Pinitas448a81f2019-11-21 14:10:25 +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 }
58
59 if(output->total_size() != 0)
60 {
61 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8_SIGNED);
62 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, input);
63 }
64
65 return Status{};
66}
67
68std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
69{
70 // Output auto initialization if not yet initialized
71 auto_init_if_empty(*output, input->clone()->set_data_type(DataType::QASYMM8_SIGNED));
72
73 // Configure kernel window
74 Window win = calculate_max_window(*input, Steps());
75
76 // NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel 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()));
80
81 return std::make_pair(Status{}, win);
82}
83} // namespace
84
85template <bool is_bounded_relu>
86void NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::run(const Window &window)
87{
88 const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(_result_offset_after_shift);
89 const int8x16_t min_s8 = vdupq_n_s8(static_cast<int8_t>(_min));
90 const int8x16_t max_s8 = vdupq_n_s8(static_cast<int8_t>(_max));
91
92 ARM_COMPUTE_UNUSED(min_s8, max_s8);
93
94 const int window_step_x = 16;
95 const auto window_start_x = static_cast<int>(window.x().start());
96 const auto window_end_x = static_cast<int>(window.x().end());
97
98 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
99 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
100
101 Iterator in(_input, win_collapsed);
102 Iterator out(_output, win_collapsed);
103 if(_bias != nullptr)
104 {
105 Window win_biases;
106 win_biases.set(Window::DimX, Window::Dimension(0, 1, 1));
107 win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
108
109 Iterator bias(_bias, win_biases);
110 execute_window_loop(win_collapsed, [&](const Coordinates &)
111 {
112 // Compute 16 elements per iteration
113 int x = window_start_x;
114 for(; x <= (window_end_x - window_step_x); x += window_step_x)
115 {
116 int32x4x4_t in_s32 =
117 {
118 {
119 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
120 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
121 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
122 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
123 }
124 };
125
126 const int32x4x4_t bias_s32 =
127 {
128 {
129 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 0),
130 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 4),
131 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 8),
132 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 12)
133 }
134 };
135
136 // Add the bias to GEMM's result
137 in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]);
138 in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]);
139 in_s32.val[2] = vaddq_s32(in_s32.val[2], bias_s32.val[2]);
140 in_s32.val[3] = vaddq_s32(in_s32.val[3], bias_s32.val[3]);
141
142 vst1q_s8(reinterpret_cast<int8_t *>(out.ptr() + x),
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100143 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 +0000144 }
145
146 // Compute left-over elements
147 for(; x < window_end_x; ++x)
148 {
149 const int32_t bias_value = *(reinterpret_cast<const int32_t *>(bias.ptr()) + x);
150 int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
151
152 // Add bias
153 in_value += bias_value;
154 // Finalize and store the result
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100155 *reinterpret_cast<int8_t *>(out.ptr() + x) = finalize_quantization(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift,
156 static_cast<int8_t>(_min), static_cast<int8_t>(_max), is_bounded_relu);
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000157 }
158 },
159 in, out, bias);
160 }
161 else
162 {
163 execute_window_loop(win_collapsed, [&](const Coordinates &)
164 {
165 // Compute 16 elements per iteration
166 int x = window_start_x;
167 for(; x <= (window_end_x - window_step_x); x += window_step_x)
168 {
169 int32x4x4_t in_s32 =
170 {
171 {
172 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
173 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
174 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
175 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
176 }
177 };
178
179 vst1q_s8(reinterpret_cast<int8_t *>(out.ptr() + x),
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100180 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 +0000181 }
182
183 // Compute left-over elements
184 for(; x < window_end_x; ++x)
185 {
186 const int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
187
188 // Finalize and store the result
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100189 *reinterpret_cast<int8_t *>(out.ptr() + x) = finalize_quantization(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift,
190 static_cast<int8_t>(_min), static_cast<int8_t>(_max), is_bounded_relu);
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000191 }
192 },
193 in, out);
194 }
195}
196
197NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel()
198 : _func(nullptr), _input(nullptr), _bias(nullptr), _output(nullptr), _result_fixedpoint_multiplier(0), _result_shift(0), _result_offset_after_shift(0), _min(0), _max(0)
199{
200}
201
202void NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift,
203 int result_offset_after_shift, int min, int max)
204{
205 // Perform validate step
206 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
207 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias != nullptr) ? bias->info() : nullptr, output->info(), min, max));
208
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
219 auto win_config = validate_and_configure_window(input->info(), output->info());
220 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 <= -128 && max >= 127);
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000225 _func = is_bounded_relu ? &NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::run<true> : &NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::run<false>;
226}
227
228Status NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
229{
230 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
231 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, min, max));
232 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
233
234 return Status{};
235}
236
237void NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::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);
244}
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100245} // namespace arm_compute