• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019-2020 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 "src/core/NEON/kernels/NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel.h"
25 
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/ITensor.h"
29 #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"
35 #include "src/core/AccessWindowStatic.h"
36 #include "src/core/NEON/NEAsymm.h"
37 #include "src/core/helpers/AutoConfiguration.h"
38 #include "src/core/helpers/WindowHelpers.h"
39 
40 #include <arm_neon.h>
41 #include <cstddef>
42 #include <cstdint>
43 
44 namespace arm_compute
45 {
46 namespace
47 {
validate_arguments(const ITensorInfo * input,const ITensorInfo * bias,const ITensorInfo * output,int min,int max)48 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
49 {
50     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S32);
51     ARM_COMPUTE_RETURN_ERROR_ON(min > max);
52 
53     // Check biases if exist
54     if(bias != nullptr)
55     {
56         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias);
57         ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1);
58         ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != bias->dimension(0));
59     }
60 
61     if(output->total_size() != 0)
62     {
63         ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8_SIGNED);
64         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, input);
65     }
66 
67     return Status{};
68 }
69 
validate_and_configure_window(ITensorInfo * input,ITensorInfo * output)70 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
71 {
72     // Output auto initialization if not yet initialized
73     auto_init_if_empty(*output, input->clone()->set_data_type(DataType::QASYMM8_SIGNED));
74 
75     // Configure kernel window
76     Window win = calculate_max_window(*input, Steps());
77 
78     // NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel doesn't need padding so update_window_and_padding() can be skipped
79     Coordinates coord;
80     coord.set_num_dimensions(output->num_dimensions());
81     output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
82 
83     return std::make_pair(Status{}, win);
84 }
85 } // namespace
86 
87 template <bool is_bounded_relu>
run(const Window & window)88 void NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::run(const Window &window)
89 {
90     const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(_result_offset_after_shift);
91     const int8x16_t min_s8                        = vdupq_n_s8(static_cast<int8_t>(_min));
92     const int8x16_t max_s8                        = vdupq_n_s8(static_cast<int8_t>(_max));
93 
94     ARM_COMPUTE_UNUSED(min_s8, max_s8);
95 
96     const int  window_step_x  = 16;
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                 int32x4x4_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                         vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
124                         vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
125                     }
126                 };
127 
128                 const int32x4x4_t bias_s32 =
129                 {
130                     {
131                         vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 0),
132                         vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 4),
133                         vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 8),
134                         vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 12)
135                     }
136                 };
137 
138                 // Add the bias to GEMM's result
139                 in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]);
140                 in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]);
141                 in_s32.val[2] = vaddq_s32(in_s32.val[2], bias_s32.val[2]);
142                 in_s32.val[3] = vaddq_s32(in_s32.val[3], bias_s32.val[3]);
143 
144                 vst1q_s8(reinterpret_cast<int8_t *>(out.ptr() + x),
145                          finalize_quantization(in_s32, _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, min_s8, max_s8, is_bounded_relu));
146             }
147 
148             // Compute left-over elements
149             for(; x < window_end_x; ++x)
150             {
151                 const int32_t bias_value = *(reinterpret_cast<const int32_t *>(bias.ptr()) + x);
152                 int32_t       in_value   = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
153 
154                 // Add bias
155                 in_value += bias_value;
156                 // Finalize and store the result
157                 *reinterpret_cast<int8_t *>(out.ptr() + x) = finalize_quantization(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift,
158                                                                                    static_cast<int8_t>(_min), static_cast<int8_t>(_max), is_bounded_relu);
159             }
160         },
161         in, out, bias);
162     }
163     else
164     {
165         execute_window_loop(win_collapsed, [&](const Coordinates &)
166         {
167             // Compute 16 elements per iteration
168             int x = window_start_x;
169             for(; x <= (window_end_x - window_step_x); x += window_step_x)
170             {
171                 int32x4x4_t in_s32 =
172                 {
173                     {
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                 };
180 
181                 vst1q_s8(reinterpret_cast<int8_t *>(out.ptr() + x),
182                          finalize_quantization(in_s32, _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, min_s8, max_s8, is_bounded_relu));
183             }
184 
185             // Compute left-over elements
186             for(; x < window_end_x; ++x)
187             {
188                 const int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
189 
190                 // Finalize and store the result
191                 *reinterpret_cast<int8_t *>(out.ptr() + x) = finalize_quantization(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift,
192                                                                                    static_cast<int8_t>(_min), static_cast<int8_t>(_max), is_bounded_relu);
193             }
194         },
195         in, out);
196     }
197 }
198 
NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel()199 NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel()
200     : _func(nullptr), _input(nullptr), _bias(nullptr), _output(nullptr), _result_fixedpoint_multiplier(0), _result_shift(0), _result_offset_after_shift(0), _min(0), _max(0)
201 {
202 }
203 
configure(const ITensor * input,const ITensor * bias,ITensor * output,int result_fixedpoint_multiplier,int result_shift,int result_offset_after_shift,int min,int max)204 void NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift,
205                                                                          int result_offset_after_shift, int min, int max)
206 {
207     // Perform validate step
208     ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
209     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias != nullptr) ? bias->info() : nullptr, output->info(), min, max));
210 
211     _input                        = input;
212     _bias                         = bias;
213     _output                       = output;
214     _result_fixedpoint_multiplier = result_fixedpoint_multiplier;
215     _result_shift                 = result_shift;
216     _result_offset_after_shift    = result_offset_after_shift;
217     _min                          = min;
218     _max                          = max;
219 
220     // Configure kernel window
221     auto win_config = validate_and_configure_window(input->info(), output->info());
222     ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
223     INEKernel::configure(win_config.second);
224 
225     // Check if we need to clamp the result using min and max
226     const bool is_bounded_relu = !(min <= -128 && max >= 127);
227     _func                      = is_bounded_relu ? &NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::run<true> : &NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::run<false>;
228 }
229 
validate(const ITensorInfo * input,const ITensorInfo * bias,const ITensorInfo * output,int min,int max)230 Status NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
231 {
232     ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
233     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, min, max));
234     ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
235 
236     return Status{};
237 }
238 
run(const Window & window,const ThreadInfo & info)239 void NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::run(const Window &window, const ThreadInfo &info)
240 {
241     ARM_COMPUTE_UNUSED(info);
242     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
243     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
244 
245     (this->*_func)(window);
246 }
247 } // namespace arm_compute
248