blob: b1e9fe77d4f37cc0802bf0e486904f48cfd1bf6f [file] [log] [blame]
Anthony Barbier8140e1e2017-12-14 23:48:46 +00001/*
Jenkins6a7771e2020-05-28 11:28:36 +01002 * Copyright (c) 2017-2020 ARM Limited.
Anthony Barbier8140e1e2017-12-14 23:48:46 +00003 *
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 */
24#include "arm_compute/runtime/CL/functions/CLDepthwiseConvolutionLayer.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
Jenkinsb3a371b2018-05-23 11:36:53 +010027#include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NCHWKernel.h"
28#include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NHWCKernel.h"
Jenkins514be652019-02-28 12:25:18 +000029#include "arm_compute/core/Helpers.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000030#include "arm_compute/core/PixelValue.h"
Anthony Barbier06ea0482018-02-22 15:45:35 +000031#include "arm_compute/core/utils/misc/ShapeCalculator.h"
32#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000033#include "arm_compute/runtime/CL/CLScheduler.h"
Jenkins6a7771e2020-05-28 11:28:36 +010034#include "support/MemorySupport.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000035
Jenkins514be652019-02-28 12:25:18 +000036namespace arm_compute
37{
Anthony Barbier06ea0482018-02-22 15:45:35 +000038using namespace arm_compute::misc;
39using namespace arm_compute::misc::shape_calculator;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000040
Jenkins0e205f72019-11-28 16:53:35 +000041namespace
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);
48 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
49 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
62 TensorInfo output_multipliers_shifts_info(TensorInfo(TensorShape(1U), 1, DataType::S32));
63 if(is_quantized)
64 {
65 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);
68
69 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 }
76 }
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
92 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));
95 }
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);
101 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));
104 }
105 else
106 {
107 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));
109 }
110 }
111 else
112 {
113 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));
115 }
116 return Status{};
117}
118} // namespace
119
Jenkins0e205f72019-11-28 16:53:35 +0000120CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::CLDepthwiseConvolutionLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager)
121 : _memory_group(std::move(memory_manager)),
122 _dwc_native_kernel(),
123 _permute_input_to_nhwc(),
124 _permute_weights_to_nhwc(),
125 _permute_output_to_nchw(),
126 _permuted_input(),
127 _permuted_weights(),
128 _permuted_output(),
129 _output_multipliers(),
130 _output_shifts(),
131 _original_weights(),
132 _input(),
133 _output(),
134 _needs_permute(false),
135 _is_prepared(false),
136 _is_quantized(false)
137{
138}
139
140void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
141 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
142{
Jenkins6a7771e2020-05-28 11:28:36 +0100143 configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
144}
145
146void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases,
147 ICLTensor *output, const PadStrideInfo &conv_info,
148 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
149{
Jenkins0e205f72019-11-28 16:53:35 +0000150 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
151 ARM_COMPUTE_ERROR_THROW_ON(CLDepthwiseConvolutionLayer::validate(input->info(),
152 weights->info(),
153 biases != nullptr ? biases->info() : nullptr,
154 output->info(),
155 conv_info,
156 depth_multiplier,
157 act_info,
158 dilation));
159
160 _is_quantized = is_data_type_quantized(input->info()->data_type());
Jenkins514be652019-02-28 12:25:18 +0000161 _is_prepared = false;
162 _original_weights = weights;
Jenkins0e205f72019-11-28 16:53:35 +0000163 _input = input;
164 _output = output;
165 _needs_permute = input->info()->data_layout() == DataLayout::NCHW;
166
167 ICLTensor *input_to_use = input;
168 const ICLTensor *weights_to_use = weights;
169 ICLTensor *output_to_use = output;
170 if(_needs_permute)
171 {
172 _memory_group.manage(&_permuted_input);
173 _memory_group.manage(&_permuted_output);
174
175 // Configure the function to transform the input tensor from NCHW -> NHWC
Jenkins6a7771e2020-05-28 11:28:36 +0100176 _permute_input_to_nhwc.configure(compile_context, input, &_permuted_input, PermutationVector(2U, 0U, 1U));
Jenkins0e205f72019-11-28 16:53:35 +0000177 _permuted_input.info()->set_data_layout(DataLayout::NHWC);
178
179 // Configure the function to transform the weights tensor from IHW -> HWI
Jenkins6a7771e2020-05-28 11:28:36 +0100180 _permute_weights_to_nhwc.configure(compile_context, weights, &_permuted_weights, PermutationVector(2U, 0U, 1U));
Jenkins0e205f72019-11-28 16:53:35 +0000181 _permuted_weights.info()->set_data_layout(DataLayout::NHWC);
182
183 // Set output quantization info before dwc kernel configure
184 _permuted_output.info()->set_quantization_info(output->info()->quantization_info());
185
186 input_to_use = &_permuted_input;
187 weights_to_use = &_permuted_weights;
188 output_to_use = &_permuted_output;
189 }
190
191 CLTensor *output_multipliers_to_use = nullptr;
192 CLTensor *output_shifts_to_use = nullptr;
193 if(_is_quantized)
194 {
195 const size_t idx_c = get_data_layout_dimension_index(weights->info()->data_layout(), DataLayoutDimension::CHANNEL);
196 const size_t num_filters = (is_data_type_quantized_per_channel(weights->info()->data_type())) ? weights->info()->dimension(idx_c) : 1;
197
198 _output_multipliers.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32));
199 _output_shifts.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32));
200
201 output_multipliers_to_use = &_output_multipliers;
202 output_shifts_to_use = &_output_shifts;
203 }
204
205 DWCWeightsKernelInfo dwc_weights_info;
206 dwc_weights_info.n0 = (depth_multiplier == 1) ? 8 : 1;
207 DWCKernelInfo dwc_info;
208 dwc_info.activation_info = act_info;
Jenkins6a7771e2020-05-28 11:28:36 +0100209 _dwc_native_kernel.configure(compile_context, input_to_use, weights_to_use, biases, output_to_use,
Jenkins0e205f72019-11-28 16:53:35 +0000210 dwc_weights_info, dwc_info, conv_info, depth_multiplier, dilation,
211 output_multipliers_to_use, output_shifts_to_use);
212
213 if(_needs_permute)
214 {
215 _permuted_input.allocator()->allocate();
216
217 // Configure the function to transform the convoluted output to NCHW format
218 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
Jenkins6a7771e2020-05-28 11:28:36 +0100219 _permute_output_to_nchw.configure(compile_context, &_permuted_output, output, PermutationVector(1U, 2U, 0U));
Jenkins0e205f72019-11-28 16:53:35 +0000220 _permuted_output.allocator()->allocate();
221 }
222
223 if(_is_quantized)
224 {
225 _output_multipliers.allocator()->allocate();
226 _output_shifts.allocator()->allocate();
227 }
228}
229
230Status CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
231 const PadStrideInfo &conv_info,
232 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
233{
234 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
235 const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
236 const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
237
238 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());
239 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());
240
241 DWCWeightsKernelInfo dwc_weights_info;
242 dwc_weights_info.n0 = (depth_multiplier == 1) ? 8 : 1;
243 DWCKernelInfo dwc_info;
244 dwc_info.activation_info = act_info;
245
246 const bool needs_permute = input->data_layout() == DataLayout::NCHW;
247
248 const bool is_quantized = is_data_type_quantized(input->data_type());
249
250 TensorInfo output_multipliers_shifts_info(TensorInfo(TensorShape(1U), 1, DataType::S32));
251 if(is_quantized)
252 {
253 if(is_data_type_quantized_per_channel(weights->data_type()))
254 {
255 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QSYMM8_PER_CHANNEL);
256
257 const size_t idx_c = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::CHANNEL);
258 output_multipliers_shifts_info.set_tensor_shape(TensorShape(weights->dimension(idx_c)));
259 }
260 else
261 {
262 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
263 }
264 }
265
266 if(needs_permute)
267 {
268 TensorShape permuted_input_shape = input->tensor_shape();
269 TensorShape permuted_weights_shape = weights->tensor_shape();
270 TensorShape permuted_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);
271
272 permute(permuted_input_shape, PermutationVector(2U, 0U, 1U));
273 permute(permuted_weights_shape, PermutationVector(2U, 0U, 1U));
274 permute(permuted_output_shape, PermutationVector(2U, 0U, 1U));
275
276 const TensorInfo permuted_input = input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NHWC);
277 const TensorInfo permuted_weights = weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NHWC);
278 const TensorInfo permuted_output = output->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_output_shape).set_data_layout(DataLayout::NHWC);
279
280 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(input, &permuted_input, PermutationVector(2U, 0U, 1U)));
281 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(weights, &permuted_weights, PermutationVector(2U, 0U, 1U)));
282 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayerNativeKernel::validate(&permuted_input, &permuted_weights, biases, &permuted_output, dwc_weights_info,
283 dwc_info, conv_info, depth_multiplier, dilation,
284 &output_multipliers_shifts_info, &output_multipliers_shifts_info));
285 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(&permuted_output, output, PermutationVector(1U, 2U, 0U)));
286 }
287 else
288 {
289 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayerNativeKernel::validate(input, weights, biases, output, dwc_weights_info, dwc_info, conv_info, depth_multiplier,
290 dilation, &output_multipliers_shifts_info, &output_multipliers_shifts_info));
291 }
292 return Status{};
293}
294
295void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::run()
296{
297 prepare();
298
299 MemoryGroupResourceScope scope_mg(_memory_group);
300
301 if(_needs_permute)
302 {
303 _permute_input_to_nhwc.run();
304 }
305 CLScheduler::get().enqueue(_dwc_native_kernel);
306 if(_needs_permute)
307 {
308 _permute_output_to_nchw.run();
309 }
310}
311
312void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::prepare()
313{
314 if(!_is_prepared)
315 {
316 if(_is_quantized)
317 {
318 _output_multipliers.map();
319 _output_shifts.map();
320 const unsigned int idx_ofms = get_data_layout_dimension_index(_output->info()->data_layout(), DataLayoutDimension::CHANNEL);
321 quantization::compute_quantized_multipliers_and_shifts(_input->info(),
322 _original_weights->info(),
323 _output->info(),
324 idx_ofms,
325 reinterpret_cast<int32_t *>(_output_multipliers.ptr_to_element(Coordinates(0))),
326 reinterpret_cast<int32_t *>(_output_shifts.ptr_to_element(Coordinates(0))));
327 _output_multipliers.unmap();
328 _output_shifts.unmap();
329 }
330
331 if(_needs_permute)
332 {
333 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
334
335 _permuted_weights.allocator()->allocate();
336 _permute_weights_to_nhwc.run();
337 _original_weights->mark_as_unused();
338 }
339 _is_prepared = true;
340 }
341}
342
343CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::CLDepthwiseConvolutionLayerInternal3x3(std::shared_ptr<IMemoryManager> memory_manager)
344 : _memory_group(std::move(memory_manager)),
345 _kernel(nullptr),
346 _border_handler(),
347 _permute_input_to_nchw(),
348 _permute_weights_to_nchw(),
349 _permute_output_to_nhwc(),
350 _reshape_weights(),
351 _permuted_input(),
352 _permuted_weights(),
353 _permuted_output(),
354 _output_multipliers(),
355 _output_shifts(),
356 _original_weights(nullptr),
357 _input(nullptr),
358 _output(nullptr),
359 _needs_permute(false),
360 _needs_weights_reshape(false),
361 _is_prepared(false),
362 _is_quantized(false)
363{
364}
365
366void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
367 const PadStrideInfo &conv_info, unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
368{
Jenkins6a7771e2020-05-28 11:28:36 +0100369 configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
370}
371
372void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases,
373 ICLTensor *output,
374 const PadStrideInfo &conv_info, unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
375{
Jenkins0e205f72019-11-28 16:53:35 +0000376 const GPUTarget gpu_target = CLScheduler::get().target();
377
378 // Perform validation step
379 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Jenkins6a7771e2020-05-28 11:28:36 +0100380 ARM_COMPUTE_ERROR_THROW_ON(CLDepthwiseConvolutionLayerInternal3x3::validate(input->info(),
Jenkins0e205f72019-11-28 16:53:35 +0000381 weights->info(),
382 biases != nullptr ? biases->info() : nullptr,
383 output->info(),
384 conv_info,
385 depth_multiplier,
386 act_info,
387 gpu_target,
388 dilation));
389
390 const bool is_nhwc = input->info()->data_layout() == DataLayout::NHWC;
391 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
392 _needs_permute = is_nhwc && (depth_multiplier > 1);
393 _needs_weights_reshape = is_nhwc && (depth_multiplier == 1) && _is_quantized;
394
395 _is_prepared = false;
396 _original_weights = weights;
397 _input = input;
398 _output = output;
Jenkins514be652019-02-28 12:25:18 +0000399
400 ICLTensor *input_to_use = input;
401 const ICLTensor *weights_to_use = weights;
402 ICLTensor *output_to_use = output;
403
Jenkins0e205f72019-11-28 16:53:35 +0000404 const bool is_quantized_per_channel = is_data_type_quantized_per_channel(weights->info()->data_type());
405 const bool is_stride_1 = ((conv_info.stride().first == conv_info.stride().second) && (conv_info.stride().first == 1));
406 const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device()) && !is_quantized_per_channel;
407 const bool is_stride_1_dilation_1 = (is_stride_1 && dilation.x() == 1 && dilation.y() == 1);
Jenkins4ba87db2019-05-23 17:11:51 +0100408
Jenkins514be652019-02-28 12:25:18 +0000409 DepthwiseConvolutionReshapeInfo info;
410 info.c0 = 4;
Jenkins4ba87db2019-05-23 17:11:51 +0100411 info.transpose = is_stride_1_dilation_1 && is_dot8_supported;
Jenkins514be652019-02-28 12:25:18 +0000412
413 if(_needs_permute)
Jenkinsb3a371b2018-05-23 11:36:53 +0100414 {
Jenkins514be652019-02-28 12:25:18 +0000415 _memory_group.manage(&_permuted_input);
416 _memory_group.manage(&_permuted_output);
417
418 // Configure the function to transform the input tensor from NHWC -> NCHW
Jenkins6a7771e2020-05-28 11:28:36 +0100419 _permute_input_to_nchw.configure(compile_context, input, &_permuted_input, PermutationVector(1U, 2U, 0U));
Jenkins514be652019-02-28 12:25:18 +0000420 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
421
422 // Configure the function to transform the weights tensor from HWI -> IHW
Jenkins6a7771e2020-05-28 11:28:36 +0100423 _permute_weights_to_nchw.configure(compile_context, weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
Jenkins514be652019-02-28 12:25:18 +0000424 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
Jenkins975dfe12019-09-02 11:47:54 +0100425 _permuted_output.info()->set_quantization_info(output->info()->quantization_info());
Jenkins514be652019-02-28 12:25:18 +0000426
427 input_to_use = &_permuted_input;
428 weights_to_use = &_permuted_weights;
429 output_to_use = &_permuted_output;
430
Jenkinsb3a371b2018-05-23 11:36:53 +0100431 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>();
432 }
Jenkins514be652019-02-28 12:25:18 +0000433 else if(is_nhwc)
434 {
435 if(_needs_weights_reshape)
436 {
Jenkins6a7771e2020-05-28 11:28:36 +0100437 _reshape_weights.configure(compile_context, weights, &_permuted_weights, info);
Jenkins514be652019-02-28 12:25:18 +0000438 weights_to_use = &_permuted_weights;
439 }
440 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NHWCKernel>();
441 }
Jenkinsb3a371b2018-05-23 11:36:53 +0100442 else
443 {
Jenkins514be652019-02-28 12:25:18 +0000444 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>();
Jenkinsb3a371b2018-05-23 11:36:53 +0100445 }
446
Jenkins0e205f72019-11-28 16:53:35 +0000447 CLTensor *output_multipliers_to_use = nullptr;
448 CLTensor *output_shifts_to_use = nullptr;
449 if(_is_quantized)
450 {
451 const size_t idx_c = get_data_layout_dimension_index(weights->info()->data_layout(), DataLayoutDimension::CHANNEL);
452 const size_t num_filters = (is_quantized_per_channel) ? weights->info()->dimension(idx_c) : 1;
453
454 _output_multipliers.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32));
455 _output_shifts.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32));
456
457 output_multipliers_to_use = &_output_multipliers;
458 output_shifts_to_use = &_output_shifts;
459 }
460
Jenkins514be652019-02-28 12:25:18 +0000461 // Configure kernel
Jenkins0e205f72019-11-28 16:53:35 +0000462 _kernel->set_target(gpu_target);
Jenkins6a7771e2020-05-28 11:28:36 +0100463 _kernel->configure(compile_context, input_to_use, weights_to_use, biases, output_to_use, conv_info, depth_multiplier,
Jenkins0e205f72019-11-28 16:53:35 +0000464 act_info, dilation, output_multipliers_to_use, output_shifts_to_use);
465
466 if(_is_quantized)
467 {
468 _output_multipliers.allocator()->allocate();
469 _output_shifts.allocator()->allocate();
470 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000471
Jenkins514be652019-02-28 12:25:18 +0000472 // Permute output if needed
473 if(_needs_permute)
474 {
475 // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
476 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
Jenkins6a7771e2020-05-28 11:28:36 +0100477 _permute_output_to_nhwc.configure(compile_context, &_permuted_output, output, PermutationVector(2U, 0U, 1U));
Jenkins514be652019-02-28 12:25:18 +0000478
479 // Allocate tensors
480 _permuted_input.allocator()->allocate();
481 _permuted_output.allocator()->allocate();
482 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000483 // Configure border handler
484 PixelValue &&zero_value(0.f);
485 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
486 {
Jenkins975dfe12019-09-02 11:47:54 +0100487 zero_value = PixelValue(static_cast<uint8_t>(input->info()->quantization_info().uniform().offset));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000488 }
Jenkins6a7771e2020-05-28 11:28:36 +0100489 _border_handler.configure(compile_context, input_to_use, _kernel->border_size(), BorderMode::CONSTANT, zero_value);
Jenkinsb3a371b2018-05-23 11:36:53 +0100490}
491
Jenkins0e205f72019-11-28 16:53:35 +0000492Status CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
493 const PadStrideInfo &conv_info, unsigned int depth_multiplier, ActivationLayerInfo act_info, GPUTarget gpu_target, const Size2D &dilation)
Jenkinsb3a371b2018-05-23 11:36:53 +0100494{
Jenkins0e205f72019-11-28 16:53:35 +0000495 return validate_arguments_3x3(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000496}
497
Jenkins0e205f72019-11-28 16:53:35 +0000498void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::run()
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000499{
Jenkins514be652019-02-28 12:25:18 +0000500 prepare();
501
Jenkins4ba87db2019-05-23 17:11:51 +0100502 MemoryGroupResourceScope scope_mg(_memory_group);
Jenkins514be652019-02-28 12:25:18 +0000503
504 if(_needs_permute)
505 {
506 _permute_input_to_nchw.run();
507 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000508 CLScheduler::get().enqueue(_border_handler);
Jenkinsb3a371b2018-05-23 11:36:53 +0100509 CLScheduler::get().enqueue(*_kernel);
Jenkins514be652019-02-28 12:25:18 +0000510
511 if(_needs_permute)
512 {
513 _permute_output_to_nhwc.run();
514 }
Jenkins514be652019-02-28 12:25:18 +0000515}
516
Jenkins0e205f72019-11-28 16:53:35 +0000517void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::prepare()
Jenkins514be652019-02-28 12:25:18 +0000518{
519 if(!_is_prepared)
520 {
Jenkins0e205f72019-11-28 16:53:35 +0000521 if(_is_quantized)
522 {
523 _output_multipliers.map();
524 _output_shifts.map();
525 const unsigned int idx_ofms = get_data_layout_dimension_index(_output->info()->data_layout(), DataLayoutDimension::CHANNEL);
526 quantization::compute_quantized_multipliers_and_shifts(_input->info(),
527 _original_weights->info(),
528 _output->info(),
529 idx_ofms,
530 reinterpret_cast<int32_t *>(_output_multipliers.ptr_to_element(Coordinates(0))),
531 reinterpret_cast<int32_t *>(_output_shifts.ptr_to_element(Coordinates(0))));
532 _output_multipliers.unmap();
533 _output_shifts.unmap();
534 }
535
Jenkins514be652019-02-28 12:25:18 +0000536 if(_needs_permute)
537 {
538 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
539
540 _permuted_weights.allocator()->allocate();
541 _permute_weights_to_nchw.run();
542 _original_weights->mark_as_unused();
543 }
544
545 if(_needs_weights_reshape)
546 {
547 ARM_COMPUTE_ERROR_ON(_needs_permute);
548 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
549 _permuted_weights.allocator()->allocate();
550 CLScheduler::get().enqueue(_reshape_weights);
551 _original_weights->mark_as_unused();
552 }
553 _is_prepared = true;
554 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000555}
556
Jenkins0e205f72019-11-28 16:53:35 +0000557CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
558 : _memory_manager(std::move(memory_manager)), _depth_conv_func(DepthwiseConvolutionFunction::GENERIC), _func_3x3(), _func_generic()
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000559{
560}
561
Jenkins0e205f72019-11-28 16:53:35 +0000562void CLDepthwiseConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier,
563 ActivationLayerInfo act_info, const Size2D &dilation)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000564{
Jenkins6a7771e2020-05-28 11:28:36 +0100565 configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
566}
567
568void CLDepthwiseConvolutionLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
569 const PadStrideInfo &conv_info,
570 unsigned int depth_multiplier,
571 ActivationLayerInfo act_info, const Size2D &dilation)
572{
Jenkins0e205f72019-11-28 16:53:35 +0000573 const GPUTarget gpu_target = CLScheduler::get().target();
574 _depth_conv_func = get_depthwiseconvolution_function(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(), conv_info, depth_multiplier, act_info,
575 dilation, gpu_target);
576 switch(_depth_conv_func)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000577 {
Jenkins0e205f72019-11-28 16:53:35 +0000578 case DepthwiseConvolutionFunction::OPTIMIZED:
579 _func_3x3.set_memory_group(_memory_manager);
Jenkins6a7771e2020-05-28 11:28:36 +0100580 _func_3x3.configure(compile_context, input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
Jenkins0e205f72019-11-28 16:53:35 +0000581 break;
582 case DepthwiseConvolutionFunction::GENERIC:
Jenkins514be652019-02-28 12:25:18 +0000583 {
Jenkins0e205f72019-11-28 16:53:35 +0000584 _func_generic.set_memory_group(_memory_manager);
Jenkins6a7771e2020-05-28 11:28:36 +0100585 _func_generic.configure(compile_context, input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
Jenkins514be652019-02-28 12:25:18 +0000586 }
Jenkins0e205f72019-11-28 16:53:35 +0000587 break;
588 default:
589 ARM_COMPUTE_ERROR("Unsupported DepthwiseConvolutionFunction");
Jenkinsb9abeae2018-11-22 11:58:08 +0000590 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000591}
592
Jenkinsb3a371b2018-05-23 11:36:53 +0100593Status CLDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Jenkins0e205f72019-11-28 16:53:35 +0000594 unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
Jenkinsb3a371b2018-05-23 11:36:53 +0100595{
Jenkins0e205f72019-11-28 16:53:35 +0000596 const GPUTarget gpu_target = CLScheduler::get().target();
597 DepthwiseConvolutionFunction depth_conv_func = get_depthwiseconvolution_function(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation, gpu_target);
598 switch(depth_conv_func)
Jenkinsb3a371b2018-05-23 11:36:53 +0100599 {
Jenkins0e205f72019-11-28 16:53:35 +0000600 case DepthwiseConvolutionFunction::OPTIMIZED:
601 return CLDepthwiseConvolutionLayerInternal3x3::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation);
602 case DepthwiseConvolutionFunction::GENERIC:
603 return CLDepthwiseConvolutionLayerGeneric::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
604 default:
605 ARM_COMPUTE_ERROR("Unsupported DepthwiseConvolutionFunction");
606 }
607}
Jenkinsb3a371b2018-05-23 11:36:53 +0100608
Jenkins0e205f72019-11-28 16:53:35 +0000609DepthwiseConvolutionFunction CLDepthwiseConvolutionLayer::get_depthwiseconvolution_function(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
610 const PadStrideInfo &conv_info,
611 unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation, GPUTarget gpu_target)
612{
613 if(bool(CLDepthwiseConvolutionLayerInternal3x3::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation)) && (is_data_type_float(input->data_type())
614 || get_arch_from_target(gpu_target) == GPUTarget::MIDGARD))
615 {
616 return DepthwiseConvolutionFunction::OPTIMIZED;
Jenkins514be652019-02-28 12:25:18 +0000617 }
618 else
Jenkinsb9abeae2018-11-22 11:58:08 +0000619 {
Jenkins0e205f72019-11-28 16:53:35 +0000620 return DepthwiseConvolutionFunction::GENERIC;
Jenkinsb9abeae2018-11-22 11:58:08 +0000621 }
Jenkinsb3a371b2018-05-23 11:36:53 +0100622}
623
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000624void CLDepthwiseConvolutionLayer::run()
625{
Jenkins0e205f72019-11-28 16:53:35 +0000626 switch(_depth_conv_func)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000627 {
Jenkins0e205f72019-11-28 16:53:35 +0000628 case DepthwiseConvolutionFunction::OPTIMIZED:
629 _func_3x3.run();
630 break;
631 case DepthwiseConvolutionFunction::GENERIC:
632 _func_generic.run();
633 break;
634 default:
635 ARM_COMPUTE_ERROR("DepthwiseConvolutionFunction not properly configured");
Jenkinsb9abeae2018-11-22 11:58:08 +0000636 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000637}
Jenkins52ba29e2018-08-29 15:32:11 +0000638
639void CLDepthwiseConvolutionLayer::prepare()
640{
Jenkins0e205f72019-11-28 16:53:35 +0000641 switch(_depth_conv_func)
Jenkins52ba29e2018-08-29 15:32:11 +0000642 {
Jenkins0e205f72019-11-28 16:53:35 +0000643 case DepthwiseConvolutionFunction::OPTIMIZED:
644 _func_3x3.prepare();
645 break;
646 case DepthwiseConvolutionFunction::GENERIC:
647 _func_generic.prepare();
648 break;
649 default:
650 ARM_COMPUTE_ERROR("DepthwiseConvolutionFunction not properly configured");
Jenkins52ba29e2018-08-29 15:32:11 +0000651 }
652}
Jenkins514be652019-02-28 12:25:18 +0000653} // namespace arm_compute