blob: 9dcbc99332bc0a441260062052eaa227719ca23c [file] [log] [blame]
Michalis Spyrou7362f0d2017-10-18 17:58:22 +01001/*
Georgios Pinitasf72f9362018-01-12 16:29:45 +00002 * Copyright (c) 2017-2018 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
34using namespace arm_compute;
Georgios Pinitasd05dce42018-01-22 16:29:17 +000035using namespace arm_compute::misc;
Georgios Pinitas4074c992018-01-30 18:13:46 +000036using namespace arm_compute::misc::shape_calculator;
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010037
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000038NEDepthwiseConvolutionLayer3x3::NEDepthwiseConvolutionLayer3x3()
Giorgio Arena26b22162018-08-13 15:49:49 +010039 : _dwc_kernel(), _output_stage_kernel(), _border_handler(), _permute_input(), _permute_weights(), _permute_output(), _accumulator(), _permuted_input(), _permuted_weights(), _permuted_output(),
40 _has_bias(false), _is_quantized(false), _is_optimized(false), _are_weights_reshaped(false), _is_nchw(true), _is_first_run(true), _permute(false)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010041{
42}
43
Giorgio Arena76572242018-04-04 17:44:26 +010044void NEDepthwiseConvolutionLayer3x3::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010045{
Georgios Pinitas20c246a2018-09-12 16:45:53 +010046 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Georgios Pinitas1250a5a2018-01-02 13:27:37 +000047 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010048
Georgios Pinitasf72f9362018-01-12 16:29:45 +000049 PixelValue zero_value(0.f);
50
51 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
52 _has_bias = biases != nullptr;
Georgios Pinitas4074c992018-01-30 18:13:46 +000053 _is_optimized = NEDepthwiseConvolutionLayer3x3Kernel::is_optimized_execution_possible(input->info()->tensor_shape(),
54 conv_info,
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010055 input->info()->data_type(),
Giorgio Arena76572242018-04-04 17:44:26 +010056 depth_multiplier,
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010057 input->info()->data_layout());
Georgios Pinitas4074c992018-01-30 18:13:46 +000058 _are_weights_reshaped = false;
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010059 _is_nchw = input->info()->data_layout() == DataLayout::NCHW;
Giorgio Arena26b22162018-08-13 15:49:49 +010060 _permute = _is_optimized == _is_nchw;
Georgios Pinitasf72f9362018-01-12 16:29:45 +000061
Georgios Pinitasa799ce02018-09-12 20:11:34 +010062 // Initialize the intermediate accumulator tensor in case of quantized input
63 if(_is_quantized)
64 {
65 TensorShape accum_shape = output->info()->tensor_shape();
66 DataLayout accum_layout = output->info()->data_layout();
67 if(!_is_optimized && !_is_nchw)
68 {
69 permute(accum_shape, PermutationVector(1U, 2U, 0U));
70 accum_layout = DataLayout::NCHW;
71 }
72
73 _accumulator.allocator()->init(TensorInfo(accum_shape, 1, DataType::S32, input->info()->quantization_info()));
74 _accumulator.info()->set_data_layout(accum_layout);
75 zero_value = PixelValue(static_cast<uint32_t>(input->info()->quantization_info().offset));
76 }
77
Georgios Pinitas4074c992018-01-30 18:13:46 +000078 if(_is_optimized)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010079 {
Georgios Pinitasa799ce02018-09-12 20:11:34 +010080 ITensor *optimized_output = (_is_quantized) ? &_accumulator : output;
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010081 if(_is_nchw)
82 {
83 // Configure the function to transform the input tensor from NCHW -> NHWC
Giorgio Arena26b22162018-08-13 15:49:49 +010084 _permute_input.configure(input, &_permuted_input, PermutationVector(2U, 0U, 1U));
85 _permuted_input.info()->set_data_layout(DataLayout::NHWC);
Georgios Pinitas4074c992018-01-30 18:13:46 +000086
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010087 // Configure the function to transform the weights tensor from IHW -> HWI
Giorgio Arena26b22162018-08-13 15:49:49 +010088 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(2U, 0U, 1U));
89 _permuted_weights.info()->set_data_layout(DataLayout::NHWC);
Georgios Pinitas4074c992018-01-30 18:13:46 +000090
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010091 // Configure optimized depthwise
Giorgio Arena26b22162018-08-13 15:49:49 +010092 _dwc_kernel.configure(&_permuted_input, &_permuted_weights, &_permuted_output, conv_info, depth_multiplier, DataLayout::NHWC);
Georgios Pinitas4074c992018-01-30 18:13:46 +000093
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010094 // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
Georgios Pinitasa799ce02018-09-12 20:11:34 +010095 _permuted_output.info()->set_data_layout(DataLayout::NHWC);
96 _permute_output.configure(&_permuted_output, optimized_output, PermutationVector(1U, 2U, 0U));
Georgios Pinitas4074c992018-01-30 18:13:46 +000097
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010098 // Allocate tensors
Giorgio Arena26b22162018-08-13 15:49:49 +010099 _permuted_input.allocator()->allocate();
100 _permuted_weights.allocator()->allocate();
101 _permuted_output.allocator()->allocate();
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100102 }
103 else
104 {
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100105 _dwc_kernel.configure(input, weights, optimized_output, conv_info, depth_multiplier, DataLayout::NHWC);
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100106 }
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000107 }
Georgios Pinitas4074c992018-01-30 18:13:46 +0000108 else
109 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100110 if(!_is_nchw)
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000111 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100112 // Configure the function to transform the input tensor from NHWC -> NCHW
113 _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
114 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000115
Giorgio Arena26b22162018-08-13 15:49:49 +0100116 // Configure the function to transform the weights tensor from HWI -> IHW
117 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
118 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
119
120 // Configure optimized depthwise
121 _dwc_kernel.configure(&_permuted_input, &_permuted_weights, (_is_quantized) ? &_accumulator : &_permuted_output, conv_info, depth_multiplier);
122
123 // Configure border handler
124 _border_handler.configure(&_permuted_input, _dwc_kernel.border_size(), BorderMode::CONSTANT, zero_value);
125
126 // Allocate tensors
127 _permuted_input.allocator()->allocate();
128 _permuted_weights.allocator()->allocate();
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000129 }
130 else
131 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100132 // Configure depthwise convolution kernel
133 _dwc_kernel.configure(input, weights, (_is_quantized) ? &_accumulator : output, conv_info, depth_multiplier);
134
135 // Configure border handler
136 _border_handler.configure(input, _dwc_kernel.border_size(), BorderMode::CONSTANT, zero_value);
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000137 }
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100138 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100139
140 // Configure biases accumulation
141 if(_is_quantized)
142 {
143 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
144
145 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
146 int output_multiplier, output_shift;
147 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100148 _output_stage_kernel.configure(&_accumulator, biases, (_is_nchw || _is_optimized) ? output : &_permuted_output, output_multiplier, output_shift, output_quant_info.offset);
Giorgio Arena26b22162018-08-13 15:49:49 +0100149 _accumulator.allocator()->allocate();
150 }
151 else if(_has_bias)
152 {
153 _output_stage_kernel.configure((_is_nchw || _is_optimized) ? output : &_permuted_output, biases);
154 }
155
156 if(!_is_optimized && !_is_nchw)
157 {
158 // Configure the function to transform the convoluted output to NHWC
159 _permute_output.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
160 _permuted_output.allocator()->allocate();
161 }
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100162}
163
Abe Mbise7784c832018-05-31 16:48:41 +0100164Status NEDepthwiseConvolutionLayer3x3::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
165 unsigned int depth_multiplier)
166{
167 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100168 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Abe Mbise7784c832018-05-31 16:48:41 +0100169
Giorgio Arena66cbafb2018-08-23 14:51:00 +0100170 if(biases != nullptr)
171 {
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100172 const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
Giorgio Arena66cbafb2018-08-23 14:51:00 +0100173 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100174 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(channel_idx));
Giorgio Arena66cbafb2018-08-23 14:51:00 +0100175 }
176
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100177 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
178 TensorInfo accumulator = TensorInfo(output->clone()->set_is_resizable(true).reset_padding().set_data_type(DataType::S32));
179
180 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseConvolutionLayer3x3Kernel::validate(input, weights, is_quantized ? &accumulator : output, conv_info, depth_multiplier));
181
182 if(is_quantized)
183 {
184 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayerOutputStageKernel::validate(&accumulator, biases, output));
185 }
186
187 return Status{};
Abe Mbise7784c832018-05-31 16:48:41 +0100188}
189
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000190void NEDepthwiseConvolutionLayer3x3::run()
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100191{
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100192 if(_is_first_run && _is_optimized)
193 {
194 _is_first_run = false;
195 // Create convolver (deferred)
196 _dwc_kernel.generate_convolver();
197 }
198
Giorgio Arena26b22162018-08-13 15:49:49 +0100199 // Permute weights
200 if(_permute)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000201 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100202 if(!_are_weights_reshaped)
203 {
204 _are_weights_reshaped = true;
205 _permute_weights.run();
206 }
207
208 _permute_input.run();
Georgios Pinitas4074c992018-01-30 18:13:46 +0000209 }
210
211 // Handle input
Giorgio Arena26b22162018-08-13 15:49:49 +0100212 if(!_is_optimized)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000213 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100214 // Fill border
Georgios Pinitas4074c992018-01-30 18:13:46 +0000215 NEScheduler::get().schedule(&_border_handler, Window::DimX);
216 }
217
218 // Execute depthwise convolution
219 NEScheduler::get().schedule(&_dwc_kernel, Window::DimX);
220
Giorgio Arena26b22162018-08-13 15:49:49 +0100221 // Permute output
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100222 if(_is_optimized && _is_nchw)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000223 {
224 _permute_output.run();
225 }
226
227 // Add biases
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000228 if(_has_bias || _is_quantized)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100229 {
Michalis Spyroub91e34c2017-12-20 15:50:55 +0000230 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100231 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100232
233 // Permute output
234 if(!_is_optimized && !_is_nchw)
235 {
236 _permute_output.run();
237 }
Michalis Spyroub7b31532017-11-23 12:10:21 +0000238}
239
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000240NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer()
Giorgio Arena26b22162018-08-13 15:49:49 +0100241 : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _v2mm_input_fill_border(), _v2mm_weights_fill_border(), _permute_input(),
242 _permute_weights(), _permute_output(), _input_reshaped(), _weights_reshaped(), _v2mm_output(), _output_reshaped(), _permuted_input(), _permuted_weights(), _permuted_output(), _is_prepared(false),
243 _is_quantized(false), _is_nhwc(false), _original_weights(nullptr)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000244{
245}
246
Giorgio Arena76572242018-04-04 17:44:26 +0100247void NEDepthwiseConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000248{
Giorgio Arena26b22162018-08-13 15:49:49 +0100249 const unsigned int channel_idx = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL);
250 ARM_COMPUTE_UNUSED(channel_idx);
251
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000252 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000253 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Giorgio Arena26b22162018-08-13 15:49:49 +0100254 ARM_COMPUTE_ERROR_ON((input->info()->dimension(channel_idx) * depth_multiplier) != weights->info()->dimension(channel_idx));
Michalis Spyroub7b31532017-11-23 12:10:21 +0000255
Giorgio Arena26b22162018-08-13 15:49:49 +0100256 _is_nhwc = input->info()->data_layout() == DataLayout::NHWC;
257
258 ITensor *input_to_use = input;
259 const ITensor *weights_to_use = weights;
260 ITensor *output_to_use = output;
261
262 if(_is_nhwc)
263 {
264 _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
265 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
266 input_to_use = &_permuted_input;
267
268 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
269 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
270 weights_to_use = &_permuted_weights;
271 }
272
273 const size_t weights_w = weights_to_use->info()->dimension(0);
274 const size_t weights_h = weights_to_use->info()->dimension(1);
275 const size_t weights_z = weights_to_use->info()->dimension(2);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000276
Georgios Pinitas1562be32018-03-08 19:09:19 +0000277 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Georgios Pinitas72219332018-06-05 14:56:06 +0100278 _is_prepared = false;
Giorgio Arena26b22162018-08-13 15:49:49 +0100279 _original_weights = weights_to_use;
Michalis Spyroub7b31532017-11-23 12:10:21 +0000280
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000281 // Should bias be appended ?
282 bool append_bias = (biases != nullptr) && !_is_quantized;
283
284 // Calculate output shape
Giorgio Arena76572242018-04-04 17:44:26 +0100285 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier);
286
287 // Output auto inizialitation if not yet initialized
288 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
289 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000290
Giorgio Arena26b22162018-08-13 15:49:49 +0100291 if(_is_nhwc)
292 {
293 permute(output_shape, PermutationVector(1U, 2U, 0U));
294 _permuted_output.allocator()->init(output->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
295 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
296 output_to_use = &_permuted_output;
297 }
298
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000299 // Output width and height
Giorgio Arena76572242018-04-04 17:44:26 +0100300 const unsigned int conv_w = output_shape.x();
301 const unsigned int conv_h = output_shape.y();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000302
303 // Set up intermediate tensors
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000304 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000305 const size_t conv_size = conv_w * conv_h;
306
307 // Im2Col configuration
Giorgio Arena26b22162018-08-13 15:49:49 +0100308 TensorShape shape_im2col = input_to_use->info()->tensor_shape();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000309 shape_im2col.set(0, patch_size);
310 shape_im2col.set(1, conv_size);
311 shape_im2col.set(2, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100312 _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col).set_data_layout(DataLayout::NCHW));
313 _im2col_kernel.configure(input_to_use, &_input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000314
315 // Weights reshape configuration
316 const TensorShape shape_weights_reshape(patch_size, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100317 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW));
318 _weights_reshape_kernel.configure(weights_to_use, &_weights_reshaped, append_bias ? biases : nullptr);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000319
320 // GEMV configuration
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000321 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
Giorgio Arena26b22162018-08-13 15:49:49 +0100322 TensorShape shape_v2mm_out = input_to_use->info()->tensor_shape();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000323 shape_v2mm_out.set(0, conv_size * weights_z);
324 shape_v2mm_out.set(1, 1);
325 shape_v2mm_out.set(2, 1);
Giorgio Arena26b22162018-08-13 15:49:49 +0100326 _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 +0000327 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
Giorgio Arena76572242018-04-04 17:44:26 +0100328 _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 +0100329 _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 +0000330
331 // Output staged configuration
332 if(_is_quantized)
333 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100334 const QuantizationInfo output_quant_info = output->info()->quantization_info();
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000335
336 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000337 int output_multiplier, output_shift;
338 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Giorgio Arena26b22162018-08-13 15:49:49 +0100339 _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 +0000340 _output_reshaped.allocator()->allocate();
341 }
342
Giorgio Arena26b22162018-08-13 15:49:49 +0100343 if(_is_nhwc)
344 {
345 _permute_output.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
346
347 _permuted_input.allocator()->allocate();
348 _permuted_weights.allocator()->allocate();
349 _permuted_output.allocator()->allocate();
350 }
351
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000352 // Fill borders on inputs
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000353 PixelValue zero_in(static_cast<int32_t>(0));
354 PixelValue zero_w(static_cast<int32_t>(0));
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000355 if(_is_quantized)
356 {
357 zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset));
358 zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset));
359 }
360 BorderSize border_size = _v2mm_kernel.border_size();
361 _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in);
362
363 border_size.bottom = 0;
364 _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000365
366 // Allocate intermediate tensors
367 _input_reshaped.allocator()->allocate();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000368 _v2mm_output.allocator()->allocate();
369}
370
Georgios Pinitas10490202018-08-17 17:16:06 +0100371Status NEDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Abe Mbise7784c832018-05-31 16:48:41 +0100372 unsigned int depth_multiplier)
373{
374 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100375 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
376
377 const unsigned int width_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
378 const unsigned int height_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
Abe Mbise7784c832018-05-31 16:48:41 +0100379
Georgios Pinitas10490202018-08-17 17:16:06 +0100380 // Clone output to use auto init
381 auto output_clone = output->clone();
382
Giorgio Arena26b22162018-08-13 15:49:49 +0100383 const ITensorInfo *input_to_use = input;
384 const ITensorInfo *weights_to_use = weights;
Georgios Pinitas10490202018-08-17 17:16:06 +0100385 const ITensorInfo *output_to_use = output_clone.get();
Giorgio Arena26b22162018-08-13 15:49:49 +0100386
387 TensorShape permuted_input_shape = input->tensor_shape();
388 TensorShape permuted_weights_shape = weights->tensor_shape();
389 TensorInfo permuted_input;
390 TensorInfo permuted_weights;
391
392 if(input->data_layout() == DataLayout::NHWC)
393 {
394 permute(permuted_input_shape, PermutationVector(1U, 2U, 0U));
395 permute(permuted_weights_shape, PermutationVector(1U, 2U, 0U));
396
397 permuted_input = TensorInfo(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NCHW));
398 permuted_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NCHW));
399
400 input_to_use = &permuted_input;
401 weights_to_use = &permuted_weights;
402 }
403
Abe Mbise7784c832018-05-31 16:48:41 +0100404 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
405 const bool append_bias = (biases != nullptr) && !is_quantized;
Giorgio Arena26b22162018-08-13 15:49:49 +0100406 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier);
407 const size_t weights_w = weights_to_use->dimension(0);
408 const size_t weights_h = weights_to_use->dimension(1);
409 const size_t weights_z = weights_to_use->dimension(2);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100410 const unsigned int conv_w = output_shape[width_idx];
411 const unsigned int conv_h = output_shape[height_idx];
Abe Mbise7784c832018-05-31 16:48:41 +0100412 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
413 const size_t conv_size = conv_w * conv_h;
414
Giorgio Arena26b22162018-08-13 15:49:49 +0100415 // Output auto inizialitation if not yet initialized
Georgios Pinitas10490202018-08-17 17:16:06 +0100416 auto_init_if_empty(*output_clone, input->clone()->set_tensor_shape(output_shape));
Abe Mbise7784c832018-05-31 16:48:41 +0100417 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
418
Giorgio Arena26b22162018-08-13 15:49:49 +0100419 TensorInfo permuted_output;
420 if(input->data_layout() == DataLayout::NHWC)
421 {
422 permute(output_shape, PermutationVector(1U, 2U, 0U));
Georgios Pinitas10490202018-08-17 17:16:06 +0100423 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 +0100424 output_to_use = &permuted_output;
425 }
426
Abe Mbise7784c832018-05-31 16:48:41 +0100427 // Im2Col configuration
Giorgio Arena26b22162018-08-13 15:49:49 +0100428 TensorShape shape_im2col = input_to_use->tensor_shape();
Abe Mbise7784c832018-05-31 16:48:41 +0100429 shape_im2col.set(0, patch_size);
430 shape_im2col.set(1, conv_size);
431 shape_im2col.set(2, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100432 TensorInfo input_reshaped(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col).set_data_layout(DataLayout::NCHW));
433 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseIm2ColKernel::validate(input_to_use, &input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier));
Abe Mbise7784c832018-05-31 16:48:41 +0100434
435 // Weights reshape configuration
436 const TensorShape shape_weights_reshape(patch_size, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100437 TensorInfo weights_reshaped(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW));
438 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseWeightsReshapeKernel::validate(weights_to_use, &weights_reshaped, append_bias ? biases : nullptr));
Abe Mbise7784c832018-05-31 16:48:41 +0100439
440 // GEMV configuration
441 DataType v2mm_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type();
Giorgio Arena26b22162018-08-13 15:49:49 +0100442 TensorShape shape_v2mm_out = input_to_use->tensor_shape();
Abe Mbise7784c832018-05-31 16:48:41 +0100443 shape_v2mm_out.set(0, conv_size * weights_z);
444 shape_v2mm_out.set(1, 1);
445 shape_v2mm_out.set(2, 1);
Giorgio Arena26b22162018-08-13 15:49:49 +0100446 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 +0100447 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixVectorMultiplyKernel::validate(&input_reshaped, &weights_reshaped, &v2mm_output));
448
Giorgio Arena26b22162018-08-13 15:49:49 +0100449 TensorInfo output_reshaped(v2mm_output.clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_to_use->tensor_shape()));
450 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 +0100451
452 if(is_quantized)
453 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100454 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayerOutputStageKernel::validate(&output_reshaped, biases, output_to_use));
Abe Mbise7784c832018-05-31 16:48:41 +0100455 }
456
457 return Status{};
458}
459
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000460void NEDepthwiseConvolutionLayer::run()
Michalis Spyroub7b31532017-11-23 12:10:21 +0000461{
Georgios Pinitas72219332018-06-05 14:56:06 +0100462 prepare();
Georgios Pinitas1562be32018-03-08 19:09:19 +0000463
Giorgio Arena26b22162018-08-13 15:49:49 +0100464 if(_is_nhwc)
465 {
466 _permute_input.run();
467 }
468
Michalis Spyroub7b31532017-11-23 12:10:21 +0000469 NEScheduler::get().schedule(&_im2col_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000470 NEScheduler::get().schedule(&_v2mm_input_fill_border, Window::DimX);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000471 NEScheduler::get().schedule(&_v2mm_kernel, Window::DimX);
472 NEScheduler::get().schedule(&_vector_to_tensor_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000473 if(_is_quantized)
474 {
475 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
476 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100477
478 if(_is_nhwc)
479 {
480 _permute_output.run();
481 }
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000482}
Georgios Pinitas72219332018-06-05 14:56:06 +0100483
484void NEDepthwiseConvolutionLayer::prepare()
485{
486 if(!_is_prepared)
487 {
488 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
489
Giorgio Arena26b22162018-08-13 15:49:49 +0100490 if(_is_nhwc)
491 {
492 _permute_weights.run();
493 }
494
Georgios Pinitas72219332018-06-05 14:56:06 +0100495 // Run reshape and mark original weights as unused
496 _weights_reshaped.allocator()->allocate();
497 NEScheduler::get().schedule(&_weights_reshape_kernel, Window::DimX);
498 NEScheduler::get().schedule(&_v2mm_weights_fill_border, Window::DimX);
499 _original_weights->mark_as_unused();
500
501 _is_prepared = true;
502 }
503}