blob: e717f793fd6703c1a719d2de0032c76f21cae0cc [file] [log] [blame]
Anthony Barbier8140e1e2017-12-14 23:48:46 +00001/*
Jenkins514be652019-02-28 12:25:18 +00002 * Copyright (c) 2017-2019 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"
34#include "support/ToolchainSupport.h"
35
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
Jenkins514be652019-02-28 12:25:18 +0000120CLDepthwiseConvolutionLayer3x3::CLDepthwiseConvolutionLayer3x3(std::shared_ptr<IMemoryManager> memory_manager)
Jenkins0e205f72019-11-28 16:53:35 +0000121 : _func(std::move(memory_manager))
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000122{
123}
124
Jenkinsb3a371b2018-05-23 11:36:53 +0100125void CLDepthwiseConvolutionLayer3x3::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier,
Jenkins4ba87db2019-05-23 17:11:51 +0100126 ActivationLayerInfo act_info, const Size2D &dilation)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000127{
Jenkins0e205f72019-11-28 16:53:35 +0000128 _func.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
129}
Jenkins4ba87db2019-05-23 17:11:51 +0100130
Jenkins0e205f72019-11-28 16:53:35 +0000131Status 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}
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000136
Jenkins0e205f72019-11-28 16:53:35 +0000137void CLDepthwiseConvolutionLayer3x3::run()
138{
139 _func.run();
140}
Jenkins514be652019-02-28 12:25:18 +0000141
Jenkins0e205f72019-11-28 16:53:35 +0000142void 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(),
156 _output_multipliers(),
157 _output_shifts(),
158 _original_weights(),
159 _input(),
160 _output(),
161 _needs_permute(false),
162 _is_prepared(false),
163 _is_quantized(false)
164{
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
180 _is_quantized = is_data_type_quantized(input->info()->data_type());
Jenkins514be652019-02-28 12:25:18 +0000181 _is_prepared = false;
182 _original_weights = weights;
Jenkins0e205f72019-11-28 16:53:35 +0000183 _input = input;
184 _output = output;
185 _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
211 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
225 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;
229 _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);
232
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 }
242
243 if(_is_quantized)
244 {
245 _output_multipliers.allocator()->allocate();
246 _output_shifts.allocator()->allocate();
247 }
248}
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
268 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
286 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,
303 dwc_info, conv_info, depth_multiplier, dilation,
304 &output_multipliers_shifts_info, &output_multipliers_shifts_info));
305 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(&permuted_output, output, PermutationVector(1U, 2U, 0U)));
306 }
307 else
308 {
309 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));
311 }
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 {
336 if(_is_quantized)
337 {
338 _output_multipliers.map();
339 _output_shifts.map();
340 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,
345 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
351 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)
364 : _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)
383{
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{
389 const GPUTarget gpu_target = CLScheduler::get().target();
390
391 // 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));
402
403 const bool is_nhwc = input->info()->data_layout() == DataLayout::NHWC;
404 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
405 _needs_permute = is_nhwc && (depth_multiplier > 1);
406 _needs_weights_reshape = is_nhwc && (depth_multiplier == 1) && _is_quantized;
407
408 _is_prepared = false;
409 _original_weights = weights;
410 _input = input;
411 _output = output;
Jenkins514be652019-02-28 12:25:18 +0000412
413 ICLTensor *input_to_use = input;
414 const ICLTensor *weights_to_use = weights;
415 ICLTensor *output_to_use = output;
416
Jenkins0e205f72019-11-28 16:53:35 +0000417 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);
Jenkins4ba87db2019-05-23 17:11:51 +0100421
Jenkins514be652019-02-28 12:25:18 +0000422 DepthwiseConvolutionReshapeInfo info;
423 info.c0 = 4;
Jenkins4ba87db2019-05-23 17:11:51 +0100424 info.transpose = is_stride_1_dilation_1 && is_dot8_supported;
Jenkins514be652019-02-28 12:25:18 +0000425
426 if(_needs_permute)
Jenkinsb3a371b2018-05-23 11:36:53 +0100427 {
Jenkins514be652019-02-28 12:25:18 +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);
Jenkins975dfe12019-09-02 11:47:54 +0100438 _permuted_output.info()->set_quantization_info(output->info()->quantization_info());
Jenkins514be652019-02-28 12:25:18 +0000439
440 input_to_use = &_permuted_input;
441 weights_to_use = &_permuted_weights;
442 output_to_use = &_permuted_output;
443
Jenkinsb3a371b2018-05-23 11:36:53 +0100444 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>();
445 }
Jenkins514be652019-02-28 12:25:18 +0000446 else if(is_nhwc)
447 {
448 if(_needs_weights_reshape)
449 {
450 _reshape_weights.configure(weights, &_permuted_weights, info);
451 weights_to_use = &_permuted_weights;
452 }
453 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NHWCKernel>();
454 }
Jenkinsb3a371b2018-05-23 11:36:53 +0100455 else
456 {
Jenkins514be652019-02-28 12:25:18 +0000457 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>();
Jenkinsb3a371b2018-05-23 11:36:53 +0100458 }
459
Jenkins0e205f72019-11-28 16:53:35 +0000460 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
Jenkins514be652019-02-28 12:25:18 +0000474 // Configure kernel
Jenkins0e205f72019-11-28 16:53:35 +0000475 _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 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000484
Jenkins514be652019-02-28 12:25:18 +0000485 // 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
489 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
490 _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 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000496 // Configure border handler
497 PixelValue &&zero_value(0.f);
498 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
499 {
Jenkins975dfe12019-09-02 11:47:54 +0100500 zero_value = PixelValue(static_cast<uint8_t>(input->info()->quantization_info().uniform().offset));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000501 }
Jenkins514be652019-02-28 12:25:18 +0000502 _border_handler.configure(input_to_use, _kernel->border_size(), BorderMode::CONSTANT, zero_value);
Jenkinsb3a371b2018-05-23 11:36:53 +0100503}
504
Jenkins0e205f72019-11-28 16:53:35 +0000505Status 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)
Jenkinsb3a371b2018-05-23 11:36:53 +0100507{
Jenkins0e205f72019-11-28 16:53:35 +0000508 return validate_arguments_3x3(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000509}
510
Jenkins0e205f72019-11-28 16:53:35 +0000511void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::run()
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000512{
Jenkins514be652019-02-28 12:25:18 +0000513 prepare();
514
Jenkins4ba87db2019-05-23 17:11:51 +0100515 MemoryGroupResourceScope scope_mg(_memory_group);
Jenkins514be652019-02-28 12:25:18 +0000516
517 if(_needs_permute)
518 {
519 _permute_input_to_nchw.run();
520 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000521 CLScheduler::get().enqueue(_border_handler);
Jenkinsb3a371b2018-05-23 11:36:53 +0100522 CLScheduler::get().enqueue(*_kernel);
Jenkins514be652019-02-28 12:25:18 +0000523
524 if(_needs_permute)
525 {
526 _permute_output_to_nhwc.run();
527 }
Jenkins514be652019-02-28 12:25:18 +0000528}
529
Jenkins0e205f72019-11-28 16:53:35 +0000530void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::prepare()
Jenkins514be652019-02-28 12:25:18 +0000531{
532 if(!_is_prepared)
533 {
Jenkins0e205f72019-11-28 16:53:35 +0000534 if(_is_quantized)
535 {
536 _output_multipliers.map();
537 _output_shifts.map();
538 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,
543 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
Jenkins514be652019-02-28 12:25:18 +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 }
557
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 }
566 _is_prepared = true;
567 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000568}
569
Jenkins0e205f72019-11-28 16:53:35 +0000570CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
571 : _memory_manager(std::move(memory_manager)), _depth_conv_func(DepthwiseConvolutionFunction::GENERIC), _func_3x3(), _func_generic()
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000572{
573}
574
Jenkins0e205f72019-11-28 16:53:35 +0000575void 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)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000577{
Jenkins0e205f72019-11-28 16:53:35 +0000578 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)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000582 {
Jenkins0e205f72019-11-28 16:53:35 +0000583 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:
Jenkins514be652019-02-28 12:25:18 +0000588 {
Jenkins0e205f72019-11-28 16:53:35 +0000589 _func_generic.set_memory_group(_memory_manager);
590 _func_generic.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
Jenkins514be652019-02-28 12:25:18 +0000591 }
Jenkins0e205f72019-11-28 16:53:35 +0000592 break;
593 default:
594 ARM_COMPUTE_ERROR("Unsupported DepthwiseConvolutionFunction");
Jenkinsb9abeae2018-11-22 11:58:08 +0000595 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000596}
597
Jenkinsb3a371b2018-05-23 11:36:53 +0100598Status CLDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Jenkins0e205f72019-11-28 16:53:35 +0000599 unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
Jenkinsb3a371b2018-05-23 11:36:53 +0100600{
Jenkins0e205f72019-11-28 16:53:35 +0000601 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)
Jenkinsb3a371b2018-05-23 11:36:53 +0100604 {
Jenkins0e205f72019-11-28 16:53:35 +0000605 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}
Jenkinsb3a371b2018-05-23 11:36:53 +0100613
Jenkins0e205f72019-11-28 16:53:35 +0000614DepthwiseConvolutionFunction 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;
Jenkins514be652019-02-28 12:25:18 +0000622 }
623 else
Jenkinsb9abeae2018-11-22 11:58:08 +0000624 {
Jenkins0e205f72019-11-28 16:53:35 +0000625 return DepthwiseConvolutionFunction::GENERIC;
Jenkinsb9abeae2018-11-22 11:58:08 +0000626 }
Jenkinsb3a371b2018-05-23 11:36:53 +0100627}
628
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000629void CLDepthwiseConvolutionLayer::run()
630{
Jenkins0e205f72019-11-28 16:53:35 +0000631 switch(_depth_conv_func)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000632 {
Jenkins0e205f72019-11-28 16:53:35 +0000633 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");
Jenkinsb9abeae2018-11-22 11:58:08 +0000641 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000642}
Jenkins52ba29e2018-08-29 15:32:11 +0000643
644void CLDepthwiseConvolutionLayer::prepare()
645{
Jenkins0e205f72019-11-28 16:53:35 +0000646 switch(_depth_conv_func)
Jenkins52ba29e2018-08-29 15:32:11 +0000647 {
Jenkins0e205f72019-11-28 16:53:35 +0000648 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");
Jenkins52ba29e2018-08-29 15:32:11 +0000656 }
657}
Jenkins514be652019-02-28 12:25:18 +0000658} // namespace arm_compute