blob: cdf3a955688660108d913a5611280ae6b5776235 [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
Manuel Bottini05069f02019-09-26 17:18:26 +010041namespace
42{
43Status validate_arguments_3x3(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
44 unsigned int depth_multiplier, ActivationLayerInfo act_info, GPUTarget gpu_target, const Size2D &dilation)
45{
46 // This function should be removed and incorporated inside CLDepthwiseConvolutionLayerInternal3x3 once CLDepthwiseConvolutionLayer3x3 is properly removed
47 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010048 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Manuel Bottini05069f02019-09-26 17:18:26 +010049 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
50
51 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
52 const bool is_nhwc = input->data_layout() == DataLayout::NHWC;
53 const bool needs_permute = is_nhwc && (depth_multiplier > 1);
54 const bool needs_weights_reshape = is_nhwc && (depth_multiplier == 1) && is_quantized;
55 const bool is_stride_1 = ((conv_info.stride().first == conv_info.stride().second) && (conv_info.stride().first == 1));
56 const bool is_stride_1_dilation_1 = (is_stride_1 && dilation.x() == 1 && dilation.y() == 1);
57 const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device());
58 DepthwiseConvolutionReshapeInfo info;
59 info.c0 = 4;
60 info.transpose = is_stride_1_dilation_1 && is_dot8_supported;
61
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010062 TensorInfo output_multipliers_shifts_info(TensorInfo(TensorShape(1U), 1, DataType::S32));
Manuel Bottini05069f02019-09-26 17:18:26 +010063 if(is_quantized)
64 {
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010065 if(is_data_type_quantized_per_channel(weights->data_type()))
66 {
67 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QSYMM8_PER_CHANNEL);
Manuel Bottini05069f02019-09-26 17:18:26 +010068
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010069 const size_t idx_c = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::CHANNEL);
70 output_multipliers_shifts_info.set_tensor_shape(TensorShape(weights->dimension(idx_c)));
71 }
72 else
73 {
74 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
75 }
Manuel Bottini05069f02019-09-26 17:18:26 +010076 }
77
78 if(needs_permute)
79 {
80 TensorShape permuted_input_shape = input->tensor_shape();
81 TensorShape permuted_weights_shape = weights->tensor_shape();
82 TensorShape permuted_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);
83
84 permute(permuted_input_shape, PermutationVector(1U, 2U, 0U));
85 permute(permuted_weights_shape, PermutationVector(1U, 2U, 0U));
86 permute(permuted_output_shape, PermutationVector(1U, 2U, 0U));
87
88 const TensorInfo permuted_input = input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NCHW);
89 const TensorInfo permuted_weights = weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NCHW);
90 const TensorInfo permuted_output = output->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_output_shape).set_data_layout(DataLayout::NCHW);
91
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010092 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NCHWKernel::validate(&permuted_input, &permuted_weights, biases, &permuted_output,
93 conv_info, depth_multiplier, act_info, gpu_target,
94 dilation, &output_multipliers_shifts_info, &output_multipliers_shifts_info));
Manuel Bottini05069f02019-09-26 17:18:26 +010095 }
96 else if(is_nhwc)
97 {
98 if(needs_weights_reshape)
99 {
100 auto reshaped_weights_shape = arm_compute::misc::shape_calculator::compute_reshaped_depthwise_weights_shape(*weights, info);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100101 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NHWCKernel::validate(input, &weights->clone()->set_tensor_shape(reshaped_weights_shape), biases,
102 output, conv_info, depth_multiplier, act_info,
103 dilation, &output_multipliers_shifts_info, &output_multipliers_shifts_info));
Manuel Bottini05069f02019-09-26 17:18:26 +0100104 }
105 else
106 {
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100107 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NHWCKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info,
108 dilation, &output_multipliers_shifts_info, &output_multipliers_shifts_info));
Manuel Bottini05069f02019-09-26 17:18:26 +0100109 }
110 }
111 else
112 {
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100113 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NCHWKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target,
114 dilation, &output_multipliers_shifts_info, &output_multipliers_shifts_info));
Manuel Bottini05069f02019-09-26 17:18:26 +0100115 }
116 return Status{};
117}
118} // namespace
119
Georgios Pinitas05045c12018-12-07 18:31:47 +0000120CLDepthwiseConvolutionLayer3x3::CLDepthwiseConvolutionLayer3x3(std::shared_ptr<IMemoryManager> memory_manager)
Manuel Bottini05069f02019-09-26 17:18:26 +0100121 : _func(std::move(memory_manager))
Giorgio Arena93a690e2017-08-01 16:09:33 +0100122{
123}
124
Giorgio Arena76572242018-04-04 17:44:26 +0100125void 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 +0100126 ActivationLayerInfo act_info, const Size2D &dilation)
Giorgio Arena9fe41442017-08-23 16:36:24 +0100127{
Manuel Bottini05069f02019-09-26 17:18:26 +0100128 _func.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
129}
130
131Status CLDepthwiseConvolutionLayer3x3::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
132 unsigned int depth_multiplier, ActivationLayerInfo act_info, GPUTarget gpu_target, const Size2D &dilation)
133{
134 return validate_arguments_3x3(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation);
135}
136
137void CLDepthwiseConvolutionLayer3x3::run()
138{
139 _func.run();
140}
141
142void CLDepthwiseConvolutionLayer3x3::prepare()
143{
144 _func.prepare();
145}
146
147CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::CLDepthwiseConvolutionLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager)
148 : _memory_group(std::move(memory_manager)),
149 _dwc_native_kernel(),
150 _permute_input_to_nhwc(),
151 _permute_weights_to_nhwc(),
152 _permute_output_to_nchw(),
153 _permuted_input(),
154 _permuted_weights(),
155 _permuted_output(),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100156 _output_multipliers(),
157 _output_shifts(),
Manuel Bottini05069f02019-09-26 17:18:26 +0100158 _original_weights(),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100159 _input(),
160 _output(),
Manuel Bottini05069f02019-09-26 17:18:26 +0100161 _needs_permute(false),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100162 _is_prepared(false),
163 _is_quantized(false)
Manuel Bottini05069f02019-09-26 17:18:26 +0100164{
165}
166
167void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
168 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
169{
170 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
171 ARM_COMPUTE_ERROR_THROW_ON(CLDepthwiseConvolutionLayer::validate(input->info(),
172 weights->info(),
173 biases != nullptr ? biases->info() : nullptr,
174 output->info(),
175 conv_info,
176 depth_multiplier,
177 act_info,
178 dilation));
179
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100180 _is_quantized = is_data_type_quantized(input->info()->data_type());
Manuel Bottini05069f02019-09-26 17:18:26 +0100181 _is_prepared = false;
182 _original_weights = weights;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100183 _input = input;
184 _output = output;
Manuel Bottini05069f02019-09-26 17:18:26 +0100185 _needs_permute = input->info()->data_layout() == DataLayout::NCHW;
186
187 ICLTensor *input_to_use = input;
188 const ICLTensor *weights_to_use = weights;
189 ICLTensor *output_to_use = output;
190 if(_needs_permute)
191 {
192 _memory_group.manage(&_permuted_input);
193 _memory_group.manage(&_permuted_output);
194
195 // Configure the function to transform the input tensor from NCHW -> NHWC
196 _permute_input_to_nhwc.configure(input, &_permuted_input, PermutationVector(2U, 0U, 1U));
197 _permuted_input.info()->set_data_layout(DataLayout::NHWC);
198
199 // Configure the function to transform the weights tensor from IHW -> HWI
200 _permute_weights_to_nhwc.configure(weights, &_permuted_weights, PermutationVector(2U, 0U, 1U));
201 _permuted_weights.info()->set_data_layout(DataLayout::NHWC);
202
203 // Set output quantization info before dwc kernel configure
204 _permuted_output.info()->set_quantization_info(output->info()->quantization_info());
205
206 input_to_use = &_permuted_input;
207 weights_to_use = &_permuted_weights;
208 output_to_use = &_permuted_output;
209 }
210
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100211 CLTensor *output_multipliers_to_use = nullptr;
212 CLTensor *output_shifts_to_use = nullptr;
213 if(_is_quantized)
214 {
215 const size_t idx_c = get_data_layout_dimension_index(weights->info()->data_layout(), DataLayoutDimension::CHANNEL);
216 const size_t num_filters = (is_data_type_quantized_per_channel(weights->info()->data_type())) ? weights->info()->dimension(idx_c) : 1;
217
218 _output_multipliers.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32));
219 _output_shifts.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32));
220
221 output_multipliers_to_use = &_output_multipliers;
222 output_shifts_to_use = &_output_shifts;
223 }
224
Manuel Bottini05069f02019-09-26 17:18:26 +0100225 DWCWeightsKernelInfo dwc_weights_info;
226 dwc_weights_info.n0 = (depth_multiplier == 1) ? 8 : 1;
227 DWCKernelInfo dwc_info;
228 dwc_info.activation_info = act_info;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100229 _dwc_native_kernel.configure(input_to_use, weights_to_use, biases, output_to_use,
230 dwc_weights_info, dwc_info, conv_info, depth_multiplier, dilation,
231 output_multipliers_to_use, output_shifts_to_use);
Manuel Bottini05069f02019-09-26 17:18:26 +0100232
233 if(_needs_permute)
234 {
235 _permuted_input.allocator()->allocate();
236
237 // Configure the function to transform the convoluted output to NCHW format
238 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
239 _permute_output_to_nchw.configure(&_permuted_output, output, PermutationVector(1U, 2U, 0U));
240 _permuted_output.allocator()->allocate();
241 }
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100242
243 if(_is_quantized)
244 {
245 _output_multipliers.allocator()->allocate();
246 _output_shifts.allocator()->allocate();
247 }
Manuel Bottini05069f02019-09-26 17:18:26 +0100248}
249
250Status CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
251 const PadStrideInfo &conv_info,
252 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
253{
254 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
255 const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
256 const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
257
258 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());
259 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());
260
261 DWCWeightsKernelInfo dwc_weights_info;
262 dwc_weights_info.n0 = (depth_multiplier == 1) ? 8 : 1;
263 DWCKernelInfo dwc_info;
264 dwc_info.activation_info = act_info;
265
266 const bool needs_permute = input->data_layout() == DataLayout::NCHW;
267
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100268 const bool is_quantized = is_data_type_quantized(input->data_type());
269
270 TensorInfo output_multipliers_shifts_info(TensorInfo(TensorShape(1U), 1, DataType::S32));
271 if(is_quantized)
272 {
273 if(is_data_type_quantized_per_channel(weights->data_type()))
274 {
275 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QSYMM8_PER_CHANNEL);
276
277 const size_t idx_c = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::CHANNEL);
278 output_multipliers_shifts_info.set_tensor_shape(TensorShape(weights->dimension(idx_c)));
279 }
280 else
281 {
282 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
283 }
284 }
285
Manuel Bottini05069f02019-09-26 17:18:26 +0100286 if(needs_permute)
287 {
288 TensorShape permuted_input_shape = input->tensor_shape();
289 TensorShape permuted_weights_shape = weights->tensor_shape();
290 TensorShape permuted_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);
291
292 permute(permuted_input_shape, PermutationVector(2U, 0U, 1U));
293 permute(permuted_weights_shape, PermutationVector(2U, 0U, 1U));
294 permute(permuted_output_shape, PermutationVector(2U, 0U, 1U));
295
296 const TensorInfo permuted_input = input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NHWC);
297 const TensorInfo permuted_weights = weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NHWC);
298 const TensorInfo permuted_output = output->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_output_shape).set_data_layout(DataLayout::NHWC);
299
300 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(input, &permuted_input, PermutationVector(2U, 0U, 1U)));
301 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(weights, &permuted_weights, PermutationVector(2U, 0U, 1U)));
302 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayerNativeKernel::validate(&permuted_input, &permuted_weights, biases, &permuted_output, dwc_weights_info,
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100303 dwc_info, conv_info, depth_multiplier, dilation,
304 &output_multipliers_shifts_info, &output_multipliers_shifts_info));
Manuel Bottini05069f02019-09-26 17:18:26 +0100305 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(&permuted_output, output, PermutationVector(1U, 2U, 0U)));
306 }
307 else
308 {
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100309 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayerNativeKernel::validate(input, weights, biases, output, dwc_weights_info, dwc_info, conv_info, depth_multiplier,
310 dilation, &output_multipliers_shifts_info, &output_multipliers_shifts_info));
Manuel Bottini05069f02019-09-26 17:18:26 +0100311 }
312 return Status{};
313}
314
315void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::run()
316{
317 prepare();
318
319 MemoryGroupResourceScope scope_mg(_memory_group);
320
321 if(_needs_permute)
322 {
323 _permute_input_to_nhwc.run();
324 }
325 CLScheduler::get().enqueue(_dwc_native_kernel);
326 if(_needs_permute)
327 {
328 _permute_output_to_nchw.run();
329 }
330}
331
332void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::prepare()
333{
334 if(!_is_prepared)
335 {
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100336 if(_is_quantized)
337 {
338 _output_multipliers.map();
339 _output_shifts.map();
340 quantization::compute_quantized_multipliers_and_shifts(_input,
341 _original_weights,
342 _output,
343 reinterpret_cast<int32_t *>(_output_multipliers.ptr_to_element(Coordinates(0))),
344 reinterpret_cast<int32_t *>(_output_shifts.ptr_to_element(Coordinates(0))));
345 _output_multipliers.unmap();
346 _output_shifts.unmap();
347 }
348
Manuel Bottini05069f02019-09-26 17:18:26 +0100349 if(_needs_permute)
350 {
351 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
352
353 _permuted_weights.allocator()->allocate();
354 _permute_weights_to_nhwc.run();
355 _original_weights->mark_as_unused();
356 }
357 _is_prepared = true;
358 }
359}
360
361CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::CLDepthwiseConvolutionLayerInternal3x3(std::shared_ptr<IMemoryManager> memory_manager)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100362 : _memory_group(std::move(memory_manager)),
363 _kernel(nullptr),
364 _border_handler(),
365 _permute_input_to_nchw(),
366 _permute_weights_to_nchw(),
367 _permute_output_to_nhwc(),
368 _reshape_weights(),
369 _permuted_input(),
370 _permuted_weights(),
371 _permuted_output(),
372 _output_multipliers(),
373 _output_shifts(),
374 _original_weights(nullptr),
375 _input(nullptr),
376 _output(nullptr),
377 _needs_permute(false),
378 _needs_weights_reshape(false),
379 _is_prepared(false),
380 _is_quantized(false)
Manuel Bottini05069f02019-09-26 17:18:26 +0100381{
382}
383
384void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
385 const PadStrideInfo &conv_info, unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
386{
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100387 const GPUTarget gpu_target = CLScheduler::get().target();
Usama Arif881f2de2019-04-12 10:29:17 +0100388
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100389 // Perform validation step
390 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
391 ARM_COMPUTE_ERROR_THROW_ON(CLDepthwiseConvolutionLayer3x3::validate(input->info(),
392 weights->info(),
393 biases != nullptr ? biases->info() : nullptr,
394 output->info(),
395 conv_info,
396 depth_multiplier,
397 act_info,
398 gpu_target,
399 dilation));
Giorgio Arena9fe41442017-08-23 16:36:24 +0100400
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100401 const bool is_nhwc = input->info()->data_layout() == DataLayout::NHWC;
402 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
giuros016d109962019-01-07 17:47:19 +0000403 _needs_permute = is_nhwc && (depth_multiplier > 1);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100404 _needs_weights_reshape = is_nhwc && (depth_multiplier == 1) && _is_quantized;
405
Georgios Pinitas05045c12018-12-07 18:31:47 +0000406 _is_prepared = false;
407 _original_weights = weights;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100408 _input = input;
409 _output = output;
Georgios Pinitas05045c12018-12-07 18:31:47 +0000410
411 ICLTensor *input_to_use = input;
412 const ICLTensor *weights_to_use = weights;
413 ICLTensor *output_to_use = output;
414
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100415 const bool is_quantized_per_channel = is_data_type_quantized_per_channel(weights->info()->data_type());
416 const bool is_stride_1 = ((conv_info.stride().first == conv_info.stride().second) && (conv_info.stride().first == 1));
417 const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device()) && !is_quantized_per_channel;
418 const bool is_stride_1_dilation_1 = (is_stride_1 && dilation.x() == 1 && dilation.y() == 1);
Usama Arife73686a2019-04-08 17:30:48 +0100419
giuros016d109962019-01-07 17:47:19 +0000420 DepthwiseConvolutionReshapeInfo info;
421 info.c0 = 4;
Usama Arife73686a2019-04-08 17:30:48 +0100422 info.transpose = is_stride_1_dilation_1 && is_dot8_supported;
giuros016d109962019-01-07 17:47:19 +0000423
Georgios Pinitas05045c12018-12-07 18:31:47 +0000424 if(_needs_permute)
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000425 {
Georgios Pinitas05045c12018-12-07 18:31:47 +0000426 _memory_group.manage(&_permuted_input);
427 _memory_group.manage(&_permuted_output);
428
429 // Configure the function to transform the input tensor from NHWC -> NCHW
430 _permute_input_to_nchw.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
431 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
432
433 // Configure the function to transform the weights tensor from HWI -> IHW
434 _permute_weights_to_nchw.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
435 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
Pablo Telloa28aebc2019-06-03 14:59:48 +0100436 _permuted_output.info()->set_quantization_info(output->info()->quantization_info());
Georgios Pinitas05045c12018-12-07 18:31:47 +0000437
438 input_to_use = &_permuted_input;
439 weights_to_use = &_permuted_weights;
440 output_to_use = &_permuted_output;
441
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000442 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>();
443 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000444 else if(is_nhwc)
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000445 {
giuros016d109962019-01-07 17:47:19 +0000446 if(_needs_weights_reshape)
447 {
448 _reshape_weights.configure(weights, &_permuted_weights, info);
449 weights_to_use = &_permuted_weights;
450 }
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000451 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NHWCKernel>();
452 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000453 else
454 {
455 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>();
456 }
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000457
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100458 CLTensor *output_multipliers_to_use = nullptr;
459 CLTensor *output_shifts_to_use = nullptr;
460 if(_is_quantized)
461 {
462 const size_t idx_c = get_data_layout_dimension_index(weights->info()->data_layout(), DataLayoutDimension::CHANNEL);
463 const size_t num_filters = (is_quantized_per_channel) ? weights->info()->dimension(idx_c) : 1;
464
465 _output_multipliers.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32));
466 _output_shifts.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32));
467
468 output_multipliers_to_use = &_output_multipliers;
469 output_shifts_to_use = &_output_shifts;
470 }
471
Georgios Pinitas05045c12018-12-07 18:31:47 +0000472 // Configure kernel
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100473 _kernel->set_target(gpu_target);
474 _kernel->configure(input_to_use, weights_to_use, biases, output_to_use, conv_info, depth_multiplier,
475 act_info, dilation, output_multipliers_to_use, output_shifts_to_use);
476
477 if(_is_quantized)
478 {
479 _output_multipliers.allocator()->allocate();
480 _output_shifts.allocator()->allocate();
481 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000482
483 // Permute output if needed
484 if(_needs_permute)
485 {
486 // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
Georgios Pinitas3f8aac42018-12-24 13:09:02 +0000487 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
Georgios Pinitas05045c12018-12-07 18:31:47 +0000488 _permute_output_to_nhwc.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
489
490 // Allocate tensors
491 _permuted_input.allocator()->allocate();
492 _permuted_output.allocator()->allocate();
493 }
Diego Lopez Recasfa0add12017-11-28 16:44:52 +0000494 // Configure border handler
495 PixelValue &&zero_value(0.f);
496 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
497 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100498 zero_value = PixelValue(static_cast<uint8_t>(input->info()->quantization_info().uniform().offset));
Diego Lopez Recasfa0add12017-11-28 16:44:52 +0000499 }
Georgios Pinitas3f8aac42018-12-24 13:09:02 +0000500 _border_handler.configure(input_to_use, _kernel->border_size(), BorderMode::CONSTANT, zero_value);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100501}
502
Manuel Bottini05069f02019-09-26 17:18:26 +0100503Status CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
504 const PadStrideInfo &conv_info, unsigned int depth_multiplier, ActivationLayerInfo act_info, GPUTarget gpu_target, const Size2D &dilation)
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100505{
Manuel Bottini05069f02019-09-26 17:18:26 +0100506 return validate_arguments_3x3(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation);
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100507}
508
Manuel Bottini05069f02019-09-26 17:18:26 +0100509void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::run()
Giorgio Arena9fe41442017-08-23 16:36:24 +0100510{
Georgios Pinitas05045c12018-12-07 18:31:47 +0000511 prepare();
512
Georgios Pinitasda953f22019-04-02 17:27:03 +0100513 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas05045c12018-12-07 18:31:47 +0000514
515 if(_needs_permute)
516 {
517 _permute_input_to_nchw.run();
518 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100519 CLScheduler::get().enqueue(_border_handler);
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000520 CLScheduler::get().enqueue(*_kernel);
Georgios Pinitas05045c12018-12-07 18:31:47 +0000521
522 if(_needs_permute)
523 {
524 _permute_output_to_nhwc.run();
525 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000526}
527
Manuel Bottini05069f02019-09-26 17:18:26 +0100528void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::prepare()
Georgios Pinitas05045c12018-12-07 18:31:47 +0000529{
530 if(!_is_prepared)
531 {
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100532 if(_is_quantized)
533 {
534 _output_multipliers.map();
535 _output_shifts.map();
536 quantization::compute_quantized_multipliers_and_shifts(_input,
537 _original_weights,
538 _output,
539 reinterpret_cast<int32_t *>(_output_multipliers.ptr_to_element(Coordinates(0))),
540 reinterpret_cast<int32_t *>(_output_shifts.ptr_to_element(Coordinates(0))));
541 _output_multipliers.unmap();
542 _output_shifts.unmap();
543 }
544
Georgios Pinitas05045c12018-12-07 18:31:47 +0000545 if(_needs_permute)
546 {
547 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
548
549 _permuted_weights.allocator()->allocate();
550 _permute_weights_to_nchw.run();
551 _original_weights->mark_as_unused();
552 }
giuros016d109962019-01-07 17:47:19 +0000553
554 if(_needs_weights_reshape)
555 {
556 ARM_COMPUTE_ERROR_ON(_needs_permute);
557 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
558 _permuted_weights.allocator()->allocate();
559 CLScheduler::get().enqueue(_reshape_weights);
560 _original_weights->mark_as_unused();
561 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000562 _is_prepared = true;
563 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100564}
565
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100566CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Manuel Bottini05069f02019-09-26 17:18:26 +0100567 : _memory_manager(std::move(memory_manager)), _depth_conv_func(DepthwiseConvolutionFunction::GENERIC), _func_3x3(), _func_generic()
Giorgio Arena9fe41442017-08-23 16:36:24 +0100568{
569}
570
Manuel Bottini05069f02019-09-26 17:18:26 +0100571void CLDepthwiseConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier,
572 ActivationLayerInfo act_info, const Size2D &dilation)
Giorgio Arena93a690e2017-08-01 16:09:33 +0100573{
Manuel Bottini05069f02019-09-26 17:18:26 +0100574 const GPUTarget gpu_target = CLScheduler::get().target();
575 _depth_conv_func = get_depthwiseconvolution_function(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(), conv_info, depth_multiplier, act_info,
576 dilation, gpu_target);
577 switch(_depth_conv_func)
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000578 {
Manuel Bottini05069f02019-09-26 17:18:26 +0100579 case DepthwiseConvolutionFunction::OPTIMIZED:
580 _func_3x3.set_memory_group(_memory_manager);
581 _func_3x3.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
582 break;
583 case DepthwiseConvolutionFunction::GENERIC:
Pablo Tello8bf622a2018-12-03 15:54:49 +0000584 {
Manuel Bottini05069f02019-09-26 17:18:26 +0100585 _func_generic.set_memory_group(_memory_manager);
586 _func_generic.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
Pablo Tello8bf622a2018-12-03 15:54:49 +0000587 }
Manuel Bottini05069f02019-09-26 17:18:26 +0100588 break;
589 default:
590 ARM_COMPUTE_ERROR("Unsupported DepthwiseConvolutionFunction");
Georgios Pinitas60e98252018-10-22 16:17:20 +0100591 }
Giorgio Arena93a690e2017-08-01 16:09:33 +0100592}
593
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100594Status CLDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Manuel Bottini05069f02019-09-26 17:18:26 +0100595 unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100596{
Manuel Bottini05069f02019-09-26 17:18:26 +0100597 const GPUTarget gpu_target = CLScheduler::get().target();
598 DepthwiseConvolutionFunction depth_conv_func = get_depthwiseconvolution_function(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation, gpu_target);
599 switch(depth_conv_func)
Georgios Pinitas60e98252018-10-22 16:17:20 +0100600 {
Manuel Bottini05069f02019-09-26 17:18:26 +0100601 case DepthwiseConvolutionFunction::OPTIMIZED:
602 return CLDepthwiseConvolutionLayerInternal3x3::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation);
603 case DepthwiseConvolutionFunction::GENERIC:
604 return CLDepthwiseConvolutionLayerGeneric::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
605 default:
606 ARM_COMPUTE_ERROR("Unsupported DepthwiseConvolutionFunction");
607 }
608}
Georgios Pinitas60e98252018-10-22 16:17:20 +0100609
Manuel Bottini05069f02019-09-26 17:18:26 +0100610DepthwiseConvolutionFunction CLDepthwiseConvolutionLayer::get_depthwiseconvolution_function(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
611 const PadStrideInfo &conv_info,
612 unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation, GPUTarget gpu_target)
613{
614 if(bool(CLDepthwiseConvolutionLayerInternal3x3::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation)) && (is_data_type_float(input->data_type())
615 || get_arch_from_target(gpu_target) == GPUTarget::MIDGARD))
616 {
617 return DepthwiseConvolutionFunction::OPTIMIZED;
Pablo Tello8bf622a2018-12-03 15:54:49 +0000618 }
giuros016d109962019-01-07 17:47:19 +0000619 else
620 {
Manuel Bottini05069f02019-09-26 17:18:26 +0100621 return DepthwiseConvolutionFunction::GENERIC;
giuros016d109962019-01-07 17:47:19 +0000622 }
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100623}
624
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000625void CLDepthwiseConvolutionLayer::run()
Giorgio Arena93a690e2017-08-01 16:09:33 +0100626{
Manuel Bottini05069f02019-09-26 17:18:26 +0100627 switch(_depth_conv_func)
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000628 {
Manuel Bottini05069f02019-09-26 17:18:26 +0100629 case DepthwiseConvolutionFunction::OPTIMIZED:
630 _func_3x3.run();
631 break;
632 case DepthwiseConvolutionFunction::GENERIC:
633 _func_generic.run();
634 break;
635 default:
636 ARM_COMPUTE_ERROR("DepthwiseConvolutionFunction not properly configured");
Georgios Pinitas60e98252018-10-22 16:17:20 +0100637 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100638}
Georgios Pinitas72219332018-06-05 14:56:06 +0100639
640void CLDepthwiseConvolutionLayer::prepare()
641{
Manuel Bottini05069f02019-09-26 17:18:26 +0100642 switch(_depth_conv_func)
Georgios Pinitas72219332018-06-05 14:56:06 +0100643 {
Manuel Bottini05069f02019-09-26 17:18:26 +0100644 case DepthwiseConvolutionFunction::OPTIMIZED:
645 _func_3x3.prepare();
646 break;
647 case DepthwiseConvolutionFunction::GENERIC:
648 _func_generic.prepare();
649 break;
650 default:
651 ARM_COMPUTE_ERROR("DepthwiseConvolutionFunction not properly configured");
Georgios Pinitas72219332018-06-05 14:56:06 +0100652 }
653}
giuros016d109962019-01-07 17:47:19 +0000654} // namespace arm_compute