Compute Library
 21.02
NEQuantizationLayerKernel.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 
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
45 {
46 constexpr auto window_step = 16;
47 
48 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
49 {
53  ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape().total_size() == 0);
56 
57  return Status{};
58 }
59 
60 template <typename T>
61 inline float32x4x4_t load_value(const T *input_ptr)
62 {
63  using Tx16_t = typename wrapper::traits::neon_vector<T, 16>::type;
64  return arm_compute::convert_to_float32x4x4<Tx16_t>(wrapper::vloadq(input_ptr));
65 }
66 
67 template <>
68 inline float32x4x4_t load_value(const float *input_ptr)
69 {
70  return { wrapper::vloadq(input_ptr),
71  wrapper::vloadq(input_ptr + 4),
72  wrapper::vloadq(input_ptr + 8),
73  wrapper::vloadq(input_ptr + 12) };
74 }
75 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
76 template <>
77 inline float32x4x4_t load_value(const float16_t *input_ptr)
78 {
79  return { vcvt_f32_f16(wrapper::vload(input_ptr)),
80  vcvt_f32_f16(wrapper::vload(input_ptr + 4)),
81  vcvt_f32_f16(wrapper::vload(input_ptr + 8)),
82  vcvt_f32_f16(wrapper::vload(input_ptr + 12)) };
83 }
84 
85 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
86 
87 template <typename element_type>
88 using vector_type = wrapper::traits::neon_vector_t<element_type, window_step>;
89 
90 template <typename quantized_type>
91 vector_type<quantized_type> vquantize_qasymm8(const float32x4x4_t &qv, const UniformQuantizationInfo &qi);
92 
93 template <>
94 vector_type<uint8_t> vquantize_qasymm8<uint8_t>(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
95 {
96  return vquantize(qv, qi);
97 }
98 
99 template <>
100 vector_type<int8_t> vquantize_qasymm8<int8_t>(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
101 {
102  return vquantize_signed(qv, qi);
103 }
104 
105 } // namespace
106 
108  : _input(nullptr), _output(nullptr), _func(nullptr)
109 {
110 }
111 
113 {
114  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
116 
117  _input = input;
118  _output = output;
119 
120  static const std::map<std::string, QuantizationFunctionExecutorPtr> quant_map =
121  {
122  { "op_QASYMM8_QASYMM8", &NEQuantizationLayerKernel::run_quantize_qasymm8<uint8_t, uint8_t> },
123  { "op_QASYMM8_QASYMM8_SIGNED", &NEQuantizationLayerKernel::run_quantize_qasymm8<uint8_t, int8_t> },
124  { "op_QASYMM8_QASYMM16", &NEQuantizationLayerKernel::run_quantize_qasymm16<uint8_t> },
125 
126  { "op_QASYMM8_SIGNED_QASYMM8", &NEQuantizationLayerKernel::run_quantize_qasymm8<int8_t, uint8_t> },
127  { "op_QASYMM8_SIGNED_QASYMM8_SIGNED", &NEQuantizationLayerKernel::run_quantize_qasymm8<int8_t, int8_t> },
128  { "op_QASYMM8_SIGNED_QASYMM16", &NEQuantizationLayerKernel::run_quantize_qasymm16<int8_t> },
129 
130  { "op_F32_QASYMM8", &NEQuantizationLayerKernel::run_quantize_qasymm8<float, uint8_t> },
131  { "op_F32_QASYMM8_SIGNED", &NEQuantizationLayerKernel::run_quantize_qasymm8<float, int8_t> },
132  { "op_F32_QASYMM16", &NEQuantizationLayerKernel::run_quantize_qasymm16<float> },
133 
134 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
135  { "op_F16_QASYMM8", &NEQuantizationLayerKernel::run_quantize_qasymm8<float16_t, uint8_t> },
136  { "op_F16_QASYMM8_SIGNED", &NEQuantizationLayerKernel::run_quantize_qasymm8<float16_t, int8_t> },
137  { "op_F16_QASYMM16", &NEQuantizationLayerKernel::run_quantize_qasymm16<float16_t> },
138 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC*/
139  };
140 
141  std::string function_to_call("op_");
142  function_to_call += string_from_data_type(_input->info()->data_type()) + "_";
143  function_to_call += string_from_data_type(_output->info()->data_type());
144 
145  auto it = quant_map.find(function_to_call);
146 
147  if(it == quant_map.end())
148  {
149  ARM_COMPUTE_ERROR("Unsupported combination of input and output data types");
150  }
151  _func = it->second;
152 
153  // Configure kernel window
154  Window win_config = calculate_max_window(*input->info(), Steps());
155 
156  Coordinates coord;
157  coord.set_num_dimensions(output->info()->num_dimensions());
158  output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
159 
160  INEKernel::configure(win_config);
161 }
162 
164 {
166  return Status{};
167 }
168 
169 template <typename TIn, typename TOut>
170 void NEQuantizationLayerKernel::run_quantize_qasymm8(const Window &window)
171 {
172  const auto window_start_x = static_cast<int>(window.x().start());
173  const auto window_end_x = static_cast<int>(window.x().end());
174 
175  const UniformQuantizationInfo uqinfo_in = _input->info()->quantization_info().uniform();
176  UniformQuantizationInfo uqinfo = _output->info()->quantization_info().uniform();
178  {
179  uqinfo = compute_requantization_scale_offset(uqinfo_in, uqinfo);
180  }
181 #ifdef __aarch64__
182  constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_EVEN;
183 #else //__aarch64__
184  constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_ZERO;
185 #endif //__aarch64__
186 
187  // Collapse window and reset first dimension to handle tail calculations manually
188  Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
189  win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
190 
191  Iterator input(_input, win_collapsed);
192  Iterator output(_output, win_collapsed);
193  execute_window_loop(win_collapsed, [&](const Coordinates &)
194  {
195  auto input_ptr = reinterpret_cast<const TIn *>(input.ptr());
196  auto output_ptr = reinterpret_cast<TOut *>(output.ptr());
197 
198  int x = window_start_x;
199  for(; x <= (window_end_x - window_step); x += window_step)
200  {
201  wrapper::vstore(&output_ptr[x], vquantize_qasymm8<TOut>(load_value(&input_ptr[x]), uqinfo));
202  }
203  // Compute left-over elements
204  for(; x < window_end_x; ++x)
205  {
206  output_ptr[x] = Qasymm8QuantizationHelper<TOut>::quantize(input_ptr[x], uqinfo, rounding_policy);
207  }
208  },
209  input, output);
210 }
211 
212 template <typename T>
213 void NEQuantizationLayerKernel::run_quantize_qasymm16(const Window &window)
214 {
215  const auto window_start_x = static_cast<int>(window.x().start());
216  const auto window_end_x = static_cast<int>(window.x().end());
217 
218  const UniformQuantizationInfo uqinfo_in = _input->info()->quantization_info().uniform();
219  UniformQuantizationInfo uqinfo = _output->info()->quantization_info().uniform();
221  {
222  uqinfo = compute_requantization_scale_offset(uqinfo_in, uqinfo);
223  }
224 #ifdef __aarch64__
225  constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_EVEN;
226 #else //__aarch64__
227  constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_ZERO;
228 #endif //__aarch64__
229 
230  // Collapse window and reset first dimension to handle tail calculations manually
231  Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
232  win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
233 
234  Iterator input(_input, win_collapsed);
235  Iterator output(_output, win_collapsed);
236  execute_window_loop(win_collapsed, [&](const Coordinates &)
237  {
238  auto input_ptr = reinterpret_cast<const T *>(input.ptr());
239  auto output_ptr = reinterpret_cast<uint16_t *>(output.ptr());
240 
241  int x = window_start_x;
242  for(; x <= (window_end_x - window_step); x += window_step)
243  {
244  uint16x8x2_t tmp = vquantize_qasymm16(load_value(&input_ptr[x]), uqinfo);
245  vst1q_u16(&output_ptr[x], tmp.val[0]);
246  vst1q_u16(&output_ptr[x + 8], tmp.val[1]);
247  }
248  // Compute left-over elements
249  for(; x < window_end_x; ++x)
250  {
251  output_ptr[x] = quantize_qasymm16(input_ptr[x], uqinfo, rounding_policy);
252  }
253  },
254  input, output);
255 }
256 
258 {
259  ARM_COMPUTE_UNUSED(info);
262  ARM_COMPUTE_ERROR_ON(_func == nullptr);
263 
264  (this->*_func)(window);
265 }
266 } // namespace arm_compute
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
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:108
void configure(const ITensor *input, ITensor *output)
Set the input, output.
#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.
1 channel, 1 F32 per channel
static Status validate(const ITensorInfo *input, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of NEQuantizationLayerKerne...
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
#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 Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
1 channel, 1 F16 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
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
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
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:941
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: Utils.h:1190
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Rounds to nearest value; half rounds to nearest even.
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_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)
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
#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
void set_num_dimensions(size_t num_dimensions)
Set number of dimensions.
Definition: Dimensions.h:149
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
Container for valid region of a window.
Definition: Types.h:188
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
static QUANTIZED_TYPE quantize(float value, const UniformQuantizationInfo &qinfo, RoundingPolicy rounding_policy=RoundingPolicy::TO_NEAREST_UP)
Quantize a value given a 8-bit asymmetric quantization scheme.
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:205
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:145