blob: e1901af21772b3886c417ce480f0bbdb25872cdf [file] [log] [blame]
steniu0127b386c2017-07-18 17:37:43 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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"
Chunosovd621bca2017-11-03 17:33:15 +070037#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
steniu0127b386c2017-07-18 17:37:43 +010038#include "support/ToolchainSupport.h"
39
40using namespace arm_compute;
41
Georgios Pinitas30902ed2017-11-14 15:32:57 +000042namespace
43{
44/** Calculates expected output shape dimension
45 *
46 * @param[in] Input shape
47 *
48 * @return Expected output shape
49 */
50TensorShape get_output_shape(TensorShape input_shape, TensorShape weights_shape, PadStrideInfo conv_info)
51{
52 unsigned int output_width = 0;
53 unsigned int output_height = 0;
54 std::tie(output_width, output_height) = scaled_dimensions(input_shape.x(), input_shape.y(), weights_shape.x(), weights_shape.y(), conv_info);
55
56 TensorShape output_shape = input_shape;
57 output_shape.set(0, output_width);
58 output_shape.set(1, output_height);
59 output_shape.set(2, weights_shape[3]);
60
61 return output_shape;
62}
63} // namespace
64
SiCong Lic51b72f2017-07-28 14:46:20 +010065CLDirectConvolutionLayerKernel::CLDirectConvolutionLayerKernel()
steniu0127b386c2017-07-18 17:37:43 +010066 : _input(nullptr), _biases(nullptr), _weights(nullptr), _output(nullptr), _border_size(0), _conv_pad_x(0), _conv_pad_y(0), _conv_stride_x(0), _conv_stride_y(0)
67{
68}
69
SiCong Lic51b72f2017-07-28 14:46:20 +010070BorderSize CLDirectConvolutionLayerKernel::border_size() const
steniu0127b386c2017-07-18 17:37:43 +010071{
72 return _border_size;
73}
74
SiCong Lic51b72f2017-07-28 14:46:20 +010075void CLDirectConvolutionLayerKernel::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info)
steniu0127b386c2017-07-18 17:37:43 +010076{
Georgios Pinitas30902ed2017-11-14 15:32:57 +000077 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
steniu0127b386c2017-07-18 17:37:43 +010078
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010079 const unsigned int kernel_size = weights->info()->dimension(0);
Chunosovd621bca2017-11-03 17:33:15 +070080 const DataType data_type = input->info()->data_type();
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010081
82 // Get convolved dimensions
Georgios Pinitas30902ed2017-11-14 15:32:57 +000083 TensorShape output_shape = get_output_shape(input->info()->tensor_shape(), weights->info()->tensor_shape(), conv_info);
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010084
85 // Output auto inizialitation if not yet initialized
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000086 auto_init_if_empty(*output->info(),
87 output_shape,
88 1,
89 input->info()->data_type(),
90 input->info()->fixed_point_position(),
91 input->info()->quantization_info());
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010092
Georgios Pinitas30902ed2017-11-14 15:32:57 +000093 // Perform validation step
94 ARM_COMPUTE_ERROR_THROW_ON(CLDirectConvolutionLayerKernel::validate(input->info(),
95 weights->info(),
96 (biases != nullptr) ? biases->info() : nullptr,
97 output->info(),
98 conv_info));
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010099
steniu0127b386c2017-07-18 17:37:43 +0100100 _conv_stride_x = std::get<0>(conv_info.stride());
101 _conv_stride_y = std::get<1>(conv_info.stride());
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100102 _conv_pad_x = std::min(std::get<0>(conv_info.pad()), kernel_size / 2);
103 _conv_pad_y = std::min(std::get<1>(conv_info.pad()), kernel_size / 2);
steniu0127b386c2017-07-18 17:37:43 +0100104
105 _input = input;
106 _weights = weights;
107 _output = output;
108 _biases = biases;
109 _border_size = BorderSize(_conv_pad_y, _conv_pad_x);
110
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100111 const GPUTarget gpu_target = get_arch_from_target(get_target());
Michalis Spyroudef665a2017-08-14 11:26:37 +0100112
Chunosovd621bca2017-11-03 17:33:15 +0700113 std::stringstream kernel_name;
114 kernel_name << "direct_convolution" << kernel_size << "x" << kernel_size;
steniu0127b386c2017-07-18 17:37:43 +0100115
Chunosovd621bca2017-11-03 17:33:15 +0700116 CLBuildOptions build_options;
117 build_options.add_option_if(_biases != nullptr, std::string("-DHAS_BIAS"));
steniu0127b386c2017-07-18 17:37:43 +0100118
Chunosovd621bca2017-11-03 17:33:15 +0700119 if((gpu_target == GPUTarget::BIFROST) && (kernel_size <= 5) && (_conv_stride_x == 1) && (_conv_stride_y == 1) && (data_type == DataType::F32))
120 {
121 build_options.add_option(std::string("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(_weights->info()->dimension(2))));
122
123 kernel_name << "_f32_bifrost";
124 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name.str(), build_options.options()));
steniu0127b386c2017-07-18 17:37:43 +0100125
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100126 // Configure kernel window
127 Window win = calculate_max_window(*output->info());
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100128
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100129 unsigned int num_elems_read_per_iteration_x = 0;
130 unsigned int num_elems_read_per_iteration_y = 0;
131 unsigned int num_elems_written_per_iteration_x = 0;
132 unsigned int num_elems_written_per_iteration_y = 0;
steniu0127b386c2017-07-18 17:37:43 +0100133
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100134 switch(kernel_size)
135 {
Gian Marco Iodice1c8409d2017-09-06 17:24:25 +0100136 case 1:
137 {
138 num_elems_read_per_iteration_x = 4;
139 num_elems_read_per_iteration_y = 4;
140 num_elems_written_per_iteration_x = 4;
141 num_elems_written_per_iteration_y = 4;
142 break;
143 }
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100144 case 3:
145 {
146 num_elems_read_per_iteration_x = 6;
147 num_elems_read_per_iteration_y = 5;
148 num_elems_written_per_iteration_x = 4;
149 num_elems_written_per_iteration_y = 3;
150 break;
151 }
152 case 5:
153 {
154 num_elems_read_per_iteration_x = 8;
155 num_elems_read_per_iteration_y = 6;
156 num_elems_written_per_iteration_x = 4;
157 num_elems_written_per_iteration_y = 2;
158 break;
159 }
160 default:
161 {
162 ARM_COMPUTE_ERROR("Kernel size not optimized for Bifrost");
163 }
164 }
steniu0127b386c2017-07-18 17:37:43 +0100165
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100166 // Calculate right and bottom border
167 const int input_width = input->info()->dimension(0) - kernel_size / 2 + _conv_pad_x;
168 const int input_height = input->info()->dimension(1) - kernel_size / 2 + _conv_pad_y;
steniu0127b386c2017-07-18 17:37:43 +0100169
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100170 // Create window and update padding
171 win = calculate_max_window(*output->info(), Steps(num_elems_written_per_iteration_x, num_elems_written_per_iteration_y));
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100172
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100173 AccessWindowStatic input_access(input->info(), -_conv_pad_x, -_conv_pad_y, input_width + num_elems_read_per_iteration_x, input_height + num_elems_read_per_iteration_y);
174 AccessWindowStatic weights_access(weights->info(), 0, 0, kernel_size, kernel_size);
175 AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_y);
steniu0127b386c2017-07-18 17:37:43 +0100176
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100177 update_window_and_padding(win, input_access, weights_access, output_access);
steniu0127b386c2017-07-18 17:37:43 +0100178
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100179 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
180
181 ICLKernel::configure(win);
182 }
183 else
184 {
Chunosovd621bca2017-11-03 17:33:15 +0700185 bool is_quantized_fixed_point = is_data_type_fixed_point(data_type);
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000186 bool is_quantized_asymm = is_data_type_quantized_asymmetric(data_type);
Chunosovd621bca2017-11-03 17:33:15 +0700187 DataType promoted_type = (is_quantized_fixed_point) ? get_promoted_data_type(data_type) : data_type;
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100188
Chunosovd621bca2017-11-03 17:33:15 +0700189 build_options.add_option_if(is_quantized_asymm, std::string("-DKERNEL_SIZE=" + support::cpp11::to_string(kernel_size)));
190 build_options.add_option(std::string("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)));
191 build_options.add_option(std::string("-DDATA_SIZE=" + get_data_size_from_data_type(data_type)));
192 build_options.add_option(std::string("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(_weights->info()->dimension(2))));
193 build_options.add_option(std::string("-DSTRIDE_X=" + support::cpp11::to_string(_conv_stride_x)));
194 build_options.add_option_if(is_quantized_fixed_point,
195 std::string("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())));
196 build_options.add_option(std::string("-DDATA_TYPE_PROMOTED=" + get_cl_type_from_data_type(promoted_type)));
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100197
Chunosovd621bca2017-11-03 17:33:15 +0700198 // Create kernel
199 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(is_quantized_asymm ? "direct_convolution_1x1_3x3_5x5_quantized" : kernel_name.str(),
200 build_options.options()));
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100201
202 // Configure kernel window
203
204 bool is_stride2 = ((kernel_size != 1) && (_conv_stride_x == 2));
205
206 const unsigned int num_elems_read_per_iteration_x = 8 + 2 * (kernel_size / 2) + (is_stride2 ? 6 + kernel_size / 2 : 0);
207 const unsigned int num_elems_read_per_iteration_y = kernel_size;
208 const unsigned int num_elems_written_per_iteration_x = 8;
209 const unsigned int num_elems_written_per_iteration_y = 1;
210
211 // Calculate right and bottom border
212 const int input_width = input->info()->dimension(0) - kernel_size / 2 + _conv_pad_x;
213 const int input_height = input->info()->dimension(1) - kernel_size / 2 + _conv_pad_y;
214
215 // Create window and update padding
216 Window win = calculate_max_window(*output->info(), Steps(num_elems_written_per_iteration_x, num_elems_written_per_iteration_y));
217
218 AccessWindowStatic input_access(input->info(), -_conv_pad_x, -_conv_pad_y, input_width + num_elems_read_per_iteration_x, input_height + num_elems_read_per_iteration_y);
219 AccessWindowStatic weights_access(weights->info(), 0, 0, kernel_size, kernel_size);
220 AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_y);
221
222 update_window_and_padding(win, input_access, weights_access, output_access);
223
224 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
225
226 ICLKernel::configure(win);
227 }
Gian Marcode691f02017-09-08 16:13:11 +0100228
Chunosovd621bca2017-11-03 17:33:15 +0700229 // Set static kernel arguments
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000230 if(is_data_type_quantized_asymmetric(data_type))
Chunosovd621bca2017-11-03 17:33:15 +0700231 {
232 int output_multiplier = 0;
233 int output_shift = 0;
234
235 float multiplier = _input->info()->quantization_info().scale * _weights->info()->quantization_info().scale / _output->info()->quantization_info().scale;
236 ARM_COMPUTE_THROW_ON_ERROR(quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift));
237
238 unsigned int idx = 3 * num_arguments_per_3D_tensor() + ((_biases != nullptr) ? num_arguments_per_1D_tensor() : 0) + 1;
239 _kernel.setArg(idx++, -_input->info()->quantization_info().offset);
240 _kernel.setArg(idx++, -_weights->info()->quantization_info().offset);
241 _kernel.setArg(idx++, _output->info()->quantization_info().offset);
242 _kernel.setArg(idx++, output_multiplier);
243 _kernel.setArg(idx++, output_shift);
244 }
245
Gian Marcode691f02017-09-08 16:13:11 +0100246 // Set config_id for enabling LWS tuning
247 _config_id = "direct_convolution_";
Chunosovd621bca2017-11-03 17:33:15 +0700248 _config_id += lower_string(string_from_data_type(data_type));
Gian Marcode691f02017-09-08 16:13:11 +0100249 _config_id += "_";
250 _config_id += support::cpp11::to_string(kernel_size);
251 _config_id += "_";
252 _config_id += support::cpp11::to_string(_conv_pad_x);
253 _config_id += "_";
254 _config_id += support::cpp11::to_string(_conv_pad_y);
255 _config_id += "_";
256 _config_id += support::cpp11::to_string(_conv_stride_x);
257 _config_id += "_";
258 _config_id += support::cpp11::to_string(_conv_stride_y);
259 _config_id += "_";
260 _config_id += support::cpp11::to_string(output->info()->dimension(0));
261 _config_id += "_";
262 _config_id += support::cpp11::to_string(output->info()->dimension(1));
steniu0127b386c2017-07-18 17:37:43 +0100263}
264
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000265Error CLDirectConvolutionLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info)
266{
267 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
268 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
269 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(0) != weights->dimension(1),
270 "Weights should have same width as length");
271 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(0) != 1 && weights->dimension(0) != 3 && weights->dimension(0) != 5,
272 "Kernel sizes other than 1x1, 3x3 or 5x5 are not supported");
273 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(2) != input->dimension(2),
274 "Weights feature map dimension should match the respective input's one");
275 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(0) != weights->dimension(1),
276 "Only rectangular weights are supported!");
277 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->num_dimensions() > 4,
278 "Weights can be at most 4 dimensional");
279 ARM_COMPUTE_RETURN_ERROR_ON_MSG((weights->dimension(0) == 1) && std::get<0>(conv_info.stride()) > 3,
280 "Strides larger than 3 not supported for 1x1 convolution.");
281 ARM_COMPUTE_RETURN_ERROR_ON_MSG((weights->dimension(0) == 3 || weights->dimension(0) == 5) && std::get<0>(conv_info.stride()) > 2,
282 "Strides larger than 2 not supported for 3x3 convolution.");
283
284 if(biases != nullptr)
285 {
286 if(is_data_type_quantized_asymmetric(input->data_type()))
287 {
288 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
289 }
290 else
291 {
292 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
293 }
294 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->dimension(0) != weights->dimension(3),
295 "Biases size and number of input feature maps should match");
296 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->num_dimensions() > 1,
297 "Biases should be one dimensional");
298 }
299
300 // Checks performed when output is configured
301 if(output->total_size() != 0)
302 {
303 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(),
304 get_output_shape(input->tensor_shape(), weights->tensor_shape(), conv_info));
305 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
306 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
307 }
308
309 return Error{};
310}
311
SiCong Lic51b72f2017-07-28 14:46:20 +0100312void CLDirectConvolutionLayerKernel::run(const Window &window, cl::CommandQueue &queue)
steniu0127b386c2017-07-18 17:37:43 +0100313{
314 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
315 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
316
317 // Get initial windows
318 Window slice = window.first_slice_window_3D();
319 Window win_in = window;
320
321 win_in.adjust(Window::DimX, -_conv_pad_x, true);
322 win_in.adjust(Window::DimY, -_conv_pad_y, true);
323 win_in.set_dimension_step(Window::DimX, window.x().step() * _conv_stride_x);
324 win_in.set_dimension_step(Window::DimY, window.y().step() * _conv_stride_y);
325
326 Window slice_in = win_in.first_slice_window_3D();
327
328 unsigned int idx1 = 2 * num_arguments_per_3D_tensor();
329 add_3D_tensor_argument(idx1, _weights, slice);
330
331 if(_biases != nullptr)
332 {
333 Window slice_biases;
SiCong Li86b53332017-08-23 11:02:43 +0100334 slice_biases.use_tensor_dimensions(_biases->info()->tensor_shape());
steniu0127b386c2017-07-18 17:37:43 +0100335 add_1D_tensor_argument(idx1, _biases, slice_biases);
336 }
337
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100338 _kernel.setArg(idx1++, static_cast<unsigned int>(_weights->info()->strides_in_bytes()[3]));
339
steniu0127b386c2017-07-18 17:37:43 +0100340 do
341 {
342 unsigned int idx = 0;
343 add_3D_tensor_argument(idx, _input, slice_in);
344 add_3D_tensor_argument(idx, _output, slice);
345
346 enqueue(queue, *this, slice, _lws_hint);
347 }
348 while(window.slide_window_slice_3D(slice) && win_in.slide_window_slice_3D(slice_in));
349}