blob: d04c1dc00dc7d6c0a5d71dd880cf3df25031cdf9 [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
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"
30#include "arm_compute/core/Error.h"
31#include "arm_compute/core/Helpers.h"
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010032#include "arm_compute/core/Size2D.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "arm_compute/core/Types.h"
34#include "arm_compute/core/Validate.h"
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010035#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
37#include <cmath>
38#include <tuple>
39
40using namespace arm_compute;
41
Georgios Pinitas358ca202017-12-07 16:47:52 +000042namespace
43{
Alex Gilday7da29b62018-03-23 14:16:00 +000044Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, bool has_bias, const Size2D &dilation)
Georgios Pinitas358ca202017-12-07 16:47:52 +000045{
46 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 +000047 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::QASYMM8 && has_bias);
Georgios Pinitas358ca202017-12-07 16:47:52 +000048 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
Alex Gilday7da29b62018-03-23 14:16:00 +000049 ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1));
Georgios Pinitas358ca202017-12-07 16:47:52 +000050
51 // Checks performed when output is configured
52 if(output->total_size() != 0)
53 {
54 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
56 }
57
58 return Status{};
59}
60} // namespace
61
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062CLIm2ColKernel::CLIm2ColKernel()
Gian Marco76faef82018-01-29 12:15:32 +000063 : _input(nullptr), _output(nullptr), _convolved_dims(), _num_elems_processed_per_iteration(1), _run_func(nullptr), _kernel_dims()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064{
65}
66
Alex Gilday7da29b62018-03-23 14:16:00 +000067void CLIm2ColKernel::configure(const ICLTensor *input, ICLTensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068{
Georgios Pinitas358ca202017-12-07 16:47:52 +000069 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
70
71 // Perform validation step
Alex Gilday7da29b62018-03-23 14:16:00 +000072 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), has_bias, dilation));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073
Gian Marco76faef82018-01-29 12:15:32 +000074 _input = input;
75 _output = output;
76 _kernel_dims = kernel_dims;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077
Anthony Barbierfcd52fb2017-11-28 10:31:43 +000078 const DataType data_type = input->info()->data_type();
Michalis Spyroua9676112018-02-22 18:07:43 +000079 const GPUTarget gpu_target = get_target();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000081 // Create kernel
82 CLBuildOptions build_opts;
83 build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)));
Gian Marco76faef82018-01-29 12:15:32 +000084 build_opts.add_option("-DELEMENT_SIZE=" + support::cpp11::to_string(input->info()->element_size()));
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000085 build_opts.add_option_if(has_bias, "-DHAS_BIAS");
86 build_opts.add_option_if(is_data_type_fixed_point(data_type), "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()));
Gian Marco Iodice368da832017-07-03 12:33:49 +010087
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088 int stride_x = 0;
89 int stride_y = 0;
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +010090
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091 std::tie(stride_x, stride_y) = conv_info.stride();
92
93 const bool run_img2col_reduced = (output->info()->dimension(0) == (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2))) && (TensorShape::num_max_dimensions >= 4)
94 && (std::equal(input->info()->tensor_shape().cbegin() + 3,
95 input->info()->tensor_shape().cend(),
96 output->info()->tensor_shape().cbegin() + 1))
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +010097 && ((stride_x == 1) && (stride_y == 1) && !conv_info.has_padding());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098
Gian Marco76faef82018-01-29 12:15:32 +000099 bool is_optimized_path = false;
100
101 _num_elems_processed_per_iteration = 1;
102
103 std::string kernel_name;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104 if(!run_img2col_reduced)
105 {
Gian Marco76faef82018-01-29 12:15:32 +0000106 // Default kernel name
107 kernel_name = "im2col_generic_dchw";
108
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100109 _convolved_dims = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1),
110 kernel_dims.width, kernel_dims.height,
Alex Gilday7da29b62018-03-23 14:16:00 +0000111 conv_info, dilation);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000113 build_opts.add_option("-DKERNEL_WIDTH=" + support::cpp11::to_string(kernel_dims.width));
114 build_opts.add_option("-DKERNEL_HEIGHT=" + support::cpp11::to_string(kernel_dims.height));
115 build_opts.add_option("-DKERNEL_DEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
116 build_opts.add_option("-DCONVOLVED_WIDTH=" + support::cpp11::to_string(_convolved_dims.first));
117 build_opts.add_option("-DCONVOLVED_HEIGHT=" + support::cpp11::to_string(_convolved_dims.second));
118 build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_info.stride().first));
119 build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_info.stride().second));
120 build_opts.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left()));
121 build_opts.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top()));
122 build_opts.add_option("-DPAD_RIGHT=" + support::cpp11::to_string(conv_info.pad_right()));
123 build_opts.add_option("-DPAD_BOTTOM=" + support::cpp11::to_string(conv_info.pad_bottom()));
124 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input->info()->dimension(0)));
125 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
Alex Gilday7da29b62018-03-23 14:16:00 +0000126 build_opts.add_option("-DDILATION_X=" + support::cpp11::to_string(dilation.x()));
127 build_opts.add_option("-DDILATION_Y=" + support::cpp11::to_string(dilation.y()));
Chunosov5124be52017-11-22 20:42:13 +0700128 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 +0100129
Gian Marco76faef82018-01-29 12:15:32 +0000130 const bool squared_im2col = kernel_dims.width == kernel_dims.height;
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000131
Alex Gilday7da29b62018-03-23 14:16:00 +0000132 if(dilation == Size2D(1U, 1U))
Gian Marco76faef82018-01-29 12:15:32 +0000133 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000134 if(squared_im2col && !is_data_type_fixed_point(data_type))
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000135 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000136 // Check if we can run an optimized im2col
137 switch(kernel_dims.width)
138 {
139 case 1:
140 // Optimized im2col1x1 if stride_x = 1 and conv_info.has_padding() = false
141 if(conv_info.stride().first == 1 && !conv_info.has_padding())
142 {
143 // Set hint for LWS
144 _lws_hint = cl::NDRange(1, 1, 8);
145 _num_elems_processed_per_iteration = 4;
146 is_optimized_path = true;
147 kernel_name = "im2col1x1_stridex1_dchw";
148 }
149 break;
150 case 3:
Gian Marco562deff2018-02-07 10:18:06 +0000151 _lws_hint = cl::NDRange(1, 1, 8);
Gian Marco76faef82018-01-29 12:15:32 +0000152 _num_elems_processed_per_iteration = 1;
153 is_optimized_path = true;
Alex Gilday7da29b62018-03-23 14:16:00 +0000154 kernel_name = "im2col3x3_dchw";
155 break;
156 case 5:
157 _num_elems_processed_per_iteration = 1;
158 is_optimized_path = true;
159 kernel_name = "im2col5x5_dchw";
160 break;
161 case 11:
162 // Optimized im2col11x11 if pad_x = pad_y = 0
163 if(!conv_info.has_padding())
164 {
165 _num_elems_processed_per_iteration = 1;
166 is_optimized_path = true;
167 kernel_name = "im2col11x11_padx0_pady0_dchw";
168 }
169 break;
170 default:
171 is_optimized_path = false;
172 break;
173 }
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000174 }
Alex Gilday7da29b62018-03-23 14:16:00 +0000175 else if(kernel_dims.width > 1 && !conv_info.has_padding())
176 {
177 _num_elems_processed_per_iteration = 1;
178 kernel_name = "im2col_generic_padx0_pady0_dchw";
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000179
Alex Gilday7da29b62018-03-23 14:16:00 +0000180 // Optimized im2col is performed using one or more vector operations with the specified vector size
181 // and a remainder. For example, for 5x5 convolutions, im2col is performed using vectors of size 4
182 // and scalars; for 7x7 convolutions, using vectors of size 4 and vectors of size 3.
183 // Using the vector size of 4 is always safe since OpenCL supports vectors of size 2 and 3.
184 // Using the vector size of 8, however, may be faster.
185 size_t vector_size = 4;
186 // For 2x2 convolutions, use vectors of size 2. (For 3x3 convolutions, im2col_kernel3x3_padx0_pady0
187 // is used instead.)
188 if(kernel_dims.width < vector_size)
189 {
190 vector_size = kernel_dims.width;
191 }
192 // Local work size and vector size optimized for the 11x11 AlexNet convolution on Bifrost.
Sam Laynton56e8e862018-04-05 13:26:08 +0100193 if(gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, GPUTarget::TNOX) && kernel_dims.width == 11)
Alex Gilday7da29b62018-03-23 14:16:00 +0000194 {
195 _lws_hint = cl::NDRange(1, 1, 1);
196 vector_size = 8;
197 }
198 const size_t width_mod_vector_size = kernel_dims.width % vector_size;
199 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
200 build_opts.add_option("-DWIDTH_MOD_VECTOR_SIZE=" + support::cpp11::to_string(width_mod_vector_size));
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000201 }
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000202 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100203 _run_func = &CLIm2ColKernel::run_generic;
204 }
205 else
206 {
207 _num_elems_processed_per_iteration = 1;
Gian Marco76faef82018-01-29 12:15:32 +0000208 kernel_name = "im2col_reduced_dchw";
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209 _run_func = &CLIm2ColKernel::run_reduced;
210 }
211
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000212 // Create kernel
213 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
214
Alex Gilday7da29b62018-03-23 14:16:00 +0000215 // Configure kernel window
Gian Marco76faef82018-01-29 12:15:32 +0000216 Window win;
217 if(is_optimized_path)
218 {
219 win = calculate_max_window(*input->info(),
220 Steps(_num_elems_processed_per_iteration),
221 false,
222 BorderSize(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left()));
223
224 const int x = -conv_info.pad_left();
225 const int y = -conv_info.pad_top();
226 const int w = kernel_dims.width * _num_elems_processed_per_iteration;
227 const int h = kernel_dims.height;
228
229 AccessWindowRectangle input_access(input->info(), x, y, w, h);
230
231 update_window_and_padding(win, input_access);
232 }
233 else
234 {
235 // For the generic case, CLIm2ColKernel doesn't need padding (we do not read out-of-bounds elements) so
236 // update_window_and_padding() can be skipped
237 win = calculate_max_window(*input->info(), Steps());
238 }
239
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100240 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
steniu01868e5412017-07-17 23:16:00 +0100241 if(!run_img2col_reduced)
242 {
243 // set the Z dimension's step same size as the whole dimension so that one can't split across the Z dimension
244 win.set_dimension_step(Window::DimZ, win[Window::DimZ].end() - win[Window::DimZ].start());
245 }
246
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100247 ICLKernel::configure(win);
Gian Marcode691f02017-09-08 16:13:11 +0100248
249 // Set config_id for enabling LWS tuning
Gian Marco76faef82018-01-29 12:15:32 +0000250 _config_id = kernel_name;
251 _config_id += "_";
Gian Marcode691f02017-09-08 16:13:11 +0100252 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
253 _config_id += "_";
254 _config_id += support::cpp11::to_string(output->info()->dimension(0));
255 _config_id += "_";
256 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100257}
258
Alex Gilday7da29b62018-03-23 14:16:00 +0000259Status 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 +0000260{
261 ARM_COMPUTE_UNUSED(kernel_dims);
262 ARM_COMPUTE_UNUSED(conv_info);
263 ARM_COMPUTE_UNUSED(has_bias);
Alex Gilday7da29b62018-03-23 14:16:00 +0000264 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, has_bias, dilation));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000265 return Status{};
266}
267
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100268void CLIm2ColKernel::run(const Window &window, cl::CommandQueue &queue)
269{
270 ARM_COMPUTE_ERROR_ON(_run_func == nullptr);
271 (this->*_run_func)(window, queue);
272}
273
274void CLIm2ColKernel::run_generic(const Window &window, cl::CommandQueue &queue)
275{
276 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
277 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
278
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100279 // Get initial windows
steniu01868e5412017-07-17 23:16:00 +0100280 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
281 // Change the Z dimension's step back to 1
282 window_collapsed.set_dimension_step(Window::DimZ, 1);
283
284 Window slice = window_collapsed.first_slice_window_3D();
285 Window slice_in = window_collapsed.first_slice_window_3D();
286 Window slice_out = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100287
Gian Marco76faef82018-01-29 12:15:32 +0000288 // Setup slice if stride_x != 0 or stride_y != 0
289 if(_convolved_dims.first != _input->info()->dimension(0) || _convolved_dims.second != _input->info()->dimension(1))
290 {
291 // If the stride_x or stride_y are not 1, the output tensor of matrix multiply (Convolved tensor) will not
292 // have the same shape of the im2col input tensor
293 // In this case we need to re-compute the window using the shape of the tensor after matrix multiply (convolved_dims)
294 slice.set(Window::DimX, Window::Dimension(0, static_cast<int>(_convolved_dims.first), 1));
295 slice.set(Window::DimY, Window::Dimension(0, static_cast<int>(_convolved_dims.second), 1));
296 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100297
298 // Setup input slice
299 // The first three dimensions of the input are increased by the inner loops
300 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
301 slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
302 slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
303
304 // Setup output slice
Gian Marco76faef82018-01-29 12:15:32 +0000305 slice_out.set(Window::DimX, Window::Dimension(0, _output->info()->dimension(0), _kernel_dims.area()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100306 slice_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 1));
307 slice_out.set(Window::DimZ, Window::Dimension(0, 1, 1));
308
309 do
310 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100311 unsigned int idx = 0;
312 add_3D_tensor_argument(idx, _input, slice_in);
313 add_2D_tensor_argument(idx, _output, slice_out);
steniu01868e5412017-07-17 23:16:00 +0100314 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input->info()->strides_in_bytes()[3]));
315 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_output->info()->strides_in_bytes()[3]));
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100316 enqueue(queue, *this, slice, _lws_hint);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100317 }
steniu01868e5412017-07-17 23:16:00 +0100318 while(window_collapsed.slide_window_slice_3D(slice) && window_collapsed.slide_window_slice_3D(slice_out) && window_collapsed.slide_window_slice_3D(slice_in));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100319}
320
321void CLIm2ColKernel::run_reduced(const Window &window, cl::CommandQueue &queue)
322{
323 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
324 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
325
326 Window out_window;
SiCong Li86b53332017-08-23 11:02:43 +0100327 out_window.use_tensor_dimensions(_output->info()->tensor_shape());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100328
329 Window out_slice = out_window.first_slice_window_1D();
330 Window in_slice = window.first_slice_window_3D();
331
332 // Run kernel
333 do
334 {
335 // Set arguments
336 unsigned int idx = 0;
337 add_3D_tensor_argument(idx, _input, in_slice);
338 add_1D_tensor_argument(idx, _output, out_slice);
339
340 _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(0));
341 _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(1));
Gian Marcod7779da2017-11-22 14:46:28 +0000342 enqueue(queue, *this, in_slice, _lws_hint);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100343 }
344 while(window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_1D(out_slice));
345}