blob: 8d2c81bc157cb383d39b57fe924d819591ad54df [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 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"
Georgios Pinitas05045c12018-12-07 18:31:47 +000027#include "arm_compute/core/Helpers.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010028#include "arm_compute/core/PixelValue.h"
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000030#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010031#include "arm_compute/runtime/CL/CLScheduler.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010032#include "src/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NCHWKernel.h"
33#include "src/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NCHWKernel.h"
34#include "src/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NHWCKernel.h"
35#include "src/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NHWCKernel.h"
36#include "src/core/CL/kernels/CLDepthwiseConvolutionLayerNativeKernel.h"
37#include "src/core/CL/kernels/CLDepthwiseConvolutionLayerReshapeWeightsKernel.h"
38#include "src/core/CL/kernels/CLFillBorderKernel.h"
39#include "src/core/CL/kernels/ICLDepthwiseConvolutionLayer3x3Kernel.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010040
giuros016d109962019-01-07 17:47:19 +000041namespace arm_compute
42{
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000043using namespace arm_compute::misc;
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +000044using namespace arm_compute::misc::shape_calculator;
Giorgio Arena93a690e2017-08-01 16:09:33 +010045
Manuel Bottini05069f02019-09-26 17:18:26 +010046namespace
47{
48Status validate_arguments_3x3(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
49 unsigned int depth_multiplier, ActivationLayerInfo act_info, GPUTarget gpu_target, const Size2D &dilation)
50{
51 // This function should be removed and incorporated inside CLDepthwiseConvolutionLayerInternal3x3 once CLDepthwiseConvolutionLayer3x3 is properly removed
52 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010053 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 +010054 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
55
56 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
57 const bool is_nhwc = input->data_layout() == DataLayout::NHWC;
58 const bool needs_permute = is_nhwc && (depth_multiplier > 1);
59 const bool needs_weights_reshape = is_nhwc && (depth_multiplier == 1) && is_quantized;
60 const bool is_stride_1 = ((conv_info.stride().first == conv_info.stride().second) && (conv_info.stride().first == 1));
61 const bool is_stride_1_dilation_1 = (is_stride_1 && dilation.x() == 1 && dilation.y() == 1);
62 const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device());
63 DepthwiseConvolutionReshapeInfo info;
64 info.c0 = 4;
65 info.transpose = is_stride_1_dilation_1 && is_dot8_supported;
66
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010067 TensorInfo output_multipliers_shifts_info(TensorInfo(TensorShape(1U), 1, DataType::S32));
Manuel Bottini05069f02019-09-26 17:18:26 +010068 if(is_quantized)
69 {
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010070 if(is_data_type_quantized_per_channel(weights->data_type()))
71 {
72 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QSYMM8_PER_CHANNEL);
Manuel Bottini05069f02019-09-26 17:18:26 +010073
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010074 const size_t idx_c = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::CHANNEL);
75 output_multipliers_shifts_info.set_tensor_shape(TensorShape(weights->dimension(idx_c)));
76 }
77 else
78 {
79 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
80 }
Manuel Bottini05069f02019-09-26 17:18:26 +010081 }
82
83 if(needs_permute)
84 {
85 TensorShape permuted_input_shape = input->tensor_shape();
86 TensorShape permuted_weights_shape = weights->tensor_shape();
87 TensorShape permuted_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);
88
89 permute(permuted_input_shape, PermutationVector(1U, 2U, 0U));
90 permute(permuted_weights_shape, PermutationVector(1U, 2U, 0U));
91 permute(permuted_output_shape, PermutationVector(1U, 2U, 0U));
92
93 const TensorInfo permuted_input = input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NCHW);
94 const TensorInfo permuted_weights = weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NCHW);
95 const TensorInfo permuted_output = output->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_output_shape).set_data_layout(DataLayout::NCHW);
96
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010097 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NCHWKernel::validate(&permuted_input, &permuted_weights, biases, &permuted_output,
98 conv_info, depth_multiplier, act_info, gpu_target,
99 dilation, &output_multipliers_shifts_info, &output_multipliers_shifts_info));
Manuel Bottini05069f02019-09-26 17:18:26 +0100100 }
101 else if(is_nhwc)
102 {
103 if(needs_weights_reshape)
104 {
105 auto reshaped_weights_shape = arm_compute::misc::shape_calculator::compute_reshaped_depthwise_weights_shape(*weights, info);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100106 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NHWCKernel::validate(input, &weights->clone()->set_tensor_shape(reshaped_weights_shape), biases,
107 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 else
111 {
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100112 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NHWCKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info,
113 dilation, &output_multipliers_shifts_info, &output_multipliers_shifts_info));
Manuel Bottini05069f02019-09-26 17:18:26 +0100114 }
115 }
116 else
117 {
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100118 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NCHWKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target,
119 dilation, &output_multipliers_shifts_info, &output_multipliers_shifts_info));
Manuel Bottini05069f02019-09-26 17:18:26 +0100120 }
121 return Status{};
122}
123} // namespace
124
Manuel Bottini05069f02019-09-26 17:18:26 +0100125CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::CLDepthwiseConvolutionLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager)
126 : _memory_group(std::move(memory_manager)),
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000127 _dwc_native_kernel(std::make_unique<CLDepthwiseConvolutionLayerNativeKernel>()),
Manuel Bottini05069f02019-09-26 17:18:26 +0100128 _permute_input_to_nhwc(),
129 _permute_weights_to_nhwc(),
130 _permute_output_to_nchw(),
131 _permuted_input(),
132 _permuted_weights(),
133 _permuted_output(),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100134 _output_multipliers(),
135 _output_shifts(),
Manuel Bottini05069f02019-09-26 17:18:26 +0100136 _original_weights(),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100137 _input(),
138 _output(),
Manuel Bottini05069f02019-09-26 17:18:26 +0100139 _needs_permute(false),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100140 _is_prepared(false),
141 _is_quantized(false)
Manuel Bottini05069f02019-09-26 17:18:26 +0100142{
143}
144
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100145CLDepthwiseConvolutionLayer::~CLDepthwiseConvolutionLayer() = default;
146
Manuel Bottini05069f02019-09-26 17:18:26 +0100147void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
148 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
149{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100150 configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
151}
152
153void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases,
154 ICLTensor *output, const PadStrideInfo &conv_info,
155 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
156{
Manuel Bottini05069f02019-09-26 17:18:26 +0100157 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
158 ARM_COMPUTE_ERROR_THROW_ON(CLDepthwiseConvolutionLayer::validate(input->info(),
159 weights->info(),
160 biases != nullptr ? biases->info() : nullptr,
161 output->info(),
162 conv_info,
163 depth_multiplier,
164 act_info,
165 dilation));
166
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100167 _is_quantized = is_data_type_quantized(input->info()->data_type());
Manuel Bottini05069f02019-09-26 17:18:26 +0100168 _is_prepared = false;
169 _original_weights = weights;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100170 _input = input;
171 _output = output;
Manuel Bottini05069f02019-09-26 17:18:26 +0100172 _needs_permute = input->info()->data_layout() == DataLayout::NCHW;
173
174 ICLTensor *input_to_use = input;
175 const ICLTensor *weights_to_use = weights;
176 ICLTensor *output_to_use = output;
177 if(_needs_permute)
178 {
179 _memory_group.manage(&_permuted_input);
180 _memory_group.manage(&_permuted_output);
181
182 // Configure the function to transform the input tensor from NCHW -> NHWC
Manuel Bottini2b84be52020-04-08 10:15:51 +0100183 _permute_input_to_nhwc.configure(compile_context, input, &_permuted_input, PermutationVector(2U, 0U, 1U));
Manuel Bottini05069f02019-09-26 17:18:26 +0100184 _permuted_input.info()->set_data_layout(DataLayout::NHWC);
185
186 // Configure the function to transform the weights tensor from IHW -> HWI
Manuel Bottini2b84be52020-04-08 10:15:51 +0100187 _permute_weights_to_nhwc.configure(compile_context, weights, &_permuted_weights, PermutationVector(2U, 0U, 1U));
Manuel Bottini05069f02019-09-26 17:18:26 +0100188 _permuted_weights.info()->set_data_layout(DataLayout::NHWC);
189
190 // Set output quantization info before dwc kernel configure
191 _permuted_output.info()->set_quantization_info(output->info()->quantization_info());
192
193 input_to_use = &_permuted_input;
194 weights_to_use = &_permuted_weights;
195 output_to_use = &_permuted_output;
196 }
197
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100198 CLTensor *output_multipliers_to_use = nullptr;
199 CLTensor *output_shifts_to_use = nullptr;
200 if(_is_quantized)
201 {
202 const size_t idx_c = get_data_layout_dimension_index(weights->info()->data_layout(), DataLayoutDimension::CHANNEL);
203 const size_t num_filters = (is_data_type_quantized_per_channel(weights->info()->data_type())) ? weights->info()->dimension(idx_c) : 1;
204
205 _output_multipliers.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32));
206 _output_shifts.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32));
207
208 output_multipliers_to_use = &_output_multipliers;
209 output_shifts_to_use = &_output_shifts;
210 }
211
Manuel Bottini05069f02019-09-26 17:18:26 +0100212 DWCWeightsKernelInfo dwc_weights_info;
213 dwc_weights_info.n0 = (depth_multiplier == 1) ? 8 : 1;
214 DWCKernelInfo dwc_info;
215 dwc_info.activation_info = act_info;
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100216 _dwc_native_kernel->configure(compile_context, input_to_use, weights_to_use, biases, output_to_use,
217 dwc_weights_info, dwc_info, conv_info, depth_multiplier, dilation,
218 output_multipliers_to_use, output_shifts_to_use);
Manuel Bottini05069f02019-09-26 17:18:26 +0100219
220 if(_needs_permute)
221 {
222 _permuted_input.allocator()->allocate();
223
224 // Configure the function to transform the convoluted output to NCHW format
225 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100226 _permute_output_to_nchw.configure(compile_context, &_permuted_output, output, PermutationVector(1U, 2U, 0U));
Manuel Bottini05069f02019-09-26 17:18:26 +0100227 _permuted_output.allocator()->allocate();
228 }
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100229
230 if(_is_quantized)
231 {
232 _output_multipliers.allocator()->allocate();
233 _output_shifts.allocator()->allocate();
234 }
Manuel Bottini05069f02019-09-26 17:18:26 +0100235}
236
237Status CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
238 const PadStrideInfo &conv_info,
239 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
240{
241 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
242 const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
243 const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
244
245 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());
246 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());
247
248 DWCWeightsKernelInfo dwc_weights_info;
249 dwc_weights_info.n0 = (depth_multiplier == 1) ? 8 : 1;
250 DWCKernelInfo dwc_info;
251 dwc_info.activation_info = act_info;
252
253 const bool needs_permute = input->data_layout() == DataLayout::NCHW;
254
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100255 const bool is_quantized = is_data_type_quantized(input->data_type());
256
257 TensorInfo output_multipliers_shifts_info(TensorInfo(TensorShape(1U), 1, DataType::S32));
258 if(is_quantized)
259 {
260 if(is_data_type_quantized_per_channel(weights->data_type()))
261 {
262 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QSYMM8_PER_CHANNEL);
263
264 const size_t idx_c = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::CHANNEL);
265 output_multipliers_shifts_info.set_tensor_shape(TensorShape(weights->dimension(idx_c)));
266 }
267 else
268 {
269 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
270 }
271 }
272
Manuel Bottini05069f02019-09-26 17:18:26 +0100273 if(needs_permute)
274 {
275 TensorShape permuted_input_shape = input->tensor_shape();
276 TensorShape permuted_weights_shape = weights->tensor_shape();
277 TensorShape permuted_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);
278
279 permute(permuted_input_shape, PermutationVector(2U, 0U, 1U));
280 permute(permuted_weights_shape, PermutationVector(2U, 0U, 1U));
281 permute(permuted_output_shape, PermutationVector(2U, 0U, 1U));
282
283 const TensorInfo permuted_input = input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NHWC);
284 const TensorInfo permuted_weights = weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NHWC);
285 const TensorInfo permuted_output = output->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_output_shape).set_data_layout(DataLayout::NHWC);
286
287 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(input, &permuted_input, PermutationVector(2U, 0U, 1U)));
288 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(weights, &permuted_weights, PermutationVector(2U, 0U, 1U)));
289 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 +0100290 dwc_info, conv_info, depth_multiplier, dilation,
291 &output_multipliers_shifts_info, &output_multipliers_shifts_info));
Manuel Bottini05069f02019-09-26 17:18:26 +0100292 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(&permuted_output, output, PermutationVector(1U, 2U, 0U)));
293 }
294 else
295 {
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100296 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayerNativeKernel::validate(input, weights, biases, output, dwc_weights_info, dwc_info, conv_info, depth_multiplier,
297 dilation, &output_multipliers_shifts_info, &output_multipliers_shifts_info));
Manuel Bottini05069f02019-09-26 17:18:26 +0100298 }
299 return Status{};
300}
301
302void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::run()
303{
304 prepare();
305
306 MemoryGroupResourceScope scope_mg(_memory_group);
307
308 if(_needs_permute)
309 {
310 _permute_input_to_nhwc.run();
311 }
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100312 CLScheduler::get().enqueue(*_dwc_native_kernel);
Manuel Bottini05069f02019-09-26 17:18:26 +0100313 if(_needs_permute)
314 {
315 _permute_output_to_nchw.run();
316 }
317}
318
319void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::prepare()
320{
321 if(!_is_prepared)
322 {
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100323 if(_is_quantized)
324 {
325 _output_multipliers.map();
326 _output_shifts.map();
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000327 const unsigned int idx_ofms = get_data_layout_dimension_index(_output->info()->data_layout(), DataLayoutDimension::CHANNEL);
328 quantization::compute_quantized_multipliers_and_shifts(_input->info(),
329 _original_weights->info(),
330 _output->info(),
331 idx_ofms,
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100332 reinterpret_cast<int32_t *>(_output_multipliers.ptr_to_element(Coordinates(0))),
333 reinterpret_cast<int32_t *>(_output_shifts.ptr_to_element(Coordinates(0))));
334 _output_multipliers.unmap();
335 _output_shifts.unmap();
336 }
337
Manuel Bottini05069f02019-09-26 17:18:26 +0100338 if(_needs_permute)
339 {
340 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
341
342 _permuted_weights.allocator()->allocate();
343 _permute_weights_to_nhwc.run();
344 _original_weights->mark_as_unused();
345 }
346 _is_prepared = true;
347 }
348}
349
350CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::CLDepthwiseConvolutionLayerInternal3x3(std::shared_ptr<IMemoryManager> memory_manager)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100351 : _memory_group(std::move(memory_manager)),
352 _kernel(nullptr),
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000353 _border_handler(std::make_unique<CLFillBorderKernel>()),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100354 _permute_input_to_nchw(),
355 _permute_weights_to_nchw(),
356 _permute_output_to_nhwc(),
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000357 _reshape_weights(std::make_unique<CLDepthwiseConvolutionLayerReshapeWeightsKernel>()),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100358 _permuted_input(),
359 _permuted_weights(),
360 _permuted_output(),
361 _output_multipliers(),
362 _output_shifts(),
363 _original_weights(nullptr),
364 _input(nullptr),
365 _output(nullptr),
366 _needs_permute(false),
367 _needs_weights_reshape(false),
368 _is_prepared(false),
369 _is_quantized(false)
Manuel Bottini05069f02019-09-26 17:18:26 +0100370{
371}
372
373void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
374 const PadStrideInfo &conv_info, unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
375{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100376 configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
377}
378
379void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases,
380 ICLTensor *output,
381 const PadStrideInfo &conv_info, unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
382{
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100383 const GPUTarget gpu_target = CLScheduler::get().target();
Usama Arif881f2de2019-04-12 10:29:17 +0100384
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100385 // Perform validation step
386 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Manuel Bottini387259a2020-05-21 17:14:36 +0100387 ARM_COMPUTE_ERROR_THROW_ON(CLDepthwiseConvolutionLayerInternal3x3::validate(input->info(),
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100388 weights->info(),
389 biases != nullptr ? biases->info() : nullptr,
390 output->info(),
391 conv_info,
392 depth_multiplier,
393 act_info,
394 gpu_target,
395 dilation));
Giorgio Arena9fe41442017-08-23 16:36:24 +0100396
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100397 const bool is_nhwc = input->info()->data_layout() == DataLayout::NHWC;
398 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
giuros016d109962019-01-07 17:47:19 +0000399 _needs_permute = is_nhwc && (depth_multiplier > 1);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100400 _needs_weights_reshape = is_nhwc && (depth_multiplier == 1) && _is_quantized;
401
Georgios Pinitas05045c12018-12-07 18:31:47 +0000402 _is_prepared = false;
403 _original_weights = weights;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100404 _input = input;
405 _output = output;
Georgios Pinitas05045c12018-12-07 18:31:47 +0000406
407 ICLTensor *input_to_use = input;
408 const ICLTensor *weights_to_use = weights;
409 ICLTensor *output_to_use = output;
410
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100411 const bool is_quantized_per_channel = is_data_type_quantized_per_channel(weights->info()->data_type());
412 const bool is_stride_1 = ((conv_info.stride().first == conv_info.stride().second) && (conv_info.stride().first == 1));
413 const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device()) && !is_quantized_per_channel;
414 const bool is_stride_1_dilation_1 = (is_stride_1 && dilation.x() == 1 && dilation.y() == 1);
Usama Arife73686a2019-04-08 17:30:48 +0100415
giuros016d109962019-01-07 17:47:19 +0000416 DepthwiseConvolutionReshapeInfo info;
417 info.c0 = 4;
Usama Arife73686a2019-04-08 17:30:48 +0100418 info.transpose = is_stride_1_dilation_1 && is_dot8_supported;
giuros016d109962019-01-07 17:47:19 +0000419
Georgios Pinitas05045c12018-12-07 18:31:47 +0000420 if(_needs_permute)
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000421 {
Georgios Pinitas05045c12018-12-07 18:31:47 +0000422 _memory_group.manage(&_permuted_input);
423 _memory_group.manage(&_permuted_output);
424
425 // Configure the function to transform the input tensor from NHWC -> NCHW
Manuel Bottini2b84be52020-04-08 10:15:51 +0100426 _permute_input_to_nchw.configure(compile_context, input, &_permuted_input, PermutationVector(1U, 2U, 0U));
Georgios Pinitas05045c12018-12-07 18:31:47 +0000427 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
428
429 // Configure the function to transform the weights tensor from HWI -> IHW
Manuel Bottini2b84be52020-04-08 10:15:51 +0100430 _permute_weights_to_nchw.configure(compile_context, weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
Georgios Pinitas05045c12018-12-07 18:31:47 +0000431 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
Pablo Telloa28aebc2019-06-03 14:59:48 +0100432 _permuted_output.info()->set_quantization_info(output->info()->quantization_info());
Georgios Pinitas05045c12018-12-07 18:31:47 +0000433
434 input_to_use = &_permuted_input;
435 weights_to_use = &_permuted_weights;
436 output_to_use = &_permuted_output;
437
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000438 _kernel = std::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>();
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000439 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000440 else if(is_nhwc)
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000441 {
giuros016d109962019-01-07 17:47:19 +0000442 if(_needs_weights_reshape)
443 {
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100444 _reshape_weights->configure(compile_context, weights, &_permuted_weights, info);
giuros016d109962019-01-07 17:47:19 +0000445 weights_to_use = &_permuted_weights;
446 }
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000447 _kernel = std::make_unique<CLDepthwiseConvolutionLayer3x3NHWCKernel>();
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000448 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000449 else
450 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000451 _kernel = std::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>();
Georgios Pinitas05045c12018-12-07 18:31:47 +0000452 }
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000453
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100454 CLTensor *output_multipliers_to_use = nullptr;
455 CLTensor *output_shifts_to_use = nullptr;
456 if(_is_quantized)
457 {
458 const size_t idx_c = get_data_layout_dimension_index(weights->info()->data_layout(), DataLayoutDimension::CHANNEL);
459 const size_t num_filters = (is_quantized_per_channel) ? weights->info()->dimension(idx_c) : 1;
460
461 _output_multipliers.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32));
462 _output_shifts.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32));
463
464 output_multipliers_to_use = &_output_multipliers;
465 output_shifts_to_use = &_output_shifts;
466 }
467
Georgios Pinitas05045c12018-12-07 18:31:47 +0000468 // Configure kernel
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100469 _kernel->set_target(gpu_target);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100470 _kernel->configure(compile_context, input_to_use, weights_to_use, biases, output_to_use, conv_info, depth_multiplier,
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100471 act_info, dilation, output_multipliers_to_use, output_shifts_to_use);
472
473 if(_is_quantized)
474 {
475 _output_multipliers.allocator()->allocate();
476 _output_shifts.allocator()->allocate();
477 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000478
479 // Permute output if needed
480 if(_needs_permute)
481 {
482 // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
Georgios Pinitas3f8aac42018-12-24 13:09:02 +0000483 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100484 _permute_output_to_nhwc.configure(compile_context, &_permuted_output, output, PermutationVector(2U, 0U, 1U));
Georgios Pinitas05045c12018-12-07 18:31:47 +0000485
486 // Allocate tensors
487 _permuted_input.allocator()->allocate();
488 _permuted_output.allocator()->allocate();
489 }
Diego Lopez Recasfa0add12017-11-28 16:44:52 +0000490 // Configure border handler
491 PixelValue &&zero_value(0.f);
492 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
493 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100494 zero_value = PixelValue(static_cast<uint8_t>(input->info()->quantization_info().uniform().offset));
Diego Lopez Recasfa0add12017-11-28 16:44:52 +0000495 }
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100496 _border_handler->configure(compile_context, input_to_use, _kernel->border_size(), BorderMode::CONSTANT, zero_value);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100497}
498
Manuel Bottini05069f02019-09-26 17:18:26 +0100499Status CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
500 const PadStrideInfo &conv_info, unsigned int depth_multiplier, ActivationLayerInfo act_info, GPUTarget gpu_target, const Size2D &dilation)
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100501{
Manuel Bottini05069f02019-09-26 17:18:26 +0100502 return validate_arguments_3x3(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation);
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100503}
504
Manuel Bottini05069f02019-09-26 17:18:26 +0100505void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::run()
Giorgio Arena9fe41442017-08-23 16:36:24 +0100506{
Georgios Pinitas05045c12018-12-07 18:31:47 +0000507 prepare();
508
Georgios Pinitasda953f22019-04-02 17:27:03 +0100509 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas05045c12018-12-07 18:31:47 +0000510
511 if(_needs_permute)
512 {
513 _permute_input_to_nchw.run();
514 }
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100515 CLScheduler::get().enqueue(*_border_handler);
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000516 CLScheduler::get().enqueue(*_kernel);
Georgios Pinitas05045c12018-12-07 18:31:47 +0000517
518 if(_needs_permute)
519 {
520 _permute_output_to_nhwc.run();
521 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000522}
523
Manuel Bottini05069f02019-09-26 17:18:26 +0100524void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::prepare()
Georgios Pinitas05045c12018-12-07 18:31:47 +0000525{
526 if(!_is_prepared)
527 {
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100528 if(_is_quantized)
529 {
530 _output_multipliers.map();
531 _output_shifts.map();
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000532 const unsigned int idx_ofms = get_data_layout_dimension_index(_output->info()->data_layout(), DataLayoutDimension::CHANNEL);
533 quantization::compute_quantized_multipliers_and_shifts(_input->info(),
534 _original_weights->info(),
535 _output->info(),
536 idx_ofms,
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100537 reinterpret_cast<int32_t *>(_output_multipliers.ptr_to_element(Coordinates(0))),
538 reinterpret_cast<int32_t *>(_output_shifts.ptr_to_element(Coordinates(0))));
539 _output_multipliers.unmap();
540 _output_shifts.unmap();
541 }
542
Georgios Pinitas05045c12018-12-07 18:31:47 +0000543 if(_needs_permute)
544 {
545 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
546
547 _permuted_weights.allocator()->allocate();
548 _permute_weights_to_nchw.run();
549 _original_weights->mark_as_unused();
550 }
giuros016d109962019-01-07 17:47:19 +0000551
552 if(_needs_weights_reshape)
553 {
554 ARM_COMPUTE_ERROR_ON(_needs_permute);
555 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
556 _permuted_weights.allocator()->allocate();
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100557 CLScheduler::get().enqueue(*_reshape_weights);
giuros016d109962019-01-07 17:47:19 +0000558 _original_weights->mark_as_unused();
559 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000560 _is_prepared = true;
561 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100562}
563
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100564CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Manuel Bottini05069f02019-09-26 17:18:26 +0100565 : _memory_manager(std::move(memory_manager)), _depth_conv_func(DepthwiseConvolutionFunction::GENERIC), _func_3x3(), _func_generic()
Giorgio Arena9fe41442017-08-23 16:36:24 +0100566{
567}
568
Manuel Bottini05069f02019-09-26 17:18:26 +0100569void CLDepthwiseConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier,
570 ActivationLayerInfo act_info, const Size2D &dilation)
Giorgio Arena93a690e2017-08-01 16:09:33 +0100571{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100572 configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
573}
574
575void CLDepthwiseConvolutionLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
576 const PadStrideInfo &conv_info,
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100577 unsigned int depth_multiplier,
Manuel Bottini2b84be52020-04-08 10:15:51 +0100578 ActivationLayerInfo act_info, const Size2D &dilation)
579{
Manuel Bottini05069f02019-09-26 17:18:26 +0100580 const GPUTarget gpu_target = CLScheduler::get().target();
581 _depth_conv_func = get_depthwiseconvolution_function(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(), conv_info, depth_multiplier, act_info,
582 dilation, gpu_target);
583 switch(_depth_conv_func)
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000584 {
Manuel Bottini05069f02019-09-26 17:18:26 +0100585 case DepthwiseConvolutionFunction::OPTIMIZED:
586 _func_3x3.set_memory_group(_memory_manager);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100587 _func_3x3.configure(compile_context, input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
Manuel Bottini05069f02019-09-26 17:18:26 +0100588 break;
589 case DepthwiseConvolutionFunction::GENERIC:
Pablo Tello8bf622a2018-12-03 15:54:49 +0000590 {
Manuel Bottini05069f02019-09-26 17:18:26 +0100591 _func_generic.set_memory_group(_memory_manager);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100592 _func_generic.configure(compile_context, input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
Pablo Tello8bf622a2018-12-03 15:54:49 +0000593 }
Manuel Bottini05069f02019-09-26 17:18:26 +0100594 break;
595 default:
596 ARM_COMPUTE_ERROR("Unsupported DepthwiseConvolutionFunction");
Georgios Pinitas60e98252018-10-22 16:17:20 +0100597 }
Giorgio Arena93a690e2017-08-01 16:09:33 +0100598}
599
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100600Status 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 +0100601 unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100602{
Manuel Bottini05069f02019-09-26 17:18:26 +0100603 const GPUTarget gpu_target = CLScheduler::get().target();
604 DepthwiseConvolutionFunction depth_conv_func = get_depthwiseconvolution_function(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation, gpu_target);
605 switch(depth_conv_func)
Georgios Pinitas60e98252018-10-22 16:17:20 +0100606 {
Manuel Bottini05069f02019-09-26 17:18:26 +0100607 case DepthwiseConvolutionFunction::OPTIMIZED:
608 return CLDepthwiseConvolutionLayerInternal3x3::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation);
609 case DepthwiseConvolutionFunction::GENERIC:
610 return CLDepthwiseConvolutionLayerGeneric::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
611 default:
612 ARM_COMPUTE_ERROR("Unsupported DepthwiseConvolutionFunction");
613 }
614}
Georgios Pinitas60e98252018-10-22 16:17:20 +0100615
Manuel Bottini05069f02019-09-26 17:18:26 +0100616DepthwiseConvolutionFunction CLDepthwiseConvolutionLayer::get_depthwiseconvolution_function(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
617 const PadStrideInfo &conv_info,
618 unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation, GPUTarget gpu_target)
619{
620 if(bool(CLDepthwiseConvolutionLayerInternal3x3::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation)) && (is_data_type_float(input->data_type())
621 || get_arch_from_target(gpu_target) == GPUTarget::MIDGARD))
622 {
623 return DepthwiseConvolutionFunction::OPTIMIZED;
Pablo Tello8bf622a2018-12-03 15:54:49 +0000624 }
giuros016d109962019-01-07 17:47:19 +0000625 else
626 {
Manuel Bottini05069f02019-09-26 17:18:26 +0100627 return DepthwiseConvolutionFunction::GENERIC;
giuros016d109962019-01-07 17:47:19 +0000628 }
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100629}
630
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000631void CLDepthwiseConvolutionLayer::run()
Giorgio Arena93a690e2017-08-01 16:09:33 +0100632{
Manuel Bottini05069f02019-09-26 17:18:26 +0100633 switch(_depth_conv_func)
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000634 {
Manuel Bottini05069f02019-09-26 17:18:26 +0100635 case DepthwiseConvolutionFunction::OPTIMIZED:
636 _func_3x3.run();
637 break;
638 case DepthwiseConvolutionFunction::GENERIC:
639 _func_generic.run();
640 break;
641 default:
642 ARM_COMPUTE_ERROR("DepthwiseConvolutionFunction not properly configured");
Georgios Pinitas60e98252018-10-22 16:17:20 +0100643 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100644}
Georgios Pinitas72219332018-06-05 14:56:06 +0100645
646void CLDepthwiseConvolutionLayer::prepare()
647{
Manuel Bottini05069f02019-09-26 17:18:26 +0100648 switch(_depth_conv_func)
Georgios Pinitas72219332018-06-05 14:56:06 +0100649 {
Manuel Bottini05069f02019-09-26 17:18:26 +0100650 case DepthwiseConvolutionFunction::OPTIMIZED:
651 _func_3x3.prepare();
652 break;
653 case DepthwiseConvolutionFunction::GENERIC:
654 _func_generic.prepare();
655 break;
656 default:
657 ARM_COMPUTE_ERROR("DepthwiseConvolutionFunction not properly configured");
Georgios Pinitas72219332018-06-05 14:56:06 +0100658 }
659}
giuros016d109962019-01-07 17:47:19 +0000660} // namespace arm_compute