blob: fa2ca5de9ca35b7eb7c1cda7dbbc592aa6666229 [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +01001/*
Manuel Bottinica62c6f2021-03-23 11:50:34 +00002 * Copyright (c) 2017-2021 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
Gian Marco Iodice561c1762021-04-16 15:08:59 +010026#include "arm_compute/core/CL/CLHelpers.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010027#include "arm_compute/core/CL/ICLTensor.h"
Georgios Pinitas05045c12018-12-07 18:31:47 +000028#include "arm_compute/core/Helpers.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010029#include "arm_compute/core/PixelValue.h"
Gian Marco Iodice561c1762021-04-16 15:08:59 +010030#include "arm_compute/core/Utils.h"
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +000031#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000032#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010033#include "arm_compute/runtime/CL/CLScheduler.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010034#include "src/core/CL/kernels/CLDepthwiseConvolutionLayerNativeKernel.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010035
giuros016d109962019-01-07 17:47:19 +000036namespace arm_compute
37{
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000038using namespace arm_compute::misc;
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +000039using namespace arm_compute::misc::shape_calculator;
Giorgio Arena93a690e2017-08-01 16:09:33 +010040
Manuel Bottini05069f02019-09-26 17:18:26 +010041namespace
42{
Gian Marco Iodice561c1762021-04-16 15:08:59 +010043bool export_weights_to_cl_image_heuristic(const ITensorInfo *weights, unsigned int depth_multiplier, GPUTarget gpu_target)
Manuel Bottini05069f02019-09-26 17:18:26 +010044{
Gian Marco Iodice561c1762021-04-16 15:08:59 +010045 if(!export_weights_to_cl_image(weights))
Manuel Bottini05069f02019-09-26 17:18:26 +010046 {
Gian Marco Iodice561c1762021-04-16 15:08:59 +010047 return false;
48 }
Manuel Bottini05069f02019-09-26 17:18:26 +010049
Gian Marco Iodice561c1762021-04-16 15:08:59 +010050 if(depth_multiplier > 1)
51 {
52 return false;
53 }
54
55 if(gpu_target == GPUTarget::G71 || get_arch_from_target(gpu_target) == GPUTarget::MIDGARD)
56 {
57 return false;
58 }
59
60 return true;
61}
62
63void initialize_dwc_native_compute_info(DWCComputeKernelInfo &dwc_compute_info, const ITensorInfo *weights, const PadStrideInfo &conv_info, const Size2D &dilation, unsigned int depth_multiplier,
64 GPUTarget gpu_target)
65{
66 if(!is_data_type_float(weights->data_type()))
67 {
68 dwc_compute_info.export_weights_to_cl_image = false;
69 dwc_compute_info.n0 = (depth_multiplier == 1) ? 4 : 1;
70 if(conv_info.stride().first == 1 && dilation.x() == 1 && depth_multiplier == 1)
71 {
72 dwc_compute_info.m0 = 2;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010073 }
74 else
75 {
Gian Marco Iodice561c1762021-04-16 15:08:59 +010076 dwc_compute_info.m0 = 1;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010077 }
Gian Marco Iodice561c1762021-04-16 15:08:59 +010078
79 return;
Manuel Bottini05069f02019-09-26 17:18:26 +010080 }
81
Gian Marco Iodice561c1762021-04-16 15:08:59 +010082 // Floating point path
83
84 // First check if we can export to cl_image.
85 dwc_compute_info.export_weights_to_cl_image = export_weights_to_cl_image_heuristic(weights, depth_multiplier, gpu_target);
86
87 // Set n0
88 if(depth_multiplier == 1)
Manuel Bottini05069f02019-09-26 17:18:26 +010089 {
Gian Marco Iodice561c1762021-04-16 15:08:59 +010090 if(dwc_compute_info.export_weights_to_cl_image == false && weights->data_type() == DataType::F16)
91 {
92 dwc_compute_info.n0 = 8;
93 }
94 else
95 {
96 dwc_compute_info.n0 = 4;
97 }
Manuel Bottini05069f02019-09-26 17:18:26 +010098 }
99 else
100 {
Gian Marco Iodice561c1762021-04-16 15:08:59 +0100101 dwc_compute_info.n0 = 1;
Manuel Bottini05069f02019-09-26 17:18:26 +0100102 }
Gian Marco Iodice561c1762021-04-16 15:08:59 +0100103
104 dwc_compute_info.n0 = adjust_vec_size(dwc_compute_info.n0, weights->dimension(0));
105
106 // Set m0 only if stride_x == 1 and dilation_x == 1
107 if(conv_info.stride().first == 1 && dilation.x() == 1)
108 {
109 const size_t idx_w = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::WIDTH);
110 const size_t kernel_w = weights->tensor_shape()[idx_w];
111
112 dwc_compute_info.m0 = (kernel_w >= 9) || (kernel_w == 1) ? 1 : 2;
113 }
114 else
115 {
116 dwc_compute_info.m0 = 1;
117 }
118 return;
Manuel Bottini05069f02019-09-26 17:18:26 +0100119}
Gian Marco Iodice561c1762021-04-16 15:08:59 +0100120
Manuel Bottini05069f02019-09-26 17:18:26 +0100121} // namespace
122
Gian Marco Iodice561c1762021-04-16 15:08:59 +0100123CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Manuel Bottini05069f02019-09-26 17:18:26 +0100124 : _memory_group(std::move(memory_manager)),
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000125 _dwc_native_kernel(std::make_unique<CLDepthwiseConvolutionLayerNativeKernel>()),
Manuel Bottini05069f02019-09-26 17:18:26 +0100126 _permute_input_to_nhwc(),
127 _permute_weights_to_nhwc(),
128 _permute_output_to_nchw(),
129 _permuted_input(),
130 _permuted_weights(),
131 _permuted_output(),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100132 _output_multipliers(),
133 _output_shifts(),
Manuel Bottini05069f02019-09-26 17:18:26 +0100134 _original_weights(),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100135 _input(),
136 _output(),
Manuel Bottini05069f02019-09-26 17:18:26 +0100137 _needs_permute(false),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100138 _is_prepared(false),
139 _is_quantized(false)
Manuel Bottini05069f02019-09-26 17:18:26 +0100140{
141}
142
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100143CLDepthwiseConvolutionLayer::~CLDepthwiseConvolutionLayer() = default;
144
Gian Marco Iodice561c1762021-04-16 15:08:59 +0100145void CLDepthwiseConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
146 unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
Manuel Bottini05069f02019-09-26 17:18:26 +0100147{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100148 configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
149}
150
Gian Marco Iodice561c1762021-04-16 15:08:59 +0100151void CLDepthwiseConvolutionLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases,
152 ICLTensor *output, const PadStrideInfo &conv_info,
153 unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
Manuel Bottini2b84be52020-04-08 10:15:51 +0100154{
Manuel Bottini05069f02019-09-26 17:18:26 +0100155 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
156 ARM_COMPUTE_ERROR_THROW_ON(CLDepthwiseConvolutionLayer::validate(input->info(),
157 weights->info(),
158 biases != nullptr ? biases->info() : nullptr,
159 output->info(),
160 conv_info,
161 depth_multiplier,
162 act_info,
163 dilation));
164
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100165 _is_quantized = is_data_type_quantized(input->info()->data_type());
Manuel Bottini05069f02019-09-26 17:18:26 +0100166 _is_prepared = false;
167 _original_weights = weights;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100168 _input = input;
169 _output = output;
Manuel Bottini05069f02019-09-26 17:18:26 +0100170 _needs_permute = input->info()->data_layout() == DataLayout::NCHW;
171
Gian Marco Iodice561c1762021-04-16 15:08:59 +0100172 const GPUTarget gpu_target = CLScheduler::get().target();
173
Manuel Bottini05069f02019-09-26 17:18:26 +0100174 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
Gian Marco Iodice561c1762021-04-16 15:08:59 +0100212 DWCComputeKernelInfo dwc_native_compute_info;
213 initialize_dwc_native_compute_info(dwc_native_compute_info, weights_to_use->info(), conv_info, dilation, depth_multiplier, gpu_target);
214
215 const ConvolutionInfo conv_kernel_info{ conv_info, depth_multiplier, act_info, dilation };
216
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100217 _dwc_native_kernel->configure(compile_context, input_to_use, weights_to_use, biases, output_to_use,
Gian Marco Iodice561c1762021-04-16 15:08:59 +0100218 dwc_native_compute_info, conv_kernel_info, 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
Gian Marco Iodice561c1762021-04-16 15:08:59 +0100237Status CLDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
238 const PadStrideInfo &conv_info,
239 unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
Manuel Bottini05069f02019-09-26 17:18:26 +0100240{
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
Gian Marco Iodice561c1762021-04-16 15:08:59 +0100248 const GPUTarget gpu_target = CLScheduler::get().target();
249
250 const ConvolutionInfo conv_kernel_info{ conv_info, depth_multiplier, act_info, dilation };
Manuel Bottini05069f02019-09-26 17:18:26 +0100251
252 const bool needs_permute = input->data_layout() == DataLayout::NCHW;
253
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100254 const bool is_quantized = is_data_type_quantized(input->data_type());
255
256 TensorInfo output_multipliers_shifts_info(TensorInfo(TensorShape(1U), 1, DataType::S32));
257 if(is_quantized)
258 {
259 if(is_data_type_quantized_per_channel(weights->data_type()))
260 {
261 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QSYMM8_PER_CHANNEL);
262
263 const size_t idx_c = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::CHANNEL);
264 output_multipliers_shifts_info.set_tensor_shape(TensorShape(weights->dimension(idx_c)));
265 }
266 else
267 {
268 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
269 }
270 }
271
Manuel Bottini05069f02019-09-26 17:18:26 +0100272 if(needs_permute)
273 {
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100274 TensorShape permuted_input_shape = input->tensor_shape();
275 TensorShape permuted_weights_shape = weights->tensor_shape();
276 const ConvolutionInfo info{ conv_info, depth_multiplier, ActivationLayerInfo(), dilation };
277 TensorShape permuted_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, info);
Manuel Bottini05069f02019-09-26 17:18:26 +0100278
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)));
Gian Marco Iodice561c1762021-04-16 15:08:59 +0100289
290 DWCComputeKernelInfo dwc_native_compute_info;
291 initialize_dwc_native_compute_info(dwc_native_compute_info, &permuted_weights, conv_info, dilation, depth_multiplier, gpu_target);
292
293 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayerNativeKernel::validate(&permuted_input, &permuted_weights, biases, &permuted_output,
294 dwc_native_compute_info, conv_kernel_info, &output_multipliers_shifts_info, &output_multipliers_shifts_info));
Manuel Bottini05069f02019-09-26 17:18:26 +0100295 ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(&permuted_output, output, PermutationVector(1U, 2U, 0U)));
296 }
297 else
298 {
Gian Marco Iodice561c1762021-04-16 15:08:59 +0100299 DWCComputeKernelInfo dwc_native_compute_info;
300 initialize_dwc_native_compute_info(dwc_native_compute_info, weights, conv_info, dilation, depth_multiplier, gpu_target);
301 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayerNativeKernel::validate(input, weights, biases, output, dwc_native_compute_info, conv_kernel_info, &output_multipliers_shifts_info,
302 &output_multipliers_shifts_info));
Manuel Bottini05069f02019-09-26 17:18:26 +0100303 }
304 return Status{};
305}
306
Gian Marco Iodice561c1762021-04-16 15:08:59 +0100307void CLDepthwiseConvolutionLayer::run()
Manuel Bottini05069f02019-09-26 17:18:26 +0100308{
309 prepare();
310
311 MemoryGroupResourceScope scope_mg(_memory_group);
312
313 if(_needs_permute)
314 {
315 _permute_input_to_nhwc.run();
316 }
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100317 CLScheduler::get().enqueue(*_dwc_native_kernel);
Manuel Bottini05069f02019-09-26 17:18:26 +0100318 if(_needs_permute)
319 {
320 _permute_output_to_nchw.run();
321 }
322}
323
Gian Marco Iodice561c1762021-04-16 15:08:59 +0100324void CLDepthwiseConvolutionLayer::prepare()
Manuel Bottini05069f02019-09-26 17:18:26 +0100325{
326 if(!_is_prepared)
327 {
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100328 if(_is_quantized)
329 {
330 _output_multipliers.map();
331 _output_shifts.map();
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000332 quantization::compute_quantized_multipliers_and_shifts(_input->info(),
333 _original_weights->info(),
334 _output->info(),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100335 reinterpret_cast<int32_t *>(_output_multipliers.ptr_to_element(Coordinates(0))),
336 reinterpret_cast<int32_t *>(_output_shifts.ptr_to_element(Coordinates(0))));
337 _output_multipliers.unmap();
338 _output_shifts.unmap();
339 }
340
Manuel Bottini05069f02019-09-26 17:18:26 +0100341 if(_needs_permute)
342 {
343 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
344
345 _permuted_weights.allocator()->allocate();
346 _permute_weights_to_nhwc.run();
347 _original_weights->mark_as_unused();
348 }
349 _is_prepared = true;
350 }
351}
giuros016d109962019-01-07 17:47:19 +0000352} // namespace arm_compute