blob: a2f0094f9d6a0dc6e5a7867cb53fd911c43f3a49 [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()
Georgios Pinitas60e98252018-10-22 16:17:20 +010039 : _dwc_kernel(), _output_stage_kernel(), _border_handler(), _permute_input(), _permute_weights(), _permute_output(), _activationlayer_function(), _accumulator(), _permuted_input(),
40 _permuted_weights(), _permuted_output(), _has_bias(false), _is_quantized(false), _is_optimized(false), _are_weights_reshaped(false), _is_nchw(true), _is_first_run(true), _permute(false),
41 _is_activationlayer_enabled(false)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010042{
43}
44
Georgios Pinitas60e98252018-10-22 16:17:20 +010045void NEDepthwiseConvolutionLayer3x3::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
46 unsigned int depth_multiplier, const ActivationLayerInfo &act_info)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010047{
Georgios Pinitas20c246a2018-09-12 16:45:53 +010048 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Georgios Pinitas1250a5a2018-01-02 13:27:37 +000049 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010050
Georgios Pinitasf72f9362018-01-12 16:29:45 +000051 PixelValue zero_value(0.f);
52
53 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
54 _has_bias = biases != nullptr;
Georgios Pinitas4074c992018-01-30 18:13:46 +000055 _is_optimized = NEDepthwiseConvolutionLayer3x3Kernel::is_optimized_execution_possible(input->info()->tensor_shape(),
56 conv_info,
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010057 input->info()->data_type(),
Giorgio Arena76572242018-04-04 17:44:26 +010058 depth_multiplier,
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010059 input->info()->data_layout());
Georgios Pinitas4074c992018-01-30 18:13:46 +000060 _are_weights_reshaped = false;
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010061 _is_nchw = input->info()->data_layout() == DataLayout::NCHW;
Giorgio Arena26b22162018-08-13 15:49:49 +010062 _permute = _is_optimized == _is_nchw;
Georgios Pinitasf72f9362018-01-12 16:29:45 +000063
Georgios Pinitasa799ce02018-09-12 20:11:34 +010064 // Initialize the intermediate accumulator tensor in case of quantized input
65 if(_is_quantized)
66 {
67 TensorShape accum_shape = output->info()->tensor_shape();
68 DataLayout accum_layout = output->info()->data_layout();
69 if(!_is_optimized && !_is_nchw)
70 {
71 permute(accum_shape, PermutationVector(1U, 2U, 0U));
72 accum_layout = DataLayout::NCHW;
73 }
74
75 _accumulator.allocator()->init(TensorInfo(accum_shape, 1, DataType::S32, input->info()->quantization_info()));
76 _accumulator.info()->set_data_layout(accum_layout);
77 zero_value = PixelValue(static_cast<uint32_t>(input->info()->quantization_info().offset));
78 }
79
Georgios Pinitas4074c992018-01-30 18:13:46 +000080 if(_is_optimized)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010081 {
Georgios Pinitasa799ce02018-09-12 20:11:34 +010082 ITensor *optimized_output = (_is_quantized) ? &_accumulator : output;
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010083 if(_is_nchw)
84 {
85 // Configure the function to transform the input tensor from NCHW -> NHWC
Giorgio Arena26b22162018-08-13 15:49:49 +010086 _permute_input.configure(input, &_permuted_input, PermutationVector(2U, 0U, 1U));
87 _permuted_input.info()->set_data_layout(DataLayout::NHWC);
Georgios Pinitas4074c992018-01-30 18:13:46 +000088
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010089 // Configure the function to transform the weights tensor from IHW -> HWI
Giorgio Arena26b22162018-08-13 15:49:49 +010090 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(2U, 0U, 1U));
91 _permuted_weights.info()->set_data_layout(DataLayout::NHWC);
Georgios Pinitas4074c992018-01-30 18:13:46 +000092
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010093 // Configure optimized depthwise
Giorgio Arena26b22162018-08-13 15:49:49 +010094 _dwc_kernel.configure(&_permuted_input, &_permuted_weights, &_permuted_output, conv_info, depth_multiplier, DataLayout::NHWC);
Georgios Pinitas4074c992018-01-30 18:13:46 +000095
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010096 // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
Georgios Pinitasa799ce02018-09-12 20:11:34 +010097 _permuted_output.info()->set_data_layout(DataLayout::NHWC);
98 _permute_output.configure(&_permuted_output, optimized_output, PermutationVector(1U, 2U, 0U));
Georgios Pinitas4074c992018-01-30 18:13:46 +000099
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100100 // Allocate tensors
Giorgio Arena26b22162018-08-13 15:49:49 +0100101 _permuted_input.allocator()->allocate();
102 _permuted_weights.allocator()->allocate();
103 _permuted_output.allocator()->allocate();
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100104 }
105 else
106 {
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100107 _dwc_kernel.configure(input, weights, optimized_output, conv_info, depth_multiplier, DataLayout::NHWC);
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100108 }
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000109 }
Georgios Pinitas4074c992018-01-30 18:13:46 +0000110 else
111 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100112 if(!_is_nchw)
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000113 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100114 // Configure the function to transform the input tensor from NHWC -> NCHW
115 _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
116 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000117
Giorgio Arena26b22162018-08-13 15:49:49 +0100118 // Configure the function to transform the weights tensor from HWI -> IHW
119 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
120 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
121
122 // Configure optimized depthwise
123 _dwc_kernel.configure(&_permuted_input, &_permuted_weights, (_is_quantized) ? &_accumulator : &_permuted_output, conv_info, depth_multiplier);
124
125 // Configure border handler
126 _border_handler.configure(&_permuted_input, _dwc_kernel.border_size(), BorderMode::CONSTANT, zero_value);
127
128 // Allocate tensors
129 _permuted_input.allocator()->allocate();
130 _permuted_weights.allocator()->allocate();
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000131 }
132 else
133 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100134 // Configure depthwise convolution kernel
135 _dwc_kernel.configure(input, weights, (_is_quantized) ? &_accumulator : output, conv_info, depth_multiplier);
136
137 // Configure border handler
138 _border_handler.configure(input, _dwc_kernel.border_size(), BorderMode::CONSTANT, zero_value);
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000139 }
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100140 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100141
142 // Configure biases accumulation
143 if(_is_quantized)
144 {
145 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
146
147 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
148 int output_multiplier, output_shift;
149 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100150 _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 +0100151 _accumulator.allocator()->allocate();
152 }
153 else if(_has_bias)
154 {
155 _output_stage_kernel.configure((_is_nchw || _is_optimized) ? output : &_permuted_output, biases);
156 }
157
158 if(!_is_optimized && !_is_nchw)
159 {
160 // Configure the function to transform the convoluted output to NHWC
161 _permute_output.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
162 _permuted_output.allocator()->allocate();
163 }
Georgios Pinitas60e98252018-10-22 16:17:20 +0100164
165 //Configure Activation Layer
166 _is_activationlayer_enabled = act_info.enabled();
167
168 if(_is_activationlayer_enabled)
169 {
170 _activationlayer_function.configure(output, nullptr, act_info);
171 }
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100172}
173
Abe Mbise7784c832018-05-31 16:48:41 +0100174Status NEDepthwiseConvolutionLayer3x3::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Georgios Pinitas60e98252018-10-22 16:17:20 +0100175 unsigned int depth_multiplier, const ActivationLayerInfo &act_info)
Abe Mbise7784c832018-05-31 16:48:41 +0100176{
177 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100178 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Abe Mbise7784c832018-05-31 16:48:41 +0100179
Giorgio Arena66cbafb2018-08-23 14:51:00 +0100180 if(biases != nullptr)
181 {
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100182 const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
Giorgio Arena66cbafb2018-08-23 14:51:00 +0100183 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100184 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(channel_idx));
Giorgio Arena66cbafb2018-08-23 14:51:00 +0100185 }
186
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100187 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
188 TensorInfo accumulator = TensorInfo(output->clone()->set_is_resizable(true).reset_padding().set_data_type(DataType::S32));
189
190 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseConvolutionLayer3x3Kernel::validate(input, weights, is_quantized ? &accumulator : output, conv_info, depth_multiplier));
191
192 if(is_quantized)
193 {
194 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayerOutputStageKernel::validate(&accumulator, biases, output));
195 }
196
Georgios Pinitas60e98252018-10-22 16:17:20 +0100197 //Validate Activation Layer
198 if(act_info.enabled())
199 {
200 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
201 }
202
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100203 return Status{};
Abe Mbise7784c832018-05-31 16:48:41 +0100204}
205
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000206void NEDepthwiseConvolutionLayer3x3::run()
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100207{
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100208 if(_is_first_run && _is_optimized)
209 {
210 _is_first_run = false;
211 // Create convolver (deferred)
212 _dwc_kernel.generate_convolver();
213 }
214
Giorgio Arena26b22162018-08-13 15:49:49 +0100215 // Permute weights
216 if(_permute)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000217 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100218 if(!_are_weights_reshaped)
219 {
220 _are_weights_reshaped = true;
221 _permute_weights.run();
222 }
223
224 _permute_input.run();
Georgios Pinitas4074c992018-01-30 18:13:46 +0000225 }
226
227 // Handle input
Giorgio Arena26b22162018-08-13 15:49:49 +0100228 if(!_is_optimized)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000229 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100230 // Fill border
Georgios Pinitas4074c992018-01-30 18:13:46 +0000231 NEScheduler::get().schedule(&_border_handler, Window::DimX);
232 }
233
234 // Execute depthwise convolution
235 NEScheduler::get().schedule(&_dwc_kernel, Window::DimX);
236
Giorgio Arena26b22162018-08-13 15:49:49 +0100237 // Permute output
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100238 if(_is_optimized && _is_nchw)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000239 {
240 _permute_output.run();
241 }
242
243 // Add biases
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000244 if(_has_bias || _is_quantized)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100245 {
Michalis Spyroub91e34c2017-12-20 15:50:55 +0000246 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100247 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100248
249 // Permute output
250 if(!_is_optimized && !_is_nchw)
251 {
252 _permute_output.run();
253 }
Georgios Pinitas60e98252018-10-22 16:17:20 +0100254
255 if(_is_activationlayer_enabled)
256 {
257 _activationlayer_function.run();
258 }
Michalis Spyroub7b31532017-11-23 12:10:21 +0000259}
260
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000261NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer()
Giorgio Arena26b22162018-08-13 15:49:49 +0100262 : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _v2mm_input_fill_border(), _v2mm_weights_fill_border(), _permute_input(),
Georgios Pinitas60e98252018-10-22 16:17:20 +0100263 _permute_weights(), _permute_output(), _activationlayer_function(), _input_reshaped(), _weights_reshaped(), _v2mm_output(), _output_reshaped(), _permuted_input(), _permuted_weights(),
264 _permuted_output(), _is_prepared(false), _is_quantized(false), _is_nhwc(false), _is_activationlayer_enabled(false), _original_weights(nullptr)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000265{
266}
267
Georgios Pinitas60e98252018-10-22 16:17:20 +0100268void NEDepthwiseConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
269 unsigned int depth_multiplier, const ActivationLayerInfo &act_info)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000270{
Giorgio Arena26b22162018-08-13 15:49:49 +0100271 const unsigned int channel_idx = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL);
272 ARM_COMPUTE_UNUSED(channel_idx);
273
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000274 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000275 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Giorgio Arena26b22162018-08-13 15:49:49 +0100276 ARM_COMPUTE_ERROR_ON((input->info()->dimension(channel_idx) * depth_multiplier) != weights->info()->dimension(channel_idx));
Michalis Spyroub7b31532017-11-23 12:10:21 +0000277
Giorgio Arena26b22162018-08-13 15:49:49 +0100278 _is_nhwc = input->info()->data_layout() == DataLayout::NHWC;
279
280 ITensor *input_to_use = input;
281 const ITensor *weights_to_use = weights;
282 ITensor *output_to_use = output;
283
284 if(_is_nhwc)
285 {
286 _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
287 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
288 input_to_use = &_permuted_input;
289
290 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
291 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
292 weights_to_use = &_permuted_weights;
293 }
294
295 const size_t weights_w = weights_to_use->info()->dimension(0);
296 const size_t weights_h = weights_to_use->info()->dimension(1);
297 const size_t weights_z = weights_to_use->info()->dimension(2);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000298
Georgios Pinitas1562be32018-03-08 19:09:19 +0000299 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Georgios Pinitas72219332018-06-05 14:56:06 +0100300 _is_prepared = false;
Giorgio Arena26b22162018-08-13 15:49:49 +0100301 _original_weights = weights_to_use;
Michalis Spyroub7b31532017-11-23 12:10:21 +0000302
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000303 // Should bias be appended ?
304 bool append_bias = (biases != nullptr) && !_is_quantized;
305
306 // Calculate output shape
Giorgio Arena76572242018-04-04 17:44:26 +0100307 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier);
308
309 // Output auto inizialitation if not yet initialized
310 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
311 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000312
Giorgio Arena26b22162018-08-13 15:49:49 +0100313 if(_is_nhwc)
314 {
315 permute(output_shape, PermutationVector(1U, 2U, 0U));
316 _permuted_output.allocator()->init(output->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
317 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
318 output_to_use = &_permuted_output;
319 }
320
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000321 // Output width and height
Giorgio Arena76572242018-04-04 17:44:26 +0100322 const unsigned int conv_w = output_shape.x();
323 const unsigned int conv_h = output_shape.y();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000324
325 // Set up intermediate tensors
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000326 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000327 const size_t conv_size = conv_w * conv_h;
328
329 // Im2Col configuration
Giorgio Arena26b22162018-08-13 15:49:49 +0100330 TensorShape shape_im2col = input_to_use->info()->tensor_shape();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000331 shape_im2col.set(0, patch_size);
332 shape_im2col.set(1, conv_size);
333 shape_im2col.set(2, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100334 _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col).set_data_layout(DataLayout::NCHW));
335 _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 +0000336
337 // Weights reshape configuration
338 const TensorShape shape_weights_reshape(patch_size, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100339 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW));
340 _weights_reshape_kernel.configure(weights_to_use, &_weights_reshaped, append_bias ? biases : nullptr);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000341
342 // GEMV configuration
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000343 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
Giorgio Arena26b22162018-08-13 15:49:49 +0100344 TensorShape shape_v2mm_out = input_to_use->info()->tensor_shape();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000345 shape_v2mm_out.set(0, conv_size * weights_z);
346 shape_v2mm_out.set(1, 1);
347 shape_v2mm_out.set(2, 1);
Giorgio Arena26b22162018-08-13 15:49:49 +0100348 _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 +0000349 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
Giorgio Arena76572242018-04-04 17:44:26 +0100350 _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 +0100351 _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 +0000352
353 // Output staged configuration
354 if(_is_quantized)
355 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100356 const QuantizationInfo output_quant_info = output->info()->quantization_info();
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000357
358 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000359 int output_multiplier, output_shift;
360 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Giorgio Arena26b22162018-08-13 15:49:49 +0100361 _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 +0000362 _output_reshaped.allocator()->allocate();
363 }
364
Giorgio Arena26b22162018-08-13 15:49:49 +0100365 if(_is_nhwc)
366 {
367 _permute_output.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
368
369 _permuted_input.allocator()->allocate();
370 _permuted_weights.allocator()->allocate();
371 _permuted_output.allocator()->allocate();
372 }
373
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000374 // Fill borders on inputs
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000375 PixelValue zero_in(static_cast<int32_t>(0));
376 PixelValue zero_w(static_cast<int32_t>(0));
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000377 if(_is_quantized)
378 {
379 zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset));
380 zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset));
381 }
382 BorderSize border_size = _v2mm_kernel.border_size();
383 _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in);
384
385 border_size.bottom = 0;
386 _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000387
388 // Allocate intermediate tensors
389 _input_reshaped.allocator()->allocate();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000390 _v2mm_output.allocator()->allocate();
Georgios Pinitas60e98252018-10-22 16:17:20 +0100391
392 //Configure Activation Layer
393 _is_activationlayer_enabled = act_info.enabled();
394
395 if(_is_activationlayer_enabled)
396 {
397 _activationlayer_function.configure(output, nullptr, act_info);
398 }
Michalis Spyroub7b31532017-11-23 12:10:21 +0000399}
400
Georgios Pinitas10490202018-08-17 17:16:06 +0100401Status NEDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Georgios Pinitas60e98252018-10-22 16:17:20 +0100402 unsigned int depth_multiplier, const ActivationLayerInfo &act_info)
Abe Mbise7784c832018-05-31 16:48:41 +0100403{
404 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100405 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
406
407 const unsigned int width_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
408 const unsigned int height_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
Abe Mbise7784c832018-05-31 16:48:41 +0100409
Georgios Pinitas10490202018-08-17 17:16:06 +0100410 // Clone output to use auto init
411 auto output_clone = output->clone();
412
Giorgio Arena26b22162018-08-13 15:49:49 +0100413 const ITensorInfo *input_to_use = input;
414 const ITensorInfo *weights_to_use = weights;
Georgios Pinitas10490202018-08-17 17:16:06 +0100415 const ITensorInfo *output_to_use = output_clone.get();
Giorgio Arena26b22162018-08-13 15:49:49 +0100416
417 TensorShape permuted_input_shape = input->tensor_shape();
418 TensorShape permuted_weights_shape = weights->tensor_shape();
419 TensorInfo permuted_input;
420 TensorInfo permuted_weights;
421
422 if(input->data_layout() == DataLayout::NHWC)
423 {
424 permute(permuted_input_shape, PermutationVector(1U, 2U, 0U));
425 permute(permuted_weights_shape, PermutationVector(1U, 2U, 0U));
426
427 permuted_input = TensorInfo(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NCHW));
428 permuted_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NCHW));
429
430 input_to_use = &permuted_input;
431 weights_to_use = &permuted_weights;
432 }
433
Abe Mbise7784c832018-05-31 16:48:41 +0100434 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
435 const bool append_bias = (biases != nullptr) && !is_quantized;
Giorgio Arena26b22162018-08-13 15:49:49 +0100436 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier);
437 const size_t weights_w = weights_to_use->dimension(0);
438 const size_t weights_h = weights_to_use->dimension(1);
439 const size_t weights_z = weights_to_use->dimension(2);
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100440 const unsigned int conv_w = output_shape[width_idx];
441 const unsigned int conv_h = output_shape[height_idx];
Abe Mbise7784c832018-05-31 16:48:41 +0100442 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
443 const size_t conv_size = conv_w * conv_h;
444
Giorgio Arena26b22162018-08-13 15:49:49 +0100445 // Output auto inizialitation if not yet initialized
Georgios Pinitas10490202018-08-17 17:16:06 +0100446 auto_init_if_empty(*output_clone, input->clone()->set_tensor_shape(output_shape));
Abe Mbise7784c832018-05-31 16:48:41 +0100447 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
448
Giorgio Arena26b22162018-08-13 15:49:49 +0100449 TensorInfo permuted_output;
450 if(input->data_layout() == DataLayout::NHWC)
451 {
452 permute(output_shape, PermutationVector(1U, 2U, 0U));
Georgios Pinitas10490202018-08-17 17:16:06 +0100453 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 +0100454 output_to_use = &permuted_output;
455 }
456
Abe Mbise7784c832018-05-31 16:48:41 +0100457 // Im2Col configuration
Giorgio Arena26b22162018-08-13 15:49:49 +0100458 TensorShape shape_im2col = input_to_use->tensor_shape();
Abe Mbise7784c832018-05-31 16:48:41 +0100459 shape_im2col.set(0, patch_size);
460 shape_im2col.set(1, conv_size);
461 shape_im2col.set(2, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100462 TensorInfo input_reshaped(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col).set_data_layout(DataLayout::NCHW));
463 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 +0100464
465 // Weights reshape configuration
466 const TensorShape shape_weights_reshape(patch_size, weights_z);
Giorgio Arena26b22162018-08-13 15:49:49 +0100467 TensorInfo weights_reshaped(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW));
468 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseWeightsReshapeKernel::validate(weights_to_use, &weights_reshaped, append_bias ? biases : nullptr));
Abe Mbise7784c832018-05-31 16:48:41 +0100469
470 // GEMV configuration
471 DataType v2mm_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type();
Giorgio Arena26b22162018-08-13 15:49:49 +0100472 TensorShape shape_v2mm_out = input_to_use->tensor_shape();
Abe Mbise7784c832018-05-31 16:48:41 +0100473 shape_v2mm_out.set(0, conv_size * weights_z);
474 shape_v2mm_out.set(1, 1);
475 shape_v2mm_out.set(2, 1);
Giorgio Arena26b22162018-08-13 15:49:49 +0100476 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 +0100477 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixVectorMultiplyKernel::validate(&input_reshaped, &weights_reshaped, &v2mm_output));
478
Giorgio Arena26b22162018-08-13 15:49:49 +0100479 TensorInfo output_reshaped(v2mm_output.clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_to_use->tensor_shape()));
480 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 +0100481
482 if(is_quantized)
483 {
Giorgio Arena26b22162018-08-13 15:49:49 +0100484 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayerOutputStageKernel::validate(&output_reshaped, biases, output_to_use));
Abe Mbise7784c832018-05-31 16:48:41 +0100485 }
486
Georgios Pinitas60e98252018-10-22 16:17:20 +0100487 // Validate Activation Layer
488 if(act_info.enabled())
489 {
490 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
491 }
492
Abe Mbise7784c832018-05-31 16:48:41 +0100493 return Status{};
494}
495
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000496void NEDepthwiseConvolutionLayer::run()
Michalis Spyroub7b31532017-11-23 12:10:21 +0000497{
Georgios Pinitas72219332018-06-05 14:56:06 +0100498 prepare();
Georgios Pinitas1562be32018-03-08 19:09:19 +0000499
Giorgio Arena26b22162018-08-13 15:49:49 +0100500 if(_is_nhwc)
501 {
502 _permute_input.run();
503 }
504
Michalis Spyroub7b31532017-11-23 12:10:21 +0000505 NEScheduler::get().schedule(&_im2col_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000506 NEScheduler::get().schedule(&_v2mm_input_fill_border, Window::DimX);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000507 NEScheduler::get().schedule(&_v2mm_kernel, Window::DimX);
508 NEScheduler::get().schedule(&_vector_to_tensor_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000509 if(_is_quantized)
510 {
511 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
512 }
Giorgio Arena26b22162018-08-13 15:49:49 +0100513
514 if(_is_nhwc)
515 {
516 _permute_output.run();
517 }
Georgios Pinitas60e98252018-10-22 16:17:20 +0100518
519 if(_is_activationlayer_enabled)
520 {
521 _activationlayer_function.run();
522 }
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000523}
Georgios Pinitas72219332018-06-05 14:56:06 +0100524
525void NEDepthwiseConvolutionLayer::prepare()
526{
527 if(!_is_prepared)
528 {
529 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
530
Giorgio Arena26b22162018-08-13 15:49:49 +0100531 if(_is_nhwc)
532 {
533 _permute_weights.run();
534 }
535
Georgios Pinitas72219332018-06-05 14:56:06 +0100536 // Run reshape and mark original weights as unused
537 _weights_reshaped.allocator()->allocate();
538 NEScheduler::get().schedule(&_weights_reshape_kernel, Window::DimX);
539 NEScheduler::get().schedule(&_v2mm_weights_fill_border, Window::DimX);
540 _original_weights->mark_as_unused();
541
542 _is_prepared = true;
543 }
544}