blob: a2f0094f9d6a0dc6e5a7867cb53fd911c43f3a49 [file] [log] [blame]
Anthony Barbier8140e1e2017-12-14 23:48:46 +00001/*
Anthony Barbierf45d5a92018-01-24 16:23:15 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier8140e1e2017-12-14 23:48:46 +00003 *
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 */
24#include "arm_compute/runtime/NEON/functions/NEDepthwiseConvolutionLayer.h"
25
26#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/ITensor.h"
28#include "arm_compute/core/PixelValue.h"
Anthony Barbier06ea0482018-02-22 15:45:35 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbierf45d5a92018-01-24 16:23:15 +000030#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000031#include "arm_compute/runtime/NEON/NEScheduler.h"
32#include "support/ToolchainSupport.h"
33
34using namespace arm_compute;
Anthony Barbier06ea0482018-02-22 15:45:35 +000035using namespace arm_compute::misc;
36using namespace arm_compute::misc::shape_calculator;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000037
38NEDepthwiseConvolutionLayer3x3::NEDepthwiseConvolutionLayer3x3()
Jenkinsb9abeae2018-11-22 11:58:08 +000039 : _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)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000042{
43}
44
Jenkinsb9abeae2018-11-22 11:58:08 +000045void 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)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000047{
Jenkinsb9abeae2018-11-22 11:58:08 +000048 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Anthony Barbierf45d5a92018-01-24 16:23:15 +000049 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000050
Anthony Barbierf45d5a92018-01-24 16:23:15 +000051 PixelValue zero_value(0.f);
52
53 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
54 _has_bias = biases != nullptr;
Anthony Barbier06ea0482018-02-22 15:45:35 +000055 _is_optimized = NEDepthwiseConvolutionLayer3x3Kernel::is_optimized_execution_possible(input->info()->tensor_shape(),
56 conv_info,
Jenkinsb3a371b2018-05-23 11:36:53 +010057 input->info()->data_type(),
58 depth_multiplier,
59 input->info()->data_layout());
Anthony Barbier06ea0482018-02-22 15:45:35 +000060 _are_weights_reshaped = false;
Jenkinsb3a371b2018-05-23 11:36:53 +010061 _is_nchw = input->info()->data_layout() == DataLayout::NCHW;
Jenkins52ba29e2018-08-29 15:32:11 +000062 _permute = _is_optimized == _is_nchw;
Anthony Barbierf45d5a92018-01-24 16:23:15 +000063
Jenkinsb9abeae2018-11-22 11:58:08 +000064 // 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
Anthony Barbier06ea0482018-02-22 15:45:35 +000080 if(_is_optimized)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000081 {
Jenkinsb9abeae2018-11-22 11:58:08 +000082 ITensor *optimized_output = (_is_quantized) ? &_accumulator : output;
Jenkinsb3a371b2018-05-23 11:36:53 +010083 if(_is_nchw)
84 {
85 // Configure the function to transform the input tensor from NCHW -> NHWC
Jenkins52ba29e2018-08-29 15:32:11 +000086 _permute_input.configure(input, &_permuted_input, PermutationVector(2U, 0U, 1U));
87 _permuted_input.info()->set_data_layout(DataLayout::NHWC);
Anthony Barbier06ea0482018-02-22 15:45:35 +000088
Jenkinsb3a371b2018-05-23 11:36:53 +010089 // Configure the function to transform the weights tensor from IHW -> HWI
Jenkins52ba29e2018-08-29 15:32:11 +000090 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(2U, 0U, 1U));
91 _permuted_weights.info()->set_data_layout(DataLayout::NHWC);
Anthony Barbier06ea0482018-02-22 15:45:35 +000092
Jenkinsb3a371b2018-05-23 11:36:53 +010093 // Configure optimized depthwise
Jenkins52ba29e2018-08-29 15:32:11 +000094 _dwc_kernel.configure(&_permuted_input, &_permuted_weights, &_permuted_output, conv_info, depth_multiplier, DataLayout::NHWC);
Anthony Barbier06ea0482018-02-22 15:45:35 +000095
Jenkinsb3a371b2018-05-23 11:36:53 +010096 // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
Jenkinsb9abeae2018-11-22 11:58:08 +000097 _permuted_output.info()->set_data_layout(DataLayout::NHWC);
98 _permute_output.configure(&_permuted_output, optimized_output, PermutationVector(1U, 2U, 0U));
Anthony Barbier06ea0482018-02-22 15:45:35 +000099
Jenkinsb3a371b2018-05-23 11:36:53 +0100100 // Allocate tensors
Jenkins52ba29e2018-08-29 15:32:11 +0000101 _permuted_input.allocator()->allocate();
102 _permuted_weights.allocator()->allocate();
103 _permuted_output.allocator()->allocate();
Jenkinsb3a371b2018-05-23 11:36:53 +0100104 }
105 else
106 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000107 _dwc_kernel.configure(input, weights, optimized_output, conv_info, depth_multiplier, DataLayout::NHWC);
Jenkinsb3a371b2018-05-23 11:36:53 +0100108 }
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000109 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000110 else
111 {
Jenkins52ba29e2018-08-29 15:32:11 +0000112 if(!_is_nchw)
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000113 {
Jenkins52ba29e2018-08-29 15:32:11 +0000114 // 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);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000117
Jenkins52ba29e2018-08-29 15:32:11 +0000118 // 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();
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000131 }
132 else
133 {
Jenkins52ba29e2018-08-29 15:32:11 +0000134 // 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);
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000139 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000140 }
Jenkins52ba29e2018-08-29 15:32:11 +0000141
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);
Jenkinsb9abeae2018-11-22 11:58:08 +0000150 _output_stage_kernel.configure(&_accumulator, biases, (_is_nchw || _is_optimized) ? output : &_permuted_output, output_multiplier, output_shift, output_quant_info.offset);
Jenkins52ba29e2018-08-29 15:32:11 +0000151 _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 }
Jenkinsb9abeae2018-11-22 11:58:08 +0000164
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 }
Jenkins52ba29e2018-08-29 15:32:11 +0000172}
173
174Status NEDepthwiseConvolutionLayer3x3::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Jenkinsb9abeae2018-11-22 11:58:08 +0000175 unsigned int depth_multiplier, const ActivationLayerInfo &act_info)
Jenkins52ba29e2018-08-29 15:32:11 +0000176{
177 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Jenkinsb9abeae2018-11-22 11:58:08 +0000178 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Jenkins52ba29e2018-08-29 15:32:11 +0000179
180 if(biases != nullptr)
181 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000182 const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
Jenkins52ba29e2018-08-29 15:32:11 +0000183 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
Jenkinsb9abeae2018-11-22 11:58:08 +0000184 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(channel_idx));
Jenkins52ba29e2018-08-29 15:32:11 +0000185 }
186
Jenkinsb9abeae2018-11-22 11:58:08 +0000187 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
197 //Validate Activation Layer
198 if(act_info.enabled())
199 {
200 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
201 }
202
203 return Status{};
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000204}
205
206void NEDepthwiseConvolutionLayer3x3::run()
207{
Jenkinsb3a371b2018-05-23 11:36:53 +0100208 if(_is_first_run && _is_optimized)
209 {
210 _is_first_run = false;
211 // Create convolver (deferred)
212 _dwc_kernel.generate_convolver();
213 }
214
Jenkins52ba29e2018-08-29 15:32:11 +0000215 // Permute weights
216 if(_permute)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000217 {
Jenkins52ba29e2018-08-29 15:32:11 +0000218 if(!_are_weights_reshaped)
219 {
220 _are_weights_reshaped = true;
221 _permute_weights.run();
222 }
223
224 _permute_input.run();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000225 }
226
227 // Handle input
Jenkins52ba29e2018-08-29 15:32:11 +0000228 if(!_is_optimized)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000229 {
Jenkins52ba29e2018-08-29 15:32:11 +0000230 // Fill border
Anthony Barbier06ea0482018-02-22 15:45:35 +0000231 NEScheduler::get().schedule(&_border_handler, Window::DimX);
232 }
233
234 // Execute depthwise convolution
235 NEScheduler::get().schedule(&_dwc_kernel, Window::DimX);
236
Jenkins52ba29e2018-08-29 15:32:11 +0000237 // Permute output
Jenkinsb3a371b2018-05-23 11:36:53 +0100238 if(_is_optimized && _is_nchw)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000239 {
240 _permute_output.run();
241 }
242
243 // Add biases
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000244 if(_has_bias || _is_quantized)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000245 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000246 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000247 }
Jenkins52ba29e2018-08-29 15:32:11 +0000248
249 // Permute output
250 if(!_is_optimized && !_is_nchw)
251 {
252 _permute_output.run();
253 }
Jenkinsb9abeae2018-11-22 11:58:08 +0000254
255 if(_is_activationlayer_enabled)
256 {
257 _activationlayer_function.run();
258 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000259}
260
261NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer()
Jenkins52ba29e2018-08-29 15:32:11 +0000262 : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _v2mm_input_fill_border(), _v2mm_weights_fill_border(), _permute_input(),
Jenkinsb9abeae2018-11-22 11:58:08 +0000263 _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)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000265{
266}
267
Jenkinsb9abeae2018-11-22 11:58:08 +0000268void 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)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000270{
Jenkins52ba29e2018-08-29 15:32:11 +0000271 const unsigned int channel_idx = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL);
272 ARM_COMPUTE_UNUSED(channel_idx);
273
Anthony Barbier06ea0482018-02-22 15:45:35 +0000274 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000275 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Jenkins52ba29e2018-08-29 15:32:11 +0000276 ARM_COMPUTE_ERROR_ON((input->info()->dimension(channel_idx) * depth_multiplier) != weights->info()->dimension(channel_idx));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000277
Jenkins52ba29e2018-08-29 15:32:11 +0000278 _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);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000298
Jenkinsb3a371b2018-05-23 11:36:53 +0100299 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Jenkins52ba29e2018-08-29 15:32:11 +0000300 _is_prepared = false;
301 _original_weights = weights_to_use;
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000302
Anthony Barbier06ea0482018-02-22 15:45:35 +0000303 // Should bias be appended ?
304 bool append_bias = (biases != nullptr) && !_is_quantized;
305
306 // Calculate output shape
Jenkinsb3a371b2018-05-23 11:36:53 +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);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000312
Jenkins52ba29e2018-08-29 15:32:11 +0000313 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
Anthony Barbier06ea0482018-02-22 15:45:35 +0000321 // Output width and height
Jenkinsb3a371b2018-05-23 11:36:53 +0100322 const unsigned int conv_w = output_shape.x();
323 const unsigned int conv_h = output_shape.y();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000324
325 // Set up intermediate tensors
Anthony Barbier06ea0482018-02-22 15:45:35 +0000326 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000327 const size_t conv_size = conv_w * conv_h;
328
329 // Im2Col configuration
Jenkins52ba29e2018-08-29 15:32:11 +0000330 TensorShape shape_im2col = input_to_use->info()->tensor_shape();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000331 shape_im2col.set(0, patch_size);
332 shape_im2col.set(1, conv_size);
333 shape_im2col.set(2, weights_z);
Jenkins52ba29e2018-08-29 15:32:11 +0000334 _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);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000336
337 // Weights reshape configuration
338 const TensorShape shape_weights_reshape(patch_size, weights_z);
Jenkins52ba29e2018-08-29 15:32:11 +0000339 _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);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000341
342 // GEMV configuration
Anthony Barbier06ea0482018-02-22 15:45:35 +0000343 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
Jenkins52ba29e2018-08-29 15:32:11 +0000344 TensorShape shape_v2mm_out = input_to_use->info()->tensor_shape();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000345 shape_v2mm_out.set(0, conv_size * weights_z);
346 shape_v2mm_out.set(1, 1);
347 shape_v2mm_out.set(2, 1);
Jenkins52ba29e2018-08-29 15:32:11 +0000348 _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));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000349 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
Jenkinsb3a371b2018-05-23 11:36:53 +0100350 _output_reshaped.allocator()->init(_v2mm_output.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
Jenkins52ba29e2018-08-29 15:32:11 +0000351 _vector_to_tensor_kernel.configure(&_v2mm_output, (_is_quantized) ? &_output_reshaped : output_to_use, conv_w, conv_h);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000352
353 // Output staged configuration
354 if(_is_quantized)
355 {
Jenkins52ba29e2018-08-29 15:32:11 +0000356 const QuantizationInfo output_quant_info = output->info()->quantization_info();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000357
358 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
359 int output_multiplier, output_shift;
360 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Jenkins52ba29e2018-08-29 15:32:11 +0000361 _output_stage_kernel.configure(&_output_reshaped, biases, output_to_use, output_multiplier, output_shift, output_quant_info.offset);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000362 _output_reshaped.allocator()->allocate();
363 }
364
Jenkins52ba29e2018-08-29 15:32:11 +0000365 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
Anthony Barbier06ea0482018-02-22 15:45:35 +0000374 // Fill borders on inputs
375 PixelValue zero_in(static_cast<int32_t>(0));
376 PixelValue zero_w(static_cast<int32_t>(0));
377 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);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000387
388 // Allocate intermediate tensors
389 _input_reshaped.allocator()->allocate();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000390 _v2mm_output.allocator()->allocate();
Jenkinsb9abeae2018-11-22 11:58:08 +0000391
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 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000399}
400
Jenkins52ba29e2018-08-29 15:32:11 +0000401Status NEDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Jenkinsb9abeae2018-11-22 11:58:08 +0000402 unsigned int depth_multiplier, const ActivationLayerInfo &act_info)
Jenkins52ba29e2018-08-29 15:32:11 +0000403{
404 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Jenkinsb9abeae2018-11-22 11:58:08 +0000405 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);
Jenkins52ba29e2018-08-29 15:32:11 +0000409
410 // Clone output to use auto init
411 auto output_clone = output->clone();
412
413 const ITensorInfo *input_to_use = input;
414 const ITensorInfo *weights_to_use = weights;
415 const ITensorInfo *output_to_use = output_clone.get();
416
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
434 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
435 const bool append_bias = (biases != nullptr) && !is_quantized;
436 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);
Jenkinsb9abeae2018-11-22 11:58:08 +0000440 const unsigned int conv_w = output_shape[width_idx];
441 const unsigned int conv_h = output_shape[height_idx];
Jenkins52ba29e2018-08-29 15:32:11 +0000442 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
443 const size_t conv_size = conv_w * conv_h;
444
445 // Output auto inizialitation if not yet initialized
446 auto_init_if_empty(*output_clone, input->clone()->set_tensor_shape(output_shape));
447 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
448
449 TensorInfo permuted_output;
450 if(input->data_layout() == DataLayout::NHWC)
451 {
452 permute(output_shape, PermutationVector(1U, 2U, 0U));
453 permuted_output = TensorInfo(output_clone->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape).set_data_layout(DataLayout::NCHW));
454 output_to_use = &permuted_output;
455 }
456
457 // Im2Col configuration
458 TensorShape shape_im2col = input_to_use->tensor_shape();
459 shape_im2col.set(0, patch_size);
460 shape_im2col.set(1, conv_size);
461 shape_im2col.set(2, weights_z);
462 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));
464
465 // Weights reshape configuration
466 const TensorShape shape_weights_reshape(patch_size, weights_z);
467 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));
469
470 // GEMV configuration
471 DataType v2mm_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type();
472 TensorShape shape_v2mm_out = input_to_use->tensor_shape();
473 shape_v2mm_out.set(0, conv_size * weights_z);
474 shape_v2mm_out.set(1, 1);
475 shape_v2mm_out.set(2, 1);
476 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));
477 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixVectorMultiplyKernel::validate(&input_reshaped, &weights_reshaped, &v2mm_output));
478
479 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));
481
482 if(is_quantized)
483 {
484 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayerOutputStageKernel::validate(&output_reshaped, biases, output_to_use));
485 }
486
Jenkinsb9abeae2018-11-22 11:58:08 +0000487 // Validate Activation Layer
488 if(act_info.enabled())
489 {
490 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
491 }
492
Jenkins52ba29e2018-08-29 15:32:11 +0000493 return Status{};
494}
495
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000496void NEDepthwiseConvolutionLayer::run()
497{
Jenkins52ba29e2018-08-29 15:32:11 +0000498 prepare();
499
500 if(_is_nhwc)
Jenkinsb3a371b2018-05-23 11:36:53 +0100501 {
Jenkins52ba29e2018-08-29 15:32:11 +0000502 _permute_input.run();
Jenkinsb3a371b2018-05-23 11:36:53 +0100503 }
504
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000505 NEScheduler::get().schedule(&_im2col_kernel, Window::DimX);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000506 NEScheduler::get().schedule(&_v2mm_input_fill_border, Window::DimX);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000507 NEScheduler::get().schedule(&_v2mm_kernel, Window::DimX);
508 NEScheduler::get().schedule(&_vector_to_tensor_kernel, Window::DimX);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000509 if(_is_quantized)
510 {
511 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
512 }
Jenkins52ba29e2018-08-29 15:32:11 +0000513
514 if(_is_nhwc)
515 {
516 _permute_output.run();
517 }
Jenkinsb9abeae2018-11-22 11:58:08 +0000518
519 if(_is_activationlayer_enabled)
520 {
521 _activationlayer_function.run();
522 }
Jenkins52ba29e2018-08-29 15:32:11 +0000523}
524
525void NEDepthwiseConvolutionLayer::prepare()
526{
527 if(!_is_prepared)
528 {
529 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
530
531 if(_is_nhwc)
532 {
533 _permute_weights.run();
534 }
535
536 // 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 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000544}