blob: 3bb69b1ffc2967ad9ef0794d9de6b977c5cd987f [file] [log] [blame]
Michalis Spyrou7362f0d2017-10-18 17:58:22 +01001/*
Georgios Pinitas2481d462019-02-19 18:47:46 +00002 * Copyright (c) 2017-2019 ARM Limited.
Michalis Spyrou7362f0d2017-10-18 17:58:22 +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/NEON/functions/NEDepthwiseConvolutionLayer.h"
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010025
26#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/ITensor.h"
28#include "arm_compute/core/PixelValue.h"
Georgios Pinitasd05dce42018-01-22 16:29:17 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitasf72f9362018-01-12 16:29:45 +000030#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010031#include "arm_compute/runtime/NEON/NEScheduler.h"
32#include "support/ToolchainSupport.h"
33
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000034#include "arm_compute/core/utils/misc/InfoHelpers.h"
35
Georgios Pinitasd05dce42018-01-22 16:29:17 +000036using namespace arm_compute::misc;
Georgios Pinitas4074c992018-01-30 18:13:46 +000037using namespace arm_compute::misc::shape_calculator;
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010038
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000039namespace arm_compute
40{
41NEDepthwiseConvolutionLayer3x3::NEDepthwiseConvolutionLayer3x3(std::shared_ptr<IMemoryManager> memory_manager)
42 : _memory_group(memory_manager), _dwc_kernel(), _dwc_optimized_func(memory_manager), _output_stage_kernel(), _border_handler(), _permute_input(), _permute_weights(), _permute_output(),
43 _activationlayer_function(), _accumulator(), _permuted_input(), _permuted_weights(), _permuted_output(), _original_weights(nullptr), _has_bias(false), _is_quantized(false), _is_optimized(false),
44 _is_nchw(true), _permute(false), _is_activationlayer_enabled(false), _is_prepared(false)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010045{
46}
47
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000048void NEDepthwiseConvolutionLayer3x3::configure_generic(ITensor *input,
49 const ITensor *weights,
50 const ITensor *biases,
51 ITensor *output,
52 const PadStrideInfo &conv_info,
53 unsigned int depth_multiplier,
Usama Arif881f2de2019-04-12 10:29:17 +010054 const ActivationLayerInfo &act_info,
55 const Size2D &dilation)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010056{
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000057 ARM_COMPUTE_UNUSED(act_info);
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010058
Georgios Pinitasf72f9362018-01-12 16:29:45 +000059 PixelValue zero_value(0.f);
60
Georgios Pinitasa799ce02018-09-12 20:11:34 +010061 // Initialize the intermediate accumulator tensor in case of quantized input
62 if(_is_quantized)
63 {
64 TensorShape accum_shape = output->info()->tensor_shape();
65 DataLayout accum_layout = output->info()->data_layout();
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000066 if(!_is_nchw)
Georgios Pinitasa799ce02018-09-12 20:11:34 +010067 {
68 permute(accum_shape, PermutationVector(1U, 2U, 0U));
69 accum_layout = DataLayout::NCHW;
70 }
71
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000072 _memory_group.manage(&_accumulator);
Georgios Pinitas2481d462019-02-19 18:47:46 +000073 _accumulator.allocator()->init(TensorInfo(accum_shape, 1, DataType::S32, output->info()->quantization_info()));
Georgios Pinitasa799ce02018-09-12 20:11:34 +010074 _accumulator.info()->set_data_layout(accum_layout);
75 zero_value = PixelValue(static_cast<uint32_t>(input->info()->quantization_info().offset));
76 }
77
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000078 if(!_is_nchw)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010079 {
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000080 _memory_group.manage(&_permuted_input);
81 _memory_group.manage(&_permuted_output);
Georgios Pinitas4074c992018-01-30 18:13:46 +000082
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000083 // Configure the function to transform the input tensor from NHWC -> NCHW
84 _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
85 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
Georgios Pinitas4074c992018-01-30 18:13:46 +000086
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000087 // Configure the function to transform the weights tensor from HWI -> IHW
88 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
89 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
Georgios Pinitas4074c992018-01-30 18:13:46 +000090
Usama Arif881f2de2019-04-12 10:29:17 +010091 // Configure depthwise
92 _dwc_kernel.configure(&_permuted_input, &_permuted_weights, (_is_quantized) ? &_accumulator : &_permuted_output, conv_info, depth_multiplier, dilation);
Georgios Pinitas4074c992018-01-30 18:13:46 +000093
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000094 // Configure border handler
95 _border_handler.configure(&_permuted_input, _dwc_kernel.border_size(), BorderMode::CONSTANT, zero_value);
96
97 // Allocate tensors
98 _permuted_input.allocator()->allocate();
Georgios Pinitasf72f9362018-01-12 16:29:45 +000099 }
Georgios Pinitas4074c992018-01-30 18:13:46 +0000100 else
101 {
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000102 // Configure depthwise convolution kernel
Usama Arif881f2de2019-04-12 10:29:17 +0100103 _dwc_kernel.configure(input, weights, (_is_quantized) ? &_accumulator : output, conv_info, depth_multiplier, dilation);
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000104
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000105 // Configure border handler
106 _border_handler.configure(input, _dwc_kernel.border_size(), BorderMode::CONSTANT, zero_value);
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100107 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100108
109 // Configure biases accumulation
110 if(_is_quantized)
111 {
112 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
113
114 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100115 int output_multiplier;
116 int output_shift;
Giorgio Arena26b22162018-08-13 15:49:49 +0100117 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000118 _output_stage_kernel.configure(&_accumulator, biases, _is_nchw ? output : &_permuted_output, output_multiplier, output_shift, output_quant_info.offset);
Giorgio Arena26b22162018-08-13 15:49:49 +0100119 _accumulator.allocator()->allocate();
120 }
121 else if(_has_bias)
122 {
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000123 _output_stage_kernel.configure(_is_nchw ? output : &_permuted_output, biases);
Giorgio Arena26b22162018-08-13 15:49:49 +0100124 }
125
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000126 // Permute output
127 if(!_is_nchw)
Giorgio Arena26b22162018-08-13 15:49:49 +0100128 {
129 // Configure the function to transform the convoluted output to NHWC
130 _permute_output.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
131 _permuted_output.allocator()->allocate();
132 }
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000133}
Georgios Pinitas60e98252018-10-22 16:17:20 +0100134
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000135void NEDepthwiseConvolutionLayer3x3::configure_optimized(const ITensor *input,
136 const ITensor *weights,
137 const ITensor *biases,
138 ITensor *output,
139 const PadStrideInfo &conv_info,
140 unsigned int depth_multiplier,
141 const ActivationLayerInfo &act_info)
142{
143 ActivationLayerInfo act_info_to_use = ActivationLayerInfo();
144 const bool is_relu = arm_compute::utils::info_helpers::is_relu(act_info);
145 const bool is_relu6 = arm_compute::utils::info_helpers::is_relu6(act_info);
146 _is_activationlayer_enabled = act_info.enabled() && !(is_relu || is_relu6);
147 if(!_is_activationlayer_enabled)
148 {
149 act_info_to_use = act_info;
150 }
151
152 if(_is_nchw)
153 {
154 _memory_group.manage(&_permuted_input);
155 _memory_group.manage(&_permuted_output);
156
157 // Configure the function to transform the input tensor from NCHW -> NHWC
158 _permute_input.configure(input, &_permuted_input, PermutationVector(2U, 0U, 1U));
159 _permuted_input.info()->set_data_layout(DataLayout::NHWC);
160
161 // Configure the function to transform the weights tensor from IHW -> HWI
162 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(2U, 0U, 1U));
163 _permuted_weights.info()->set_data_layout(DataLayout::NHWC);
164
165 // Configure optimized depthwise
166 _dwc_optimized_func.configure(&_permuted_input, &_permuted_weights, biases, &_permuted_output, conv_info, depth_multiplier, act_info_to_use);
167
168 // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
169 _permuted_output.info()->set_data_layout(DataLayout::NHWC);
170 _permute_output.configure(&_permuted_output, output, PermutationVector(1U, 2U, 0U));
171
172 // Allocate tensors
173 _permuted_input.allocator()->allocate();
174 _permuted_output.allocator()->allocate();
175 }
176 else
177 {
178 _dwc_optimized_func.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info_to_use);
179 }
180}
181
182void NEDepthwiseConvolutionLayer3x3::configure(ITensor *input,
183 const ITensor *weights,
184 const ITensor *biases,
185 ITensor *output, const PadStrideInfo &conv_info,
186 unsigned int depth_multiplier,
Usama Arife73686a2019-04-08 17:30:48 +0100187 const ActivationLayerInfo &act_info,
188 const Size2D &dilation)
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000189{
190 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
191 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
192
Usama Arif881f2de2019-04-12 10:29:17 +0100193 // idx_w and idx_h only used for validation
194 const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
195 const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
196 ARM_COMPUTE_UNUSED(idx_w);
197 ARM_COMPUTE_UNUSED(idx_h);
198
199 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(idx_w) + (weights->info()->dimension(idx_w) - 1) * (dilation.x() - 1) > input->info()->dimension(idx_w) + conv_info.pad_left() + conv_info.pad_right());
200 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(idx_h) + (weights->info()->dimension(idx_h) - 1) * (dilation.y() - 1) > input->info()->dimension(idx_h) + conv_info.pad_top() + conv_info.pad_bottom());
201
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000202 _original_weights = weights;
203 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
204 _has_bias = biases != nullptr;
205 _is_optimized = NEDepthwiseConvolutionAssemblyDispatch::is_optimized_supported(input->info(),
206 weights->info(),
207 conv_info,
Usama Arif881f2de2019-04-12 10:29:17 +0100208 depth_multiplier, dilation);
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000209 _is_nchw = input->info()->data_layout() == DataLayout::NCHW;
210 _permute = _is_optimized == _is_nchw;
211 _is_prepared = false;
Georgios Pinitas60e98252018-10-22 16:17:20 +0100212 _is_activationlayer_enabled = act_info.enabled();
213
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000214 // Configure appropriate pipeline
215 if(_is_optimized)
216 {
217 configure_optimized(input, weights, biases, output, conv_info, depth_multiplier, act_info);
218 }
219 else
220 {
Usama Arif881f2de2019-04-12 10:29:17 +0100221 configure_generic(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000222 }
223
224 // Configure activation
Georgios Pinitas60e98252018-10-22 16:17:20 +0100225 if(_is_activationlayer_enabled)
226 {
227 _activationlayer_function.configure(output, nullptr, act_info);
228 }
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100229}
230
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000231Status NEDepthwiseConvolutionLayer3x3::validate(const ITensorInfo *input,
232 const ITensorInfo *weights,
233 const ITensorInfo *biases,
234 const ITensorInfo *output,
235 const PadStrideInfo &conv_info,
236 unsigned int depth_multiplier,
Usama Arife73686a2019-04-08 17:30:48 +0100237 const ActivationLayerInfo &act_info,
238 const Size2D &dilation)
Abe Mbise7784c832018-05-31 16:48:41 +0100239{
240 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100241 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Usama Arif881f2de2019-04-12 10:29:17 +0100242 ARM_COMPUTE_RETURN_ERROR_ON(dilation.x() < 1 || dilation.y() < 1);
243 const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
244 const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
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());
Abe Mbise7784c832018-05-31 16:48:41 +0100247
Giorgio Arena66cbafb2018-08-23 14:51:00 +0100248 if(biases != nullptr)
249 {
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100250 const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
Giorgio Arena66cbafb2018-08-23 14:51:00 +0100251 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100252 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(channel_idx));
Giorgio Arena66cbafb2018-08-23 14:51:00 +0100253 }
254
Usama Arif881f2de2019-04-12 10:29:17 +0100255 if(!NEDepthwiseConvolutionAssemblyDispatch::is_optimized_supported(input, weights, conv_info, depth_multiplier, dilation))
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100256 {
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000257 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
258 TensorInfo accumulator = TensorInfo(output->clone()->set_is_resizable(true).reset_padding().set_data_type(DataType::S32));
259 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseConvolutionLayer3x3Kernel::validate(input, weights, is_quantized ? &accumulator : output, conv_info, depth_multiplier));
260
261 if(is_quantized)
262 {
263 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayerOutputStageKernel::validate(&accumulator, biases, output));
264 }
265 }
266 else
267 {
268 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseConvolutionAssemblyDispatch::validate(input, weights, biases, output, conv_info, depth_multiplier));
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100269 }
270
Georgios Pinitas60e98252018-10-22 16:17:20 +0100271 //Validate Activation Layer
272 if(act_info.enabled())
273 {
274 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
275 }
276
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100277 return Status{};
Abe Mbise7784c832018-05-31 16:48:41 +0100278}
279
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000280void NEDepthwiseConvolutionLayer3x3::run_generic()
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100281{
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000282 // Fill border
283 NEScheduler::get().schedule(&_border_handler, Window::DimX);
Georgios Pinitas4074c992018-01-30 18:13:46 +0000284
285 // Execute depthwise convolution
286 NEScheduler::get().schedule(&_dwc_kernel, Window::DimX);
287
Georgios Pinitas4074c992018-01-30 18:13:46 +0000288 // Add biases
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000289 if(_has_bias || _is_quantized)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100290 {
Michalis Spyroub91e34c2017-12-20 15:50:55 +0000291 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100292 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100293
294 // Permute output
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000295 if(!_is_nchw)
Giorgio Arena26b22162018-08-13 15:49:49 +0100296 {
297 _permute_output.run();
298 }
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000299}
Georgios Pinitas60e98252018-10-22 16:17:20 +0100300
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000301void NEDepthwiseConvolutionLayer3x3::run_optimized()
302{
303 // Run assembly function
304 _dwc_optimized_func.run();
305
306 // Permute output
307 if(_is_nchw)
308 {
309 _permute_output.run();
310 }
311}
312
313void NEDepthwiseConvolutionLayer3x3::run()
314{
315 prepare();
316
Georgios Pinitasda953f22019-04-02 17:27:03 +0100317 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000318
319 // Permute input
320 if(_permute)
321 {
322 _permute_input.run();
323 }
324
325 _is_optimized ? run_optimized() : run_generic();
326
327 // Run activation
Georgios Pinitas60e98252018-10-22 16:17:20 +0100328 if(_is_activationlayer_enabled)
329 {
330 _activationlayer_function.run();
331 }
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000332}
333
334void NEDepthwiseConvolutionLayer3x3::prepare()
335{
336 if(!_is_prepared)
337 {
338 // Permute weights
339 if(_permute)
340 {
341 _permuted_weights.allocator()->allocate();
342 _permute_weights.run();
343 _original_weights->mark_as_unused();
344 }
345
346 // Prepare optimized function
347 if(_is_optimized)
348 {
349 _dwc_optimized_func.prepare();
350 if(!_permuted_weights.is_used())
351 {
352 _permuted_weights.allocator()->free();
353 }
354 }
355
356 _is_prepared = true;
357 }
Michalis Spyroub7b31532017-11-23 12:10:21 +0000358}
359
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000360NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer()
Giorgio Arena26b22162018-08-13 15:49:49 +0100361 : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _v2mm_input_fill_border(), _v2mm_weights_fill_border(), _permute_input(),
Georgios Pinitas60e98252018-10-22 16:17:20 +0100362 _permute_weights(), _permute_output(), _activationlayer_function(), _input_reshaped(), _weights_reshaped(), _v2mm_output(), _output_reshaped(), _permuted_input(), _permuted_weights(),
363 _permuted_output(), _is_prepared(false), _is_quantized(false), _is_nhwc(false), _is_activationlayer_enabled(false), _original_weights(nullptr)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000364{
365}
366
Georgios Pinitas60e98252018-10-22 16:17:20 +0100367void NEDepthwiseConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
Usama Arife73686a2019-04-08 17:30:48 +0100368 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000369{
Giorgio Arena26b22162018-08-13 15:49:49 +0100370 const unsigned int channel_idx = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL);
371 ARM_COMPUTE_UNUSED(channel_idx);
Georgios Pinitas8cffcd62018-11-16 17:11:50 +0000372 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000373 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Giorgio Arena26b22162018-08-13 15:49:49 +0100374 ARM_COMPUTE_ERROR_ON((input->info()->dimension(channel_idx) * depth_multiplier) != weights->info()->dimension(channel_idx));
Usama Arif881f2de2019-04-12 10:29:17 +0100375 // idx_w and idx_h only used for validation
376 const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
377 const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
378 ARM_COMPUTE_UNUSED(idx_w);
379 ARM_COMPUTE_UNUSED(idx_h);
380
381 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(idx_w) + (weights->info()->dimension(idx_w) - 1) * (dilation.x() - 1) > input->info()->dimension(idx_w) + conv_info.pad_left() + conv_info.pad_right());
382 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(idx_h) + (weights->info()->dimension(idx_h) - 1) * (dilation.y() - 1) > input->info()->dimension(idx_h) + conv_info.pad_top() + conv_info.pad_bottom());
Michalis Spyroub7b31532017-11-23 12:10:21 +0000383
Giorgio Arena26b22162018-08-13 15:49:49 +0100384 _is_nhwc = input->info()->data_layout() == DataLayout::NHWC;
385
386 ITensor *input_to_use = input;
387 const ITensor *weights_to_use = weights;
388 ITensor *output_to_use = output;
389
390 if(_is_nhwc)
391 {
392 _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
393 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
394 input_to_use = &_permuted_input;
395
396 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
397 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
398 weights_to_use = &_permuted_weights;
399 }
400
401 const size_t weights_w = weights_to_use->info()->dimension(0);
402 const size_t weights_h = weights_to_use->info()->dimension(1);
403 const size_t weights_z = weights_to_use->info()->dimension(2);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000404
Georgios Pinitas1562be32018-03-08 19:09:19 +0000405 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Georgios Pinitas72219332018-06-05 14:56:06 +0100406 _is_prepared = false;
Giorgio Arena26b22162018-08-13 15:49:49 +0100407 _original_weights = weights_to_use;
Michalis Spyroub7b31532017-11-23 12:10:21 +0000408
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000409 // Should bias be appended ?
410 bool append_bias = (biases != nullptr) && !_is_quantized;
411
412 // Calculate output shape
Usama Arif881f2de2019-04-12 10:29:17 +0100413 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier, dilation);
Giorgio Arena76572242018-04-04 17:44:26 +0100414
415 // Output auto inizialitation if not yet initialized
416 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
417 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000418
Giorgio Arena26b22162018-08-13 15:49:49 +0100419 if(_is_nhwc)
420 {
421 permute(output_shape, PermutationVector(1U, 2U, 0U));
422 _permuted_output.allocator()->init(output->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
423 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
424 output_to_use = &_permuted_output;
425 }
426
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000427 // Output width and height
Giorgio Arena76572242018-04-04 17:44:26 +0100428 const unsigned int conv_w = output_shape.x();
429 const unsigned int conv_h = output_shape.y();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000430
431 // Set up intermediate tensors
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000432 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000433 const size_t conv_size = conv_w * conv_h;
434
435 // Im2Col configuration
Giorgio Arena26b22162018-08-13 15:49:49 +0100436 TensorShape shape_im2col = input_to_use->info()->tensor_shape();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000437 shape_im2col.set(0, patch_size);
438 shape_im2col.set(1, conv_size);
439 shape_im2col.set(2, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100440 _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col).set_data_layout(DataLayout::NCHW));
Usama Arif881f2de2019-04-12 10:29:17 +0100441 _im2col_kernel.configure(input_to_use, &_input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier, dilation);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000442
443 // Weights reshape configuration
444 const TensorShape shape_weights_reshape(patch_size, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100445 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW));
446 _weights_reshape_kernel.configure(weights_to_use, &_weights_reshaped, append_bias ? biases : nullptr);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000447
448 // GEMV configuration
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000449 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
Giorgio Arena26b22162018-08-13 15:49:49 +0100450 TensorShape shape_v2mm_out = input_to_use->info()->tensor_shape();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000451 shape_v2mm_out.set(0, conv_size * weights_z);
452 shape_v2mm_out.set(1, 1);
453 shape_v2mm_out.set(2, 1);
Giorgio Arena26b22162018-08-13 15:49:49 +0100454 _v2mm_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out).set_data_layout(DataLayout::NCHW));
Michalis Spyroub7b31532017-11-23 12:10:21 +0000455 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
Giorgio Arena76572242018-04-04 17:44:26 +0100456 _output_reshaped.allocator()->init(_v2mm_output.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
Giorgio Arena26b22162018-08-13 15:49:49 +0100457 _vector_to_tensor_kernel.configure(&_v2mm_output, (_is_quantized) ? &_output_reshaped : output_to_use, conv_w, conv_h);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000458
459 // Output staged configuration
460 if(_is_quantized)
461 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100462 const QuantizationInfo output_quant_info = output->info()->quantization_info();
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000463
464 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100465 int output_multiplier;
466 int output_shift;
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000467 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Giorgio Arena26b22162018-08-13 15:49:49 +0100468 _output_stage_kernel.configure(&_output_reshaped, biases, output_to_use, output_multiplier, output_shift, output_quant_info.offset);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000469 _output_reshaped.allocator()->allocate();
470 }
471
Giorgio Arena26b22162018-08-13 15:49:49 +0100472 if(_is_nhwc)
473 {
474 _permute_output.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
475
476 _permuted_input.allocator()->allocate();
477 _permuted_weights.allocator()->allocate();
478 _permuted_output.allocator()->allocate();
479 }
480
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000481 // Fill borders on inputs
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000482 PixelValue zero_in(static_cast<int32_t>(0));
483 PixelValue zero_w(static_cast<int32_t>(0));
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000484 if(_is_quantized)
485 {
486 zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset));
487 zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset));
488 }
489 BorderSize border_size = _v2mm_kernel.border_size();
490 _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in);
491
492 border_size.bottom = 0;
493 _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000494
495 // Allocate intermediate tensors
496 _input_reshaped.allocator()->allocate();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000497 _v2mm_output.allocator()->allocate();
Georgios Pinitas60e98252018-10-22 16:17:20 +0100498
499 //Configure Activation Layer
500 _is_activationlayer_enabled = act_info.enabled();
501
502 if(_is_activationlayer_enabled)
503 {
504 _activationlayer_function.configure(output, nullptr, act_info);
505 }
Michalis Spyroub7b31532017-11-23 12:10:21 +0000506}
507
Georgios Pinitas10490202018-08-17 17:16:06 +0100508Status NEDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Usama Arife73686a2019-04-08 17:30:48 +0100509 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
Abe Mbise7784c832018-05-31 16:48:41 +0100510{
511 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100512 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Usama Arif881f2de2019-04-12 10:29:17 +0100513 ARM_COMPUTE_RETURN_ERROR_ON(dilation.x() < 1 || dilation.y() < 1);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100514
515 const unsigned int width_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
516 const unsigned int height_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
Abe Mbise7784c832018-05-31 16:48:41 +0100517
Usama Arif881f2de2019-04-12 10:29:17 +0100518 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(width_idx) + (weights->dimension(width_idx) - 1) * (dilation.x() - 1) > input->dimension(width_idx) + conv_info.pad_left() + conv_info.pad_right());
519 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(height_idx) + (weights->dimension(height_idx) - 1) * (dilation.y() - 1) > input->dimension(height_idx) + conv_info.pad_top() + conv_info.pad_bottom());
Georgios Pinitas10490202018-08-17 17:16:06 +0100520 // Clone output to use auto init
521 auto output_clone = output->clone();
522
Giorgio Arena26b22162018-08-13 15:49:49 +0100523 const ITensorInfo *input_to_use = input;
524 const ITensorInfo *weights_to_use = weights;
Georgios Pinitas10490202018-08-17 17:16:06 +0100525 const ITensorInfo *output_to_use = output_clone.get();
Giorgio Arena26b22162018-08-13 15:49:49 +0100526
527 TensorShape permuted_input_shape = input->tensor_shape();
528 TensorShape permuted_weights_shape = weights->tensor_shape();
529 TensorInfo permuted_input;
530 TensorInfo permuted_weights;
531
532 if(input->data_layout() == DataLayout::NHWC)
533 {
534 permute(permuted_input_shape, PermutationVector(1U, 2U, 0U));
535 permute(permuted_weights_shape, PermutationVector(1U, 2U, 0U));
536
537 permuted_input = TensorInfo(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NCHW));
538 permuted_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NCHW));
539
540 input_to_use = &permuted_input;
541 weights_to_use = &permuted_weights;
542 }
543
Abe Mbise7784c832018-05-31 16:48:41 +0100544 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
545 const bool append_bias = (biases != nullptr) && !is_quantized;
Usama Arif881f2de2019-04-12 10:29:17 +0100546 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);
Giorgio Arena26b22162018-08-13 15:49:49 +0100547 const size_t weights_w = weights_to_use->dimension(0);
548 const size_t weights_h = weights_to_use->dimension(1);
549 const size_t weights_z = weights_to_use->dimension(2);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100550 const unsigned int conv_w = output_shape[width_idx];
551 const unsigned int conv_h = output_shape[height_idx];
Abe Mbise7784c832018-05-31 16:48:41 +0100552 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
553 const size_t conv_size = conv_w * conv_h;
554
Giorgio Arena26b22162018-08-13 15:49:49 +0100555 // Output auto inizialitation if not yet initialized
Georgios Pinitas10490202018-08-17 17:16:06 +0100556 auto_init_if_empty(*output_clone, input->clone()->set_tensor_shape(output_shape));
Abe Mbise7784c832018-05-31 16:48:41 +0100557 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
558
Giorgio Arena26b22162018-08-13 15:49:49 +0100559 TensorInfo permuted_output;
560 if(input->data_layout() == DataLayout::NHWC)
561 {
562 permute(output_shape, PermutationVector(1U, 2U, 0U));
Georgios Pinitas10490202018-08-17 17:16:06 +0100563 permuted_output = TensorInfo(output_clone->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape).set_data_layout(DataLayout::NCHW));
Giorgio Arena26b22162018-08-13 15:49:49 +0100564 output_to_use = &permuted_output;
565 }
566
Abe Mbise7784c832018-05-31 16:48:41 +0100567 // Im2Col configuration
Giorgio Arena26b22162018-08-13 15:49:49 +0100568 TensorShape shape_im2col = input_to_use->tensor_shape();
Abe Mbise7784c832018-05-31 16:48:41 +0100569 shape_im2col.set(0, patch_size);
570 shape_im2col.set(1, conv_size);
571 shape_im2col.set(2, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100572 TensorInfo input_reshaped(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col).set_data_layout(DataLayout::NCHW));
Usama Arif881f2de2019-04-12 10:29:17 +0100573 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseIm2ColKernel::validate(input_to_use, &input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier, dilation));
Abe Mbise7784c832018-05-31 16:48:41 +0100574
575 // Weights reshape configuration
576 const TensorShape shape_weights_reshape(patch_size, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100577 TensorInfo weights_reshaped(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW));
578 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseWeightsReshapeKernel::validate(weights_to_use, &weights_reshaped, append_bias ? biases : nullptr));
Abe Mbise7784c832018-05-31 16:48:41 +0100579
580 // GEMV configuration
581 DataType v2mm_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type();
Giorgio Arena26b22162018-08-13 15:49:49 +0100582 TensorShape shape_v2mm_out = input_to_use->tensor_shape();
Abe Mbise7784c832018-05-31 16:48:41 +0100583 shape_v2mm_out.set(0, conv_size * weights_z);
584 shape_v2mm_out.set(1, 1);
585 shape_v2mm_out.set(2, 1);
Giorgio Arena26b22162018-08-13 15:49:49 +0100586 TensorInfo v2mm_output(input->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out).set_data_layout(DataLayout::NCHW));
Abe Mbise7784c832018-05-31 16:48:41 +0100587 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixVectorMultiplyKernel::validate(&input_reshaped, &weights_reshaped, &v2mm_output));
588
Giorgio Arena26b22162018-08-13 15:49:49 +0100589 TensorInfo output_reshaped(v2mm_output.clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_to_use->tensor_shape()));
590 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseVectorToTensorKernel::validate(&v2mm_output, (is_quantized) ? &output_reshaped : output_to_use, conv_w, conv_h));
Abe Mbise7784c832018-05-31 16:48:41 +0100591
592 if(is_quantized)
593 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100594 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayerOutputStageKernel::validate(&output_reshaped, biases, output_to_use));
Abe Mbise7784c832018-05-31 16:48:41 +0100595 }
596
Georgios Pinitas60e98252018-10-22 16:17:20 +0100597 // Validate Activation Layer
598 if(act_info.enabled())
599 {
600 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
601 }
602
Abe Mbise7784c832018-05-31 16:48:41 +0100603 return Status{};
604}
605
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000606void NEDepthwiseConvolutionLayer::run()
Michalis Spyroub7b31532017-11-23 12:10:21 +0000607{
Georgios Pinitas72219332018-06-05 14:56:06 +0100608 prepare();
Georgios Pinitas1562be32018-03-08 19:09:19 +0000609
Giorgio Arena26b22162018-08-13 15:49:49 +0100610 if(_is_nhwc)
611 {
612 _permute_input.run();
613 }
614
Michalis Spyroub7b31532017-11-23 12:10:21 +0000615 NEScheduler::get().schedule(&_im2col_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000616 NEScheduler::get().schedule(&_v2mm_input_fill_border, Window::DimX);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000617 NEScheduler::get().schedule(&_v2mm_kernel, Window::DimX);
618 NEScheduler::get().schedule(&_vector_to_tensor_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000619 if(_is_quantized)
620 {
621 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
622 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100623
624 if(_is_nhwc)
625 {
626 _permute_output.run();
627 }
Georgios Pinitas60e98252018-10-22 16:17:20 +0100628
629 if(_is_activationlayer_enabled)
630 {
631 _activationlayer_function.run();
632 }
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000633}
Georgios Pinitas72219332018-06-05 14:56:06 +0100634
635void NEDepthwiseConvolutionLayer::prepare()
636{
637 if(!_is_prepared)
638 {
639 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
640
Giorgio Arena26b22162018-08-13 15:49:49 +0100641 if(_is_nhwc)
642 {
643 _permute_weights.run();
644 }
645
Georgios Pinitas72219332018-06-05 14:56:06 +0100646 // Run reshape and mark original weights as unused
647 _weights_reshaped.allocator()->allocate();
648 NEScheduler::get().schedule(&_weights_reshape_kernel, Window::DimX);
649 NEScheduler::get().schedule(&_v2mm_weights_fill_border, Window::DimX);
650 _original_weights->mark_as_unused();
651
652 _is_prepared = true;
653 }
654}
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000655} // namespace arm_compute