blob: f071dbc468e235a0196ef79e8726c78f5cf0a785 [file] [log] [blame]
steniu0127b386c2017-07-18 17:37:43 +01001/*
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +00002 * Copyright (c) 2017-2021 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 */
Sheri Zhang1efed922021-03-10 22:43:38 +000024#include "src/core/gpu/cl/kernels/ClDirectConvolutionKernel.h"
steniu0127b386c2017-07-18 17:37:43 +010025
steniu0127b386c2017-07-18 17:37:43 +010026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
steniu0127b386c2017-07-18 17:37:43 +010029#include "arm_compute/core/Helpers.h"
steniu0127b386c2017-07-18 17:37:43 +010030#include "arm_compute/core/ITensor.h"
Gian Marco Iodiced95c3e82021-01-19 17:39:02 +000031#include "arm_compute/core/PixelValue.h"
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010032#include "arm_compute/core/Utils.h"
Giorgio Arenac0f54432018-03-16 14:02:34 +000033#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Chunosovd621bca2017-11-03 17:33:15 +070034#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/AccessWindowStatic.h"
36#include "src/core/CL/CLValidate.h"
37#include "src/core/helpers/AutoConfiguration.h"
38#include "src/core/helpers/WindowHelpers.h"
Sheri Zhang1efed922021-03-10 22:43:38 +000039#include "support/Cast.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000040#include "support/StringSupport.h"
steniu0127b386c2017-07-18 17:37:43 +010041
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +010042namespace arm_compute
43{
Sheri Zhang1efed922021-03-10 22:43:38 +000044namespace opencl
45{
46namespace kernels
47{
Georgios Pinitas30902ed2017-11-14 15:32:57 +000048namespace
49{
Sheri Zhang1efed922021-03-10 22:43:38 +000050Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const PadStrideInfo &conv_info)
Georgios Pinitas30902ed2017-11-14 15:32:57 +000051{
Sheri Zhang1efed922021-03-10 22:43:38 +000052 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
53 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::F16, DataType::F32);
54 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, weights);
Pablo Tello3d319462018-06-21 15:13:17 +010055
Sheri Zhang1efed922021-03-10 22:43:38 +000056 const DataLayout data_layout = src->data_layout();
Pablo Tello3d319462018-06-21 15:13:17 +010057 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
58 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
59 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
60
61 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) != weights->dimension(height_idx), "Weights should have same width and height");
Sheri Zhang1efed922021-03-10 22:43:38 +000062 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(channel_idx) != src->dimension(channel_idx),
63 "Weights feature map dimension should match the respective src's one");
Pablo Tello3d319462018-06-21 15:13:17 +010064 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->num_dimensions() > 4, "Weights can be at most 4 dimensional");
65 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.");
Georgios Pinitasaa95ddc2020-07-21 22:45:13 +010066 ARM_COMPUTE_RETURN_ERROR_ON_MSG((weights->dimension(width_idx) == 3 || weights->dimension(width_idx) == 5 || weights->dimension(width_idx) == 9)
67 && std::get<0>(conv_info.stride()) > 2,
68 "Strides larger than 2 not supported for 3x3, 5x5, 9x9 convolution.");
Georgios Pinitas30902ed2017-11-14 15:32:57 +000069
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +000070 if(data_layout == DataLayout::NCHW)
71 {
Sheri Zhang1efed922021-03-10 22:43:38 +000072 if(is_data_type_quantized(src->data_type()))
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +000073 {
74 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,
Gian Marco Iodiced95c3e82021-01-19 17:39:02 +000075 "Kernel sizes other than 1x1, 3x3, 5x5 or 9x9 are not supported with quantized data types");
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +000076 }
77 else
78 {
79 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) != 1 && weights->dimension(width_idx) != 3 && weights->dimension(width_idx) != 5,
80 "Kernel sizes other than 1x1, 3x3 or 5x5 are not supported with float data types");
81 }
82 }
83
Georgios Pinitas30902ed2017-11-14 15:32:57 +000084 if(biases != nullptr)
85 {
Sheri Zhang1efed922021-03-10 22:43:38 +000086 if(is_data_type_quantized_asymmetric(src->data_type()))
Georgios Pinitas30902ed2017-11-14 15:32:57 +000087 {
88 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
89 }
90 else
91 {
92 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
93 }
94 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->dimension(0) != weights->dimension(3),
Sheri Zhang1efed922021-03-10 22:43:38 +000095 "Biases size and number of src feature maps should match");
Georgios Pinitas30902ed2017-11-14 15:32:57 +000096 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->num_dimensions() > 1,
97 "Biases should be one dimensional");
98 }
99
Sheri Zhang1efed922021-03-10 22:43:38 +0000100 // Checks performed when dst is configured
101 if(dst->total_size() != 0)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000102 {
Sheri Zhang1efed922021-03-10 22:43:38 +0000103 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(),
104 misc::shape_calculator::compute_deep_convolution_shape(*src, *weights, conv_info));
105 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000106 }
107
Sheri Zhang1efed922021-03-10 22:43:38 +0000108 const auto data_type = src->data_type();
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100109 if(is_data_type_quantized(data_type))
110 {
Sheri Zhang1efed922021-03-10 22:43:38 +0000111 const UniformQuantizationInfo iqinfo = src->quantization_info().uniform();
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100112 const UniformQuantizationInfo wqinfo = weights->quantization_info().uniform();
Sheri Zhang1efed922021-03-10 22:43:38 +0000113 const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform();
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100114
115 float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale;
116 int output_multiplier = 0;
117 int output_shift = 0;
118 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift));
119 }
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000120 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000121}
122
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000123inline bool can_run_optimized_kernel_for_bifrost_nchw(GPUTarget gpu_target, unsigned int conv_stride_x, unsigned int conv_stride_y, unsigned int kernel_size,
124 DataType data_type, DataLayout data_layout)
Giorgio Arena59486342017-12-01 10:42:47 +0000125{
Georgios Pinitasa34286e2018-09-04 12:18:50 +0100126 return gpu_target_is_in(gpu_target,
127 GPUTarget::G71, GPUTarget::G72, GPUTarget::G76,
128 GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT,
129 GPUTarget::G52, GPUTarget::G52LIT)
130 && (kernel_size <= 5)
131 && (conv_stride_x == 1) && (conv_stride_y == 1)
132 && (data_type == DataType::F32)
133 && (data_layout == DataLayout::NCHW);
Pablo Tello3d319462018-06-21 15:13:17 +0100134}
Giorgio Arena59486342017-12-01 10:42:47 +0000135
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000136inline void setup_num_elems_nchw(unsigned int &num_elems_read_per_iteration_x, unsigned int &num_elems_read_per_iteration_y,
137 unsigned int &num_elems_written_per_iteration_x, unsigned int &num_elems_written_per_iteration_y,
Sheri Zhang1efed922021-03-10 22:43:38 +0000138 unsigned int kernel_size, const PadStrideInfo &conv_info, const GPUTarget target, ITensorInfo *src)
Pablo Tello3d319462018-06-21 15:13:17 +0100139{
Sheri Zhang1efed922021-03-10 22:43:38 +0000140 const DataType data_type = src->data_type();
141 const DataLayout data_layout = src->data_layout();
Pablo Tello3d319462018-06-21 15:13:17 +0100142 unsigned int conv_stride_x = std::get<0>(conv_info.stride());
143 unsigned int conv_stride_y = std::get<1>(conv_info.stride());
Giorgio Arena59486342017-12-01 10:42:47 +0000144
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000145 const bool run_optimized_bifrost = can_run_optimized_kernel_for_bifrost_nchw(target, conv_stride_x, conv_stride_y, kernel_size, data_type, data_layout);
Giorgio Arena59486342017-12-01 10:42:47 +0000146
Pablo Tello3d319462018-06-21 15:13:17 +0100147 if(run_optimized_bifrost)
Giorgio Arena59486342017-12-01 10:42:47 +0000148 {
149 // Configure kernel window
Giorgio Arena59486342017-12-01 10:42:47 +0000150 switch(kernel_size)
151 {
152 case 1:
153 {
154 num_elems_read_per_iteration_x = 4;
155 num_elems_read_per_iteration_y = 4;
156 num_elems_written_per_iteration_x = 4;
157 num_elems_written_per_iteration_y = 4;
158 break;
159 }
160 case 3:
161 {
162 num_elems_read_per_iteration_x = 6;
163 num_elems_read_per_iteration_y = 5;
164 num_elems_written_per_iteration_x = 4;
165 num_elems_written_per_iteration_y = 3;
166 break;
167 }
168 case 5:
169 {
170 num_elems_read_per_iteration_x = 8;
171 num_elems_read_per_iteration_y = 6;
172 num_elems_written_per_iteration_x = 4;
173 num_elems_written_per_iteration_y = 2;
174 break;
175 }
176 default:
177 {
178 ARM_COMPUTE_ERROR("Kernel size not optimized for Bifrost");
179 }
180 }
181 }
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000182 else
Giorgio Arena59486342017-12-01 10:42:47 +0000183 {
Giorgio Arena59486342017-12-01 10:42:47 +0000184 num_elems_read_per_iteration_y = kernel_size;
185 num_elems_written_per_iteration_x = 8;
186 num_elems_written_per_iteration_y = 1;
Anthony Barbiercc9fed52017-12-13 10:46:00 +0000187 switch(kernel_size)
188 {
189 case 1:
190 switch(conv_stride_x)
191 {
192 case 1:
193 num_elems_read_per_iteration_x = 8;
194 break;
195 case 2:
196 num_elems_read_per_iteration_x = 16;
197 break;
198 case 3:
Sheri Zhang1efed922021-03-10 22:43:38 +0000199 switch(src->element_size())
Anthony Barbiercc9fed52017-12-13 10:46:00 +0000200 {
201 case 1:
202 num_elems_read_per_iteration_x = 28;
203 break;
204 case 2:
205 num_elems_read_per_iteration_x = 24;
206 break;
207 case 4:
208 num_elems_read_per_iteration_x = 22;
209 break;
210 default:
211 ARM_COMPUTE_ERROR("Invalid data size");
212 }
213 break;
214 default:
215 ARM_COMPUTE_ERROR("Invalid convolution stride X");
216 }
217 break;
218 case 3:
219 switch(conv_stride_x)
220 {
221 case 1:
222 num_elems_read_per_iteration_x = 10;
223 break;
224 case 2:
225 num_elems_read_per_iteration_x = 17;
226 break;
227 default:
228 ARM_COMPUTE_ERROR("Invalid convolution stride X");
229 }
230 break;
231 case 5:
232 switch(conv_stride_x)
233 {
234 case 1:
235 num_elems_read_per_iteration_x = 12;
236 break;
237 case 2:
238 num_elems_read_per_iteration_x = 20;
239 break;
240 default:
241 ARM_COMPUTE_ERROR("Invalid convolution stride X");
242 }
243 break;
Sang-Hoon Parkab5b1a22019-10-15 09:29:13 +0100244 case 9:
245 switch(conv_stride_x)
246 {
247 case 1:
248 num_elems_read_per_iteration_x = 16;
249 break;
250 case 2:
251 num_elems_read_per_iteration_x = 24;
252 break;
253 default:
254 ARM_COMPUTE_ERROR("Invalid convolution stride X");
255 }
256 break;
Anthony Barbiercc9fed52017-12-13 10:46:00 +0000257 default:
258 ARM_COMPUTE_ERROR("Invalid direct convolution size");
259 }
Giorgio Arena59486342017-12-01 10:42:47 +0000260 }
Pablo Tello3d319462018-06-21 15:13:17 +0100261}
262
Sheri Zhang1efed922021-03-10 22:43:38 +0000263std::pair<Status, Window> validate_and_configure_window(ITensorInfo *src, ITensorInfo *weights, ITensorInfo *dst, const PadStrideInfo &conv_info, const GPUTarget target)
Pablo Tello3d319462018-06-21 15:13:17 +0100264{
Sheri Zhang1efed922021-03-10 22:43:38 +0000265 const DataLayout data_layout = src->data_layout();
Pablo Tello3d319462018-06-21 15:13:17 +0100266
Sheri Zhang1efed922021-03-10 22:43:38 +0000267 // Get dst shape
268 TensorShape output_shape = misc::shape_calculator::compute_deep_convolution_shape(*src, *weights, conv_info);
Pablo Tello3d319462018-06-21 15:13:17 +0100269
270 // Output auto inizialitation if not yet initialized
Sheri Zhang1efed922021-03-10 22:43:38 +0000271 auto_init_if_empty(*dst, output_shape,
Pablo Tello3d319462018-06-21 15:13:17 +0100272 1,
Sheri Zhang1efed922021-03-10 22:43:38 +0000273 src->data_type(),
274 src->quantization_info());
Pablo Tello3d319462018-06-21 15:13:17 +0100275
Pablo Tello3d319462018-06-21 15:13:17 +0100276 if(data_layout == DataLayout::NHWC)
277 {
Sheri Zhang1efed922021-03-10 22:43:38 +0000278 const unsigned int vec_size = std::min(static_cast<unsigned int>(dst->tensor_shape()[0]), 4u);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000279
280 // Create window and update padding
Sheri Zhang1efed922021-03-10 22:43:38 +0000281 Window win = calculate_max_window(*dst, Steps(vec_size, 1U));
282 dst->set_valid_region(ValidRegion(Coordinates(), dst->tensor_shape()));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000283 Status err = Status{};
Pablo Tello3d319462018-06-21 15:13:17 +0100284 return std::make_pair(err, win);
285 }
286 else if(data_layout == DataLayout::NCHW)
287 {
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000288 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
289 const unsigned int kernel_size = weights->dimension(width_idx);
290
291 unsigned int num_elems_read_per_iteration_x = 0;
292 unsigned int num_elems_read_per_iteration_y = 0;
293 unsigned int num_elems_written_per_iteration_x = 0;
294 unsigned int num_elems_written_per_iteration_y = 0;
295
296 unsigned int conv_pad_left = conv_info.pad_left();
297 unsigned int conv_pad_top = conv_info.pad_top();
298 unsigned int conv_stride_x = std::get<0>(conv_info.stride());
299 unsigned int conv_stride_y = std::get<1>(conv_info.stride());
300
301 setup_num_elems_nchw(num_elems_read_per_iteration_x, num_elems_read_per_iteration_y,
302 num_elems_written_per_iteration_x, num_elems_written_per_iteration_y,
Sheri Zhang1efed922021-03-10 22:43:38 +0000303 kernel_size, conv_info, target, src);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000304
305 // Create window and update padding
306 bool window_changed = false;
Sheri Zhang1efed922021-03-10 22:43:38 +0000307 Window win = calculate_max_window(*dst, Steps(num_elems_written_per_iteration_x, num_elems_written_per_iteration_y));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000308
Sheri Zhang1efed922021-03-10 22:43:38 +0000309 AccessWindowRectangle input_access(src, -conv_pad_left, -conv_pad_top, num_elems_read_per_iteration_x, num_elems_read_per_iteration_y, conv_stride_x, conv_stride_y);
Pablo Tello3d319462018-06-21 15:13:17 +0100310 AccessWindowStatic weights_access(weights, 0, 0, kernel_size, kernel_size);
Sheri Zhang1efed922021-03-10 22:43:38 +0000311 AccessWindowRectangle output_access(dst, 0, 0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_y);
Pablo Tello3d319462018-06-21 15:13:17 +0100312 window_changed = update_window_and_padding(win, input_access, weights_access, output_access);
Sheri Zhang1efed922021-03-10 22:43:38 +0000313 output_access.set_valid_region(win, ValidRegion(Coordinates(), dst->tensor_shape()));
Pablo Tello3d319462018-06-21 15:13:17 +0100314 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
315 return std::make_pair(err, win);
316 }
317 else
318 {
319 ARM_COMPUTE_ERROR("Not supported");
320 }
Giorgio Arena59486342017-12-01 10:42:47 +0000321}
322} // namespace
323
Sheri Zhang1efed922021-03-10 22:43:38 +0000324BorderSize ClDirectConvolutionKernel::border_size() const
Giorgio Arena59486342017-12-01 10:42:47 +0000325{
326 return _border_size;
327}
328
Sheri Zhang1efed922021-03-10 22:43:38 +0000329void ClDirectConvolutionKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *dst,
330 const PadStrideInfo &conv_info)
Giorgio Arena59486342017-12-01 10:42:47 +0000331{
Sheri Zhang1efed922021-03-10 22:43:38 +0000332 ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst);
Giorgio Arena59486342017-12-01 10:42:47 +0000333
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000334 // Perform validation
Sheri Zhang1efed922021-03-10 22:43:38 +0000335 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src,
336 weights,
337 (biases != nullptr) ? biases : nullptr,
338 dst,
Giorgio Arena59486342017-12-01 10:42:47 +0000339 conv_info));
340
Sheri Zhang1efed922021-03-10 22:43:38 +0000341 const int conv_stride_x = std::get<0>(conv_info.stride());
342 const int conv_stride_y = std::get<1>(conv_info.stride());
343
344 _data_layout = src->data_layout();
345 _conv_info = conv_info;
Pablo Tello3d319462018-06-21 15:13:17 +0100346
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000347 const unsigned int width_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
348 const unsigned int height_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
349 const unsigned int channel_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
Sheri Zhang1efed922021-03-10 22:43:38 +0000350 const unsigned int kernel_size = weights->dimension(width_idx);
351 const DataType data_type = src->data_type();
Giorgio Arena59486342017-12-01 10:42:47 +0000352
Michalis Spyroua9676112018-02-22 18:07:43 +0000353 const GPUTarget gpu_target = get_target();
Giorgio Arena59486342017-12-01 10:42:47 +0000354
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000355 // Configure kernel window
Sheri Zhang1efed922021-03-10 22:43:38 +0000356 auto win_config = validate_and_configure_window(src, weights, dst, conv_info, gpu_target);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000357 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
358 ICLKernel::configure_internal(win_config.second);
359
Giorgio Arena59486342017-12-01 10:42:47 +0000360 std::stringstream kernel_name;
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000361 CLBuildOptions build_options;
362
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000363 if(_data_layout == DataLayout::NHWC)
Pablo Tello3d319462018-06-21 15:13:17 +0100364 {
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000365 _border_size = BorderSize();
Giorgio Arena59486342017-12-01 10:42:47 +0000366
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000367 kernel_name << "direct_convolution_nhwc";
Giorgio Arena59486342017-12-01 10:42:47 +0000368
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000369 const unsigned int n0 = win_config.second.x().step();
370 const unsigned int m0 = win_config.second.y().step();
Sheri Zhang1efed922021-03-10 22:43:38 +0000371 const unsigned int k0 = adjust_vec_size(16u, src->dimension(channel_idx));
372 const unsigned int partial_store_n0 = dst->dimension(channel_idx) % n0;
373 const unsigned int partial_store_m0 = (dst->dimension(width_idx) * dst->dimension(height_idx)) % m0;
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000374 const unsigned int pad_left = conv_info.pad_left();
375 const unsigned int pad_top = conv_info.pad_top();
Pablo Tello3d319462018-06-21 15:13:17 +0100376
Sheri Zhang1efed922021-03-10 22:43:38 +0000377 if(biases != nullptr)
Teresa Charlin8d8a1c52021-02-03 17:01:23 +0000378 {
379 build_options.add_option(std::string("-DHAS_BIAS"));
Sheri Zhang1efed922021-03-10 22:43:38 +0000380 build_options.add_option(std::string("-DBIA_DATA_TYPE=" + get_cl_type_from_data_type(biases->data_type())));
Teresa Charlin8d8a1c52021-02-03 17:01:23 +0000381 }
Sheri Zhang1efed922021-03-10 22:43:38 +0000382 build_options.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(width_idx)));
383 build_options.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(height_idx)));
384 build_options.add_option("-DSRC_CHANNELS=" + support::cpp11::to_string(src->dimension(channel_idx)));
385 build_options.add_option("-DSRC_DATA_TYPE=" + get_cl_type_from_data_type(src->data_type()));
386 build_options.add_option("-DDST_WIDTH=" + support::cpp11::to_string(dst->dimension(width_idx)));
387 build_options.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(dst->dimension(height_idx)));
388 build_options.add_option("-DDST_CHANNELS=" + support::cpp11::to_string(dst->dimension(channel_idx)));
389 build_options.add_option("-DDST_DATA_TYPE=" + get_cl_type_from_data_type(dst->data_type()));
390 build_options.add_option("-DWEI_WIDTH=" + support::cpp11::to_string(weights->dimension(width_idx)));
391 build_options.add_option("-DWEI_HEIGHT=" + support::cpp11::to_string(weights->dimension(height_idx)));
392 build_options.add_option("-DWEI_DATA_TYPE=" + get_cl_type_from_data_type(weights->data_type()));
393 build_options.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_stride_x));
394 build_options.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_stride_y));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000395 build_options.add_option("-DPAD_LEFT=" + support::cpp11::to_string(pad_left));
396 build_options.add_option("-DPAD_TOP=" + support::cpp11::to_string(pad_top));
397 build_options.add_option("-DN0=" + support::cpp11::to_string(n0));
398 build_options.add_option("-DM0=" + support::cpp11::to_string(m0));
399 build_options.add_option("-DK0=" + support::cpp11::to_string(k0));
400 build_options.add_option("-DPARTIAL_STORE_N0=" + support::cpp11::to_string(partial_store_n0));
401 build_options.add_option("-DPARTIAL_STORE_M0=" + support::cpp11::to_string(partial_store_m0));
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100402
403 if(is_data_type_quantized(data_type))
404 {
Sheri Zhang1efed922021-03-10 22:43:38 +0000405 const UniformQuantizationInfo iqinfo = src->quantization_info().uniform();
406 const UniformQuantizationInfo wqinfo = weights->quantization_info().uniform();
407 const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform();
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100408
Sheri Zhang1efed922021-03-10 22:43:38 +0000409 PixelValue zero_value = PixelValue(0, src->data_type(), src->quantization_info());
Gian Marco Iodiced95c3e82021-01-19 17:39:02 +0000410 int zero_value_s32;
411 zero_value.get(zero_value_s32);
412
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100413 float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale;
414 int output_multiplier = 0;
415 int output_shift = 0;
416 quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
Gian Marco Iodiced95c3e82021-01-19 17:39:02 +0000417 build_options.add_option("-DIS_QUANTIZED");
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000418 build_options.add_option("-DDST_MULTIPLIER=" + support::cpp11::to_string(output_multiplier));
419 build_options.add_option("-DDST_SHIFT=" + support::cpp11::to_string(output_shift));
420 build_options.add_option("-DSRC_OFFSET=" + support::cpp11::to_string(-iqinfo.offset));
421 build_options.add_option("-DWEI_OFFSET=" + support::cpp11::to_string(-wqinfo.offset));
422 build_options.add_option("-DDST_OFFSET=" + support::cpp11::to_string(oqinfo.offset));
Gian Marco Iodiced95c3e82021-01-19 17:39:02 +0000423 build_options.add_option("-DZERO_VALUE=" + support::cpp11::to_string(zero_value_s32));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000424 build_options.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(DataType::S32));
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100425 }
426 else
427 {
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000428 build_options.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(data_type));
429 build_options.add_option("-DSRC_OFFSET=" + support::cpp11::to_string(0));
430 build_options.add_option("-DWEI_OFFSET=" + support::cpp11::to_string(0));
431 build_options.add_option("-DDST_OFFSET=" + support::cpp11::to_string(0));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000432 }
433 }
434 else
435 {
Sheri Zhang1efed922021-03-10 22:43:38 +0000436 _border_size = BorderSize(src->padding());
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000437
438 kernel_name << "direct_convolution" << kernel_size << "x" << kernel_size;
439
Sheri Zhang1efed922021-03-10 22:43:38 +0000440 build_options.add_option_if(biases != nullptr, std::string("-DHAS_BIAS"));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000441
Sheri Zhang1efed922021-03-10 22:43:38 +0000442 const bool run_optimized_for_bifrost = can_run_optimized_kernel_for_bifrost_nchw(gpu_target, conv_stride_x, conv_stride_y, kernel_size, data_type, _data_layout);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000443
444 if(run_optimized_for_bifrost)
445 {
Sheri Zhang1efed922021-03-10 22:43:38 +0000446 build_options.add_option(std::string("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(weights->dimension(channel_idx))));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000447
448 kernel_name << "_f32_bifrost";
449 }
450 else
451 {
452 build_options.add_option(std::string("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)));
453 build_options.add_option(std::string("-DDATA_SIZE=" + get_data_size_from_data_type(data_type)));
Sheri Zhang1efed922021-03-10 22:43:38 +0000454 build_options.add_option(std::string("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(weights->dimension(channel_idx))));
455 build_options.add_option(std::string("-DSTRIDE_X=" + support::cpp11::to_string(conv_stride_x)));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000456 build_options.add_option(std::string("-DDATA_TYPE_PROMOTED=" + get_cl_type_from_data_type(data_type)));
457
458 if(is_data_type_quantized(data_type))
459 {
Sheri Zhang1efed922021-03-10 22:43:38 +0000460 const UniformQuantizationInfo iqinfo = src->quantization_info().uniform();
461 const UniformQuantizationInfo wqinfo = weights->quantization_info().uniform();
462 const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform();
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000463
464 float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale;
465 int output_multiplier = 0;
466 int output_shift = 0;
467 quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
468 build_options.add_option("-DOUTPUT_MULTIPLIER=" + support::cpp11::to_string(output_multiplier));
469 build_options.add_option("-DOUTPUT_SHIFT=" + support::cpp11::to_string(output_shift));
470 build_options.add_option("-DKERNEL_SIZE=" + support::cpp11::to_string(kernel_size));
471 build_options.add_option("-DINPUT_OFFSET=" + support::cpp11::to_string(-iqinfo.offset));
472 build_options.add_option("-DWEIGHTS_OFFSET=" + support::cpp11::to_string(-wqinfo.offset));
473 build_options.add_option("-DOUTPUT_OFFSET=" + support::cpp11::to_string(oqinfo.offset));
474
475 kernel_name.str("direct_convolution_quantized");
476 }
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100477 }
Giorgio Arena59486342017-12-01 10:42:47 +0000478 }
479
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000480 _kernel = create_kernel(compile_context, kernel_name.str(), build_options.options());
Giorgio Arena59486342017-12-01 10:42:47 +0000481
Giorgio Arena59486342017-12-01 10:42:47 +0000482 // Set config_id for enabling LWS tuning
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000483 _config_id = kernel_name.str();
484 _config_id += "_";
Giorgio Arena59486342017-12-01 10:42:47 +0000485 _config_id += lower_string(string_from_data_type(data_type));
486 _config_id += "_";
487 _config_id += support::cpp11::to_string(kernel_size);
488 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000489 _config_id += support::cpp11::to_string(border_size().left);
Giorgio Arena59486342017-12-01 10:42:47 +0000490 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000491 _config_id += support::cpp11::to_string(border_size().top);
Giorgio Arena59486342017-12-01 10:42:47 +0000492 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000493 _config_id += support::cpp11::to_string(border_size().right);
Giorgio Arena59486342017-12-01 10:42:47 +0000494 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000495 _config_id += support::cpp11::to_string(border_size().bottom);
Giorgio Arena59486342017-12-01 10:42:47 +0000496 _config_id += "_";
Sheri Zhang1efed922021-03-10 22:43:38 +0000497 _config_id += support::cpp11::to_string(conv_stride_x);
Giorgio Arena59486342017-12-01 10:42:47 +0000498 _config_id += "_";
Sheri Zhang1efed922021-03-10 22:43:38 +0000499 _config_id += support::cpp11::to_string(conv_stride_y);
Giorgio Arena59486342017-12-01 10:42:47 +0000500 _config_id += "_";
Sheri Zhang1efed922021-03-10 22:43:38 +0000501 _config_id += support::cpp11::to_string(dst->dimension(width_idx));
Giorgio Arena59486342017-12-01 10:42:47 +0000502 _config_id += "_";
Sheri Zhang1efed922021-03-10 22:43:38 +0000503 _config_id += support::cpp11::to_string(dst->dimension(height_idx));
Pablo Tello3d319462018-06-21 15:13:17 +0100504 _config_id += "_";
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000505 _config_id += lower_string(string_from_data_layout(_data_layout));
Giorgio Arena59486342017-12-01 10:42:47 +0000506}
507
Sheri Zhang1efed922021-03-10 22:43:38 +0000508Status ClDirectConvolutionKernel::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const PadStrideInfo &conv_info,
509 const GPUTarget target)
Giorgio Arena59486342017-12-01 10:42:47 +0000510{
Sheri Zhang1efed922021-03-10 22:43:38 +0000511 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, weights, biases, dst, conv_info));
512 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src->clone().get(), weights->clone().get(), dst->clone().get(), conv_info, target).first);
Giorgio Arena59486342017-12-01 10:42:47 +0000513
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000514 return Status{};
Giorgio Arena59486342017-12-01 10:42:47 +0000515}
516
Sheri Zhang1efed922021-03-10 22:43:38 +0000517void ClDirectConvolutionKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
steniu0127b386c2017-07-18 17:37:43 +0100518{
519 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
520 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
521
522 // Get initial windows
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000523 Window slice = window.first_slice_window_3D();
steniu0127b386c2017-07-18 17:37:43 +0100524
Sheri Zhang1efed922021-03-10 22:43:38 +0000525 const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
526 const auto weights = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
527 const auto biases = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2));
528 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
529
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000530 if(_data_layout == DataLayout::NHWC)
steniu0127b386c2017-07-18 17:37:43 +0100531 {
Sheri Zhang1efed922021-03-10 22:43:38 +0000532 slice.set(Window::DimY, Window::Dimension(0, dst->info()->dimension(1) * dst->info()->dimension(2), 1));
533 slice.set(Window::DimZ, Window::Dimension(0, dst->info()->dimension(3), 1));
steniu0127b386c2017-07-18 17:37:43 +0100534
steniu0127b386c2017-07-18 17:37:43 +0100535 unsigned int idx = 0;
Sheri Zhang1efed922021-03-10 22:43:38 +0000536 add_3D_tensor_argument(idx, src, slice);
537 add_3D_tensor_argument(idx, dst, slice);
538 add_3D_tensor_argument(idx, weights, slice);
539 if(biases != nullptr)
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000540 {
Sheri Zhang1efed922021-03-10 22:43:38 +0000541 add_1D_tensor_argument(idx, biases, slice);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000542 }
Sheri Zhang1efed922021-03-10 22:43:38 +0000543 _kernel.setArg(idx++, static_cast<unsigned int>(weights->info()->strides_in_bytes()[3]));
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100544 enqueue(queue, *this, slice, lws_hint());
steniu0127b386c2017-07-18 17:37:43 +0100545 }
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000546 else
547 {
548 Window win_in = window;
549
Giorgio Arena9a091802021-02-17 16:04:05 +0000550 win_in.adjust(Window::DimX, -_conv_info.pad_left(), true);
551 win_in.adjust(Window::DimY, -_conv_info.pad_top(), true);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000552
553 const int width_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
554 const int height_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
555
Sheri Zhang1efed922021-03-10 22:43:38 +0000556 const int conv_stride_x = std::get<0>(_conv_info.stride());
557 const int conv_stride_y = std::get<1>(_conv_info.stride());
558
559 win_in.set_dimension_step(width_idx, window[width_idx].step() * conv_stride_x);
560 win_in.set_dimension_step(height_idx, window[height_idx].step() * conv_stride_y);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000561
562 Window slice_in = win_in.first_slice_window_3D();
563 unsigned int idx1 = 2 * num_arguments_per_3D_tensor();
Sheri Zhang1efed922021-03-10 22:43:38 +0000564 add_3D_tensor_argument(idx1, weights, slice);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000565
Sheri Zhang1efed922021-03-10 22:43:38 +0000566 if(biases != nullptr)
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000567 {
568 Window slice_biases;
Sheri Zhang1efed922021-03-10 22:43:38 +0000569 slice_biases.use_tensor_dimensions(biases->info()->tensor_shape());
570 add_1D_tensor_argument(idx1, biases, slice_biases);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000571 }
572
Sheri Zhang1efed922021-03-10 22:43:38 +0000573 _kernel.setArg(idx1++, static_cast<unsigned int>(weights->info()->strides_in_bytes()[3]));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000574
575 do
576 {
577 unsigned int idx = 0;
Sheri Zhang1efed922021-03-10 22:43:38 +0000578 add_3D_tensor_argument(idx, src, slice_in);
579 add_3D_tensor_argument(idx, dst, slice);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000580 enqueue(queue, *this, slice, lws_hint());
581 }
582 while(window.slide_window_slice_3D(slice) && win_in.slide_window_slice_3D(slice_in));
583 }
steniu0127b386c2017-07-18 17:37:43 +0100584}
Sheri Zhang1efed922021-03-10 22:43:38 +0000585} // namespace kernels
586} // namespace opencl
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100587} // namespace arm_compute