blob: 28b5bd2d6208b77d78cbaa2e99d2553865eda7e7 [file] [log] [blame]
Frank Lei8cdfdb82018-01-02 16:49:33 +08001/*
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/GLES_COMPUTE/kernels/GCDepthwiseConvolutionLayer3x3Kernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h"
29#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h"
30#include "arm_compute/core/GLES_COMPUTE/IGCKernel.h"
31#include "arm_compute/core/GLES_COMPUTE/IGCTensor.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"
36
37using namespace arm_compute;
38
39namespace
40{
41/** Calculates expected output shape dimension
42 *
43 * @param[in] Input shape
44 *
45 * @return Expected output shape
46 */
47TensorShape get_output_shape(TensorShape input_shape, TensorShape weights_shape, PadStrideInfo conv_info)
48{
49 unsigned int output_width = 0;
50 unsigned int output_height = 0;
51
52 std::tie(output_width, output_height) = scaled_dimensions(input_shape.x(), input_shape.y(), weights_shape.x(), weights_shape.y(), conv_info);
53
54 TensorShape output_shape = input_shape;
55 output_shape.set(0, output_width);
56 output_shape.set(1, output_height);
57
58 return output_shape;
59}
60} // namespace
61
62GCDepthwiseConvolutionLayer3x3Kernel::GCDepthwiseConvolutionLayer3x3Kernel()
63 : _border_size(0), _input(), _output(), _weights(), _biases(), _conv_stride_x(0), _conv_stride_y(0), _conv_pad_left(0), _conv_pad_top(0), _lws(gles::NDRange(1U, 1U, 1U))
64{
65}
66
67BorderSize GCDepthwiseConvolutionLayer3x3Kernel::border_size() const
68{
69 return _border_size;
70}
71
72void GCDepthwiseConvolutionLayer3x3Kernel::configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output, const PadStrideInfo &conv_info)
73{
74 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16);
75 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
76 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(0) != 3 || weights->info()->dimension(1) != 3);
77
78 if(biases != nullptr)
79 {
80 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
81 ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(2));
82 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
83 }
84
85 // Get convolved dimensions
86 TensorShape output_shape = get_output_shape(input->info()->tensor_shape(), weights->info()->tensor_shape(), conv_info);
87
88 // Output auto inizialitation if not yet initialized
89 auto_init_if_empty(*output->info(),
90 output_shape,
91 1,
92 input->info()->data_type(),
93 input->info()->fixed_point_position());
94
95 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
96
97 _input = input;
98 _output = output;
99 _weights = weights;
100 _biases = biases;
101 _conv_stride_x = conv_info.stride().first;
102 _conv_stride_y = conv_info.stride().second;
103 _conv_pad_left = conv_info.pad_left();
104 _conv_pad_top = conv_info.pad_top();
105 _border_size = BorderSize(_conv_pad_top, conv_info.pad_right(), conv_info.pad_bottom(), _conv_pad_left);
106
107 // Set build options
108 ARM_COMPUTE_ERROR_ON(_conv_stride_x < 1 || _conv_stride_x > 3);
109 std::set<std::string> options;
110
111 options.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(_lws[0]));
112 options.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(_lws[1]));
113 options.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(_lws[2]));
114 options.emplace("#define STRIDE_X " + support::cpp11::to_string(_conv_stride_x));
115 options.emplace("#define STRIDE_Y " + support::cpp11::to_string(_conv_stride_y));
116
117 std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
118 options.emplace(("#define " + dt_name));
119
120 unsigned int num_elems_read_per_iteration_x = 8;
121 unsigned int num_elems_read_per_iteration_y = 1;
122 unsigned int num_elems_written_per_iteration_x = 4;
123 unsigned int num_elems_written_per_iteration_y = 1;
124 unsigned int num_elems_written_per_iteration_z = 1;
125
126 if((_conv_stride_x == 1) && (_conv_stride_y == 1))
127 {
128 switch(input->info()->data_type())
129 {
130#define PROCESS_4X_3Y_1Z
131
132 case DataType::F16:
133#if defined(PROCESS_4X_3Y_1Z)
134 options.emplace("#define PROCESS_4X_3Y_1Z");
135 num_elems_read_per_iteration_y = 5;
136 num_elems_written_per_iteration_y = 3;
137#endif /* PROCESS_4X_3Y_1Z */
138#undef PROCESS_4X_3Y_1Z
139 break;
140
141 default:
142 ARM_COMPUTE_ERROR("Current data type is not supported");
143 break;
144 }
145 }
146 else
147 {
148 switch(input->info()->data_type())
149 {
150 case DataType::F16:
151 options.emplace("#define PROCESS_4X_1Y_1Z");
152 break;
153
154 default:
155 ARM_COMPUTE_ERROR("Current data type is not supported");
156 break;
157 }
158 }
159
160 if(_biases != nullptr)
161 {
162 options.emplace("#define BIAS");
163 }
164
165 // Create kernel
166 std::string kernel_name = "depthwise_convolution_3x3";
167 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel(kernel_name, options));
168
169 // Calculate output right and bottom border
170 const int output_width = output->info()->dimension(0);
171 const int output_height = output->info()->dimension(1);
172 const int output_padding_right = ceil_to_multiple(output_width, num_elems_written_per_iteration_x * _lws[0]) - output_width;
173 const int output_padding_bottom = ceil_to_multiple(output_height, num_elems_written_per_iteration_y * _lws[1]) - output_height;
174
175 // Calculate input right and bottom border
176 const int input_width = input->info()->dimension(0);
177 const int input_height = input->info()->dimension(1);
178 const int padding_right = ceil_to_multiple(((output_width + output_padding_right) * _conv_stride_x + 2), num_elems_read_per_iteration_x * _lws[0]) - _conv_pad_left - input_width;
179 const int padding_bottom = ceil_to_multiple(((output_height + output_padding_bottom) * _conv_stride_y + 2), num_elems_read_per_iteration_y * _lws[1]) - _conv_pad_top - input_height;
180
181 BorderSize border = BorderSize(0, output_padding_right, output_padding_bottom, 0);
182
183 Window win = calculate_max_enlarged_window(*output->info(), Steps(num_elems_written_per_iteration_x, num_elems_written_per_iteration_y, num_elems_written_per_iteration_z), border);
184
185 AccessWindowStatic input_access(input->info(), -_conv_pad_left, -_conv_pad_top, input_width + padding_right, input_height + padding_bottom);
186 AccessWindowStatic weights_access = AccessWindowStatic(nullptr, 0, 0, 0, 0);
187 AccessWindowStatic bias_access = AccessWindowStatic(nullptr, 0, 0, 0, 1);
188
189 switch(weights->info()->data_type())
190 {
191 case DataType::F16:
192 weights_access = AccessWindowStatic(weights->info(), 0, 0, 4, 3);
193 if(_biases != nullptr)
194 {
195 bias_access = AccessWindowStatic(_biases->info(), 0, 0, _biases->info()->dimension(0) + 1, 1);
196 }
197 break;
198
199 default:
200 ARM_COMPUTE_ERROR("Current data type is not supported");
201 break;
202 }
203
204 AccessWindowStatic output_access(output->info(), 0, 0, output_width + output_padding_right, output_height + output_padding_bottom);
205
206 if(_biases != nullptr)
207 {
208 update_window_and_padding(win, input_access, weights_access, bias_access, output_access);
209 }
210 else
211 {
212 update_window_and_padding(win, input_access, weights_access, output_access);
213 }
214
215 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
216
217 IGCKernel::configure(win);
218}
219
220void GCDepthwiseConvolutionLayer3x3Kernel::run(const Window &window)
221{
222 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
223 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
224
225 _kernel.use();
226
227 // Create input window and adjust
228 Window win_in = window;
229 win_in.adjust(Window::DimX, -_conv_pad_left, true);
230 win_in.adjust(Window::DimY, -_conv_pad_top, true);
231 win_in.set_dimension_step(Window::DimX, window.x().step() * _conv_stride_x);
232 win_in.set_dimension_step(Window::DimY, window.y().step() * _conv_stride_y);
233
234 Window slice_in = win_in.first_slice_window_3D();
235 Window slice_out = window.first_slice_window_3D();
236 Window slice_weights = window.first_slice_window_3D();
237 slice_weights.set_dimension_step(Window::DimX, 0);
238 slice_weights.set_dimension_step(Window::DimY, 0);
239
240 // Set biases
241 if(_biases != nullptr)
242 {
243 unsigned int idx = 3 * num_arguments_per_3D_tensor();
244 Window slice_biases;
245 slice_biases.use_tensor_dimensions(_biases->info()->tensor_shape());
246 add_1D_tensor_argument(idx, _biases, 4, slice_biases);
247 }
248
249 do
250 {
251 unsigned int idx = 0;
252 add_3D_tensor_argument(idx, _input, 1, slice_in);
253 add_3D_tensor_argument(idx, _output, 2, slice_out);
254 add_3D_tensor_argument(idx, _weights, 3, slice_weights);
255
256 _kernel.update_shader_params();
257 enqueue(*this, slice_out, _lws);
258 }
259 while(window.slide_window_slice_3D(slice_out) && win_in.slide_window_slice_3D(slice_in));
260}