blob: d24089d6155064afdea2b68c5c05defc8309b49c [file] [log] [blame]
Georgios Pinitas448a81f2019-11-21 14:10:25 +00001/*
2 * Copyright (c) 2019 ARM Limited.
3 *
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);
49 ARM_COMPUTE_RETURN_ERROR_ON(max > 127);
50 ARM_COMPUTE_RETURN_ERROR_ON(min < -128 || 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 }
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
78 Coordinates coord;
79 coord.set_num_dimensions(output->num_dimensions());
80 output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
81
82 return std::make_pair(Status{}, win);
83}
84} // namespace
85
86template <bool is_bounded_relu>
87void NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::run(const Window &window)
88{
89 const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(_result_offset_after_shift);
90 const int8x16_t min_s8 = vdupq_n_s8(static_cast<int8_t>(_min));
91 const int8x16_t max_s8 = vdupq_n_s8(static_cast<int8_t>(_max));
92
93 ARM_COMPUTE_UNUSED(min_s8, max_s8);
94
95 const int window_step_x = 16;
96 const auto window_start_x = static_cast<int>(window.x().start());
97 const auto window_end_x = static_cast<int>(window.x().end());
98
99 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
100 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
101
102 Iterator in(_input, win_collapsed);
103 Iterator out(_output, win_collapsed);
104 if(_bias != nullptr)
105 {
106 Window win_biases;
107 win_biases.set(Window::DimX, Window::Dimension(0, 1, 1));
108 win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
109
110 Iterator bias(_bias, win_biases);
111 execute_window_loop(win_collapsed, [&](const Coordinates &)
112 {
113 // Compute 16 elements per iteration
114 int x = window_start_x;
115 for(; x <= (window_end_x - window_step_x); x += window_step_x)
116 {
117 int32x4x4_t in_s32 =
118 {
119 {
120 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
121 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
122 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
123 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
124 }
125 };
126
127 const int32x4x4_t bias_s32 =
128 {
129 {
130 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 0),
131 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 4),
132 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 8),
133 vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 12)
134 }
135 };
136
137 // Add the bias to GEMM's result
138 in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]);
139 in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]);
140 in_s32.val[2] = vaddq_s32(in_s32.val[2], bias_s32.val[2]);
141 in_s32.val[3] = vaddq_s32(in_s32.val[3], bias_s32.val[3]);
142
143 vst1q_s8(reinterpret_cast<int8_t *>(out.ptr() + x),
144 finalize_quantization<is_bounded_relu>(in_s32, _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, min_s8, max_s8));
145 }
146
147 // Compute left-over elements
148 for(; x < window_end_x; ++x)
149 {
150 const int32_t bias_value = *(reinterpret_cast<const int32_t *>(bias.ptr()) + x);
151 int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
152
153 // Add bias
154 in_value += bias_value;
155 // Finalize and store the result
156 *reinterpret_cast<int8_t *>(out.ptr() + x) = finalize_quantization<is_bounded_relu>(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift,
157 static_cast<int8_t>(_min), static_cast<int8_t>(_max));
158 }
159 },
160 in, out, bias);
161 }
162 else
163 {
164 execute_window_loop(win_collapsed, [&](const Coordinates &)
165 {
166 // Compute 16 elements per iteration
167 int x = window_start_x;
168 for(; x <= (window_end_x - window_step_x); x += window_step_x)
169 {
170 int32x4x4_t in_s32 =
171 {
172 {
173 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
174 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
175 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
176 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
177 }
178 };
179
180 vst1q_s8(reinterpret_cast<int8_t *>(out.ptr() + x),
181 finalize_quantization<is_bounded_relu>(in_s32, _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, min_s8, max_s8));
182 }
183
184 // Compute left-over elements
185 for(; x < window_end_x; ++x)
186 {
187 const int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
188
189 // Finalize and store the result
190 *reinterpret_cast<int8_t *>(out.ptr() + x) = finalize_quantization<is_bounded_relu>(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift,
191 static_cast<int8_t>(_min), static_cast<int8_t>(_max));
192 }
193 },
194 in, out);
195 }
196}
197
198NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel()
199 : _func(nullptr), _input(nullptr), _bias(nullptr), _output(nullptr), _result_fixedpoint_multiplier(0), _result_shift(0), _result_offset_after_shift(0), _min(0), _max(0)
200{
201}
202
203void NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift,
204 int result_offset_after_shift, int min, int max)
205{
206 // Perform validate step
207 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
208 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias != nullptr) ? bias->info() : nullptr, output->info(), min, max));
209
210 _input = input;
211 _bias = bias;
212 _output = output;
213 _result_fixedpoint_multiplier = result_fixedpoint_multiplier;
214 _result_shift = result_shift;
215 _result_offset_after_shift = result_offset_after_shift;
216 _min = min;
217 _max = max;
218
219 // Configure kernel window
220 auto win_config = validate_and_configure_window(input->info(), output->info());
221 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
222 INEKernel::configure(win_config.second);
223
224 // Check if we need to clamp the result using min and max
225 const bool is_bounded_relu = ((min != max) && !(min == -128 && max == 127));
226 _func = is_bounded_relu ? &NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::run<true> : &NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::run<false>;
227}
228
229Status NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
230{
231 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
232 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, min, max));
233 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
234
235 return Status{};
236}
237
238void NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::run(const Window &window, const ThreadInfo &info)
239{
240 ARM_COMPUTE_UNUSED(info);
241 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
242 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
243
244 (this->*_func)(window);
245}
246} // namespace arm_compute