Compute Library
 21.02
NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel.cpp
Go to the documentation of this file.
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  */
25 
26 #include "arm_compute/core/Error.h"
30 #include "arm_compute/core/Types.h"
31 #include "arm_compute/core/Utils.h"
36 #include "src/core/NEON/NESymm.h"
39 
40 #include <arm_neon.h>
41 #include <cstddef>
42 #include <cstdint>
43 
44 namespace arm_compute
45 {
46 namespace
47 {
48 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
49 {
51  ARM_COMPUTE_RETURN_ERROR_ON(min > max);
52 
53  // Check biases if exist
54  if(bias != nullptr)
55  {
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  {
65  }
66 
67  return Status{};
68 }
69 
70 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
71 {
72  // Output auto inizialitation if not yet initialized
73  auto_init_if_empty(*output, input->clone()->set_data_type(DataType::QSYMM16));
74 
75  // Configure kernel window
76  Window win = calculate_max_window(*input, Steps());
77 
78  // NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel 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 class Coordinates;
88 
89 template <bool is_bounded_relu>
91 {
92  const int16x8_t min_s16 = vdupq_n_s16(static_cast<int16_t>(_min));
93  const int16x8_t max_s16 = vdupq_n_s16(static_cast<int16_t>(_max));
94 
95  ARM_COMPUTE_UNUSED(min_s16);
96  ARM_COMPUTE_UNUSED(max_s16);
97 
98  const int window_step_x = 8;
99  const auto window_start_x = static_cast<int>(window.x().start());
100  const auto window_end_x = static_cast<int>(window.x().end());
101 
102  Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
103  win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
104 
105  Iterator in(_input, win_collapsed);
106  Iterator out(_output, win_collapsed);
107  if(_bias != nullptr)
108  {
109  Window win_biases;
110  win_biases.set(Window::DimX, Window::Dimension(0, 1, 1));
111  win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
112 
113  Iterator bias(_bias, win_biases);
114  execute_window_loop(win_collapsed, [&](const Coordinates &)
115  {
116  // Compute 16 elements per iteration
117  int x = window_start_x;
118  for(; x <= (window_end_x - window_step_x); x += window_step_x)
119  {
120  int32x4x2_t in_s32 =
121  {
122  {
123  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
124  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4)
125  }
126  };
127 
128  const int32x4x2_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  }
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 
140  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));
141  }
142 
143  // Compute left-over elements
144  for(; x < window_end_x; ++x)
145  {
146  const int32_t bias_value = *(reinterpret_cast<const int32_t *>(bias.ptr()) + x);
147  int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
148 
149  // Add bias
150  in_value += bias_value;
151  // Finalize and store the result
152  *(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),
153  static_cast<int16_t>(_max));
154  }
155  },
156  in, out, bias);
157  }
158  else
159  {
160  execute_window_loop(win_collapsed, [&](const Coordinates &)
161  {
162  // Compute 16 elements per iteration
163  int x = window_start_x;
164  for(; x <= (window_end_x - window_step_x); x += window_step_x)
165  {
166  int32x4x2_t in_s32 =
167  {
168  {
169  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
170  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4)
171  }
172  };
173 
174  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));
175  }
176 
177  // Compute left-over elements
178  for(; x < window_end_x; ++x)
179  {
180  const int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
181  ARM_COMPUTE_UNUSED(in_value);
182  // Finalize and store the result
183  *(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),
184  static_cast<int16_t>(_max));
185  }
186  },
187  in, out);
188  }
189 }
190 
192  : _func(nullptr), _input(nullptr), _bias(nullptr), _output(nullptr), _result_fixedpoint_multiplier(0), _result_shift(0), _min(0), _max(0)
193 {
194 }
195 
196 void NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift,
197  int min, int max)
198 {
199  // Perform validate step
200  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
201  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias != nullptr) ? bias->info() : nullptr, output->info(), min, max));
202 
203  _input = input;
204  _bias = bias;
205  _output = output;
206  _result_fixedpoint_multiplier = result_fixedpoint_multiplier;
207  _result_shift = result_shift;
208  _min = min;
209  _max = max;
210 
211  // Configure kernel window
212  auto win_config = validate_and_configure_window(input->info(), output->info());
213  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
214  INEKernel::configure(win_config.second);
215 
216  // Check if we need to clamp the result using min and max
217  const bool is_bounded_relu = !(min <= -32768 && max >= 32767);
218  _func = is_bounded_relu ? &NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::run<true> : &NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::run<false>;
219 }
220 
222 {
223  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
224  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, min, max));
225  ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
226 
227  return Status{};
228 }
229 
231 {
232  ARM_COMPUTE_UNUSED(info);
235 
236  (this->*_func)(window);
237 }
238 } // namespace arm_compute
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
static Status validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min=0, int max=0)
Static function to check if given info will lead to a valid configuration of NEGEMMLowpQuantizeDownIn...
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
quantized, symmetric fixed-point 16-bit number
void configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift, int min=0, int max=0)
Initialise the kernel&#39;s input and output.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
Interface for Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 S32 per channel
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:235
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:443
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205