blob: 3613419273c9d1e14655444328289e08be4ad427 [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +01001/*
Giorgio Arena944d3f72018-01-16 15:38:35 +00002 * Copyright (c) 2017-2018 ARM Limited.
Giorgio Arena93a690e2017-08-01 16:09:33 +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 */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000024#include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3Kernel.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010025
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/ICLKernel.h"
30#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/TensorInfo.h"
34#include "arm_compute/core/Types.h"
35#include "arm_compute/core/Utils.h"
Georgios Pinitas1250a5a2018-01-02 13:27:37 +000036#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Dmitry Savenkod7295b72017-11-20 22:00:08 +070037#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010038
39using namespace arm_compute;
Georgios Pinitas1250a5a2018-01-02 13:27:37 +000040using namespace arm_compute::misc::shape_calculator;
Georgios Pinitas236bfe72017-11-23 15:59:55 +000041
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000042CLDepthwiseConvolutionLayer3x3Kernel::CLDepthwiseConvolutionLayer3x3Kernel()
Jaroslaw Rzepecki16cdf892017-10-27 13:15:03 +010043 : _border_size(0), _input(), _output(), _weights(), _biases(), _conv_stride_x(0), _conv_stride_y(0), _conv_pad_left(0), _conv_pad_top(0)
Giorgio Arena93a690e2017-08-01 16:09:33 +010044{
45}
46
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000047BorderSize CLDepthwiseConvolutionLayer3x3Kernel::border_size() const
Giorgio Arena93a690e2017-08-01 16:09:33 +010048{
49 return _border_size;
50}
51
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000052void CLDepthwiseConvolutionLayer3x3Kernel::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info)
Giorgio Arena93a690e2017-08-01 16:09:33 +010053{
Dmitry Savenkod7295b72017-11-20 22:00:08 +070054 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32);
Georgios Pinitas236bfe72017-11-23 15:59:55 +000055 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Giorgio Arena93a690e2017-08-01 16:09:33 +010056 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(0) != 3 || weights->info()->dimension(1) != 3);
57
Giorgio Arena287b5702018-02-16 11:01:04 +000058 bool is_qasymm = is_data_type_quantized_asymmetric(input->info()->data_type());
59
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010060 if(biases != nullptr)
61 {
Giorgio Arena287b5702018-02-16 11:01:04 +000062 if(is_qasymm)
Dmitry Savenkod7295b72017-11-20 22:00:08 +070063 {
64 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
65 }
66 else
67 {
68 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
69 }
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010070 ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(2));
71 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
72 }
73
Georgios Pinitas236bfe72017-11-23 15:59:55 +000074 // Get convolved dimensions
Georgios Pinitas1250a5a2018-01-02 13:27:37 +000075 const TensorShape output_shape = compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info);
Georgios Pinitasc26ecf82017-09-22 13:44:05 +010076
Georgios Pinitas236bfe72017-11-23 15:59:55 +000077 // Output auto inizialitation if not yet initialized
78 auto_init_if_empty(*output->info(),
79 output_shape,
80 1,
81 input->info()->data_type(),
82 input->info()->fixed_point_position(),
83 input->info()->quantization_info());
84
85 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
Georgios Pinitasc26ecf82017-09-22 13:44:05 +010086
Giorgio Arena93a690e2017-08-01 16:09:33 +010087 _input = input;
88 _output = output;
89 _weights = weights;
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010090 _biases = biases;
Giorgio Arena93a690e2017-08-01 16:09:33 +010091 _conv_stride_x = conv_info.stride().first;
92 _conv_stride_y = conv_info.stride().second;
Jaroslaw Rzepecki16cdf892017-10-27 13:15:03 +010093 _conv_pad_left = conv_info.pad_left();
94 _conv_pad_top = conv_info.pad_top();
95 _border_size = BorderSize(_conv_pad_top, conv_info.pad_right(), conv_info.pad_bottom(), _conv_pad_left);
Giorgio Arena93a690e2017-08-01 16:09:33 +010096
97 // Set build options
Georgios Pinitasc26ecf82017-09-22 13:44:05 +010098 ARM_COMPUTE_ERROR_ON(_conv_stride_x < 1 || _conv_stride_x > 3);
Dmitry Savenkod7295b72017-11-20 22:00:08 +070099 CLBuildOptions build_opts;
100 build_opts.add_option("-DCONV_STRIDE_X=" + support::cpp11::to_string(_conv_stride_x));
101 build_opts.add_option_if(_biases != nullptr, "-DHAS_BIAS");
Giorgio Arena93a690e2017-08-01 16:09:33 +0100102
Giorgio Arena287b5702018-02-16 11:01:04 +0000103 if(is_qasymm)
104 {
105 float multiplier = _input->info()->quantization_info().scale * _weights->info()->quantization_info().scale / _output->info()->quantization_info().scale;
106 int output_multiplier = 0;
107 int output_shift = 0;
108 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
109
110 build_opts.add_option("-DCONV_STRIDE_Y=" + support::cpp11::to_string(_conv_stride_y));
111 build_opts.add_option("-DINPUT_OFFSET=" + support::cpp11::to_string(-_input->info()->quantization_info().offset));
112 build_opts.add_option("-DWEIGHTS_OFFSET=" + support::cpp11::to_string(-_weights->info()->quantization_info().offset));
113 build_opts.add_option("-DOUTPUT_OFFSET=" + support::cpp11::to_string(_output->info()->quantization_info().offset));
114 build_opts.add_option("-DK_OFFSET=" + support::cpp11::to_string(9 * input->info()->quantization_info().offset * weights->info()->quantization_info().offset));
115 build_opts.add_option("-DOUTPUT_MULTIPLIER=" + support::cpp11::to_string(output_multiplier));
116 build_opts.add_option("-DOUTPUT_SHIFT=" + support::cpp11::to_string(output_shift));
117 }
118
Gian Marcoc799ed82018-02-01 16:57:48 +0000119 // Configure the local work size for Bifrost with a value obtained
120 // via exhaustive autotuning for the MobileNets tensor shapes.
121 const GPUTarget gpu_target = get_arch_from_target(get_target());
122
123 // Configure kernel window
124 const unsigned int conv_pad_left = std::max(conv_info.pad_left(), 1U);
125 const unsigned int conv_pad_top = std::max(conv_info.pad_top(), 1U);
126 const unsigned int conv_pad_right = std::max(conv_info.pad_right(), 1U);
127 const unsigned int conv_pad_bottom = std::max(conv_info.pad_bottom(), 1U);
128
129 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;
133
Anthony Barbiera2ea7532017-11-28 10:33:22 +0000134 // Create kernel
Gian Marcoc799ed82018-02-01 16:57:48 +0000135 std::string kernel_name;
136
137 if(input->info()->data_type() == DataType::F32 && gpu_target == GPUTarget::BIFROST)
138 {
139 if(_conv_stride_x == 1 && _conv_stride_y == 1)
140 {
141 kernel_name = "depthwise_convolution_3x3_stridex1_stridey1_bifrost";
142 num_elems_read_per_iteration_x = 4;
143 num_elems_read_per_iteration_y = 6;
144 num_elems_written_per_iteration_x = 2;
145 num_elems_written_per_iteration_y = 4;
146 }
147 else if(_conv_stride_x == 2 && _conv_stride_y == 2)
148 {
149 kernel_name = "depthwise_convolution_3x3_stridex2_stridey2_bifrost";
150 num_elems_read_per_iteration_x = 6;
151 num_elems_read_per_iteration_y = 5;
152 num_elems_written_per_iteration_x = 2;
153 num_elems_written_per_iteration_y = 2;
154 }
155 else
156 {
157 kernel_name = "depthwise_convolution_3x3";
158 num_elems_written_per_iteration_x = 8 / data_size_from_type(input->info()->data_type());
159 num_elems_written_per_iteration_y = 1;
160 num_elems_read_per_iteration_x = 3 + (num_elems_written_per_iteration_x - 1) * _conv_stride_x;
161 num_elems_read_per_iteration_y = 3;
162 }
163 }
164 else
165 {
Giorgio Arena287b5702018-02-16 11:01:04 +0000166 kernel_name = is_qasymm ? "depthwise_convolution_3x3_quantized" : "depthwise_convolution_3x3";
Gian Marcoc799ed82018-02-01 16:57:48 +0000167 num_elems_written_per_iteration_x = 8 / data_size_from_type(input->info()->data_type());
Giorgio Arena287b5702018-02-16 11:01:04 +0000168 num_elems_written_per_iteration_y = (is_qasymm && _conv_stride_y < 3) ? (2 / _conv_stride_y) : 1;
Gian Marcoc799ed82018-02-01 16:57:48 +0000169 num_elems_read_per_iteration_x = 3 + (num_elems_written_per_iteration_x - 1) * _conv_stride_x;
Giorgio Arena287b5702018-02-16 11:01:04 +0000170 num_elems_read_per_iteration_y = num_elems_written_per_iteration_y + 2;
Gian Marcoc799ed82018-02-01 16:57:48 +0000171 }
172
173 // Calculate right and bottom border
174 int input_width = input->info()->dimension(0) + conv_pad_left + conv_pad_right;
175 int input_height = input->info()->dimension(1) + conv_pad_top + conv_pad_bottom;
176
177 // Add padding only if necessary or it would always result in a window_changed
178 input_width = ceil_to_multiple(input_width, num_elems_read_per_iteration_x);
179 input_height = ceil_to_multiple(input_height, num_elems_read_per_iteration_y);
180
181 // Create window and update padding
182 Window win = calculate_max_window(*output->info(), Steps(num_elems_written_per_iteration_x, num_elems_written_per_iteration_y));
183
184 AccessWindowStatic input_access(input->info(), -conv_pad_left, -conv_pad_top, input_width, input_height);
185 AccessWindowStatic weights_access(weights->info(), 0, 0, 3, 3);
186 AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_y);
187
188 update_window_and_padding(win, input_access, weights_access, output_access);
189
190 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
191
192 ICLKernel::configure(win);
193
194 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Giorgio Arena93a690e2017-08-01 16:09:33 +0100195
Gian Marco85e6f512018-02-01 16:57:48 +0000196 // Set config_id for enabling LWS tuning
Gian Marcoc799ed82018-02-01 16:57:48 +0000197 _config_id = kernel_name;
198 _config_id += "_";
Gian Marco85e6f512018-02-01 16:57:48 +0000199 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
200 _config_id += "_";
201 _config_id += support::cpp11::to_string(input->info()->dimension(0));
202 _config_id += "_";
203 _config_id += support::cpp11::to_string(input->info()->dimension(1));
204 _config_id += "_";
205 _config_id += support::cpp11::to_string(input->info()->dimension(2));
206 _config_id += "_";
207 _config_id += support::cpp11::to_string(output->info()->dimension(0));
208 _config_id += "_";
209 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Giorgio Arena93a690e2017-08-01 16:09:33 +0100210}
211
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000212void CLDepthwiseConvolutionLayer3x3Kernel::run(const Window &window, cl::CommandQueue &queue)
Giorgio Arena93a690e2017-08-01 16:09:33 +0100213{
214 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
215 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
216
Georgios Pinitasb6f182d32017-11-29 10:17:56 +0000217 // Create input window and adjust
218 Window win_in = window;
219 win_in.adjust(Window::DimX, -_conv_pad_left, true);
220 win_in.adjust(Window::DimY, -_conv_pad_top, true);
221 win_in.set_dimension_step(Window::DimX, window.x().step() * _conv_stride_x);
222 win_in.set_dimension_step(Window::DimY, window.y().step() * _conv_stride_y);
223
224 Window slice_in = win_in.first_slice_window_3D();
Giorgio Arena93a690e2017-08-01 16:09:33 +0100225 Window slice_out = window.first_slice_window_3D();
226 Window slice_weights = window.first_slice_window_3D();
Giorgio Arena93a690e2017-08-01 16:09:33 +0100227 slice_weights.set_dimension_step(Window::DimX, 0);
228 slice_weights.set_dimension_step(Window::DimY, 0);
229
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100230 // Set biases
231 if(_biases != nullptr)
232 {
233 unsigned int idx = 3 * num_arguments_per_3D_tensor();
234 Window slice_biases;
235 slice_biases.use_tensor_dimensions(_biases->info()->tensor_shape());
236 add_1D_tensor_argument(idx, _biases, slice_biases);
237 }
238
Giorgio Arena93a690e2017-08-01 16:09:33 +0100239 do
240 {
241 unsigned int idx = 0;
242 add_3D_tensor_argument(idx, _input, slice_in);
243 add_3D_tensor_argument(idx, _output, slice_out);
244 add_3D_tensor_argument(idx, _weights, slice_weights);
245
Anthony Barbiera2ea7532017-11-28 10:33:22 +0000246 enqueue(queue, *this, slice_out, _lws_hint);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100247 }
Georgios Pinitasb6f182d32017-11-29 10:17:56 +0000248 while(window.slide_window_slice_3D(slice_out) && win_in.slide_window_slice_3D(slice_in));
Giorgio Arena9fe41442017-08-23 16:36:24 +0100249}