blob: 44012690e792148b10aa3d4866e7621b0df781e1 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyrou702dc0c2021-03-19 15:06:07 +00002 * Copyright (c) 2017-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLIm2ColKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Helpers.h"
Pablo Tello4a626a72018-04-04 10:01:14 +010031#include "arm_compute/core/TensorInfo.h"
Pablo Tello4a626a72018-04-04 10:01:14 +010032#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/AccessWindowStatic.h"
35#include "src/core/CL/CLValidate.h"
36#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000038#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
40#include <cmath>
41#include <tuple>
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010042#include <utility>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043
Manuel Bottini8481d832019-12-10 15:28:40 +000044namespace arm_compute
45{
46using namespace misc::shape_calculator;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047
Georgios Pinitas358ca202017-12-07 16:47:52 +000048namespace
49{
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010050struct Im2ColConfiguration
51{
52 std::string kernel_name{};
53 std::set<std::string> build_options{};
54 unsigned int num_elems_processed_per_iteration{};
55 bool is_padding_required_nchw{};
56};
57
Giorgio Arena0f170392018-07-18 16:13:12 +010058Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation,
59 unsigned int num_groups)
Georgios Pinitas358ca202017-12-07 16:47:52 +000060{
Giorgio Arena0f170392018-07-18 16:13:12 +010061 const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
62
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010063 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Manuel Bottini8481d832019-12-10 15:28:40 +000064 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
65 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized(input->data_type()) && has_bias);
Georgios Pinitas358ca202017-12-07 16:47:52 +000066 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
Alex Gilday7da29b62018-03-23 14:16:00 +000067 ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1));
Pablo Tello4a626a72018-04-04 10:01:14 +010068 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Giorgio Arena0f170392018-07-18 16:13:12 +010069 ARM_COMPUTE_RETURN_ERROR_ON(num_groups == 0);
70 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::NHWC && num_groups > 1);
71 ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(channel_idx) % num_groups) != 0);
Georgios Pinitas358ca202017-12-07 16:47:52 +000072
SiCong Lif650ea52020-08-05 15:04:00 +010073 // Since there's no implicit padding added, check the total input spatial dimensions (with conv paddings) are big enough for the kernel dimensions
74 const unsigned int width_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
75 const unsigned int height_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
76 const unsigned total_width = input->dimension(width_idx) + conv_info.pad_left() + conv_info.pad_right();
77 const unsigned total_height = input->dimension(height_idx) + conv_info.pad_top() + conv_info.pad_bottom();
78 ARM_COMPUTE_RETURN_ERROR_ON((total_width < kernel_dims.width) || (total_height < kernel_dims.height));
79
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010080 if(output->total_size() > 0)
Georgios Pinitas358ca202017-12-07 16:47:52 +000081 {
Giorgio Arena0f170392018-07-18 16:13:12 +010082 const TensorInfo tensor_info_output = output->clone()->set_tensor_shape(compute_im2col_conv_shape(input, kernel_dims, conv_info, has_bias, dilation, num_groups == 1, num_groups));
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010083 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
Georgios Pinitas358ca202017-12-07 16:47:52 +000084 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000085 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Georgios Pinitas358ca202017-12-07 16:47:52 +000086 }
87
88 return Status{};
89}
Pablo Tello4a626a72018-04-04 10:01:14 +010090
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010091std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation,
Giorgio Arena0f170392018-07-18 16:13:12 +010092 unsigned int num_elems_processed_per_iteration, bool is_padding_required_nchw, unsigned int num_groups)
Pablo Tello4a626a72018-04-04 10:01:14 +010093{
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010094 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Pablo Tello4a626a72018-04-04 10:01:14 +010095
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010096 // Output tensor auto initialization if not yet initialized
Giorgio Arena0f170392018-07-18 16:13:12 +010097 TensorShape expected_output_shape = compute_im2col_conv_shape(input, kernel_dims, conv_info, has_bias, dilation, num_groups == 1, num_groups);
Pablo Tello4a626a72018-04-04 10:01:14 +010098
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010099 auto_init_if_empty(*output, input->clone()->set_tensor_shape(expected_output_shape));
Pablo Tello4a626a72018-04-04 10:01:14 +0100100
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100101 const DataLayout data_layout = input->data_layout();
102 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
103 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
104 const unsigned int input_width = input->dimension(width_idx);
105 const unsigned int input_height = input->dimension(height_idx);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000106
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100107 // Configure the execute window based on the selected optimal OpenCL kernel
108 bool window_changed = false;
109 Window win;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100111 if(data_layout == DataLayout::NHWC)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100113 win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100114 }
115 else
116 {
117 if(is_padding_required_nchw)
Pablo Tello4a626a72018-04-04 10:01:14 +0100118 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100119 const BorderSize border(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left());
120 win = calculate_max_window(*input,
121 Steps(num_elems_processed_per_iteration * conv_info.stride().first, conv_info.stride().second));
122 AccessWindowStatic input_access(input,
123 -border.left,
124 -border.top,
125 ceil_to_multiple(input_width + border.right, kernel_dims.width * num_elems_processed_per_iteration),
126 input_height + border.bottom);
127 window_changed = window_changed || update_window_and_padding(win, input_access);
Pablo Tello4a626a72018-04-04 10:01:14 +0100128 }
129 else
130 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100131 // For the generic case, CLIm2ColKernel doesn't need padding (we do not read out-of-bounds elements) so
132 // update_window_and_padding() can be skipped
133 win = calculate_max_window(*input, Steps());
134 }
135 }
Michalis Spyrou702dc0c2021-03-19 15:06:07 +0000136
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100137 // set the Z dimension's step same size as the whole dimension so that one can't split across the Z dimension
138 win.set_dimension_step(Window::DimZ, win[Window::DimZ].end() - win[Window::DimZ].start());
139
140 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
141 return std::make_pair(err, win);
142}
143
Giorgio Arena0f170392018-07-18 16:13:12 +0100144Im2ColConfiguration configure_opencl_kernel(const ITensorInfo *input, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation, unsigned int num_groups)
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100145{
146 const DataLayout data_layout = input->data_layout();
147 const DataType data_type = input->data_type();
148 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
149 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
150 const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
151 const unsigned int input_width = input->dimension(width_idx);
152 const unsigned int input_height = input->dimension(height_idx);
153 const unsigned int input_channel = input->dimension(channel_idx);
154
155 const std::pair<unsigned int, unsigned int> convolved_dims = scaled_dimensions(input_width, input_height, kernel_dims.width, kernel_dims.height, conv_info, dilation);
156
157 // Im2Col configuration
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100158 std::string kernel_name = "im2col_generic_";
159 CLBuildOptions build_opts;
160 unsigned int num_elems_processed_per_iteration = 1;
161 bool is_padding_required_nchw = false;
162 const UniformQuantizationInfo qinfo = input->quantization_info().uniform();
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100163
164 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
165 build_opts.add_option("-DELEMENT_SIZE=" + support::cpp11::to_string(input->element_size()));
166 build_opts.add_option("-DKERNEL_WIDTH=" + support::cpp11::to_string(kernel_dims.width));
167 build_opts.add_option("-DKERNEL_HEIGHT=" + support::cpp11::to_string(kernel_dims.height));
168 build_opts.add_option("-DCONVOLVED_WIDTH=" + support::cpp11::to_string(convolved_dims.first));
169 build_opts.add_option("-DCONVOLVED_HEIGHT=" + support::cpp11::to_string(convolved_dims.second));
170 build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_info.stride().first));
171 build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_info.stride().second));
172 build_opts.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left()));
173 build_opts.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top()));
174 build_opts.add_option("-DPAD_RIGHT=" + support::cpp11::to_string(conv_info.pad_right()));
175 build_opts.add_option("-DPAD_BOTTOM=" + support::cpp11::to_string(conv_info.pad_bottom()));
176 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input_width));
177 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input_height));
178 build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(input_channel));
179 build_opts.add_option("-DDILATION_X=" + support::cpp11::to_string(dilation.x()));
180 build_opts.add_option("-DDILATION_Y=" + support::cpp11::to_string(dilation.y()));
Giorgio Arena0f170392018-07-18 16:13:12 +0100181 build_opts.add_option_if(num_groups > 1, "-DNUM_GROUPS=" + support::cpp11::to_string(num_groups));
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100182 build_opts.add_option_if_else(is_data_type_quantized(data_type), "-DPAD_VALUE=" + support::cpp11::to_string(qinfo.offset), "-DPAD_VALUE=0");
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100183 build_opts.add_option_if(has_bias, "-DHAS_BIAS");
184
185 if(data_layout == DataLayout::NHWC)
186 {
SiCong Lif650ea52020-08-05 15:04:00 +0100187 num_elems_processed_per_iteration = std::min(2U, input_channel);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100188 is_padding_required_nchw = false;
189
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000190 // Only the 3x3 and 9x9 cases are optimized for NHWC
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100191 if(kernel_dims == Size2D(3U, 3U))
192 {
193 kernel_name = "im2col3x3_";
Pablo Tello4a626a72018-04-04 10:01:14 +0100194 }
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000195 else if(kernel_dims == Size2D(9U, 9U))
196 {
197 kernel_name = "im2col9x9_";
198 }
Gian Marco76faef82018-01-29 12:15:32 +0000199
SiCong Lif650ea52020-08-05 15:04:00 +0100200 // Get boundary vector (the first/last vector with potentially a partial vector size) size
201 // If input_channel is a multiple of num_elems_processed_per_iteration, the boundary vec size is the (full) vector size
202 // otherwise, the boundary vec size is the (partial) remainder vector size
203 const unsigned int vec_size = num_elems_processed_per_iteration;
204 const unsigned int partial_vec_size = input_channel % vec_size;
205 const unsigned int boundary_vec_size = vec_size - ((vec_size - partial_vec_size) % vec_size);
206 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vec_size));
207 build_opts.add_option("-DBOUNDARY_VECTOR_SIZE=" + support::cpp11::to_string(boundary_vec_size));
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100208 }
209 else
210 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000211 if(dilation == Size2D(1U, 1U))
Gian Marco76faef82018-01-29 12:15:32 +0000212 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100213 const bool squared_im2col = kernel_dims.width == kernel_dims.height;
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100214 if(squared_im2col)
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000215 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100216 // Check if we can run an optimized im2col for NCHW
Alex Gilday7da29b62018-03-23 14:16:00 +0000217 switch(kernel_dims.width)
218 {
219 case 1:
220 // Optimized im2col1x1 if stride_x = 1 and conv_info.has_padding() = false
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100221 if(conv_info.stride().first == 1 && !conv_info.has_padding())
Alex Gilday7da29b62018-03-23 14:16:00 +0000222 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100223 kernel_name = "im2col1x1_stridex1_";
224 num_elems_processed_per_iteration = 4;
225 is_padding_required_nchw = true;
Alex Gilday7da29b62018-03-23 14:16:00 +0000226 }
227 break;
228 case 3:
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100229 kernel_name = "im2col3x3_";
230 num_elems_processed_per_iteration = 1;
231 is_padding_required_nchw = true;
Alex Gilday7da29b62018-03-23 14:16:00 +0000232 break;
233 case 5:
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100234 kernel_name = "im2col5x5_";
235 num_elems_processed_per_iteration = 1;
236 is_padding_required_nchw = true;
Alex Gilday7da29b62018-03-23 14:16:00 +0000237 break;
238 case 11:
239 // Optimized im2col11x11 if pad_x = pad_y = 0
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100240 if(!conv_info.has_padding())
Alex Gilday7da29b62018-03-23 14:16:00 +0000241 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100242 kernel_name = "im2col11x11_padx0_pady0_";
243 num_elems_processed_per_iteration = 1;
244 is_padding_required_nchw = true;
Alex Gilday7da29b62018-03-23 14:16:00 +0000245 }
246 break;
247 default:
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100248 kernel_name = "im2col_generic_";
249 num_elems_processed_per_iteration = 1;
250 is_padding_required_nchw = false;
Alex Gilday7da29b62018-03-23 14:16:00 +0000251 break;
252 }
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000253 }
Alex Gilday7da29b62018-03-23 14:16:00 +0000254 else if(kernel_dims.width > 1 && !conv_info.has_padding())
255 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100256 kernel_name = "im2col_generic_padx0_pady0_";
257 num_elems_processed_per_iteration = 1;
258 is_padding_required_nchw = false;
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000259
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100260 // Optimized im2col is performed using one or more vector operations with the specified vector size
261 // and a remainder. For example, for 5x5 convolutions, im2col is performed using vectors of size 4
262 // and scalars; for 7x7 convolutions, using vectors of size 4 and vectors of size 3.
263 // Using the vector size of 4 is always safe since OpenCL supports vectors of size 2 and 3.
264 // Using the vector size of 8, however, may be faster.
265 // For 2x2 convolutions, use vectors of size 2. (For 3x3 convolutions, im2col_kernel3x3_padx0_pady0
266 // is used instead.)
267 const size_t vector_size = std::min(static_cast<size_t>(4), kernel_dims.width);
268 const size_t width_mod_vector_size = kernel_dims.width % vector_size;
269 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
270 build_opts.add_option("-DWIDTH_MOD_VECTOR_SIZE=" + support::cpp11::to_string(width_mod_vector_size));
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000271 }
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000272 }
Gian Marco76faef82018-01-29 12:15:32 +0000273 }
274
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100275 // Append the data layout to the kernel_name
276 kernel_name += lower_string(string_from_data_layout(data_layout));
277
278 Im2ColConfiguration im2col_config;
279 im2col_config.kernel_name = kernel_name;
280 im2col_config.build_options = build_opts.options();
281 im2col_config.num_elems_processed_per_iteration = num_elems_processed_per_iteration;
282 im2col_config.is_padding_required_nchw = is_padding_required_nchw;
283
284 return im2col_config;
285}
286} // namespace
287
288CLIm2ColKernel::CLIm2ColKernel()
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000289 : _input(nullptr), _output(nullptr), _data_layout(DataLayout::UNKNOWN), _convolved_dims(), _num_elems_processed_per_iteration(1), _kernel_dims(), _conv_info(), _num_groups()
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100290{
Pablo Tello4a626a72018-04-04 10:01:14 +0100291}
292
Giorgio Arena0f170392018-07-18 16:13:12 +0100293void CLIm2ColKernel::configure(const ICLTensor *input, ICLTensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation,
294 unsigned int num_groups)
Pablo Tello4a626a72018-04-04 10:01:14 +0100295{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100296 configure(CLKernelLibrary::get().get_compile_context(), input, output, kernel_dims, conv_info, has_bias, dilation, num_groups);
297}
298
Manuel Bottini679fc962020-04-21 16:08:53 +0100299void CLIm2ColKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100300 const Size2D &dilation,
301 unsigned int num_groups)
302{
Pablo Tello4a626a72018-04-04 10:01:14 +0100303 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Giorgio Arena0f170392018-07-18 16:13:12 +0100304 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), kernel_dims, conv_info, has_bias, dilation, num_groups));
Pablo Tello4a626a72018-04-04 10:01:14 +0100305
SiCong Li04a07062020-11-17 14:09:01 +0000306 auto padding_info = get_padding_info({ input, output });
307 _data_layout = input->info()->data_layout();
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000308
309 const unsigned int width_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
310 const unsigned int height_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100311 const unsigned int input_width = input->info()->dimension(width_idx);
312 const unsigned int input_height = input->info()->dimension(height_idx);
Pablo Tello4a626a72018-04-04 10:01:14 +0100313
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100314 // Select and configure the optimal OpenCL kernel to run.
315 // This function returns the OpenCL kernel's name, the arguments to pass at compile time, the number of elements processed per iteration
316 // and the padding requirement flag
Giorgio Arena0f170392018-07-18 16:13:12 +0100317 Im2ColConfiguration im2col_config = configure_opencl_kernel(input->info(), kernel_dims, conv_info, has_bias, dilation, num_groups);
Pablo Tello4a626a72018-04-04 10:01:14 +0100318
319 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100320 _kernel = create_kernel(compile_context, im2col_config.kernel_name, im2col_config.build_options);
Pablo Tello4a626a72018-04-04 10:01:14 +0100321
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100322 _input = input;
323 _output = output;
324 _convolved_dims = scaled_dimensions(input_width, input_height, kernel_dims.width, kernel_dims.height, conv_info, dilation);
325 _num_elems_processed_per_iteration = im2col_config.num_elems_processed_per_iteration;
326 _kernel_dims = kernel_dims; // Only needed by the Tuner
327 _conv_info = conv_info; // Only needed by the Tuner
Giorgio Arena0f170392018-07-18 16:13:12 +0100328 _num_groups = num_groups;
Pablo Tello4a626a72018-04-04 10:01:14 +0100329
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100330 // Configure kernel window
331 auto win_config = validate_and_configure_window(input->info(), output->info(), kernel_dims, conv_info, has_bias, dilation, im2col_config.num_elems_processed_per_iteration,
Giorgio Arena0f170392018-07-18 16:13:12 +0100332 im2col_config.is_padding_required_nchw, num_groups);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100333 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100334 ICLKernel::configure_internal(win_config.second);
Gian Marcode691f02017-09-08 16:13:11 +0100335
336 // Set config_id for enabling LWS tuning
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100337 _config_id = im2col_config.kernel_name;
Gian Marco76faef82018-01-29 12:15:32 +0000338 _config_id += "_";
Gian Marcode691f02017-09-08 16:13:11 +0100339 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
340 _config_id += "_";
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100341 _config_id += support::cpp11::to_string(num_groups);
342 _config_id += "_";
Gian Marcode691f02017-09-08 16:13:11 +0100343 _config_id += support::cpp11::to_string(output->info()->dimension(0));
344 _config_id += "_";
345 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Giorgio Arena00b93f52018-06-28 17:18:50 +0100346 _config_id += "_";
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000347 _config_id += lower_string(string_from_data_layout(_data_layout));
SiCong Li04a07062020-11-17 14:09:01 +0000348
349 ARM_COMPUTE_ERROR_ON(input->info()->data_layout() == DataLayout::NHWC && has_padding_changed(padding_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100350}
351
Giorgio Arena0f170392018-07-18 16:13:12 +0100352Status CLIm2ColKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation,
353 unsigned int num_groups)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000354{
Giorgio Arena0f170392018-07-18 16:13:12 +0100355 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, kernel_dims, conv_info, has_bias, dilation, num_groups));
356 Im2ColConfiguration im2col_config = configure_opencl_kernel(input, kernel_dims, conv_info, has_bias, dilation, num_groups);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100357 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), kernel_dims, conv_info, has_bias, dilation, im2col_config.num_elems_processed_per_iteration,
Giorgio Arena0f170392018-07-18 16:13:12 +0100358 im2col_config.is_padding_required_nchw, num_groups)
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100359 .first);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000360 return Status{};
361}
362
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100363void CLIm2ColKernel::run(const Window &window, cl::CommandQueue &queue)
364{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100365 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
366 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
367
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100368 // Get initial windows
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100369 // Collapse in order to have (SRC_DEPTH * BATCH_SIZE) on the 3rd dimension
steniu01868e5412017-07-17 23:16:00 +0100370 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
steniu01868e5412017-07-17 23:16:00 +0100371 window_collapsed.set_dimension_step(Window::DimZ, 1);
372
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100373 Window window_output;
374 window_output.use_tensor_dimensions(_output->info()->tensor_shape());
375
Pablo Tello4a626a72018-04-04 10:01:14 +0100376 const Window first_slice_3d = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100377
Pablo Tello4a626a72018-04-04 10:01:14 +0100378 Window slice = first_slice_3d;
379 Window slice_in = first_slice_3d;
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100380 Window slice_out = window_output.first_slice_window_2D();
Pablo Tello4a626a72018-04-04 10:01:14 +0100381
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000382 if(_data_layout == DataLayout::NHWC)
Gian Marco76faef82018-01-29 12:15:32 +0000383 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100384 const Window tmp_win = window.collapse_if_possible(ICLKernel::window(), 3);
385 const int num_batches = tmp_win[3].end();
386
387 slice.set(1, Window::Dimension(0, static_cast<int>(_output->info()->tensor_shape()[1]), 1));
388 slice.set(2, Window::Dimension(0, static_cast<int>(num_batches), 1));
389 }
390 else
391 {
392 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));
393 slice.set(1, Window::Dimension(0, static_cast<int>(_convolved_dims.second), 1));
394 // Note: In case of NCHW the 3rd dimension is already set collapsing the input window
Gian Marco76faef82018-01-29 12:15:32 +0000395 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100396
397 // Setup input slice
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100398 // The dimensions of the input are increased within the OpenCL kernel
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100399 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
400 slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
401 slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
402
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100403 // Setup output slice
404 // The dimensions of the output are increased within the OpenCL kernel
405 slice_out.set(Window::DimX, Window::Dimension(0, 0, 0));
406 slice_out.set(Window::DimY, Window::Dimension(0, 0, 0));
407
Giorgio Arena0f170392018-07-18 16:13:12 +0100408 unsigned int idx = num_arguments_per_3D_tensor() + (_num_groups == 1 ? num_arguments_per_2D_tensor() : num_arguments_per_3D_tensor());
409 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input->info()->strides_in_bytes()[3]));
410 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_output->info()->strides_in_bytes()[((_num_groups == 1) ? 2 : 3)]));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100411 do
412 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100413 unsigned int idx = 0;
414 add_3D_tensor_argument(idx, _input, slice_in);
Giorgio Arena0f170392018-07-18 16:13:12 +0100415 if(_num_groups == 1)
416 {
417 add_2D_tensor_argument(idx, _output, slice_out);
418 }
419 else
420 {
421 add_3D_tensor_argument(idx, _output, slice_out);
422 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100423 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100424 }
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100425 while(window_collapsed.slide_window_slice_3D(slice) && window_output.slide_window_slice_2D(slice_out) && window_collapsed.slide_window_slice_3D(slice_in));
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100426}
Matthew Bentham758b5ba2020-03-05 23:37:48 +0000427} // namespace arm_compute