blob: f01b58a8b33c4d85bef2bf39f3e250c5a5624488 [file] [log] [blame]
Anthony Barbier8140e1e2017-12-14 23:48:46 +00001/*
Jenkins514be652019-02-28 12:25:18 +00002 * Copyright (c) 2017-2019 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"
Jenkins514be652019-02-28 12:25:18 +000029#include "arm_compute/core/Helpers.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000030#include "arm_compute/core/PixelValue.h"
Anthony Barbier06ea0482018-02-22 15:45:35 +000031#include "arm_compute/core/utils/misc/ShapeCalculator.h"
32#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000033#include "arm_compute/runtime/CL/CLScheduler.h"
34#include "support/ToolchainSupport.h"
35
Jenkins514be652019-02-28 12:25:18 +000036namespace arm_compute
37{
Anthony Barbier06ea0482018-02-22 15:45:35 +000038using namespace arm_compute::misc;
39using namespace arm_compute::misc::shape_calculator;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000040
Jenkins514be652019-02-28 12:25:18 +000041CLDepthwiseConvolutionLayer3x3::CLDepthwiseConvolutionLayer3x3(std::shared_ptr<IMemoryManager> memory_manager)
42 : _memory_group(std::move(memory_manager)), _kernel(nullptr), _border_handler(), _permute_input_to_nchw(), _permute_weights_to_nchw(), _permute_output_to_nhwc(), _reshape_weights(), _permuted_input(),
43 _permuted_weights(), _permuted_output(), _original_weights(nullptr), _needs_permute(false), _needs_weights_reshape(false), _is_prepared(false)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000044{
45}
46
Jenkinsb3a371b2018-05-23 11:36:53 +010047void CLDepthwiseConvolutionLayer3x3::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier,
Jenkins4ba87db2019-05-23 17:11:51 +010048 ActivationLayerInfo act_info, const Size2D &dilation)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000049{
Anthony Barbier06ea0482018-02-22 15:45:35 +000050 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000051 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Jenkins4ba87db2019-05-23 17:11:51 +010052 // idx_w and idx_h only used for validation
53 const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
54 const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
55 ARM_COMPUTE_UNUSED(idx_w);
56 ARM_COMPUTE_UNUSED(idx_h);
57
58 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(idx_w) + (weights->info()->dimension(idx_w) - 1) * (dilation.x() - 1) > input->info()->dimension(idx_w) + conv_info.pad_left() + conv_info.pad_right());
59 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(idx_h) + (weights->info()->dimension(idx_h) - 1) * (dilation.y() - 1) > input->info()->dimension(idx_h) + conv_info.pad_top() + conv_info.pad_bottom());
Anthony Barbier8140e1e2017-12-14 23:48:46 +000060
Jenkins514be652019-02-28 12:25:18 +000061 const bool is_nhwc = input->info()->data_layout() == DataLayout::NHWC;
62
63 _needs_permute = is_nhwc && (depth_multiplier > 1);
64 _needs_weights_reshape = is_nhwc && (depth_multiplier == 1)
65 && is_data_type_quantized_asymmetric(input->info()->data_type());
66 _is_prepared = false;
67 _original_weights = weights;
68
69 ICLTensor *input_to_use = input;
70 const ICLTensor *weights_to_use = weights;
71 ICLTensor *output_to_use = output;
72
Jenkins4ba87db2019-05-23 17:11:51 +010073 const bool is_stride_1 = ((conv_info.stride().first == conv_info.stride().second) && (conv_info.stride().first == 1));
74 const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device());
75 const bool is_stride_1_dilation_1 = (is_stride_1 && dilation.x() == 1 && dilation.y() == 1);
76
Jenkins514be652019-02-28 12:25:18 +000077 DepthwiseConvolutionReshapeInfo info;
78 info.c0 = 4;
Jenkins4ba87db2019-05-23 17:11:51 +010079 info.transpose = is_stride_1_dilation_1 && is_dot8_supported;
Jenkins514be652019-02-28 12:25:18 +000080
81 if(_needs_permute)
Jenkinsb3a371b2018-05-23 11:36:53 +010082 {
Jenkins514be652019-02-28 12:25:18 +000083 _memory_group.manage(&_permuted_input);
84 _memory_group.manage(&_permuted_output);
85
86 // Configure the function to transform the input tensor from NHWC -> NCHW
87 _permute_input_to_nchw.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
88 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
89
90 // Configure the function to transform the weights tensor from HWI -> IHW
91 _permute_weights_to_nchw.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
92 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
Jenkins975dfe12019-09-02 11:47:54 +010093 _permuted_output.info()->set_quantization_info(output->info()->quantization_info());
Jenkins514be652019-02-28 12:25:18 +000094
95 input_to_use = &_permuted_input;
96 weights_to_use = &_permuted_weights;
97 output_to_use = &_permuted_output;
98
Jenkinsb3a371b2018-05-23 11:36:53 +010099 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>();
100 }
Jenkins514be652019-02-28 12:25:18 +0000101 else if(is_nhwc)
102 {
103 if(_needs_weights_reshape)
104 {
105 _reshape_weights.configure(weights, &_permuted_weights, info);
106 weights_to_use = &_permuted_weights;
107 }
108 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NHWCKernel>();
109 }
Jenkinsb3a371b2018-05-23 11:36:53 +0100110 else
111 {
Jenkins514be652019-02-28 12:25:18 +0000112 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>();
Jenkinsb3a371b2018-05-23 11:36:53 +0100113 }
114
Jenkins514be652019-02-28 12:25:18 +0000115 // Configure kernel
Jenkinsb3a371b2018-05-23 11:36:53 +0100116 _kernel->set_target(CLScheduler::get().target());
Jenkins4ba87db2019-05-23 17:11:51 +0100117 _kernel->configure(input_to_use, weights_to_use, biases, output_to_use, conv_info, depth_multiplier, act_info, dilation);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000118
Jenkins514be652019-02-28 12:25:18 +0000119 // Permute output if needed
120 if(_needs_permute)
121 {
122 // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
123 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
124 _permute_output_to_nhwc.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
125
126 // Allocate tensors
127 _permuted_input.allocator()->allocate();
128 _permuted_output.allocator()->allocate();
129 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000130 // Configure border handler
131 PixelValue &&zero_value(0.f);
132 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
133 {
Jenkins975dfe12019-09-02 11:47:54 +0100134 zero_value = PixelValue(static_cast<uint8_t>(input->info()->quantization_info().uniform().offset));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000135 }
Jenkins514be652019-02-28 12:25:18 +0000136 _border_handler.configure(input_to_use, _kernel->border_size(), BorderMode::CONSTANT, zero_value);
Jenkinsb3a371b2018-05-23 11:36:53 +0100137}
138
139Status CLDepthwiseConvolutionLayer3x3::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Jenkins4ba87db2019-05-23 17:11:51 +0100140 unsigned int depth_multiplier, ActivationLayerInfo act_info, GPUTarget gpu_target, const Size2D &dilation)
Jenkinsb3a371b2018-05-23 11:36:53 +0100141{
142 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Jenkins52ba29e2018-08-29 15:32:11 +0000143 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Jenkinsb3a371b2018-05-23 11:36:53 +0100144
Jenkins975dfe12019-09-02 11:47:54 +0100145 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
Jenkins4ba87db2019-05-23 17:11:51 +0100146 const bool is_nhwc = input->data_layout() == DataLayout::NHWC;
147 const bool needs_permute = is_nhwc && (depth_multiplier > 1);
Jenkins975dfe12019-09-02 11:47:54 +0100148 const bool needs_weights_reshape = is_nhwc && (depth_multiplier == 1) && is_quantized;
Jenkins4ba87db2019-05-23 17:11:51 +0100149 const bool is_stride_1 = ((conv_info.stride().first == conv_info.stride().second) && (conv_info.stride().first == 1));
150 const bool is_stride_1_dilation_1 = (is_stride_1 && dilation.x() == 1 && dilation.y() == 1);
151 const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device());
Jenkins514be652019-02-28 12:25:18 +0000152 DepthwiseConvolutionReshapeInfo info;
153 info.c0 = 4;
Jenkins4ba87db2019-05-23 17:11:51 +0100154 info.transpose = is_stride_1_dilation_1 && is_dot8_supported;
Jenkins514be652019-02-28 12:25:18 +0000155
Jenkins975dfe12019-09-02 11:47:54 +0100156 if(is_quantized)
157 {
158 const UniformQuantizationInfo iq_info = input->quantization_info().uniform();
159 const UniformQuantizationInfo wq_info = weights->quantization_info().uniform();
160 const UniformQuantizationInfo oq_info = (output->total_size() == 0) ? iq_info : output->quantization_info().uniform();
161
162 const float multiplier = iq_info.scale * wq_info.scale / oq_info.scale;
163 ARM_COMPUTE_UNUSED(multiplier);
164 ARM_COMPUTE_RETURN_ERROR_ON(multiplier > 1.0f);
165 }
166
Jenkins514be652019-02-28 12:25:18 +0000167 if(needs_permute)
Jenkinsb3a371b2018-05-23 11:36:53 +0100168 {
Jenkins514be652019-02-28 12:25:18 +0000169 TensorShape permuted_input_shape = input->tensor_shape();
170 TensorShape permuted_weights_shape = weights->tensor_shape();
Jenkins4ba87db2019-05-23 17:11:51 +0100171 TensorShape permuted_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);
Jenkins514be652019-02-28 12:25:18 +0000172
173 permute(permuted_input_shape, PermutationVector(1U, 2U, 0U));
174 permute(permuted_weights_shape, PermutationVector(1U, 2U, 0U));
175 permute(permuted_output_shape, PermutationVector(1U, 2U, 0U));
176
177 const TensorInfo permuted_input = input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NCHW);
178 const TensorInfo permuted_weights = weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NCHW);
179 const TensorInfo permuted_output = output->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_output_shape).set_data_layout(DataLayout::NCHW);
180
Jenkins4ba87db2019-05-23 17:11:51 +0100181 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NCHWKernel::validate(&permuted_input, &permuted_weights, biases, &permuted_output, conv_info, depth_multiplier, act_info, gpu_target,
182 dilation));
Jenkins514be652019-02-28 12:25:18 +0000183 }
184 else if(is_nhwc)
185 {
186 if(needs_weights_reshape)
187 {
188 auto reshaped_weights_shape = arm_compute::misc::shape_calculator::compute_reshaped_depthwise_weights_shape(*weights, info);
189 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NHWCKernel::validate(input, &weights->clone()->set_tensor_shape(reshaped_weights_shape), biases, output, conv_info, depth_multiplier,
Jenkins4ba87db2019-05-23 17:11:51 +0100190 act_info, dilation));
Jenkins514be652019-02-28 12:25:18 +0000191 }
Jenkins975dfe12019-09-02 11:47:54 +0100192 else
193 {
194 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NHWCKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation));
195 }
Jenkins514be652019-02-28 12:25:18 +0000196 }
197 else
198 {
Jenkins4ba87db2019-05-23 17:11:51 +0100199 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NCHWKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation));
Jenkinsb3a371b2018-05-23 11:36:53 +0100200 }
201
Jenkins514be652019-02-28 12:25:18 +0000202 return Status{};
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000203}
204
205void CLDepthwiseConvolutionLayer3x3::run()
206{
Jenkins514be652019-02-28 12:25:18 +0000207 prepare();
208
Jenkins4ba87db2019-05-23 17:11:51 +0100209 MemoryGroupResourceScope scope_mg(_memory_group);
Jenkins514be652019-02-28 12:25:18 +0000210
211 if(_needs_permute)
212 {
213 _permute_input_to_nchw.run();
214 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000215 CLScheduler::get().enqueue(_border_handler);
Jenkinsb3a371b2018-05-23 11:36:53 +0100216 CLScheduler::get().enqueue(*_kernel);
Jenkins514be652019-02-28 12:25:18 +0000217
218 if(_needs_permute)
219 {
220 _permute_output_to_nhwc.run();
221 }
Jenkins514be652019-02-28 12:25:18 +0000222}
223
224void CLDepthwiseConvolutionLayer3x3::prepare()
225{
226 if(!_is_prepared)
227 {
228 if(_needs_permute)
229 {
230 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
231
232 _permuted_weights.allocator()->allocate();
233 _permute_weights_to_nchw.run();
234 _original_weights->mark_as_unused();
235 }
236
237 if(_needs_weights_reshape)
238 {
239 ARM_COMPUTE_ERROR_ON(_needs_permute);
240 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
241 _permuted_weights.allocator()->allocate();
242 CLScheduler::get().enqueue(_reshape_weights);
243 _original_weights->mark_as_unused();
244 }
245 _is_prepared = true;
246 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000247}
248
249CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayer()
Jenkinsb9abeae2018-11-22 11:58:08 +0000250 : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _activationlayer_function(), _v2mm_input_fill_border(), _v2mm_weights_fill_border(),
Jenkins514be652019-02-28 12:25:18 +0000251 _input_reshaped(), _weights_reshaped(), _v2mm_output(), _output_reshaped(), _is_prepared(false), _is_quantized(false), _is_activationlayer_enabled(false), _original_weights(nullptr),
252 _optimised_function(nullptr)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000253{
254}
255
Jenkinsb9abeae2018-11-22 11:58:08 +0000256void CLDepthwiseConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
Jenkins4ba87db2019-05-23 17:11:51 +0100257 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000258{
Jenkinsb3a371b2018-05-23 11:36:53 +0100259 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000260 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Jenkins52ba29e2018-08-29 15:32:11 +0000261 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000262
Jenkins52ba29e2018-08-29 15:32:11 +0000263 const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
264 const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000265
Jenkins4ba87db2019-05-23 17:11:51 +0100266 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(idx_w) + (weights->info()->dimension(idx_w) - 1) * (dilation.x() - 1) > input->info()->dimension(idx_w) + conv_info.pad_left() + conv_info.pad_right());
267 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(idx_h) + (weights->info()->dimension(idx_h) - 1) * (dilation.y() - 1) > input->info()->dimension(idx_h) + conv_info.pad_top() + conv_info.pad_bottom());
268
Jenkins514be652019-02-28 12:25:18 +0000269 const bool can_run_optimised_3x3_kernel = (weights->info()->dimension(idx_w) == 3) && (weights->info()->dimension(idx_h) == 3);
Jenkins52ba29e2018-08-29 15:32:11 +0000270
Jenkins514be652019-02-28 12:25:18 +0000271 if(bool(can_run_optimised_3x3_kernel))
Anthony Barbier06ea0482018-02-22 15:45:35 +0000272 {
Jenkins514be652019-02-28 12:25:18 +0000273 auto f = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3>();
Jenkins4ba87db2019-05-23 17:11:51 +0100274 f->configure(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
Jenkins514be652019-02-28 12:25:18 +0000275 _optimised_function = std::move(f);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000276 }
Jenkins514be652019-02-28 12:25:18 +0000277 else
Anthony Barbier06ea0482018-02-22 15:45:35 +0000278 {
Jenkins514be652019-02-28 12:25:18 +0000279 const size_t idx_c = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000280
Jenkins514be652019-02-28 12:25:18 +0000281 const size_t weights_w = weights->info()->dimension(idx_w);
282 const size_t weights_h = weights->info()->dimension(idx_h);
283 const size_t weights_z = weights->info()->dimension(idx_c);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000284
Jenkins514be652019-02-28 12:25:18 +0000285 _is_prepared = false;
286 _original_weights = weights;
287 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Jenkinsb9abeae2018-11-22 11:58:08 +0000288
Jenkins514be652019-02-28 12:25:18 +0000289 bool append_bias = (biases != nullptr) && !_is_quantized;
290 const GPUTarget gpu_target = CLScheduler::get().target();
Jenkinsb9abeae2018-11-22 11:58:08 +0000291
Jenkins514be652019-02-28 12:25:18 +0000292 // Calculate output shape
Jenkins4ba87db2019-05-23 17:11:51 +0100293 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier, dilation);
Jenkins514be652019-02-28 12:25:18 +0000294
295 // Output auto inizialitation if not yet initialized
296 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
297 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
298
299 // Output width and height
300 const unsigned int conv_w = output_shape[idx_w];
301 const unsigned int conv_h = output_shape[idx_h];
302
303 // Set up intermediate tensors
304 const size_t patch_size = weights_w * weights_h + ((append_bias) ? 1 : 0);
305 const size_t conv_size = conv_w * conv_h;
306
Jenkins975dfe12019-09-02 11:47:54 +0100307 const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform();
308 const UniformQuantizationInfo wq_info = weights->info()->quantization_info().uniform();
309 const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform();
310
Jenkins514be652019-02-28 12:25:18 +0000311 // Im2Col configuration
312 TensorShape shape_im2col = input->info()->tensor_shape();
313 shape_im2col.set(0, patch_size);
314 shape_im2col.set(1, conv_size);
315 shape_im2col.set(2, weights_z);
316 _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
317 _im2col_kernel.set_target(gpu_target);
Jenkins4ba87db2019-05-23 17:11:51 +0100318 _im2col_kernel.configure(input, &_input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier, dilation);
Jenkins514be652019-02-28 12:25:18 +0000319 CLScheduler::get().tune_kernel_static(_im2col_kernel);
320
321 // Weights reshape configuration
322 const TensorShape shape_weights_reshape(patch_size, weights_z);
323 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape));
324 _weights_reshape_kernel.configure(weights, &_weights_reshaped, append_bias ? biases : nullptr);
325
326 // GEMV configuration
327 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
328 TensorShape shape_v2mm_out = input->info()->tensor_shape();
329 shape_v2mm_out.set(0, conv_size * weights_z);
330 shape_v2mm_out.set(1, 1);
331 shape_v2mm_out.set(2, 1);
332 _v2mm_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out));
333 _v2mm_kernel.set_target(gpu_target);
334 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
335 CLScheduler::get().tune_kernel_static(_v2mm_kernel);
336 _output_reshaped.allocator()->init(_v2mm_output.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
337 _vector_to_tensor_kernel.configure(&_v2mm_output, (_is_quantized) ? &_output_reshaped : output, conv_w, conv_h);
338
339 // Output staged configuration
340 if(_is_quantized)
341 {
Jenkins975dfe12019-09-02 11:47:54 +0100342 const UniformQuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? iq_info : oq_info;
Jenkins514be652019-02-28 12:25:18 +0000343
Jenkins975dfe12019-09-02 11:47:54 +0100344 int output_multiplier = 0;
345 int output_shift = 0;
346 const float multiplier = iq_info.scale * wq_info.scale / output_quant_info.scale;
Jenkins514be652019-02-28 12:25:18 +0000347 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
348 _output_stage_kernel.configure(&_output_reshaped, biases, output, output_multiplier, output_shift, output_quant_info.offset);
349 _output_reshaped.allocator()->allocate();
350 }
351
352 // Fill borders on inputs
353 PixelValue zero_in(static_cast<int32_t>(0));
354 PixelValue zero_w(static_cast<int32_t>(0));
355 if(_is_quantized)
356 {
Jenkins975dfe12019-09-02 11:47:54 +0100357 zero_in = PixelValue(static_cast<int32_t>(iq_info.offset));
358 zero_w = PixelValue(static_cast<int32_t>(wq_info.offset));
Jenkins514be652019-02-28 12:25:18 +0000359 }
360 BorderSize border_size = _v2mm_kernel.border_size();
361 _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in);
362
363 border_size.bottom = 0;
364 _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w);
365
366 // Allocate intermediate tensors
367 _input_reshaped.allocator()->allocate();
368 _v2mm_output.allocator()->allocate();
369
370 //Configure Activation Layer
371 _is_activationlayer_enabled = act_info.enabled();
372
373 if(_is_activationlayer_enabled)
374 {
375 _activationlayer_function.configure(output, nullptr, act_info);
376 }
Jenkinsb9abeae2018-11-22 11:58:08 +0000377 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000378}
379
Jenkinsb3a371b2018-05-23 11:36:53 +0100380Status CLDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Jenkins4ba87db2019-05-23 17:11:51 +0100381 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
Jenkinsb3a371b2018-05-23 11:36:53 +0100382{
Jenkins52ba29e2018-08-29 15:32:11 +0000383 const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
384 const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
Jenkins52ba29e2018-08-29 15:32:11 +0000385
Jenkins4ba87db2019-05-23 17:11:51 +0100386 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) + (weights->dimension(idx_w) - 1) * (dilation.x() - 1) > input->dimension(idx_w) + conv_info.pad_left() + conv_info.pad_right());
387 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_h) + (weights->dimension(idx_h) - 1) * (dilation.y() - 1) > input->dimension(idx_h) + conv_info.pad_top() + conv_info.pad_bottom());
388
Jenkins514be652019-02-28 12:25:18 +0000389 const bool can_run_optimised_3x3_kernel = (weights->dimension(idx_w) == 3) && (weights->dimension(idx_h) == 3);
Jenkinsb3a371b2018-05-23 11:36:53 +0100390
Jenkins975dfe12019-09-02 11:47:54 +0100391 if(!can_run_optimised_3x3_kernel)
Jenkinsb3a371b2018-05-23 11:36:53 +0100392 {
Jenkins514be652019-02-28 12:25:18 +0000393 const size_t idx_c = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
Jenkinsb3a371b2018-05-23 11:36:53 +0100394
Jenkins514be652019-02-28 12:25:18 +0000395 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
396 ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(idx_c) * depth_multiplier) != weights->dimension(idx_c));
397
398 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
399 const bool append_bias = (biases != nullptr) && !is_quantized;
Jenkins4ba87db2019-05-23 17:11:51 +0100400 const TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);
Jenkins514be652019-02-28 12:25:18 +0000401 const size_t weights_w = weights->dimension(idx_w);
402 const size_t weights_h = weights->dimension(idx_h);
403 const size_t weights_z = weights->dimension(idx_c);
404 const unsigned int conv_w = output_shape[idx_w];
405 const unsigned int conv_h = output_shape[idx_h];
406 const size_t patch_size = weights_w * weights_h + ((append_bias) ? 1 : 0);
407 const size_t conv_size = conv_w * conv_h;
408
409 TensorShape shape_im2col = input->tensor_shape();
410 shape_im2col.set(0, patch_size);
411 shape_im2col.set(1, conv_size);
412 shape_im2col.set(2, weights_z);
413 TensorInfo input_reshaped(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
Jenkins4ba87db2019-05-23 17:11:51 +0100414 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseIm2ColKernel::validate(input, &input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier, dilation));
Jenkins514be652019-02-28 12:25:18 +0000415
416 const TensorShape shape_weights_reshape(patch_size, weights_z);
417 TensorInfo weights_reshaped(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape));
418 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayerReshapeWeightsGenericKernel::validate(weights, &weights_reshaped, append_bias ? biases : nullptr));
419
420 DataType v2mm_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type();
421 TensorShape shape_v2mm_out = input->tensor_shape();
422 shape_v2mm_out.set(0, conv_size * weights_z);
423 shape_v2mm_out.set(1, 1);
424 shape_v2mm_out.set(2, 1);
425 TensorInfo v2mm_output(input->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out));
426 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixVectorMultiplyKernel::validate(&input_reshaped, &weights_reshaped, &v2mm_output));
427
428 TensorInfo output_reshaped(v2mm_output.clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
429 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseVectorToTensorKernel::validate(&v2mm_output, (is_quantized) ? &output_reshaped : output, conv_w, conv_h));
430
431 if(is_quantized)
432 {
Jenkins975dfe12019-09-02 11:47:54 +0100433 const UniformQuantizationInfo iq_info = input->quantization_info().uniform();
434 const UniformQuantizationInfo wq_info = weights->quantization_info().uniform();
435 const UniformQuantizationInfo oq_info = (output->total_size() == 0) ? iq_info : output->quantization_info().uniform();
436
437 const float multiplier = iq_info.scale * wq_info.scale / oq_info.scale;
438 ARM_COMPUTE_UNUSED(multiplier);
439 ARM_COMPUTE_RETURN_ERROR_ON(multiplier > 1.0f);
Jenkins514be652019-02-28 12:25:18 +0000440 ARM_COMPUTE_RETURN_ON_ERROR(CLDirectConvolutionLayerOutputStageKernel::validate(&output_reshaped, biases, output));
441 }
442
443 // Validate Activation Layer
444 if(act_info.enabled())
445 {
446 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info));
447 }
448 }
449 else
Jenkinsb9abeae2018-11-22 11:58:08 +0000450 {
Jenkins975dfe12019-09-02 11:47:54 +0100451 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, GPUTarget::MIDGARD, dilation));
Jenkinsb9abeae2018-11-22 11:58:08 +0000452 }
Jenkinsb3a371b2018-05-23 11:36:53 +0100453 return Status{};
454}
455
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000456void CLDepthwiseConvolutionLayer::run()
457{
Jenkins52ba29e2018-08-29 15:32:11 +0000458 prepare();
Jenkinsb3a371b2018-05-23 11:36:53 +0100459
Jenkins514be652019-02-28 12:25:18 +0000460 if(_optimised_function != nullptr)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000461 {
Jenkins514be652019-02-28 12:25:18 +0000462 _optimised_function->run();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000463 }
Jenkins514be652019-02-28 12:25:18 +0000464 else
Jenkinsb9abeae2018-11-22 11:58:08 +0000465 {
Jenkins514be652019-02-28 12:25:18 +0000466 CLScheduler::get().enqueue(_im2col_kernel);
467 CLScheduler::get().enqueue(_v2mm_input_fill_border);
468 CLScheduler::get().enqueue(_v2mm_kernel);
469 CLScheduler::get().enqueue(_vector_to_tensor_kernel);
470 if(_is_quantized)
471 {
472 CLScheduler::get().enqueue(_output_stage_kernel);
473 }
474 if(_is_activationlayer_enabled)
475 {
476 _activationlayer_function.run();
477 }
Jenkinsb9abeae2018-11-22 11:58:08 +0000478 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000479}
Jenkins52ba29e2018-08-29 15:32:11 +0000480
481void CLDepthwiseConvolutionLayer::prepare()
482{
Jenkins514be652019-02-28 12:25:18 +0000483 if(_optimised_function != nullptr)
Jenkins52ba29e2018-08-29 15:32:11 +0000484 {
Jenkins514be652019-02-28 12:25:18 +0000485 _optimised_function->prepare();
486 }
487 else
488 {
489 if(!_is_prepared)
490 {
491 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
Jenkins52ba29e2018-08-29 15:32:11 +0000492
Jenkins514be652019-02-28 12:25:18 +0000493 // Run weights reshaping and mark original weights tensor as unused
494 _weights_reshaped.allocator()->allocate();
495 CLScheduler::get().enqueue(_weights_reshape_kernel);
496 CLScheduler::get().enqueue(_v2mm_weights_fill_border);
497 _original_weights->mark_as_unused();
Jenkins52ba29e2018-08-29 15:32:11 +0000498
Jenkins514be652019-02-28 12:25:18 +0000499 CLScheduler::get().queue().finish();
500 _is_prepared = true;
501 }
Jenkins52ba29e2018-08-29 15:32:11 +0000502 }
503}
Jenkins514be652019-02-28 12:25:18 +0000504} // namespace arm_compute