blob: 5133756993d06b017f70c8e912d78e3fc3ba400b [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;
115 int output_multiplier, output_shift;
116 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000117 _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 +0100118 _accumulator.allocator()->allocate();
119 }
120 else if(_has_bias)
121 {
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000122 _output_stage_kernel.configure(_is_nchw ? output : &_permuted_output, biases);
Giorgio Arena26b22162018-08-13 15:49:49 +0100123 }
124
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000125 // Permute output
126 if(!_is_nchw)
Giorgio Arena26b22162018-08-13 15:49:49 +0100127 {
128 // Configure the function to transform the convoluted output to NHWC
129 _permute_output.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
130 _permuted_output.allocator()->allocate();
131 }
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000132}
Georgios Pinitas60e98252018-10-22 16:17:20 +0100133
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000134void NEDepthwiseConvolutionLayer3x3::configure_optimized(const ITensor *input,
135 const ITensor *weights,
136 const ITensor *biases,
137 ITensor *output,
138 const PadStrideInfo &conv_info,
139 unsigned int depth_multiplier,
140 const ActivationLayerInfo &act_info)
141{
142 ActivationLayerInfo act_info_to_use = ActivationLayerInfo();
143 const bool is_relu = arm_compute::utils::info_helpers::is_relu(act_info);
144 const bool is_relu6 = arm_compute::utils::info_helpers::is_relu6(act_info);
145 _is_activationlayer_enabled = act_info.enabled() && !(is_relu || is_relu6);
146 if(!_is_activationlayer_enabled)
147 {
148 act_info_to_use = act_info;
149 }
150
151 if(_is_nchw)
152 {
153 _memory_group.manage(&_permuted_input);
154 _memory_group.manage(&_permuted_output);
155
156 // Configure the function to transform the input tensor from NCHW -> NHWC
157 _permute_input.configure(input, &_permuted_input, PermutationVector(2U, 0U, 1U));
158 _permuted_input.info()->set_data_layout(DataLayout::NHWC);
159
160 // Configure the function to transform the weights tensor from IHW -> HWI
161 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(2U, 0U, 1U));
162 _permuted_weights.info()->set_data_layout(DataLayout::NHWC);
163
164 // Configure optimized depthwise
165 _dwc_optimized_func.configure(&_permuted_input, &_permuted_weights, biases, &_permuted_output, conv_info, depth_multiplier, act_info_to_use);
166
167 // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
168 _permuted_output.info()->set_data_layout(DataLayout::NHWC);
169 _permute_output.configure(&_permuted_output, output, PermutationVector(1U, 2U, 0U));
170
171 // Allocate tensors
172 _permuted_input.allocator()->allocate();
173 _permuted_output.allocator()->allocate();
174 }
175 else
176 {
177 _dwc_optimized_func.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info_to_use);
178 }
179}
180
181void NEDepthwiseConvolutionLayer3x3::configure(ITensor *input,
182 const ITensor *weights,
183 const ITensor *biases,
184 ITensor *output, const PadStrideInfo &conv_info,
185 unsigned int depth_multiplier,
Usama Arife73686a2019-04-08 17:30:48 +0100186 const ActivationLayerInfo &act_info,
187 const Size2D &dilation)
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000188{
189 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
190 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
191
Usama Arif881f2de2019-04-12 10:29:17 +0100192 // idx_w and idx_h only used for validation
193 const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
194 const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
195 ARM_COMPUTE_UNUSED(idx_w);
196 ARM_COMPUTE_UNUSED(idx_h);
197
198 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());
199 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());
200
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000201 _original_weights = weights;
202 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
203 _has_bias = biases != nullptr;
204 _is_optimized = NEDepthwiseConvolutionAssemblyDispatch::is_optimized_supported(input->info(),
205 weights->info(),
206 conv_info,
Usama Arif881f2de2019-04-12 10:29:17 +0100207 depth_multiplier, dilation);
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000208 _is_nchw = input->info()->data_layout() == DataLayout::NCHW;
209 _permute = _is_optimized == _is_nchw;
210 _is_prepared = false;
Georgios Pinitas60e98252018-10-22 16:17:20 +0100211 _is_activationlayer_enabled = act_info.enabled();
212
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000213 // Configure appropriate pipeline
214 if(_is_optimized)
215 {
216 configure_optimized(input, weights, biases, output, conv_info, depth_multiplier, act_info);
217 }
218 else
219 {
Usama Arif881f2de2019-04-12 10:29:17 +0100220 configure_generic(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000221 }
222
223 // Configure activation
Georgios Pinitas60e98252018-10-22 16:17:20 +0100224 if(_is_activationlayer_enabled)
225 {
226 _activationlayer_function.configure(output, nullptr, act_info);
227 }
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100228}
229
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000230Status NEDepthwiseConvolutionLayer3x3::validate(const ITensorInfo *input,
231 const ITensorInfo *weights,
232 const ITensorInfo *biases,
233 const ITensorInfo *output,
234 const PadStrideInfo &conv_info,
235 unsigned int depth_multiplier,
Usama Arife73686a2019-04-08 17:30:48 +0100236 const ActivationLayerInfo &act_info,
237 const Size2D &dilation)
Abe Mbise7784c832018-05-31 16:48:41 +0100238{
239 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100240 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Usama Arif881f2de2019-04-12 10:29:17 +0100241 ARM_COMPUTE_RETURN_ERROR_ON(dilation.x() < 1 || dilation.y() < 1);
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 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());
245 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 +0100246
Giorgio Arena66cbafb2018-08-23 14:51:00 +0100247 if(biases != nullptr)
248 {
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100249 const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
Giorgio Arena66cbafb2018-08-23 14:51:00 +0100250 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100251 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(channel_idx));
Giorgio Arena66cbafb2018-08-23 14:51:00 +0100252 }
253
Usama Arif881f2de2019-04-12 10:29:17 +0100254 if(!NEDepthwiseConvolutionAssemblyDispatch::is_optimized_supported(input, weights, conv_info, depth_multiplier, dilation))
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100255 {
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000256 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
257 TensorInfo accumulator = TensorInfo(output->clone()->set_is_resizable(true).reset_padding().set_data_type(DataType::S32));
258 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseConvolutionLayer3x3Kernel::validate(input, weights, is_quantized ? &accumulator : output, conv_info, depth_multiplier));
259
260 if(is_quantized)
261 {
262 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayerOutputStageKernel::validate(&accumulator, biases, output));
263 }
264 }
265 else
266 {
267 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseConvolutionAssemblyDispatch::validate(input, weights, biases, output, conv_info, depth_multiplier));
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100268 }
269
Georgios Pinitas60e98252018-10-22 16:17:20 +0100270 //Validate Activation Layer
271 if(act_info.enabled())
272 {
273 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
274 }
275
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100276 return Status{};
Abe Mbise7784c832018-05-31 16:48:41 +0100277}
278
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000279void NEDepthwiseConvolutionLayer3x3::run_generic()
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100280{
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000281 // Fill border
282 NEScheduler::get().schedule(&_border_handler, Window::DimX);
Georgios Pinitas4074c992018-01-30 18:13:46 +0000283
284 // Execute depthwise convolution
285 NEScheduler::get().schedule(&_dwc_kernel, Window::DimX);
286
Georgios Pinitas4074c992018-01-30 18:13:46 +0000287 // Add biases
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000288 if(_has_bias || _is_quantized)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100289 {
Michalis Spyroub91e34c2017-12-20 15:50:55 +0000290 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100291 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100292
293 // Permute output
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000294 if(!_is_nchw)
Giorgio Arena26b22162018-08-13 15:49:49 +0100295 {
296 _permute_output.run();
297 }
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000298}
Georgios Pinitas60e98252018-10-22 16:17:20 +0100299
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000300void NEDepthwiseConvolutionLayer3x3::run_optimized()
301{
302 // Run assembly function
303 _dwc_optimized_func.run();
304
305 // Permute output
306 if(_is_nchw)
307 {
308 _permute_output.run();
309 }
310}
311
312void NEDepthwiseConvolutionLayer3x3::run()
313{
314 prepare();
315
Georgios Pinitasda953f22019-04-02 17:27:03 +0100316 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000317
318 // Permute input
319 if(_permute)
320 {
321 _permute_input.run();
322 }
323
324 _is_optimized ? run_optimized() : run_generic();
325
326 // Run activation
Georgios Pinitas60e98252018-10-22 16:17:20 +0100327 if(_is_activationlayer_enabled)
328 {
329 _activationlayer_function.run();
330 }
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000331}
332
333void NEDepthwiseConvolutionLayer3x3::prepare()
334{
335 if(!_is_prepared)
336 {
337 // Permute weights
338 if(_permute)
339 {
340 _permuted_weights.allocator()->allocate();
341 _permute_weights.run();
342 _original_weights->mark_as_unused();
343 }
344
345 // Prepare optimized function
346 if(_is_optimized)
347 {
348 _dwc_optimized_func.prepare();
349 if(!_permuted_weights.is_used())
350 {
351 _permuted_weights.allocator()->free();
352 }
353 }
354
355 _is_prepared = true;
356 }
Michalis Spyroub7b31532017-11-23 12:10:21 +0000357}
358
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000359NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer()
Giorgio Arena26b22162018-08-13 15:49:49 +0100360 : _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 +0100361 _permute_weights(), _permute_output(), _activationlayer_function(), _input_reshaped(), _weights_reshaped(), _v2mm_output(), _output_reshaped(), _permuted_input(), _permuted_weights(),
362 _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 +0000363{
364}
365
Georgios Pinitas60e98252018-10-22 16:17:20 +0100366void NEDepthwiseConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
Usama Arife73686a2019-04-08 17:30:48 +0100367 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000368{
Giorgio Arena26b22162018-08-13 15:49:49 +0100369 const unsigned int channel_idx = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL);
370 ARM_COMPUTE_UNUSED(channel_idx);
Georgios Pinitas8cffcd62018-11-16 17:11:50 +0000371 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000372 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Giorgio Arena26b22162018-08-13 15:49:49 +0100373 ARM_COMPUTE_ERROR_ON((input->info()->dimension(channel_idx) * depth_multiplier) != weights->info()->dimension(channel_idx));
Usama Arif881f2de2019-04-12 10:29:17 +0100374 // idx_w and idx_h only used for validation
375 const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
376 const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
377 ARM_COMPUTE_UNUSED(idx_w);
378 ARM_COMPUTE_UNUSED(idx_h);
379
380 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());
381 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 +0000382
Giorgio Arena26b22162018-08-13 15:49:49 +0100383 _is_nhwc = input->info()->data_layout() == DataLayout::NHWC;
384
385 ITensor *input_to_use = input;
386 const ITensor *weights_to_use = weights;
387 ITensor *output_to_use = output;
388
389 if(_is_nhwc)
390 {
391 _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
392 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
393 input_to_use = &_permuted_input;
394
395 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
396 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
397 weights_to_use = &_permuted_weights;
398 }
399
400 const size_t weights_w = weights_to_use->info()->dimension(0);
401 const size_t weights_h = weights_to_use->info()->dimension(1);
402 const size_t weights_z = weights_to_use->info()->dimension(2);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000403
Georgios Pinitas1562be32018-03-08 19:09:19 +0000404 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Georgios Pinitas72219332018-06-05 14:56:06 +0100405 _is_prepared = false;
Giorgio Arena26b22162018-08-13 15:49:49 +0100406 _original_weights = weights_to_use;
Michalis Spyroub7b31532017-11-23 12:10:21 +0000407
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000408 // Should bias be appended ?
409 bool append_bias = (biases != nullptr) && !_is_quantized;
410
411 // Calculate output shape
Usama Arif881f2de2019-04-12 10:29:17 +0100412 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 +0100413
414 // Output auto inizialitation if not yet initialized
415 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
416 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000417
Giorgio Arena26b22162018-08-13 15:49:49 +0100418 if(_is_nhwc)
419 {
420 permute(output_shape, PermutationVector(1U, 2U, 0U));
421 _permuted_output.allocator()->init(output->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
422 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
423 output_to_use = &_permuted_output;
424 }
425
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000426 // Output width and height
Giorgio Arena76572242018-04-04 17:44:26 +0100427 const unsigned int conv_w = output_shape.x();
428 const unsigned int conv_h = output_shape.y();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000429
430 // Set up intermediate tensors
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000431 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000432 const size_t conv_size = conv_w * conv_h;
433
434 // Im2Col configuration
Giorgio Arena26b22162018-08-13 15:49:49 +0100435 TensorShape shape_im2col = input_to_use->info()->tensor_shape();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000436 shape_im2col.set(0, patch_size);
437 shape_im2col.set(1, conv_size);
438 shape_im2col.set(2, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100439 _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 +0100440 _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 +0000441
442 // Weights reshape configuration
443 const TensorShape shape_weights_reshape(patch_size, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100444 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW));
445 _weights_reshape_kernel.configure(weights_to_use, &_weights_reshaped, append_bias ? biases : nullptr);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000446
447 // GEMV configuration
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000448 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
Giorgio Arena26b22162018-08-13 15:49:49 +0100449 TensorShape shape_v2mm_out = input_to_use->info()->tensor_shape();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000450 shape_v2mm_out.set(0, conv_size * weights_z);
451 shape_v2mm_out.set(1, 1);
452 shape_v2mm_out.set(2, 1);
Giorgio Arena26b22162018-08-13 15:49:49 +0100453 _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 +0000454 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
Giorgio Arena76572242018-04-04 17:44:26 +0100455 _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 +0100456 _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 +0000457
458 // Output staged configuration
459 if(_is_quantized)
460 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100461 const QuantizationInfo output_quant_info = output->info()->quantization_info();
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000462
463 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000464 int output_multiplier, output_shift;
465 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Giorgio Arena26b22162018-08-13 15:49:49 +0100466 _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 +0000467 _output_reshaped.allocator()->allocate();
468 }
469
Giorgio Arena26b22162018-08-13 15:49:49 +0100470 if(_is_nhwc)
471 {
472 _permute_output.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
473
474 _permuted_input.allocator()->allocate();
475 _permuted_weights.allocator()->allocate();
476 _permuted_output.allocator()->allocate();
477 }
478
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000479 // Fill borders on inputs
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000480 PixelValue zero_in(static_cast<int32_t>(0));
481 PixelValue zero_w(static_cast<int32_t>(0));
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000482 if(_is_quantized)
483 {
484 zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset));
485 zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset));
486 }
487 BorderSize border_size = _v2mm_kernel.border_size();
488 _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in);
489
490 border_size.bottom = 0;
491 _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000492
493 // Allocate intermediate tensors
494 _input_reshaped.allocator()->allocate();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000495 _v2mm_output.allocator()->allocate();
Georgios Pinitas60e98252018-10-22 16:17:20 +0100496
497 //Configure Activation Layer
498 _is_activationlayer_enabled = act_info.enabled();
499
500 if(_is_activationlayer_enabled)
501 {
502 _activationlayer_function.configure(output, nullptr, act_info);
503 }
Michalis Spyroub7b31532017-11-23 12:10:21 +0000504}
505
Georgios Pinitas10490202018-08-17 17:16:06 +0100506Status 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 +0100507 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
Abe Mbise7784c832018-05-31 16:48:41 +0100508{
509 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100510 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Usama Arif881f2de2019-04-12 10:29:17 +0100511 ARM_COMPUTE_RETURN_ERROR_ON(dilation.x() < 1 || dilation.y() < 1);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100512
513 const unsigned int width_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
514 const unsigned int height_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
Abe Mbise7784c832018-05-31 16:48:41 +0100515
Usama Arif881f2de2019-04-12 10:29:17 +0100516 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());
517 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 +0100518 // Clone output to use auto init
519 auto output_clone = output->clone();
520
Giorgio Arena26b22162018-08-13 15:49:49 +0100521 const ITensorInfo *input_to_use = input;
522 const ITensorInfo *weights_to_use = weights;
Georgios Pinitas10490202018-08-17 17:16:06 +0100523 const ITensorInfo *output_to_use = output_clone.get();
Giorgio Arena26b22162018-08-13 15:49:49 +0100524
525 TensorShape permuted_input_shape = input->tensor_shape();
526 TensorShape permuted_weights_shape = weights->tensor_shape();
527 TensorInfo permuted_input;
528 TensorInfo permuted_weights;
529
530 if(input->data_layout() == DataLayout::NHWC)
531 {
532 permute(permuted_input_shape, PermutationVector(1U, 2U, 0U));
533 permute(permuted_weights_shape, PermutationVector(1U, 2U, 0U));
534
535 permuted_input = TensorInfo(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NCHW));
536 permuted_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NCHW));
537
538 input_to_use = &permuted_input;
539 weights_to_use = &permuted_weights;
540 }
541
Abe Mbise7784c832018-05-31 16:48:41 +0100542 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
543 const bool append_bias = (biases != nullptr) && !is_quantized;
Usama Arif881f2de2019-04-12 10:29:17 +0100544 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);
Giorgio Arena26b22162018-08-13 15:49:49 +0100545 const size_t weights_w = weights_to_use->dimension(0);
546 const size_t weights_h = weights_to_use->dimension(1);
547 const size_t weights_z = weights_to_use->dimension(2);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100548 const unsigned int conv_w = output_shape[width_idx];
549 const unsigned int conv_h = output_shape[height_idx];
Abe Mbise7784c832018-05-31 16:48:41 +0100550 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
551 const size_t conv_size = conv_w * conv_h;
552
Giorgio Arena26b22162018-08-13 15:49:49 +0100553 // Output auto inizialitation if not yet initialized
Georgios Pinitas10490202018-08-17 17:16:06 +0100554 auto_init_if_empty(*output_clone, input->clone()->set_tensor_shape(output_shape));
Abe Mbise7784c832018-05-31 16:48:41 +0100555 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
556
Giorgio Arena26b22162018-08-13 15:49:49 +0100557 TensorInfo permuted_output;
558 if(input->data_layout() == DataLayout::NHWC)
559 {
560 permute(output_shape, PermutationVector(1U, 2U, 0U));
Georgios Pinitas10490202018-08-17 17:16:06 +0100561 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 +0100562 output_to_use = &permuted_output;
563 }
564
Abe Mbise7784c832018-05-31 16:48:41 +0100565 // Im2Col configuration
Giorgio Arena26b22162018-08-13 15:49:49 +0100566 TensorShape shape_im2col = input_to_use->tensor_shape();
Abe Mbise7784c832018-05-31 16:48:41 +0100567 shape_im2col.set(0, patch_size);
568 shape_im2col.set(1, conv_size);
569 shape_im2col.set(2, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100570 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 +0100571 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 +0100572
573 // Weights reshape configuration
574 const TensorShape shape_weights_reshape(patch_size, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100575 TensorInfo weights_reshaped(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW));
576 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseWeightsReshapeKernel::validate(weights_to_use, &weights_reshaped, append_bias ? biases : nullptr));
Abe Mbise7784c832018-05-31 16:48:41 +0100577
578 // GEMV configuration
579 DataType v2mm_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type();
Giorgio Arena26b22162018-08-13 15:49:49 +0100580 TensorShape shape_v2mm_out = input_to_use->tensor_shape();
Abe Mbise7784c832018-05-31 16:48:41 +0100581 shape_v2mm_out.set(0, conv_size * weights_z);
582 shape_v2mm_out.set(1, 1);
583 shape_v2mm_out.set(2, 1);
Giorgio Arena26b22162018-08-13 15:49:49 +0100584 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 +0100585 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixVectorMultiplyKernel::validate(&input_reshaped, &weights_reshaped, &v2mm_output));
586
Giorgio Arena26b22162018-08-13 15:49:49 +0100587 TensorInfo output_reshaped(v2mm_output.clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_to_use->tensor_shape()));
588 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 +0100589
590 if(is_quantized)
591 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100592 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayerOutputStageKernel::validate(&output_reshaped, biases, output_to_use));
Abe Mbise7784c832018-05-31 16:48:41 +0100593 }
594
Georgios Pinitas60e98252018-10-22 16:17:20 +0100595 // Validate Activation Layer
596 if(act_info.enabled())
597 {
598 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
599 }
600
Abe Mbise7784c832018-05-31 16:48:41 +0100601 return Status{};
602}
603
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000604void NEDepthwiseConvolutionLayer::run()
Michalis Spyroub7b31532017-11-23 12:10:21 +0000605{
Georgios Pinitas72219332018-06-05 14:56:06 +0100606 prepare();
Georgios Pinitas1562be32018-03-08 19:09:19 +0000607
Giorgio Arena26b22162018-08-13 15:49:49 +0100608 if(_is_nhwc)
609 {
610 _permute_input.run();
611 }
612
Michalis Spyroub7b31532017-11-23 12:10:21 +0000613 NEScheduler::get().schedule(&_im2col_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000614 NEScheduler::get().schedule(&_v2mm_input_fill_border, Window::DimX);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000615 NEScheduler::get().schedule(&_v2mm_kernel, Window::DimX);
616 NEScheduler::get().schedule(&_vector_to_tensor_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000617 if(_is_quantized)
618 {
619 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
620 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100621
622 if(_is_nhwc)
623 {
624 _permute_output.run();
625 }
Georgios Pinitas60e98252018-10-22 16:17:20 +0100626
627 if(_is_activationlayer_enabled)
628 {
629 _activationlayer_function.run();
630 }
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000631}
Georgios Pinitas72219332018-06-05 14:56:06 +0100632
633void NEDepthwiseConvolutionLayer::prepare()
634{
635 if(!_is_prepared)
636 {
637 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
638
Giorgio Arena26b22162018-08-13 15:49:49 +0100639 if(_is_nhwc)
640 {
641 _permute_weights.run();
642 }
643
Georgios Pinitas72219332018-06-05 14:56:06 +0100644 // Run reshape and mark original weights as unused
645 _weights_reshaped.allocator()->allocate();
646 NEScheduler::get().schedule(&_weights_reshape_kernel, Window::DimX);
647 NEScheduler::get().schedule(&_v2mm_weights_fill_border, Window::DimX);
648 _original_weights->mark_as_unused();
649
650 _is_prepared = true;
651 }
652}
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000653} // namespace arm_compute