Compute Library
 22.11
quantized.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 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  */
24 #ifndef SRC_CORE_NEON_KERNELS_CONV3D_QUANTIZED_H
25 #define SRC_CORE_NEON_KERNELS_CONV3D_QUANTIZED_H
26 
27 #include "arm_compute/core/Types.h"
31 #include "src/core/NEON/NEAsymm.h"
34 
35 namespace arm_compute
36 {
37 namespace cpu
38 {
39 template <typename T>
40 void directconv3d_quantized_neon_ndhwc(const ITensor *src0, const ITensor *src1, const ITensor *src2, ITensor *dst, const Conv3dInfo &conv_info, const Window &window)
41 {
42  const ITensor *src = src0;
43  const ITensor *weights = src1;
44  const ITensor *biases = src2;
45 
47  using vector_type = typename vtype::type;
48  using tag_type = typename vtype::tag_type;
49  constexpr int num_elems_read_per_iteration = 16 / sizeof(T);
50  using q16_t = typename wrapper::traits::promote_t<T>;
51  using q32_t = typename wrapper::traits::promote_t<q16_t>;
52  using q32x4_t = typename wrapper::traits::neon_vector<q32_t, 4>::type;
53 
54  const int32_t input_offset = -src->info()->quantization_info().uniform().offset;
55  const float input_scale = src->info()->quantization_info().uniform().scale;
56  const int32_t weights_offset = -weights->info()->quantization_info().uniform().offset;
57  const float weights_scale = weights->info()->quantization_info().uniform().scale;
58  const int32_t output_offset = dst->info()->quantization_info().uniform().offset;
59  const float output_scale = dst->info()->quantization_info().uniform().scale;
60 
61  int32_t output_multiplier = 0;
62  int32_t output_shift = 0;
63  const float multiplier = input_scale * weights_scale / output_scale;
64  arm_compute::quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
65 
66  // Scalar quantities (N D H W Cin)
67  const int element_size = src->info()->element_size();
68  const int input_stride_w = src->info()->strides_in_bytes().y() / element_size;
69  const int input_stride_h = src->info()->strides_in_bytes().z() / element_size;
70  const int input_stride_d = src->info()->strides_in_bytes()[3] / element_size;
71  const int input_stride_n = src->info()->strides_in_bytes()[4] / element_size;
72  const int input_dim_w = src->info()->dimension(1);
73  const int input_dim_h = src->info()->dimension(2);
74  const int input_dim_d = src->info()->dimension(3);
75 
76  // Kernel info (D H W Cin Cout)
77  const unsigned int kernel_stride_w = weights->info()->strides_in_bytes()[2] / element_size;
78  const unsigned int kernel_stride_h = weights->info()->strides_in_bytes()[3] / element_size;
79  const unsigned int kernel_stride_d = weights->info()->strides_in_bytes()[4] / element_size;
80  const int kernel_dim_w = weights->info()->dimension(2);
81  const int kernel_dim_h = weights->info()->dimension(3);
82  const int kernel_dim_d = weights->info()->dimension(4);
83 
84  // Convolution padding and stride
85  const int conv_pad_top = conv_info.padding.top;
86  const int conv_pad_left = conv_info.padding.left;
87  const int conv_pad_front = conv_info.padding.front;
88  const int conv_stride_w = conv_info.stride.width;
89  const int conv_stride_h = conv_info.stride.height;
90  const int conv_stride_d = conv_info.stride.depth;
91 
92  // Setup input window for the output iterator
93  Window window_out = window;
94  window_out.set(Window::DimX, Window::Dimension(0, 1, 1));
95 
96  // Setup input window for the weights iterator
97  Window window_w = calculate_max_window(*weights->info(), Steps());
98  window_w.set(Window::DimY, Window::Dimension(0, 1, 1));
99  window_w.set(Window::DimZ, Window::Dimension(0, 1, 1));
100  window_w.set(Window::DimW, Window::Dimension(0, 1, 1));
101  window_w.set(4, Window::Dimension(0, 1, 1));
102 
103  Iterator out(dst, window_out);
104  Iterator wei(weights, window_w);
105 
106  const int32_t *biases_ptr = nullptr;
107  if(biases != nullptr)
108  {
109  biases_ptr = reinterpret_cast<int32_t *>(biases->buffer() + biases->info()->offset_first_element_in_bytes());
110  }
111  execute_window_loop(window_out, [&](const Coordinates & id)
112  {
113  // We are computing the theoretical input starting points
114  const int in_w_start_t = static_cast<int>(id.y()) * conv_stride_w - conv_pad_left;
115  const int in_h_start_t = static_cast<int>(id.z()) * conv_stride_h - conv_pad_top;
116  const int in_d_start_t = static_cast<int>(id[3]) * conv_stride_d - conv_pad_front;
117  const int in_w_end_t = in_w_start_t + kernel_dim_w;
118  const int in_h_end_t = in_h_start_t + kernel_dim_h;
119  const int in_d_end_t = in_d_start_t + kernel_dim_d;
120 
121  // We are computing the valid initial and ending input points by checking the borders
122  const int in_w_start = std::max(in_w_start_t, 0);
123  const int in_h_start = std::max(in_h_start_t, 0);
124  const int in_d_start = std::max(in_d_start_t, 0);
125  const int in_w_end = std::min(in_w_end_t, input_dim_w);
126  const int in_h_end = std::min(in_h_end_t, input_dim_h);
127  const int in_d_end = std::min(in_d_end_t, input_dim_d);
128 
129  // We use the input points to select the valid weight points to use
130  const int wei_w_start = in_w_start - in_w_start_t;
131  const int wei_h_start = in_h_start - in_h_start_t;
132  const int wei_d_start = in_d_start - in_d_start_t;
133  const int wei_w_end = kernel_dim_w - (in_w_end_t - in_w_end);
134  const int wei_h_end = kernel_dim_h - (in_h_end_t - in_h_end);
135  const int wei_d_end = kernel_dim_d - (in_d_end_t - in_d_end);
136 
137  const int index_c_out_end = weights->info()->dimension(0);
138  const int index_c_in_end = weights->info()->dimension(1);
139  const T *const in_ptr_start = reinterpret_cast<const T *>(src->buffer() + src->info()->offset_first_element_in_bytes()) + id[4] * input_stride_n;
140 
141  execute_window_loop(window_w, [&](const Coordinates & id_w)
142  {
143  /*
144  * This is the loop in the weights, and it goes along OFM (output feature map)
145  */
146  const auto weights_ptr_start = reinterpret_cast<const T *>(wei.ptr());
147  int32_t acc = static_cast<int32_t>(0);
148  T *out_ptr = reinterpret_cast<T *>(out.ptr());
149  for(int index_wei_d = wei_d_start, index_in_d = in_d_start; index_wei_d < wei_d_end; ++index_wei_d, ++index_in_d)
150  {
151  const auto in_ptr_d = in_ptr_start + index_in_d * input_stride_d;
152  const auto weights_ptr_d = weights_ptr_start + index_wei_d * kernel_stride_d;
153  for(int index_wei_h = wei_h_start, index_in_h = in_h_start; index_wei_h < wei_h_end; ++index_wei_h, ++index_in_h)
154  {
155  const T *const in_ptr_row = in_ptr_d + index_in_h * input_stride_h;
156  const T *const weights_ptr_row = weights_ptr_d + index_wei_h * kernel_stride_h;
157  for(int index_wei_w = wei_w_start, index_in_w = in_w_start; index_wei_w < wei_w_end; ++index_wei_w, ++index_in_w)
158  {
159  const T *in_ptr_mover = in_ptr_row + index_in_w * input_stride_w;
160  const T *weights_ptr_mover = weights_ptr_row + index_wei_w * kernel_stride_w;
161  int index_c_in = 0;
162  vector_type w_vec = wrapper::vdup_n(static_cast<T>(0), tag_type());
163 
164  q32x4_t acc_q32_0 = wrapper::vdup_n(static_cast<q32_t>(0), tag_type());
165  q32x4_t acc_q32_1 = wrapper::vdup_n(static_cast<q32_t>(0), tag_type());
166  q32x4_t acc_q32_2 = wrapper::vdup_n(static_cast<q32_t>(0), tag_type());
167  q32x4_t acc_q32_3 = wrapper::vdup_n(static_cast<q32_t>(0), tag_type());
168 
169  for(; index_c_in <= index_c_in_end - num_elems_read_per_iteration;
170  index_c_in += num_elems_read_per_iteration, in_ptr_mover += num_elems_read_per_iteration)
171  {
172  const auto src_vec = wrapper::vloadq(in_ptr_mover);
173  //Load Cin weights
174  for(int k = 0; k < num_elems_read_per_iteration; ++k, weights_ptr_mover += index_c_out_end)
175  {
176  w_vec = wrapper::vsetlane(*weights_ptr_mover, w_vec, k);
177  }
178  q32x4_t src_q32_0 = wrapper::vdup_n(static_cast<q32_t>(input_offset), tag_type());
179  q32x4_t src_q32_1 = wrapper::vdup_n(static_cast<q32_t>(input_offset), tag_type());
180  q32x4_t src_q32_2 = wrapper::vdup_n(static_cast<q32_t>(input_offset), tag_type());
181  q32x4_t src_q32_3 = wrapper::vdup_n(static_cast<q32_t>(input_offset), tag_type());
182 
183  q32x4_t wei_q32_0 = wrapper::vdup_n(static_cast<q32_t>(weights_offset), tag_type());
184  q32x4_t wei_q32_1 = wrapper::vdup_n(static_cast<q32_t>(weights_offset), tag_type());
185  q32x4_t wei_q32_2 = wrapper::vdup_n(static_cast<q32_t>(weights_offset), tag_type());
186  q32x4_t wei_q32_3 = wrapper::vdup_n(static_cast<q32_t>(weights_offset), tag_type());
187 
188  const auto src_q16_0 = wrapper::vmovl(wrapper::vgetlow(src_vec));
189  const auto src_q16_1 = wrapper::vmovl(wrapper::vgethigh(src_vec));
190  const auto wei_q16_0 = wrapper::vmovl(wrapper::vgetlow(w_vec));
191  const auto wei_q16_1 = wrapper::vmovl(wrapper::vgethigh(w_vec));
192 
193  src_q32_0 = wrapper::vadd(src_q32_0, wrapper::vmovl(wrapper::vgetlow(src_q16_0)));
194  src_q32_1 = wrapper::vadd(src_q32_1, wrapper::vmovl(wrapper::vgethigh(src_q16_0)));
195  src_q32_2 = wrapper::vadd(src_q32_2, wrapper::vmovl(wrapper::vgetlow(src_q16_1)));
196  src_q32_3 = wrapper::vadd(src_q32_3, wrapper::vmovl(wrapper::vgethigh(src_q16_1)));
197 
198  wei_q32_0 = wrapper::vadd(wei_q32_0, wrapper::vmovl(wrapper::vgetlow(wei_q16_0)));
199  wei_q32_1 = wrapper::vadd(wei_q32_1, wrapper::vmovl(wrapper::vgethigh(wei_q16_0)));
200  wei_q32_2 = wrapper::vadd(wei_q32_2, wrapper::vmovl(wrapper::vgetlow(wei_q16_1)));
201  wei_q32_3 = wrapper::vadd(wei_q32_3, wrapper::vmovl(wrapper::vgethigh(wei_q16_1)));
202 
203  acc_q32_0 = wrapper::vmla(acc_q32_0, wei_q32_0, src_q32_0);
204  acc_q32_1 = wrapper::vmla(acc_q32_1, wei_q32_1, src_q32_1);
205  acc_q32_2 = wrapper::vmla(acc_q32_2, wei_q32_2, src_q32_2);
206  acc_q32_3 = wrapper::vmla(acc_q32_3, wei_q32_3, src_q32_3);
207  }
208 #if defined(__aarch64__)
209  acc += wrapper::vaddv(acc_q32_0);
210  acc += wrapper::vaddv(acc_q32_1);
211  acc += wrapper::vaddv(acc_q32_2);
212  acc += wrapper::vaddv(acc_q32_3);
213 #else // __aarch64__
214  auto temp = wrapper::vpadd(wrapper::vgethigh(acc_q32_0), wrapper::vgetlow(acc_q32_0));
215  temp = wrapper::vpadd(temp, temp);
216  acc += wrapper::vgetlane(temp, 0);
217 
218  temp = wrapper::vpadd(wrapper::vgethigh(acc_q32_1), wrapper::vgetlow(acc_q32_1));
219  temp = wrapper::vpadd(temp, temp);
220  acc += wrapper::vgetlane(temp, 0);
221 
222  temp = wrapper::vpadd(wrapper::vgethigh(acc_q32_2), wrapper::vgetlow(acc_q32_2));
223  temp = wrapper::vpadd(temp, temp);
224  acc += wrapper::vgetlane(temp, 0);
225 
226  temp = wrapper::vpadd(wrapper::vgethigh(acc_q32_3), wrapper::vgetlow(acc_q32_3));
227  temp = wrapper::vpadd(temp, temp);
228  acc += wrapper::vgetlane(temp, 0);
229 
230 #endif // __aarch64__
231 
232  for(; index_c_in < index_c_in_end; ++index_c_in, ++in_ptr_mover, weights_ptr_mover += index_c_out_end)
233  {
234  const auto src_val = *(in_ptr_mover) + input_offset;
235  const auto w_val = *(weights_ptr_mover) + weights_offset;
236  acc += src_val * w_val;
237  }
238  }
239  }
240  }
241 
242  if(biases)
243  {
244  acc += *reinterpret_cast<const int32_t *>(biases_ptr + id_w[0]);
245  }
246 
247  T out_val = finalize_quantization(acc, output_multiplier, output_shift, output_offset, T(0), T(0), false);
248  *(reinterpret_cast<T *>(out_ptr + id_w[0])) = out_val;
249  },
250  wei);
251  },
252  out);
253 }
254 } // namespace cpu
255 } // namespace arm_compute
256 #endif // SRC_CORE_NEON_KERNELS_CONV3D_QUANTIZED_H
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
Descriptor used by the 3d Convolution function.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
uint8x16_t vloadq(const uint8_t *ptr)
Definition: load.h:58
uint8x8_t vadd(const uint8x8_t &a, const uint8x8_t &b)
Definition: add.h:39
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:79
Status calculate_quantized_multiplier(float multiplier, int32_t *quant_multiplier, int32_t *shift, bool ignore_epsilon=false)
Calculate quantized representation of multiplier.
const size_t conv_pad_top
Definition: impl.cpp:60
decltype(strategy::transforms) typedef type
Interface for CPU tensor.
Definition: ITensor.h:36
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2022 Arm Limited.
const size_t conv_pad_left
Definition: impl.cpp:59
typename promote< T >::type promote_t
Get promoted type.
Definition: traits.h:147
uint8x8_t vpadd(const uint8x8_t &a, const uint8x8_t &b)
Definition: add.h:187
uint8_t vgetlane(const uint8x8_t vector, const unsigned int lane)
Definition: getlane.h:91
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
size_t front
Padding across the depth dimenstion on the front, in elements.
Definition: Types.h:820
Create the appropriate SIMD vector given its type and size in terms of elements.
Definition: traits.h:48
void directconv3d_quantized_neon_ndhwc(const ITensor *src0, const ITensor *src1, const ITensor *src2, ITensor *dst, const Conv3dInfo &conv_info, const Window &window)
Definition: quantized.h:40
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
T z() const
Alias to access the size of the third dimension.
Definition: Dimensions.h:97
size_t height
Height of the 3D shape or object.
Definition: Size3D.h:93
Coordinates of an item.
Definition: Coordinates.h:37
virtual uint8_t * buffer() const =0
Interface to be implemented by the child class to return a pointer to CPU memory. ...
UniformQuantizationInfo uniform() const
Return per layer quantization info.
Create the appropriate SIMD vector given its type and size in terms of bits.
Definition: traits.h:92
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
size_t top
Padding across the height dimenstion on the top, in elements.
Definition: Types.h:818
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
uint8x8_t vgetlow(const uint8x16_t val)
Definition: getlow.h:39
size_t left
Padding across the width dimenstion on the left, in elements.
Definition: Types.h:816
size_t width
Width of the 3D shape or object.
Definition: Size3D.h:92
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
uint8x8_t vsetlane(const uint8_t value, const uint8x8_t vector, const unsigned int lane)
Definition: setlane.h:91
static constexpr size_t DimW
Alias for dimension 3 also known as W dimension.
Definition: Window.h:49
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
uint8x8_t vgethigh(const uint8x16_t val)
Definition: gethigh.h:39
virtual size_t offset_first_element_in_bytes() const =0
The offset from the beginning of the memory allocation to the first element of the tensor...
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
size_t depth
Depth of the 3D shape or object.
Definition: Size3D.h:94
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
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
uint8x8_t vdup_n(uint8_t value, traits::vector_64_tag)
Definition: dup_n.h:41
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
T y() const
Alias to access the size of the second dimension.
Definition: Dimensions.h:92
Includes all wrapper headers at once.
virtual const Strides & strides_in_bytes() const =0
The strides in bytes for accessing each dimension of the tensor.
uint8x8_t vmla(const uint8x8_t &a, const uint8x8_t &b, const uint8x8_t &c)
Definition: mla.h:46
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
uint16x8_t vmovl(const uint8x8_t &a)
Definition: movl.h:39
Describe a multidimensional execution window.
Definition: Window.h:39