blob: 95fcf8805eeb78c30b7de04dacd17c3baa78a451 [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),
40 _is_quantized(false), _is_optimized(false), _are_weights_reshaped(false)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000041{
42}
43
44void NEDepthwiseConvolutionLayer3x3::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info)
45{
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,
55 input->info()->data_type());
56 _are_weights_reshaped = false;
Anthony Barbierf45d5a92018-01-24 16:23:15 +000057
Anthony Barbier06ea0482018-02-22 15:45:35 +000058 if(_is_optimized)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000059 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000060 // Configure the function to transform the input tensor from NCHW -> NHWC
61 _permute_input.configure(input, &_input_nhwc, PermutationVector(2U, 0U, 1U));
62
63 // Configure the function to transform the weights tensor from IHW -> HWI
64 _permute_weights.configure(weights, &_weights_hwio, PermutationVector(2U, 0U, 1U));
65
66 // Configure optimized depthwise
67 _dwc_kernel.configure(&_input_nhwc, &_weights_hwio, &_output_nhwc, conv_info, DataLayout::NHWC);
68
69 // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
70 _permute_output.configure(&_output_nhwc, output, PermutationVector(1U, 2U, 0U));
71
72 // Allocate tensors
73 _input_nhwc.allocator()->allocate();
74 _weights_hwio.allocator()->allocate();
75 _output_nhwc.allocator()->allocate();
76
77 // Create convolver (deferred)
78 _dwc_kernel.generate_convolver();
Anthony Barbierf45d5a92018-01-24 16:23:15 +000079 }
Anthony Barbier06ea0482018-02-22 15:45:35 +000080 else
81 {
82 // Allocate the intermediate accumulator tensor in case of fixed point input
83 if(_is_quantized)
84 {
85 _accumulator.allocator()->init(TensorInfo(output->info()->tensor_shape(), 1, DataType::S32));
86 _accumulator.info()->set_quantization_info(input->info()->quantization_info());
87 zero_value = PixelValue(static_cast<uint32_t>(input->info()->quantization_info().offset));
88 }
Anthony Barbierf45d5a92018-01-24 16:23:15 +000089
Anthony Barbier06ea0482018-02-22 15:45:35 +000090 // Configure depthwise convolution kernel
91 _dwc_kernel.configure(input, weights, (_is_quantized) ? &_accumulator : output, conv_info);
Anthony Barbierf45d5a92018-01-24 16:23:15 +000092
Anthony Barbier06ea0482018-02-22 15:45:35 +000093 // Configure border handler
94 _border_handler.configure(input, _dwc_kernel.border_size(), BorderMode::CONSTANT, zero_value);
95 }
Anthony Barbierf45d5a92018-01-24 16:23:15 +000096
97 // Configure biases accumulation
98 if(_has_bias || _is_quantized)
99 {
100 if(_is_quantized)
101 {
Anthony Barbier06ea0482018-02-22 15:45:35 +0000102 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
103
104 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000105 int output_multiplier, output_shift;
106 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000107 _output_stage_kernel.configure(&_accumulator, biases, output, output_multiplier, output_shift, output_quant_info.offset);
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000108 _accumulator.allocator()->allocate();
109 }
110 else
111 {
112 _output_stage_kernel.configure(output, biases);
113 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000114 }
115}
116
117void NEDepthwiseConvolutionLayer3x3::run()
118{
Anthony Barbier06ea0482018-02-22 15:45:35 +0000119 // Permute weights in HWIO format if the optimized kernel will be executedd
120 if(!_are_weights_reshaped && _is_optimized)
121 {
122 _are_weights_reshaped = true;
123 _permute_weights.run();
124 }
125
126 // Handle input
127 if(_is_optimized)
128 {
129 // Permute input to NHWC format execution
130 _permute_input.run();
131 }
132 else
133 {
134 // Fill border in NCHW format execution
135 NEScheduler::get().schedule(&_border_handler, Window::DimX);
136 }
137
138 // Execute depthwise convolution
139 NEScheduler::get().schedule(&_dwc_kernel, Window::DimX);
140
141 // Permute output to ACL's native NCHW format in case of NHWC execution
142 if(_is_optimized)
143 {
144 _permute_output.run();
145 }
146
147 // Add biases
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000148 if(_has_bias || _is_quantized)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000149 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000150 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000151 }
152}
153
154NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer()
Anthony Barbier06ea0482018-02-22 15:45:35 +0000155 : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _v2mm_input_fill_border(), _v2mm_weights_fill_border(), _input_reshaped(),
156 _weights_reshaped(), _v2mm_output(), _output_reshaped(), _is_quantized(false)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000157{
158}
159
160void NEDepthwiseConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info)
161{
Anthony Barbier06ea0482018-02-22 15:45:35 +0000162 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000163 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
164 ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != weights->info()->dimension(2));
165
166 const size_t weights_w = weights->info()->dimension(0);
167 const size_t weights_h = weights->info()->dimension(1);
168 const size_t weights_z = weights->info()->dimension(2);
169
Anthony Barbier06ea0482018-02-22 15:45:35 +0000170 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000171
Anthony Barbier06ea0482018-02-22 15:45:35 +0000172 // Should bias be appended ?
173 bool append_bias = (biases != nullptr) && !_is_quantized;
174
175 // Calculate output shape
176 TensorShape dwc_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info);
177
178 // Output width and height
179 const unsigned int conv_w = dwc_output_shape.x();
180 const unsigned int conv_h = dwc_output_shape.y();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000181
182 // Set up intermediate tensors
Anthony Barbier06ea0482018-02-22 15:45:35 +0000183 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000184 const size_t conv_size = conv_w * conv_h;
185
186 // Im2Col configuration
187 TensorShape shape_im2col = input->info()->tensor_shape();
188 shape_im2col.set(0, patch_size);
189 shape_im2col.set(1, conv_size);
190 shape_im2col.set(2, weights_z);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000191 _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
192 _im2col_kernel.configure(input, &_input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000193
194 // Weights reshape configuration
195 const TensorShape shape_weights_reshape(patch_size, weights_z);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000196 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape));
197 _weights_reshape_kernel.configure(weights, &_weights_reshaped, append_bias ? biases : nullptr);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000198
199 // GEMV configuration
Anthony Barbier06ea0482018-02-22 15:45:35 +0000200 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000201 TensorShape shape_v2mm_out = input->info()->tensor_shape();
202 shape_v2mm_out.set(0, conv_size * weights_z);
203 shape_v2mm_out.set(1, 1);
204 shape_v2mm_out.set(2, 1);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000205 _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 +0000206 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000207 _output_reshaped.allocator()->init(_v2mm_output.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(dwc_output_shape));
208 _vector_to_tensor_kernel.configure(&_v2mm_output, (_is_quantized) ? &_output_reshaped : output, conv_w, conv_h);
209
210 // Output staged configuration
211 if(_is_quantized)
212 {
213 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
214
215 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
216 int output_multiplier, output_shift;
217 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
218 _output_stage_kernel.configure(&_output_reshaped, biases, output, output_multiplier, output_shift, output_quant_info.offset);
219 _output_reshaped.allocator()->allocate();
220 }
221
222 // Fill borders on inputs
223 PixelValue zero_in(static_cast<int32_t>(0));
224 PixelValue zero_w(static_cast<int32_t>(0));
225 if(_is_quantized)
226 {
227 zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset));
228 zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset));
229 }
230 BorderSize border_size = _v2mm_kernel.border_size();
231 _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in);
232
233 border_size.bottom = 0;
234 _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000235
236 // Allocate intermediate tensors
237 _input_reshaped.allocator()->allocate();
238 _weights_reshaped.allocator()->allocate();
239 _v2mm_output.allocator()->allocate();
240}
241
242void NEDepthwiseConvolutionLayer::run()
243{
244 NEScheduler::get().schedule(&_im2col_kernel, Window::DimX);
245 NEScheduler::get().schedule(&_weights_reshape_kernel, Window::DimX);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000246 NEScheduler::get().schedule(&_v2mm_input_fill_border, Window::DimX);
247 NEScheduler::get().schedule(&_v2mm_weights_fill_border, Window::DimX);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000248 NEScheduler::get().schedule(&_v2mm_kernel, Window::DimX);
249 NEScheduler::get().schedule(&_vector_to_tensor_kernel, Window::DimX);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000250 if(_is_quantized)
251 {
252 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
253 }
254}