blob: 8d22f03d7f453fd633b1db3004407c683aa153bf [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);
166 ARM_COMPUTE_UNUSED(biases);
167 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() != DataLayout::NCHW && input->data_layout() != DataLayout::NHWC);
168
169 return NEDepthwiseConvolutionLayer3x3Kernel::validate(input, weights, output, conv_info, depth_multiplier);
170}
171
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000172void NEDepthwiseConvolutionLayer3x3::run()
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100173{
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100174 if(_is_first_run && _is_optimized)
175 {
176 _is_first_run = false;
177 // Create convolver (deferred)
178 _dwc_kernel.generate_convolver();
179 }
180
Giorgio Arena26b22162018-08-13 15:49:49 +0100181 // Permute weights
182 if(_permute)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000183 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100184 if(!_are_weights_reshaped)
185 {
186 _are_weights_reshaped = true;
187 _permute_weights.run();
188 }
189
190 _permute_input.run();
Georgios Pinitas4074c992018-01-30 18:13:46 +0000191 }
192
193 // Handle input
Giorgio Arena26b22162018-08-13 15:49:49 +0100194 if(!_is_optimized)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000195 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100196 // Fill border
Georgios Pinitas4074c992018-01-30 18:13:46 +0000197 NEScheduler::get().schedule(&_border_handler, Window::DimX);
198 }
199
200 // Execute depthwise convolution
201 NEScheduler::get().schedule(&_dwc_kernel, Window::DimX);
202
Giorgio Arena26b22162018-08-13 15:49:49 +0100203 // Permute output
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100204 if(_is_optimized && _is_nchw)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000205 {
206 _permute_output.run();
207 }
208
209 // Add biases
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000210 if(_has_bias || _is_quantized)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100211 {
Michalis Spyroub91e34c2017-12-20 15:50:55 +0000212 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100213 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100214
215 // Permute output
216 if(!_is_optimized && !_is_nchw)
217 {
218 _permute_output.run();
219 }
Michalis Spyroub7b31532017-11-23 12:10:21 +0000220}
221
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000222NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer()
Giorgio Arena26b22162018-08-13 15:49:49 +0100223 : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _v2mm_input_fill_border(), _v2mm_weights_fill_border(), _permute_input(),
224 _permute_weights(), _permute_output(), _input_reshaped(), _weights_reshaped(), _v2mm_output(), _output_reshaped(), _permuted_input(), _permuted_weights(), _permuted_output(), _is_prepared(false),
225 _is_quantized(false), _is_nhwc(false), _original_weights(nullptr)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000226{
227}
228
Giorgio Arena76572242018-04-04 17:44:26 +0100229void 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 +0000230{
Giorgio Arena26b22162018-08-13 15:49:49 +0100231 const unsigned int channel_idx = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL);
232 ARM_COMPUTE_UNUSED(channel_idx);
233
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000234 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000235 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Giorgio Arena26b22162018-08-13 15:49:49 +0100236 ARM_COMPUTE_ERROR_ON((input->info()->dimension(channel_idx) * depth_multiplier) != weights->info()->dimension(channel_idx));
Michalis Spyroub7b31532017-11-23 12:10:21 +0000237
Giorgio Arena26b22162018-08-13 15:49:49 +0100238 _is_nhwc = input->info()->data_layout() == DataLayout::NHWC;
239
240 ITensor *input_to_use = input;
241 const ITensor *weights_to_use = weights;
242 ITensor *output_to_use = output;
243
244 if(_is_nhwc)
245 {
246 _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
247 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
248 input_to_use = &_permuted_input;
249
250 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
251 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
252 weights_to_use = &_permuted_weights;
253 }
254
255 const size_t weights_w = weights_to_use->info()->dimension(0);
256 const size_t weights_h = weights_to_use->info()->dimension(1);
257 const size_t weights_z = weights_to_use->info()->dimension(2);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000258
Georgios Pinitas1562be32018-03-08 19:09:19 +0000259 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Georgios Pinitas72219332018-06-05 14:56:06 +0100260 _is_prepared = false;
Giorgio Arena26b22162018-08-13 15:49:49 +0100261 _original_weights = weights_to_use;
Michalis Spyroub7b31532017-11-23 12:10:21 +0000262
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000263 // Should bias be appended ?
264 bool append_bias = (biases != nullptr) && !_is_quantized;
265
266 // Calculate output shape
Giorgio Arena76572242018-04-04 17:44:26 +0100267 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier);
268
269 // Output auto inizialitation if not yet initialized
270 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
271 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000272
Giorgio Arena26b22162018-08-13 15:49:49 +0100273 if(_is_nhwc)
274 {
275 permute(output_shape, PermutationVector(1U, 2U, 0U));
276 _permuted_output.allocator()->init(output->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
277 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
278 output_to_use = &_permuted_output;
279 }
280
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000281 // Output width and height
Giorgio Arena76572242018-04-04 17:44:26 +0100282 const unsigned int conv_w = output_shape.x();
283 const unsigned int conv_h = output_shape.y();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000284
285 // Set up intermediate tensors
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000286 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000287 const size_t conv_size = conv_w * conv_h;
288
289 // Im2Col configuration
Giorgio Arena26b22162018-08-13 15:49:49 +0100290 TensorShape shape_im2col = input_to_use->info()->tensor_shape();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000291 shape_im2col.set(0, patch_size);
292 shape_im2col.set(1, conv_size);
293 shape_im2col.set(2, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100294 _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col).set_data_layout(DataLayout::NCHW));
295 _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 +0000296
297 // Weights reshape configuration
298 const TensorShape shape_weights_reshape(patch_size, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100299 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW));
300 _weights_reshape_kernel.configure(weights_to_use, &_weights_reshaped, append_bias ? biases : nullptr);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000301
302 // GEMV configuration
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000303 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
Giorgio Arena26b22162018-08-13 15:49:49 +0100304 TensorShape shape_v2mm_out = input_to_use->info()->tensor_shape();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000305 shape_v2mm_out.set(0, conv_size * weights_z);
306 shape_v2mm_out.set(1, 1);
307 shape_v2mm_out.set(2, 1);
Giorgio Arena26b22162018-08-13 15:49:49 +0100308 _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 +0000309 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
Giorgio Arena76572242018-04-04 17:44:26 +0100310 _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 +0100311 _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 +0000312
313 // Output staged configuration
314 if(_is_quantized)
315 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100316 const QuantizationInfo output_quant_info = output->info()->quantization_info();
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000317
318 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000319 int output_multiplier, output_shift;
320 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Giorgio Arena26b22162018-08-13 15:49:49 +0100321 _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 +0000322 _output_reshaped.allocator()->allocate();
323 }
324
Giorgio Arena26b22162018-08-13 15:49:49 +0100325 if(_is_nhwc)
326 {
327 _permute_output.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
328
329 _permuted_input.allocator()->allocate();
330 _permuted_weights.allocator()->allocate();
331 _permuted_output.allocator()->allocate();
332 }
333
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000334 // Fill borders on inputs
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000335 PixelValue zero_in(static_cast<int32_t>(0));
336 PixelValue zero_w(static_cast<int32_t>(0));
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000337 if(_is_quantized)
338 {
339 zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset));
340 zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset));
341 }
342 BorderSize border_size = _v2mm_kernel.border_size();
343 _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in);
344
345 border_size.bottom = 0;
346 _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000347
348 // Allocate intermediate tensors
349 _input_reshaped.allocator()->allocate();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000350 _v2mm_output.allocator()->allocate();
351}
352
Georgios Pinitas10490202018-08-17 17:16:06 +0100353Status 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 +0100354 unsigned int depth_multiplier)
355{
356 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
357 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() != DataLayout::NCHW && input->data_layout() != DataLayout::NHWC);
358
Georgios Pinitas10490202018-08-17 17:16:06 +0100359 // Clone output to use auto init
360 auto output_clone = output->clone();
361
Giorgio Arena26b22162018-08-13 15:49:49 +0100362 const ITensorInfo *input_to_use = input;
363 const ITensorInfo *weights_to_use = weights;
Georgios Pinitas10490202018-08-17 17:16:06 +0100364 const ITensorInfo *output_to_use = output_clone.get();
Giorgio Arena26b22162018-08-13 15:49:49 +0100365
366 TensorShape permuted_input_shape = input->tensor_shape();
367 TensorShape permuted_weights_shape = weights->tensor_shape();
368 TensorInfo permuted_input;
369 TensorInfo permuted_weights;
370
371 if(input->data_layout() == DataLayout::NHWC)
372 {
373 permute(permuted_input_shape, PermutationVector(1U, 2U, 0U));
374 permute(permuted_weights_shape, PermutationVector(1U, 2U, 0U));
375
376 permuted_input = TensorInfo(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NCHW));
377 permuted_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NCHW));
378
379 input_to_use = &permuted_input;
380 weights_to_use = &permuted_weights;
381 }
382
Abe Mbise7784c832018-05-31 16:48:41 +0100383 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
384 const bool append_bias = (biases != nullptr) && !is_quantized;
Giorgio Arena26b22162018-08-13 15:49:49 +0100385 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier);
386 const size_t weights_w = weights_to_use->dimension(0);
387 const size_t weights_h = weights_to_use->dimension(1);
388 const size_t weights_z = weights_to_use->dimension(2);
Abe Mbise7784c832018-05-31 16:48:41 +0100389 const unsigned int conv_w = output_shape.x();
390 const unsigned int conv_h = output_shape.y();
391 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
392 const size_t conv_size = conv_w * conv_h;
393
Giorgio Arena26b22162018-08-13 15:49:49 +0100394 // Output auto inizialitation if not yet initialized
Georgios Pinitas10490202018-08-17 17:16:06 +0100395 auto_init_if_empty(*output_clone, input->clone()->set_tensor_shape(output_shape));
Abe Mbise7784c832018-05-31 16:48:41 +0100396 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
397
Giorgio Arena26b22162018-08-13 15:49:49 +0100398 TensorInfo permuted_output;
399 if(input->data_layout() == DataLayout::NHWC)
400 {
401 permute(output_shape, PermutationVector(1U, 2U, 0U));
Georgios Pinitas10490202018-08-17 17:16:06 +0100402 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 +0100403 output_to_use = &permuted_output;
404 }
405
Abe Mbise7784c832018-05-31 16:48:41 +0100406 // Im2Col configuration
Giorgio Arena26b22162018-08-13 15:49:49 +0100407 TensorShape shape_im2col = input_to_use->tensor_shape();
Abe Mbise7784c832018-05-31 16:48:41 +0100408 shape_im2col.set(0, patch_size);
409 shape_im2col.set(1, conv_size);
410 shape_im2col.set(2, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100411 TensorInfo input_reshaped(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col).set_data_layout(DataLayout::NCHW));
412 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 +0100413
414 // Weights reshape configuration
415 const TensorShape shape_weights_reshape(patch_size, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100416 TensorInfo weights_reshaped(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW));
417 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseWeightsReshapeKernel::validate(weights_to_use, &weights_reshaped, append_bias ? biases : nullptr));
Abe Mbise7784c832018-05-31 16:48:41 +0100418
419 // GEMV configuration
420 DataType v2mm_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type();
Giorgio Arena26b22162018-08-13 15:49:49 +0100421 TensorShape shape_v2mm_out = input_to_use->tensor_shape();
Abe Mbise7784c832018-05-31 16:48:41 +0100422 shape_v2mm_out.set(0, conv_size * weights_z);
423 shape_v2mm_out.set(1, 1);
424 shape_v2mm_out.set(2, 1);
Giorgio Arena26b22162018-08-13 15:49:49 +0100425 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 +0100426 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixVectorMultiplyKernel::validate(&input_reshaped, &weights_reshaped, &v2mm_output));
427
Giorgio Arena26b22162018-08-13 15:49:49 +0100428 TensorInfo output_reshaped(v2mm_output.clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_to_use->tensor_shape()));
429 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 +0100430
431 if(is_quantized)
432 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100433 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayerOutputStageKernel::validate(&output_reshaped, biases, output_to_use));
Abe Mbise7784c832018-05-31 16:48:41 +0100434 }
435
436 return Status{};
437}
438
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000439void NEDepthwiseConvolutionLayer::run()
Michalis Spyroub7b31532017-11-23 12:10:21 +0000440{
Georgios Pinitas72219332018-06-05 14:56:06 +0100441 prepare();
Georgios Pinitas1562be32018-03-08 19:09:19 +0000442
Giorgio Arena26b22162018-08-13 15:49:49 +0100443 if(_is_nhwc)
444 {
445 _permute_input.run();
446 }
447
Michalis Spyroub7b31532017-11-23 12:10:21 +0000448 NEScheduler::get().schedule(&_im2col_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000449 NEScheduler::get().schedule(&_v2mm_input_fill_border, Window::DimX);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000450 NEScheduler::get().schedule(&_v2mm_kernel, Window::DimX);
451 NEScheduler::get().schedule(&_vector_to_tensor_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000452 if(_is_quantized)
453 {
454 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
455 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100456
457 if(_is_nhwc)
458 {
459 _permute_output.run();
460 }
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000461}
Georgios Pinitas72219332018-06-05 14:56:06 +0100462
463void NEDepthwiseConvolutionLayer::prepare()
464{
465 if(!_is_prepared)
466 {
467 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
468
Giorgio Arena26b22162018-08-13 15:49:49 +0100469 if(_is_nhwc)
470 {
471 _permute_weights.run();
472 }
473
Georgios Pinitas72219332018-06-05 14:56:06 +0100474 // Run reshape and mark original weights as unused
475 _weights_reshaped.allocator()->allocate();
476 NEScheduler::get().schedule(&_weights_reshape_kernel, Window::DimX);
477 NEScheduler::get().schedule(&_v2mm_weights_fill_border, Window::DimX);
478 _original_weights->mark_as_unused();
479
480 _is_prepared = true;
481 }
482}