blob: 6de97d40af7e86cd6de84f1d598270445f1bd4e7 [file] [log] [blame]
steniu0127b386c2017-07-18 17:37:43 +01001/*
Georgios Pinitas15997872018-02-19 13:58:22 +00002 * Copyright (c) 2017-2018 ARM Limited.
steniu0127b386c2017-07-18 17:37:43 +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/CLDirectConvolutionLayerKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#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"
steniu0127b386c2017-07-18 17:37:43 +010030#include "arm_compute/core/CL/ICLTensor.h"
31#include "arm_compute/core/Error.h"
32#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/IAccessWindow.h"
34#include "arm_compute/core/ITensor.h"
35#include "arm_compute/core/Types.h"
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010036#include "arm_compute/core/Utils.h"
Giorgio Arenac0f54432018-03-16 14:02:34 +000037#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Chunosovd621bca2017-11-03 17:33:15 +070038#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
steniu0127b386c2017-07-18 17:37:43 +010039#include "support/ToolchainSupport.h"
40
41using namespace arm_compute;
42
Georgios Pinitas30902ed2017-11-14 15:32:57 +000043namespace
44{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000045Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info)
Georgios Pinitas30902ed2017-11-14 15:32:57 +000046{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010047 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010048 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Georgios Pinitas30902ed2017-11-14 15:32:57 +000049 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Pablo Tello3d319462018-06-21 15:13:17 +010050
51 const DataLayout data_layout = input->data_layout();
52 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
53 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
54 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
55
56 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) != weights->dimension(height_idx), "Weights should have same width and height");
57 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) != 1 && weights->dimension(width_idx) != 3 && weights->dimension(width_idx) != 5,
Georgios Pinitas30902ed2017-11-14 15:32:57 +000058 "Kernel sizes other than 1x1, 3x3 or 5x5 are not supported");
Pablo Tello3d319462018-06-21 15:13:17 +010059 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(channel_idx) != input->dimension(channel_idx),
Georgios Pinitas30902ed2017-11-14 15:32:57 +000060 "Weights feature map dimension should match the respective input's one");
Pablo Tello3d319462018-06-21 15:13:17 +010061 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->num_dimensions() > 4, "Weights can be at most 4 dimensional");
62 ARM_COMPUTE_RETURN_ERROR_ON_MSG((weights->dimension(width_idx) == 1) && std::get<0>(conv_info.stride()) > 3, "Strides larger than 3 not supported for 1x1 convolution.");
63 ARM_COMPUTE_RETURN_ERROR_ON_MSG((weights->dimension(width_idx) == 3 || weights->dimension(width_idx) == 5) && std::get<0>(conv_info.stride()) > 2,
Georgios Pinitas30902ed2017-11-14 15:32:57 +000064 "Strides larger than 2 not supported for 3x3 convolution.");
65
66 if(biases != nullptr)
67 {
68 if(is_data_type_quantized_asymmetric(input->data_type()))
69 {
70 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
71 }
72 else
73 {
74 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
75 }
76 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->dimension(0) != weights->dimension(3),
77 "Biases size and number of input feature maps should match");
78 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->num_dimensions() > 1,
79 "Biases should be one dimensional");
80 }
81
82 // Checks performed when output is configured
83 if(output->total_size() != 0)
84 {
85 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(),
Giorgio Arenac0f54432018-03-16 14:02:34 +000086 misc::shape_calculator::compute_deep_convolution_shape(*input, *weights, conv_info));
Georgios Pinitas30902ed2017-11-14 15:32:57 +000087 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitas30902ed2017-11-14 15:32:57 +000088 }
89
Georgios Pinitas631c41a2017-12-06 11:53:03 +000090 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +000091}
92
Pablo Tello3d319462018-06-21 15:13:17 +010093inline bool can_run_optimized_kernel_for_bifrost(GPUTarget gpu_target, unsigned int conv_stride_x, unsigned int conv_stride_y, unsigned int kernel_size,
94 DataType data_type, DataLayout data_layout)
Giorgio Arena59486342017-12-01 10:42:47 +000095{
Pablo Tello3d319462018-06-21 15:13:17 +010096 return gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, GPUTarget::G76) && (kernel_size <= 5)
97 && (conv_stride_x == 1) && (conv_stride_y == 1) && (data_type == DataType::F32) && (data_layout == DataLayout::NCHW);
98}
Giorgio Arena59486342017-12-01 10:42:47 +000099
Pablo Tello3d319462018-06-21 15:13:17 +0100100inline void setup_num_elems(unsigned int &num_elems_read_per_iteration_x, unsigned int &num_elems_read_per_iteration_y,
101 unsigned int &num_elems_written_per_iteration_x, unsigned int &num_elems_written_per_iteration_y,
102 unsigned int kernel_size, const PadStrideInfo &conv_info, const GPUTarget target, ITensorInfo *input)
103{
104 const DataType data_type = input->data_type();
105 const DataLayout data_layout = input->data_layout();
106 unsigned int conv_stride_x = std::get<0>(conv_info.stride());
107 unsigned int conv_stride_y = std::get<1>(conv_info.stride());
Giorgio Arena59486342017-12-01 10:42:47 +0000108
Pablo Tello3d319462018-06-21 15:13:17 +0100109 const bool run_optimized_bifrost = can_run_optimized_kernel_for_bifrost(target, conv_stride_x, conv_stride_y, kernel_size, data_type, data_layout);
Giorgio Arena59486342017-12-01 10:42:47 +0000110
Pablo Tello3d319462018-06-21 15:13:17 +0100111 if(run_optimized_bifrost)
Giorgio Arena59486342017-12-01 10:42:47 +0000112 {
113 // Configure kernel window
Giorgio Arena59486342017-12-01 10:42:47 +0000114 switch(kernel_size)
115 {
116 case 1:
117 {
118 num_elems_read_per_iteration_x = 4;
119 num_elems_read_per_iteration_y = 4;
120 num_elems_written_per_iteration_x = 4;
121 num_elems_written_per_iteration_y = 4;
122 break;
123 }
124 case 3:
125 {
126 num_elems_read_per_iteration_x = 6;
127 num_elems_read_per_iteration_y = 5;
128 num_elems_written_per_iteration_x = 4;
129 num_elems_written_per_iteration_y = 3;
130 break;
131 }
132 case 5:
133 {
134 num_elems_read_per_iteration_x = 8;
135 num_elems_read_per_iteration_y = 6;
136 num_elems_written_per_iteration_x = 4;
137 num_elems_written_per_iteration_y = 2;
138 break;
139 }
140 default:
141 {
142 ARM_COMPUTE_ERROR("Kernel size not optimized for Bifrost");
143 }
144 }
145 }
146 else
147 {
Giorgio Arena59486342017-12-01 10:42:47 +0000148 num_elems_read_per_iteration_y = kernel_size;
149 num_elems_written_per_iteration_x = 8;
150 num_elems_written_per_iteration_y = 1;
Anthony Barbiercc9fed52017-12-13 10:46:00 +0000151 switch(kernel_size)
152 {
153 case 1:
154 switch(conv_stride_x)
155 {
156 case 1:
157 num_elems_read_per_iteration_x = 8;
158 break;
159 case 2:
160 num_elems_read_per_iteration_x = 16;
161 break;
162 case 3:
163 switch(input->element_size())
164 {
165 case 1:
166 num_elems_read_per_iteration_x = 28;
167 break;
168 case 2:
169 num_elems_read_per_iteration_x = 24;
170 break;
171 case 4:
172 num_elems_read_per_iteration_x = 22;
173 break;
174 default:
175 ARM_COMPUTE_ERROR("Invalid data size");
176 }
177 break;
178 default:
179 ARM_COMPUTE_ERROR("Invalid convolution stride X");
180 }
181 break;
182 case 3:
183 switch(conv_stride_x)
184 {
185 case 1:
186 num_elems_read_per_iteration_x = 10;
187 break;
188 case 2:
189 num_elems_read_per_iteration_x = 17;
190 break;
191 default:
192 ARM_COMPUTE_ERROR("Invalid convolution stride X");
193 }
194 break;
195 case 5:
196 switch(conv_stride_x)
197 {
198 case 1:
199 num_elems_read_per_iteration_x = 12;
200 break;
201 case 2:
202 num_elems_read_per_iteration_x = 20;
203 break;
204 default:
205 ARM_COMPUTE_ERROR("Invalid convolution stride X");
206 }
207 break;
208 default:
209 ARM_COMPUTE_ERROR("Invalid direct convolution size");
210 }
Giorgio Arena59486342017-12-01 10:42:47 +0000211 }
212
Pablo Tello3d319462018-06-21 15:13:17 +0100213 if(data_layout == DataLayout::NHWC)
214 {
215 num_elems_written_per_iteration_x = 1;
216 num_elems_read_per_iteration_x = 1;
217 switch(kernel_size)
218 {
219 case 1:
220 switch(conv_stride_x)
221 {
222 case 1:
223 num_elems_read_per_iteration_y = 8;
224 num_elems_written_per_iteration_y = 8;
225 break;
226 case 2:
227 num_elems_read_per_iteration_y = 16;
228 num_elems_written_per_iteration_y = 8;
229 break;
230 default:
231 ARM_COMPUTE_ERROR("Invalid convolution stride X");
232 }
233 break;
234 case 3:
235 switch(conv_stride_x)
236 {
237 case 1:
238 num_elems_read_per_iteration_y = 10;
239 num_elems_written_per_iteration_y = 8;
240 break;
241 case 2:
242 num_elems_read_per_iteration_y = 17;
243 num_elems_written_per_iteration_y = 8;
244 break;
245 default:
246 ARM_COMPUTE_ERROR("Invalid convolution stride X");
247 }
248 break;
249 case 5:
250 switch(conv_stride_x)
251 {
252 case 1:
253 num_elems_read_per_iteration_y = 12;
254 num_elems_written_per_iteration_y = 8;
255 break;
256 case 2:
257 num_elems_read_per_iteration_y = 20;
258 num_elems_written_per_iteration_y = 8;
259 break;
260 default:
261 ARM_COMPUTE_ERROR("Invalid convolution stride X");
262 }
263 break;
264 default:
265 ARM_COMPUTE_ERROR("Not implemented.");
266 break;
267 }
268 }
269}
270
271std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *output, const PadStrideInfo &conv_info, const GPUTarget target)
272{
273 const DataLayout data_layout = input->data_layout();
274 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
275 const unsigned int kernel_size = weights->dimension(width_idx);
276
277 // Get convolved dimensions
278 TensorShape output_shape = misc::shape_calculator::compute_deep_convolution_shape(*input, *weights, conv_info);
279
280 // Output auto inizialitation if not yet initialized
281 // FIXME: input->clone()->set_tensor_shape(output_shape) doesn't work with subtensors for grouped direct convolutions (AlexNet).
282 auto_init_if_empty(*output, output_shape,
283 1,
284 input->data_type(),
285 input->quantization_info());
286
287 unsigned int num_elems_read_per_iteration_x = 0;
288 unsigned int num_elems_read_per_iteration_y = 0;
289 unsigned int num_elems_written_per_iteration_x = 0;
290 unsigned int num_elems_written_per_iteration_y = 0;
291
292 unsigned int conv_pad_left = conv_info.pad_left();
293 unsigned int conv_pad_top = conv_info.pad_top();
294 unsigned int conv_stride_x = std::get<0>(conv_info.stride());
295 unsigned int conv_stride_y = std::get<1>(conv_info.stride());
296
297 setup_num_elems(num_elems_read_per_iteration_x, num_elems_read_per_iteration_y,
298 num_elems_written_per_iteration_x, num_elems_written_per_iteration_y,
299 kernel_size, conv_info, target, input);
300
Giorgio Arena59486342017-12-01 10:42:47 +0000301 // Create window and update padding
Anthony Barbiercc9fed52017-12-13 10:46:00 +0000302 bool window_changed = false;
303 Window win = calculate_max_window(*output, Steps(num_elems_written_per_iteration_x, num_elems_written_per_iteration_y));
Giorgio Arena59486342017-12-01 10:42:47 +0000304
Pablo Tello3d319462018-06-21 15:13:17 +0100305 if(data_layout == DataLayout::NHWC)
306 {
307 AccessWindowStatic input_access(input, 0, -conv_pad_left,
308 num_elems_read_per_iteration_x,
309 ceil_to_multiple(input->dimension(1) + conv_info.pad_right(), num_elems_read_per_iteration_y));
310 AccessWindowStatic weights_access(weights, 0, 0, weights->dimension(0), weights->dimension(1));
311 AccessWindowRectangle output_access(output, 0, 0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_y);
312 window_changed = update_window_and_padding(win, input_access, weights_access, output_access);
313 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
314 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
315 return std::make_pair(err, win);
316 }
317 else if(data_layout == DataLayout::NCHW)
318 {
319 AccessWindowRectangle input_access(input, -conv_pad_left, -conv_pad_top, num_elems_read_per_iteration_x, num_elems_read_per_iteration_y, conv_stride_x, conv_stride_y);
320 AccessWindowStatic weights_access(weights, 0, 0, kernel_size, kernel_size);
321 AccessWindowRectangle output_access(output, 0, 0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_y);
322 window_changed = update_window_and_padding(win, input_access, weights_access, output_access);
323 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
324 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
325 return std::make_pair(err, win);
326 }
327 else
328 {
329 ARM_COMPUTE_ERROR("Not supported");
330 }
Giorgio Arena59486342017-12-01 10:42:47 +0000331}
332} // namespace
333
334CLDirectConvolutionLayerKernel::CLDirectConvolutionLayerKernel()
335 : _input(nullptr), _biases(nullptr), _weights(nullptr), _output(nullptr), _border_size(0), _conv_stride_x(0), _conv_stride_y(0)
336{
337}
338
339BorderSize CLDirectConvolutionLayerKernel::border_size() const
340{
341 return _border_size;
342}
343
344void CLDirectConvolutionLayerKernel::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info)
345{
346 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
347
Pablo Tello3d319462018-06-21 15:13:17 +0100348 const DataLayout data_layout = input->info()->data_layout();
349 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
350 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
351 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
352
353 const unsigned int kernel_size = weights->info()->dimension(width_idx);
Giorgio Arena59486342017-12-01 10:42:47 +0000354 const DataType data_type = input->info()->data_type();
355
356 // Get convolved dimensions
Giorgio Arenac0f54432018-03-16 14:02:34 +0000357 TensorShape output_shape = misc::shape_calculator::compute_deep_convolution_shape(*input->info(), *weights->info(), conv_info);
Giorgio Arena59486342017-12-01 10:42:47 +0000358
359 // Output auto inizialitation if not yet initialized
360 // FIXME: input->clone()->set_tensor_shape(output_shape) doesn't work with subtensors for grouped direct convolutions (AlexNet).
361 auto_init_if_empty(*output->info(),
362 output_shape,
363 1,
364 input->info()->data_type(),
Giorgio Arena59486342017-12-01 10:42:47 +0000365 input->info()->quantization_info());
366
367 // Perform validation step
368 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(),
369 weights->info(),
370 (biases != nullptr) ? biases->info() : nullptr,
371 output->info(),
372 conv_info));
373
374 _conv_stride_x = std::get<0>(conv_info.stride());
375 _conv_stride_y = std::get<1>(conv_info.stride());
Pablo Tello3d319462018-06-21 15:13:17 +0100376
377 if(data_layout == DataLayout::NHWC)
378 {
379 _border_size = BorderSize(conv_info.pad_left(), 0, conv_info.pad_right(), 0);
380 }
381 else if(data_layout == DataLayout::NCHW)
382 {
383 _border_size = BorderSize(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left());
384 }
385 else
386 {
387 ARM_COMPUTE_ERROR("Not supported");
388 }
Giorgio Arena59486342017-12-01 10:42:47 +0000389
390 _input = input;
391 _weights = weights;
392 _output = output;
393 _biases = biases;
394
Michalis Spyroua9676112018-02-22 18:07:43 +0000395 const GPUTarget gpu_target = get_target();
Giorgio Arena59486342017-12-01 10:42:47 +0000396
397 std::stringstream kernel_name;
398 kernel_name << "direct_convolution" << kernel_size << "x" << kernel_size;
Pablo Tello3d319462018-06-21 15:13:17 +0100399 if(data_layout == DataLayout::NHWC)
400 {
401 kernel_name << "_" << lower_string(string_from_data_layout(data_layout));
402 }
Giorgio Arena59486342017-12-01 10:42:47 +0000403
404 CLBuildOptions build_options;
405 build_options.add_option_if(_biases != nullptr, std::string("-DHAS_BIAS"));
406
Pablo Tello3d319462018-06-21 15:13:17 +0100407 const bool run_optimized_for_bifrost = can_run_optimized_kernel_for_bifrost(gpu_target, _conv_stride_x, _conv_stride_y, kernel_size, data_type, data_layout);
408
409 if(run_optimized_for_bifrost)
Giorgio Arena59486342017-12-01 10:42:47 +0000410 {
Pablo Tello3d319462018-06-21 15:13:17 +0100411 build_options.add_option(std::string("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(_weights->info()->dimension(channel_idx))));
Giorgio Arena59486342017-12-01 10:42:47 +0000412
413 kernel_name << "_f32_bifrost";
414 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name.str(), build_options.options()));
Giorgio Arena59486342017-12-01 10:42:47 +0000415 }
416 else
417 {
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100418 bool is_quantized_asymm = is_data_type_quantized_asymmetric(data_type);
Giorgio Arena59486342017-12-01 10:42:47 +0000419
420 build_options.add_option_if(is_quantized_asymm, std::string("-DKERNEL_SIZE=" + support::cpp11::to_string(kernel_size)));
421 build_options.add_option(std::string("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)));
422 build_options.add_option(std::string("-DDATA_SIZE=" + get_data_size_from_data_type(data_type)));
Pablo Tello3d319462018-06-21 15:13:17 +0100423 build_options.add_option(std::string("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(_weights->info()->dimension(channel_idx))));
Giorgio Arena59486342017-12-01 10:42:47 +0000424 build_options.add_option(std::string("-DSTRIDE_X=" + support::cpp11::to_string(_conv_stride_x)));
Pablo Tello3d319462018-06-21 15:13:17 +0100425 if(data_layout == DataLayout::NHWC)
426 {
427 build_options.add_option(std::string("-DDATA_LAYOUT_NHWC=1"));
428 build_options.add_option(std::string("-DDST_HEIGHT=" + support::cpp11::to_string(_output->info()->dimension(height_idx))));
429 build_options.add_option(std::string("-DDST_WIDTH=" + support::cpp11::to_string(_output->info()->dimension(width_idx))));
430 build_options.add_option(std::string("-DSRC_HEIGHT=" + support::cpp11::to_string(_input->info()->dimension(height_idx))));
431 build_options.add_option(std::string("-DSRC_WIDTH=" + support::cpp11::to_string(_input->info()->dimension(width_idx))));
432 build_options.add_option(std::string("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left())));
433 build_options.add_option(std::string("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top())));
434 build_options.add_option(std::string("-DSTRIDE_Y=" + support::cpp11::to_string(_conv_stride_y)));
435 }
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100436 build_options.add_option(std::string("-DDATA_TYPE_PROMOTED=" + get_cl_type_from_data_type(data_type)));
Giorgio Arena59486342017-12-01 10:42:47 +0000437 // Create kernel
438 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(is_quantized_asymm ? "direct_convolution_1x1_3x3_5x5_quantized" : kernel_name.str(),
439 build_options.options()));
440 }
441
442 // Configure kernel window
443 auto win_config = validate_and_configure_window(input->info(), weights->info(), output->info(), conv_info, gpu_target);
444 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100445 ICLKernel::configure_internal(win_config.second);
Giorgio Arena59486342017-12-01 10:42:47 +0000446
447 // Set static kernel arguments
448 if(is_data_type_quantized_asymmetric(data_type))
449 {
450 int output_multiplier = 0;
451 int output_shift = 0;
452
453 float multiplier = _input->info()->quantization_info().scale * _weights->info()->quantization_info().scale / _output->info()->quantization_info().scale;
454 ARM_COMPUTE_THROW_ON_ERROR(quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift));
455
456 unsigned int idx = 3 * num_arguments_per_3D_tensor() + ((_biases != nullptr) ? num_arguments_per_1D_tensor() : 0) + 1;
457 _kernel.setArg(idx++, -_input->info()->quantization_info().offset);
458 _kernel.setArg(idx++, -_weights->info()->quantization_info().offset);
459 _kernel.setArg(idx++, _output->info()->quantization_info().offset);
460 _kernel.setArg(idx++, output_multiplier);
461 _kernel.setArg(idx++, output_shift);
462 }
463
464 // Set config_id for enabling LWS tuning
465 _config_id = "direct_convolution_";
466 _config_id += lower_string(string_from_data_type(data_type));
467 _config_id += "_";
468 _config_id += support::cpp11::to_string(kernel_size);
469 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000470 _config_id += support::cpp11::to_string(border_size().left);
Giorgio Arena59486342017-12-01 10:42:47 +0000471 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000472 _config_id += support::cpp11::to_string(border_size().top);
Giorgio Arena59486342017-12-01 10:42:47 +0000473 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000474 _config_id += support::cpp11::to_string(border_size().right);
Giorgio Arena59486342017-12-01 10:42:47 +0000475 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000476 _config_id += support::cpp11::to_string(border_size().bottom);
Giorgio Arena59486342017-12-01 10:42:47 +0000477 _config_id += "_";
478 _config_id += support::cpp11::to_string(_conv_stride_x);
479 _config_id += "_";
480 _config_id += support::cpp11::to_string(_conv_stride_y);
481 _config_id += "_";
Pablo Tello3d319462018-06-21 15:13:17 +0100482 _config_id += support::cpp11::to_string(output->info()->dimension(width_idx));
Giorgio Arena59486342017-12-01 10:42:47 +0000483 _config_id += "_";
Pablo Tello3d319462018-06-21 15:13:17 +0100484 _config_id += support::cpp11::to_string(output->info()->dimension(height_idx));
485 _config_id += "_";
486 _config_id += lower_string(string_from_data_layout(data_layout));
Giorgio Arena59486342017-12-01 10:42:47 +0000487}
488
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000489Status CLDirectConvolutionLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
490 const GPUTarget target)
Giorgio Arena59486342017-12-01 10:42:47 +0000491{
492 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, conv_info));
493 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), weights->clone().get(), output->clone().get(), conv_info, target).first);
494
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000495 return Status{};
Giorgio Arena59486342017-12-01 10:42:47 +0000496}
497
SiCong Lic51b72f2017-07-28 14:46:20 +0100498void CLDirectConvolutionLayerKernel::run(const Window &window, cl::CommandQueue &queue)
steniu0127b386c2017-07-18 17:37:43 +0100499{
500 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
501 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
502
503 // Get initial windows
504 Window slice = window.first_slice_window_3D();
505 Window win_in = window;
506
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +0000507 win_in.adjust(Window::DimX, -_border_size.left, true);
508 win_in.adjust(Window::DimY, -_border_size.top, true);
steniu0127b386c2017-07-18 17:37:43 +0100509
Pablo Tello3d319462018-06-21 15:13:17 +0100510 const DataLayout data_layout = _input->info()->data_layout();
511 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
512 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
steniu0127b386c2017-07-18 17:37:43 +0100513
Pablo Tello3d319462018-06-21 15:13:17 +0100514 win_in.set_dimension_step(width_idx, window[width_idx].step() * _conv_stride_x);
515 win_in.set_dimension_step(height_idx, window[height_idx].step() * _conv_stride_y);
516
517 Window slice_in = win_in.first_slice_window_3D();
518 unsigned int idx1 = 2 * num_arguments_per_3D_tensor();
steniu0127b386c2017-07-18 17:37:43 +0100519 add_3D_tensor_argument(idx1, _weights, slice);
520
521 if(_biases != nullptr)
522 {
523 Window slice_biases;
SiCong Li86b53332017-08-23 11:02:43 +0100524 slice_biases.use_tensor_dimensions(_biases->info()->tensor_shape());
steniu0127b386c2017-07-18 17:37:43 +0100525 add_1D_tensor_argument(idx1, _biases, slice_biases);
526 }
527
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100528 _kernel.setArg(idx1++, static_cast<unsigned int>(_weights->info()->strides_in_bytes()[3]));
529
steniu0127b386c2017-07-18 17:37:43 +0100530 do
531 {
532 unsigned int idx = 0;
533 add_3D_tensor_argument(idx, _input, slice_in);
534 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100535 enqueue(queue, *this, slice, lws_hint());
steniu0127b386c2017-07-18 17:37:43 +0100536 }
537 while(window.slide_window_slice_3D(slice) && win_in.slide_window_slice_3D(slice_in));
538}