blob: d9c21150df51ba610aae0bc512c1b4f5ded9f479 [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +01001/*
giuros016d109962019-01-07 17:47:19 +00002 * Copyright (c) 2017-2019 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/runtime/CL/functions/CLDepthwiseConvolutionLayer.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010025
26#include "arm_compute/core/CL/ICLTensor.h"
Giorgio Arenaad0c7382018-04-23 16:16:21 +010027#include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NCHWKernel.h"
28#include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NHWCKernel.h"
Georgios Pinitas05045c12018-12-07 18:31:47 +000029#include "arm_compute/core/Helpers.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010030#include "arm_compute/core/PixelValue.h"
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +000031#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000032#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010033#include "arm_compute/runtime/CL/CLScheduler.h"
34#include "support/ToolchainSupport.h"
35
giuros016d109962019-01-07 17:47:19 +000036namespace arm_compute
37{
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000038using namespace arm_compute::misc;
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +000039using namespace arm_compute::misc::shape_calculator;
Giorgio Arena93a690e2017-08-01 16:09:33 +010040
Georgios Pinitas05045c12018-12-07 18:31:47 +000041CLDepthwiseConvolutionLayer3x3::CLDepthwiseConvolutionLayer3x3(std::shared_ptr<IMemoryManager> memory_manager)
giuros016d109962019-01-07 17:47:19 +000042 : _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)
Giorgio Arena93a690e2017-08-01 16:09:33 +010044{
45}
46
Giorgio Arena76572242018-04-04 17:44:26 +010047void CLDepthwiseConvolutionLayer3x3::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier,
Usama Arife73686a2019-04-08 17:30:48 +010048 ActivationLayerInfo act_info, const Size2D &dilation)
Giorgio Arena9fe41442017-08-23 16:36:24 +010049{
Michele Di Giorgio933fe862018-02-19 15:42:12 +000050 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Georgios Pinitas236bfe72017-11-23 15:59:55 +000051 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Usama Arif881f2de2019-04-12 10:29:17 +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());
Giorgio Arena9fe41442017-08-23 16:36:24 +010060
Georgios Pinitas05045c12018-12-07 18:31:47 +000061 const bool is_nhwc = input->info()->data_layout() == DataLayout::NHWC;
62
giuros016d109962019-01-07 17:47:19 +000063 _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());
Georgios Pinitas05045c12018-12-07 18:31:47 +000066 _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
Usama Arife73686a2019-04-08 17:30:48 +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
giuros016d109962019-01-07 17:47:19 +000077 DepthwiseConvolutionReshapeInfo info;
78 info.c0 = 4;
Usama Arife73686a2019-04-08 17:30:48 +010079 info.transpose = is_stride_1_dilation_1 && is_dot8_supported;
giuros016d109962019-01-07 17:47:19 +000080
Georgios Pinitas05045c12018-12-07 18:31:47 +000081 if(_needs_permute)
Giorgio Arenadfca60b2018-01-31 10:30:59 +000082 {
Georgios Pinitas05045c12018-12-07 18:31:47 +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);
Pablo Telloa28aebc2019-06-03 14:59:48 +010093 _permuted_output.info()->set_quantization_info(output->info()->quantization_info());
Georgios Pinitas05045c12018-12-07 18:31:47 +000094
95 input_to_use = &_permuted_input;
96 weights_to_use = &_permuted_weights;
97 output_to_use = &_permuted_output;
98
Giorgio Arenadfca60b2018-01-31 10:30:59 +000099 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>();
100 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000101 else if(is_nhwc)
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000102 {
giuros016d109962019-01-07 17:47:19 +0000103 if(_needs_weights_reshape)
104 {
105 _reshape_weights.configure(weights, &_permuted_weights, info);
106 weights_to_use = &_permuted_weights;
107 }
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000108 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NHWCKernel>();
109 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000110 else
111 {
112 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>();
113 }
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000114
Georgios Pinitas05045c12018-12-07 18:31:47 +0000115 // Configure kernel
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000116 _kernel->set_target(CLScheduler::get().target());
Usama Arife73686a2019-04-08 17:30:48 +0100117 _kernel->configure(input_to_use, weights_to_use, biases, output_to_use, conv_info, depth_multiplier, act_info, dilation);
Georgios Pinitas05045c12018-12-07 18:31:47 +0000118
119 // 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
Georgios Pinitas3f8aac42018-12-24 13:09:02 +0000123 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
Georgios Pinitas05045c12018-12-07 18:31:47 +0000124 _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 }
Diego Lopez Recasfa0add12017-11-28 16:44:52 +0000130 // Configure border handler
131 PixelValue &&zero_value(0.f);
132 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
133 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100134 zero_value = PixelValue(static_cast<uint8_t>(input->info()->quantization_info().uniform().offset));
Diego Lopez Recasfa0add12017-11-28 16:44:52 +0000135 }
Georgios Pinitas3f8aac42018-12-24 13:09:02 +0000136 _border_handler.configure(input_to_use, _kernel->border_size(), BorderMode::CONSTANT, zero_value);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100137}
138
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100139Status CLDepthwiseConvolutionLayer3x3::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Usama Arife73686a2019-04-08 17:30:48 +0100140 unsigned int depth_multiplier, ActivationLayerInfo act_info, GPUTarget gpu_target, const Size2D &dilation)
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100141{
142 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodicedff601d2018-08-09 13:28:41 +0100143 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100144
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100145 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
Usama Arife73686a2019-04-08 17:30:48 +0100146 const bool is_nhwc = input->data_layout() == DataLayout::NHWC;
147 const bool needs_permute = is_nhwc && (depth_multiplier > 1);
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100148 const bool needs_weights_reshape = is_nhwc && (depth_multiplier == 1) && is_quantized;
Usama Arife73686a2019-04-08 17:30:48 +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());
giuros016d109962019-01-07 17:47:19 +0000152 DepthwiseConvolutionReshapeInfo info;
153 info.c0 = 4;
Usama Arife73686a2019-04-08 17:30:48 +0100154 info.transpose = is_stride_1_dilation_1 && is_dot8_supported;
Georgios Pinitas05045c12018-12-07 18:31:47 +0000155
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +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
Georgios Pinitas05045c12018-12-07 18:31:47 +0000167 if(needs_permute)
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100168 {
Georgios Pinitas05045c12018-12-07 18:31:47 +0000169 TensorShape permuted_input_shape = input->tensor_shape();
170 TensorShape permuted_weights_shape = weights->tensor_shape();
Usama Arife73686a2019-04-08 17:30:48 +0100171 TensorShape permuted_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);
Georgios Pinitas05045c12018-12-07 18:31:47 +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
Usama Arife73686a2019-04-08 17:30:48 +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));
Georgios Pinitas05045c12018-12-07 18:31:47 +0000183 }
184 else if(is_nhwc)
185 {
giuros016d109962019-01-07 17:47:19 +0000186 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,
Usama Arife73686a2019-04-08 17:30:48 +0100190 act_info, dilation));
giuros016d109962019-01-07 17:47:19 +0000191 }
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100192 else
193 {
194 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NHWCKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation));
195 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000196 }
197 else
198 {
Usama Arife73686a2019-04-08 17:30:48 +0100199 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NCHWKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation));
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100200 }
201
Georgios Pinitas05045c12018-12-07 18:31:47 +0000202 return Status{};
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100203}
204
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000205void CLDepthwiseConvolutionLayer3x3::run()
Giorgio Arena9fe41442017-08-23 16:36:24 +0100206{
Georgios Pinitas05045c12018-12-07 18:31:47 +0000207 prepare();
208
Georgios Pinitasda953f22019-04-02 17:27:03 +0100209 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas05045c12018-12-07 18:31:47 +0000210
211 if(_needs_permute)
212 {
213 _permute_input_to_nchw.run();
214 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100215 CLScheduler::get().enqueue(_border_handler);
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000216 CLScheduler::get().enqueue(*_kernel);
Georgios Pinitas05045c12018-12-07 18:31:47 +0000217
218 if(_needs_permute)
219 {
220 _permute_output_to_nhwc.run();
221 }
Georgios Pinitas05045c12018-12-07 18:31:47 +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 }
giuros016d109962019-01-07 17:47:19 +0000236
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 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000245 _is_prepared = true;
246 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100247}
248
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100249CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
250 : _memory_group(std::move(memory_manager)),
251 _optimised_function(nullptr),
252 _dwc_native_kernel(),
253 _permute_input_to_nhwc(),
254 _permute_weights_to_nhwc(),
255 _permute_output_to_nchw(),
256 _permuted_input(),
257 _permuted_weights(),
258 _permuted_output(),
259 _original_weights(),
260 _needs_permute(false),
261 _is_prepared(false)
Giorgio Arena9fe41442017-08-23 16:36:24 +0100262{
263}
264
Georgios Pinitas60e98252018-10-22 16:17:20 +0100265void CLDepthwiseConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
Usama Arife73686a2019-04-08 17:30:48 +0100266 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
Giorgio Arena93a690e2017-08-01 16:09:33 +0100267{
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100268 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
269 ARM_COMPUTE_ERROR_THROW_ON(CLDepthwiseConvolutionLayer::validate(input->info(),
270 weights->info(),
271 biases != nullptr ? biases->info() : nullptr,
272 output->info(),
273 conv_info,
274 depth_multiplier,
275 act_info,
276 dilation));
Giorgio Arena93a690e2017-08-01 16:09:33 +0100277
giuros016d109962019-01-07 17:47:19 +0000278 const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
279 const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
280
281 const bool can_run_optimised_3x3_kernel = (weights->info()->dimension(idx_w) == 3) && (weights->info()->dimension(idx_h) == 3);
282
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100283 _needs_permute = false;
284 _is_prepared = false;
285 _original_weights = weights;
286
Georgios Pinitas3f8aac42018-12-24 13:09:02 +0000287 if(bool(can_run_optimised_3x3_kernel))
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000288 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000289 auto f = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3>();
Usama Arife73686a2019-04-08 17:30:48 +0100290 f->configure(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
Pablo Tello8bf622a2018-12-03 15:54:49 +0000291 _optimised_function = std::move(f);
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000292 }
Pablo Tello8bf622a2018-12-03 15:54:49 +0000293 else
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000294 {
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100295 _needs_permute = input->info()->data_layout() == DataLayout::NCHW;
Giorgio Arena9fe41442017-08-23 16:36:24 +0100296
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100297 ICLTensor *input_to_use = input;
298 const ICLTensor *weights_to_use = weights;
299 ICLTensor *output_to_use = output;
300 if(_needs_permute)
Pablo Tello8bf622a2018-12-03 15:54:49 +0000301 {
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100302 _memory_group.manage(&_permuted_input);
303 _memory_group.manage(&_permuted_output);
Pablo Tello8bf622a2018-12-03 15:54:49 +0000304
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100305 // Configure the function to transform the input tensor from NCHW -> NHWC
306 _permute_input_to_nhwc.configure(input, &_permuted_input, PermutationVector(2U, 0U, 1U));
307 _permuted_input.info()->set_data_layout(DataLayout::NHWC);
308
309 // Configure the function to transform the weights tensor from IHW -> HWI
310 _permute_weights_to_nhwc.configure(weights, &_permuted_weights, PermutationVector(2U, 0U, 1U));
311 _permuted_weights.info()->set_data_layout(DataLayout::NHWC);
312
313 // Set output quantization info before dwc kernel configure
314 _permuted_output.info()->set_quantization_info(output->info()->quantization_info());
315
316 input_to_use = &_permuted_input;
317 weights_to_use = &_permuted_weights;
318 output_to_use = &_permuted_output;
Pablo Tello8bf622a2018-12-03 15:54:49 +0000319 }
320
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100321 DWCWeightsKernelInfo dwc_weights_info;
322 dwc_weights_info.n0 = (depth_multiplier == 1) ? 8 : 1;
323 DWCKernelInfo dwc_info;
324 dwc_info.activation_info = act_info;
325 _dwc_native_kernel.configure(input_to_use, weights_to_use, biases, output_to_use, dwc_weights_info, dwc_info, conv_info, depth_multiplier, dilation);
326
327 if(_needs_permute)
Pablo Tello8bf622a2018-12-03 15:54:49 +0000328 {
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100329 _permuted_input.allocator()->allocate();
Pablo Tello8bf622a2018-12-03 15:54:49 +0000330
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100331 // Configure the function to transform the convoluted output to NCHW format
332 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
333 _permute_output_to_nchw.configure(&_permuted_output, output, PermutationVector(1U, 2U, 0U));
334 _permuted_output.allocator()->allocate();
Pablo Tello8bf622a2018-12-03 15:54:49 +0000335 }
Georgios Pinitas60e98252018-10-22 16:17:20 +0100336 }
Giorgio Arena93a690e2017-08-01 16:09:33 +0100337}
338
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100339Status CLDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Usama Arife73686a2019-04-08 17:30:48 +0100340 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100341{
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100342 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
343
giuros016d109962019-01-07 17:47:19 +0000344 const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
345 const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
346
Usama Arife73686a2019-04-08 17:30:48 +0100347 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());
348 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());
349
giuros016d109962019-01-07 17:47:19 +0000350 const bool can_run_optimised_3x3_kernel = (weights->dimension(idx_w) == 3) && (weights->dimension(idx_h) == 3);
351
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100352 if(!can_run_optimised_3x3_kernel)
Georgios Pinitas60e98252018-10-22 16:17:20 +0100353 {
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100354 DWCWeightsKernelInfo dwc_weights_info;
355 dwc_weights_info.n0 = (depth_multiplier == 1) ? 8 : 1;
356 DWCKernelInfo dwc_info;
357 dwc_info.activation_info = act_info;
Georgios Pinitas60e98252018-10-22 16:17:20 +0100358
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100359 const bool needs_permute = input->data_layout() == DataLayout::NCHW;
Pablo Tello8bf622a2018-12-03 15:54:49 +0000360
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100361 if(needs_permute)
Pablo Tello8bf622a2018-12-03 15:54:49 +0000362 {
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100363 TensorShape permuted_input_shape = input->tensor_shape();
364 TensorShape permuted_weights_shape = weights->tensor_shape();
365 TensorShape permuted_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100366
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100367 permute(permuted_input_shape, PermutationVector(2U, 0U, 1U));
368 permute(permuted_weights_shape, PermutationVector(2U, 0U, 1U));
369 permute(permuted_output_shape, PermutationVector(2U, 0U, 1U));
370
371 const TensorInfo permuted_input = input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NHWC);
372 const TensorInfo permuted_weights = weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NHWC);
373 const TensorInfo permuted_output = output->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_output_shape).set_data_layout(DataLayout::NHWC);
374
375 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(input, &permuted_input, PermutationVector(2U, 0U, 1U)));
376 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(weights, &permuted_weights, PermutationVector(2U, 0U, 1U)));
377 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayerNativeKernel::validate(&permuted_input, &permuted_weights, biases, &permuted_output, dwc_weights_info,
378 dwc_info, conv_info, depth_multiplier, dilation));
379 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(&permuted_output, output, PermutationVector(1U, 2U, 0U)));
Pablo Tello8bf622a2018-12-03 15:54:49 +0000380 }
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100381 else
Pablo Tello8bf622a2018-12-03 15:54:49 +0000382 {
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100383 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayerNativeKernel::validate(input, weights, biases, output, dwc_weights_info, dwc_info, conv_info, depth_multiplier, dilation));
Pablo Tello8bf622a2018-12-03 15:54:49 +0000384 }
385 }
giuros016d109962019-01-07 17:47:19 +0000386 else
387 {
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100388 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, GPUTarget::MIDGARD, dilation));
giuros016d109962019-01-07 17:47:19 +0000389 }
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100390 return Status{};
391}
392
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000393void CLDepthwiseConvolutionLayer::run()
Giorgio Arena93a690e2017-08-01 16:09:33 +0100394{
Georgios Pinitas72219332018-06-05 14:56:06 +0100395 prepare();
Georgios Pinitas1562be32018-03-08 19:09:19 +0000396
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100397 MemoryGroupResourceScope scope_mg(_memory_group);
398
Pablo Tello8bf622a2018-12-03 15:54:49 +0000399 if(_optimised_function != nullptr)
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000400 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000401 _optimised_function->run();
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000402 }
Pablo Tello8bf622a2018-12-03 15:54:49 +0000403 else
Georgios Pinitas60e98252018-10-22 16:17:20 +0100404 {
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100405 if(_needs_permute)
Pablo Tello8bf622a2018-12-03 15:54:49 +0000406 {
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100407 _permute_input_to_nhwc.run();
Pablo Tello8bf622a2018-12-03 15:54:49 +0000408 }
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100409 CLScheduler::get().enqueue(_dwc_native_kernel);
410 if(_needs_permute)
Pablo Tello8bf622a2018-12-03 15:54:49 +0000411 {
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100412 _permute_output_to_nchw.run();
Pablo Tello8bf622a2018-12-03 15:54:49 +0000413 }
Georgios Pinitas60e98252018-10-22 16:17:20 +0100414 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100415}
Georgios Pinitas72219332018-06-05 14:56:06 +0100416
417void CLDepthwiseConvolutionLayer::prepare()
418{
Pablo Tello8bf622a2018-12-03 15:54:49 +0000419 if(_optimised_function != nullptr)
Georgios Pinitas72219332018-06-05 14:56:06 +0100420 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000421 _optimised_function->prepare();
422 }
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100423 else if(!_is_prepared)
Pablo Tello8bf622a2018-12-03 15:54:49 +0000424 {
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100425 if(_needs_permute)
Pablo Tello8bf622a2018-12-03 15:54:49 +0000426 {
427 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
Georgios Pinitas72219332018-06-05 14:56:06 +0100428
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100429 _permuted_weights.allocator()->allocate();
430 _permute_weights_to_nhwc.run();
Pablo Tello8bf622a2018-12-03 15:54:49 +0000431 _original_weights->mark_as_unused();
Pablo Tello8bf622a2018-12-03 15:54:49 +0000432 }
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100433 _is_prepared = true;
Georgios Pinitas72219332018-06-05 14:56:06 +0100434 }
435}
giuros016d109962019-01-07 17:47:19 +0000436} // namespace arm_compute