blob: e3d8df53e5fc56cd75b1916de3ee3fe6146c330f [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Isabella Gottardie6630e42018-01-18 15:50:39 +00002 * Copyright (c) 2017-2018 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 */
24#include "arm_compute/core/CL/kernels/CLIm2ColKernel.h"
25
Georgios Pinitas09b19122018-06-21 13:07:35 +010026#include "arm_compute/core/AccessWindowStatic.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010029#include "arm_compute/core/CL/CLValidate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/CL/ICLTensor.h"
31#include "arm_compute/core/CL/OpenCL.h"
32#include "arm_compute/core/Error.h"
33#include "arm_compute/core/Helpers.h"
Pablo Tello4a626a72018-04-04 10:01:14 +010034#include "arm_compute/core/TensorInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035#include "arm_compute/core/Types.h"
Pablo Tello4a626a72018-04-04 10:01:14 +010036#include "arm_compute/core/Validate.h"
37#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010038#include "support/ToolchainSupport.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
44using namespace arm_compute;
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010045using namespace arm_compute::misc::shape_calculator;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046
Georgios Pinitas358ca202017-12-07 16:47:52 +000047namespace
48{
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010049struct Im2ColConfiguration
50{
51 std::string kernel_name{};
52 std::set<std::string> build_options{};
53 unsigned int num_elems_processed_per_iteration{};
54 bool is_padding_required_nchw{};
55};
56
Giorgio Arena0f170392018-07-18 16:13:12 +010057Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation,
58 unsigned int num_groups)
Georgios Pinitas358ca202017-12-07 16:47:52 +000059{
Giorgio Arena0f170392018-07-18 16:13:12 +010060 const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
61
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010062 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010063 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Isabella Gottardie6630e42018-01-18 15:50:39 +000064 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::QASYMM8 && has_bias);
Georgios Pinitas358ca202017-12-07 16:47:52 +000065 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
Alex Gilday7da29b62018-03-23 14:16:00 +000066 ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1));
Pablo Tello4a626a72018-04-04 10:01:14 +010067 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Giorgio Arena0f170392018-07-18 16:13:12 +010068 ARM_COMPUTE_RETURN_ERROR_ON(num_groups == 0);
69 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::NHWC && num_groups > 1);
70 ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(channel_idx) % num_groups) != 0);
Georgios Pinitas358ca202017-12-07 16:47:52 +000071
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010072 if(output->total_size() > 0)
Georgios Pinitas358ca202017-12-07 16:47:52 +000073 {
Giorgio Arena0f170392018-07-18 16:13:12 +010074 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 +010075 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
Georgios Pinitas358ca202017-12-07 16:47:52 +000076 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitas358ca202017-12-07 16:47:52 +000077 }
78
79 return Status{};
80}
Pablo Tello4a626a72018-04-04 10:01:14 +010081
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010082std::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 +010083 unsigned int num_elems_processed_per_iteration, bool is_padding_required_nchw, unsigned int num_groups)
Pablo Tello4a626a72018-04-04 10:01:14 +010084{
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010085 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Pablo Tello4a626a72018-04-04 10:01:14 +010086
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010087 // Output tensor auto initialization if not yet initialized
Giorgio Arena0f170392018-07-18 16:13:12 +010088 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 +010089
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010090 auto_init_if_empty(*output, input->clone()->set_tensor_shape(expected_output_shape));
Pablo Tello4a626a72018-04-04 10:01:14 +010091
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010092 const DataLayout data_layout = input->data_layout();
93 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
94 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
95 const unsigned int input_width = input->dimension(width_idx);
96 const unsigned int input_height = input->dimension(height_idx);
Georgios Pinitas358ca202017-12-07 16:47:52 +000097
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010098 // Configure the execute window based on the selected optimal OpenCL kernel
99 bool window_changed = false;
100 Window win;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100102 if(data_layout == DataLayout::NHWC)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100104 win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
105
106 const int xin_start = 0;
107 const int xin_end = input->dimension(0) < num_elems_processed_per_iteration ? ceil_to_multiple(input->dimension(0), num_elems_processed_per_iteration) : input->dimension(0);
108 const int yin_start = 0;
109 const int yin_end = input->dimension(1);
110
111 const int xout_start = 0;
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000112 const int xout_end = input->dimension(0) < num_elems_processed_per_iteration ? output->dimension(0) + (num_elems_processed_per_iteration - input->dimension(0)) : output->dimension(0);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100113 const int yout_start = 0;
114 const int yout_end = output->dimension(1);
115
116 AccessWindowStatic input_access(input, xin_start, yin_start, xin_end, yin_end);
117 AccessWindowStatic output_access(output, xout_start, yout_start, xout_end, yout_end);
118 window_changed = window_changed || update_window_and_padding(win, input_access, output_access);
119 }
120 else
121 {
122 if(is_padding_required_nchw)
Pablo Tello4a626a72018-04-04 10:01:14 +0100123 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100124 const BorderSize border(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left());
125 win = calculate_max_window(*input,
126 Steps(num_elems_processed_per_iteration * conv_info.stride().first, conv_info.stride().second));
127 AccessWindowStatic input_access(input,
128 -border.left,
129 -border.top,
130 ceil_to_multiple(input_width + border.right, kernel_dims.width * num_elems_processed_per_iteration),
131 input_height + border.bottom);
132 window_changed = window_changed || update_window_and_padding(win, input_access);
Pablo Tello4a626a72018-04-04 10:01:14 +0100133 }
134 else
135 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100136 // For the generic case, CLIm2ColKernel doesn't need padding (we do not read out-of-bounds elements) so
137 // update_window_and_padding() can be skipped
138 win = calculate_max_window(*input, Steps());
139 }
140 }
141
142 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
143 // set the Z dimension's step same size as the whole dimension so that one can't split across the Z dimension
144 win.set_dimension_step(Window::DimZ, win[Window::DimZ].end() - win[Window::DimZ].start());
145
146 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
147 return std::make_pair(err, win);
148}
149
Giorgio Arena0f170392018-07-18 16:13:12 +0100150Im2ColConfiguration 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 +0100151{
152 const DataLayout data_layout = input->data_layout();
153 const DataType data_type = input->data_type();
154 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
155 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
156 const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
157 const unsigned int input_width = input->dimension(width_idx);
158 const unsigned int input_height = input->dimension(height_idx);
159 const unsigned int input_channel = input->dimension(channel_idx);
160
161 const std::pair<unsigned int, unsigned int> convolved_dims = scaled_dimensions(input_width, input_height, kernel_dims.width, kernel_dims.height, conv_info, dilation);
162
163 // Im2Col configuration
164 std::string kernel_name = "im2col_generic_";
165 CLBuildOptions build_opts;
166 unsigned int num_elems_processed_per_iteration = 1;
167 bool is_padding_required_nchw = false;
168
169 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
170 build_opts.add_option("-DELEMENT_SIZE=" + support::cpp11::to_string(input->element_size()));
171 build_opts.add_option("-DKERNEL_WIDTH=" + support::cpp11::to_string(kernel_dims.width));
172 build_opts.add_option("-DKERNEL_HEIGHT=" + support::cpp11::to_string(kernel_dims.height));
173 build_opts.add_option("-DCONVOLVED_WIDTH=" + support::cpp11::to_string(convolved_dims.first));
174 build_opts.add_option("-DCONVOLVED_HEIGHT=" + support::cpp11::to_string(convolved_dims.second));
175 build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_info.stride().first));
176 build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_info.stride().second));
177 build_opts.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left()));
178 build_opts.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top()));
179 build_opts.add_option("-DPAD_RIGHT=" + support::cpp11::to_string(conv_info.pad_right()));
180 build_opts.add_option("-DPAD_BOTTOM=" + support::cpp11::to_string(conv_info.pad_bottom()));
181 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input_width));
182 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input_height));
183 build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(input_channel));
184 build_opts.add_option("-DDILATION_X=" + support::cpp11::to_string(dilation.x()));
185 build_opts.add_option("-DDILATION_Y=" + support::cpp11::to_string(dilation.y()));
Giorgio Arena0f170392018-07-18 16:13:12 +0100186 build_opts.add_option_if(num_groups > 1, "-DNUM_GROUPS=" + support::cpp11::to_string(num_groups));
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100187 build_opts.add_option_if_else(is_data_type_quantized(data_type), "-DPAD_VALUE=" + support::cpp11::to_string(input->quantization_info().offset), "-DPAD_VALUE=0");
188 build_opts.add_option_if(has_bias, "-DHAS_BIAS");
189
190 if(data_layout == DataLayout::NHWC)
191 {
192 num_elems_processed_per_iteration = 2;
193 is_padding_required_nchw = false;
194
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000195 // Only the 3x3 and 9x9 cases are optimized for NHWC
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100196 if(kernel_dims == Size2D(3U, 3U))
197 {
198 kernel_name = "im2col3x3_";
Pablo Tello4a626a72018-04-04 10:01:14 +0100199 }
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000200 else if(kernel_dims == Size2D(9U, 9U))
201 {
202 kernel_name = "im2col9x9_";
203 }
Gian Marco76faef82018-01-29 12:15:32 +0000204
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100205 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
206 build_opts.add_option("-DLAST_ACCESSED=" + support::cpp11::to_string(std::max(static_cast<int>(input_channel - num_elems_processed_per_iteration), 0)));
207 }
208 else
209 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000210 if(dilation == Size2D(1U, 1U))
Gian Marco76faef82018-01-29 12:15:32 +0000211 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100212 const bool squared_im2col = kernel_dims.width == kernel_dims.height;
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100213 if(squared_im2col)
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000214 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100215 // Check if we can run an optimized im2col for NCHW
Alex Gilday7da29b62018-03-23 14:16:00 +0000216 switch(kernel_dims.width)
217 {
218 case 1:
219 // Optimized im2col1x1 if stride_x = 1 and conv_info.has_padding() = false
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100220 if(conv_info.stride().first == 1 && !conv_info.has_padding())
Alex Gilday7da29b62018-03-23 14:16:00 +0000221 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100222 kernel_name = "im2col1x1_stridex1_";
223 num_elems_processed_per_iteration = 4;
224 is_padding_required_nchw = true;
Alex Gilday7da29b62018-03-23 14:16:00 +0000225 }
226 break;
227 case 3:
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100228 kernel_name = "im2col3x3_";
229 num_elems_processed_per_iteration = 1;
230 is_padding_required_nchw = true;
Alex Gilday7da29b62018-03-23 14:16:00 +0000231 break;
232 case 5:
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100233 kernel_name = "im2col5x5_";
234 num_elems_processed_per_iteration = 1;
235 is_padding_required_nchw = true;
Alex Gilday7da29b62018-03-23 14:16:00 +0000236 break;
237 case 11:
238 // Optimized im2col11x11 if pad_x = pad_y = 0
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100239 if(!conv_info.has_padding())
Alex Gilday7da29b62018-03-23 14:16:00 +0000240 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100241 kernel_name = "im2col11x11_padx0_pady0_";
242 num_elems_processed_per_iteration = 1;
243 is_padding_required_nchw = true;
Alex Gilday7da29b62018-03-23 14:16:00 +0000244 }
245 break;
246 default:
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100247 kernel_name = "im2col_generic_";
248 num_elems_processed_per_iteration = 1;
249 is_padding_required_nchw = false;
Alex Gilday7da29b62018-03-23 14:16:00 +0000250 break;
251 }
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000252 }
Alex Gilday7da29b62018-03-23 14:16:00 +0000253 else if(kernel_dims.width > 1 && !conv_info.has_padding())
254 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100255 kernel_name = "im2col_generic_padx0_pady0_";
256 num_elems_processed_per_iteration = 1;
257 is_padding_required_nchw = false;
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000258
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100259 // Optimized im2col is performed using one or more vector operations with the specified vector size
260 // and a remainder. For example, for 5x5 convolutions, im2col is performed using vectors of size 4
261 // and scalars; for 7x7 convolutions, using vectors of size 4 and vectors of size 3.
262 // Using the vector size of 4 is always safe since OpenCL supports vectors of size 2 and 3.
263 // Using the vector size of 8, however, may be faster.
264 // For 2x2 convolutions, use vectors of size 2. (For 3x3 convolutions, im2col_kernel3x3_padx0_pady0
265 // is used instead.)
266 const size_t vector_size = std::min(static_cast<size_t>(4), kernel_dims.width);
267 const size_t width_mod_vector_size = kernel_dims.width % vector_size;
268 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
269 build_opts.add_option("-DWIDTH_MOD_VECTOR_SIZE=" + support::cpp11::to_string(width_mod_vector_size));
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000270 }
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000271 }
Gian Marco76faef82018-01-29 12:15:32 +0000272 }
273
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100274 // Append the data layout to the kernel_name
275 kernel_name += lower_string(string_from_data_layout(data_layout));
276
277 Im2ColConfiguration im2col_config;
278 im2col_config.kernel_name = kernel_name;
279 im2col_config.build_options = build_opts.options();
280 im2col_config.num_elems_processed_per_iteration = num_elems_processed_per_iteration;
281 im2col_config.is_padding_required_nchw = is_padding_required_nchw;
282
283 return im2col_config;
284}
285} // namespace
286
287CLIm2ColKernel::CLIm2ColKernel()
Giorgio Arena0f170392018-07-18 16:13:12 +0100288 : _input(nullptr), _output(nullptr), _convolved_dims(), _num_elems_processed_per_iteration(1), _kernel_dims(), _conv_info(), _num_groups()
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100289{
Pablo Tello4a626a72018-04-04 10:01:14 +0100290}
291
Giorgio Arena0f170392018-07-18 16:13:12 +0100292void CLIm2ColKernel::configure(const ICLTensor *input, ICLTensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation,
293 unsigned int num_groups)
Pablo Tello4a626a72018-04-04 10:01:14 +0100294{
295 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Giorgio Arena0f170392018-07-18 16:13:12 +0100296 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 +0100297
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100298 const DataLayout data_layout = input->info()->data_layout();
299 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
300 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
301 const unsigned int input_width = input->info()->dimension(width_idx);
302 const unsigned int input_height = input->info()->dimension(height_idx);
Pablo Tello4a626a72018-04-04 10:01:14 +0100303
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100304 // Select and configure the optimal OpenCL kernel to run.
305 // This function returns the OpenCL kernel's name, the arguments to pass at compile time, the number of elements processed per iteration
306 // and the padding requirement flag
Giorgio Arena0f170392018-07-18 16:13:12 +0100307 Im2ColConfiguration im2col_config = configure_opencl_kernel(input->info(), kernel_dims, conv_info, has_bias, dilation, num_groups);
Pablo Tello4a626a72018-04-04 10:01:14 +0100308
309 // Create kernel
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100310 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(im2col_config.kernel_name, im2col_config.build_options));
Pablo Tello4a626a72018-04-04 10:01:14 +0100311
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100312 _input = input;
313 _output = output;
314 _convolved_dims = scaled_dimensions(input_width, input_height, kernel_dims.width, kernel_dims.height, conv_info, dilation);
315 _num_elems_processed_per_iteration = im2col_config.num_elems_processed_per_iteration;
316 _kernel_dims = kernel_dims; // Only needed by the Tuner
317 _conv_info = conv_info; // Only needed by the Tuner
Giorgio Arena0f170392018-07-18 16:13:12 +0100318 _num_groups = num_groups;
Pablo Tello4a626a72018-04-04 10:01:14 +0100319
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100320 // Configure kernel window
321 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 +0100322 im2col_config.is_padding_required_nchw, num_groups);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100323 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100324 ICLKernel::configure_internal(win_config.second);
Gian Marcode691f02017-09-08 16:13:11 +0100325
326 // Set config_id for enabling LWS tuning
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100327 _config_id = im2col_config.kernel_name;
Gian Marco76faef82018-01-29 12:15:32 +0000328 _config_id += "_";
Gian Marcode691f02017-09-08 16:13:11 +0100329 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
330 _config_id += "_";
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100331 _config_id += support::cpp11::to_string(num_groups);
332 _config_id += "_";
Gian Marcode691f02017-09-08 16:13:11 +0100333 _config_id += support::cpp11::to_string(output->info()->dimension(0));
334 _config_id += "_";
335 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Giorgio Arena00b93f52018-06-28 17:18:50 +0100336 _config_id += "_";
337 _config_id += lower_string(string_from_data_layout(input->info()->data_layout()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100338}
339
Giorgio Arena0f170392018-07-18 16:13:12 +0100340Status CLIm2ColKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation,
341 unsigned int num_groups)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000342{
Giorgio Arena0f170392018-07-18 16:13:12 +0100343 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, kernel_dims, conv_info, has_bias, dilation, num_groups));
344 Im2ColConfiguration im2col_config = configure_opencl_kernel(input, kernel_dims, conv_info, has_bias, dilation, num_groups);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100345 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 +0100346 im2col_config.is_padding_required_nchw, num_groups)
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100347 .first);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000348 return Status{};
349}
350
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100351void CLIm2ColKernel::run(const Window &window, cl::CommandQueue &queue)
352{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100353 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
354 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
355
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100356 // Get initial windows
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100357 // Collapse in order to have (SRC_DEPTH * BATCH_SIZE) on the 3rd dimension
steniu01868e5412017-07-17 23:16:00 +0100358 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
steniu01868e5412017-07-17 23:16:00 +0100359 window_collapsed.set_dimension_step(Window::DimZ, 1);
360
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100361 Window window_output;
362 window_output.use_tensor_dimensions(_output->info()->tensor_shape());
363
Pablo Tello4a626a72018-04-04 10:01:14 +0100364 const Window first_slice_3d = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100365
Pablo Tello4a626a72018-04-04 10:01:14 +0100366 Window slice = first_slice_3d;
367 Window slice_in = first_slice_3d;
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100368 Window slice_out = window_output.first_slice_window_2D();
Pablo Tello4a626a72018-04-04 10:01:14 +0100369
Giorgio Arena0f170392018-07-18 16:13:12 +0100370 if(_input->info()->data_layout() == DataLayout::NHWC)
Gian Marco76faef82018-01-29 12:15:32 +0000371 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100372 const Window tmp_win = window.collapse_if_possible(ICLKernel::window(), 3);
373 const int num_batches = tmp_win[3].end();
374
375 slice.set(1, Window::Dimension(0, static_cast<int>(_output->info()->tensor_shape()[1]), 1));
376 slice.set(2, Window::Dimension(0, static_cast<int>(num_batches), 1));
377 }
378 else
379 {
380 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));
381 slice.set(1, Window::Dimension(0, static_cast<int>(_convolved_dims.second), 1));
382 // Note: In case of NCHW the 3rd dimension is already set collapsing the input window
Gian Marco76faef82018-01-29 12:15:32 +0000383 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100384
385 // Setup input slice
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100386 // The dimensions of the input are increased within the OpenCL kernel
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100387 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
388 slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
389 slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
390
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100391 // Setup output slice
392 // The dimensions of the output are increased within the OpenCL kernel
393 slice_out.set(Window::DimX, Window::Dimension(0, 0, 0));
394 slice_out.set(Window::DimY, Window::Dimension(0, 0, 0));
395
Giorgio Arena0f170392018-07-18 16:13:12 +0100396 unsigned int idx = num_arguments_per_3D_tensor() + (_num_groups == 1 ? num_arguments_per_2D_tensor() : num_arguments_per_3D_tensor());
397 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input->info()->strides_in_bytes()[3]));
398 _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 +0100399 do
400 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100401 unsigned int idx = 0;
402 add_3D_tensor_argument(idx, _input, slice_in);
Giorgio Arena0f170392018-07-18 16:13:12 +0100403 if(_num_groups == 1)
404 {
405 add_2D_tensor_argument(idx, _output, slice_out);
406 }
407 else
408 {
409 add_3D_tensor_argument(idx, _output, slice_out);
410 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100411 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100412 }
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100413 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 +0100414}