blob: d2794d7abd055b04535dce59659de2b050f682f7 [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);
Georgios Pinitas30902ed2017-11-14 15:32:57 +000048 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
49 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
50 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(0) != weights->dimension(1),
51 "Weights should have same width as length");
52 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(0) != 1 && weights->dimension(0) != 3 && weights->dimension(0) != 5,
53 "Kernel sizes other than 1x1, 3x3 or 5x5 are not supported");
54 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(2) != input->dimension(2),
55 "Weights feature map dimension should match the respective input's one");
56 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(0) != weights->dimension(1),
57 "Only rectangular weights are supported!");
58 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->num_dimensions() > 4,
59 "Weights can be at most 4 dimensional");
60 ARM_COMPUTE_RETURN_ERROR_ON_MSG((weights->dimension(0) == 1) && std::get<0>(conv_info.stride()) > 3,
61 "Strides larger than 3 not supported for 1x1 convolution.");
62 ARM_COMPUTE_RETURN_ERROR_ON_MSG((weights->dimension(0) == 3 || weights->dimension(0) == 5) && std::get<0>(conv_info.stride()) > 2,
63 "Strides larger than 2 not supported for 3x3 convolution.");
64
65 if(biases != nullptr)
66 {
67 if(is_data_type_quantized_asymmetric(input->data_type()))
68 {
69 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
70 }
71 else
72 {
73 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
74 }
75 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->dimension(0) != weights->dimension(3),
76 "Biases size and number of input feature maps should match");
77 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->num_dimensions() > 1,
78 "Biases should be one dimensional");
79 }
80
81 // Checks performed when output is configured
82 if(output->total_size() != 0)
83 {
84 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(),
Giorgio Arenac0f54432018-03-16 14:02:34 +000085 misc::shape_calculator::compute_deep_convolution_shape(*input, *weights, conv_info));
Georgios Pinitas30902ed2017-11-14 15:32:57 +000086 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
87 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
88 }
89
Georgios Pinitas631c41a2017-12-06 11:53:03 +000090 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +000091}
92
Georgios Pinitas631c41a2017-12-06 11:53:03 +000093std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *output, const PadStrideInfo &conv_info, const GPUTarget target)
Giorgio Arena59486342017-12-01 10:42:47 +000094{
95 const unsigned int kernel_size = weights->dimension(0);
96 const DataType data_type = input->data_type();
97
98 // Get convolved dimensions
Giorgio Arenac0f54432018-03-16 14:02:34 +000099 TensorShape output_shape = misc::shape_calculator::compute_deep_convolution_shape(*input, *weights, conv_info);
Giorgio Arena59486342017-12-01 10:42:47 +0000100
101 // Output auto inizialitation if not yet initialized
102 // FIXME: input->clone()->set_tensor_shape(output_shape) doesn't work with subtensors for grouped direct convolutions (AlexNet).
103 auto_init_if_empty(*output, output_shape,
104 1,
105 input->data_type(),
106 input->fixed_point_position(),
107 input->quantization_info());
108
Anthony Barbier328891c2018-02-21 14:00:44 +0000109 unsigned int conv_stride_x = std::get<0>(conv_info.stride());
110 unsigned int conv_stride_y = std::get<1>(conv_info.stride());
111 unsigned int conv_pad_left = conv_info.pad_left();
112 unsigned int conv_pad_top = conv_info.pad_top();
Giorgio Arena59486342017-12-01 10:42:47 +0000113
114 unsigned int num_elems_read_per_iteration_x = 0;
115 unsigned int num_elems_read_per_iteration_y = 0;
116 unsigned int num_elems_written_per_iteration_x = 0;
117 unsigned int num_elems_written_per_iteration_y = 0;
118
Giorgio Arenac0f54432018-03-16 14:02:34 +0000119 if(gpu_target_is_in(target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, GPUTarget::TNOX) && (kernel_size <= 5) && (conv_stride_x == 1)
120 && (conv_stride_y == 1) && (data_type == DataType::F32))
Giorgio Arena59486342017-12-01 10:42:47 +0000121 {
122 // Configure kernel window
Giorgio Arena59486342017-12-01 10:42:47 +0000123
124 switch(kernel_size)
125 {
126 case 1:
127 {
128 num_elems_read_per_iteration_x = 4;
129 num_elems_read_per_iteration_y = 4;
130 num_elems_written_per_iteration_x = 4;
131 num_elems_written_per_iteration_y = 4;
132 break;
133 }
134 case 3:
135 {
136 num_elems_read_per_iteration_x = 6;
137 num_elems_read_per_iteration_y = 5;
138 num_elems_written_per_iteration_x = 4;
139 num_elems_written_per_iteration_y = 3;
140 break;
141 }
142 case 5:
143 {
144 num_elems_read_per_iteration_x = 8;
145 num_elems_read_per_iteration_y = 6;
146 num_elems_written_per_iteration_x = 4;
147 num_elems_written_per_iteration_y = 2;
148 break;
149 }
150 default:
151 {
152 ARM_COMPUTE_ERROR("Kernel size not optimized for Bifrost");
153 }
154 }
155 }
156 else
157 {
Giorgio Arena59486342017-12-01 10:42:47 +0000158 num_elems_read_per_iteration_y = kernel_size;
159 num_elems_written_per_iteration_x = 8;
160 num_elems_written_per_iteration_y = 1;
Anthony Barbiercc9fed52017-12-13 10:46:00 +0000161 switch(kernel_size)
162 {
163 case 1:
164 switch(conv_stride_x)
165 {
166 case 1:
167 num_elems_read_per_iteration_x = 8;
168 break;
169 case 2:
170 num_elems_read_per_iteration_x = 16;
171 break;
172 case 3:
173 switch(input->element_size())
174 {
175 case 1:
176 num_elems_read_per_iteration_x = 28;
177 break;
178 case 2:
179 num_elems_read_per_iteration_x = 24;
180 break;
181 case 4:
182 num_elems_read_per_iteration_x = 22;
183 break;
184 default:
185 ARM_COMPUTE_ERROR("Invalid data size");
186 }
187 break;
188 default:
189 ARM_COMPUTE_ERROR("Invalid convolution stride X");
190 }
191 break;
192 case 3:
193 switch(conv_stride_x)
194 {
195 case 1:
196 num_elems_read_per_iteration_x = 10;
197 break;
198 case 2:
199 num_elems_read_per_iteration_x = 17;
200 break;
201 default:
202 ARM_COMPUTE_ERROR("Invalid convolution stride X");
203 }
204 break;
205 case 5:
206 switch(conv_stride_x)
207 {
208 case 1:
209 num_elems_read_per_iteration_x = 12;
210 break;
211 case 2:
212 num_elems_read_per_iteration_x = 20;
213 break;
214 default:
215 ARM_COMPUTE_ERROR("Invalid convolution stride X");
216 }
217 break;
218 default:
219 ARM_COMPUTE_ERROR("Invalid direct convolution size");
220 }
Giorgio Arena59486342017-12-01 10:42:47 +0000221 }
222
Giorgio Arena59486342017-12-01 10:42:47 +0000223 // Create window and update padding
Anthony Barbiercc9fed52017-12-13 10:46:00 +0000224 bool window_changed = false;
225 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 +0000226
Georgios Pinitas1a03d762018-02-21 14:47:09 +0000227 AccessWindowRectangle input_access(input, -conv_pad_left, -conv_pad_top,
228 num_elems_read_per_iteration_x, num_elems_read_per_iteration_y,
229 conv_stride_x, conv_stride_y);
Giorgio Arena59486342017-12-01 10:42:47 +0000230 AccessWindowStatic weights_access(weights, 0, 0, kernel_size, kernel_size);
231 AccessWindowRectangle output_access(output, 0, 0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_y);
232
233 window_changed = update_window_and_padding(win, input_access, weights_access, output_access);
234
235 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
236
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000237 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arena59486342017-12-01 10:42:47 +0000238 return std::make_pair(err, win);
239}
240} // namespace
241
242CLDirectConvolutionLayerKernel::CLDirectConvolutionLayerKernel()
243 : _input(nullptr), _biases(nullptr), _weights(nullptr), _output(nullptr), _border_size(0), _conv_stride_x(0), _conv_stride_y(0)
244{
245}
246
247BorderSize CLDirectConvolutionLayerKernel::border_size() const
248{
249 return _border_size;
250}
251
252void CLDirectConvolutionLayerKernel::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info)
253{
254 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
255
256 const unsigned int kernel_size = weights->info()->dimension(0);
257 const DataType data_type = input->info()->data_type();
258
259 // Get convolved dimensions
Giorgio Arenac0f54432018-03-16 14:02:34 +0000260 TensorShape output_shape = misc::shape_calculator::compute_deep_convolution_shape(*input->info(), *weights->info(), conv_info);
Giorgio Arena59486342017-12-01 10:42:47 +0000261
262 // Output auto inizialitation if not yet initialized
263 // FIXME: input->clone()->set_tensor_shape(output_shape) doesn't work with subtensors for grouped direct convolutions (AlexNet).
264 auto_init_if_empty(*output->info(),
265 output_shape,
266 1,
267 input->info()->data_type(),
268 input->info()->fixed_point_position(),
269 input->info()->quantization_info());
270
271 // Perform validation step
272 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(),
273 weights->info(),
274 (biases != nullptr) ? biases->info() : nullptr,
275 output->info(),
276 conv_info));
277
278 _conv_stride_x = std::get<0>(conv_info.stride());
279 _conv_stride_y = std::get<1>(conv_info.stride());
Georgios Pinitas15997872018-02-19 13:58:22 +0000280 _border_size = BorderSize(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left());
Giorgio Arena59486342017-12-01 10:42:47 +0000281
282 _input = input;
283 _weights = weights;
284 _output = output;
285 _biases = biases;
286
Michalis Spyroua9676112018-02-22 18:07:43 +0000287 const GPUTarget gpu_target = get_target();
Giorgio Arena59486342017-12-01 10:42:47 +0000288
289 std::stringstream kernel_name;
290 kernel_name << "direct_convolution" << kernel_size << "x" << kernel_size;
291
292 CLBuildOptions build_options;
293 build_options.add_option_if(_biases != nullptr, std::string("-DHAS_BIAS"));
294
Giorgio Arenac0f54432018-03-16 14:02:34 +0000295 if(gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, GPUTarget::TNOX) && (kernel_size <= 5) && (_conv_stride_x == 1)
296 && (_conv_stride_y == 1) && (data_type == DataType::F32))
Giorgio Arena59486342017-12-01 10:42:47 +0000297 {
298 build_options.add_option(std::string("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(_weights->info()->dimension(2))));
299
300 kernel_name << "_f32_bifrost";
301 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name.str(), build_options.options()));
Giorgio Arena59486342017-12-01 10:42:47 +0000302 }
303 else
304 {
305 bool is_quantized_fixed_point = is_data_type_fixed_point(data_type);
306 bool is_quantized_asymm = is_data_type_quantized_asymmetric(data_type);
307 DataType promoted_type = (is_quantized_fixed_point) ? get_promoted_data_type(data_type) : data_type;
308
309 build_options.add_option_if(is_quantized_asymm, std::string("-DKERNEL_SIZE=" + support::cpp11::to_string(kernel_size)));
310 build_options.add_option(std::string("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)));
311 build_options.add_option(std::string("-DDATA_SIZE=" + get_data_size_from_data_type(data_type)));
312 build_options.add_option(std::string("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(_weights->info()->dimension(2))));
313 build_options.add_option(std::string("-DSTRIDE_X=" + support::cpp11::to_string(_conv_stride_x)));
314 build_options.add_option_if(is_quantized_fixed_point,
315 std::string("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())));
316 build_options.add_option(std::string("-DDATA_TYPE_PROMOTED=" + get_cl_type_from_data_type(promoted_type)));
317
318 // Create kernel
319 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(is_quantized_asymm ? "direct_convolution_1x1_3x3_5x5_quantized" : kernel_name.str(),
320 build_options.options()));
321 }
322
323 // Configure kernel window
324 auto win_config = validate_and_configure_window(input->info(), weights->info(), output->info(), conv_info, gpu_target);
325 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
326 ICLKernel::configure(win_config.second);
327
328 // Set static kernel arguments
329 if(is_data_type_quantized_asymmetric(data_type))
330 {
331 int output_multiplier = 0;
332 int output_shift = 0;
333
334 float multiplier = _input->info()->quantization_info().scale * _weights->info()->quantization_info().scale / _output->info()->quantization_info().scale;
335 ARM_COMPUTE_THROW_ON_ERROR(quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift));
336
337 unsigned int idx = 3 * num_arguments_per_3D_tensor() + ((_biases != nullptr) ? num_arguments_per_1D_tensor() : 0) + 1;
338 _kernel.setArg(idx++, -_input->info()->quantization_info().offset);
339 _kernel.setArg(idx++, -_weights->info()->quantization_info().offset);
340 _kernel.setArg(idx++, _output->info()->quantization_info().offset);
341 _kernel.setArg(idx++, output_multiplier);
342 _kernel.setArg(idx++, output_shift);
343 }
344
345 // Set config_id for enabling LWS tuning
346 _config_id = "direct_convolution_";
347 _config_id += lower_string(string_from_data_type(data_type));
348 _config_id += "_";
349 _config_id += support::cpp11::to_string(kernel_size);
350 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000351 _config_id += support::cpp11::to_string(border_size().left);
Giorgio Arena59486342017-12-01 10:42:47 +0000352 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000353 _config_id += support::cpp11::to_string(border_size().top);
Giorgio Arena59486342017-12-01 10:42:47 +0000354 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000355 _config_id += support::cpp11::to_string(border_size().right);
Giorgio Arena59486342017-12-01 10:42:47 +0000356 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000357 _config_id += support::cpp11::to_string(border_size().bottom);
Giorgio Arena59486342017-12-01 10:42:47 +0000358 _config_id += "_";
359 _config_id += support::cpp11::to_string(_conv_stride_x);
360 _config_id += "_";
361 _config_id += support::cpp11::to_string(_conv_stride_y);
362 _config_id += "_";
363 _config_id += support::cpp11::to_string(output->info()->dimension(0));
364 _config_id += "_";
365 _config_id += support::cpp11::to_string(output->info()->dimension(1));
366}
367
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000368Status CLDirectConvolutionLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
369 const GPUTarget target)
Giorgio Arena59486342017-12-01 10:42:47 +0000370{
371 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, conv_info));
372 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), weights->clone().get(), output->clone().get(), conv_info, target).first);
373
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000374 return Status{};
Giorgio Arena59486342017-12-01 10:42:47 +0000375}
376
SiCong Lic51b72f2017-07-28 14:46:20 +0100377void CLDirectConvolutionLayerKernel::run(const Window &window, cl::CommandQueue &queue)
steniu0127b386c2017-07-18 17:37:43 +0100378{
379 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
380 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
381
382 // Get initial windows
383 Window slice = window.first_slice_window_3D();
384 Window win_in = window;
385
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +0000386 win_in.adjust(Window::DimX, -_border_size.left, true);
387 win_in.adjust(Window::DimY, -_border_size.top, true);
steniu0127b386c2017-07-18 17:37:43 +0100388 win_in.set_dimension_step(Window::DimX, window.x().step() * _conv_stride_x);
389 win_in.set_dimension_step(Window::DimY, window.y().step() * _conv_stride_y);
390
391 Window slice_in = win_in.first_slice_window_3D();
392
393 unsigned int idx1 = 2 * num_arguments_per_3D_tensor();
394 add_3D_tensor_argument(idx1, _weights, slice);
395
396 if(_biases != nullptr)
397 {
398 Window slice_biases;
SiCong Li86b53332017-08-23 11:02:43 +0100399 slice_biases.use_tensor_dimensions(_biases->info()->tensor_shape());
steniu0127b386c2017-07-18 17:37:43 +0100400 add_1D_tensor_argument(idx1, _biases, slice_biases);
401 }
402
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100403 _kernel.setArg(idx1++, static_cast<unsigned int>(_weights->info()->strides_in_bytes()[3]));
404
steniu0127b386c2017-07-18 17:37:43 +0100405 do
406 {
407 unsigned int idx = 0;
408 add_3D_tensor_argument(idx, _input, slice_in);
409 add_3D_tensor_argument(idx, _output, slice);
410
411 enqueue(queue, *this, slice, _lws_hint);
412 }
413 while(window.slide_window_slice_3D(slice) && win_in.slide_window_slice_3D(slice_in));
414}