blob: e717f793fd6703c1a719d2de0032c76f21cae0cc [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();
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000340 const unsigned int idx_ofms = get_data_layout_dimension_index(_output->info()->data_layout(), DataLayoutDimension::CHANNEL);
341 quantization::compute_quantized_multipliers_and_shifts(_input->info(),
342 _original_weights->info(),
343 _output->info(),
344 idx_ofms,
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100345 reinterpret_cast<int32_t *>(_output_multipliers.ptr_to_element(Coordinates(0))),
346 reinterpret_cast<int32_t *>(_output_shifts.ptr_to_element(Coordinates(0))));
347 _output_multipliers.unmap();
348 _output_shifts.unmap();
349 }
350
Manuel Bottini05069f02019-09-26 17:18:26 +0100351 if(_needs_permute)
352 {
353 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
354
355 _permuted_weights.allocator()->allocate();
356 _permute_weights_to_nhwc.run();
357 _original_weights->mark_as_unused();
358 }
359 _is_prepared = true;
360 }
361}
362
363CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::CLDepthwiseConvolutionLayerInternal3x3(std::shared_ptr<IMemoryManager> memory_manager)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100364 : _memory_group(std::move(memory_manager)),
365 _kernel(nullptr),
366 _border_handler(),
367 _permute_input_to_nchw(),
368 _permute_weights_to_nchw(),
369 _permute_output_to_nhwc(),
370 _reshape_weights(),
371 _permuted_input(),
372 _permuted_weights(),
373 _permuted_output(),
374 _output_multipliers(),
375 _output_shifts(),
376 _original_weights(nullptr),
377 _input(nullptr),
378 _output(nullptr),
379 _needs_permute(false),
380 _needs_weights_reshape(false),
381 _is_prepared(false),
382 _is_quantized(false)
Manuel Bottini05069f02019-09-26 17:18:26 +0100383{
384}
385
386void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
387 const PadStrideInfo &conv_info, unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
388{
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100389 const GPUTarget gpu_target = CLScheduler::get().target();
Usama Arif881f2de2019-04-12 10:29:17 +0100390
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100391 // Perform validation step
392 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
393 ARM_COMPUTE_ERROR_THROW_ON(CLDepthwiseConvolutionLayer3x3::validate(input->info(),
394 weights->info(),
395 biases != nullptr ? biases->info() : nullptr,
396 output->info(),
397 conv_info,
398 depth_multiplier,
399 act_info,
400 gpu_target,
401 dilation));
Giorgio Arena9fe41442017-08-23 16:36:24 +0100402
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100403 const bool is_nhwc = input->info()->data_layout() == DataLayout::NHWC;
404 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
giuros016d109962019-01-07 17:47:19 +0000405 _needs_permute = is_nhwc && (depth_multiplier > 1);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100406 _needs_weights_reshape = is_nhwc && (depth_multiplier == 1) && _is_quantized;
407
Georgios Pinitas05045c12018-12-07 18:31:47 +0000408 _is_prepared = false;
409 _original_weights = weights;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100410 _input = input;
411 _output = output;
Georgios Pinitas05045c12018-12-07 18:31:47 +0000412
413 ICLTensor *input_to_use = input;
414 const ICLTensor *weights_to_use = weights;
415 ICLTensor *output_to_use = output;
416
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100417 const bool is_quantized_per_channel = is_data_type_quantized_per_channel(weights->info()->data_type());
418 const bool is_stride_1 = ((conv_info.stride().first == conv_info.stride().second) && (conv_info.stride().first == 1));
419 const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device()) && !is_quantized_per_channel;
420 const bool is_stride_1_dilation_1 = (is_stride_1 && dilation.x() == 1 && dilation.y() == 1);
Usama Arife73686a2019-04-08 17:30:48 +0100421
giuros016d109962019-01-07 17:47:19 +0000422 DepthwiseConvolutionReshapeInfo info;
423 info.c0 = 4;
Usama Arife73686a2019-04-08 17:30:48 +0100424 info.transpose = is_stride_1_dilation_1 && is_dot8_supported;
giuros016d109962019-01-07 17:47:19 +0000425
Georgios Pinitas05045c12018-12-07 18:31:47 +0000426 if(_needs_permute)
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000427 {
Georgios Pinitas05045c12018-12-07 18:31:47 +0000428 _memory_group.manage(&_permuted_input);
429 _memory_group.manage(&_permuted_output);
430
431 // Configure the function to transform the input tensor from NHWC -> NCHW
432 _permute_input_to_nchw.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
433 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
434
435 // Configure the function to transform the weights tensor from HWI -> IHW
436 _permute_weights_to_nchw.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
437 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
Pablo Telloa28aebc2019-06-03 14:59:48 +0100438 _permuted_output.info()->set_quantization_info(output->info()->quantization_info());
Georgios Pinitas05045c12018-12-07 18:31:47 +0000439
440 input_to_use = &_permuted_input;
441 weights_to_use = &_permuted_weights;
442 output_to_use = &_permuted_output;
443
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000444 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>();
445 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000446 else if(is_nhwc)
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000447 {
giuros016d109962019-01-07 17:47:19 +0000448 if(_needs_weights_reshape)
449 {
450 _reshape_weights.configure(weights, &_permuted_weights, info);
451 weights_to_use = &_permuted_weights;
452 }
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000453 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NHWCKernel>();
454 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000455 else
456 {
457 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>();
458 }
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000459
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100460 CLTensor *output_multipliers_to_use = nullptr;
461 CLTensor *output_shifts_to_use = nullptr;
462 if(_is_quantized)
463 {
464 const size_t idx_c = get_data_layout_dimension_index(weights->info()->data_layout(), DataLayoutDimension::CHANNEL);
465 const size_t num_filters = (is_quantized_per_channel) ? weights->info()->dimension(idx_c) : 1;
466
467 _output_multipliers.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32));
468 _output_shifts.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32));
469
470 output_multipliers_to_use = &_output_multipliers;
471 output_shifts_to_use = &_output_shifts;
472 }
473
Georgios Pinitas05045c12018-12-07 18:31:47 +0000474 // Configure kernel
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100475 _kernel->set_target(gpu_target);
476 _kernel->configure(input_to_use, weights_to_use, biases, output_to_use, conv_info, depth_multiplier,
477 act_info, dilation, output_multipliers_to_use, output_shifts_to_use);
478
479 if(_is_quantized)
480 {
481 _output_multipliers.allocator()->allocate();
482 _output_shifts.allocator()->allocate();
483 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000484
485 // Permute output if needed
486 if(_needs_permute)
487 {
488 // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
Georgios Pinitas3f8aac42018-12-24 13:09:02 +0000489 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
Georgios Pinitas05045c12018-12-07 18:31:47 +0000490 _permute_output_to_nhwc.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
491
492 // Allocate tensors
493 _permuted_input.allocator()->allocate();
494 _permuted_output.allocator()->allocate();
495 }
Diego Lopez Recasfa0add12017-11-28 16:44:52 +0000496 // Configure border handler
497 PixelValue &&zero_value(0.f);
498 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
499 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100500 zero_value = PixelValue(static_cast<uint8_t>(input->info()->quantization_info().uniform().offset));
Diego Lopez Recasfa0add12017-11-28 16:44:52 +0000501 }
Georgios Pinitas3f8aac42018-12-24 13:09:02 +0000502 _border_handler.configure(input_to_use, _kernel->border_size(), BorderMode::CONSTANT, zero_value);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100503}
504
Manuel Bottini05069f02019-09-26 17:18:26 +0100505Status CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
506 const PadStrideInfo &conv_info, unsigned int depth_multiplier, ActivationLayerInfo act_info, GPUTarget gpu_target, const Size2D &dilation)
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100507{
Manuel Bottini05069f02019-09-26 17:18:26 +0100508 return validate_arguments_3x3(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation);
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100509}
510
Manuel Bottini05069f02019-09-26 17:18:26 +0100511void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::run()
Giorgio Arena9fe41442017-08-23 16:36:24 +0100512{
Georgios Pinitas05045c12018-12-07 18:31:47 +0000513 prepare();
514
Georgios Pinitasda953f22019-04-02 17:27:03 +0100515 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas05045c12018-12-07 18:31:47 +0000516
517 if(_needs_permute)
518 {
519 _permute_input_to_nchw.run();
520 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100521 CLScheduler::get().enqueue(_border_handler);
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000522 CLScheduler::get().enqueue(*_kernel);
Georgios Pinitas05045c12018-12-07 18:31:47 +0000523
524 if(_needs_permute)
525 {
526 _permute_output_to_nhwc.run();
527 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000528}
529
Manuel Bottini05069f02019-09-26 17:18:26 +0100530void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::prepare()
Georgios Pinitas05045c12018-12-07 18:31:47 +0000531{
532 if(!_is_prepared)
533 {
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100534 if(_is_quantized)
535 {
536 _output_multipliers.map();
537 _output_shifts.map();
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000538 const unsigned int idx_ofms = get_data_layout_dimension_index(_output->info()->data_layout(), DataLayoutDimension::CHANNEL);
539 quantization::compute_quantized_multipliers_and_shifts(_input->info(),
540 _original_weights->info(),
541 _output->info(),
542 idx_ofms,
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100543 reinterpret_cast<int32_t *>(_output_multipliers.ptr_to_element(Coordinates(0))),
544 reinterpret_cast<int32_t *>(_output_shifts.ptr_to_element(Coordinates(0))));
545 _output_multipliers.unmap();
546 _output_shifts.unmap();
547 }
548
Georgios Pinitas05045c12018-12-07 18:31:47 +0000549 if(_needs_permute)
550 {
551 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
552
553 _permuted_weights.allocator()->allocate();
554 _permute_weights_to_nchw.run();
555 _original_weights->mark_as_unused();
556 }
giuros016d109962019-01-07 17:47:19 +0000557
558 if(_needs_weights_reshape)
559 {
560 ARM_COMPUTE_ERROR_ON(_needs_permute);
561 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
562 _permuted_weights.allocator()->allocate();
563 CLScheduler::get().enqueue(_reshape_weights);
564 _original_weights->mark_as_unused();
565 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000566 _is_prepared = true;
567 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100568}
569
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100570CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Manuel Bottini05069f02019-09-26 17:18:26 +0100571 : _memory_manager(std::move(memory_manager)), _depth_conv_func(DepthwiseConvolutionFunction::GENERIC), _func_3x3(), _func_generic()
Giorgio Arena9fe41442017-08-23 16:36:24 +0100572{
573}
574
Manuel Bottini05069f02019-09-26 17:18:26 +0100575void CLDepthwiseConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier,
576 ActivationLayerInfo act_info, const Size2D &dilation)
Giorgio Arena93a690e2017-08-01 16:09:33 +0100577{
Manuel Bottini05069f02019-09-26 17:18:26 +0100578 const GPUTarget gpu_target = CLScheduler::get().target();
579 _depth_conv_func = get_depthwiseconvolution_function(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(), conv_info, depth_multiplier, act_info,
580 dilation, gpu_target);
581 switch(_depth_conv_func)
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000582 {
Manuel Bottini05069f02019-09-26 17:18:26 +0100583 case DepthwiseConvolutionFunction::OPTIMIZED:
584 _func_3x3.set_memory_group(_memory_manager);
585 _func_3x3.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
586 break;
587 case DepthwiseConvolutionFunction::GENERIC:
Pablo Tello8bf622a2018-12-03 15:54:49 +0000588 {
Manuel Bottini05069f02019-09-26 17:18:26 +0100589 _func_generic.set_memory_group(_memory_manager);
590 _func_generic.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
Pablo Tello8bf622a2018-12-03 15:54:49 +0000591 }
Manuel Bottini05069f02019-09-26 17:18:26 +0100592 break;
593 default:
594 ARM_COMPUTE_ERROR("Unsupported DepthwiseConvolutionFunction");
Georgios Pinitas60e98252018-10-22 16:17:20 +0100595 }
Giorgio Arena93a690e2017-08-01 16:09:33 +0100596}
597
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100598Status 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 +0100599 unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100600{
Manuel Bottini05069f02019-09-26 17:18:26 +0100601 const GPUTarget gpu_target = CLScheduler::get().target();
602 DepthwiseConvolutionFunction depth_conv_func = get_depthwiseconvolution_function(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation, gpu_target);
603 switch(depth_conv_func)
Georgios Pinitas60e98252018-10-22 16:17:20 +0100604 {
Manuel Bottini05069f02019-09-26 17:18:26 +0100605 case DepthwiseConvolutionFunction::OPTIMIZED:
606 return CLDepthwiseConvolutionLayerInternal3x3::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation);
607 case DepthwiseConvolutionFunction::GENERIC:
608 return CLDepthwiseConvolutionLayerGeneric::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
609 default:
610 ARM_COMPUTE_ERROR("Unsupported DepthwiseConvolutionFunction");
611 }
612}
Georgios Pinitas60e98252018-10-22 16:17:20 +0100613
Manuel Bottini05069f02019-09-26 17:18:26 +0100614DepthwiseConvolutionFunction CLDepthwiseConvolutionLayer::get_depthwiseconvolution_function(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
615 const PadStrideInfo &conv_info,
616 unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation, GPUTarget gpu_target)
617{
618 if(bool(CLDepthwiseConvolutionLayerInternal3x3::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation)) && (is_data_type_float(input->data_type())
619 || get_arch_from_target(gpu_target) == GPUTarget::MIDGARD))
620 {
621 return DepthwiseConvolutionFunction::OPTIMIZED;
Pablo Tello8bf622a2018-12-03 15:54:49 +0000622 }
giuros016d109962019-01-07 17:47:19 +0000623 else
624 {
Manuel Bottini05069f02019-09-26 17:18:26 +0100625 return DepthwiseConvolutionFunction::GENERIC;
giuros016d109962019-01-07 17:47:19 +0000626 }
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100627}
628
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000629void CLDepthwiseConvolutionLayer::run()
Giorgio Arena93a690e2017-08-01 16:09:33 +0100630{
Manuel Bottini05069f02019-09-26 17:18:26 +0100631 switch(_depth_conv_func)
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000632 {
Manuel Bottini05069f02019-09-26 17:18:26 +0100633 case DepthwiseConvolutionFunction::OPTIMIZED:
634 _func_3x3.run();
635 break;
636 case DepthwiseConvolutionFunction::GENERIC:
637 _func_generic.run();
638 break;
639 default:
640 ARM_COMPUTE_ERROR("DepthwiseConvolutionFunction not properly configured");
Georgios Pinitas60e98252018-10-22 16:17:20 +0100641 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100642}
Georgios Pinitas72219332018-06-05 14:56:06 +0100643
644void CLDepthwiseConvolutionLayer::prepare()
645{
Manuel Bottini05069f02019-09-26 17:18:26 +0100646 switch(_depth_conv_func)
Georgios Pinitas72219332018-06-05 14:56:06 +0100647 {
Manuel Bottini05069f02019-09-26 17:18:26 +0100648 case DepthwiseConvolutionFunction::OPTIMIZED:
649 _func_3x3.prepare();
650 break;
651 case DepthwiseConvolutionFunction::GENERIC:
652 _func_generic.prepare();
653 break;
654 default:
655 ARM_COMPUTE_ERROR("DepthwiseConvolutionFunction not properly configured");
Georgios Pinitas72219332018-06-05 14:56:06 +0100656 }
657}
giuros016d109962019-01-07 17:47:19 +0000658} // namespace arm_compute