blob: 0e9f2c5344a531e34642b3fba95f2dd4f69d4fec [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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{
44Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
45{
46 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
47 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
48
49 // Checks performed when output is configured
50 if(output->total_size() != 0)
51 {
52 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
53 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
54 }
55
56 return Status{};
57}
58} // namespace
59
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060CLIm2ColKernel::CLIm2ColKernel()
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010061 : _input(nullptr), _output(nullptr), _convolved_dims(), _num_elems_processed_per_iteration(1), _run_func(nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062{
63}
64
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010065void CLIm2ColKernel::configure(const ICLTensor *input, ICLTensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066{
Georgios Pinitas358ca202017-12-07 16:47:52 +000067 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
68
69 // Perform validation step
70 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071
72 _input = input;
73 _output = output;
74
Anthony Barbierfcd52fb2017-11-28 10:31:43 +000075 const DataType data_type = input->info()->data_type();
76 const GPUTarget gpu_target = get_arch_from_target(get_target());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000078 // Create kernel
79 CLBuildOptions build_opts;
80 build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)));
81 build_opts.add_option_if(has_bias, "-DHAS_BIAS");
82 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 +010083
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 int stride_x = 0;
85 int stride_y = 0;
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +010086
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087 std::tie(stride_x, stride_y) = conv_info.stride();
88
89 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)
90 && (std::equal(input->info()->tensor_shape().cbegin() + 3,
91 input->info()->tensor_shape().cend(),
92 output->info()->tensor_shape().cbegin() + 1))
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +010093 && ((stride_x == 1) && (stride_y == 1) && !conv_info.has_padding());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000095 std::string kernel_name = "im2col_generic";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096 if(!run_img2col_reduced)
97 {
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010098 _convolved_dims = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1),
99 kernel_dims.width, kernel_dims.height,
100 conv_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101 _num_elems_processed_per_iteration = output->info()->dimension(0);
102
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000103 build_opts.add_option("-DKERNEL_WIDTH=" + support::cpp11::to_string(kernel_dims.width));
104 build_opts.add_option("-DKERNEL_HEIGHT=" + support::cpp11::to_string(kernel_dims.height));
105 build_opts.add_option("-DKERNEL_DEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
106 build_opts.add_option("-DCONVOLVED_WIDTH=" + support::cpp11::to_string(_convolved_dims.first));
107 build_opts.add_option("-DCONVOLVED_HEIGHT=" + support::cpp11::to_string(_convolved_dims.second));
108 build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_info.stride().first));
109 build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_info.stride().second));
110 build_opts.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left()));
111 build_opts.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top()));
112 build_opts.add_option("-DPAD_RIGHT=" + support::cpp11::to_string(conv_info.pad_right()));
113 build_opts.add_option("-DPAD_BOTTOM=" + support::cpp11::to_string(conv_info.pad_bottom()));
114 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input->info()->dimension(0)));
115 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
Chunosov5124be52017-11-22 20:42:13 +0700116 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 +0100117
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100118 if(kernel_dims.width == 3 && kernel_dims.height == 3 && !conv_info.has_padding())
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100119 {
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000120 kernel_name = "im2col_kernel3x3_padx0_pady0";
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000121
122 // Local work size optimized for the 3x3 MobileNets convolution on Bifrost.
123 if(gpu_target == GPUTarget::BIFROST && input->info()->dimension(0) == 224)
124 {
125 _lws_hint = cl::NDRange(2, 3, 3);
126 }
127 }
128 else if(kernel_dims.width > 1 && !conv_info.has_padding())
129 {
130 kernel_name = "im2col_generic_padx0_pady0";
131
132 // Optimized im2col is performed using one or more vector operations with the specified vector size
133 // and a remainder. For example, for 5x5 convolutions, im2col is performed using vectors of size 4
134 // and scalars; for 7x7 convolutions, using vectors of size 4 and vectors of size 3.
135 // Using the vector size of 4 is always safe since OpenCL supports vectors of size 2 and 3.
136 // Using the vector size of 8, however, may be faster.
137 size_t vector_size = 4;
138 // For 2x2 convolutions, use vectors of size 2. (For 3x3 convolutions, im2col_kernel3x3_padx0_pady0
139 // is used instead.)
140 if(kernel_dims.width < vector_size)
141 {
142 vector_size = kernel_dims.width;
143 }
144 // Local work size and vector size optimized for the 11x11 AlexNet convolution on Bifrost.
145 if(gpu_target == GPUTarget::BIFROST && kernel_dims.width == 11)
146 {
147 _lws_hint = cl::NDRange(1, 1, 1);
148 vector_size = 8;
149 }
150 const size_t width_mod_vector_size = kernel_dims.width % vector_size;
151 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
152 build_opts.add_option("-DWIDTH_MOD_VECTOR_SIZE=" + support::cpp11::to_string(width_mod_vector_size));
153 }
154 else
155 {
156 if(gpu_target == GPUTarget::BIFROST)
157 {
158 const size_t input_channels = input->info()->dimension(2);
159 if((input_channels & (input_channels - 1)) == 0)
160 {
161 // input_channels is a power of two
162 _lws_hint = cl::NDRange(1, 1, 4);
163 }
164 else if(input_channels < 192 && (input_channels % 4) == 0)
165 {
166 // input_channels is less than 192 and is a multiple of 4
167 _lws_hint = cl::NDRange(1, 1, 2);
168 }
169 // otherwise the default is optimal
170 }
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100171 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172 _run_func = &CLIm2ColKernel::run_generic;
173 }
174 else
175 {
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000176 kernel_name = "im2col_reduced";
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177 _num_elems_processed_per_iteration = 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178 _run_func = &CLIm2ColKernel::run_reduced;
179 }
180
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000181 // Create kernel
182 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
183
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184 // Configure kernel window
185 Window win = calculate_max_window(*input->info(), Steps());
186 // The CLIm2ColKernel doesn't need padding so update_window_and_padding() can be skipped
187 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
steniu01868e5412017-07-17 23:16:00 +0100188 if(!run_img2col_reduced)
189 {
190 // set the Z dimension's step same size as the whole dimension so that one can't split across the Z dimension
191 win.set_dimension_step(Window::DimZ, win[Window::DimZ].end() - win[Window::DimZ].start());
192 }
193
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194 ICLKernel::configure(win);
Gian Marcode691f02017-09-08 16:13:11 +0100195
196 // Set config_id for enabling LWS tuning
197 _config_id = "im2col_";
198 _config_id += (run_img2col_reduced ? "reduced_" : "");
199 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
200 _config_id += "_";
201 _config_id += support::cpp11::to_string(output->info()->dimension(0));
202 _config_id += "_";
203 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204}
205
Georgios Pinitas358ca202017-12-07 16:47:52 +0000206Status CLIm2ColKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias)
207{
208 ARM_COMPUTE_UNUSED(kernel_dims);
209 ARM_COMPUTE_UNUSED(conv_info);
210 ARM_COMPUTE_UNUSED(has_bias);
211 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
212 return Status{};
213}
214
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215void CLIm2ColKernel::run(const Window &window, cl::CommandQueue &queue)
216{
217 ARM_COMPUTE_ERROR_ON(_run_func == nullptr);
218 (this->*_run_func)(window, queue);
219}
220
221void CLIm2ColKernel::run_generic(const Window &window, cl::CommandQueue &queue)
222{
223 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
224 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
225
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100226 // Get initial windows
steniu01868e5412017-07-17 23:16:00 +0100227 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
228 // Change the Z dimension's step back to 1
229 window_collapsed.set_dimension_step(Window::DimZ, 1);
230
231 Window slice = window_collapsed.first_slice_window_3D();
232 Window slice_in = window_collapsed.first_slice_window_3D();
233 Window slice_out = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100234
235 // Setup slice
236 slice.set(Window::DimX, Window::Dimension(0, static_cast<int>(_convolved_dims.first), 1));
237 slice.set(Window::DimY, Window::Dimension(0, static_cast<int>(_convolved_dims.second), 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100238
239 // Setup input slice
240 // The first three dimensions of the input are increased by the inner loops
241 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
242 slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
243 slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
244
245 // Setup output slice
246 slice_out.set(Window::DimX, Window::Dimension(0, _output->info()->dimension(0), _num_elems_processed_per_iteration));
247 slice_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 1));
248 slice_out.set(Window::DimZ, Window::Dimension(0, 1, 1));
249
250 do
251 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100252 unsigned int idx = 0;
253 add_3D_tensor_argument(idx, _input, slice_in);
254 add_2D_tensor_argument(idx, _output, slice_out);
steniu01868e5412017-07-17 23:16:00 +0100255 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input->info()->strides_in_bytes()[3]));
256 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_output->info()->strides_in_bytes()[3]));
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100257 enqueue(queue, *this, slice, _lws_hint);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100258 }
steniu01868e5412017-07-17 23:16:00 +0100259 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 +0100260}
261
262void CLIm2ColKernel::run_reduced(const Window &window, cl::CommandQueue &queue)
263{
264 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
265 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
266
267 Window out_window;
SiCong Li86b53332017-08-23 11:02:43 +0100268 out_window.use_tensor_dimensions(_output->info()->tensor_shape());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100269
270 Window out_slice = out_window.first_slice_window_1D();
271 Window in_slice = window.first_slice_window_3D();
272
273 // Run kernel
274 do
275 {
276 // Set arguments
277 unsigned int idx = 0;
278 add_3D_tensor_argument(idx, _input, in_slice);
279 add_1D_tensor_argument(idx, _output, out_slice);
280
281 _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(0));
282 _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(1));
Gian Marcod7779da2017-11-22 14:46:28 +0000283 enqueue(queue, *this, in_slice, _lws_hint);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100284 }
285 while(window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_1D(out_slice));
286}