blob: 4bc8439d93f1c418a3216af03b3c843d867c89a7 [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);
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010075 zero_value = PixelValue(static_cast<uint32_t>(input->info()->quantization_info().uniform().offset));
Georgios Pinitasa799ce02018-09-12 20:11:34 +010076 }
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 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100112 const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform();
113 const UniformQuantizationInfo wq_info = weights->info()->quantization_info().uniform();
114 const UniformQuantizationInfo oq_info = (output->info()->total_size() == 0) ? iq_info : output->info()->quantization_info().uniform();
Giorgio Arena26b22162018-08-13 15:49:49 +0100115
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100116 float multiplier = (iq_info.scale * wq_info.scale) / oq_info.scale;
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100117 int output_multiplier;
118 int output_shift;
Giorgio Arena26b22162018-08-13 15:49:49 +0100119 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100120 _output_stage_kernel.configure(&_accumulator, biases, _is_nchw ? output : &_permuted_output, output_multiplier, output_shift, oq_info.offset);
Giorgio Arena26b22162018-08-13 15:49:49 +0100121 _accumulator.allocator()->allocate();
122 }
123 else if(_has_bias)
124 {
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000125 _output_stage_kernel.configure(_is_nchw ? output : &_permuted_output, biases);
Giorgio Arena26b22162018-08-13 15:49:49 +0100126 }
127
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000128 // Permute output
129 if(!_is_nchw)
Giorgio Arena26b22162018-08-13 15:49:49 +0100130 {
131 // Configure the function to transform the convoluted output to NHWC
132 _permute_output.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
133 _permuted_output.allocator()->allocate();
134 }
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000135}
Georgios Pinitas60e98252018-10-22 16:17:20 +0100136
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000137void NEDepthwiseConvolutionLayer3x3::configure_optimized(const ITensor *input,
138 const ITensor *weights,
139 const ITensor *biases,
140 ITensor *output,
141 const PadStrideInfo &conv_info,
142 unsigned int depth_multiplier,
143 const ActivationLayerInfo &act_info)
144{
145 ActivationLayerInfo act_info_to_use = ActivationLayerInfo();
146 const bool is_relu = arm_compute::utils::info_helpers::is_relu(act_info);
147 const bool is_relu6 = arm_compute::utils::info_helpers::is_relu6(act_info);
148 _is_activationlayer_enabled = act_info.enabled() && !(is_relu || is_relu6);
149 if(!_is_activationlayer_enabled)
150 {
151 act_info_to_use = act_info;
152 }
153
154 if(_is_nchw)
155 {
156 _memory_group.manage(&_permuted_input);
157 _memory_group.manage(&_permuted_output);
158
159 // Configure the function to transform the input tensor from NCHW -> NHWC
160 _permute_input.configure(input, &_permuted_input, PermutationVector(2U, 0U, 1U));
161 _permuted_input.info()->set_data_layout(DataLayout::NHWC);
162
163 // Configure the function to transform the weights tensor from IHW -> HWI
164 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(2U, 0U, 1U));
165 _permuted_weights.info()->set_data_layout(DataLayout::NHWC);
166
167 // Configure optimized depthwise
168 _dwc_optimized_func.configure(&_permuted_input, &_permuted_weights, biases, &_permuted_output, conv_info, depth_multiplier, act_info_to_use);
169
170 // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
171 _permuted_output.info()->set_data_layout(DataLayout::NHWC);
172 _permute_output.configure(&_permuted_output, output, PermutationVector(1U, 2U, 0U));
173
174 // Allocate tensors
175 _permuted_input.allocator()->allocate();
176 _permuted_output.allocator()->allocate();
177 }
178 else
179 {
180 _dwc_optimized_func.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info_to_use);
181 }
182}
183
184void NEDepthwiseConvolutionLayer3x3::configure(ITensor *input,
185 const ITensor *weights,
186 const ITensor *biases,
187 ITensor *output, const PadStrideInfo &conv_info,
188 unsigned int depth_multiplier,
Usama Arife73686a2019-04-08 17:30:48 +0100189 const ActivationLayerInfo &act_info,
190 const Size2D &dilation)
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000191{
192 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
193 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
194
Usama Arif881f2de2019-04-12 10:29:17 +0100195 // idx_w and idx_h only used for validation
196 const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
197 const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
198 ARM_COMPUTE_UNUSED(idx_w);
199 ARM_COMPUTE_UNUSED(idx_h);
200
201 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());
202 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());
203
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000204 _original_weights = weights;
205 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
206 _has_bias = biases != nullptr;
207 _is_optimized = NEDepthwiseConvolutionAssemblyDispatch::is_optimized_supported(input->info(),
208 weights->info(),
209 conv_info,
Usama Arif881f2de2019-04-12 10:29:17 +0100210 depth_multiplier, dilation);
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000211 _is_nchw = input->info()->data_layout() == DataLayout::NCHW;
212 _permute = _is_optimized == _is_nchw;
213 _is_prepared = false;
Georgios Pinitas60e98252018-10-22 16:17:20 +0100214 _is_activationlayer_enabled = act_info.enabled();
215
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000216 // Configure appropriate pipeline
217 if(_is_optimized)
218 {
219 configure_optimized(input, weights, biases, output, conv_info, depth_multiplier, act_info);
220 }
221 else
222 {
Usama Arif881f2de2019-04-12 10:29:17 +0100223 configure_generic(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000224 }
225
226 // Configure activation
Georgios Pinitas60e98252018-10-22 16:17:20 +0100227 if(_is_activationlayer_enabled)
228 {
229 _activationlayer_function.configure(output, nullptr, act_info);
230 }
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100231}
232
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000233Status NEDepthwiseConvolutionLayer3x3::validate(const ITensorInfo *input,
234 const ITensorInfo *weights,
235 const ITensorInfo *biases,
236 const ITensorInfo *output,
237 const PadStrideInfo &conv_info,
238 unsigned int depth_multiplier,
Usama Arife73686a2019-04-08 17:30:48 +0100239 const ActivationLayerInfo &act_info,
240 const Size2D &dilation)
Abe Mbise7784c832018-05-31 16:48:41 +0100241{
242 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100243 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Usama Arif881f2de2019-04-12 10:29:17 +0100244 ARM_COMPUTE_RETURN_ERROR_ON(dilation.x() < 1 || dilation.y() < 1);
245 const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
246 const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
247 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());
248 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 +0100249
Giorgio Arena66cbafb2018-08-23 14:51:00 +0100250 if(biases != nullptr)
251 {
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100252 const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
Giorgio Arena66cbafb2018-08-23 14:51:00 +0100253 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100254 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(channel_idx));
Giorgio Arena66cbafb2018-08-23 14:51:00 +0100255 }
256
Usama Arif881f2de2019-04-12 10:29:17 +0100257 if(!NEDepthwiseConvolutionAssemblyDispatch::is_optimized_supported(input, weights, conv_info, depth_multiplier, dilation))
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100258 {
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000259 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
260 TensorInfo accumulator = TensorInfo(output->clone()->set_is_resizable(true).reset_padding().set_data_type(DataType::S32));
261 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseConvolutionLayer3x3Kernel::validate(input, weights, is_quantized ? &accumulator : output, conv_info, depth_multiplier));
262
263 if(is_quantized)
264 {
265 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayerOutputStageKernel::validate(&accumulator, biases, output));
266 }
267 }
268 else
269 {
270 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseConvolutionAssemblyDispatch::validate(input, weights, biases, output, conv_info, depth_multiplier));
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100271 }
272
Georgios Pinitas60e98252018-10-22 16:17:20 +0100273 //Validate Activation Layer
274 if(act_info.enabled())
275 {
276 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
277 }
278
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100279 return Status{};
Abe Mbise7784c832018-05-31 16:48:41 +0100280}
281
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000282void NEDepthwiseConvolutionLayer3x3::run_generic()
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100283{
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000284 // Fill border
285 NEScheduler::get().schedule(&_border_handler, Window::DimX);
Georgios Pinitas4074c992018-01-30 18:13:46 +0000286
287 // Execute depthwise convolution
288 NEScheduler::get().schedule(&_dwc_kernel, Window::DimX);
289
Georgios Pinitas4074c992018-01-30 18:13:46 +0000290 // Add biases
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000291 if(_has_bias || _is_quantized)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100292 {
Michalis Spyroub91e34c2017-12-20 15:50:55 +0000293 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100294 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100295
296 // Permute output
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000297 if(!_is_nchw)
Giorgio Arena26b22162018-08-13 15:49:49 +0100298 {
299 _permute_output.run();
300 }
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000301}
Georgios Pinitas60e98252018-10-22 16:17:20 +0100302
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000303void NEDepthwiseConvolutionLayer3x3::run_optimized()
304{
305 // Run assembly function
306 _dwc_optimized_func.run();
307
308 // Permute output
309 if(_is_nchw)
310 {
311 _permute_output.run();
312 }
313}
314
315void NEDepthwiseConvolutionLayer3x3::run()
316{
317 prepare();
318
Georgios Pinitasda953f22019-04-02 17:27:03 +0100319 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000320
321 // Permute input
322 if(_permute)
323 {
324 _permute_input.run();
325 }
326
327 _is_optimized ? run_optimized() : run_generic();
328
329 // Run activation
Georgios Pinitas60e98252018-10-22 16:17:20 +0100330 if(_is_activationlayer_enabled)
331 {
332 _activationlayer_function.run();
333 }
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000334}
335
336void NEDepthwiseConvolutionLayer3x3::prepare()
337{
338 if(!_is_prepared)
339 {
340 // Permute weights
341 if(_permute)
342 {
343 _permuted_weights.allocator()->allocate();
344 _permute_weights.run();
345 _original_weights->mark_as_unused();
346 }
347
348 // Prepare optimized function
349 if(_is_optimized)
350 {
351 _dwc_optimized_func.prepare();
352 if(!_permuted_weights.is_used())
353 {
354 _permuted_weights.allocator()->free();
355 }
356 }
357
358 _is_prepared = true;
359 }
Michalis Spyroub7b31532017-11-23 12:10:21 +0000360}
361
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000362NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer()
Giorgio Arena26b22162018-08-13 15:49:49 +0100363 : _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 +0100364 _permute_weights(), _permute_output(), _activationlayer_function(), _input_reshaped(), _weights_reshaped(), _v2mm_output(), _output_reshaped(), _permuted_input(), _permuted_weights(),
365 _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 +0000366{
367}
368
Georgios Pinitas60e98252018-10-22 16:17:20 +0100369void NEDepthwiseConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
Usama Arife73686a2019-04-08 17:30:48 +0100370 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000371{
Giorgio Arena26b22162018-08-13 15:49:49 +0100372 const unsigned int channel_idx = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL);
373 ARM_COMPUTE_UNUSED(channel_idx);
Georgios Pinitas8cffcd62018-11-16 17:11:50 +0000374 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000375 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Giorgio Arena26b22162018-08-13 15:49:49 +0100376 ARM_COMPUTE_ERROR_ON((input->info()->dimension(channel_idx) * depth_multiplier) != weights->info()->dimension(channel_idx));
Usama Arif881f2de2019-04-12 10:29:17 +0100377 // idx_w and idx_h only used for validation
378 const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
379 const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
380 ARM_COMPUTE_UNUSED(idx_w);
381 ARM_COMPUTE_UNUSED(idx_h);
382
383 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());
384 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 +0000385
Giorgio Arena26b22162018-08-13 15:49:49 +0100386 _is_nhwc = input->info()->data_layout() == DataLayout::NHWC;
387
388 ITensor *input_to_use = input;
389 const ITensor *weights_to_use = weights;
390 ITensor *output_to_use = output;
391
392 if(_is_nhwc)
393 {
394 _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
395 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
396 input_to_use = &_permuted_input;
397
398 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
399 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
400 weights_to_use = &_permuted_weights;
401 }
402
403 const size_t weights_w = weights_to_use->info()->dimension(0);
404 const size_t weights_h = weights_to_use->info()->dimension(1);
405 const size_t weights_z = weights_to_use->info()->dimension(2);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000406
Georgios Pinitas1562be32018-03-08 19:09:19 +0000407 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Georgios Pinitas72219332018-06-05 14:56:06 +0100408 _is_prepared = false;
Giorgio Arena26b22162018-08-13 15:49:49 +0100409 _original_weights = weights_to_use;
Michalis Spyroub7b31532017-11-23 12:10:21 +0000410
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000411 // Should bias be appended ?
412 bool append_bias = (biases != nullptr) && !_is_quantized;
413
414 // Calculate output shape
Usama Arif881f2de2019-04-12 10:29:17 +0100415 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 +0100416
417 // Output auto inizialitation if not yet initialized
418 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
419 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000420
Giorgio Arena26b22162018-08-13 15:49:49 +0100421 if(_is_nhwc)
422 {
423 permute(output_shape, PermutationVector(1U, 2U, 0U));
424 _permuted_output.allocator()->init(output->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
425 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
426 output_to_use = &_permuted_output;
427 }
428
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000429 // Output width and height
Giorgio Arena76572242018-04-04 17:44:26 +0100430 const unsigned int conv_w = output_shape.x();
431 const unsigned int conv_h = output_shape.y();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000432
433 // Set up intermediate tensors
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000434 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000435 const size_t conv_size = conv_w * conv_h;
436
437 // Im2Col configuration
Giorgio Arena26b22162018-08-13 15:49:49 +0100438 TensorShape shape_im2col = input_to_use->info()->tensor_shape();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000439 shape_im2col.set(0, patch_size);
440 shape_im2col.set(1, conv_size);
441 shape_im2col.set(2, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100442 _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 +0100443 _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 +0000444
445 // Weights reshape configuration
446 const TensorShape shape_weights_reshape(patch_size, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100447 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW));
448 _weights_reshape_kernel.configure(weights_to_use, &_weights_reshaped, append_bias ? biases : nullptr);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000449
450 // GEMV configuration
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000451 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
Giorgio Arena26b22162018-08-13 15:49:49 +0100452 TensorShape shape_v2mm_out = input_to_use->info()->tensor_shape();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000453 shape_v2mm_out.set(0, conv_size * weights_z);
454 shape_v2mm_out.set(1, 1);
455 shape_v2mm_out.set(2, 1);
Giorgio Arena26b22162018-08-13 15:49:49 +0100456 _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 +0000457 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
Giorgio Arena76572242018-04-04 17:44:26 +0100458 _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 +0100459 _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 +0000460
461 // Output staged configuration
462 if(_is_quantized)
463 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100464 const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform();
465 const UniformQuantizationInfo wq_info = weights->info()->quantization_info().uniform();
466 const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform();
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000467
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100468 float multiplier = (iq_info.scale * wq_info.scale) / oq_info.scale;
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100469 int output_multiplier;
470 int output_shift;
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000471 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100472 _output_stage_kernel.configure(&_output_reshaped, biases, output_to_use, output_multiplier, output_shift, oq_info.offset);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000473 _output_reshaped.allocator()->allocate();
474 }
475
Giorgio Arena26b22162018-08-13 15:49:49 +0100476 if(_is_nhwc)
477 {
478 _permute_output.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
479
480 _permuted_input.allocator()->allocate();
481 _permuted_weights.allocator()->allocate();
482 _permuted_output.allocator()->allocate();
483 }
484
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000485 // Fill borders on inputs
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000486 PixelValue zero_in(static_cast<int32_t>(0));
487 PixelValue zero_w(static_cast<int32_t>(0));
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000488 if(_is_quantized)
489 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100490 zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().uniform().offset));
491 zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().uniform().offset));
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000492 }
493 BorderSize border_size = _v2mm_kernel.border_size();
494 _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in);
495
496 border_size.bottom = 0;
497 _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000498
499 // Allocate intermediate tensors
500 _input_reshaped.allocator()->allocate();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000501 _v2mm_output.allocator()->allocate();
Georgios Pinitas60e98252018-10-22 16:17:20 +0100502
503 //Configure Activation Layer
504 _is_activationlayer_enabled = act_info.enabled();
505
506 if(_is_activationlayer_enabled)
507 {
508 _activationlayer_function.configure(output, nullptr, act_info);
509 }
Michalis Spyroub7b31532017-11-23 12:10:21 +0000510}
511
Georgios Pinitas10490202018-08-17 17:16:06 +0100512Status 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 +0100513 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
Abe Mbise7784c832018-05-31 16:48:41 +0100514{
515 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100516 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Usama Arif881f2de2019-04-12 10:29:17 +0100517 ARM_COMPUTE_RETURN_ERROR_ON(dilation.x() < 1 || dilation.y() < 1);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100518
519 const unsigned int width_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
520 const unsigned int height_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
Abe Mbise7784c832018-05-31 16:48:41 +0100521
Usama Arif881f2de2019-04-12 10:29:17 +0100522 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());
523 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 +0100524 // Clone output to use auto init
525 auto output_clone = output->clone();
526
Giorgio Arena26b22162018-08-13 15:49:49 +0100527 const ITensorInfo *input_to_use = input;
528 const ITensorInfo *weights_to_use = weights;
Georgios Pinitas10490202018-08-17 17:16:06 +0100529 const ITensorInfo *output_to_use = output_clone.get();
Giorgio Arena26b22162018-08-13 15:49:49 +0100530
531 TensorShape permuted_input_shape = input->tensor_shape();
532 TensorShape permuted_weights_shape = weights->tensor_shape();
533 TensorInfo permuted_input;
534 TensorInfo permuted_weights;
535
536 if(input->data_layout() == DataLayout::NHWC)
537 {
538 permute(permuted_input_shape, PermutationVector(1U, 2U, 0U));
539 permute(permuted_weights_shape, PermutationVector(1U, 2U, 0U));
540
541 permuted_input = TensorInfo(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NCHW));
542 permuted_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NCHW));
543
544 input_to_use = &permuted_input;
545 weights_to_use = &permuted_weights;
546 }
547
Abe Mbise7784c832018-05-31 16:48:41 +0100548 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
549 const bool append_bias = (biases != nullptr) && !is_quantized;
Usama Arif881f2de2019-04-12 10:29:17 +0100550 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);
Giorgio Arena26b22162018-08-13 15:49:49 +0100551 const size_t weights_w = weights_to_use->dimension(0);
552 const size_t weights_h = weights_to_use->dimension(1);
553 const size_t weights_z = weights_to_use->dimension(2);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100554 const unsigned int conv_w = output_shape[width_idx];
555 const unsigned int conv_h = output_shape[height_idx];
Abe Mbise7784c832018-05-31 16:48:41 +0100556 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
557 const size_t conv_size = conv_w * conv_h;
558
Giorgio Arena26b22162018-08-13 15:49:49 +0100559 // Output auto inizialitation if not yet initialized
Georgios Pinitas10490202018-08-17 17:16:06 +0100560 auto_init_if_empty(*output_clone, input->clone()->set_tensor_shape(output_shape));
Abe Mbise7784c832018-05-31 16:48:41 +0100561 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
562
Giorgio Arena26b22162018-08-13 15:49:49 +0100563 TensorInfo permuted_output;
564 if(input->data_layout() == DataLayout::NHWC)
565 {
566 permute(output_shape, PermutationVector(1U, 2U, 0U));
Georgios Pinitas10490202018-08-17 17:16:06 +0100567 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 +0100568 output_to_use = &permuted_output;
569 }
570
Abe Mbise7784c832018-05-31 16:48:41 +0100571 // Im2Col configuration
Giorgio Arena26b22162018-08-13 15:49:49 +0100572 TensorShape shape_im2col = input_to_use->tensor_shape();
Abe Mbise7784c832018-05-31 16:48:41 +0100573 shape_im2col.set(0, patch_size);
574 shape_im2col.set(1, conv_size);
575 shape_im2col.set(2, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100576 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 +0100577 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 +0100578
579 // Weights reshape configuration
580 const TensorShape shape_weights_reshape(patch_size, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100581 TensorInfo weights_reshaped(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW));
582 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseWeightsReshapeKernel::validate(weights_to_use, &weights_reshaped, append_bias ? biases : nullptr));
Abe Mbise7784c832018-05-31 16:48:41 +0100583
584 // GEMV configuration
585 DataType v2mm_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type();
Giorgio Arena26b22162018-08-13 15:49:49 +0100586 TensorShape shape_v2mm_out = input_to_use->tensor_shape();
Abe Mbise7784c832018-05-31 16:48:41 +0100587 shape_v2mm_out.set(0, conv_size * weights_z);
588 shape_v2mm_out.set(1, 1);
589 shape_v2mm_out.set(2, 1);
Giorgio Arena26b22162018-08-13 15:49:49 +0100590 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 +0100591 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixVectorMultiplyKernel::validate(&input_reshaped, &weights_reshaped, &v2mm_output));
592
Giorgio Arena26b22162018-08-13 15:49:49 +0100593 TensorInfo output_reshaped(v2mm_output.clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_to_use->tensor_shape()));
594 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 +0100595
596 if(is_quantized)
597 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100598 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayerOutputStageKernel::validate(&output_reshaped, biases, output_to_use));
Abe Mbise7784c832018-05-31 16:48:41 +0100599 }
600
Georgios Pinitas60e98252018-10-22 16:17:20 +0100601 // Validate Activation Layer
602 if(act_info.enabled())
603 {
604 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
605 }
606
Abe Mbise7784c832018-05-31 16:48:41 +0100607 return Status{};
608}
609
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000610void NEDepthwiseConvolutionLayer::run()
Michalis Spyroub7b31532017-11-23 12:10:21 +0000611{
Georgios Pinitas72219332018-06-05 14:56:06 +0100612 prepare();
Georgios Pinitas1562be32018-03-08 19:09:19 +0000613
Giorgio Arena26b22162018-08-13 15:49:49 +0100614 if(_is_nhwc)
615 {
616 _permute_input.run();
617 }
618
Michalis Spyroub7b31532017-11-23 12:10:21 +0000619 NEScheduler::get().schedule(&_im2col_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000620 NEScheduler::get().schedule(&_v2mm_input_fill_border, Window::DimX);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000621 NEScheduler::get().schedule(&_v2mm_kernel, Window::DimX);
622 NEScheduler::get().schedule(&_vector_to_tensor_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000623 if(_is_quantized)
624 {
625 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
626 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100627
628 if(_is_nhwc)
629 {
630 _permute_output.run();
631 }
Georgios Pinitas60e98252018-10-22 16:17:20 +0100632
633 if(_is_activationlayer_enabled)
634 {
635 _activationlayer_function.run();
636 }
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000637}
Georgios Pinitas72219332018-06-05 14:56:06 +0100638
639void NEDepthwiseConvolutionLayer::prepare()
640{
641 if(!_is_prepared)
642 {
643 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
644
Giorgio Arena26b22162018-08-13 15:49:49 +0100645 if(_is_nhwc)
646 {
647 _permute_weights.run();
648 }
649
Georgios Pinitas72219332018-06-05 14:56:06 +0100650 // Run reshape and mark original weights as unused
651 _weights_reshaped.allocator()->allocate();
652 NEScheduler::get().schedule(&_weights_reshape_kernel, Window::DimX);
653 NEScheduler::get().schedule(&_v2mm_weights_fill_border, Window::DimX);
654 _original_weights->mark_as_unused();
655
656 _is_prepared = true;
657 }
658}
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000659} // namespace arm_compute