Compute Library
 20.08
NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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 
27 #include "arm_compute/core/Error.h"
32 #include "arm_compute/core/Types.h"
33 #include "arm_compute/core/Utils.h"
37 
38 #include <arm_neon.h>
39 #include <cstddef>
40 #include <cstdint>
41 
42 namespace arm_compute
43 {
44 namespace
45 {
46 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
47 {
49  ARM_COMPUTE_RETURN_ERROR_ON(min > max);
50 
51  // Check biases if exist
52  if(bias != nullptr)
53  {
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  {
63  }
64 
65  return Status{};
66 }
67 
68 std::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::QASYMM8));
72 
73  // Configure kernel window
74  Window win = calculate_max_window(*input, Steps());
75 
76  // NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel 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 
85 namespace arm_compute
86 {
87 class Coordinates;
88 } // namespace arm_compute
89 
90 template <bool is_bounded_relu>
92 {
93  const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(_result_offset_after_shift);
94  const uint8x16_t min_u8 = vdupq_n_u8(static_cast<uint8_t>(_min));
95  const uint8x16_t max_u8 = vdupq_n_u8(static_cast<uint8_t>(_max));
96 
97  ARM_COMPUTE_UNUSED(min_u8);
98  ARM_COMPUTE_UNUSED(max_u8);
99 
100  const int window_step_x = 16;
101  const auto window_start_x = static_cast<int>(window.x().start());
102  const auto window_end_x = static_cast<int>(window.x().end());
103 
105  win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
106 
107  Iterator in(_input, win_collapsed);
108  Iterator out(_output, win_collapsed);
109  if(_bias != nullptr)
110  {
111  Window win_biases;
112  win_biases.set(Window::DimX, Window::Dimension(0, 1, 1));
113  win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
114 
115  Iterator bias(_bias, win_biases);
116  execute_window_loop(win_collapsed, [&](const Coordinates &)
117  {
118  // Compute 16 elements per iteration
119  int x = window_start_x;
120  for(; x <= (window_end_x - window_step_x); x += window_step_x)
121  {
122  int32x4x4_t in_s32 =
123  {
124  {
125  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
126  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
127  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
128  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
129  }
130  };
131 
132  const int32x4x4_t bias_s32 =
133  {
134  {
135  vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 0),
136  vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 4),
137  vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 8),
138  vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 12)
139  }
140  };
141 
142  // Add the bias to GEMM's result
143  in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]);
144  in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]);
145  in_s32.val[2] = vaddq_s32(in_s32.val[2], bias_s32.val[2]);
146  in_s32.val[3] = vaddq_s32(in_s32.val[3], bias_s32.val[3]);
147 
148  vst1q_u8(out.ptr() + x, finalize_quantization(in_s32, _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, min_u8, max_u8, is_bounded_relu));
149  }
150 
151  // Compute left-over elements
152  for(; x < window_end_x; ++x)
153  {
154  const int32_t bias_value = *(reinterpret_cast<const int32_t *>(bias.ptr()) + x);
155  int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
156 
157  // Add bias
158  in_value += bias_value;
159  // Finalize and store the result
160  *(out.ptr() + x) = finalize_quantization(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift, static_cast<uint8_t>(_min), static_cast<uint8_t>(_max), is_bounded_relu);
161  }
162  },
163  in, out, bias);
164  }
165  else
166  {
167  execute_window_loop(win_collapsed, [&](const Coordinates &)
168  {
169  // Compute 16 elements per iteration
170  int x = window_start_x;
171  for(; x <= (window_end_x - window_step_x); x += window_step_x)
172  {
173  int32x4x4_t in_s32 =
174  {
175  {
176  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
177  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
178  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
179  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
180  }
181  };
182 
183  vst1q_u8(out.ptr() + x, finalize_quantization(in_s32, _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, min_u8, max_u8, is_bounded_relu));
184  }
185 
186  // Compute left-over elements
187  for(; x < window_end_x; ++x)
188  {
189  const int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
190 
191  // Finalize and store the result
192  *(out.ptr() + x) = finalize_quantization(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift, static_cast<uint8_t>(_min), static_cast<uint8_t>(_max), is_bounded_relu);
193  }
194  },
195  in, out);
196  }
197 }
198 
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 
204 void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::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
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 <= 0 && max >= 255);
227  _func = is_bounded_relu ? &NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run<true> : &NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run<false>;
228 }
229 
231 {
234  ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
235 
236  return Status{};
237 }
238 
240 {
244 
245  (this->*_func)(window);
246 }
247 } // namespace arm_compute
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
void configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift, int min=0, int max=0)
Initialise the kernel's input and output.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
Store the tensor's metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:75
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
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
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps=Steps(), bool skip_border=false, BorderSize border_size=BorderSize())
Calculate the maximum window for a given tensor shape and border setting.
Definition: Helpers.cpp:28
Copyright (c) 2017-2020 Arm Limited.
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...
Definition: Helpers.inl:207
ITensorInfo * info() const override
Interface to be implemented by the child class to return the tensor's metadata.
Definition: Tensor.cpp:33
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
Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed=nullptr) const
Collapse the dimensions between first and last if possible.
Definition: Window.inl:68
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:443
quantized, asymmetric fixed-point 8-bit number unsigned
Coordinates of an item.
Definition: Coordinates.h:37
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...
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's metadata.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
uint8x16_t finalize_quantization(int32x4x4_t &in_s32, int result_fixedpoint_multiplier, int32_t result_shift, int32x4_t result_offset_after_shift_s32, uint8x16_t min_u8, uint8x16_t max_u8, bool is_bounded_relu)
Performs final quantization step on 16 elements.
Definition: NEAsymm.h:80
Information about executing thread and CPU.
Definition: CPPTypes.h:235
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
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:128
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:97
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:353
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:92
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:143