blob: 5d4e039e942db9878ac8280fb07f845a2fb5568b [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"
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010034#include "arm_compute/core/Size2D.h"
Pablo Tello4a626a72018-04-04 10:01:14 +010035#include "arm_compute/core/TensorInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036#include "arm_compute/core/Types.h"
Pablo Tello4a626a72018-04-04 10:01:14 +010037#include "arm_compute/core/Validate.h"
38#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010039#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040
41#include <cmath>
42#include <tuple>
43
44using namespace arm_compute;
45
Georgios Pinitas358ca202017-12-07 16:47:52 +000046namespace
47{
Alex Gilday7da29b62018-03-23 14:16:00 +000048Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, bool has_bias, const Size2D &dilation)
Georgios Pinitas358ca202017-12-07 16:47:52 +000049{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010050 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Georgios Pinitas358ca202017-12-07 16:47:52 +000051 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
Isabella Gottardie6630e42018-01-18 15:50:39 +000052 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::QASYMM8 && has_bias);
Georgios Pinitas358ca202017-12-07 16:47:52 +000053 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
Alex Gilday7da29b62018-03-23 14:16:00 +000054 ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1));
Pablo Tello4a626a72018-04-04 10:01:14 +010055 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Georgios Pinitas358ca202017-12-07 16:47:52 +000056
57 // Checks performed when output is configured
58 if(output->total_size() != 0)
59 {
60 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
62 }
63
64 return Status{};
65}
Pablo Tello4a626a72018-04-04 10:01:14 +010066
67inline bool run_im2col_reduced(ITensorInfo *input, ITensorInfo *output, const PadStrideInfo &conv_info)
68{
69 int stride_x = 0;
70 int stride_y = 0;
71
72 std::tie(stride_x, stride_y) = conv_info.stride();
73
74 return (output->dimension(0) == (input->dimension(0) * input->dimension(1) * input->dimension(2))) && (TensorShape::num_max_dimensions >= 4)
75 && (std::equal(input->tensor_shape().cbegin() + 3,
76 input->tensor_shape().cend(),
77 output->tensor_shape().cbegin() + 1))
78 && ((stride_x == 1) && (stride_y == 1) && !conv_info.has_padding());
79}
80
Georgios Pinitas358ca202017-12-07 16:47:52 +000081} // namespace
82
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083CLIm2ColKernel::CLIm2ColKernel()
Georgios Pinitas17812ba2018-06-04 19:27:13 +010084 : _input(nullptr), _output(nullptr), _conv_info(), _convolved_dims(), _num_elems_processed_per_iteration(1), _run_func(nullptr), _kernel_dims()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085{
86}
87
Pablo Tello4a626a72018-04-04 10:01:14 +010088std::string
89CLIm2ColKernel::configure_window(const ICLTensor *input, ICLTensor *output, const Size2D &kernel_dims,
90 const Size2D &dilation, const PadStrideInfo &conv_info, CLBuildOptions &build_opts)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091{
Pablo Tello4a626a72018-04-04 10:01:14 +010092 std::string kernel_name;
93 bool is_optimized_path = false;
94 const bool reduced = run_im2col_reduced(input->info(), output->info(), conv_info);
95 const DataType data_type = input->info()->data_type();
96 const bool squared_im2col = kernel_dims.width == kernel_dims.height;
97 const DataLayout data_layout = input->info()->data_layout();
Georgios Pinitas358ca202017-12-07 16:47:52 +000098
Georgios Pinitas09b19122018-06-21 13:07:35 +010099 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
100 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
101 const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
102 const unsigned int input_width = input->info()->dimension(width_idx);
103 const unsigned int input_height = input->info()->dimension(height_idx);
104 const unsigned int input_channel = input->info()->dimension(channel_idx);
105
Pablo Tello4a626a72018-04-04 10:01:14 +0100106 if(!reduced)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 {
Gian Marco76faef82018-01-29 12:15:32 +0000108 // Default kernel name
Pablo Tello4a626a72018-04-04 10:01:14 +0100109 if(data_layout == DataLayout::NCHW)
110 {
111 kernel_name = "im2col_generic_dchw";
112 }
113 else
114 {
115 kernel_name = "im2col_generic_nhwc";
116 }
Gian Marco76faef82018-01-29 12:15:32 +0000117
Pablo Tello4a626a72018-04-04 10:01:14 +0100118 _convolved_dims = scaled_dimensions(input_width, input_height, kernel_dims.width, kernel_dims.height, conv_info, dilation);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000120 build_opts.add_option("-DKERNEL_WIDTH=" + support::cpp11::to_string(kernel_dims.width));
121 build_opts.add_option("-DKERNEL_HEIGHT=" + support::cpp11::to_string(kernel_dims.height));
Pablo Tello4a626a72018-04-04 10:01:14 +0100122 build_opts.add_option("-DKERNEL_DEPTH=" + support::cpp11::to_string(input_channel));
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000123 build_opts.add_option("-DCONVOLVED_WIDTH=" + support::cpp11::to_string(_convolved_dims.first));
124 build_opts.add_option("-DCONVOLVED_HEIGHT=" + support::cpp11::to_string(_convolved_dims.second));
125 build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_info.stride().first));
126 build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_info.stride().second));
127 build_opts.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left()));
128 build_opts.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top()));
129 build_opts.add_option("-DPAD_RIGHT=" + support::cpp11::to_string(conv_info.pad_right()));
130 build_opts.add_option("-DPAD_BOTTOM=" + support::cpp11::to_string(conv_info.pad_bottom()));
Pablo Tello4a626a72018-04-04 10:01:14 +0100131 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input_width));
132 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input_height));
Alex Gilday7da29b62018-03-23 14:16:00 +0000133 build_opts.add_option("-DDILATION_X=" + support::cpp11::to_string(dilation.x()));
134 build_opts.add_option("-DDILATION_Y=" + support::cpp11::to_string(dilation.y()));
Chunosov5124be52017-11-22 20:42:13 +0700135 build_opts.add_option_if_else(is_data_type_quantized(data_type), "-DPAD_VALUE=" + support::cpp11::to_string(input->info()->quantization_info().offset), "-DPAD_VALUE=0");
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100136
Alex Gilday7da29b62018-03-23 14:16:00 +0000137 if(dilation == Size2D(1U, 1U))
Gian Marco76faef82018-01-29 12:15:32 +0000138 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000139 if(squared_im2col && !is_data_type_fixed_point(data_type))
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000140 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000141 // Check if we can run an optimized im2col
142 switch(kernel_dims.width)
143 {
144 case 1:
145 // Optimized im2col1x1 if stride_x = 1 and conv_info.has_padding() = false
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100146 if(conv_info.stride().first == 1 && !conv_info.has_padding() && data_layout == DataLayout::NCHW)
Alex Gilday7da29b62018-03-23 14:16:00 +0000147 {
148 // Set hint for LWS
149 _lws_hint = cl::NDRange(1, 1, 8);
150 _num_elems_processed_per_iteration = 4;
151 is_optimized_path = true;
152 kernel_name = "im2col1x1_stridex1_dchw";
153 }
154 break;
155 case 3:
Gian Marco562deff2018-02-07 10:18:06 +0000156 _lws_hint = cl::NDRange(1, 1, 8);
Gian Marco76faef82018-01-29 12:15:32 +0000157 _num_elems_processed_per_iteration = 1;
158 is_optimized_path = true;
Pablo Tello4a626a72018-04-04 10:01:14 +0100159 switch(data_layout)
160 {
161 case DataLayout::NCHW:
162 kernel_name = "im2col3x3_dchw";
163 break;
164 case DataLayout::NHWC:
165 kernel_name = "im2col3x3_nhwc";
166 break;
167 default:
168 ARM_COMPUTE_ERROR("Not supported.");
169 break;
170 }
Alex Gilday7da29b62018-03-23 14:16:00 +0000171 break;
172 case 5:
173 _num_elems_processed_per_iteration = 1;
Pablo Tello4a626a72018-04-04 10:01:14 +0100174 switch(data_layout)
175 {
176 case DataLayout::NCHW:
Georgios Pinitas9e454f32018-06-29 12:51:22 +0100177 is_optimized_path = true;
178 kernel_name = "im2col5x5_dchw";
Pablo Tello4a626a72018-04-04 10:01:14 +0100179 break;
180 default:
181 // using generic_nhwc
Georgios Pinitas9e454f32018-06-29 12:51:22 +0100182 is_optimized_path = false;
Pablo Tello4a626a72018-04-04 10:01:14 +0100183 break;
184 }
Alex Gilday7da29b62018-03-23 14:16:00 +0000185 break;
186 case 11:
Georgios Pinitas9e454f32018-06-29 12:51:22 +0100187 _num_elems_processed_per_iteration = 1;
Alex Gilday7da29b62018-03-23 14:16:00 +0000188 // Optimized im2col11x11 if pad_x = pad_y = 0
Georgios Pinitas9e454f32018-06-29 12:51:22 +0100189 if(!conv_info.has_padding() && data_layout == DataLayout::NCHW)
Alex Gilday7da29b62018-03-23 14:16:00 +0000190 {
Georgios Pinitas9e454f32018-06-29 12:51:22 +0100191 is_optimized_path = true;
192 kernel_name = "im2col11x11_padx0_pady0_dchw";
Alex Gilday7da29b62018-03-23 14:16:00 +0000193 }
194 break;
195 default:
196 is_optimized_path = false;
197 break;
198 }
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000199 }
Alex Gilday7da29b62018-03-23 14:16:00 +0000200 else if(kernel_dims.width > 1 && !conv_info.has_padding())
201 {
202 _num_elems_processed_per_iteration = 1;
Pablo Tello4a626a72018-04-04 10:01:14 +0100203 is_optimized_path = false;
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000204
Pablo Tello4a626a72018-04-04 10:01:14 +0100205 if(data_layout == DataLayout::NCHW)
Alex Gilday7da29b62018-03-23 14:16:00 +0000206 {
Pablo Tello4a626a72018-04-04 10:01:14 +0100207 kernel_name = "im2col_generic_padx0_pady0_dchw";
208
209 // Optimized im2col is performed using one or more vector operations with the specified vector size
210 // and a remainder. For example, for 5x5 convolutions, im2col is performed using vectors of size 4
211 // and scalars; for 7x7 convolutions, using vectors of size 4 and vectors of size 3.
212 // Using the vector size of 4 is always safe since OpenCL supports vectors of size 2 and 3.
213 // Using the vector size of 8, however, may be faster.
214 size_t vector_size = 4;
215 // For 2x2 convolutions, use vectors of size 2. (For 3x3 convolutions, im2col_kernel3x3_padx0_pady0
216 // is used instead.)
217 if(kernel_dims.width < vector_size)
218 {
219 vector_size = kernel_dims.width;
220 }
221 // Vector size optimized for the 11x11 AlexNet convolution on Bifrost.
222 const GPUTarget gpu_target = get_target();
223 if(gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, GPUTarget::TNOX) && kernel_dims.width == 11)
224 {
225 vector_size = 8;
226 }
227 const size_t width_mod_vector_size = kernel_dims.width % vector_size;
228 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
229 build_opts.add_option("-DWIDTH_MOD_VECTOR_SIZE=" + support::cpp11::to_string(width_mod_vector_size));
Alex Gilday7da29b62018-03-23 14:16:00 +0000230 }
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000231 }
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000232 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100233 _run_func = &CLIm2ColKernel::run_generic;
234 }
235 else
236 {
237 _num_elems_processed_per_iteration = 1;
Gian Marco76faef82018-01-29 12:15:32 +0000238 kernel_name = "im2col_reduced_dchw";
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100239 _run_func = &CLIm2ColKernel::run_reduced;
240 }
Alex Gilday7da29b62018-03-23 14:16:00 +0000241 // Configure kernel window
Gian Marco76faef82018-01-29 12:15:32 +0000242 Window win;
243 if(is_optimized_path)
244 {
Pablo Tello4a626a72018-04-04 10:01:14 +0100245 if(data_layout == DataLayout::NHWC)
246 {
247 win = calculate_max_window(*input->info(),
248 Steps(_num_elems_processed_per_iteration),
249 false,
250 BorderSize(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left()));
251 const int x = -conv_info.pad_left();
252 const int y = -conv_info.pad_top();
253 const int h = kernel_dims.width * _num_elems_processed_per_iteration;
254 const int w = 1;
255 AccessWindowRectangle input_access(input->info(), x, y, w, h);
256 update_window_and_padding(win, input_access);
257 }
258 else
259 {
Georgios Pinitas09b19122018-06-21 13:07:35 +0100260 const BorderSize border(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left());
Pablo Tello4a626a72018-04-04 10:01:14 +0100261 win = calculate_max_window(*input->info(),
Georgios Pinitas09b19122018-06-21 13:07:35 +0100262 Steps(_num_elems_processed_per_iteration * conv_info.stride().first, conv_info.stride().second));
263 AccessWindowStatic input_access(input->info(),
264 -border.left,
265 -border.top,
266 ceil_to_multiple(input_width + border.right, kernel_dims.width),
267 input_height + border.bottom);
Pablo Tello4a626a72018-04-04 10:01:14 +0100268 update_window_and_padding(win, input_access);
269 }
Gian Marco76faef82018-01-29 12:15:32 +0000270 }
271 else
272 {
273 // For the generic case, CLIm2ColKernel doesn't need padding (we do not read out-of-bounds elements) so
274 // update_window_and_padding() can be skipped
275 win = calculate_max_window(*input->info(), Steps());
276 }
277
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100278 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
Pablo Tello4a626a72018-04-04 10:01:14 +0100279 if(!reduced)
steniu01868e5412017-07-17 23:16:00 +0100280 {
281 // set the Z dimension's step same size as the whole dimension so that one can't split across the Z dimension
282 win.set_dimension_step(Window::DimZ, win[Window::DimZ].end() - win[Window::DimZ].start());
283 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100284 ICLKernel::configure(win);
Pablo Tello4a626a72018-04-04 10:01:14 +0100285 return kernel_name;
286}
287
288void CLIm2ColKernel::configure(const ICLTensor *input, ICLTensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation)
289{
290 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
291 ARM_COMPUTE_ERROR_ON(input->info()->data_layout() == DataLayout::UNKNOWN);
292 ARM_COMPUTE_ERROR_ON_MSG(output->info()->data_layout() != DataLayout::NCHW, "Special case Im2Col output layout is NCHW");
293 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), has_bias, dilation));
294
295 _input = input;
296 _output = output;
297 _kernel_dims = kernel_dims;
298 _conv_info = conv_info;
299
300 const DataType data_type = input->info()->data_type();
301
302 // Create kernel
303 CLBuildOptions build_opts;
304 build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)));
305 build_opts.add_option("-DELEMENT_SIZE=" + support::cpp11::to_string(input->info()->element_size()));
306 build_opts.add_option_if(has_bias, "-DHAS_BIAS");
307 build_opts.add_option_if(is_data_type_fixed_point(data_type), "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()));
308
309 _num_elems_processed_per_iteration = 1;
310
311 const std::string kernel_name = configure_window(input, output, kernel_dims, dilation, conv_info, build_opts);
312 // Create kernel
313 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Gian Marcode691f02017-09-08 16:13:11 +0100314
315 // Set config_id for enabling LWS tuning
Gian Marco76faef82018-01-29 12:15:32 +0000316 _config_id = kernel_name;
317 _config_id += "_";
Gian Marcode691f02017-09-08 16:13:11 +0100318 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
319 _config_id += "_";
320 _config_id += support::cpp11::to_string(output->info()->dimension(0));
321 _config_id += "_";
322 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Giorgio Arena00b93f52018-06-28 17:18:50 +0100323 _config_id += "_";
324 _config_id += lower_string(string_from_data_layout(input->info()->data_layout()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100325}
326
Alex Gilday7da29b62018-03-23 14:16:00 +0000327Status CLIm2ColKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000328{
329 ARM_COMPUTE_UNUSED(kernel_dims);
330 ARM_COMPUTE_UNUSED(conv_info);
331 ARM_COMPUTE_UNUSED(has_bias);
Alex Gilday7da29b62018-03-23 14:16:00 +0000332 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, has_bias, dilation));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000333 return Status{};
334}
335
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100336void CLIm2ColKernel::run(const Window &window, cl::CommandQueue &queue)
337{
338 ARM_COMPUTE_ERROR_ON(_run_func == nullptr);
339 (this->*_run_func)(window, queue);
340}
341
342void CLIm2ColKernel::run_generic(const Window &window, cl::CommandQueue &queue)
343{
344 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
345 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
346
Pablo Tello4a626a72018-04-04 10:01:14 +0100347 const DataLayout data_layout = _input->info()->data_layout();
348 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
349 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
350
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100351 // Get initial windows
steniu01868e5412017-07-17 23:16:00 +0100352 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
353 // Change the Z dimension's step back to 1
354 window_collapsed.set_dimension_step(Window::DimZ, 1);
355
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100356 Window window_output;
357 window_output.use_tensor_dimensions(_output->info()->tensor_shape());
358
Pablo Tello4a626a72018-04-04 10:01:14 +0100359 const Window first_slice_3d = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100360
Pablo Tello4a626a72018-04-04 10:01:14 +0100361 Window slice = first_slice_3d;
362 Window slice_in = first_slice_3d;
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100363 Window slice_out = window_output.first_slice_window_2D();
Pablo Tello4a626a72018-04-04 10:01:14 +0100364
365 const bool out_dim_not_same_input_dim = _convolved_dims.first != _input->info()->dimension(width_idx) || _convolved_dims.second != _input->info()->dimension(height_idx);
366
367 // Setup slice if convolved dims are not the same as input dims
368 if(out_dim_not_same_input_dim)
Gian Marco76faef82018-01-29 12:15:32 +0000369 {
370 // If the stride_x or stride_y are not 1, the output tensor of matrix multiply (Convolved tensor) will not
371 // have the same shape of the im2col input tensor
372 // In this case we need to re-compute the window using the shape of the tensor after matrix multiply (convolved_dims)
Pablo Tello4a626a72018-04-04 10:01:14 +0100373 slice.set(width_idx, Window::Dimension(0, static_cast<int>(_convolved_dims.first), 1));
374 if(data_layout == DataLayout::NHWC)
375 {
376 // if layout is NHWC, we need to multiply convolved_dims.height by the number of batches as for this
377 // format we collapsed HEIGHT and all subsequent dimensions (batches) together. This is necessary to ensure
378 // global_id(2) values are in the correct range.
379 const Window tmp_win = window.collapse_if_possible(ICLKernel::window(), 3);
380 const int num_batches = tmp_win[3].end();
381 slice.set(height_idx, Window::Dimension(0, static_cast<int>(_convolved_dims.second) * num_batches, 1));
382 }
383 else
384 {
385 slice.set(height_idx, Window::Dimension(0, static_cast<int>(_convolved_dims.second), 1));
386 }
Gian Marco76faef82018-01-29 12:15:32 +0000387 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100388
389 // Setup input slice
390 // The first three dimensions of the input are increased by the inner loops
391 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
392 slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
393 slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
394
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100395 do
396 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100397 unsigned int idx = 0;
398 add_3D_tensor_argument(idx, _input, slice_in);
399 add_2D_tensor_argument(idx, _output, slice_out);
steniu01868e5412017-07-17 23:16:00 +0100400 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input->info()->strides_in_bytes()[3]));
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100401 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_output->info()->strides_in_bytes()[2]));
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100402 enqueue(queue, *this, slice, _lws_hint);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100403 }
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100404 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 Barbier6ff3b192017-09-04 18:44:23 +0100405}
406
407void CLIm2ColKernel::run_reduced(const Window &window, cl::CommandQueue &queue)
408{
409 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
410 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
411
412 Window out_window;
SiCong Li86b53332017-08-23 11:02:43 +0100413 out_window.use_tensor_dimensions(_output->info()->tensor_shape());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100414
415 Window out_slice = out_window.first_slice_window_1D();
416 Window in_slice = window.first_slice_window_3D();
417
418 // Run kernel
419 do
420 {
421 // Set arguments
422 unsigned int idx = 0;
423 add_3D_tensor_argument(idx, _input, in_slice);
424 add_1D_tensor_argument(idx, _output, out_slice);
425
426 _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(0));
427 _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(1));
Gian Marcod7779da2017-11-22 14:46:28 +0000428 enqueue(queue, *this, in_slice, _lws_hint);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100429 }
430 while(window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_1D(out_slice));
431}