blob: c42762b99c43778923c87a57cf4e742c6ca5cb16 [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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/gpu/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"
Manuel Bottinid844c082021-07-14 12:58:54 +010038#include "support/Cast.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000039#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040
41#include <cmath>
42#include <tuple>
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010043#include <utility>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044
Manuel Bottini8481d832019-12-10 15:28:40 +000045namespace arm_compute
46{
47using namespace misc::shape_calculator;
Manuel Bottinid844c082021-07-14 12:58:54 +010048namespace opencl
49{
50namespace kernels
51{
Georgios Pinitas358ca202017-12-07 16:47:52 +000052namespace
53{
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010054struct Im2ColConfiguration
55{
56 std::string kernel_name{};
57 std::set<std::string> build_options{};
58 unsigned int num_elems_processed_per_iteration{};
59 bool is_padding_required_nchw{};
60};
61
Manuel Bottinid844c082021-07-14 12:58:54 +010062Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation,
Giorgio Arena0f170392018-07-18 16:13:12 +010063 unsigned int num_groups)
Georgios Pinitas358ca202017-12-07 16:47:52 +000064{
Manuel Bottinid844c082021-07-14 12:58:54 +010065 const unsigned int channel_idx = get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::CHANNEL);
Giorgio Arena0f170392018-07-18 16:13:12 +010066
Manuel Bottinid844c082021-07-14 12:58:54 +010067 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
68 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
69 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized(src->data_type()) && has_bias);
70 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(dst);
Alex Gilday7da29b62018-03-23 14:16:00 +000071 ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1));
Manuel Bottinid844c082021-07-14 12:58:54 +010072 ARM_COMPUTE_RETURN_ERROR_ON(src->data_layout() == DataLayout::UNKNOWN);
Giorgio Arena0f170392018-07-18 16:13:12 +010073 ARM_COMPUTE_RETURN_ERROR_ON(num_groups == 0);
Manuel Bottinid844c082021-07-14 12:58:54 +010074 ARM_COMPUTE_RETURN_ERROR_ON(src->data_layout() == DataLayout::NHWC && num_groups > 1);
75 ARM_COMPUTE_RETURN_ERROR_ON((src->dimension(channel_idx) % num_groups) != 0);
Georgios Pinitas358ca202017-12-07 16:47:52 +000076
SiCong Lif650ea52020-08-05 15:04:00 +010077 // Since there's no implicit padding added, check the total input spatial dimensions (with conv paddings) are big enough for the kernel dimensions
Manuel Bottinid844c082021-07-14 12:58:54 +010078 const unsigned int width_idx = get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::WIDTH);
79 const unsigned int height_idx = get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::HEIGHT);
80 const unsigned total_width = src->dimension(width_idx) + conv_info.pad_left() + conv_info.pad_right();
81 const unsigned total_height = src->dimension(height_idx) + conv_info.pad_top() + conv_info.pad_bottom();
SiCong Lif650ea52020-08-05 15:04:00 +010082 ARM_COMPUTE_RETURN_ERROR_ON((total_width < kernel_dims.width) || (total_height < kernel_dims.height));
83
Manuel Bottinid844c082021-07-14 12:58:54 +010084 if(dst->total_size() > 0)
Georgios Pinitas358ca202017-12-07 16:47:52 +000085 {
Manuel Bottinid844c082021-07-14 12:58:54 +010086 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 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &tensor_info_output);
88 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
89 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst);
Georgios Pinitas358ca202017-12-07 16:47:52 +000090 }
91
92 return Status{};
93}
Pablo Tello4a626a72018-04-04 10:01:14 +010094
Manuel Bottinid844c082021-07-14 12:58:54 +010095std::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,
Giorgio Arena0f170392018-07-18 16:13:12 +010096 unsigned int num_elems_processed_per_iteration, bool is_padding_required_nchw, unsigned int num_groups)
Pablo Tello4a626a72018-04-04 10:01:14 +010097{
Manuel Bottinid844c082021-07-14 12:58:54 +010098 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
Pablo Tello4a626a72018-04-04 10:01:14 +010099
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100100 // Output tensor auto initialization if not yet initialized
Manuel Bottinid844c082021-07-14 12:58:54 +0100101 TensorShape expected_output_shape = compute_im2col_conv_shape(src, kernel_dims, conv_info, has_bias, dilation, num_groups == 1, num_groups);
Pablo Tello4a626a72018-04-04 10:01:14 +0100102
Manuel Bottinid844c082021-07-14 12:58:54 +0100103 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(expected_output_shape));
Pablo Tello4a626a72018-04-04 10:01:14 +0100104
Manuel Bottinid844c082021-07-14 12:58:54 +0100105 const DataLayout data_layout = src->data_layout();
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100106 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
107 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Manuel Bottinid844c082021-07-14 12:58:54 +0100108 const unsigned int input_width = src->dimension(width_idx);
109 const unsigned int input_height = src->dimension(height_idx);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000110
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100111 // Configure the execute window based on the selected optimal OpenCL kernel
112 bool window_changed = false;
113 Window win;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100115 if(data_layout == DataLayout::NHWC)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116 {
Manuel Bottinid844c082021-07-14 12:58:54 +0100117 win = calculate_max_window(*src, Steps(num_elems_processed_per_iteration));
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100118 }
119 else
120 {
121 if(is_padding_required_nchw)
Pablo Tello4a626a72018-04-04 10:01:14 +0100122 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100123 const BorderSize border(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left());
Manuel Bottinid844c082021-07-14 12:58:54 +0100124 win = calculate_max_window(*src,
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100125 Steps(num_elems_processed_per_iteration * conv_info.stride().first, conv_info.stride().second));
Manuel Bottinid844c082021-07-14 12:58:54 +0100126 AccessWindowStatic input_access(src,
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100127 -border.left,
128 -border.top,
129 ceil_to_multiple(input_width + border.right, kernel_dims.width * num_elems_processed_per_iteration),
130 input_height + border.bottom);
131 window_changed = window_changed || update_window_and_padding(win, input_access);
Pablo Tello4a626a72018-04-04 10:01:14 +0100132 }
133 else
134 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100135 // For the generic case, CLIm2ColKernel doesn't need padding (we do not read out-of-bounds elements) so
136 // update_window_and_padding() can be skipped
Manuel Bottinid844c082021-07-14 12:58:54 +0100137 win = calculate_max_window(*src, Steps());
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100138 }
139 }
Michalis Spyrou702dc0c2021-03-19 15:06:07 +0000140
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100141 // set the Z dimension's step same size as the whole dimension so that one can't split across the Z dimension
142 win.set_dimension_step(Window::DimZ, win[Window::DimZ].end() - win[Window::DimZ].start());
143
144 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
145 return std::make_pair(err, win);
146}
147
Manuel Bottinid844c082021-07-14 12:58:54 +0100148Im2ColConfiguration configure_opencl_kernel(const ITensorInfo *src, 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 +0100149{
Manuel Bottinid844c082021-07-14 12:58:54 +0100150 const DataLayout data_layout = src->data_layout();
151 const DataType data_type = src->data_type();
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100152 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
153 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
154 const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
Manuel Bottinid844c082021-07-14 12:58:54 +0100155 const unsigned int input_width = src->dimension(width_idx);
156 const unsigned int input_height = src->dimension(height_idx);
157 const unsigned int input_channel = src->dimension(channel_idx);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100158
159 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
161 // Im2Col configuration
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100162 std::string kernel_name = "im2col_generic_";
163 CLBuildOptions build_opts;
164 unsigned int num_elems_processed_per_iteration = 1;
165 bool is_padding_required_nchw = false;
Manuel Bottinid844c082021-07-14 12:58:54 +0100166 const UniformQuantizationInfo qinfo = src->quantization_info().uniform();
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100167
168 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
Manuel Bottinid844c082021-07-14 12:58:54 +0100169 build_opts.add_option("-DELEMENT_SIZE=" + support::cpp11::to_string(src->element_size()));
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100170 build_opts.add_option("-DKERNEL_WIDTH=" + support::cpp11::to_string(kernel_dims.width));
171 build_opts.add_option("-DKERNEL_HEIGHT=" + support::cpp11::to_string(kernel_dims.height));
172 build_opts.add_option("-DCONVOLVED_WIDTH=" + support::cpp11::to_string(convolved_dims.first));
173 build_opts.add_option("-DCONVOLVED_HEIGHT=" + support::cpp11::to_string(convolved_dims.second));
174 build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_info.stride().first));
175 build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_info.stride().second));
176 build_opts.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left()));
177 build_opts.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top()));
178 build_opts.add_option("-DPAD_RIGHT=" + support::cpp11::to_string(conv_info.pad_right()));
179 build_opts.add_option("-DPAD_BOTTOM=" + support::cpp11::to_string(conv_info.pad_bottom()));
180 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input_width));
181 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input_height));
182 build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(input_channel));
183 build_opts.add_option("-DDILATION_X=" + support::cpp11::to_string(dilation.x()));
184 build_opts.add_option("-DDILATION_Y=" + support::cpp11::to_string(dilation.y()));
Giorgio Arena0f170392018-07-18 16:13:12 +0100185 build_opts.add_option_if(num_groups > 1, "-DNUM_GROUPS=" + support::cpp11::to_string(num_groups));
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100186 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 +0100187 build_opts.add_option_if(has_bias, "-DHAS_BIAS");
188
189 if(data_layout == DataLayout::NHWC)
190 {
SiCong Lif650ea52020-08-05 15:04:00 +0100191 num_elems_processed_per_iteration = std::min(2U, input_channel);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100192 is_padding_required_nchw = false;
193
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000194 // Only the 3x3 and 9x9 cases are optimized for NHWC
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100195 if(kernel_dims == Size2D(3U, 3U))
196 {
197 kernel_name = "im2col3x3_";
Pablo Tello4a626a72018-04-04 10:01:14 +0100198 }
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000199 else if(kernel_dims == Size2D(9U, 9U))
200 {
201 kernel_name = "im2col9x9_";
202 }
Gian Marco76faef82018-01-29 12:15:32 +0000203
SiCong Lif650ea52020-08-05 15:04:00 +0100204 // Get boundary vector (the first/last vector with potentially a partial vector size) size
205 // If input_channel is a multiple of num_elems_processed_per_iteration, the boundary vec size is the (full) vector size
206 // otherwise, the boundary vec size is the (partial) remainder vector size
207 const unsigned int vec_size = num_elems_processed_per_iteration;
208 const unsigned int partial_vec_size = input_channel % vec_size;
209 const unsigned int boundary_vec_size = vec_size - ((vec_size - partial_vec_size) % vec_size);
210 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vec_size));
211 build_opts.add_option("-DBOUNDARY_VECTOR_SIZE=" + support::cpp11::to_string(boundary_vec_size));
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100212 }
213 else
214 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000215 if(dilation == Size2D(1U, 1U))
Gian Marco76faef82018-01-29 12:15:32 +0000216 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100217 const bool squared_im2col = kernel_dims.width == kernel_dims.height;
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100218 if(squared_im2col)
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000219 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100220 // Check if we can run an optimized im2col for NCHW
Alex Gilday7da29b62018-03-23 14:16:00 +0000221 switch(kernel_dims.width)
222 {
223 case 1:
224 // Optimized im2col1x1 if stride_x = 1 and conv_info.has_padding() = false
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100225 if(conv_info.stride().first == 1 && !conv_info.has_padding())
Alex Gilday7da29b62018-03-23 14:16:00 +0000226 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100227 kernel_name = "im2col1x1_stridex1_";
228 num_elems_processed_per_iteration = 4;
229 is_padding_required_nchw = true;
Alex Gilday7da29b62018-03-23 14:16:00 +0000230 }
231 break;
232 case 3:
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100233 kernel_name = "im2col3x3_";
234 num_elems_processed_per_iteration = 1;
235 is_padding_required_nchw = true;
Alex Gilday7da29b62018-03-23 14:16:00 +0000236 break;
237 case 5:
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100238 kernel_name = "im2col5x5_";
239 num_elems_processed_per_iteration = 1;
240 is_padding_required_nchw = true;
Alex Gilday7da29b62018-03-23 14:16:00 +0000241 break;
242 case 11:
243 // Optimized im2col11x11 if pad_x = pad_y = 0
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100244 if(!conv_info.has_padding())
Alex Gilday7da29b62018-03-23 14:16:00 +0000245 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100246 kernel_name = "im2col11x11_padx0_pady0_";
247 num_elems_processed_per_iteration = 1;
248 is_padding_required_nchw = true;
Alex Gilday7da29b62018-03-23 14:16:00 +0000249 }
250 break;
251 default:
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100252 kernel_name = "im2col_generic_";
253 num_elems_processed_per_iteration = 1;
254 is_padding_required_nchw = false;
Alex Gilday7da29b62018-03-23 14:16:00 +0000255 break;
256 }
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000257 }
Alex Gilday7da29b62018-03-23 14:16:00 +0000258 else if(kernel_dims.width > 1 && !conv_info.has_padding())
259 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100260 kernel_name = "im2col_generic_padx0_pady0_";
261 num_elems_processed_per_iteration = 1;
262 is_padding_required_nchw = false;
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000263
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100264 // Optimized im2col is performed using one or more vector operations with the specified vector size
265 // and a remainder. For example, for 5x5 convolutions, im2col is performed using vectors of size 4
266 // and scalars; for 7x7 convolutions, using vectors of size 4 and vectors of size 3.
267 // Using the vector size of 4 is always safe since OpenCL supports vectors of size 2 and 3.
268 // Using the vector size of 8, however, may be faster.
269 // For 2x2 convolutions, use vectors of size 2. (For 3x3 convolutions, im2col_kernel3x3_padx0_pady0
270 // is used instead.)
271 const size_t vector_size = std::min(static_cast<size_t>(4), kernel_dims.width);
272 const size_t width_mod_vector_size = kernel_dims.width % vector_size;
273 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
274 build_opts.add_option("-DWIDTH_MOD_VECTOR_SIZE=" + support::cpp11::to_string(width_mod_vector_size));
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000275 }
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000276 }
Gian Marco76faef82018-01-29 12:15:32 +0000277 }
278
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100279 // Append the data layout to the kernel_name
280 kernel_name += lower_string(string_from_data_layout(data_layout));
281
282 Im2ColConfiguration im2col_config;
283 im2col_config.kernel_name = kernel_name;
284 im2col_config.build_options = build_opts.options();
285 im2col_config.num_elems_processed_per_iteration = num_elems_processed_per_iteration;
286 im2col_config.is_padding_required_nchw = is_padding_required_nchw;
287
288 return im2col_config;
289}
290} // namespace
291
Manuel Bottinid844c082021-07-14 12:58:54 +0100292ClIm2ColKernel::ClIm2ColKernel()
293 : _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 +0100294{
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100295 _type = CLKernelType::ELEMENTWISE;
Pablo Tello4a626a72018-04-04 10:01:14 +0100296}
297
Manuel Bottinid844c082021-07-14 12:58:54 +0100298void ClIm2ColKernel::configure(const ClCompileContext &compile_context, ITensorInfo *src, ITensorInfo *dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100299 const Size2D &dilation,
300 unsigned int num_groups)
301{
Manuel Bottinid844c082021-07-14 12:58:54 +0100302 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
303 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, kernel_dims, conv_info, has_bias, dilation, num_groups));
Pablo Tello4a626a72018-04-04 10:01:14 +0100304
Manuel Bottinid844c082021-07-14 12:58:54 +0100305 auto padding_info = get_padding_info({ src, dst });
306 _data_layout = src->data_layout();
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000307
308 const unsigned int width_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
309 const unsigned int height_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
Manuel Bottinid844c082021-07-14 12:58:54 +0100310 const unsigned int input_width = src->dimension(width_idx);
311 const unsigned int input_height = src->dimension(height_idx);
Pablo Tello4a626a72018-04-04 10:01:14 +0100312
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100313 // Select and configure the optimal OpenCL kernel to run.
314 // This function returns the OpenCL kernel's name, the arguments to pass at compile time, the number of elements processed per iteration
315 // and the padding requirement flag
Manuel Bottinid844c082021-07-14 12:58:54 +0100316 Im2ColConfiguration im2col_config = configure_opencl_kernel(src, kernel_dims, conv_info, has_bias, dilation, num_groups);
Pablo Tello4a626a72018-04-04 10:01:14 +0100317
318 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100319 _kernel = create_kernel(compile_context, im2col_config.kernel_name, im2col_config.build_options);
Pablo Tello4a626a72018-04-04 10:01:14 +0100320
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100321 _convolved_dims = scaled_dimensions(input_width, input_height, kernel_dims.width, kernel_dims.height, conv_info, dilation);
322 _num_elems_processed_per_iteration = im2col_config.num_elems_processed_per_iteration;
323 _kernel_dims = kernel_dims; // Only needed by the Tuner
324 _conv_info = conv_info; // Only needed by the Tuner
Giorgio Arena0f170392018-07-18 16:13:12 +0100325 _num_groups = num_groups;
Pablo Tello4a626a72018-04-04 10:01:14 +0100326
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100327 // Configure kernel window
Manuel Bottinid844c082021-07-14 12:58:54 +0100328 auto win_config = validate_and_configure_window(src, dst, kernel_dims, conv_info, has_bias, dilation, im2col_config.num_elems_processed_per_iteration,
Giorgio Arena0f170392018-07-18 16:13:12 +0100329 im2col_config.is_padding_required_nchw, num_groups);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100330 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Manuel Bottinid844c082021-07-14 12:58:54 +0100331 IClKernel::configure_internal(win_config.second);
Gian Marcode691f02017-09-08 16:13:11 +0100332
333 // Set config_id for enabling LWS tuning
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100334 _config_id = im2col_config.kernel_name;
Gian Marco76faef82018-01-29 12:15:32 +0000335 _config_id += "_";
Manuel Bottinid844c082021-07-14 12:58:54 +0100336 _config_id += lower_string(string_from_data_type(src->data_type()));
Gian Marcode691f02017-09-08 16:13:11 +0100337 _config_id += "_";
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100338 _config_id += support::cpp11::to_string(num_groups);
339 _config_id += "_";
Manuel Bottinid844c082021-07-14 12:58:54 +0100340 _config_id += support::cpp11::to_string(dst->dimension(0));
Gian Marcode691f02017-09-08 16:13:11 +0100341 _config_id += "_";
Manuel Bottinid844c082021-07-14 12:58:54 +0100342 _config_id += support::cpp11::to_string(dst->dimension(1));
Giorgio Arena00b93f52018-06-28 17:18:50 +0100343 _config_id += "_";
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000344 _config_id += lower_string(string_from_data_layout(_data_layout));
SiCong Li04a07062020-11-17 14:09:01 +0000345
Manuel Bottinid844c082021-07-14 12:58:54 +0100346 ARM_COMPUTE_ERROR_ON(src->data_layout() == DataLayout::NHWC && has_padding_changed(padding_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100347}
348
Manuel Bottinid844c082021-07-14 12:58:54 +0100349Status ClIm2ColKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation,
Giorgio Arena0f170392018-07-18 16:13:12 +0100350 unsigned int num_groups)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000351{
Manuel Bottinid844c082021-07-14 12:58:54 +0100352 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, kernel_dims, conv_info, has_bias, dilation, num_groups));
353 Im2ColConfiguration im2col_config = configure_opencl_kernel(src, kernel_dims, conv_info, has_bias, dilation, num_groups);
354 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,
Giorgio Arena0f170392018-07-18 16:13:12 +0100355 im2col_config.is_padding_required_nchw, num_groups)
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100356 .first);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000357 return Status{};
358}
359
Manuel Bottinid844c082021-07-14 12:58:54 +0100360void ClIm2ColKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100361{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100362 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Manuel Bottinid844c082021-07-14 12:58:54 +0100363 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IClKernel::window(), window);
364 ARM_COMPUTE_ERROR_ON(tensors.empty());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100365
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100366 // Get initial windows
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100367 // Collapse in order to have (SRC_DEPTH * BATCH_SIZE) on the 3rd dimension
steniu01868e5412017-07-17 23:16:00 +0100368 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
steniu01868e5412017-07-17 23:16:00 +0100369 window_collapsed.set_dimension_step(Window::DimZ, 1);
370
Manuel Bottinid844c082021-07-14 12:58:54 +0100371 auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
372 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
373 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
374
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100375 Window window_output;
Manuel Bottinid844c082021-07-14 12:58:54 +0100376 window_output.use_tensor_dimensions(dst->info()->tensor_shape());
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100377
Pablo Tello4a626a72018-04-04 10:01:14 +0100378 const Window first_slice_3d = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100379
Pablo Tello4a626a72018-04-04 10:01:14 +0100380 Window slice = first_slice_3d;
381 Window slice_in = first_slice_3d;
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100382 Window slice_out = window_output.first_slice_window_2D();
Pablo Tello4a626a72018-04-04 10:01:14 +0100383
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000384 if(_data_layout == DataLayout::NHWC)
Gian Marco76faef82018-01-29 12:15:32 +0000385 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100386 const Window tmp_win = window.collapse_if_possible(ICLKernel::window(), 3);
387 const int num_batches = tmp_win[3].end();
388
Manuel Bottinid844c082021-07-14 12:58:54 +0100389 slice.set(1, Window::Dimension(0, static_cast<int>(dst->info()->tensor_shape()[1]), 1));
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100390 slice.set(2, Window::Dimension(0, static_cast<int>(num_batches), 1));
391 }
392 else
393 {
394 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));
395 slice.set(1, Window::Dimension(0, static_cast<int>(_convolved_dims.second), 1));
396 // Note: In case of NCHW the 3rd dimension is already set collapsing the input window
Gian Marco76faef82018-01-29 12:15:32 +0000397 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100398
399 // Setup input slice
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100400 // The dimensions of the input are increased within the OpenCL kernel
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100401 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
402 slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
403 slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
404
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100405 // Setup output slice
406 // The dimensions of the output are increased within the OpenCL kernel
407 slice_out.set(Window::DimX, Window::Dimension(0, 0, 0));
408 slice_out.set(Window::DimY, Window::Dimension(0, 0, 0));
409
Giorgio Arena0f170392018-07-18 16:13:12 +0100410 unsigned int idx = num_arguments_per_3D_tensor() + (_num_groups == 1 ? num_arguments_per_2D_tensor() : num_arguments_per_3D_tensor());
Manuel Bottinid844c082021-07-14 12:58:54 +0100411 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(src->info()->strides_in_bytes()[3]));
412 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(dst->info()->strides_in_bytes()[((_num_groups == 1) ? 2 : 3)]));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100413 do
414 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100415 unsigned int idx = 0;
Manuel Bottinid844c082021-07-14 12:58:54 +0100416 add_3D_tensor_argument(idx, src, slice_in);
Giorgio Arena0f170392018-07-18 16:13:12 +0100417 if(_num_groups == 1)
418 {
Manuel Bottinid844c082021-07-14 12:58:54 +0100419 add_2D_tensor_argument(idx, dst, slice_out);
Giorgio Arena0f170392018-07-18 16:13:12 +0100420 }
421 else
422 {
Manuel Bottinid844c082021-07-14 12:58:54 +0100423 add_3D_tensor_argument(idx, dst, slice_out);
Giorgio Arena0f170392018-07-18 16:13:12 +0100424 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100425 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100426 }
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100427 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 +0100428}
Manuel Bottinid844c082021-07-14 12:58:54 +0100429} // namespace kernels
430} // namespace opencl
Matthew Bentham758b5ba2020-03-05 23:37:48 +0000431} // namespace arm_compute