Compute Library
 21.11
CpuQuantizeKernel.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"
28 #include "arm_compute/core/Utils.h"
31 #include "src/core/NEON/NEAsymm.h"
32 #include "src/core/NEON/NEMath.h"
36 
37 #include "src/core/CPP/Validate.h"
38 
39 #include <arm_neon.h>
40 #include <map>
41 
42 namespace arm_compute
43 {
44 namespace cpu
45 {
46 namespace kernels
47 {
48 namespace
49 {
50 constexpr auto window_step = 16;
51 
52 Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst)
53 {
57  ARM_COMPUTE_RETURN_ERROR_ON(dst->tensor_shape().total_size() == 0);
60 
61  return Status{};
62 }
63 
64 template <typename T>
65 inline float32x4x4_t load_value(const T *input_ptr)
66 {
67  using Tx16_t = typename wrapper::traits::neon_vector<T, 16>::type;
68  return arm_compute::convert_to_float32x4x4<Tx16_t>(wrapper::vloadq(input_ptr));
69 }
70 
71 template <>
72 inline float32x4x4_t load_value(const float *input_ptr)
73 {
74  return { wrapper::vloadq(input_ptr),
75  wrapper::vloadq(input_ptr + 4),
76  wrapper::vloadq(input_ptr + 8),
77  wrapper::vloadq(input_ptr + 12) };
78 }
79 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
80 template <>
81 inline float32x4x4_t load_value(const float16_t *input_ptr)
82 {
83  return { vcvt_f32_f16(wrapper::vload(input_ptr)),
84  vcvt_f32_f16(wrapper::vload(input_ptr + 4)),
85  vcvt_f32_f16(wrapper::vload(input_ptr + 8)),
86  vcvt_f32_f16(wrapper::vload(input_ptr + 12)) };
87 }
88 
89 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
90 
91 template <typename element_type>
92 using vector_type = wrapper::traits::neon_vector_t<element_type, window_step>;
93 
94 template <typename quantized_type>
95 vector_type<quantized_type> vquantize_qasymm8(const float32x4x4_t &qv, const UniformQuantizationInfo &qi);
96 
97 template <>
98 vector_type<uint8_t> vquantize_qasymm8<uint8_t>(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
99 {
100  return vquantize(qv, qi);
101 }
102 
103 template <>
104 vector_type<int8_t> vquantize_qasymm8<int8_t>(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
105 {
106  return vquantize_signed(qv, qi);
107 }
108 
109 } // namespace
110 
112 {
114  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst));
115 
116  static const std::map<std::string, QuantizeFunctionExecutorPtr> quant_map =
117  {
118  { "op_QASYMM8_QASYMM8", &CpuQuantizeKernel::run_quantize_qasymm8<uint8_t, uint8_t> },
119  { "op_QASYMM8_QASYMM8_SIGNED", &CpuQuantizeKernel::run_quantize_qasymm8<uint8_t, int8_t> },
120  { "op_QASYMM8_QASYMM16", &CpuQuantizeKernel::run_quantize_qasymm16<uint8_t> },
121 
122  { "op_QASYMM8_SIGNED_QASYMM8", &CpuQuantizeKernel::run_quantize_qasymm8<int8_t, uint8_t> },
123  { "op_QASYMM8_SIGNED_QASYMM8_SIGNED", &CpuQuantizeKernel::run_quantize_qasymm8<int8_t, int8_t> },
124  { "op_QASYMM8_SIGNED_QASYMM16", &CpuQuantizeKernel::run_quantize_qasymm16<int8_t> },
125 
126  { "op_F32_QASYMM8", &CpuQuantizeKernel::run_quantize_qasymm8<float, uint8_t> },
127  { "op_F32_QASYMM8_SIGNED", &CpuQuantizeKernel::run_quantize_qasymm8<float, int8_t> },
128  { "op_F32_QASYMM16", &CpuQuantizeKernel::run_quantize_qasymm16<float> },
129 
130 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
131  { "op_F16_QASYMM8", &CpuQuantizeKernel::run_quantize_qasymm8<float16_t, uint8_t> },
132  { "op_F16_QASYMM8_SIGNED", &CpuQuantizeKernel::run_quantize_qasymm8<float16_t, int8_t> },
133  { "op_F16_QASYMM16", &CpuQuantizeKernel::run_quantize_qasymm16<float16_t> },
134 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC*/
135  };
136 
137  std::string function_to_call("op_");
138  function_to_call += string_from_data_type(src->data_type()) + "_";
139  function_to_call += string_from_data_type(dst->data_type());
140 
141  auto it = quant_map.find(function_to_call);
142 
143  if(it == quant_map.end())
144  {
145  ARM_COMPUTE_ERROR("Unsupported combination of input and output data types");
146  }
147  _func = it->second;
148 
149  // Configure kernel window
150  Window win_config = calculate_max_window(*src, Steps());
151  ICpuKernel::configure(win_config);
152 }
153 
155 {
156  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst));
157  return Status{};
158 }
159 
160 template <typename TIn, typename TOut>
161 void CpuQuantizeKernel::run_quantize_qasymm8(const ITensor *src, ITensor *dst, const Window &window)
162 {
163  const auto window_start_x = static_cast<int>(window.x().start());
164  const auto window_end_x = static_cast<int>(window.x().end());
165 
166  const UniformQuantizationInfo uqinfo_in = src->info()->quantization_info().uniform();
169  {
170  uqinfo = compute_requantization_scale_offset(uqinfo_in, uqinfo);
171  }
172 #ifdef __aarch64__
173  constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_EVEN;
174 #else //__aarch64__
175  constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_ZERO;
176 #endif //__aarch64__
177 
178  // Collapse window and reset first dimension to handle tail calculations manually
179  Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
180  win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
181 
182  Iterator input(src, win_collapsed);
183  Iterator output(dst, win_collapsed);
184  execute_window_loop(win_collapsed, [&](const Coordinates &)
185  {
186  auto input_ptr = reinterpret_cast<const TIn *>(input.ptr());
187  auto output_ptr = reinterpret_cast<TOut *>(output.ptr());
188 
189  int x = window_start_x;
190  for(; x <= (window_end_x - window_step); x += window_step)
191  {
192  wrapper::vstore(&output_ptr[x], vquantize_qasymm8<TOut>(load_value(&input_ptr[x]), uqinfo));
193  }
194  // Compute left-over elements
195  for(; x < window_end_x; ++x)
196  {
197  output_ptr[x] = Qasymm8QuantizationHelper<TOut>::quantize(input_ptr[x], uqinfo, rounding_policy);
198  }
199  },
200  input, output);
201 }
202 
203 template <typename T>
204 void CpuQuantizeKernel::run_quantize_qasymm16(const ITensor *src, ITensor *dst, const Window &window)
205 {
206  const auto window_start_x = static_cast<int>(window.x().start());
207  const auto window_end_x = static_cast<int>(window.x().end());
208 
209  const UniformQuantizationInfo uqinfo_in = src->info()->quantization_info().uniform();
212  {
213  uqinfo = compute_requantization_scale_offset(uqinfo_in, uqinfo);
214  }
215 #ifdef __aarch64__
216  constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_EVEN;
217 #else //__aarch64__
218  constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_ZERO;
219 #endif //__aarch64__
220 
221  // Collapse window and reset first dimension to handle tail calculations manually
222  Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
223  win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
224 
225  Iterator input(src, win_collapsed);
226  Iterator output(dst, win_collapsed);
227  execute_window_loop(win_collapsed, [&](const Coordinates &)
228  {
229  auto input_ptr = reinterpret_cast<const T *>(input.ptr());
230  auto output_ptr = reinterpret_cast<uint16_t *>(output.ptr());
231 
232  int x = window_start_x;
233  for(; x <= (window_end_x - window_step); x += window_step)
234  {
235  uint16x8x2_t tmp = vquantize_qasymm16(load_value(&input_ptr[x]), uqinfo);
236  vst1q_u16(&output_ptr[x], tmp.val[0]);
237  vst1q_u16(&output_ptr[x + 8], tmp.val[1]);
238  }
239  // Compute left-over elements
240  for(; x < window_end_x; ++x)
241  {
242  output_ptr[x] = quantize_qasymm16(input_ptr[x], uqinfo, rounding_policy);
243  }
244  },
245  input, output);
246 }
247 
248 void CpuQuantizeKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
249 {
250  ARM_COMPUTE_UNUSED(info);
253  ARM_COMPUTE_ERROR_ON(_func == nullptr);
254 
255  const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
256  auto dst = tensors.get_tensor(TensorType::ACL_DST);
257  (this->*_func)(src, dst, window);
258 }
259 
260 const char *CpuQuantizeKernel::name() const
261 {
262  return "CpuQuantizeKernel";
263 }
264 } // namespace kernels
265 } // namespace cpu
266 } // namespace arm_compute
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
uint16x8x2_t vquantize_qasymm16(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
Quantize to QASYMM16 a neon vector holding 16 floating point values.
Definition: NEAsymm.h:711
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:115
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
uint8x16_t vloadq(const uint8_t *ptr)
Definition: load.h:58
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
virtual DataType data_type() const =0
Data type used for each element of the tensor.
void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Quantization info when assuming per layer quantization.
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
quantized, asymmetric fixed-point 16-bit number
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
decltype(strategy::transforms) typedef type
Interface for CPU tensor.
Definition: ITensor.h:36
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
static QUANTIZED_TYPE quantize(float value, const UniformQuantizationInfo &qinfo)
Quantize a value given a 8-bit asymmetric quantization scheme.
1 channel, 1 F16 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:54
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
Definition: Utils.cpp:135
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
static Status validate(const ITensorInfo *src, const ITensorInfo *dst)
Static function to check if given info will lead to a valid configuration.
quantized, asymmetric fixed-point 8-bit number unsigned
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
UniformQuantizationInfo uniform() const
Return per layer quantization info.
RoundingPolicy
Rounding method.
Definition: Rounding.h:30
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
UniformQuantizationInfo compute_requantization_scale_offset(const UniformQuantizationInfo &uqinfo_in, const UniformQuantizationInfo &uqinfo_out)
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: Utils.h:1003
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
Rounds to nearest value; half rounds to nearest even.
Information about executing thread and CPU.
Definition: CPPTypes.h:158
const char * name() const override
Name of the kernel.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:439
void configure(const ITensorInfo *src, ITensorInfo *dst)
Set the input, output.
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
uint8x8_t vquantize(const float32x4x2_t &qv, const UniformQuantizationInfo &qi)
Quantize a neon vector holding 8 floating point values.
Definition: NEAsymm.h:602
uint8x8_t vload(const uint8_t *ptr)
Definition: load.h:39
void vstore(uint8_t *ptr, uint8x8_t val)
Definition: store.h:39
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
quantized, asymmetric fixed-point 8-bit number signed
Includes all wrapper headers at once.
int8x8_t vquantize_signed(const float32x4x2_t &qv, const UniformQuantizationInfo &qi)
Quantize a neon vector holding 8 floating point values.
Definition: NEAsymm.h:630
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:99
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
Truncates the least significant values that are lost in operations.
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:94
Describe a multidimensional execution window.
Definition: Window.h:39
uint16_t quantize_qasymm16(float value, const UniformQuantizationInfo &qinfo, RoundingPolicy rounding_policy=RoundingPolicy::TO_NEAREST_UP)
Quantize a value given a 16-bit asymmetric quantization scheme.
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:145