blob: 1f3e5d1192c61015b54aa97779a5aa5bae593464 [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;
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010036
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000037NEDepthwiseConvolutionLayer3x3::NEDepthwiseConvolutionLayer3x3()
Georgios Pinitasf72f9362018-01-12 16:29:45 +000038 : _kernel(), _output_stage_kernel(), _border_handler(), _accumulator(), _has_bias(false), _is_quantized(false)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010039{
40}
41
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000042void NEDepthwiseConvolutionLayer3x3::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010043{
Georgios Pinitasf72f9362018-01-12 16:29:45 +000044 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32);
Georgios Pinitas1250a5a2018-01-02 13:27:37 +000045 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010046
Georgios Pinitasf72f9362018-01-12 16:29:45 +000047 PixelValue zero_value(0.f);
48
49 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
50 _has_bias = biases != nullptr;
51
52 // Allocate the intermediate accumulator tensor in case of fixed point input
53 if(_is_quantized)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010054 {
Georgios Pinitasf72f9362018-01-12 16:29:45 +000055 _accumulator.allocator()->init(TensorInfo(output->info()->tensor_shape(), 1, DataType::S32));
56 _accumulator.info()->set_quantization_info(input->info()->quantization_info());
57 zero_value = PixelValue(static_cast<uint32_t>(input->info()->quantization_info().offset));
58 }
59
60 // Configure depthwise convolution kernel
61 _kernel.configure(input, weights, (_is_quantized) ? &_accumulator : output, conv_info);
62
63 // Configure border handler
64 _border_handler.configure(input, _kernel.border_size(), BorderMode::CONSTANT, zero_value);
65
66 // Configure biases accumulation
67 if(_has_bias || _is_quantized)
68 {
69 if(_is_quantized)
70 {
71 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output->info()->quantization_info().scale;
72 int output_multiplier, output_shift;
73 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
74 _output_stage_kernel.configure(&_accumulator, biases, output, output_multiplier, output_shift, output->info()->quantization_info().offset);
75 _accumulator.allocator()->allocate();
76 }
77 else
78 {
79 _output_stage_kernel.configure(output, biases);
80 }
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010081 }
82}
83
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000084void NEDepthwiseConvolutionLayer3x3::run()
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010085{
86 NEScheduler::get().schedule(&_border_handler, Window::DimX);
87 NEScheduler::get().schedule(&_kernel, Window::DimX);
Georgios Pinitasf72f9362018-01-12 16:29:45 +000088 if(_has_bias || _is_quantized)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010089 {
Michalis Spyroub91e34c2017-12-20 15:50:55 +000090 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010091 }
Michalis Spyroub7b31532017-11-23 12:10:21 +000092}
93
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000094NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer()
Georgios Pinitasd05dce42018-01-22 16:29:17 +000095 : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _v2mm_input_fill_border(), _v2mm_weights_fill_border(), _input_reshaped(),
96 _weights_reshaped(), _v2mm_output(), _output_reshaped(), _is_quantized(false)
Michalis Spyroub7b31532017-11-23 12:10:21 +000097{
98}
99
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000100void NEDepthwiseConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000101{
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000102 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000103 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
104 ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != weights->info()->dimension(2));
105
106 const size_t weights_w = weights->info()->dimension(0);
107 const size_t weights_h = weights->info()->dimension(1);
108 const size_t weights_z = weights->info()->dimension(2);
109
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000110 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Michalis Spyroub7b31532017-11-23 12:10:21 +0000111
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000112 // Should bias be appended ?
113 bool append_bias = (biases != nullptr) && !_is_quantized;
114
115 // Calculate output shape
116 TensorShape dwc_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info);
117
118 // Output width and height
119 const unsigned int conv_w = dwc_output_shape.x();
120 const unsigned int conv_h = dwc_output_shape.y();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000121
122 // Set up intermediate tensors
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000123 const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000124 const size_t conv_size = conv_w * conv_h;
125
126 // Im2Col configuration
127 TensorShape shape_im2col = input->info()->tensor_shape();
128 shape_im2col.set(0, patch_size);
129 shape_im2col.set(1, conv_size);
130 shape_im2col.set(2, weights_z);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000131 _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
132 _im2col_kernel.configure(input, &_input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000133
134 // Weights reshape configuration
135 const TensorShape shape_weights_reshape(patch_size, weights_z);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000136 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape));
137 _weights_reshape_kernel.configure(weights, &_weights_reshaped, append_bias ? biases : nullptr);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000138
139 // GEMV configuration
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000140 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
Michalis Spyroub7b31532017-11-23 12:10:21 +0000141 TensorShape shape_v2mm_out = input->info()->tensor_shape();
142 shape_v2mm_out.set(0, conv_size * weights_z);
143 shape_v2mm_out.set(1, 1);
144 shape_v2mm_out.set(2, 1);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000145 _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 +0000146 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000147 _output_reshaped.allocator()->init(_v2mm_output.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(dwc_output_shape));
148 _vector_to_tensor_kernel.configure(&_v2mm_output, (_is_quantized) ? &_output_reshaped : output, conv_w, conv_h);
149
150 // Output staged configuration
151 if(_is_quantized)
152 {
153 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output->info()->quantization_info().scale;
154 int output_multiplier, output_shift;
155 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
156 _output_stage_kernel.configure(&_output_reshaped, biases, output, output_multiplier, output_shift, output->info()->quantization_info().offset);
157 _output_reshaped.allocator()->allocate();
158 }
159
160 // Fill borders on inputs
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000161 PixelValue zero_in(static_cast<int32_t>(0));
162 PixelValue zero_w(static_cast<int32_t>(0));
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000163 if(_is_quantized)
164 {
165 zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset));
166 zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset));
167 }
168 BorderSize border_size = _v2mm_kernel.border_size();
169 _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in);
170
171 border_size.bottom = 0;
172 _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000173
174 // Allocate intermediate tensors
175 _input_reshaped.allocator()->allocate();
176 _weights_reshaped.allocator()->allocate();
177 _v2mm_output.allocator()->allocate();
178}
179
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000180void NEDepthwiseConvolutionLayer::run()
Michalis Spyroub7b31532017-11-23 12:10:21 +0000181{
182 NEScheduler::get().schedule(&_im2col_kernel, Window::DimX);
183 NEScheduler::get().schedule(&_weights_reshape_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000184 NEScheduler::get().schedule(&_v2mm_input_fill_border, Window::DimX);
185 NEScheduler::get().schedule(&_v2mm_weights_fill_border, Window::DimX);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000186 NEScheduler::get().schedule(&_v2mm_kernel, Window::DimX);
187 NEScheduler::get().schedule(&_vector_to_tensor_kernel, Window::DimX);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000188 if(_is_quantized)
189 {
190 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
191 }
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000192}