Compute Library
 21.11
CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 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"
35 #include "src/core/NEON/NEAsymm.h"
38 
39 #include <arm_neon.h>
40 
41 namespace arm_compute
42 {
43 namespace cpu
44 {
45 namespace kernels
46 {
47 namespace
48 {
49 Status validate_arguments(const ITensorInfo *src, const ITensorInfo *bias, const ITensorInfo *dst, int min, int max)
50 {
53  ARM_COMPUTE_RETURN_ERROR_ON(min > max);
54 
55  // Check biases if exist
56  if(bias != nullptr)
57  {
59  ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1);
60  ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(0) != bias->dimension(0));
61  }
62 
63  if(dst->total_size() != 0)
64  {
67  }
68 
69  return Status{};
70 }
71 } // namespace
72 
73 template <bool is_bounded_relu>
74 void CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run_internal(const ITensor *src, const ITensor *bias, ITensor *dst, const Window &window)
75 {
76  const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(_result_offset_after_shift);
77  const uint8x16_t min_u8 = vdupq_n_u8(static_cast<uint8_t>(_min));
78  const uint8x16_t max_u8 = vdupq_n_u8(static_cast<uint8_t>(_max));
79 
80  ARM_COMPUTE_UNUSED(min_u8);
81  ARM_COMPUTE_UNUSED(max_u8);
82 
83  const int window_step_x = 16;
84  const auto window_start_x = static_cast<int>(window.x().start());
85  const auto window_end_x = static_cast<int>(window.x().end());
86 
87  Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
88  win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
89 
90  Iterator in(src, win_collapsed);
91  Iterator out(dst, win_collapsed);
92  if(bias != nullptr)
93  {
94  Window win_biases;
95  win_biases.set(Window::DimX, Window::Dimension(0, 1, 1));
96  win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
97 
98  Iterator bias_i(bias, win_biases);
99  execute_window_loop(win_collapsed, [&](const Coordinates &)
100  {
101  // Compute 16 elements per iteration
102  int x = window_start_x;
103  for(; x <= (window_end_x - window_step_x); x += window_step_x)
104  {
105  int32x4x4_t in_s32 =
106  {
107  {
108  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
109  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
110  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
111  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
112  }
113  };
114 
115  const int32x4x4_t bias_s32 =
116  {
117  {
118  vld1q_s32(reinterpret_cast<const int32_t *>(bias_i.ptr()) + x + 0),
119  vld1q_s32(reinterpret_cast<const int32_t *>(bias_i.ptr()) + x + 4),
120  vld1q_s32(reinterpret_cast<const int32_t *>(bias_i.ptr()) + x + 8),
121  vld1q_s32(reinterpret_cast<const int32_t *>(bias_i.ptr()) + x + 12)
122  }
123  };
124 
125  // Add the bias to GEMM's result
126  in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]);
127  in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]);
128  in_s32.val[2] = vaddq_s32(in_s32.val[2], bias_s32.val[2]);
129  in_s32.val[3] = vaddq_s32(in_s32.val[3], bias_s32.val[3]);
130 
131  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));
132  }
133 
134  // Compute left-over elements
135  for(; x < window_end_x; ++x)
136  {
137  const int32_t bias_value = *(reinterpret_cast<const int32_t *>(bias_i.ptr()) + x);
138  int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
139 
140  // Add bias
141  in_value += bias_value;
142  // Finalize and store the result
143  *(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);
144  }
145  },
146  in, out, bias_i);
147  }
148  else
149  {
150  execute_window_loop(win_collapsed, [&](const Coordinates &)
151  {
152  // Compute 16 elements per iteration
153  int x = window_start_x;
154  for(; x <= (window_end_x - window_step_x); x += window_step_x)
155  {
156  int32x4x4_t in_s32 =
157  {
158  {
159  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
160  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
161  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
162  vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
163  }
164  };
165 
166  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));
167  }
168 
169  // Compute left-over elements
170  for(; x < window_end_x; ++x)
171  {
172  const int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
173 
174  // Finalize and store the result
175  *(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);
176  }
177  },
178  in, out);
179  }
180 }
181 
182 void CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::configure(ITensorInfo *src, ITensorInfo *bias, ITensorInfo *dst, int result_fixedpoint_multiplier, int result_shift,
183  int result_offset_after_shift, int min, int max)
184 {
185  ARM_COMPUTE_UNUSED(bias);
186  // Perform validate step
188  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, bias, dst, min, max));
189 
190  _result_fixedpoint_multiplier = result_fixedpoint_multiplier;
191  _result_shift = result_shift;
192  _result_offset_after_shift = result_offset_after_shift;
193  _min = min;
194  _max = max;
195 
196  // Output auto inizialitation if not yet initialized
197  auto_init_if_empty(*dst, src->clone()->set_data_type(DataType::QASYMM8));
198 
199  // Configure kernel window
200  auto win_config = calculate_max_window(*src, Steps());
201  ICpuKernel::configure(win_config);
202 
203  // Check if we need to clamp the result using min and max
204  const bool is_bounded_relu = !(min <= 0 && max >= 255);
205  _func = is_bounded_relu ? &CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run_internal<true> :
206  &CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run_internal<false>;
207 }
208 
210 {
212  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, bias, dst, min, max));
213  return Status{};
214 }
215 
217 {
218  ARM_COMPUTE_UNUSED(info);
221  ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No inputs provided");
222 
223  auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
224  auto bias = tensors.get_const_tensor(TensorType::ACL_BIAS);
225  auto dst = tensors.get_tensor(TensorType::ACL_DST);
226 
227  (this->*_func)(src, bias, dst, window);
228 }
229 
231 {
232  return "CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel";
233 }
234 } // namespace kernels
235 } // namespace cpu
236 } // namespace arm_compute
static Status validate(const ITensorInfo *src, const ITensorInfo *bias, const ITensorInfo *dst, int min=0, int max=0)
Static function to check if given info will lead to a valid configuration.
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
bool empty() const
Checks if pack is empty.
Definition: ITensorPack.cpp:80
#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
void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
Status class.
Definition: Error.h:52
void configure(ITensorInfo *src, ITensorInfo *bias, ITensorInfo *dst, int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift, int min=0, int max=0)
Initialise the kernel&#39;s input and output.
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 S32 per channel
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:54
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
quantized, asymmetric fixed-point 8-bit number unsigned
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
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.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
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)
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
Definition: ITensorPack.cpp:64
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:81
Information about executing thread and CPU.
Definition: CPPTypes.h:158
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:439
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:541
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
Tensor packing service.
Definition: ITensorPack.h:39
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
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:201