xref: /aosp_15_r20/external/ComputeLibrary/src/gpu/cl/kernels/ClIm2ColKernel.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2017-2021 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/gpu/cl/kernels/ClIm2ColKernel.h"
25*c217d954SCole Faust 
26*c217d954SCole Faust #include "arm_compute/core/CL/CLHelpers.h"
27*c217d954SCole Faust #include "arm_compute/core/CL/CLKernelLibrary.h"
28*c217d954SCole Faust #include "arm_compute/core/CL/ICLTensor.h"
29*c217d954SCole Faust #include "arm_compute/core/CL/OpenCL.h"
30*c217d954SCole Faust #include "arm_compute/core/Helpers.h"
31*c217d954SCole Faust #include "arm_compute/core/TensorInfo.h"
32*c217d954SCole Faust #include "arm_compute/core/Validate.h"
33*c217d954SCole Faust #include "arm_compute/core/utils/misc/ShapeCalculator.h"
34*c217d954SCole Faust #include "src/core/AccessWindowStatic.h"
35*c217d954SCole Faust #include "src/core/CL/CLValidate.h"
36*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
37*c217d954SCole Faust #include "src/core/helpers/WindowHelpers.h"
38*c217d954SCole Faust #include "support/Cast.h"
39*c217d954SCole Faust #include "support/StringSupport.h"
40*c217d954SCole Faust 
41*c217d954SCole Faust #include <cmath>
42*c217d954SCole Faust #include <tuple>
43*c217d954SCole Faust #include <utility>
44*c217d954SCole Faust 
45*c217d954SCole Faust namespace arm_compute
46*c217d954SCole Faust {
47*c217d954SCole Faust using namespace misc::shape_calculator;
48*c217d954SCole Faust namespace opencl
49*c217d954SCole Faust {
50*c217d954SCole Faust namespace kernels
51*c217d954SCole Faust {
52*c217d954SCole Faust namespace
53*c217d954SCole Faust {
54*c217d954SCole Faust struct Im2ColConfiguration
55*c217d954SCole Faust {
56*c217d954SCole Faust     std::string           kernel_name{};
57*c217d954SCole Faust     std::set<std::string> build_options{};
58*c217d954SCole Faust     unsigned int          num_elems_processed_per_iteration{};
59*c217d954SCole Faust     bool                  is_padding_required_nchw{};
60*c217d954SCole Faust };
61*c217d954SCole Faust 
validate_arguments(const ITensorInfo * src,const ITensorInfo * dst,const Size2D & kernel_dims,const PadStrideInfo & conv_info,bool has_bias,const Size2D & dilation,unsigned int num_groups)62*c217d954SCole Faust Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation,
63*c217d954SCole Faust                           unsigned int num_groups)
64*c217d954SCole Faust {
65*c217d954SCole Faust     const unsigned int channel_idx = get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::CHANNEL);
66*c217d954SCole Faust 
67*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
68*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
69*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized(src->data_type()) && has_bias);
70*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(dst);
71*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1));
72*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(src->data_layout() == DataLayout::UNKNOWN);
73*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(num_groups == 0);
74*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(src->data_layout() == DataLayout::NHWC && num_groups > 1);
75*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON((src->dimension(channel_idx) % num_groups) != 0);
76*c217d954SCole Faust 
77*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
78*c217d954SCole Faust     const unsigned int width_idx    = get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::WIDTH);
79*c217d954SCole Faust     const unsigned int height_idx   = get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::HEIGHT);
80*c217d954SCole Faust     const unsigned     total_width  = src->dimension(width_idx) + conv_info.pad_left() + conv_info.pad_right();
81*c217d954SCole Faust     const unsigned     total_height = src->dimension(height_idx) + conv_info.pad_top() + conv_info.pad_bottom();
82*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON((total_width < kernel_dims.width) || (total_height < kernel_dims.height));
83*c217d954SCole Faust 
84*c217d954SCole Faust     if(dst->total_size() > 0)
85*c217d954SCole Faust     {
86*c217d954SCole Faust         const TensorInfo tensor_info_output = dst->clone()->set_tensor_shape(compute_im2col_conv_shape(src, kernel_dims, conv_info, has_bias, dilation, num_groups == 1, num_groups));
87*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &tensor_info_output);
88*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
89*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst);
90*c217d954SCole Faust     }
91*c217d954SCole Faust 
92*c217d954SCole Faust     return Status{};
93*c217d954SCole Faust }
94*c217d954SCole Faust 
validate_and_configure_window(ITensorInfo * src,ITensorInfo * dst,const Size2D & kernel_dims,const PadStrideInfo & conv_info,bool has_bias,const Size2D & dilation,unsigned int num_elems_processed_per_iteration,bool is_padding_required_nchw,unsigned int num_groups)95*c217d954SCole Faust std::pair<Status, Window> validate_and_configure_window(ITensorInfo *src, ITensorInfo *dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation,
96*c217d954SCole Faust                                                         unsigned int num_elems_processed_per_iteration, bool is_padding_required_nchw, unsigned int num_groups)
97*c217d954SCole Faust {
98*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
99*c217d954SCole Faust 
100*c217d954SCole Faust     // Output tensor auto initialization if not yet initialized
101*c217d954SCole Faust     TensorShape expected_output_shape = compute_im2col_conv_shape(src, kernel_dims, conv_info, has_bias, dilation, num_groups == 1, num_groups);
102*c217d954SCole Faust 
103*c217d954SCole Faust     auto_init_if_empty(*dst, src->clone()->set_tensor_shape(expected_output_shape));
104*c217d954SCole Faust 
105*c217d954SCole Faust     const DataLayout   data_layout  = src->data_layout();
106*c217d954SCole Faust     const unsigned int width_idx    = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
107*c217d954SCole Faust     const unsigned int height_idx   = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
108*c217d954SCole Faust     const unsigned int input_width  = src->dimension(width_idx);
109*c217d954SCole Faust     const unsigned int input_height = src->dimension(height_idx);
110*c217d954SCole Faust 
111*c217d954SCole Faust     // Configure the execute window based on the selected optimal OpenCL kernel
112*c217d954SCole Faust     bool   window_changed = false;
113*c217d954SCole Faust     Window win;
114*c217d954SCole Faust 
115*c217d954SCole Faust     if(data_layout == DataLayout::NHWC)
116*c217d954SCole Faust     {
117*c217d954SCole Faust         win = calculate_max_window(*src, Steps(num_elems_processed_per_iteration));
118*c217d954SCole Faust     }
119*c217d954SCole Faust     else
120*c217d954SCole Faust     {
121*c217d954SCole Faust         if(is_padding_required_nchw)
122*c217d954SCole Faust         {
123*c217d954SCole Faust             const BorderSize border(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left());
124*c217d954SCole Faust             win = calculate_max_window(*src,
125*c217d954SCole Faust                                        Steps(num_elems_processed_per_iteration * conv_info.stride().first, conv_info.stride().second));
126*c217d954SCole Faust             AccessWindowStatic input_access(src,
127*c217d954SCole Faust                                             -border.left,
128*c217d954SCole Faust                                             -border.top,
129*c217d954SCole Faust                                             ceil_to_multiple(input_width + border.right, kernel_dims.width * num_elems_processed_per_iteration),
130*c217d954SCole Faust                                             input_height + border.bottom);
131*c217d954SCole Faust             window_changed = window_changed || update_window_and_padding(win, input_access);
132*c217d954SCole Faust         }
133*c217d954SCole Faust         else
134*c217d954SCole Faust         {
135*c217d954SCole Faust             // For the generic case, CLIm2ColKernel doesn't need padding (we do not read out-of-bounds elements) so
136*c217d954SCole Faust             // update_window_and_padding() can be skipped
137*c217d954SCole Faust             win = calculate_max_window(*src, Steps());
138*c217d954SCole Faust         }
139*c217d954SCole Faust     }
140*c217d954SCole Faust 
141*c217d954SCole Faust     // set the Z dimension's step same size as the whole dimension so that one can't split across the Z dimension
142*c217d954SCole Faust     win.set_dimension_step(Window::DimZ, win[Window::DimZ].end() - win[Window::DimZ].start());
143*c217d954SCole Faust 
144*c217d954SCole Faust     Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
145*c217d954SCole Faust     return std::make_pair(err, win);
146*c217d954SCole Faust }
147*c217d954SCole Faust 
configure_opencl_kernel(const ITensorInfo * src,const Size2D & kernel_dims,const PadStrideInfo & conv_info,bool has_bias,const Size2D & dilation,unsigned int num_groups)148*c217d954SCole Faust Im2ColConfiguration configure_opencl_kernel(const ITensorInfo *src, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation, unsigned int num_groups)
149*c217d954SCole Faust {
150*c217d954SCole Faust     const DataLayout   data_layout   = src->data_layout();
151*c217d954SCole Faust     const DataType     data_type     = src->data_type();
152*c217d954SCole Faust     const unsigned int width_idx     = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
153*c217d954SCole Faust     const unsigned int height_idx    = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
154*c217d954SCole Faust     const unsigned int channel_idx   = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
155*c217d954SCole Faust     const unsigned int input_width   = src->dimension(width_idx);
156*c217d954SCole Faust     const unsigned int input_height  = src->dimension(height_idx);
157*c217d954SCole Faust     const unsigned int input_channel = src->dimension(channel_idx);
158*c217d954SCole Faust 
159*c217d954SCole Faust     const std::pair<unsigned int, unsigned int> convolved_dims = scaled_dimensions(input_width, input_height, kernel_dims.width, kernel_dims.height, conv_info, dilation);
160*c217d954SCole Faust 
161*c217d954SCole Faust     // Im2Col configuration
162*c217d954SCole Faust     std::string                   kernel_name = "im2col_generic_";
163*c217d954SCole Faust     CLBuildOptions                build_opts;
164*c217d954SCole Faust     unsigned int                  num_elems_processed_per_iteration = 1;
165*c217d954SCole Faust     bool                          is_padding_required_nchw          = false;
166*c217d954SCole Faust     const UniformQuantizationInfo qinfo                             = src->quantization_info().uniform();
167*c217d954SCole Faust 
168*c217d954SCole Faust     build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
169*c217d954SCole Faust     build_opts.add_option("-DELEMENT_SIZE=" + support::cpp11::to_string(src->element_size()));
170*c217d954SCole Faust     build_opts.add_option("-DKERNEL_WIDTH=" + support::cpp11::to_string(kernel_dims.width));
171*c217d954SCole Faust     build_opts.add_option("-DKERNEL_HEIGHT=" + support::cpp11::to_string(kernel_dims.height));
172*c217d954SCole Faust     build_opts.add_option("-DCONVOLVED_WIDTH=" + support::cpp11::to_string(convolved_dims.first));
173*c217d954SCole Faust     build_opts.add_option("-DCONVOLVED_HEIGHT=" + support::cpp11::to_string(convolved_dims.second));
174*c217d954SCole Faust     build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_info.stride().first));
175*c217d954SCole Faust     build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_info.stride().second));
176*c217d954SCole Faust     build_opts.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left()));
177*c217d954SCole Faust     build_opts.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top()));
178*c217d954SCole Faust     build_opts.add_option("-DPAD_RIGHT=" + support::cpp11::to_string(conv_info.pad_right()));
179*c217d954SCole Faust     build_opts.add_option("-DPAD_BOTTOM=" + support::cpp11::to_string(conv_info.pad_bottom()));
180*c217d954SCole Faust     build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input_width));
181*c217d954SCole Faust     build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input_height));
182*c217d954SCole Faust     build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(input_channel));
183*c217d954SCole Faust     build_opts.add_option("-DDILATION_X=" + support::cpp11::to_string(dilation.x()));
184*c217d954SCole Faust     build_opts.add_option("-DDILATION_Y=" + support::cpp11::to_string(dilation.y()));
185*c217d954SCole Faust     build_opts.add_option_if(num_groups > 1, "-DNUM_GROUPS=" + support::cpp11::to_string(num_groups));
186*c217d954SCole Faust     build_opts.add_option_if_else(is_data_type_quantized(data_type), "-DPAD_VALUE=" + support::cpp11::to_string(qinfo.offset), "-DPAD_VALUE=0");
187*c217d954SCole Faust     build_opts.add_option_if(has_bias, "-DHAS_BIAS");
188*c217d954SCole Faust 
189*c217d954SCole Faust     if(data_layout == DataLayout::NHWC)
190*c217d954SCole Faust     {
191*c217d954SCole Faust         num_elems_processed_per_iteration = std::min(2U, input_channel);
192*c217d954SCole Faust         is_padding_required_nchw          = false;
193*c217d954SCole Faust 
194*c217d954SCole Faust         // Only the 3x3 and 9x9 cases are optimized for NHWC
195*c217d954SCole Faust         if(kernel_dims == Size2D(3U, 3U))
196*c217d954SCole Faust         {
197*c217d954SCole Faust             kernel_name = "im2col3x3_";
198*c217d954SCole Faust             build_opts.add_option("-DIM2COL_3X3");
199*c217d954SCole Faust         }
200*c217d954SCole Faust         else if(kernel_dims == Size2D(9U, 9U))
201*c217d954SCole Faust         {
202*c217d954SCole Faust             kernel_name = "im2col9x9_";
203*c217d954SCole Faust             build_opts.add_option("-DIM2COL_9X9");
204*c217d954SCole Faust         }
205*c217d954SCole Faust         else
206*c217d954SCole Faust         {
207*c217d954SCole Faust             build_opts.add_option("-DIM2COL_GENERIC");
208*c217d954SCole Faust         }
209*c217d954SCole Faust 
210*c217d954SCole Faust         // Get boundary vector (the first/last vector with potentially a partial vector size) size
211*c217d954SCole Faust         // If input_channel is a multiple of num_elems_processed_per_iteration, the boundary vec size is the (full) vector size
212*c217d954SCole Faust         // otherwise, the boundary vec size is the (partial) remainder vector size
213*c217d954SCole Faust         const unsigned int vec_size          = num_elems_processed_per_iteration;
214*c217d954SCole Faust         const unsigned int partial_vec_size  = input_channel % vec_size;
215*c217d954SCole Faust         const unsigned int boundary_vec_size = vec_size - ((vec_size - partial_vec_size) % vec_size);
216*c217d954SCole Faust         build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vec_size));
217*c217d954SCole Faust         build_opts.add_option("-DBOUNDARY_VECTOR_SIZE=" + support::cpp11::to_string(boundary_vec_size));
218*c217d954SCole Faust     }
219*c217d954SCole Faust     else
220*c217d954SCole Faust     {
221*c217d954SCole Faust         if(dilation == Size2D(1U, 1U))
222*c217d954SCole Faust         {
223*c217d954SCole Faust             const bool squared_im2col = kernel_dims.width == kernel_dims.height;
224*c217d954SCole Faust             if(squared_im2col)
225*c217d954SCole Faust             {
226*c217d954SCole Faust                 // Check if we can run an optimized im2col for NCHW
227*c217d954SCole Faust                 switch(kernel_dims.width)
228*c217d954SCole Faust                 {
229*c217d954SCole Faust                     case 1:
230*c217d954SCole Faust                         // Optimized im2col1x1 if stride_x = 1 and conv_info.has_padding() = false
231*c217d954SCole Faust                         if(conv_info.stride().first == 1 && !conv_info.has_padding())
232*c217d954SCole Faust                         {
233*c217d954SCole Faust                             kernel_name                       = "im2col1x1_stridex1_";
234*c217d954SCole Faust                             num_elems_processed_per_iteration = 4;
235*c217d954SCole Faust                             is_padding_required_nchw          = true;
236*c217d954SCole Faust                         }
237*c217d954SCole Faust                         break;
238*c217d954SCole Faust                     case 3:
239*c217d954SCole Faust                         kernel_name                       = "im2col3x3_";
240*c217d954SCole Faust                         num_elems_processed_per_iteration = 1;
241*c217d954SCole Faust                         is_padding_required_nchw          = true;
242*c217d954SCole Faust                         break;
243*c217d954SCole Faust                     case 5:
244*c217d954SCole Faust                         kernel_name                       = "im2col5x5_";
245*c217d954SCole Faust                         num_elems_processed_per_iteration = 1;
246*c217d954SCole Faust                         is_padding_required_nchw          = true;
247*c217d954SCole Faust                         break;
248*c217d954SCole Faust                     case 11:
249*c217d954SCole Faust                         // Optimized im2col11x11 if pad_x = pad_y = 0
250*c217d954SCole Faust                         if(!conv_info.has_padding())
251*c217d954SCole Faust                         {
252*c217d954SCole Faust                             kernel_name                       = "im2col11x11_padx0_pady0_";
253*c217d954SCole Faust                             num_elems_processed_per_iteration = 1;
254*c217d954SCole Faust                             is_padding_required_nchw          = true;
255*c217d954SCole Faust                         }
256*c217d954SCole Faust                         break;
257*c217d954SCole Faust                     default:
258*c217d954SCole Faust                         kernel_name                       = "im2col_generic_";
259*c217d954SCole Faust                         num_elems_processed_per_iteration = 1;
260*c217d954SCole Faust                         is_padding_required_nchw          = false;
261*c217d954SCole Faust                         break;
262*c217d954SCole Faust                 }
263*c217d954SCole Faust             }
264*c217d954SCole Faust             else if(kernel_dims.width > 1 && !conv_info.has_padding())
265*c217d954SCole Faust             {
266*c217d954SCole Faust                 kernel_name                       = "im2col_generic_padx0_pady0_";
267*c217d954SCole Faust                 num_elems_processed_per_iteration = 1;
268*c217d954SCole Faust                 is_padding_required_nchw          = false;
269*c217d954SCole Faust 
270*c217d954SCole Faust                 // Optimized im2col is performed using one or more vector operations with the specified vector size
271*c217d954SCole Faust                 // and a remainder. For example, for 5x5 convolutions, im2col is performed using vectors of size 4
272*c217d954SCole Faust                 // and scalars; for 7x7 convolutions, using vectors of size 4 and vectors of size 3.
273*c217d954SCole Faust                 // Using the vector size of 4 is always safe since OpenCL supports vectors of size 2 and 3.
274*c217d954SCole Faust                 // Using the vector size of 8, however, may be faster.
275*c217d954SCole Faust                 // For 2x2 convolutions, use vectors of size 2. (For 3x3 convolutions, im2col_kernel3x3_padx0_pady0
276*c217d954SCole Faust                 // is used instead.)
277*c217d954SCole Faust                 const size_t vector_size           = std::min(static_cast<size_t>(4), kernel_dims.width);
278*c217d954SCole Faust                 const size_t width_mod_vector_size = kernel_dims.width % vector_size;
279*c217d954SCole Faust                 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
280*c217d954SCole Faust                 build_opts.add_option("-DWIDTH_MOD_VECTOR_SIZE=" + support::cpp11::to_string(width_mod_vector_size));
281*c217d954SCole Faust             }
282*c217d954SCole Faust         }
283*c217d954SCole Faust     }
284*c217d954SCole Faust 
285*c217d954SCole Faust     // Append the data layout to the kernel_name
286*c217d954SCole Faust     kernel_name += lower_string(string_from_data_layout(data_layout));
287*c217d954SCole Faust 
288*c217d954SCole Faust     Im2ColConfiguration im2col_config;
289*c217d954SCole Faust     im2col_config.kernel_name                       = kernel_name;
290*c217d954SCole Faust     im2col_config.build_options                     = build_opts.options();
291*c217d954SCole Faust     im2col_config.num_elems_processed_per_iteration = num_elems_processed_per_iteration;
292*c217d954SCole Faust     im2col_config.is_padding_required_nchw          = is_padding_required_nchw;
293*c217d954SCole Faust 
294*c217d954SCole Faust     return im2col_config;
295*c217d954SCole Faust }
296*c217d954SCole Faust } // namespace
297*c217d954SCole Faust 
ClIm2ColKernel()298*c217d954SCole Faust ClIm2ColKernel::ClIm2ColKernel()
299*c217d954SCole Faust     : _data_layout(DataLayout::UNKNOWN), _convolved_dims(), _num_elems_processed_per_iteration(1), _kernel_dims(), _conv_info(), _num_groups()
300*c217d954SCole Faust {
301*c217d954SCole Faust     _type = CLKernelType::ELEMENTWISE;
302*c217d954SCole Faust }
303*c217d954SCole Faust 
configure(const ClCompileContext & compile_context,ITensorInfo * src,ITensorInfo * dst,const Size2D & kernel_dims,const PadStrideInfo & conv_info,bool has_bias,const Size2D & dilation,unsigned int num_groups)304*c217d954SCole Faust void ClIm2ColKernel::configure(const ClCompileContext &compile_context, ITensorInfo *src, ITensorInfo *dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias,
305*c217d954SCole Faust                                const Size2D &dilation,
306*c217d954SCole Faust                                unsigned int  num_groups)
307*c217d954SCole Faust {
308*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
309*c217d954SCole Faust     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, kernel_dims, conv_info, has_bias, dilation, num_groups));
310*c217d954SCole Faust 
311*c217d954SCole Faust     auto padding_info = get_padding_info({ src, dst });
312*c217d954SCole Faust     _data_layout      = src->data_layout();
313*c217d954SCole Faust 
314*c217d954SCole Faust     const unsigned int width_idx    = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
315*c217d954SCole Faust     const unsigned int height_idx   = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
316*c217d954SCole Faust     const unsigned int input_width  = src->dimension(width_idx);
317*c217d954SCole Faust     const unsigned int input_height = src->dimension(height_idx);
318*c217d954SCole Faust 
319*c217d954SCole Faust     // Select and configure the optimal OpenCL kernel to run.
320*c217d954SCole Faust     // This function returns the OpenCL kernel's name, the arguments to pass at compile time, the number of elements processed per iteration
321*c217d954SCole Faust     // and the padding requirement flag
322*c217d954SCole Faust     Im2ColConfiguration im2col_config = configure_opencl_kernel(src, kernel_dims, conv_info, has_bias, dilation, num_groups);
323*c217d954SCole Faust 
324*c217d954SCole Faust     // Create kernel
325*c217d954SCole Faust     _kernel = create_kernel(compile_context, im2col_config.kernel_name, im2col_config.build_options);
326*c217d954SCole Faust 
327*c217d954SCole Faust     _convolved_dims                    = scaled_dimensions(input_width, input_height, kernel_dims.width, kernel_dims.height, conv_info, dilation);
328*c217d954SCole Faust     _num_elems_processed_per_iteration = im2col_config.num_elems_processed_per_iteration;
329*c217d954SCole Faust     _kernel_dims                       = kernel_dims; // Only needed by the Tuner
330*c217d954SCole Faust     _conv_info                         = conv_info;   // Only needed by the Tuner
331*c217d954SCole Faust     _num_groups                        = num_groups;
332*c217d954SCole Faust 
333*c217d954SCole Faust     // Configure kernel window
334*c217d954SCole Faust     auto win_config = validate_and_configure_window(src, dst, kernel_dims, conv_info, has_bias, dilation, im2col_config.num_elems_processed_per_iteration,
335*c217d954SCole Faust                                                     im2col_config.is_padding_required_nchw, num_groups);
336*c217d954SCole Faust     ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
337*c217d954SCole Faust     IClKernel::configure_internal(win_config.second);
338*c217d954SCole Faust 
339*c217d954SCole Faust     // Set config_id for enabling LWS tuning
340*c217d954SCole Faust     _config_id = im2col_config.kernel_name;
341*c217d954SCole Faust     _config_id += "_";
342*c217d954SCole Faust     _config_id += lower_string(string_from_data_type(src->data_type()));
343*c217d954SCole Faust     _config_id += "_";
344*c217d954SCole Faust     _config_id += support::cpp11::to_string(num_groups);
345*c217d954SCole Faust     _config_id += "_";
346*c217d954SCole Faust     _config_id += support::cpp11::to_string(dst->dimension(0));
347*c217d954SCole Faust     _config_id += "_";
348*c217d954SCole Faust     _config_id += support::cpp11::to_string(dst->dimension(1));
349*c217d954SCole Faust     _config_id += "_";
350*c217d954SCole Faust     _config_id += lower_string(string_from_data_layout(_data_layout));
351*c217d954SCole Faust 
352*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON(src->data_layout() == DataLayout::NHWC && has_padding_changed(padding_info));
353*c217d954SCole Faust }
354*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)355*c217d954SCole Faust Status ClIm2ColKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation,
356*c217d954SCole Faust                                 unsigned int num_groups)
357*c217d954SCole Faust {
358*c217d954SCole Faust     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, kernel_dims, conv_info, has_bias, dilation, num_groups));
359*c217d954SCole Faust     Im2ColConfiguration im2col_config = configure_opencl_kernel(src, kernel_dims, conv_info, has_bias, dilation, num_groups);
360*c217d954SCole Faust     ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src->clone().get(), dst->clone().get(), kernel_dims, conv_info, has_bias, dilation, im2col_config.num_elems_processed_per_iteration,
361*c217d954SCole Faust                                                               im2col_config.is_padding_required_nchw, num_groups)
362*c217d954SCole Faust                                 .first);
363*c217d954SCole Faust     return Status{};
364*c217d954SCole Faust }
365*c217d954SCole Faust 
run_op(ITensorPack & tensors,const Window & window,cl::CommandQueue & queue)366*c217d954SCole Faust void ClIm2ColKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
367*c217d954SCole Faust {
368*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
369*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IClKernel::window(), window);
370*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON(tensors.empty());
371*c217d954SCole Faust 
372*c217d954SCole Faust     // Get initial windows
373*c217d954SCole Faust     // Collapse in order to have (SRC_DEPTH * BATCH_SIZE) on the 3rd dimension
374*c217d954SCole Faust     Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
375*c217d954SCole Faust     window_collapsed.set_dimension_step(Window::DimZ, 1);
376*c217d954SCole Faust 
377*c217d954SCole Faust     auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
378*c217d954SCole Faust     auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
379*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
380*c217d954SCole Faust 
381*c217d954SCole Faust     Window window_output;
382*c217d954SCole Faust     window_output.use_tensor_dimensions(dst->info()->tensor_shape());
383*c217d954SCole Faust 
384*c217d954SCole Faust     const Window first_slice_3d = window_collapsed.first_slice_window_3D();
385*c217d954SCole Faust 
386*c217d954SCole Faust     Window slice     = first_slice_3d;
387*c217d954SCole Faust     Window slice_in  = first_slice_3d;
388*c217d954SCole Faust     Window slice_out = window_output.first_slice_window_2D();
389*c217d954SCole Faust 
390*c217d954SCole Faust     if(_data_layout == DataLayout::NHWC)
391*c217d954SCole Faust     {
392*c217d954SCole Faust         const Window tmp_win     = window.collapse_if_possible(ICLKernel::window(), 3);
393*c217d954SCole Faust         const int    num_batches = tmp_win[3].end();
394*c217d954SCole Faust 
395*c217d954SCole Faust         slice.set(1, Window::Dimension(0, static_cast<int>(dst->info()->tensor_shape()[1]), 1));
396*c217d954SCole Faust         slice.set(2, Window::Dimension(0, static_cast<int>(num_batches), 1));
397*c217d954SCole Faust     }
398*c217d954SCole Faust     else
399*c217d954SCole Faust     {
400*c217d954SCole Faust         slice.set(0, Window::Dimension(0, static_cast<int>(ceil_to_multiple(_convolved_dims.first, _num_elems_processed_per_iteration)), _num_elems_processed_per_iteration));
401*c217d954SCole Faust         slice.set(1, Window::Dimension(0, static_cast<int>(_convolved_dims.second), 1));
402*c217d954SCole Faust         // Note: In case of NCHW the 3rd dimension is already set collapsing the input window
403*c217d954SCole Faust     }
404*c217d954SCole Faust 
405*c217d954SCole Faust     // Setup input slice
406*c217d954SCole Faust     // The dimensions of the input are increased within the OpenCL kernel
407*c217d954SCole Faust     slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
408*c217d954SCole Faust     slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
409*c217d954SCole Faust     slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
410*c217d954SCole Faust 
411*c217d954SCole Faust     // Setup output slice
412*c217d954SCole Faust     // The dimensions of the output are increased within the OpenCL kernel
413*c217d954SCole Faust     slice_out.set(Window::DimX, Window::Dimension(0, 0, 0));
414*c217d954SCole Faust     slice_out.set(Window::DimY, Window::Dimension(0, 0, 0));
415*c217d954SCole Faust 
416*c217d954SCole Faust     unsigned int idx = num_arguments_per_3D_tensor() + (_num_groups == 1 ? num_arguments_per_2D_tensor() : num_arguments_per_3D_tensor());
417*c217d954SCole Faust     _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(src->info()->strides_in_bytes()[3]));
418*c217d954SCole Faust     _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(dst->info()->strides_in_bytes()[((_num_groups == 1) ? 2 : 3)]));
419*c217d954SCole Faust     do
420*c217d954SCole Faust     {
421*c217d954SCole Faust         unsigned int idx = 0;
422*c217d954SCole Faust         add_3D_tensor_argument(idx, src, slice_in);
423*c217d954SCole Faust         if(_num_groups == 1)
424*c217d954SCole Faust         {
425*c217d954SCole Faust             add_2D_tensor_argument(idx, dst, slice_out);
426*c217d954SCole Faust         }
427*c217d954SCole Faust         else
428*c217d954SCole Faust         {
429*c217d954SCole Faust             add_3D_tensor_argument(idx, dst, slice_out);
430*c217d954SCole Faust         }
431*c217d954SCole Faust         enqueue(queue, *this, slice, lws_hint());
432*c217d954SCole Faust     }
433*c217d954SCole Faust     while(window_collapsed.slide_window_slice_3D(slice) && window_output.slide_window_slice_2D(slice_out) && window_collapsed.slide_window_slice_3D(slice_in));
434*c217d954SCole Faust }
435*c217d954SCole Faust } // namespace kernels
436*c217d954SCole Faust } // namespace opencl
437*c217d954SCole Faust } // namespace arm_compute
438