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