xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/kernels/CpuIm2ColKernel.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2017-2022 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #include "src/cpu/kernels/CpuIm2ColKernel.h"
25*c217d954SCole Faust 
26*c217d954SCole Faust #include "arm_compute/core/Error.h"
27*c217d954SCole Faust #include "arm_compute/core/Helpers.h"
28*c217d954SCole Faust #include "arm_compute/core/ITensor.h"
29*c217d954SCole Faust #include "arm_compute/core/Size2D.h"
30*c217d954SCole Faust #include "arm_compute/core/TensorInfo.h"
31*c217d954SCole Faust #include "arm_compute/core/Types.h"
32*c217d954SCole Faust #include "arm_compute/core/Validate.h"
33*c217d954SCole Faust #include "src/core/CPP/Validate.h"
34*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
35*c217d954SCole Faust #include "src/core/helpers/WindowHelpers.h"
36*c217d954SCole Faust 
37*c217d954SCole Faust #include "arm_compute/core/utils/misc/ShapeCalculator.h"
38*c217d954SCole Faust 
39*c217d954SCole Faust #include <arm_neon.h>
40*c217d954SCole Faust #include <cstddef>
41*c217d954SCole Faust #include <cstdint>
42*c217d954SCole Faust #include <cstring>
43*c217d954SCole Faust #include <tuple>
44*c217d954SCole Faust 
45*c217d954SCole Faust namespace arm_compute
46*c217d954SCole Faust {
47*c217d954SCole Faust using namespace misc::shape_calculator;
48*c217d954SCole Faust namespace cpu
49*c217d954SCole Faust {
50*c217d954SCole Faust namespace kernels
51*c217d954SCole Faust {
52*c217d954SCole Faust namespace
53*c217d954SCole Faust {
validate_arguments(const ITensorInfo * input,const ITensorInfo * output,const Size2D & kernel_dims,const PadStrideInfo & conv_info,bool has_bias,const Size2D & dilation,unsigned int num_groups)54*c217d954SCole Faust Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info,
55*c217d954SCole Faust                           bool has_bias, const Size2D &dilation, unsigned int num_groups)
56*c217d954SCole Faust {
57*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
58*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
59*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::BFLOAT16, DataType::F16, DataType::F32);
60*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized(input->data_type()) && has_bias);
61*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1));
62*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups > 1, "Number of groups greater than one are not supported on Neon");
63*c217d954SCole Faust 
64*c217d954SCole Faust     // Since there's no implicit padding added, check the total input spatial dimensions (with conv paddings) are big enough for the kernel dimensions
65*c217d954SCole Faust     const unsigned int width_idx    = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
66*c217d954SCole Faust     const unsigned int height_idx   = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
67*c217d954SCole Faust     const unsigned     total_width  = input->dimension(width_idx) + conv_info.pad_left() + conv_info.pad_right();
68*c217d954SCole Faust     const unsigned     total_height = input->dimension(height_idx) + conv_info.pad_top() + conv_info.pad_bottom();
69*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON((total_width < kernel_dims.width) || (total_height < kernel_dims.height));
70*c217d954SCole Faust 
71*c217d954SCole Faust     if(output->total_size() > 0)
72*c217d954SCole Faust     {
73*c217d954SCole Faust         TensorInfo expected_output = output->clone()->set_tensor_shape(compute_im2col_conv_shape(input, kernel_dims, conv_info, has_bias, dilation, false));
74*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&expected_output, output);
75*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
76*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
77*c217d954SCole Faust     }
78*c217d954SCole Faust 
79*c217d954SCole Faust     return Status{};
80*c217d954SCole Faust }
81*c217d954SCole Faust 
82*c217d954SCole Faust template <typename T, bool has_pads>
linearize_volume_nchw(const uint8_t * const in_ptr,T * out_ptr,bool has_bias,int top_left_x,int top_left_y,int kernel_width,int kernel_height,int kernel_depth,int input_w,int input_h,int input_stride_x,int input_stride_y,int input_stride_z,int pad_value,int dilation_x,int dilation_y)83*c217d954SCole Faust inline void linearize_volume_nchw(const uint8_t *const in_ptr,
84*c217d954SCole Faust                                   T                   *out_ptr,
85*c217d954SCole Faust                                   bool                 has_bias,
86*c217d954SCole Faust                                   int                  top_left_x,
87*c217d954SCole Faust                                   int                  top_left_y,
88*c217d954SCole Faust                                   int                  kernel_width,
89*c217d954SCole Faust                                   int                  kernel_height,
90*c217d954SCole Faust                                   int                  kernel_depth,
91*c217d954SCole Faust                                   int                  input_w,
92*c217d954SCole Faust                                   int                  input_h,
93*c217d954SCole Faust                                   int                  input_stride_x,
94*c217d954SCole Faust                                   int                  input_stride_y,
95*c217d954SCole Faust                                   int                  input_stride_z,
96*c217d954SCole Faust                                   int                  pad_value,
97*c217d954SCole Faust                                   int                  dilation_x,
98*c217d954SCole Faust                                   int                  dilation_y)
99*c217d954SCole Faust {
100*c217d954SCole Faust     const int kernel_size2 = kernel_width * kernel_height;
101*c217d954SCole Faust     const int x_e          = top_left_x + kernel_width * dilation_x;
102*c217d954SCole Faust     const int y_e          = top_left_y + kernel_height * dilation_y;
103*c217d954SCole Faust 
104*c217d954SCole Faust     // Linearize volume
105*c217d954SCole Faust     int d = 0;
106*c217d954SCole Faust     // This for loop linearize a volume with 3 slices. This allows:
107*c217d954SCole Faust     // 1) to reduce the iterations of the outer for loop "d"
108*c217d954SCole Faust     // 2) to have an optimized im2col for the first convolution layer where usually we have 3 IFMs
109*c217d954SCole Faust     for(; d <= (kernel_depth - 3); d += 3)
110*c217d954SCole Faust     {
111*c217d954SCole Faust         for(int y = top_left_y; y < y_e; y += dilation_y)
112*c217d954SCole Faust         {
113*c217d954SCole Faust             if((y < 0 || y >= input_h) && has_pads)
114*c217d954SCole Faust             {
115*c217d954SCole Faust                 // All the values will be the offset (will be zeros when not quantized)
116*c217d954SCole Faust                 for(int x = top_left_x; x < x_e; x += dilation_x, ++out_ptr)
117*c217d954SCole Faust                 {
118*c217d954SCole Faust                     *(out_ptr + 0 * kernel_size2) = pad_value;
119*c217d954SCole Faust                     *(out_ptr + 1 * kernel_size2) = pad_value;
120*c217d954SCole Faust                     *(out_ptr + 2 * kernel_size2) = pad_value;
121*c217d954SCole Faust                 }
122*c217d954SCole Faust             }
123*c217d954SCole Faust             else
124*c217d954SCole Faust             {
125*c217d954SCole Faust                 for(int x = top_left_x; x < x_e; x += dilation_x, ++out_ptr)
126*c217d954SCole Faust                 {
127*c217d954SCole Faust                     if((x < 0 || x >= input_w) && has_pads)
128*c217d954SCole Faust                     {
129*c217d954SCole Faust                         *(out_ptr + 0 * kernel_size2) = pad_value;
130*c217d954SCole Faust                         *(out_ptr + 1 * kernel_size2) = pad_value;
131*c217d954SCole Faust                         *(out_ptr + 2 * kernel_size2) = pad_value;
132*c217d954SCole Faust                     }
133*c217d954SCole Faust                     else
134*c217d954SCole Faust                     {
135*c217d954SCole Faust                         *(out_ptr + 0 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 0) * input_stride_z + y * input_stride_y + x * input_stride_x)));
136*c217d954SCole Faust                         *(out_ptr + 1 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 1) * input_stride_z + y * input_stride_y + x * input_stride_x)));
137*c217d954SCole Faust                         *(out_ptr + 2 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 2) * input_stride_z + y * input_stride_y + x * input_stride_x)));
138*c217d954SCole Faust                     }
139*c217d954SCole Faust                 }
140*c217d954SCole Faust             }
141*c217d954SCole Faust         }
142*c217d954SCole Faust         out_ptr += 2 * kernel_size2;
143*c217d954SCole Faust     }
144*c217d954SCole Faust 
145*c217d954SCole Faust     // Left over
146*c217d954SCole Faust     for(; d < kernel_depth; d++)
147*c217d954SCole Faust     {
148*c217d954SCole Faust         for(int y = top_left_y; y < y_e; y += dilation_y)
149*c217d954SCole Faust         {
150*c217d954SCole Faust             if((y < 0 || y >= input_h) && has_pads)
151*c217d954SCole Faust             {
152*c217d954SCole Faust                 // All the values will be the offset (will be zeros when not quantized)
153*c217d954SCole Faust                 memset(static_cast<void *>(out_ptr), pad_value, kernel_width * sizeof(T));
154*c217d954SCole Faust                 out_ptr += kernel_width;
155*c217d954SCole Faust             }
156*c217d954SCole Faust             else
157*c217d954SCole Faust             {
158*c217d954SCole Faust                 for(int x = top_left_x; x < x_e; x += dilation_x, ++out_ptr)
159*c217d954SCole Faust                 {
160*c217d954SCole Faust                     if((x < 0 || x >= input_w) && has_pads)
161*c217d954SCole Faust                     {
162*c217d954SCole Faust                         *out_ptr = pad_value;
163*c217d954SCole Faust                     }
164*c217d954SCole Faust                     else
165*c217d954SCole Faust                     {
166*c217d954SCole Faust                         *out_ptr = *(reinterpret_cast<const T *>(in_ptr + (d * input_stride_z + y * input_stride_y + x * input_stride_x)));
167*c217d954SCole Faust                     }
168*c217d954SCole Faust                 }
169*c217d954SCole Faust             }
170*c217d954SCole Faust         }
171*c217d954SCole Faust     }
172*c217d954SCole Faust 
173*c217d954SCole Faust     // Append 1 if the convolution layer has biases
174*c217d954SCole Faust     if(has_bias)
175*c217d954SCole Faust     {
176*c217d954SCole Faust         *out_ptr = static_cast<T>(1);
177*c217d954SCole Faust     }
178*c217d954SCole Faust }
179*c217d954SCole Faust 
180*c217d954SCole Faust template <typename T, bool has_pads>
linearize_volume_nhwc(const uint8_t * const in_ptr,T * out_ptr,bool has_bias,int start_x,int start_y,int kernel_width,int kernel_height,int input_w,int input_h,int input_c,int input_stride_y,int input_stride_z,int pad_value,int dilation_x,int dilation_y)181*c217d954SCole Faust inline void linearize_volume_nhwc(const uint8_t *const in_ptr,
182*c217d954SCole Faust                                   T                   *out_ptr,
183*c217d954SCole Faust                                   bool                 has_bias,
184*c217d954SCole Faust                                   int                  start_x,
185*c217d954SCole Faust                                   int                  start_y,
186*c217d954SCole Faust                                   int                  kernel_width,
187*c217d954SCole Faust                                   int                  kernel_height,
188*c217d954SCole Faust                                   int                  input_w,
189*c217d954SCole Faust                                   int                  input_h,
190*c217d954SCole Faust                                   int                  input_c,
191*c217d954SCole Faust                                   int                  input_stride_y,
192*c217d954SCole Faust                                   int                  input_stride_z,
193*c217d954SCole Faust                                   int                  pad_value,
194*c217d954SCole Faust                                   int                  dilation_x,
195*c217d954SCole Faust                                   int                  dilation_y)
196*c217d954SCole Faust {
197*c217d954SCole Faust     const int end_x        = start_x + kernel_width * dilation_x;
198*c217d954SCole Faust     const int end_y        = start_y + kernel_height * dilation_y;
199*c217d954SCole Faust     const int pad_quant    = kernel_width * input_c;
200*c217d954SCole Faust     const int element_size = static_cast<int>(sizeof(T));
201*c217d954SCole Faust     if((start_y >= 0) && (end_y < input_h) && (start_x >= 0) && (end_x < input_w) && (dilation_x == 1) && (input_stride_y == input_c * element_size))
202*c217d954SCole Faust     {
203*c217d954SCole Faust         for(int y = start_y; y < end_y; y += dilation_y)
204*c217d954SCole Faust         {
205*c217d954SCole Faust             //optimized for no dilation and no boundary pixels
206*c217d954SCole Faust             memcpy(out_ptr, reinterpret_cast<const T *>(in_ptr + (y * input_stride_z + start_x * input_stride_y)), input_c * kernel_width * element_size);
207*c217d954SCole Faust             out_ptr += input_c * kernel_width;
208*c217d954SCole Faust         }
209*c217d954SCole Faust     }
210*c217d954SCole Faust     else
211*c217d954SCole Faust     {
212*c217d954SCole Faust         for(int y = start_y; y < end_y; y += dilation_y)
213*c217d954SCole Faust         {
214*c217d954SCole Faust             if(y < 0 || y >= input_h)
215*c217d954SCole Faust             {
216*c217d954SCole Faust                 memset(static_cast<void *>(out_ptr), pad_value, pad_quant * element_size);
217*c217d954SCole Faust                 out_ptr += pad_quant;
218*c217d954SCole Faust             }
219*c217d954SCole Faust             else if(dilation_x > 1 || start_x < 0 || end_x >= input_w || input_stride_y != input_c * element_size)
220*c217d954SCole Faust             {
221*c217d954SCole Faust                 for(int x = start_x; x < end_x; x += dilation_x)
222*c217d954SCole Faust                 {
223*c217d954SCole Faust                     if(x < 0 || x >= input_w)
224*c217d954SCole Faust                     {
225*c217d954SCole Faust                         memset(static_cast<void *>(out_ptr), pad_value, input_c * element_size);
226*c217d954SCole Faust                         out_ptr += input_c;
227*c217d954SCole Faust                     }
228*c217d954SCole Faust                     else
229*c217d954SCole Faust                     {
230*c217d954SCole Faust                         memcpy(out_ptr, reinterpret_cast<const T *>(in_ptr + (y * input_stride_z + x * input_stride_y)), input_c * element_size);
231*c217d954SCole Faust                         out_ptr += input_c;
232*c217d954SCole Faust                     }
233*c217d954SCole Faust                 }
234*c217d954SCole Faust             }
235*c217d954SCole Faust             else
236*c217d954SCole Faust             {
237*c217d954SCole Faust                 //optimized for no dilation and no boundary pixels
238*c217d954SCole Faust                 memcpy(out_ptr, reinterpret_cast<const T *>(in_ptr + (y * input_stride_z + start_x * input_stride_y)), input_c * kernel_width * element_size);
239*c217d954SCole Faust                 out_ptr += input_c * kernel_width;
240*c217d954SCole Faust             }
241*c217d954SCole Faust         }
242*c217d954SCole Faust     }
243*c217d954SCole Faust     // Append 1 if the convolution layer has biases
244*c217d954SCole Faust     if(has_bias)
245*c217d954SCole Faust     {
246*c217d954SCole Faust         *out_ptr = static_cast<T>(1);
247*c217d954SCole Faust     }
248*c217d954SCole Faust }
249*c217d954SCole Faust } // namespace
250*c217d954SCole Faust 
251*c217d954SCole Faust template <typename T, bool has_pads, bool is_nchw>
run_im2col(const ITensor * src,ITensor * dst,const Window & window)252*c217d954SCole Faust void CpuIm2ColKernel::run_im2col(const ITensor *src, ITensor *dst, const Window &window)
253*c217d954SCole Faust {
254*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
255*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
256*c217d954SCole Faust 
257*c217d954SCole Faust     const unsigned int width_idx   = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
258*c217d954SCole Faust     const unsigned int height_idx  = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
259*c217d954SCole Faust     const unsigned int channel_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
260*c217d954SCole Faust 
261*c217d954SCole Faust     const int input_w        = src->info()->dimension(width_idx);
262*c217d954SCole Faust     const int input_h        = src->info()->dimension(height_idx);
263*c217d954SCole Faust     const int input_c        = src->info()->dimension(channel_idx);
264*c217d954SCole Faust     const int input_stride_x = src->info()->strides_in_bytes().x();
265*c217d954SCole Faust     const int input_stride_y = src->info()->strides_in_bytes().y();
266*c217d954SCole Faust     const int input_stride_z = src->info()->strides_in_bytes().z();
267*c217d954SCole Faust     const int pad_left       = _conv_info.pad_left();
268*c217d954SCole Faust     const int pad_top        = _conv_info.pad_top();
269*c217d954SCole Faust     const int stride_x       = _conv_info.stride().first;
270*c217d954SCole Faust     const int stride_y       = _conv_info.stride().second;
271*c217d954SCole Faust     const int pad_value      = is_data_type_quantized(src->info()->data_type()) ? src->info()->quantization_info().uniform().offset : 0;
272*c217d954SCole Faust 
273*c217d954SCole Faust     Window window_in_out(window);
274*c217d954SCole Faust     // The first three dimensions of the input and output are increased by the inner loops
275*c217d954SCole Faust     window_in_out.set(Window::DimX, Window::Dimension(0, 0, 0));
276*c217d954SCole Faust     window_in_out.set(Window::DimY, Window::Dimension(0, 0, 0));
277*c217d954SCole Faust     window_in_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
278*c217d954SCole Faust 
279*c217d954SCole Faust     // Create iterators
280*c217d954SCole Faust     Iterator in(src, window_in_out);
281*c217d954SCole Faust     Iterator out(dst, window_in_out);
282*c217d954SCole Faust 
283*c217d954SCole Faust     execute_window_loop(window, [&](const Coordinates & id)
284*c217d954SCole Faust     {
285*c217d954SCole Faust         const int start_w = id[width_idx] * stride_x - pad_left;
286*c217d954SCole Faust         const int start_h = id[height_idx] * stride_y - pad_top;
287*c217d954SCole Faust 
288*c217d954SCole Faust         // Get pointers
289*c217d954SCole Faust         const uint8_t *const input_ptr  = in.ptr();
290*c217d954SCole Faust         auto                 output_ptr = reinterpret_cast<T *>(out.ptr() + (id[width_idx] + id[height_idx] * _convolved_dims.first) * dst->info()->strides_in_bytes().y());
291*c217d954SCole Faust 
292*c217d954SCole Faust         // Linearize volume
293*c217d954SCole Faust         if(is_nchw)
294*c217d954SCole Faust         {
295*c217d954SCole Faust             linearize_volume_nchw<T, has_pads>(input_ptr,
296*c217d954SCole Faust                                                output_ptr,
297*c217d954SCole Faust                                                _has_bias,
298*c217d954SCole Faust                                                start_w,
299*c217d954SCole Faust                                                start_h,
300*c217d954SCole Faust                                                _kernel_width,
301*c217d954SCole Faust                                                _kernel_height,
302*c217d954SCole Faust                                                input_c,
303*c217d954SCole Faust                                                input_w,
304*c217d954SCole Faust                                                input_h,
305*c217d954SCole Faust                                                input_stride_x,
306*c217d954SCole Faust                                                input_stride_y,
307*c217d954SCole Faust                                                input_stride_z,
308*c217d954SCole Faust                                                pad_value,
309*c217d954SCole Faust                                                _dilation.x(),
310*c217d954SCole Faust                                                _dilation.y());
311*c217d954SCole Faust         }
312*c217d954SCole Faust         else
313*c217d954SCole Faust         {
314*c217d954SCole Faust             linearize_volume_nhwc<T, has_pads>(input_ptr,
315*c217d954SCole Faust                                                output_ptr,
316*c217d954SCole Faust                                                _has_bias,
317*c217d954SCole Faust                                                start_w,
318*c217d954SCole Faust                                                start_h,
319*c217d954SCole Faust                                                _kernel_width,
320*c217d954SCole Faust                                                _kernel_height,
321*c217d954SCole Faust                                                input_w,
322*c217d954SCole Faust                                                input_h,
323*c217d954SCole Faust                                                input_c,
324*c217d954SCole Faust                                                input_stride_y,
325*c217d954SCole Faust                                                input_stride_z,
326*c217d954SCole Faust                                                pad_value,
327*c217d954SCole Faust                                                _dilation.x(),
328*c217d954SCole Faust                                                _dilation.y());
329*c217d954SCole Faust         }
330*c217d954SCole Faust     },
331*c217d954SCole Faust     in, out);
332*c217d954SCole Faust }
333*c217d954SCole Faust 
configure(const ITensorInfo * src,ITensorInfo * dst,const Size2D & kernel_dims,const PadStrideInfo & conv_info,bool has_bias,const Size2D & dilation,unsigned int num_groups)334*c217d954SCole Faust void CpuIm2ColKernel::configure(const ITensorInfo *src, ITensorInfo *dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info,
335*c217d954SCole Faust                                 bool has_bias, const Size2D &dilation, unsigned int num_groups)
336*c217d954SCole Faust {
337*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
338*c217d954SCole Faust     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, kernel_dims, conv_info, has_bias, dilation, num_groups));
339*c217d954SCole Faust     ARM_COMPUTE_UNUSED(num_groups);
340*c217d954SCole Faust 
341*c217d954SCole Faust     _data_layout                   = src->data_layout();
342*c217d954SCole Faust     const unsigned int width_idx   = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
343*c217d954SCole Faust     const unsigned int height_idx  = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
344*c217d954SCole Faust     const unsigned int channel_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
345*c217d954SCole Faust 
346*c217d954SCole Faust     _conv_info      = conv_info;
347*c217d954SCole Faust     _kernel_width   = kernel_dims.width;
348*c217d954SCole Faust     _kernel_height  = kernel_dims.height;
349*c217d954SCole Faust     _dilation       = dilation;
350*c217d954SCole Faust     _convolved_dims = scaled_dimensions(src->dimension(width_idx), dst->dimension(height_idx),
351*c217d954SCole Faust                                         _kernel_width, _kernel_height,
352*c217d954SCole Faust                                         _conv_info, _dilation);
353*c217d954SCole Faust     _has_bias = has_bias;
354*c217d954SCole Faust 
355*c217d954SCole Faust     if(_data_layout == DataLayout::NCHW)
356*c217d954SCole Faust     {
357*c217d954SCole Faust         switch(src->data_type())
358*c217d954SCole Faust         {
359*c217d954SCole Faust             case DataType::F32:
360*c217d954SCole Faust                 _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<float, false, true> : &CpuIm2ColKernel::run_im2col<float, true, true>;
361*c217d954SCole Faust                 break;
362*c217d954SCole Faust #if defined(ARM_COMPUTE_ENABLE_BF16)
363*c217d954SCole Faust             case DataType::BFLOAT16:
364*c217d954SCole Faust                 _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<bfloat16, false, true> : &CpuIm2ColKernel::run_im2col<bfloat16, true, true>;
365*c217d954SCole Faust                 break;
366*c217d954SCole Faust #endif /* defined(ARM_COMPUTE_ENABLE_BF16) */
367*c217d954SCole Faust #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
368*c217d954SCole Faust             case DataType::F16:
369*c217d954SCole Faust                 _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<float16_t, false, true> : &CpuIm2ColKernel::run_im2col<float16_t, true, true>;
370*c217d954SCole Faust                 break;
371*c217d954SCole Faust #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
372*c217d954SCole Faust             case DataType::QASYMM8_SIGNED:
373*c217d954SCole Faust             case DataType::QASYMM8:
374*c217d954SCole Faust                 _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<qasymm8_t, false, true> : &CpuIm2ColKernel::run_im2col<qasymm8_t, true, true>;
375*c217d954SCole Faust                 break;
376*c217d954SCole Faust             default:
377*c217d954SCole Faust                 ARM_COMPUTE_ERROR("Data type not supported");
378*c217d954SCole Faust                 break;
379*c217d954SCole Faust         }
380*c217d954SCole Faust     }
381*c217d954SCole Faust     else
382*c217d954SCole Faust     {
383*c217d954SCole Faust         switch(src->data_type())
384*c217d954SCole Faust         {
385*c217d954SCole Faust             case DataType::F32:
386*c217d954SCole Faust                 _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<float, false, false> : &CpuIm2ColKernel::run_im2col<float, true, false>;
387*c217d954SCole Faust                 break;
388*c217d954SCole Faust #if defined(ARM_COMPUTE_ENABLE_BF16)
389*c217d954SCole Faust             case DataType::BFLOAT16:
390*c217d954SCole Faust                 _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<bfloat16, false, false> : &CpuIm2ColKernel::run_im2col<bfloat16, true, false>;
391*c217d954SCole Faust                 break;
392*c217d954SCole Faust #endif /* defined(ARM_COMPUTE_ENABLE_BF16) */
393*c217d954SCole Faust #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
394*c217d954SCole Faust             case DataType::F16:
395*c217d954SCole Faust                 _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<float16_t, false, false> : &CpuIm2ColKernel::run_im2col<float16_t, true, false>;
396*c217d954SCole Faust                 break;
397*c217d954SCole Faust #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
398*c217d954SCole Faust             case DataType::QASYMM8:
399*c217d954SCole Faust                 _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<uint8_t, false, false> : &CpuIm2ColKernel::run_im2col<qasymm8_t, true, false>;
400*c217d954SCole Faust                 break;
401*c217d954SCole Faust             case DataType::QASYMM8_SIGNED:
402*c217d954SCole Faust                 _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<int8_t, false, false> : &CpuIm2ColKernel::run_im2col<qasymm8_t, true, false>;
403*c217d954SCole Faust                 break;
404*c217d954SCole Faust             default:
405*c217d954SCole Faust                 ARM_COMPUTE_ERROR("Data type not supported");
406*c217d954SCole Faust                 break;
407*c217d954SCole Faust         }
408*c217d954SCole Faust     }
409*c217d954SCole Faust 
410*c217d954SCole Faust     // Output tensor auto initialization if not yet initialized
411*c217d954SCole Faust     auto_init_if_empty(*dst, src->clone()->set_tensor_shape(compute_im2col_conv_shape(src, kernel_dims, conv_info, has_bias, dilation, false)));
412*c217d954SCole Faust 
413*c217d954SCole Faust     std::pair<unsigned int, unsigned int> convolved_dims = scaled_dimensions(src->dimension(width_idx), src->dimension(height_idx),
414*c217d954SCole Faust                                                                              kernel_dims.width, kernel_dims.height,
415*c217d954SCole Faust                                                                              conv_info, dilation);
416*c217d954SCole Faust 
417*c217d954SCole Faust     Window win = calculate_max_window(*src, Steps());
418*c217d954SCole Faust     win.set(width_idx, Window::Dimension(0, convolved_dims.first, 1));
419*c217d954SCole Faust     win.set(height_idx, Window::Dimension(0, convolved_dims.second, 1));
420*c217d954SCole Faust     win.set(channel_idx, Window::Dimension(0, 1, 1));
421*c217d954SCole Faust     // Configure kernel window
422*c217d954SCole Faust     ICpuKernel::configure(win);
423*c217d954SCole Faust }
424*c217d954SCole Faust 
validate(const ITensorInfo * src,const ITensorInfo * dst,const Size2D & kernel_dims,const PadStrideInfo & conv_info,bool has_bias,const Size2D & dilation,unsigned int num_groups)425*c217d954SCole Faust Status CpuIm2ColKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info,
426*c217d954SCole Faust                                  bool has_bias, const Size2D &dilation, unsigned int num_groups)
427*c217d954SCole Faust {
428*c217d954SCole Faust     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, kernel_dims, conv_info, has_bias, dilation, num_groups));
429*c217d954SCole Faust     return Status{};
430*c217d954SCole Faust }
431*c217d954SCole Faust 
run_op(ITensorPack & tensors,const Window & window,const ThreadInfo & info)432*c217d954SCole Faust void CpuIm2ColKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
433*c217d954SCole Faust {
434*c217d954SCole Faust     ARM_COMPUTE_UNUSED(info);
435*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
436*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
437*c217d954SCole Faust 
438*c217d954SCole Faust     auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
439*c217d954SCole Faust     auto dst = tensors.get_tensor(TensorType::ACL_DST);
440*c217d954SCole Faust     (this->*_func)(src, dst, window);
441*c217d954SCole Faust }
name() const442*c217d954SCole Faust const char *CpuIm2ColKernel::name() const
443*c217d954SCole Faust {
444*c217d954SCole Faust     return "CpuIm2ColKernel";
445*c217d954SCole Faust }
446*c217d954SCole Faust 
get_mws(const CPUInfo & platform,size_t thread_count) const447*c217d954SCole Faust size_t CpuIm2ColKernel::get_mws(const CPUInfo &platform, size_t thread_count) const
448*c217d954SCole Faust {
449*c217d954SCole Faust     ARM_COMPUTE_UNUSED(thread_count);
450*c217d954SCole Faust     ARM_COMPUTE_UNUSED(platform);
451*c217d954SCole Faust 
452*c217d954SCole Faust     return ICPPKernel::default_mws;
453*c217d954SCole Faust }
454*c217d954SCole Faust } // namespace kernels
455*c217d954SCole Faust } // namespace cpu
456*c217d954SCole Faust } // namespace arm_compute
457