blob: 8691fb9f7613f0c44f1bed8ee1ff5337a257fb22 [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 Pinitas4074c992018-01-30 18:13:46 +000039 : _dwc_kernel(), _output_stage_kernel(), _border_handler(), _permute_input(), _permute_weights(), _permute_output(), _accumulator(), _input_nhwc(), _weights_hwio(), _output_nhwc(), _has_bias(false),
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010040 _is_quantized(false), _is_optimized(false), _are_weights_reshaped(false), _is_nchw(true), _is_first_run(true)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010041{
42}
43
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000044void NEDepthwiseConvolutionLayer3x3::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010045{
Georgios Pinitasf72f9362018-01-12 16:29:45 +000046 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32);
Georgios Pinitas1250a5a2018-01-02 13:27:37 +000047 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010048
Georgios Pinitasf72f9362018-01-12 16:29:45 +000049 PixelValue zero_value(0.f);
50
51 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
52 _has_bias = biases != nullptr;
Georgios Pinitas4074c992018-01-30 18:13:46 +000053 _is_optimized = NEDepthwiseConvolutionLayer3x3Kernel::is_optimized_execution_possible(input->info()->tensor_shape(),
54 conv_info,
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010055 input->info()->data_type(),
56 input->info()->data_layout());
Georgios Pinitas4074c992018-01-30 18:13:46 +000057 _are_weights_reshaped = false;
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010058 _is_nchw = input->info()->data_layout() == DataLayout::NCHW;
59
60 ARM_COMPUTE_ERROR_ON(!_is_optimized && !_is_nchw);
Georgios Pinitasf72f9362018-01-12 16:29:45 +000061
Georgios Pinitas4074c992018-01-30 18:13:46 +000062 if(_is_optimized)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010063 {
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010064 if(_is_nchw)
65 {
66 // Configure the function to transform the input tensor from NCHW -> NHWC
67 _permute_input.configure(input, &_input_nhwc, PermutationVector(2U, 0U, 1U));
Georgios Pinitas4074c992018-01-30 18:13:46 +000068
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010069 // Configure the function to transform the weights tensor from IHW -> HWI
70 _permute_weights.configure(weights, &_weights_hwio, PermutationVector(2U, 0U, 1U));
Georgios Pinitas4074c992018-01-30 18:13:46 +000071
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010072 // Configure optimized depthwise
73 _dwc_kernel.configure(&_input_nhwc, &_weights_hwio, &_output_nhwc, conv_info, DataLayout::NHWC);
Georgios Pinitas4074c992018-01-30 18:13:46 +000074
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010075 // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
76 _permute_output.configure(&_output_nhwc, output, PermutationVector(1U, 2U, 0U));
Georgios Pinitas4074c992018-01-30 18:13:46 +000077
Giorgio Arena1ed1fc62018-03-26 16:20:05 +010078 // Allocate tensors
79 _input_nhwc.allocator()->allocate();
80 _weights_hwio.allocator()->allocate();
81 _output_nhwc.allocator()->allocate();
82 }
83 else
84 {
85 _dwc_kernel.configure(input, weights, output, conv_info, DataLayout::NHWC);
86 }
Georgios Pinitasf72f9362018-01-12 16:29:45 +000087 }
Georgios Pinitas4074c992018-01-30 18:13:46 +000088 else
89 {
90 // Allocate the intermediate accumulator tensor in case of fixed point input
91 if(_is_quantized)
92 {
93 _accumulator.allocator()->init(TensorInfo(output->info()->tensor_shape(), 1, DataType::S32));
94 _accumulator.info()->set_quantization_info(input->info()->quantization_info());
95 zero_value = PixelValue(static_cast<uint32_t>(input->info()->quantization_info().offset));
96 }
Georgios Pinitasf72f9362018-01-12 16:29:45 +000097
Georgios Pinitas4074c992018-01-30 18:13:46 +000098 // Configure depthwise convolution kernel
99 _dwc_kernel.configure(input, weights, (_is_quantized) ? &_accumulator : output, conv_info);
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000100
Georgios Pinitas4074c992018-01-30 18:13:46 +0000101 // Configure border handler
102 _border_handler.configure(input, _dwc_kernel.border_size(), BorderMode::CONSTANT, zero_value);
103 }
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000104
105 // Configure biases accumulation
106 if(_has_bias || _is_quantized)
107 {
108 if(_is_quantized)
109 {
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000110 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
111
112 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000113 int output_multiplier, output_shift;
114 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000115 _output_stage_kernel.configure(&_accumulator, biases, output, output_multiplier, output_shift, output_quant_info.offset);
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000116 _accumulator.allocator()->allocate();
117 }
118 else
119 {
120 _output_stage_kernel.configure(output, biases);
121 }
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100122 }
123}
124
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000125void NEDepthwiseConvolutionLayer3x3::run()
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100126{
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100127 if(_is_first_run && _is_optimized)
128 {
129 _is_first_run = false;
130 // Create convolver (deferred)
131 _dwc_kernel.generate_convolver();
132 }
133
Georgios Pinitas4074c992018-01-30 18:13:46 +0000134 // Permute weights in HWIO format if the optimized kernel will be executedd
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100135 if(!_are_weights_reshaped && _is_optimized && _is_nchw)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000136 {
137 _are_weights_reshaped = true;
138 _permute_weights.run();
139 }
140
141 // Handle input
142 if(_is_optimized)
143 {
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100144 if(_is_nchw)
145 {
146 // Permute input to NHWC format execution
147 _permute_input.run();
148 }
Georgios Pinitas4074c992018-01-30 18:13:46 +0000149 }
150 else
151 {
152 // Fill border in NCHW format execution
153 NEScheduler::get().schedule(&_border_handler, Window::DimX);
154 }
155
156 // Execute depthwise convolution
157 NEScheduler::get().schedule(&_dwc_kernel, Window::DimX);
158
159 // Permute output to ACL's native NCHW format in case of NHWC execution
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100160 if(_is_optimized && _is_nchw)
Georgios Pinitas4074c992018-01-30 18:13:46 +0000161 {
162 _permute_output.run();
163 }
164
165 // Add biases
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000166 if(_has_bias || _is_quantized)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100167 {
Michalis Spyroub91e34c2017-12-20 15:50:55 +0000168 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100169 }
Michalis Spyroub7b31532017-11-23 12:10:21 +0000170}
171
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000172NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer()
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000173 : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _v2mm_input_fill_border(), _v2mm_weights_fill_border(), _input_reshaped(),
Georgios Pinitas1562be32018-03-08 19:09:19 +0000174 _weights_reshaped(), _v2mm_output(), _output_reshaped(), _is_first_run(true), _is_quantized(false), _original_weights(nullptr)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000175{
176}
177
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000178void NEDepthwiseConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000179{
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000180 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000181 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
182 ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != weights->info()->dimension(2));
183
184 const size_t weights_w = weights->info()->dimension(0);
185 const size_t weights_h = weights->info()->dimension(1);
186 const size_t weights_z = weights->info()->dimension(2);
187
Georgios Pinitas1562be32018-03-08 19:09:19 +0000188 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
189 _is_first_run = true;
190 _original_weights = weights;
Michalis Spyroub7b31532017-11-23 12:10:21 +0000191
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000192 // Should bias be appended ?
193 bool append_bias = (biases != nullptr) && !_is_quantized;
194
195 // Calculate output shape
196 TensorShape dwc_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info);
197
198 // Output width and height
199 const unsigned int conv_w = dwc_output_shape.x();
200 const unsigned int conv_h = dwc_output_shape.y();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000201
202 // Set up intermediate tensors
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000203 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000204 const size_t conv_size = conv_w * conv_h;
205
206 // Im2Col configuration
207 TensorShape shape_im2col = input->info()->tensor_shape();
208 shape_im2col.set(0, patch_size);
209 shape_im2col.set(1, conv_size);
210 shape_im2col.set(2, weights_z);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000211 _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
212 _im2col_kernel.configure(input, &_input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000213
214 // Weights reshape configuration
215 const TensorShape shape_weights_reshape(patch_size, weights_z);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000216 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape));
217 _weights_reshape_kernel.configure(weights, &_weights_reshaped, append_bias ? biases : nullptr);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000218
219 // GEMV configuration
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000220 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000221 TensorShape shape_v2mm_out = input->info()->tensor_shape();
222 shape_v2mm_out.set(0, conv_size * weights_z);
223 shape_v2mm_out.set(1, 1);
224 shape_v2mm_out.set(2, 1);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000225 _v2mm_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out));
Michalis Spyroub7b31532017-11-23 12:10:21 +0000226 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000227 _output_reshaped.allocator()->init(_v2mm_output.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(dwc_output_shape));
228 _vector_to_tensor_kernel.configure(&_v2mm_output, (_is_quantized) ? &_output_reshaped : output, conv_w, conv_h);
229
230 // Output staged configuration
231 if(_is_quantized)
232 {
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000233 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
234
235 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000236 int output_multiplier, output_shift;
237 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000238 _output_stage_kernel.configure(&_output_reshaped, biases, output, output_multiplier, output_shift, output_quant_info.offset);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000239 _output_reshaped.allocator()->allocate();
240 }
241
242 // Fill borders on inputs
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000243 PixelValue zero_in(static_cast<int32_t>(0));
244 PixelValue zero_w(static_cast<int32_t>(0));
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000245 if(_is_quantized)
246 {
247 zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset));
248 zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset));
249 }
250 BorderSize border_size = _v2mm_kernel.border_size();
251 _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in);
252
253 border_size.bottom = 0;
254 _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000255
256 // Allocate intermediate tensors
257 _input_reshaped.allocator()->allocate();
258 _weights_reshaped.allocator()->allocate();
259 _v2mm_output.allocator()->allocate();
260}
261
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000262void NEDepthwiseConvolutionLayer::run()
Michalis Spyroub7b31532017-11-23 12:10:21 +0000263{
Georgios Pinitas1562be32018-03-08 19:09:19 +0000264 // Run weights reshaping (Runs once for every configure)
265 if(_is_first_run)
266 {
267 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
268
269 NEScheduler::get().schedule(&_weights_reshape_kernel, Window::DimX);
270 NEScheduler::get().schedule(&_v2mm_weights_fill_border, Window::DimX);
271 _is_first_run = false;
272
273 // Mark original weights tensor as unused
274 _original_weights->mark_as_unused();
275 }
276
Michalis Spyroub7b31532017-11-23 12:10:21 +0000277 NEScheduler::get().schedule(&_im2col_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000278 NEScheduler::get().schedule(&_v2mm_input_fill_border, Window::DimX);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000279 NEScheduler::get().schedule(&_v2mm_kernel, Window::DimX);
280 NEScheduler::get().schedule(&_vector_to_tensor_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000281 if(_is_quantized)
282 {
283 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
284 }
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000285}