blob: 10119d8e8eb69c6767cd1e9f66c57635f53c4f2b [file] [log] [blame]
steniu0127b386c2017-07-18 17:37:43 +01001/*
Georgios Pinitasf52cd782019-03-25 14:06:14 +00002 * Copyright (c) 2017-2019 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");
Michalis Spyrou45091732019-05-13 17:41:01 +010057 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) != 1 && weights->dimension(width_idx) != 3 && weights->dimension(width_idx) != 5 && weights->dimension(width_idx) != 9,
58 "Kernel sizes other than 1x1, 3x3, 5x5 or 9x9 are not supported");
Georgios Pinitasee0f6cc2019-10-11 15:23:41 +010059 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) == 9 && input->data_type() == DataType::QASYMM8, "Kernel sizes of 9x9 is not supported for quantized types");
Pablo Tello3d319462018-06-21 15:13:17 +010060 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(channel_idx) != input->dimension(channel_idx),
Georgios Pinitas30902ed2017-11-14 15:32:57 +000061 "Weights feature map dimension should match the respective input's one");
Pablo Tello3d319462018-06-21 15:13:17 +010062 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->num_dimensions() > 4, "Weights can be at most 4 dimensional");
63 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.");
64 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 +000065 "Strides larger than 2 not supported for 3x3 convolution.");
Michalis Spyrou45091732019-05-13 17:41:01 +010066 ARM_COMPUTE_RETURN_ERROR_ON_MSG((weights->dimension(width_idx) == 9) && data_layout == DataLayout::NCHW, "Only NHWC layout is supported for 9x9 convolution.");
Georgios Pinitas30902ed2017-11-14 15:32:57 +000067
68 if(biases != nullptr)
69 {
70 if(is_data_type_quantized_asymmetric(input->data_type()))
71 {
72 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
73 }
74 else
75 {
76 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
77 }
78 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->dimension(0) != weights->dimension(3),
79 "Biases size and number of input feature maps should match");
80 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->num_dimensions() > 1,
81 "Biases should be one dimensional");
82 }
83
84 // Checks performed when output is configured
85 if(output->total_size() != 0)
86 {
87 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(),
Giorgio Arenac0f54432018-03-16 14:02:34 +000088 misc::shape_calculator::compute_deep_convolution_shape(*input, *weights, conv_info));
Georgios Pinitas30902ed2017-11-14 15:32:57 +000089 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitas30902ed2017-11-14 15:32:57 +000090 }
91
Georgios Pinitas631c41a2017-12-06 11:53:03 +000092 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +000093}
94
Pablo Tello3d319462018-06-21 15:13:17 +010095inline bool can_run_optimized_kernel_for_bifrost(GPUTarget gpu_target, unsigned int conv_stride_x, unsigned int conv_stride_y, unsigned int kernel_size,
96 DataType data_type, DataLayout data_layout)
Giorgio Arena59486342017-12-01 10:42:47 +000097{
Georgios Pinitasa34286e2018-09-04 12:18:50 +010098 return gpu_target_is_in(gpu_target,
99 GPUTarget::G71, GPUTarget::G72, GPUTarget::G76,
100 GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT,
101 GPUTarget::G52, GPUTarget::G52LIT)
102 && (kernel_size <= 5)
103 && (conv_stride_x == 1) && (conv_stride_y == 1)
104 && (data_type == DataType::F32)
105 && (data_layout == DataLayout::NCHW);
Pablo Tello3d319462018-06-21 15:13:17 +0100106}
Giorgio Arena59486342017-12-01 10:42:47 +0000107
Michalis Spyrou45091732019-05-13 17:41:01 +0100108inline bool can_run_optimized_kernel_for_bifrost_nhwc(GPUTarget gpu_target, unsigned int conv_stride_x, unsigned int conv_stride_y, unsigned int kernel_size,
109 DataType data_type, DataLayout data_layout)
110{
111 return gpu_target_is_in(gpu_target,
112 GPUTarget::G71, GPUTarget::G72, GPUTarget::G76,
113 GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT,
114 GPUTarget::G52, GPUTarget::G52LIT)
115 && (kernel_size == 9)
116 && (conv_stride_x == 1) && (conv_stride_y == 1)
117 && (data_type == DataType::F32)
118 && (data_layout == DataLayout::NHWC);
119}
120
Pablo Tello3d319462018-06-21 15:13:17 +0100121inline void setup_num_elems(unsigned int &num_elems_read_per_iteration_x, unsigned int &num_elems_read_per_iteration_y,
122 unsigned int &num_elems_written_per_iteration_x, unsigned int &num_elems_written_per_iteration_y,
123 unsigned int kernel_size, const PadStrideInfo &conv_info, const GPUTarget target, ITensorInfo *input)
124{
125 const DataType data_type = input->data_type();
126 const DataLayout data_layout = input->data_layout();
127 unsigned int conv_stride_x = std::get<0>(conv_info.stride());
128 unsigned int conv_stride_y = std::get<1>(conv_info.stride());
Giorgio Arena59486342017-12-01 10:42:47 +0000129
Pablo Tello3d319462018-06-21 15:13:17 +0100130 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 +0000131
Pablo Tello3d319462018-06-21 15:13:17 +0100132 if(run_optimized_bifrost)
Giorgio Arena59486342017-12-01 10:42:47 +0000133 {
134 // Configure kernel window
Giorgio Arena59486342017-12-01 10:42:47 +0000135 switch(kernel_size)
136 {
137 case 1:
138 {
139 num_elems_read_per_iteration_x = 4;
140 num_elems_read_per_iteration_y = 4;
141 num_elems_written_per_iteration_x = 4;
142 num_elems_written_per_iteration_y = 4;
143 break;
144 }
145 case 3:
146 {
147 num_elems_read_per_iteration_x = 6;
148 num_elems_read_per_iteration_y = 5;
149 num_elems_written_per_iteration_x = 4;
150 num_elems_written_per_iteration_y = 3;
151 break;
152 }
153 case 5:
154 {
155 num_elems_read_per_iteration_x = 8;
156 num_elems_read_per_iteration_y = 6;
157 num_elems_written_per_iteration_x = 4;
158 num_elems_written_per_iteration_y = 2;
159 break;
160 }
161 default:
162 {
163 ARM_COMPUTE_ERROR("Kernel size not optimized for Bifrost");
164 }
165 }
166 }
Michalis Spyrou45091732019-05-13 17:41:01 +0100167 else if(data_layout == DataLayout::NCHW)
Giorgio Arena59486342017-12-01 10:42:47 +0000168 {
Giorgio Arena59486342017-12-01 10:42:47 +0000169 num_elems_read_per_iteration_y = kernel_size;
170 num_elems_written_per_iteration_x = 8;
171 num_elems_written_per_iteration_y = 1;
Anthony Barbiercc9fed52017-12-13 10:46:00 +0000172 switch(kernel_size)
173 {
174 case 1:
175 switch(conv_stride_x)
176 {
177 case 1:
178 num_elems_read_per_iteration_x = 8;
179 break;
180 case 2:
181 num_elems_read_per_iteration_x = 16;
182 break;
183 case 3:
184 switch(input->element_size())
185 {
186 case 1:
187 num_elems_read_per_iteration_x = 28;
188 break;
189 case 2:
190 num_elems_read_per_iteration_x = 24;
191 break;
192 case 4:
193 num_elems_read_per_iteration_x = 22;
194 break;
195 default:
196 ARM_COMPUTE_ERROR("Invalid data size");
197 }
198 break;
199 default:
200 ARM_COMPUTE_ERROR("Invalid convolution stride X");
201 }
202 break;
203 case 3:
204 switch(conv_stride_x)
205 {
206 case 1:
207 num_elems_read_per_iteration_x = 10;
208 break;
209 case 2:
210 num_elems_read_per_iteration_x = 17;
211 break;
212 default:
213 ARM_COMPUTE_ERROR("Invalid convolution stride X");
214 }
215 break;
216 case 5:
217 switch(conv_stride_x)
218 {
219 case 1:
220 num_elems_read_per_iteration_x = 12;
221 break;
222 case 2:
223 num_elems_read_per_iteration_x = 20;
224 break;
225 default:
226 ARM_COMPUTE_ERROR("Invalid convolution stride X");
227 }
228 break;
229 default:
230 ARM_COMPUTE_ERROR("Invalid direct convolution size");
231 }
Giorgio Arena59486342017-12-01 10:42:47 +0000232 }
Michalis Spyrou45091732019-05-13 17:41:01 +0100233 else // data_layout == NHWC
Pablo Tello3d319462018-06-21 15:13:17 +0100234 {
Michalis Spyrou45091732019-05-13 17:41:01 +0100235 const bool run_optimized_bifrost_nhwc = can_run_optimized_kernel_for_bifrost_nhwc(target, conv_stride_x, conv_stride_y, kernel_size, data_type, data_layout);
236
Pablo Tello3d319462018-06-21 15:13:17 +0100237 num_elems_written_per_iteration_x = 1;
Michalis Spyrou45091732019-05-13 17:41:01 +0100238
239 if(run_optimized_bifrost_nhwc)
240 {
241 num_elems_read_per_iteration_x = 4;
242 }
giuros01c878f1f2019-07-09 11:01:34 +0100243 else
244 {
245 num_elems_read_per_iteration_x = 1;
246 }
Michalis Spyrou45091732019-05-13 17:41:01 +0100247
Pablo Tello3d319462018-06-21 15:13:17 +0100248 switch(kernel_size)
249 {
250 case 1:
251 switch(conv_stride_x)
252 {
253 case 1:
254 num_elems_read_per_iteration_y = 8;
255 num_elems_written_per_iteration_y = 8;
256 break;
257 case 2:
258 num_elems_read_per_iteration_y = 16;
259 num_elems_written_per_iteration_y = 8;
260 break;
261 default:
262 ARM_COMPUTE_ERROR("Invalid convolution stride X");
263 }
264 break;
265 case 3:
266 switch(conv_stride_x)
267 {
268 case 1:
269 num_elems_read_per_iteration_y = 10;
270 num_elems_written_per_iteration_y = 8;
271 break;
272 case 2:
273 num_elems_read_per_iteration_y = 17;
274 num_elems_written_per_iteration_y = 8;
275 break;
276 default:
277 ARM_COMPUTE_ERROR("Invalid convolution stride X");
278 }
279 break;
280 case 5:
281 switch(conv_stride_x)
282 {
283 case 1:
284 num_elems_read_per_iteration_y = 12;
285 num_elems_written_per_iteration_y = 8;
286 break;
287 case 2:
288 num_elems_read_per_iteration_y = 20;
289 num_elems_written_per_iteration_y = 8;
290 break;
291 default:
292 ARM_COMPUTE_ERROR("Invalid convolution stride X");
293 }
294 break;
Michalis Spyrou45091732019-05-13 17:41:01 +0100295 case 9:
296 switch(conv_stride_x)
297 {
298 case 1:
299 num_elems_read_per_iteration_y = 16;
300 num_elems_written_per_iteration_y = 8;
301 break;
302 case 2:
303 num_elems_read_per_iteration_y = 24;
304 num_elems_written_per_iteration_y = 8;
305 break;
306 default:
307 ARM_COMPUTE_ERROR("Invalid convolution stride X");
308 }
309 break;
Pablo Tello3d319462018-06-21 15:13:17 +0100310 default:
311 ARM_COMPUTE_ERROR("Not implemented.");
312 break;
313 }
314 }
315}
316
317std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *output, const PadStrideInfo &conv_info, const GPUTarget target)
318{
319 const DataLayout data_layout = input->data_layout();
320 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
321 const unsigned int kernel_size = weights->dimension(width_idx);
322
323 // Get convolved dimensions
324 TensorShape output_shape = misc::shape_calculator::compute_deep_convolution_shape(*input, *weights, conv_info);
325
326 // Output auto inizialitation if not yet initialized
Georgios Pinitasf52cd782019-03-25 14:06:14 +0000327 // TODO(COMPMID-2078): input->clone()->set_tensor_shape(output_shape) doesn't work with subtensors for grouped direct convolutions (AlexNet).
Pablo Tello3d319462018-06-21 15:13:17 +0100328 auto_init_if_empty(*output, output_shape,
329 1,
330 input->data_type(),
331 input->quantization_info());
332
333 unsigned int num_elems_read_per_iteration_x = 0;
334 unsigned int num_elems_read_per_iteration_y = 0;
335 unsigned int num_elems_written_per_iteration_x = 0;
336 unsigned int num_elems_written_per_iteration_y = 0;
337
338 unsigned int conv_pad_left = conv_info.pad_left();
339 unsigned int conv_pad_top = conv_info.pad_top();
340 unsigned int conv_stride_x = std::get<0>(conv_info.stride());
341 unsigned int conv_stride_y = std::get<1>(conv_info.stride());
342
343 setup_num_elems(num_elems_read_per_iteration_x, num_elems_read_per_iteration_y,
344 num_elems_written_per_iteration_x, num_elems_written_per_iteration_y,
345 kernel_size, conv_info, target, input);
346
Giorgio Arena59486342017-12-01 10:42:47 +0000347 // Create window and update padding
Anthony Barbiercc9fed52017-12-13 10:46:00 +0000348 bool window_changed = false;
349 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 +0000350
Pablo Tello3d319462018-06-21 15:13:17 +0100351 if(data_layout == DataLayout::NHWC)
352 {
353 AccessWindowStatic input_access(input, 0, -conv_pad_left,
giuros01c878f1f2019-07-09 11:01:34 +0100354 ceil_to_multiple(input->dimension(0), num_elems_read_per_iteration_x),
Pablo Tello3d319462018-06-21 15:13:17 +0100355 ceil_to_multiple(input->dimension(1) + conv_info.pad_right(), num_elems_read_per_iteration_y));
356 AccessWindowStatic weights_access(weights, 0, 0, weights->dimension(0), weights->dimension(1));
357 AccessWindowRectangle output_access(output, 0, 0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_y);
358 window_changed = update_window_and_padding(win, input_access, weights_access, output_access);
359 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
360 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
361 return std::make_pair(err, win);
362 }
363 else if(data_layout == DataLayout::NCHW)
364 {
365 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);
366 AccessWindowStatic weights_access(weights, 0, 0, kernel_size, kernel_size);
367 AccessWindowRectangle output_access(output, 0, 0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_y);
368 window_changed = update_window_and_padding(win, input_access, weights_access, output_access);
369 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
370 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
371 return std::make_pair(err, win);
372 }
373 else
374 {
375 ARM_COMPUTE_ERROR("Not supported");
376 }
Giorgio Arena59486342017-12-01 10:42:47 +0000377}
378} // namespace
379
380CLDirectConvolutionLayerKernel::CLDirectConvolutionLayerKernel()
381 : _input(nullptr), _biases(nullptr), _weights(nullptr), _output(nullptr), _border_size(0), _conv_stride_x(0), _conv_stride_y(0)
382{
383}
384
385BorderSize CLDirectConvolutionLayerKernel::border_size() const
386{
387 return _border_size;
388}
389
390void CLDirectConvolutionLayerKernel::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info)
391{
392 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
393
Pablo Tello3d319462018-06-21 15:13:17 +0100394 const DataLayout data_layout = input->info()->data_layout();
395 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
396 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
397 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
398
399 const unsigned int kernel_size = weights->info()->dimension(width_idx);
Giorgio Arena59486342017-12-01 10:42:47 +0000400 const DataType data_type = input->info()->data_type();
401
402 // Get convolved dimensions
Giorgio Arenac0f54432018-03-16 14:02:34 +0000403 TensorShape output_shape = misc::shape_calculator::compute_deep_convolution_shape(*input->info(), *weights->info(), conv_info);
Giorgio Arena59486342017-12-01 10:42:47 +0000404
405 // Output auto inizialitation if not yet initialized
Georgios Pinitasf52cd782019-03-25 14:06:14 +0000406 // TODO(COMPMID-2078): input->clone()->set_tensor_shape(output_shape) doesn't work with subtensors for grouped direct convolutions (AlexNet).
Giorgio Arena59486342017-12-01 10:42:47 +0000407 auto_init_if_empty(*output->info(),
408 output_shape,
409 1,
410 input->info()->data_type(),
Giorgio Arena59486342017-12-01 10:42:47 +0000411 input->info()->quantization_info());
412
413 // Perform validation step
414 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(),
415 weights->info(),
416 (biases != nullptr) ? biases->info() : nullptr,
417 output->info(),
418 conv_info));
419
420 _conv_stride_x = std::get<0>(conv_info.stride());
421 _conv_stride_y = std::get<1>(conv_info.stride());
Pablo Tello3d319462018-06-21 15:13:17 +0100422
423 if(data_layout == DataLayout::NHWC)
424 {
425 _border_size = BorderSize(conv_info.pad_left(), 0, conv_info.pad_right(), 0);
426 }
427 else if(data_layout == DataLayout::NCHW)
428 {
429 _border_size = BorderSize(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left());
430 }
431 else
432 {
433 ARM_COMPUTE_ERROR("Not supported");
434 }
Giorgio Arena59486342017-12-01 10:42:47 +0000435
436 _input = input;
437 _weights = weights;
438 _output = output;
439 _biases = biases;
440
Michalis Spyroua9676112018-02-22 18:07:43 +0000441 const GPUTarget gpu_target = get_target();
Giorgio Arena59486342017-12-01 10:42:47 +0000442
443 std::stringstream kernel_name;
444 kernel_name << "direct_convolution" << kernel_size << "x" << kernel_size;
Pablo Tello3d319462018-06-21 15:13:17 +0100445 if(data_layout == DataLayout::NHWC)
446 {
447 kernel_name << "_" << lower_string(string_from_data_layout(data_layout));
448 }
Giorgio Arena59486342017-12-01 10:42:47 +0000449
450 CLBuildOptions build_options;
451 build_options.add_option_if(_biases != nullptr, std::string("-DHAS_BIAS"));
452
Pablo Tello3d319462018-06-21 15:13:17 +0100453 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);
454
455 if(run_optimized_for_bifrost)
Giorgio Arena59486342017-12-01 10:42:47 +0000456 {
Pablo Tello3d319462018-06-21 15:13:17 +0100457 build_options.add_option(std::string("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(_weights->info()->dimension(channel_idx))));
Giorgio Arena59486342017-12-01 10:42:47 +0000458
459 kernel_name << "_f32_bifrost";
460 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name.str(), build_options.options()));
Giorgio Arena59486342017-12-01 10:42:47 +0000461 }
462 else
463 {
Pablo Tellod041a832018-10-03 17:11:09 +0100464 const bool is_quantized_asymm = is_data_type_quantized_asymmetric(data_type);
Giorgio Arena59486342017-12-01 10:42:47 +0000465 build_options.add_option_if(is_quantized_asymm, std::string("-DKERNEL_SIZE=" + support::cpp11::to_string(kernel_size)));
466 build_options.add_option(std::string("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)));
467 build_options.add_option(std::string("-DDATA_SIZE=" + get_data_size_from_data_type(data_type)));
Pablo Tello3d319462018-06-21 15:13:17 +0100468 build_options.add_option(std::string("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(_weights->info()->dimension(channel_idx))));
Giorgio Arena59486342017-12-01 10:42:47 +0000469 build_options.add_option(std::string("-DSTRIDE_X=" + support::cpp11::to_string(_conv_stride_x)));
Pablo Tello3d319462018-06-21 15:13:17 +0100470 if(data_layout == DataLayout::NHWC)
471 {
Michalis Spyrou45091732019-05-13 17:41:01 +0100472 const bool run_optimized_for_bifrost_nhwc = can_run_optimized_kernel_for_bifrost_nhwc(gpu_target, _conv_stride_x, _conv_stride_y, kernel_size, data_type, data_layout);
Pablo Tello3d319462018-06-21 15:13:17 +0100473 build_options.add_option(std::string("-DDATA_LAYOUT_NHWC=1"));
474 build_options.add_option(std::string("-DDST_HEIGHT=" + support::cpp11::to_string(_output->info()->dimension(height_idx))));
475 build_options.add_option(std::string("-DDST_WIDTH=" + support::cpp11::to_string(_output->info()->dimension(width_idx))));
476 build_options.add_option(std::string("-DSRC_HEIGHT=" + support::cpp11::to_string(_input->info()->dimension(height_idx))));
477 build_options.add_option(std::string("-DSRC_WIDTH=" + support::cpp11::to_string(_input->info()->dimension(width_idx))));
478 build_options.add_option(std::string("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left())));
479 build_options.add_option(std::string("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top())));
480 build_options.add_option(std::string("-DSTRIDE_Y=" + support::cpp11::to_string(_conv_stride_y)));
Michalis Spyrou45091732019-05-13 17:41:01 +0100481 if(run_optimized_for_bifrost_nhwc)
482 {
483 const unsigned int num_elems_read_per_iteration_x = 4;
484 _border_size.right = num_elems_read_per_iteration_x;
485 build_options.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_read_per_iteration_x));
486 }
Pablo Tello3d319462018-06-21 15:13:17 +0100487 }
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100488 build_options.add_option(std::string("-DDATA_TYPE_PROMOTED=" + get_cl_type_from_data_type(data_type)));
Giorgio Arena59486342017-12-01 10:42:47 +0000489 // Create kernel
490 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(is_quantized_asymm ? "direct_convolution_1x1_3x3_5x5_quantized" : kernel_name.str(),
491 build_options.options()));
492 }
493
494 // Configure kernel window
495 auto win_config = validate_and_configure_window(input->info(), weights->info(), output->info(), conv_info, gpu_target);
496 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100497 ICLKernel::configure_internal(win_config.second);
Giorgio Arena59486342017-12-01 10:42:47 +0000498
499 // Set static kernel arguments
500 if(is_data_type_quantized_asymmetric(data_type))
501 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100502 const UniformQuantizationInfo iqinfo = _input->info()->quantization_info().uniform();
503 const UniformQuantizationInfo wqinfo = _weights->info()->quantization_info().uniform();
504 const UniformQuantizationInfo oqinfo = _output->info()->quantization_info().uniform();
505
Giorgio Arena59486342017-12-01 10:42:47 +0000506 int output_multiplier = 0;
507 int output_shift = 0;
508
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100509 float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale;
Giorgio Arena59486342017-12-01 10:42:47 +0000510 ARM_COMPUTE_THROW_ON_ERROR(quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift));
511
512 unsigned int idx = 3 * num_arguments_per_3D_tensor() + ((_biases != nullptr) ? num_arguments_per_1D_tensor() : 0) + 1;
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100513 _kernel.setArg(idx++, -iqinfo.offset);
514 _kernel.setArg(idx++, -wqinfo.offset);
515 _kernel.setArg(idx++, oqinfo.offset);
Giorgio Arena59486342017-12-01 10:42:47 +0000516 _kernel.setArg(idx++, output_multiplier);
517 _kernel.setArg(idx++, output_shift);
518 }
519
520 // Set config_id for enabling LWS tuning
521 _config_id = "direct_convolution_";
522 _config_id += lower_string(string_from_data_type(data_type));
523 _config_id += "_";
524 _config_id += support::cpp11::to_string(kernel_size);
525 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000526 _config_id += support::cpp11::to_string(border_size().left);
Giorgio Arena59486342017-12-01 10:42:47 +0000527 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000528 _config_id += support::cpp11::to_string(border_size().top);
Giorgio Arena59486342017-12-01 10:42:47 +0000529 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000530 _config_id += support::cpp11::to_string(border_size().right);
Giorgio Arena59486342017-12-01 10:42:47 +0000531 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000532 _config_id += support::cpp11::to_string(border_size().bottom);
Giorgio Arena59486342017-12-01 10:42:47 +0000533 _config_id += "_";
534 _config_id += support::cpp11::to_string(_conv_stride_x);
535 _config_id += "_";
536 _config_id += support::cpp11::to_string(_conv_stride_y);
537 _config_id += "_";
Pablo Tello3d319462018-06-21 15:13:17 +0100538 _config_id += support::cpp11::to_string(output->info()->dimension(width_idx));
Giorgio Arena59486342017-12-01 10:42:47 +0000539 _config_id += "_";
Pablo Tello3d319462018-06-21 15:13:17 +0100540 _config_id += support::cpp11::to_string(output->info()->dimension(height_idx));
541 _config_id += "_";
542 _config_id += lower_string(string_from_data_layout(data_layout));
Giorgio Arena59486342017-12-01 10:42:47 +0000543}
544
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000545Status CLDirectConvolutionLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
546 const GPUTarget target)
Giorgio Arena59486342017-12-01 10:42:47 +0000547{
548 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, conv_info));
549 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), weights->clone().get(), output->clone().get(), conv_info, target).first);
550
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000551 return Status{};
Giorgio Arena59486342017-12-01 10:42:47 +0000552}
553
SiCong Lic51b72f2017-07-28 14:46:20 +0100554void CLDirectConvolutionLayerKernel::run(const Window &window, cl::CommandQueue &queue)
steniu0127b386c2017-07-18 17:37:43 +0100555{
556 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
557 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
558
559 // Get initial windows
560 Window slice = window.first_slice_window_3D();
561 Window win_in = window;
562
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +0000563 win_in.adjust(Window::DimX, -_border_size.left, true);
564 win_in.adjust(Window::DimY, -_border_size.top, true);
steniu0127b386c2017-07-18 17:37:43 +0100565
Pablo Tello3d319462018-06-21 15:13:17 +0100566 const DataLayout data_layout = _input->info()->data_layout();
567 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
568 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
steniu0127b386c2017-07-18 17:37:43 +0100569
Pablo Tello3d319462018-06-21 15:13:17 +0100570 win_in.set_dimension_step(width_idx, window[width_idx].step() * _conv_stride_x);
571 win_in.set_dimension_step(height_idx, window[height_idx].step() * _conv_stride_y);
572
573 Window slice_in = win_in.first_slice_window_3D();
574 unsigned int idx1 = 2 * num_arguments_per_3D_tensor();
steniu0127b386c2017-07-18 17:37:43 +0100575 add_3D_tensor_argument(idx1, _weights, slice);
576
577 if(_biases != nullptr)
578 {
579 Window slice_biases;
SiCong Li86b53332017-08-23 11:02:43 +0100580 slice_biases.use_tensor_dimensions(_biases->info()->tensor_shape());
steniu0127b386c2017-07-18 17:37:43 +0100581 add_1D_tensor_argument(idx1, _biases, slice_biases);
582 }
583
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100584 _kernel.setArg(idx1++, static_cast<unsigned int>(_weights->info()->strides_in_bytes()[3]));
585
steniu0127b386c2017-07-18 17:37:43 +0100586 do
587 {
588 unsigned int idx = 0;
589 add_3D_tensor_argument(idx, _input, slice_in);
590 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100591 enqueue(queue, *this, slice, lws_hint());
steniu0127b386c2017-07-18 17:37:43 +0100592 }
593 while(window.slide_window_slice_3D(slice) && win_in.slide_window_slice_3D(slice_in));
594}