blob: ccbd01e2e25a18644f264d83d57177a830e8352c [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 Pinitasf72f9362018-01-12 16:29:45 +000046 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, 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 Pinitas4074c992018-01-30 18:13:46 +000062 if(_is_optimized)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010063 {
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010064 if(_is_nchw)
65 {
66 // Configure the function to transform the input tensor from NCHW -> NHWC
Giorgio Arena26b22162018-08-13 15:49:49 +010067 _permute_input.configure(input, &_permuted_input, PermutationVector(2U, 0U, 1U));
68 _permuted_input.info()->set_data_layout(DataLayout::NHWC);
Georgios Pinitas4074c992018-01-30 18:13:46 +000069
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010070 // Configure the function to transform the weights tensor from IHW -> HWI
Giorgio Arena26b22162018-08-13 15:49:49 +010071 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(2U, 0U, 1U));
72 _permuted_weights.info()->set_data_layout(DataLayout::NHWC);
Georgios Pinitas4074c992018-01-30 18:13:46 +000073
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010074 // Configure optimized depthwise
Giorgio Arena26b22162018-08-13 15:49:49 +010075 _dwc_kernel.configure(&_permuted_input, &_permuted_weights, &_permuted_output, conv_info, depth_multiplier, DataLayout::NHWC);
Georgios Pinitas4074c992018-01-30 18:13:46 +000076
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010077 // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
Giorgio Arena26b22162018-08-13 15:49:49 +010078 _permute_output.configure(&_permuted_output, output, PermutationVector(1U, 2U, 0U));
79 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
Georgios Pinitas4074c992018-01-30 18:13:46 +000080
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010081 // Allocate tensors
Giorgio Arena26b22162018-08-13 15:49:49 +010082 _permuted_input.allocator()->allocate();
83 _permuted_weights.allocator()->allocate();
84 _permuted_output.allocator()->allocate();
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010085 }
86 else
87 {
Giorgio Arena76572242018-04-04 17:44:26 +010088 _dwc_kernel.configure(input, weights, output, conv_info, depth_multiplier, DataLayout::NHWC);
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010089 }
Georgios Pinitasf72f9362018-01-12 16:29:45 +000090 }
Georgios Pinitas4074c992018-01-30 18:13:46 +000091 else
92 {
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010093 // Allocate the intermediate accumulator tensor in case of quantized input
Georgios Pinitas4074c992018-01-30 18:13:46 +000094 if(_is_quantized)
95 {
Giorgio Arena26b22162018-08-13 15:49:49 +010096 TensorShape accum_shape = output->info()->tensor_shape();
97
98 if(!_is_nchw)
99 {
100 permute(accum_shape, PermutationVector(1U, 2U, 0U));
101 }
102
103 _accumulator.allocator()->init(TensorInfo(accum_shape, 1, DataType::S32));
Georgios Pinitas4074c992018-01-30 18:13:46 +0000104 _accumulator.info()->set_quantization_info(input->info()->quantization_info());
105 zero_value = PixelValue(static_cast<uint32_t>(input->info()->quantization_info().offset));
106 }
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000107
Giorgio Arena26b22162018-08-13 15:49:49 +0100108 if(!_is_nchw)
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000109 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100110 // Configure the function to transform the input tensor from NHWC -> NCHW
111 _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
112 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000113
Giorgio Arena26b22162018-08-13 15:49:49 +0100114 // Configure the function to transform the weights tensor from HWI -> IHW
115 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
116 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
117
118 // Configure optimized depthwise
119 _dwc_kernel.configure(&_permuted_input, &_permuted_weights, (_is_quantized) ? &_accumulator : &_permuted_output, conv_info, depth_multiplier);
120
121 // Configure border handler
122 _border_handler.configure(&_permuted_input, _dwc_kernel.border_size(), BorderMode::CONSTANT, zero_value);
123
124 // Allocate tensors
125 _permuted_input.allocator()->allocate();
126 _permuted_weights.allocator()->allocate();
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000127 }
128 else
129 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100130 // Configure depthwise convolution kernel
131 _dwc_kernel.configure(input, weights, (_is_quantized) ? &_accumulator : output, conv_info, depth_multiplier);
132
133 // Configure border handler
134 _border_handler.configure(input, _dwc_kernel.border_size(), BorderMode::CONSTANT, zero_value);
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000135 }
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100136 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100137
138 // Configure biases accumulation
139 if(_is_quantized)
140 {
141 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
142
143 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
144 int output_multiplier, output_shift;
145 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
146 _output_stage_kernel.configure(&_accumulator, biases, _is_nchw ? output : &_permuted_output, output_multiplier, output_shift, output_quant_info.offset);
147 _accumulator.allocator()->allocate();
148 }
149 else if(_has_bias)
150 {
151 _output_stage_kernel.configure((_is_nchw || _is_optimized) ? output : &_permuted_output, biases);
152 }
153
154 if(!_is_optimized && !_is_nchw)
155 {
156 // Configure the function to transform the convoluted output to NHWC
157 _permute_output.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
158 _permuted_output.allocator()->allocate();
159 }
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100160}
161
Abe Mbise7784c832018-05-31 16:48:41 +0100162Status NEDepthwiseConvolutionLayer3x3::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
163 unsigned int depth_multiplier)
164{
165 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100166 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Abe Mbise7784c832018-05-31 16:48:41 +0100167
Giorgio Arena66cbafb2018-08-23 14:51:00 +0100168 if(biases != nullptr)
169 {
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100170 const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
Giorgio Arena66cbafb2018-08-23 14:51:00 +0100171 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100172 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(channel_idx));
Giorgio Arena66cbafb2018-08-23 14:51:00 +0100173 }
174
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100175 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
176 TensorInfo accumulator = TensorInfo(output->clone()->set_is_resizable(true).reset_padding().set_data_type(DataType::S32));
177
178 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseConvolutionLayer3x3Kernel::validate(input, weights, is_quantized ? &accumulator : output, conv_info, depth_multiplier));
179
180 if(is_quantized)
181 {
182 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayerOutputStageKernel::validate(&accumulator, biases, output));
183 }
184
185 return Status{};
Abe Mbise7784c832018-05-31 16:48:41 +0100186}
187
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000188void NEDepthwiseConvolutionLayer3x3::run()
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100189{
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100190 if(_is_first_run && _is_optimized)
191 {
192 _is_first_run = false;
193 // Create convolver (deferred)
194 _dwc_kernel.generate_convolver();
195 }
196
Giorgio Arena26b22162018-08-13 15:49:49 +0100197 // Permute weights
198 if(_permute)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000199 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100200 if(!_are_weights_reshaped)
201 {
202 _are_weights_reshaped = true;
203 _permute_weights.run();
204 }
205
206 _permute_input.run();
Georgios Pinitas4074c992018-01-30 18:13:46 +0000207 }
208
209 // Handle input
Giorgio Arena26b22162018-08-13 15:49:49 +0100210 if(!_is_optimized)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000211 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100212 // Fill border
Georgios Pinitas4074c992018-01-30 18:13:46 +0000213 NEScheduler::get().schedule(&_border_handler, Window::DimX);
214 }
215
216 // Execute depthwise convolution
217 NEScheduler::get().schedule(&_dwc_kernel, Window::DimX);
218
Giorgio Arena26b22162018-08-13 15:49:49 +0100219 // Permute output
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100220 if(_is_optimized && _is_nchw)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000221 {
222 _permute_output.run();
223 }
224
225 // Add biases
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000226 if(_has_bias || _is_quantized)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100227 {
Michalis Spyroub91e34c2017-12-20 15:50:55 +0000228 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100229 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100230
231 // Permute output
232 if(!_is_optimized && !_is_nchw)
233 {
234 _permute_output.run();
235 }
Michalis Spyroub7b31532017-11-23 12:10:21 +0000236}
237
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000238NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer()
Giorgio Arena26b22162018-08-13 15:49:49 +0100239 : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _v2mm_input_fill_border(), _v2mm_weights_fill_border(), _permute_input(),
240 _permute_weights(), _permute_output(), _input_reshaped(), _weights_reshaped(), _v2mm_output(), _output_reshaped(), _permuted_input(), _permuted_weights(), _permuted_output(), _is_prepared(false),
241 _is_quantized(false), _is_nhwc(false), _original_weights(nullptr)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000242{
243}
244
Giorgio Arena76572242018-04-04 17:44:26 +0100245void 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 +0000246{
Giorgio Arena26b22162018-08-13 15:49:49 +0100247 const unsigned int channel_idx = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL);
248 ARM_COMPUTE_UNUSED(channel_idx);
249
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000250 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000251 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Giorgio Arena26b22162018-08-13 15:49:49 +0100252 ARM_COMPUTE_ERROR_ON((input->info()->dimension(channel_idx) * depth_multiplier) != weights->info()->dimension(channel_idx));
Michalis Spyroub7b31532017-11-23 12:10:21 +0000253
Giorgio Arena26b22162018-08-13 15:49:49 +0100254 _is_nhwc = input->info()->data_layout() == DataLayout::NHWC;
255
256 ITensor *input_to_use = input;
257 const ITensor *weights_to_use = weights;
258 ITensor *output_to_use = output;
259
260 if(_is_nhwc)
261 {
262 _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
263 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
264 input_to_use = &_permuted_input;
265
266 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
267 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
268 weights_to_use = &_permuted_weights;
269 }
270
271 const size_t weights_w = weights_to_use->info()->dimension(0);
272 const size_t weights_h = weights_to_use->info()->dimension(1);
273 const size_t weights_z = weights_to_use->info()->dimension(2);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000274
Georgios Pinitas1562be32018-03-08 19:09:19 +0000275 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Georgios Pinitas72219332018-06-05 14:56:06 +0100276 _is_prepared = false;
Giorgio Arena26b22162018-08-13 15:49:49 +0100277 _original_weights = weights_to_use;
Michalis Spyroub7b31532017-11-23 12:10:21 +0000278
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000279 // Should bias be appended ?
280 bool append_bias = (biases != nullptr) && !_is_quantized;
281
282 // Calculate output shape
Giorgio Arena76572242018-04-04 17:44:26 +0100283 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier);
284
285 // Output auto inizialitation if not yet initialized
286 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
287 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000288
Giorgio Arena26b22162018-08-13 15:49:49 +0100289 if(_is_nhwc)
290 {
291 permute(output_shape, PermutationVector(1U, 2U, 0U));
292 _permuted_output.allocator()->init(output->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
293 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
294 output_to_use = &_permuted_output;
295 }
296
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000297 // Output width and height
Giorgio Arena76572242018-04-04 17:44:26 +0100298 const unsigned int conv_w = output_shape.x();
299 const unsigned int conv_h = output_shape.y();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000300
301 // Set up intermediate tensors
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000302 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000303 const size_t conv_size = conv_w * conv_h;
304
305 // Im2Col configuration
Giorgio Arena26b22162018-08-13 15:49:49 +0100306 TensorShape shape_im2col = input_to_use->info()->tensor_shape();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000307 shape_im2col.set(0, patch_size);
308 shape_im2col.set(1, conv_size);
309 shape_im2col.set(2, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100310 _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col).set_data_layout(DataLayout::NCHW));
311 _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 +0000312
313 // Weights reshape configuration
314 const TensorShape shape_weights_reshape(patch_size, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100315 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW));
316 _weights_reshape_kernel.configure(weights_to_use, &_weights_reshaped, append_bias ? biases : nullptr);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000317
318 // GEMV configuration
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000319 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
Giorgio Arena26b22162018-08-13 15:49:49 +0100320 TensorShape shape_v2mm_out = input_to_use->info()->tensor_shape();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000321 shape_v2mm_out.set(0, conv_size * weights_z);
322 shape_v2mm_out.set(1, 1);
323 shape_v2mm_out.set(2, 1);
Giorgio Arena26b22162018-08-13 15:49:49 +0100324 _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 +0000325 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
Giorgio Arena76572242018-04-04 17:44:26 +0100326 _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 +0100327 _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 +0000328
329 // Output staged configuration
330 if(_is_quantized)
331 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100332 const QuantizationInfo output_quant_info = output->info()->quantization_info();
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000333
334 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000335 int output_multiplier, output_shift;
336 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Giorgio Arena26b22162018-08-13 15:49:49 +0100337 _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 +0000338 _output_reshaped.allocator()->allocate();
339 }
340
Giorgio Arena26b22162018-08-13 15:49:49 +0100341 if(_is_nhwc)
342 {
343 _permute_output.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
344
345 _permuted_input.allocator()->allocate();
346 _permuted_weights.allocator()->allocate();
347 _permuted_output.allocator()->allocate();
348 }
349
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000350 // Fill borders on inputs
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000351 PixelValue zero_in(static_cast<int32_t>(0));
352 PixelValue zero_w(static_cast<int32_t>(0));
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000353 if(_is_quantized)
354 {
355 zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset));
356 zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset));
357 }
358 BorderSize border_size = _v2mm_kernel.border_size();
359 _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in);
360
361 border_size.bottom = 0;
362 _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000363
364 // Allocate intermediate tensors
365 _input_reshaped.allocator()->allocate();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000366 _v2mm_output.allocator()->allocate();
367}
368
Georgios Pinitas10490202018-08-17 17:16:06 +0100369Status 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 +0100370 unsigned int depth_multiplier)
371{
372 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100373 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
374
375 const unsigned int width_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
376 const unsigned int height_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
Abe Mbise7784c832018-05-31 16:48:41 +0100377
Georgios Pinitas10490202018-08-17 17:16:06 +0100378 // Clone output to use auto init
379 auto output_clone = output->clone();
380
Giorgio Arena26b22162018-08-13 15:49:49 +0100381 const ITensorInfo *input_to_use = input;
382 const ITensorInfo *weights_to_use = weights;
Georgios Pinitas10490202018-08-17 17:16:06 +0100383 const ITensorInfo *output_to_use = output_clone.get();
Giorgio Arena26b22162018-08-13 15:49:49 +0100384
385 TensorShape permuted_input_shape = input->tensor_shape();
386 TensorShape permuted_weights_shape = weights->tensor_shape();
387 TensorInfo permuted_input;
388 TensorInfo permuted_weights;
389
390 if(input->data_layout() == DataLayout::NHWC)
391 {
392 permute(permuted_input_shape, PermutationVector(1U, 2U, 0U));
393 permute(permuted_weights_shape, PermutationVector(1U, 2U, 0U));
394
395 permuted_input = TensorInfo(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NCHW));
396 permuted_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NCHW));
397
398 input_to_use = &permuted_input;
399 weights_to_use = &permuted_weights;
400 }
401
Abe Mbise7784c832018-05-31 16:48:41 +0100402 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
403 const bool append_bias = (biases != nullptr) && !is_quantized;
Giorgio Arena26b22162018-08-13 15:49:49 +0100404 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier);
405 const size_t weights_w = weights_to_use->dimension(0);
406 const size_t weights_h = weights_to_use->dimension(1);
407 const size_t weights_z = weights_to_use->dimension(2);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100408 const unsigned int conv_w = output_shape[width_idx];
409 const unsigned int conv_h = output_shape[height_idx];
Abe Mbise7784c832018-05-31 16:48:41 +0100410 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
411 const size_t conv_size = conv_w * conv_h;
412
Giorgio Arena26b22162018-08-13 15:49:49 +0100413 // Output auto inizialitation if not yet initialized
Georgios Pinitas10490202018-08-17 17:16:06 +0100414 auto_init_if_empty(*output_clone, input->clone()->set_tensor_shape(output_shape));
Abe Mbise7784c832018-05-31 16:48:41 +0100415 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
416
Giorgio Arena26b22162018-08-13 15:49:49 +0100417 TensorInfo permuted_output;
418 if(input->data_layout() == DataLayout::NHWC)
419 {
420 permute(output_shape, PermutationVector(1U, 2U, 0U));
Georgios Pinitas10490202018-08-17 17:16:06 +0100421 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 +0100422 output_to_use = &permuted_output;
423 }
424
Abe Mbise7784c832018-05-31 16:48:41 +0100425 // Im2Col configuration
Giorgio Arena26b22162018-08-13 15:49:49 +0100426 TensorShape shape_im2col = input_to_use->tensor_shape();
Abe Mbise7784c832018-05-31 16:48:41 +0100427 shape_im2col.set(0, patch_size);
428 shape_im2col.set(1, conv_size);
429 shape_im2col.set(2, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100430 TensorInfo input_reshaped(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col).set_data_layout(DataLayout::NCHW));
431 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 +0100432
433 // Weights reshape configuration
434 const TensorShape shape_weights_reshape(patch_size, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100435 TensorInfo weights_reshaped(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW));
436 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseWeightsReshapeKernel::validate(weights_to_use, &weights_reshaped, append_bias ? biases : nullptr));
Abe Mbise7784c832018-05-31 16:48:41 +0100437
438 // GEMV configuration
439 DataType v2mm_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type();
Giorgio Arena26b22162018-08-13 15:49:49 +0100440 TensorShape shape_v2mm_out = input_to_use->tensor_shape();
Abe Mbise7784c832018-05-31 16:48:41 +0100441 shape_v2mm_out.set(0, conv_size * weights_z);
442 shape_v2mm_out.set(1, 1);
443 shape_v2mm_out.set(2, 1);
Giorgio Arena26b22162018-08-13 15:49:49 +0100444 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 +0100445 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixVectorMultiplyKernel::validate(&input_reshaped, &weights_reshaped, &v2mm_output));
446
Giorgio Arena26b22162018-08-13 15:49:49 +0100447 TensorInfo output_reshaped(v2mm_output.clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_to_use->tensor_shape()));
448 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 +0100449
450 if(is_quantized)
451 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100452 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayerOutputStageKernel::validate(&output_reshaped, biases, output_to_use));
Abe Mbise7784c832018-05-31 16:48:41 +0100453 }
454
455 return Status{};
456}
457
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000458void NEDepthwiseConvolutionLayer::run()
Michalis Spyroub7b31532017-11-23 12:10:21 +0000459{
Georgios Pinitas72219332018-06-05 14:56:06 +0100460 prepare();
Georgios Pinitas1562be32018-03-08 19:09:19 +0000461
Giorgio Arena26b22162018-08-13 15:49:49 +0100462 if(_is_nhwc)
463 {
464 _permute_input.run();
465 }
466
Michalis Spyroub7b31532017-11-23 12:10:21 +0000467 NEScheduler::get().schedule(&_im2col_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000468 NEScheduler::get().schedule(&_v2mm_input_fill_border, Window::DimX);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000469 NEScheduler::get().schedule(&_v2mm_kernel, Window::DimX);
470 NEScheduler::get().schedule(&_vector_to_tensor_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000471 if(_is_quantized)
472 {
473 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
474 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100475
476 if(_is_nhwc)
477 {
478 _permute_output.run();
479 }
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000480}
Georgios Pinitas72219332018-06-05 14:56:06 +0100481
482void NEDepthwiseConvolutionLayer::prepare()
483{
484 if(!_is_prepared)
485 {
486 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
487
Giorgio Arena26b22162018-08-13 15:49:49 +0100488 if(_is_nhwc)
489 {
490 _permute_weights.run();
491 }
492
Georgios Pinitas72219332018-06-05 14:56:06 +0100493 // Run reshape and mark original weights as unused
494 _weights_reshaped.allocator()->allocate();
495 NEScheduler::get().schedule(&_weights_reshape_kernel, Window::DimX);
496 NEScheduler::get().schedule(&_v2mm_weights_fill_border, Window::DimX);
497 _original_weights->mark_as_unused();
498
499 _is_prepared = true;
500 }
501}