blob: 16082775c32db4dafcb2e30a384267e3ea70e348 [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
Usama Arife73686a2019-04-08 17:30:48 +0100145 const bool is_nhwc = input->data_layout() == DataLayout::NHWC;
146 const bool needs_permute = is_nhwc && (depth_multiplier > 1);
147 const bool needs_weights_reshape = is_nhwc && (depth_multiplier == 1);
148 const bool is_stride_1 = ((conv_info.stride().first == conv_info.stride().second) && (conv_info.stride().first == 1));
149 const bool is_stride_1_dilation_1 = (is_stride_1 && dilation.x() == 1 && dilation.y() == 1);
150 const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device());
giuros016d109962019-01-07 17:47:19 +0000151 DepthwiseConvolutionReshapeInfo info;
152 info.c0 = 4;
Usama Arife73686a2019-04-08 17:30:48 +0100153 info.transpose = is_stride_1_dilation_1 && is_dot8_supported;
Georgios Pinitas05045c12018-12-07 18:31:47 +0000154
155 if(needs_permute)
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100156 {
Georgios Pinitas05045c12018-12-07 18:31:47 +0000157 TensorShape permuted_input_shape = input->tensor_shape();
158 TensorShape permuted_weights_shape = weights->tensor_shape();
Usama Arife73686a2019-04-08 17:30:48 +0100159 TensorShape permuted_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);
Georgios Pinitas05045c12018-12-07 18:31:47 +0000160
161 permute(permuted_input_shape, PermutationVector(1U, 2U, 0U));
162 permute(permuted_weights_shape, PermutationVector(1U, 2U, 0U));
163 permute(permuted_output_shape, PermutationVector(1U, 2U, 0U));
164
165 const TensorInfo permuted_input = input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NCHW);
166 const TensorInfo permuted_weights = weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NCHW);
167 const TensorInfo permuted_output = output->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_output_shape).set_data_layout(DataLayout::NCHW);
168
Usama Arife73686a2019-04-08 17:30:48 +0100169 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NCHWKernel::validate(&permuted_input, &permuted_weights, biases, &permuted_output, conv_info, depth_multiplier, act_info, gpu_target,
170 dilation));
Georgios Pinitas05045c12018-12-07 18:31:47 +0000171 }
172 else if(is_nhwc)
173 {
giuros016d109962019-01-07 17:47:19 +0000174 if(needs_weights_reshape)
175 {
176 auto reshaped_weights_shape = arm_compute::misc::shape_calculator::compute_reshaped_depthwise_weights_shape(*weights, info);
177 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 +0100178 act_info, dilation));
giuros016d109962019-01-07 17:47:19 +0000179 }
Usama Arife73686a2019-04-08 17:30:48 +0100180 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NHWCKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation));
Georgios Pinitas05045c12018-12-07 18:31:47 +0000181 }
182 else
183 {
Usama Arife73686a2019-04-08 17:30:48 +0100184 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 +0100185 }
186
Georgios Pinitas05045c12018-12-07 18:31:47 +0000187 return Status{};
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100188}
189
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000190void CLDepthwiseConvolutionLayer3x3::run()
Giorgio Arena9fe41442017-08-23 16:36:24 +0100191{
Georgios Pinitas05045c12018-12-07 18:31:47 +0000192 prepare();
193
Georgios Pinitasda953f22019-04-02 17:27:03 +0100194 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas05045c12018-12-07 18:31:47 +0000195
196 if(_needs_permute)
197 {
198 _permute_input_to_nchw.run();
199 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100200 CLScheduler::get().enqueue(_border_handler);
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000201 CLScheduler::get().enqueue(*_kernel);
Georgios Pinitas05045c12018-12-07 18:31:47 +0000202
203 if(_needs_permute)
204 {
205 _permute_output_to_nhwc.run();
206 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000207}
208
209void CLDepthwiseConvolutionLayer3x3::prepare()
210{
211 if(!_is_prepared)
212 {
213 if(_needs_permute)
214 {
215 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
216
217 _permuted_weights.allocator()->allocate();
218 _permute_weights_to_nchw.run();
219 _original_weights->mark_as_unused();
220 }
giuros016d109962019-01-07 17:47:19 +0000221
222 if(_needs_weights_reshape)
223 {
224 ARM_COMPUTE_ERROR_ON(_needs_permute);
225 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
226 _permuted_weights.allocator()->allocate();
227 CLScheduler::get().enqueue(_reshape_weights);
228 _original_weights->mark_as_unused();
229 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000230 _is_prepared = true;
231 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100232}
233
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000234CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayer()
Georgios Pinitas60e98252018-10-22 16:17:20 +0100235 : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _activationlayer_function(), _v2mm_input_fill_border(), _v2mm_weights_fill_border(),
Pablo Tello8bf622a2018-12-03 15:54:49 +0000236 _input_reshaped(), _weights_reshaped(), _v2mm_output(), _output_reshaped(), _is_prepared(false), _is_quantized(false), _is_activationlayer_enabled(false), _original_weights(nullptr),
237 _optimised_function(nullptr)
Giorgio Arena9fe41442017-08-23 16:36:24 +0100238{
239}
240
Georgios Pinitas60e98252018-10-22 16:17:20 +0100241void CLDepthwiseConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
Usama Arife73686a2019-04-08 17:30:48 +0100242 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
Giorgio Arena93a690e2017-08-01 16:09:33 +0100243{
Michele Di Giorgiod24af8a2018-05-08 17:23:52 +0100244 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100245 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Giorgio Arenad051e972018-06-20 11:46:42 +0100246 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100247
giuros016d109962019-01-07 17:47:19 +0000248 const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
249 const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
250
Usama Arif881f2de2019-04-12 10:29:17 +0100251 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());
252 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());
253
giuros016d109962019-01-07 17:47:19 +0000254 const bool can_run_optimised_3x3_kernel = (weights->info()->dimension(idx_w) == 3) && (weights->info()->dimension(idx_h) == 3);
255
Georgios Pinitas3f8aac42018-12-24 13:09:02 +0000256 if(bool(can_run_optimised_3x3_kernel))
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000257 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000258 auto f = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3>();
Usama Arife73686a2019-04-08 17:30:48 +0100259 f->configure(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
Pablo Tello8bf622a2018-12-03 15:54:49 +0000260 _optimised_function = std::move(f);
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000261 }
Pablo Tello8bf622a2018-12-03 15:54:49 +0000262 else
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000263 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000264 const size_t idx_c = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100265
Pablo Tello8bf622a2018-12-03 15:54:49 +0000266 const size_t weights_w = weights->info()->dimension(idx_w);
267 const size_t weights_h = weights->info()->dimension(idx_h);
268 const size_t weights_z = weights->info()->dimension(idx_c);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100269
Pablo Tello8bf622a2018-12-03 15:54:49 +0000270 _is_prepared = false;
271 _original_weights = weights;
272 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Georgios Pinitas60e98252018-10-22 16:17:20 +0100273
Pablo Tello8bf622a2018-12-03 15:54:49 +0000274 bool append_bias = (biases != nullptr) && !_is_quantized;
275 const GPUTarget gpu_target = CLScheduler::get().target();
Georgios Pinitas60e98252018-10-22 16:17:20 +0100276
Pablo Tello8bf622a2018-12-03 15:54:49 +0000277 // Calculate output shape
Usama Arife73686a2019-04-08 17:30:48 +0100278 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier, dilation);
Pablo Tello8bf622a2018-12-03 15:54:49 +0000279
280 // Output auto inizialitation if not yet initialized
281 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
282 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
283
284 // Output width and height
285 const unsigned int conv_w = output_shape[idx_w];
286 const unsigned int conv_h = output_shape[idx_h];
287
288 // Set up intermediate tensors
289 const size_t patch_size = weights_w * weights_h + ((append_bias) ? 1 : 0);
290 const size_t conv_size = conv_w * conv_h;
291
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100292 const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform();
293 const UniformQuantizationInfo wq_info = weights->info()->quantization_info().uniform();
294 const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform();
295
Pablo Tello8bf622a2018-12-03 15:54:49 +0000296 // Im2Col configuration
297 TensorShape shape_im2col = input->info()->tensor_shape();
298 shape_im2col.set(0, patch_size);
299 shape_im2col.set(1, conv_size);
300 shape_im2col.set(2, weights_z);
301 _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
302 _im2col_kernel.set_target(gpu_target);
Usama Arife73686a2019-04-08 17:30:48 +0100303 _im2col_kernel.configure(input, &_input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier, dilation);
Pablo Tello8bf622a2018-12-03 15:54:49 +0000304 CLScheduler::get().tune_kernel_static(_im2col_kernel);
305
306 // Weights reshape configuration
307 const TensorShape shape_weights_reshape(patch_size, weights_z);
308 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape));
309 _weights_reshape_kernel.configure(weights, &_weights_reshaped, append_bias ? biases : nullptr);
310
311 // GEMV configuration
312 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
313 TensorShape shape_v2mm_out = input->info()->tensor_shape();
314 shape_v2mm_out.set(0, conv_size * weights_z);
315 shape_v2mm_out.set(1, 1);
316 shape_v2mm_out.set(2, 1);
317 _v2mm_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out));
318 _v2mm_kernel.set_target(gpu_target);
319 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
320 CLScheduler::get().tune_kernel_static(_v2mm_kernel);
321 _output_reshaped.allocator()->init(_v2mm_output.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
322 _vector_to_tensor_kernel.configure(&_v2mm_output, (_is_quantized) ? &_output_reshaped : output, conv_w, conv_h);
323
324 // Output staged configuration
325 if(_is_quantized)
326 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100327 const UniformQuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? iq_info : oq_info;
Pablo Tello8bf622a2018-12-03 15:54:49 +0000328
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100329 float multiplier = iq_info.scale * wq_info.scale / output_quant_info.scale;
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100330 int output_multiplier;
331 int output_shift;
Pablo Tello8bf622a2018-12-03 15:54:49 +0000332 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
333 _output_stage_kernel.configure(&_output_reshaped, biases, output, output_multiplier, output_shift, output_quant_info.offset);
334 _output_reshaped.allocator()->allocate();
335 }
336
337 // Fill borders on inputs
338 PixelValue zero_in(static_cast<int32_t>(0));
339 PixelValue zero_w(static_cast<int32_t>(0));
340 if(_is_quantized)
341 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100342 zero_in = PixelValue(static_cast<int32_t>(iq_info.offset));
343 zero_w = PixelValue(static_cast<int32_t>(wq_info.offset));
Pablo Tello8bf622a2018-12-03 15:54:49 +0000344 }
345 BorderSize border_size = _v2mm_kernel.border_size();
346 _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in);
347
348 border_size.bottom = 0;
349 _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w);
350
351 // Allocate intermediate tensors
352 _input_reshaped.allocator()->allocate();
353 _v2mm_output.allocator()->allocate();
354
355 //Configure Activation Layer
356 _is_activationlayer_enabled = act_info.enabled();
357
358 if(_is_activationlayer_enabled)
359 {
360 _activationlayer_function.configure(output, nullptr, act_info);
361 }
Georgios Pinitas60e98252018-10-22 16:17:20 +0100362 }
Giorgio Arena93a690e2017-08-01 16:09:33 +0100363}
364
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100365Status 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 +0100366 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100367{
giuros016d109962019-01-07 17:47:19 +0000368 const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
369 const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
370
Usama Arife73686a2019-04-08 17:30:48 +0100371 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());
372 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());
373
giuros016d109962019-01-07 17:47:19 +0000374 const bool can_run_optimised_3x3_kernel = (weights->dimension(idx_w) == 3) && (weights->dimension(idx_h) == 3);
375
376 if(can_run_optimised_3x3_kernel)
Georgios Pinitas60e98252018-10-22 16:17:20 +0100377 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000378 const size_t idx_c = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
Georgios Pinitas60e98252018-10-22 16:17:20 +0100379
Pablo Tello8bf622a2018-12-03 15:54:49 +0000380 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
381 ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(idx_c) * depth_multiplier) != weights->dimension(idx_c));
382
383 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
384 const bool append_bias = (biases != nullptr) && !is_quantized;
Usama Arife73686a2019-04-08 17:30:48 +0100385 const TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);
Pablo Tello8bf622a2018-12-03 15:54:49 +0000386 const size_t weights_w = weights->dimension(idx_w);
387 const size_t weights_h = weights->dimension(idx_h);
388 const size_t weights_z = weights->dimension(idx_c);
389 const unsigned int conv_w = output_shape[idx_w];
390 const unsigned int conv_h = output_shape[idx_h];
391 const size_t patch_size = weights_w * weights_h + ((append_bias) ? 1 : 0);
392 const size_t conv_size = conv_w * conv_h;
393
394 TensorShape shape_im2col = input->tensor_shape();
395 shape_im2col.set(0, patch_size);
396 shape_im2col.set(1, conv_size);
397 shape_im2col.set(2, weights_z);
398 TensorInfo input_reshaped(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
Usama Arife73686a2019-04-08 17:30:48 +0100399 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseIm2ColKernel::validate(input, &input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier, dilation));
Pablo Tello8bf622a2018-12-03 15:54:49 +0000400
401 const TensorShape shape_weights_reshape(patch_size, weights_z);
402 TensorInfo weights_reshaped(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape));
giuros016d109962019-01-07 17:47:19 +0000403 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayerReshapeWeightsGenericKernel::validate(weights, &weights_reshaped, append_bias ? biases : nullptr));
Pablo Tello8bf622a2018-12-03 15:54:49 +0000404
405 DataType v2mm_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type();
406 TensorShape shape_v2mm_out = input->tensor_shape();
407 shape_v2mm_out.set(0, conv_size * weights_z);
408 shape_v2mm_out.set(1, 1);
409 shape_v2mm_out.set(2, 1);
410 TensorInfo v2mm_output(input->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out));
411 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixVectorMultiplyKernel::validate(&input_reshaped, &weights_reshaped, &v2mm_output));
412
413 TensorInfo output_reshaped(v2mm_output.clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
414 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseVectorToTensorKernel::validate(&v2mm_output, (is_quantized) ? &output_reshaped : output, conv_w, conv_h));
415
416 if(is_quantized)
417 {
418 ARM_COMPUTE_RETURN_ON_ERROR(CLDirectConvolutionLayerOutputStageKernel::validate(&output_reshaped, biases, output));
419 }
420
421 // Validate Activation Layer
422 if(act_info.enabled())
423 {
424 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info));
425 }
426 }
giuros016d109962019-01-07 17:47:19 +0000427 else
428 {
Usama Arife73686a2019-04-08 17:30:48 +0100429 CLDepthwiseConvolutionLayer3x3::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, GPUTarget::MIDGARD, dilation);
giuros016d109962019-01-07 17:47:19 +0000430 }
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100431 return Status{};
432}
433
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000434void CLDepthwiseConvolutionLayer::run()
Giorgio Arena93a690e2017-08-01 16:09:33 +0100435{
Georgios Pinitas72219332018-06-05 14:56:06 +0100436 prepare();
Georgios Pinitas1562be32018-03-08 19:09:19 +0000437
Pablo Tello8bf622a2018-12-03 15:54:49 +0000438 if(_optimised_function != nullptr)
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000439 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000440 _optimised_function->run();
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000441 }
Pablo Tello8bf622a2018-12-03 15:54:49 +0000442 else
Georgios Pinitas60e98252018-10-22 16:17:20 +0100443 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000444 CLScheduler::get().enqueue(_im2col_kernel);
445 CLScheduler::get().enqueue(_v2mm_input_fill_border);
446 CLScheduler::get().enqueue(_v2mm_kernel);
447 CLScheduler::get().enqueue(_vector_to_tensor_kernel);
448 if(_is_quantized)
449 {
450 CLScheduler::get().enqueue(_output_stage_kernel);
451 }
452 if(_is_activationlayer_enabled)
453 {
454 _activationlayer_function.run();
455 }
Georgios Pinitas60e98252018-10-22 16:17:20 +0100456 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100457}
Georgios Pinitas72219332018-06-05 14:56:06 +0100458
459void CLDepthwiseConvolutionLayer::prepare()
460{
Pablo Tello8bf622a2018-12-03 15:54:49 +0000461 if(_optimised_function != nullptr)
Georgios Pinitas72219332018-06-05 14:56:06 +0100462 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000463 _optimised_function->prepare();
464 }
465 else
466 {
467 if(!_is_prepared)
468 {
469 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
Georgios Pinitas72219332018-06-05 14:56:06 +0100470
Pablo Tello8bf622a2018-12-03 15:54:49 +0000471 // Run weights reshaping and mark original weights tensor as unused
472 _weights_reshaped.allocator()->allocate();
473 CLScheduler::get().enqueue(_weights_reshape_kernel);
474 CLScheduler::get().enqueue(_v2mm_weights_fill_border);
475 _original_weights->mark_as_unused();
Georgios Pinitas72219332018-06-05 14:56:06 +0100476
Pablo Tello8bf622a2018-12-03 15:54:49 +0000477 CLScheduler::get().queue().finish();
478 _is_prepared = true;
479 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100480 }
481}
giuros016d109962019-01-07 17:47:19 +0000482} // namespace arm_compute