blob: 68f16c5fc7f7b34e7e8b06c51c4d295673e8cd95 [file] [log] [blame]
Gian Marco Iodicebc415af2019-06-13 15:58:32 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 Arm Limited.
Gian Marco Iodicebc415af2019-06-13 15:58:32 +01003 *
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/NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel.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"
Gian Marco Iodicebc415af2019-06-13 15:58:32 +010030#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Types.h"
32#include "arm_compute/core/Utils.h"
33#include "arm_compute/core/Validate.h"
34#include "arm_compute/core/Window.h"
35#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010036#include "src/core/NEON/NESymm.h"
Gian Marco Iodicebc415af2019-06-13 15:58:32 +010037
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);
Gian Marco Iodicebc415af2019-06-13 15:58:32 +010050
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::QSYMM16);
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 inizialitation if not yet initialized
71 auto_init_if_empty(*output, input->clone()->set_data_type(DataType::QSYMM16));
72
73 // Configure kernel window
74 Window win = calculate_max_window(*input, Steps());
75
76 // NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel 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
85class Coordinates;
86
87template <bool is_bounded_relu>
88void NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::run(const Window &window)
89{
90 const int16x8_t min_s16 = vdupq_n_s16(static_cast<int16_t>(_min));
91 const int16x8_t max_s16 = vdupq_n_s16(static_cast<int16_t>(_max));
92
93 ARM_COMPUTE_UNUSED(min_s16);
94 ARM_COMPUTE_UNUSED(max_s16);
95
96 const int window_step_x = 8;
97 const auto window_start_x = static_cast<int>(window.x().start());
98 const auto window_end_x = static_cast<int>(window.x().end());
99
100 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
101 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
102
103 Iterator in(_input, win_collapsed);
104 Iterator out(_output, win_collapsed);
105 if(_bias != nullptr)
106 {
107 Window win_biases;
108 win_biases.set(Window::DimX, Window::Dimension(0, 1, 1));
109 win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
110
111 Iterator bias(_bias, win_biases);
112 execute_window_loop(win_collapsed, [&](const Coordinates &)
113 {
114 // Compute 16 elements per iteration
115 int x = window_start_x;
116 for(; x <= (window_end_x - window_step_x); x += window_step_x)
117 {
118 int32x4x2_t in_s32 =
119 {
120 {
121 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
122 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4)
123 }
124 };
125
126 const int32x4x2_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 }
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
138 vst1q_s16(reinterpret_cast<int16_t *>(out.ptr()) + x, finalize_quantization_int16<is_bounded_relu>(in_s32, _result_fixedpoint_multiplier, _result_shift, min_s16, max_s16));
139 }
140
141 // Compute left-over elements
142 for(; x < window_end_x; ++x)
143 {
144 const int32_t bias_value = *(reinterpret_cast<const int32_t *>(bias.ptr()) + x);
145 int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
146
147 // Add bias
148 in_value += bias_value;
149 // Finalize and store the result
150 *(reinterpret_cast<int16_t *>(out.ptr()) + x) = finalize_quantization_int16<is_bounded_relu>(in_value, _result_fixedpoint_multiplier, _result_shift, static_cast<int16_t>(_min),
151 static_cast<int16_t>(_max));
152 }
153 },
154 in, out, bias);
155 }
156 else
157 {
158 execute_window_loop(win_collapsed, [&](const Coordinates &)
159 {
160 // Compute 16 elements per iteration
161 int x = window_start_x;
162 for(; x <= (window_end_x - window_step_x); x += window_step_x)
163 {
164 int32x4x2_t in_s32 =
165 {
166 {
167 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
168 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4)
169 }
170 };
171
172 vst1q_s16(reinterpret_cast<int16_t *>(out.ptr()) + x, finalize_quantization_int16<is_bounded_relu>(in_s32, _result_fixedpoint_multiplier, _result_shift, min_s16, max_s16));
173 }
174
175 // Compute left-over elements
176 for(; x < window_end_x; ++x)
177 {
178 const int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
179 ARM_COMPUTE_UNUSED(in_value);
180 // Finalize and store the result
181 *(reinterpret_cast<int16_t *>(out.ptr()) + x) = finalize_quantization_int16<is_bounded_relu>(in_value, _result_fixedpoint_multiplier, _result_shift, static_cast<int16_t>(_min),
182 static_cast<int16_t>(_max));
183 }
184 },
185 in, out);
186 }
187}
188
189NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel()
190 : _func(nullptr), _input(nullptr), _bias(nullptr), _output(nullptr), _result_fixedpoint_multiplier(0), _result_shift(0), _min(0), _max(0)
191{
192}
193
194void NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift,
195 int min, int max)
196{
197 // Perform validate step
198 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
199 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias != nullptr) ? bias->info() : nullptr, output->info(), min, max));
200
201 _input = input;
202 _bias = bias;
203 _output = output;
204 _result_fixedpoint_multiplier = result_fixedpoint_multiplier;
205 _result_shift = result_shift;
206 _min = min;
207 _max = max;
208
209 // Configure kernel window
210 auto win_config = validate_and_configure_window(input->info(), output->info());
211 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
212 INEKernel::configure(win_config.second);
213
214 // Check if we need to clamp the result using min and max
Giorgio Arena1856ff72020-02-07 13:46:45 +0000215 const bool is_bounded_relu = !(min <= -32768 && max >= 32767);
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100216 _func = is_bounded_relu ? &NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::run<true> : &NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::run<false>;
217}
218
219Status NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
220{
221 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
222 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, min, max));
223 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
224
225 return Status{};
226}
227
228void NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::run(const Window &window, const ThreadInfo &info)
229{
230 ARM_COMPUTE_UNUSED(info);
231 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
232 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
233
234 (this->*_func)(window);
235}
236} // namespace arm_compute