blob: 497cdae85c453ca72ec10d998abf63293bb150e4 [file] [log] [blame]
Anthony Barbier8140e1e2017-12-14 23:48:46 +00001/*
Anthony Barbier06ea0482018-02-22 15:45:35 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier8140e1e2017-12-14 23:48:46 +00003 *
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/runtime/CL/functions/CLDepthwiseConvolutionLayer.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
Jenkinsb3a371b2018-05-23 11:36:53 +010027#include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NCHWKernel.h"
28#include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NHWCKernel.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000029#include "arm_compute/core/PixelValue.h"
Anthony Barbier06ea0482018-02-22 15:45:35 +000030#include "arm_compute/core/utils/misc/ShapeCalculator.h"
31#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000032#include "arm_compute/runtime/CL/CLScheduler.h"
33#include "support/ToolchainSupport.h"
34
35using namespace arm_compute;
Anthony Barbier06ea0482018-02-22 15:45:35 +000036using namespace arm_compute::misc;
37using namespace arm_compute::misc::shape_calculator;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000038
39CLDepthwiseConvolutionLayer3x3::CLDepthwiseConvolutionLayer3x3()
Jenkinsb3a371b2018-05-23 11:36:53 +010040 : _kernel(nullptr), _border_handler()
Anthony Barbier8140e1e2017-12-14 23:48:46 +000041{
42}
43
Jenkinsb3a371b2018-05-23 11:36:53 +010044void CLDepthwiseConvolutionLayer3x3::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier,
45 ActivationLayerInfo act_info)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000046{
Anthony Barbier06ea0482018-02-22 15:45:35 +000047 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000048 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
49
Jenkinsb3a371b2018-05-23 11:36:53 +010050 if(input->info()->data_layout() == DataLayout::NCHW)
51 {
52 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>();
53 }
54 else
55 {
56 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NHWCKernel>();
57 }
58
59 _kernel->set_target(CLScheduler::get().target());
60 _kernel->configure(input, weights, biases, output, conv_info, depth_multiplier, act_info);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000061
62 // Configure border handler
63 PixelValue &&zero_value(0.f);
64 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
65 {
66 zero_value = PixelValue(static_cast<uint8_t>(input->info()->quantization_info().offset));
67 }
Jenkinsb3a371b2018-05-23 11:36:53 +010068 _border_handler.configure(input, _kernel->border_size(), BorderMode::CONSTANT, zero_value);
69}
70
71Status CLDepthwiseConvolutionLayer3x3::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
72 unsigned int depth_multiplier,
73 ActivationLayerInfo act_info, GPUTarget gpu_target)
74{
75 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Jenkins52ba29e2018-08-29 15:32:11 +000076 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Jenkinsb3a371b2018-05-23 11:36:53 +010077
78 if(input->data_layout() == DataLayout::NCHW)
79 {
80 return CLDepthwiseConvolutionLayer3x3NCHWKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target);
81 }
82
83 return CLDepthwiseConvolutionLayer3x3NHWCKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000084}
85
86void CLDepthwiseConvolutionLayer3x3::run()
87{
88 CLScheduler::get().enqueue(_border_handler);
Jenkinsb3a371b2018-05-23 11:36:53 +010089 CLScheduler::get().enqueue(*_kernel);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000090}
91
92CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayer()
Jenkinsb9abeae2018-11-22 11:58:08 +000093 : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _activationlayer_function(), _v2mm_input_fill_border(), _v2mm_weights_fill_border(),
94 _input_reshaped(), _weights_reshaped(), _v2mm_output(), _output_reshaped(), _is_prepared(false), _is_quantized(false), _is_activationlayer_enabled(false), _original_weights(nullptr)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000095{
96}
97
Jenkinsb9abeae2018-11-22 11:58:08 +000098void CLDepthwiseConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
99 unsigned int depth_multiplier, const ActivationLayerInfo &act_info)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000100{
Jenkinsb3a371b2018-05-23 11:36:53 +0100101 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000102 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Jenkins52ba29e2018-08-29 15:32:11 +0000103 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000104
Jenkins52ba29e2018-08-29 15:32:11 +0000105 const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
106 const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
107 const size_t idx_c = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000108
Jenkins52ba29e2018-08-29 15:32:11 +0000109 const size_t weights_w = weights->info()->dimension(idx_w);
110 const size_t weights_h = weights->info()->dimension(idx_h);
111 const size_t weights_z = weights->info()->dimension(idx_c);
112
113 _is_prepared = false;
Jenkinsb3a371b2018-05-23 11:36:53 +0100114 _original_weights = weights;
115 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000116
Anthony Barbier06ea0482018-02-22 15:45:35 +0000117 bool append_bias = (biases != nullptr) && !_is_quantized;
118 const GPUTarget gpu_target = CLScheduler::get().target();
119
120 // Calculate output shape
Jenkinsb3a371b2018-05-23 11:36:53 +0100121 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier);
122
123 // Output auto inizialitation if not yet initialized
124 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
125 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000126
127 // Output width and height
Jenkins52ba29e2018-08-29 15:32:11 +0000128 const unsigned int conv_w = output_shape[idx_w];
129 const unsigned int conv_h = output_shape[idx_h];
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000130
131 // Set up intermediate tensors
Anthony Barbier06ea0482018-02-22 15:45:35 +0000132 const size_t patch_size = weights_w * weights_h + ((append_bias) ? 1 : 0);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000133 const size_t conv_size = conv_w * conv_h;
134
135 // Im2Col configuration
136 TensorShape shape_im2col = input->info()->tensor_shape();
137 shape_im2col.set(0, patch_size);
138 shape_im2col.set(1, conv_size);
139 shape_im2col.set(2, weights_z);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000140 _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000141 _im2col_kernel.set_target(gpu_target);
Jenkinsb3a371b2018-05-23 11:36:53 +0100142 _im2col_kernel.configure(input, &_input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier);
Jenkins52ba29e2018-08-29 15:32:11 +0000143 CLScheduler::get().tune_kernel_static(_im2col_kernel);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000144
145 // Weights reshape configuration
146 const TensorShape shape_weights_reshape(patch_size, weights_z);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000147 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape));
148 _weights_reshape_kernel.configure(weights, &_weights_reshaped, append_bias ? biases : nullptr);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000149
150 // GEMV configuration
Anthony Barbier06ea0482018-02-22 15:45:35 +0000151 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000152 TensorShape shape_v2mm_out = input->info()->tensor_shape();
153 shape_v2mm_out.set(0, conv_size * weights_z);
154 shape_v2mm_out.set(1, 1);
155 shape_v2mm_out.set(2, 1);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000156 _v2mm_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000157 _v2mm_kernel.set_target(gpu_target);
158 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
Jenkins52ba29e2018-08-29 15:32:11 +0000159 CLScheduler::get().tune_kernel_static(_v2mm_kernel);
Jenkinsb3a371b2018-05-23 11:36:53 +0100160 _output_reshaped.allocator()->init(_v2mm_output.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000161 _vector_to_tensor_kernel.configure(&_v2mm_output, (_is_quantized) ? &_output_reshaped : output, conv_w, conv_h);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000162
Anthony Barbier06ea0482018-02-22 15:45:35 +0000163 // Output staged configuration
164 if(_is_quantized)
165 {
166 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
167
168 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
169 int output_multiplier, output_shift;
170 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
171 _output_stage_kernel.configure(&_output_reshaped, biases, output, output_multiplier, output_shift, output_quant_info.offset);
172 _output_reshaped.allocator()->allocate();
173 }
174
175 // Fill borders on inputs
176 PixelValue zero_in(static_cast<int32_t>(0));
177 PixelValue zero_w(static_cast<int32_t>(0));
178 if(_is_quantized)
179 {
180 zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset));
181 zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset));
182 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000183 BorderSize border_size = _v2mm_kernel.border_size();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000184 _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000185
186 border_size.bottom = 0;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000187 _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000188
189 // Allocate intermediate tensors
190 _input_reshaped.allocator()->allocate();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000191 _v2mm_output.allocator()->allocate();
Jenkinsb9abeae2018-11-22 11:58:08 +0000192
193 //Configure Activation Layer
194 _is_activationlayer_enabled = act_info.enabled();
195
196 if(_is_activationlayer_enabled)
197 {
198 _activationlayer_function.configure(output, nullptr, act_info);
199 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000200}
201
Jenkinsb3a371b2018-05-23 11:36:53 +0100202Status CLDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Jenkinsb9abeae2018-11-22 11:58:08 +0000203 unsigned int depth_multiplier, const ActivationLayerInfo &act_info)
Jenkinsb3a371b2018-05-23 11:36:53 +0100204{
Jenkins52ba29e2018-08-29 15:32:11 +0000205 const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
206 const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
207 const size_t idx_c = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
208
Jenkinsb3a371b2018-05-23 11:36:53 +0100209 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Jenkins52ba29e2018-08-29 15:32:11 +0000210 ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(idx_c) * depth_multiplier) != weights->dimension(idx_c));
Jenkinsb3a371b2018-05-23 11:36:53 +0100211
212 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
213 const bool append_bias = (biases != nullptr) && !is_quantized;
214 const TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier);
Jenkins52ba29e2018-08-29 15:32:11 +0000215 const size_t weights_w = weights->dimension(idx_w);
216 const size_t weights_h = weights->dimension(idx_h);
217 const size_t weights_z = weights->dimension(idx_c);
218 const unsigned int conv_w = output_shape[idx_w];
219 const unsigned int conv_h = output_shape[idx_h];
Jenkinsb3a371b2018-05-23 11:36:53 +0100220 const size_t patch_size = weights_w * weights_h + ((append_bias) ? 1 : 0);
221 const size_t conv_size = conv_w * conv_h;
222
223 TensorShape shape_im2col = input->tensor_shape();
224 shape_im2col.set(0, patch_size);
225 shape_im2col.set(1, conv_size);
226 shape_im2col.set(2, weights_z);
227 TensorInfo input_reshaped(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
228 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseIm2ColKernel::validate(input, &input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier));
229
230 const TensorShape shape_weights_reshape(patch_size, weights_z);
231 TensorInfo weights_reshaped(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape));
232 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseWeightsReshapeKernel::validate(weights, &weights_reshaped, append_bias ? biases : nullptr));
233
234 DataType v2mm_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type();
235 TensorShape shape_v2mm_out = input->tensor_shape();
236 shape_v2mm_out.set(0, conv_size * weights_z);
237 shape_v2mm_out.set(1, 1);
238 shape_v2mm_out.set(2, 1);
239 TensorInfo v2mm_output(input->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out));
240 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixVectorMultiplyKernel::validate(&input_reshaped, &weights_reshaped, &v2mm_output));
241
242 TensorInfo output_reshaped(v2mm_output.clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
243 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseVectorToTensorKernel::validate(&v2mm_output, (is_quantized) ? &output_reshaped : output, conv_w, conv_h));
244
245 if(is_quantized)
246 {
247 ARM_COMPUTE_RETURN_ON_ERROR(CLDirectConvolutionLayerOutputStageKernel::validate(&output_reshaped, biases, output));
248 }
249
Jenkinsb9abeae2018-11-22 11:58:08 +0000250 // Validate Activation Layer
251 if(act_info.enabled())
252 {
253 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info));
254 }
255
Jenkinsb3a371b2018-05-23 11:36:53 +0100256 return Status{};
257}
258
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000259void CLDepthwiseConvolutionLayer::run()
260{
Jenkins52ba29e2018-08-29 15:32:11 +0000261 prepare();
Jenkinsb3a371b2018-05-23 11:36:53 +0100262
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000263 CLScheduler::get().enqueue(_im2col_kernel);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000264 CLScheduler::get().enqueue(_v2mm_input_fill_border);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000265 CLScheduler::get().enqueue(_v2mm_kernel);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000266 CLScheduler::get().enqueue(_vector_to_tensor_kernel);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000267 if(_is_quantized)
268 {
269 CLScheduler::get().enqueue(_output_stage_kernel);
270 }
Jenkinsb9abeae2018-11-22 11:58:08 +0000271 if(_is_activationlayer_enabled)
272 {
273 _activationlayer_function.run();
274 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000275}
Jenkins52ba29e2018-08-29 15:32:11 +0000276
277void CLDepthwiseConvolutionLayer::prepare()
278{
279 if(!_is_prepared)
280 {
281 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
282
283 // Run weights reshaping and mark original weights tensor as unused
284 _weights_reshaped.allocator()->allocate();
285 CLScheduler::get().enqueue(_weights_reshape_kernel);
286 CLScheduler::get().enqueue(_v2mm_weights_fill_border);
287 _original_weights->mark_as_unused();
288
289 CLScheduler::get().queue().finish();
290 _is_prepared = true;
291 }
292}