blob: 0a977ad08d2983b26ded5ce2c09e0ec54a11327f [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()
Anthony Barbier06ea0482018-02-22 15:45:35 +000039 : _dwc_kernel(), _output_stage_kernel(), _border_handler(), _permute_input(), _permute_weights(), _permute_output(), _accumulator(), _input_nhwc(), _weights_hwio(), _output_nhwc(), _has_bias(false),
Jenkinsb3a371b2018-05-23 11:36:53 +010040 _is_quantized(false), _is_optimized(false), _are_weights_reshaped(false), _is_nchw(true), _is_first_run(true)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000041{
42}
43
Jenkinsb3a371b2018-05-23 11:36:53 +010044void NEDepthwiseConvolutionLayer3x3::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000045{
Anthony Barbierf45d5a92018-01-24 16:23:15 +000046 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32);
47 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000048
Anthony Barbierf45d5a92018-01-24 16:23:15 +000049 PixelValue zero_value(0.f);
50
51 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
52 _has_bias = biases != nullptr;
Anthony Barbier06ea0482018-02-22 15:45:35 +000053 _is_optimized = NEDepthwiseConvolutionLayer3x3Kernel::is_optimized_execution_possible(input->info()->tensor_shape(),
54 conv_info,
Jenkinsb3a371b2018-05-23 11:36:53 +010055 input->info()->data_type(),
56 depth_multiplier,
57 input->info()->data_layout());
Anthony Barbier06ea0482018-02-22 15:45:35 +000058 _are_weights_reshaped = false;
Jenkinsb3a371b2018-05-23 11:36:53 +010059 _is_nchw = input->info()->data_layout() == DataLayout::NCHW;
60
61 ARM_COMPUTE_ERROR_ON(!_is_optimized && !_is_nchw);
Anthony Barbierf45d5a92018-01-24 16:23:15 +000062
Anthony Barbier06ea0482018-02-22 15:45:35 +000063 if(_is_optimized)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000064 {
Jenkinsb3a371b2018-05-23 11:36:53 +010065 if(_is_nchw)
66 {
67 // Configure the function to transform the input tensor from NCHW -> NHWC
68 _permute_input.configure(input, &_input_nhwc, PermutationVector(2U, 0U, 1U));
Anthony Barbier06ea0482018-02-22 15:45:35 +000069
Jenkinsb3a371b2018-05-23 11:36:53 +010070 // Configure the function to transform the weights tensor from IHW -> HWI
71 _permute_weights.configure(weights, &_weights_hwio, PermutationVector(2U, 0U, 1U));
Anthony Barbier06ea0482018-02-22 15:45:35 +000072
Jenkinsb3a371b2018-05-23 11:36:53 +010073 // Configure optimized depthwise
74 _dwc_kernel.configure(&_input_nhwc, &_weights_hwio, &_output_nhwc, conv_info, depth_multiplier, DataLayout::NHWC);
Anthony Barbier06ea0482018-02-22 15:45:35 +000075
Jenkinsb3a371b2018-05-23 11:36:53 +010076 // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
77 _permute_output.configure(&_output_nhwc, output, PermutationVector(1U, 2U, 0U));
Anthony Barbier06ea0482018-02-22 15:45:35 +000078
Jenkinsb3a371b2018-05-23 11:36:53 +010079 // Allocate tensors
80 _input_nhwc.allocator()->allocate();
81 _weights_hwio.allocator()->allocate();
82 _output_nhwc.allocator()->allocate();
83 }
84 else
85 {
86 _dwc_kernel.configure(input, weights, output, conv_info, depth_multiplier, DataLayout::NHWC);
87 }
Anthony Barbierf45d5a92018-01-24 16:23:15 +000088 }
Anthony Barbier06ea0482018-02-22 15:45:35 +000089 else
90 {
91 // Allocate the intermediate accumulator tensor in case of fixed point input
92 if(_is_quantized)
93 {
94 _accumulator.allocator()->init(TensorInfo(output->info()->tensor_shape(), 1, DataType::S32));
95 _accumulator.info()->set_quantization_info(input->info()->quantization_info());
96 zero_value = PixelValue(static_cast<uint32_t>(input->info()->quantization_info().offset));
97 }
Anthony Barbierf45d5a92018-01-24 16:23:15 +000098
Anthony Barbier06ea0482018-02-22 15:45:35 +000099 // Configure depthwise convolution kernel
Jenkinsb3a371b2018-05-23 11:36:53 +0100100 _dwc_kernel.configure(input, weights, (_is_quantized) ? &_accumulator : output, conv_info, depth_multiplier);
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000101
Anthony Barbier06ea0482018-02-22 15:45:35 +0000102 // Configure border handler
103 _border_handler.configure(input, _dwc_kernel.border_size(), BorderMode::CONSTANT, zero_value);
104 }
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000105
106 // Configure biases accumulation
107 if(_has_bias || _is_quantized)
108 {
109 if(_is_quantized)
110 {
Anthony Barbier06ea0482018-02-22 15:45:35 +0000111 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
112
113 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000114 int output_multiplier, output_shift;
115 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000116 _output_stage_kernel.configure(&_accumulator, biases, output, output_multiplier, output_shift, output_quant_info.offset);
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000117 _accumulator.allocator()->allocate();
118 }
119 else
120 {
121 _output_stage_kernel.configure(output, biases);
122 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000123 }
124}
125
126void NEDepthwiseConvolutionLayer3x3::run()
127{
Jenkinsb3a371b2018-05-23 11:36:53 +0100128 if(_is_first_run && _is_optimized)
129 {
130 _is_first_run = false;
131 // Create convolver (deferred)
132 _dwc_kernel.generate_convolver();
133 }
134
Anthony Barbier06ea0482018-02-22 15:45:35 +0000135 // Permute weights in HWIO format if the optimized kernel will be executedd
Jenkinsb3a371b2018-05-23 11:36:53 +0100136 if(!_are_weights_reshaped && _is_optimized && _is_nchw)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000137 {
138 _are_weights_reshaped = true;
139 _permute_weights.run();
140 }
141
142 // Handle input
143 if(_is_optimized)
144 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100145 if(_is_nchw)
146 {
147 // Permute input to NHWC format execution
148 _permute_input.run();
149 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000150 }
151 else
152 {
153 // Fill border in NCHW format execution
154 NEScheduler::get().schedule(&_border_handler, Window::DimX);
155 }
156
157 // Execute depthwise convolution
158 NEScheduler::get().schedule(&_dwc_kernel, Window::DimX);
159
160 // Permute output to ACL's native NCHW format in case of NHWC execution
Jenkinsb3a371b2018-05-23 11:36:53 +0100161 if(_is_optimized && _is_nchw)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000162 {
163 _permute_output.run();
164 }
165
166 // Add biases
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000167 if(_has_bias || _is_quantized)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000168 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000169 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000170 }
171}
172
173NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer()
Anthony Barbier06ea0482018-02-22 15:45:35 +0000174 : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _v2mm_input_fill_border(), _v2mm_weights_fill_border(), _input_reshaped(),
Jenkinsb3a371b2018-05-23 11:36:53 +0100175 _weights_reshaped(), _v2mm_output(), _output_reshaped(), _is_first_run(true), _is_quantized(false), _original_weights(nullptr)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000176{
177}
178
Jenkinsb3a371b2018-05-23 11:36:53 +0100179void NEDepthwiseConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000180{
Anthony Barbier06ea0482018-02-22 15:45:35 +0000181 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000182 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Jenkinsb3a371b2018-05-23 11:36:53 +0100183 ARM_COMPUTE_ERROR_ON((input->info()->dimension(2) * depth_multiplier) != weights->info()->dimension(2));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000184
185 const size_t weights_w = weights->info()->dimension(0);
186 const size_t weights_h = weights->info()->dimension(1);
187 const size_t weights_z = weights->info()->dimension(2);
188
Jenkinsb3a371b2018-05-23 11:36:53 +0100189 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
190 _is_first_run = true;
191 _original_weights = weights;
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000192
Anthony Barbier06ea0482018-02-22 15:45:35 +0000193 // Should bias be appended ?
194 bool append_bias = (biases != nullptr) && !_is_quantized;
195
196 // Calculate output shape
Jenkinsb3a371b2018-05-23 11:36:53 +0100197 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier);
198
199 // Output auto inizialitation if not yet initialized
200 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
201 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000202
203 // Output width and height
Jenkinsb3a371b2018-05-23 11:36:53 +0100204 const unsigned int conv_w = output_shape.x();
205 const unsigned int conv_h = output_shape.y();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000206
207 // Set up intermediate tensors
Anthony Barbier06ea0482018-02-22 15:45:35 +0000208 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000209 const size_t conv_size = conv_w * conv_h;
210
211 // Im2Col configuration
212 TensorShape shape_im2col = input->info()->tensor_shape();
213 shape_im2col.set(0, patch_size);
214 shape_im2col.set(1, conv_size);
215 shape_im2col.set(2, weights_z);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000216 _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
Jenkinsb3a371b2018-05-23 11:36:53 +0100217 _im2col_kernel.configure(input, &_input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000218
219 // Weights reshape configuration
220 const TensorShape shape_weights_reshape(patch_size, weights_z);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000221 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape));
222 _weights_reshape_kernel.configure(weights, &_weights_reshaped, append_bias ? biases : nullptr);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000223
224 // GEMV configuration
Anthony Barbier06ea0482018-02-22 15:45:35 +0000225 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000226 TensorShape shape_v2mm_out = input->info()->tensor_shape();
227 shape_v2mm_out.set(0, conv_size * weights_z);
228 shape_v2mm_out.set(1, 1);
229 shape_v2mm_out.set(2, 1);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000230 _v2mm_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000231 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
Jenkinsb3a371b2018-05-23 11:36:53 +0100232 _output_reshaped.allocator()->init(_v2mm_output.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000233 _vector_to_tensor_kernel.configure(&_v2mm_output, (_is_quantized) ? &_output_reshaped : output, conv_w, conv_h);
234
235 // Output staged configuration
236 if(_is_quantized)
237 {
238 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
239
240 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
241 int output_multiplier, output_shift;
242 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
243 _output_stage_kernel.configure(&_output_reshaped, biases, output, output_multiplier, output_shift, output_quant_info.offset);
244 _output_reshaped.allocator()->allocate();
245 }
246
247 // Fill borders on inputs
248 PixelValue zero_in(static_cast<int32_t>(0));
249 PixelValue zero_w(static_cast<int32_t>(0));
250 if(_is_quantized)
251 {
252 zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset));
253 zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset));
254 }
255 BorderSize border_size = _v2mm_kernel.border_size();
256 _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in);
257
258 border_size.bottom = 0;
259 _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000260
261 // Allocate intermediate tensors
262 _input_reshaped.allocator()->allocate();
263 _weights_reshaped.allocator()->allocate();
264 _v2mm_output.allocator()->allocate();
265}
266
267void NEDepthwiseConvolutionLayer::run()
268{
Jenkinsb3a371b2018-05-23 11:36:53 +0100269 // Run weights reshaping (Runs once for every configure)
270 if(_is_first_run)
271 {
272 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
273
274 NEScheduler::get().schedule(&_weights_reshape_kernel, Window::DimX);
275 NEScheduler::get().schedule(&_v2mm_weights_fill_border, Window::DimX);
276 _is_first_run = false;
277
278 // Mark original weights tensor as unused
279 _original_weights->mark_as_unused();
280 }
281
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000282 NEScheduler::get().schedule(&_im2col_kernel, Window::DimX);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000283 NEScheduler::get().schedule(&_v2mm_input_fill_border, Window::DimX);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000284 NEScheduler::get().schedule(&_v2mm_kernel, Window::DimX);
285 NEScheduler::get().schedule(&_vector_to_tensor_kernel, Window::DimX);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000286 if(_is_quantized)
287 {
288 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
289 }
290}