blob: 24b12f4969f04d2d8af8ea1979726d6ddf6e43d4 [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);
Abe Mbise7784c832018-05-31 16:48:41 +0100166 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() != DataLayout::NCHW && input->data_layout() != DataLayout::NHWC);
167
Giorgio Arena66cbafb2018-08-23 14:51:00 +0100168 if(biases != nullptr)
169 {
170 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
171 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
172 }
173
Abe Mbise7784c832018-05-31 16:48:41 +0100174 return NEDepthwiseConvolutionLayer3x3Kernel::validate(input, weights, output, conv_info, depth_multiplier);
175}
176
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000177void NEDepthwiseConvolutionLayer3x3::run()
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100178{
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100179 if(_is_first_run && _is_optimized)
180 {
181 _is_first_run = false;
182 // Create convolver (deferred)
183 _dwc_kernel.generate_convolver();
184 }
185
Giorgio Arena26b22162018-08-13 15:49:49 +0100186 // Permute weights
187 if(_permute)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000188 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100189 if(!_are_weights_reshaped)
190 {
191 _are_weights_reshaped = true;
192 _permute_weights.run();
193 }
194
195 _permute_input.run();
Georgios Pinitas4074c992018-01-30 18:13:46 +0000196 }
197
198 // Handle input
Giorgio Arena26b22162018-08-13 15:49:49 +0100199 if(!_is_optimized)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000200 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100201 // Fill border
Georgios Pinitas4074c992018-01-30 18:13:46 +0000202 NEScheduler::get().schedule(&_border_handler, Window::DimX);
203 }
204
205 // Execute depthwise convolution
206 NEScheduler::get().schedule(&_dwc_kernel, Window::DimX);
207
Giorgio Arena26b22162018-08-13 15:49:49 +0100208 // Permute output
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100209 if(_is_optimized && _is_nchw)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000210 {
211 _permute_output.run();
212 }
213
214 // Add biases
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000215 if(_has_bias || _is_quantized)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100216 {
Michalis Spyroub91e34c2017-12-20 15:50:55 +0000217 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100218 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100219
220 // Permute output
221 if(!_is_optimized && !_is_nchw)
222 {
223 _permute_output.run();
224 }
Michalis Spyroub7b31532017-11-23 12:10:21 +0000225}
226
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000227NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer()
Giorgio Arena26b22162018-08-13 15:49:49 +0100228 : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _v2mm_input_fill_border(), _v2mm_weights_fill_border(), _permute_input(),
229 _permute_weights(), _permute_output(), _input_reshaped(), _weights_reshaped(), _v2mm_output(), _output_reshaped(), _permuted_input(), _permuted_weights(), _permuted_output(), _is_prepared(false),
230 _is_quantized(false), _is_nhwc(false), _original_weights(nullptr)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000231{
232}
233
Giorgio Arena76572242018-04-04 17:44:26 +0100234void 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 +0000235{
Giorgio Arena26b22162018-08-13 15:49:49 +0100236 const unsigned int channel_idx = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL);
237 ARM_COMPUTE_UNUSED(channel_idx);
238
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000239 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000240 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Giorgio Arena26b22162018-08-13 15:49:49 +0100241 ARM_COMPUTE_ERROR_ON((input->info()->dimension(channel_idx) * depth_multiplier) != weights->info()->dimension(channel_idx));
Michalis Spyroub7b31532017-11-23 12:10:21 +0000242
Giorgio Arena26b22162018-08-13 15:49:49 +0100243 _is_nhwc = input->info()->data_layout() == DataLayout::NHWC;
244
245 ITensor *input_to_use = input;
246 const ITensor *weights_to_use = weights;
247 ITensor *output_to_use = output;
248
249 if(_is_nhwc)
250 {
251 _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
252 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
253 input_to_use = &_permuted_input;
254
255 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
256 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
257 weights_to_use = &_permuted_weights;
258 }
259
260 const size_t weights_w = weights_to_use->info()->dimension(0);
261 const size_t weights_h = weights_to_use->info()->dimension(1);
262 const size_t weights_z = weights_to_use->info()->dimension(2);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000263
Georgios Pinitas1562be32018-03-08 19:09:19 +0000264 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Georgios Pinitas72219332018-06-05 14:56:06 +0100265 _is_prepared = false;
Giorgio Arena26b22162018-08-13 15:49:49 +0100266 _original_weights = weights_to_use;
Michalis Spyroub7b31532017-11-23 12:10:21 +0000267
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000268 // Should bias be appended ?
269 bool append_bias = (biases != nullptr) && !_is_quantized;
270
271 // Calculate output shape
Giorgio Arena76572242018-04-04 17:44:26 +0100272 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier);
273
274 // Output auto inizialitation if not yet initialized
275 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
276 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000277
Giorgio Arena26b22162018-08-13 15:49:49 +0100278 if(_is_nhwc)
279 {
280 permute(output_shape, PermutationVector(1U, 2U, 0U));
281 _permuted_output.allocator()->init(output->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
282 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
283 output_to_use = &_permuted_output;
284 }
285
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000286 // Output width and height
Giorgio Arena76572242018-04-04 17:44:26 +0100287 const unsigned int conv_w = output_shape.x();
288 const unsigned int conv_h = output_shape.y();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000289
290 // Set up intermediate tensors
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000291 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000292 const size_t conv_size = conv_w * conv_h;
293
294 // Im2Col configuration
Giorgio Arena26b22162018-08-13 15:49:49 +0100295 TensorShape shape_im2col = input_to_use->info()->tensor_shape();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000296 shape_im2col.set(0, patch_size);
297 shape_im2col.set(1, conv_size);
298 shape_im2col.set(2, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100299 _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col).set_data_layout(DataLayout::NCHW));
300 _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 +0000301
302 // Weights reshape configuration
303 const TensorShape shape_weights_reshape(patch_size, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100304 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW));
305 _weights_reshape_kernel.configure(weights_to_use, &_weights_reshaped, append_bias ? biases : nullptr);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000306
307 // GEMV configuration
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000308 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
Giorgio Arena26b22162018-08-13 15:49:49 +0100309 TensorShape shape_v2mm_out = input_to_use->info()->tensor_shape();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000310 shape_v2mm_out.set(0, conv_size * weights_z);
311 shape_v2mm_out.set(1, 1);
312 shape_v2mm_out.set(2, 1);
Giorgio Arena26b22162018-08-13 15:49:49 +0100313 _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 +0000314 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
Giorgio Arena76572242018-04-04 17:44:26 +0100315 _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 +0100316 _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 +0000317
318 // Output staged configuration
319 if(_is_quantized)
320 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100321 const QuantizationInfo output_quant_info = output->info()->quantization_info();
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000322
323 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000324 int output_multiplier, output_shift;
325 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Giorgio Arena26b22162018-08-13 15:49:49 +0100326 _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 +0000327 _output_reshaped.allocator()->allocate();
328 }
329
Giorgio Arena26b22162018-08-13 15:49:49 +0100330 if(_is_nhwc)
331 {
332 _permute_output.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
333
334 _permuted_input.allocator()->allocate();
335 _permuted_weights.allocator()->allocate();
336 _permuted_output.allocator()->allocate();
337 }
338
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000339 // Fill borders on inputs
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000340 PixelValue zero_in(static_cast<int32_t>(0));
341 PixelValue zero_w(static_cast<int32_t>(0));
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000342 if(_is_quantized)
343 {
344 zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset));
345 zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset));
346 }
347 BorderSize border_size = _v2mm_kernel.border_size();
348 _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in);
349
350 border_size.bottom = 0;
351 _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000352
353 // Allocate intermediate tensors
354 _input_reshaped.allocator()->allocate();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000355 _v2mm_output.allocator()->allocate();
356}
357
Georgios Pinitas10490202018-08-17 17:16:06 +0100358Status 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 +0100359 unsigned int depth_multiplier)
360{
361 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
362 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() != DataLayout::NCHW && input->data_layout() != DataLayout::NHWC);
363
Georgios Pinitas10490202018-08-17 17:16:06 +0100364 // Clone output to use auto init
365 auto output_clone = output->clone();
366
Giorgio Arena26b22162018-08-13 15:49:49 +0100367 const ITensorInfo *input_to_use = input;
368 const ITensorInfo *weights_to_use = weights;
Georgios Pinitas10490202018-08-17 17:16:06 +0100369 const ITensorInfo *output_to_use = output_clone.get();
Giorgio Arena26b22162018-08-13 15:49:49 +0100370
371 TensorShape permuted_input_shape = input->tensor_shape();
372 TensorShape permuted_weights_shape = weights->tensor_shape();
373 TensorInfo permuted_input;
374 TensorInfo permuted_weights;
375
376 if(input->data_layout() == DataLayout::NHWC)
377 {
378 permute(permuted_input_shape, PermutationVector(1U, 2U, 0U));
379 permute(permuted_weights_shape, PermutationVector(1U, 2U, 0U));
380
381 permuted_input = TensorInfo(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NCHW));
382 permuted_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NCHW));
383
384 input_to_use = &permuted_input;
385 weights_to_use = &permuted_weights;
386 }
387
Abe Mbise7784c832018-05-31 16:48:41 +0100388 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
389 const bool append_bias = (biases != nullptr) && !is_quantized;
Giorgio Arena26b22162018-08-13 15:49:49 +0100390 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier);
391 const size_t weights_w = weights_to_use->dimension(0);
392 const size_t weights_h = weights_to_use->dimension(1);
393 const size_t weights_z = weights_to_use->dimension(2);
Abe Mbise7784c832018-05-31 16:48:41 +0100394 const unsigned int conv_w = output_shape.x();
395 const unsigned int conv_h = output_shape.y();
396 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
397 const size_t conv_size = conv_w * conv_h;
398
Giorgio Arena26b22162018-08-13 15:49:49 +0100399 // Output auto inizialitation if not yet initialized
Georgios Pinitas10490202018-08-17 17:16:06 +0100400 auto_init_if_empty(*output_clone, input->clone()->set_tensor_shape(output_shape));
Abe Mbise7784c832018-05-31 16:48:41 +0100401 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
402
Giorgio Arena26b22162018-08-13 15:49:49 +0100403 TensorInfo permuted_output;
404 if(input->data_layout() == DataLayout::NHWC)
405 {
406 permute(output_shape, PermutationVector(1U, 2U, 0U));
Georgios Pinitas10490202018-08-17 17:16:06 +0100407 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 +0100408 output_to_use = &permuted_output;
409 }
410
Abe Mbise7784c832018-05-31 16:48:41 +0100411 // Im2Col configuration
Giorgio Arena26b22162018-08-13 15:49:49 +0100412 TensorShape shape_im2col = input_to_use->tensor_shape();
Abe Mbise7784c832018-05-31 16:48:41 +0100413 shape_im2col.set(0, patch_size);
414 shape_im2col.set(1, conv_size);
415 shape_im2col.set(2, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100416 TensorInfo input_reshaped(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col).set_data_layout(DataLayout::NCHW));
417 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 +0100418
419 // Weights reshape configuration
420 const TensorShape shape_weights_reshape(patch_size, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100421 TensorInfo weights_reshaped(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW));
422 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseWeightsReshapeKernel::validate(weights_to_use, &weights_reshaped, append_bias ? biases : nullptr));
Abe Mbise7784c832018-05-31 16:48:41 +0100423
424 // GEMV configuration
425 DataType v2mm_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type();
Giorgio Arena26b22162018-08-13 15:49:49 +0100426 TensorShape shape_v2mm_out = input_to_use->tensor_shape();
Abe Mbise7784c832018-05-31 16:48:41 +0100427 shape_v2mm_out.set(0, conv_size * weights_z);
428 shape_v2mm_out.set(1, 1);
429 shape_v2mm_out.set(2, 1);
Giorgio Arena26b22162018-08-13 15:49:49 +0100430 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 +0100431 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixVectorMultiplyKernel::validate(&input_reshaped, &weights_reshaped, &v2mm_output));
432
Giorgio Arena26b22162018-08-13 15:49:49 +0100433 TensorInfo output_reshaped(v2mm_output.clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_to_use->tensor_shape()));
434 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 +0100435
436 if(is_quantized)
437 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100438 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayerOutputStageKernel::validate(&output_reshaped, biases, output_to_use));
Abe Mbise7784c832018-05-31 16:48:41 +0100439 }
440
441 return Status{};
442}
443
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000444void NEDepthwiseConvolutionLayer::run()
Michalis Spyroub7b31532017-11-23 12:10:21 +0000445{
Georgios Pinitas72219332018-06-05 14:56:06 +0100446 prepare();
Georgios Pinitas1562be32018-03-08 19:09:19 +0000447
Giorgio Arena26b22162018-08-13 15:49:49 +0100448 if(_is_nhwc)
449 {
450 _permute_input.run();
451 }
452
Michalis Spyroub7b31532017-11-23 12:10:21 +0000453 NEScheduler::get().schedule(&_im2col_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000454 NEScheduler::get().schedule(&_v2mm_input_fill_border, Window::DimX);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000455 NEScheduler::get().schedule(&_v2mm_kernel, Window::DimX);
456 NEScheduler::get().schedule(&_vector_to_tensor_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000457 if(_is_quantized)
458 {
459 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
460 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100461
462 if(_is_nhwc)
463 {
464 _permute_output.run();
465 }
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000466}
Georgios Pinitas72219332018-06-05 14:56:06 +0100467
468void NEDepthwiseConvolutionLayer::prepare()
469{
470 if(!_is_prepared)
471 {
472 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
473
Giorgio Arena26b22162018-08-13 15:49:49 +0100474 if(_is_nhwc)
475 {
476 _permute_weights.run();
477 }
478
Georgios Pinitas72219332018-06-05 14:56:06 +0100479 // Run reshape and mark original weights as unused
480 _weights_reshaped.allocator()->allocate();
481 NEScheduler::get().schedule(&_weights_reshape_kernel, Window::DimX);
482 NEScheduler::get().schedule(&_v2mm_weights_fill_border, Window::DimX);
483 _original_weights->mark_as_unused();
484
485 _is_prepared = true;
486 }
487}