blob: 2d08b4521091ad6bbaed972297428cc0780e8806 [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 Barbierf45d5a92018-01-24 16:23:15 +000029#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000030#include "arm_compute/runtime/NEON/NEScheduler.h"
31#include "support/ToolchainSupport.h"
32
33using namespace arm_compute;
34
35NEDepthwiseConvolutionLayer3x3::NEDepthwiseConvolutionLayer3x3()
Anthony Barbierf45d5a92018-01-24 16:23:15 +000036 : _kernel(), _output_stage_kernel(), _border_handler(), _accumulator(), _has_bias(false), _is_quantized(false)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000037{
38}
39
40void NEDepthwiseConvolutionLayer3x3::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info)
41{
Anthony Barbierf45d5a92018-01-24 16:23:15 +000042 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32);
43 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000044
Anthony Barbierf45d5a92018-01-24 16:23:15 +000045 PixelValue zero_value(0.f);
46
47 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
48 _has_bias = biases != nullptr;
49
50 // Allocate the intermediate accumulator tensor in case of fixed point input
51 if(_is_quantized)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000052 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +000053 _accumulator.allocator()->init(TensorInfo(output->info()->tensor_shape(), 1, DataType::S32));
54 _accumulator.info()->set_quantization_info(input->info()->quantization_info());
55 zero_value = PixelValue(static_cast<uint32_t>(input->info()->quantization_info().offset));
56 }
57
58 // Configure depthwise convolution kernel
59 _kernel.configure(input, weights, (_is_quantized) ? &_accumulator : output, conv_info);
60
61 // Configure border handler
62 _border_handler.configure(input, _kernel.border_size(), BorderMode::CONSTANT, zero_value);
63
64 // Configure biases accumulation
65 if(_has_bias || _is_quantized)
66 {
67 if(_is_quantized)
68 {
69 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output->info()->quantization_info().scale;
70 int output_multiplier, output_shift;
71 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
72 _output_stage_kernel.configure(&_accumulator, biases, output, output_multiplier, output_shift, output->info()->quantization_info().offset);
73 _accumulator.allocator()->allocate();
74 }
75 else
76 {
77 _output_stage_kernel.configure(output, biases);
78 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +000079 }
80}
81
82void NEDepthwiseConvolutionLayer3x3::run()
83{
84 NEScheduler::get().schedule(&_border_handler, Window::DimX);
85 NEScheduler::get().schedule(&_kernel, Window::DimX);
Anthony Barbierf45d5a92018-01-24 16:23:15 +000086 if(_has_bias || _is_quantized)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000087 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +000088 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000089 }
90}
91
92NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer()
93 : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _input_reshaped(), _weights_reshaped(), _v2mm_output()
94{
95}
96
97void NEDepthwiseConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info)
98{
99 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
100 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
101 ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != weights->info()->dimension(2));
102
103 const size_t weights_w = weights->info()->dimension(0);
104 const size_t weights_h = weights->info()->dimension(1);
105 const size_t weights_z = weights->info()->dimension(2);
106
107 bool has_bias = (biases != nullptr);
108
109 unsigned int conv_w = 0;
110 unsigned int conv_h = 0;
111 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), weights_w, weights_h, conv_info);
112
113 // Set up intermediate tensors
114 const size_t patch_size = weights_w * weights_h + ((has_bias) ? 1 : 0);
115 const size_t conv_size = conv_w * conv_h;
116
117 // Im2Col configuration
118 TensorShape shape_im2col = input->info()->tensor_shape();
119 shape_im2col.set(0, patch_size);
120 shape_im2col.set(1, conv_size);
121 shape_im2col.set(2, weights_z);
122 const TensorInfo info_im2col(shape_im2col, 1, input->info()->data_type(), input->info()->fixed_point_position());
123 _input_reshaped.allocator()->init(info_im2col);
124 _im2col_kernel.configure(input, &_input_reshaped, Size2D(weights_w, weights_h), conv_info, has_bias);
125
126 // Weights reshape configuration
127 const TensorShape shape_weights_reshape(patch_size, weights_z);
128 const TensorInfo info_weights_reshape(shape_weights_reshape, 1, weights->info()->data_type(), weights->info()->fixed_point_position());
129 _weights_reshaped.allocator()->init(info_weights_reshape);
130 _weights_reshape_kernel.configure(weights, &_weights_reshaped, biases);
131
132 // GEMV configuration
133 TensorShape shape_v2mm_out = input->info()->tensor_shape();
134 shape_v2mm_out.set(0, conv_size * weights_z);
135 shape_v2mm_out.set(1, 1);
136 shape_v2mm_out.set(2, 1);
137 const TensorInfo info_v2mm_out(shape_v2mm_out, 1, input->info()->data_type(), input->info()->fixed_point_position());
138 _v2mm_output.allocator()->init(info_v2mm_out);
139 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
140 _vector_to_tensor_kernel.configure(&_v2mm_output, output, conv_w, conv_h);
141
142 // Allocate intermediate tensors
143 _input_reshaped.allocator()->allocate();
144 _weights_reshaped.allocator()->allocate();
145 _v2mm_output.allocator()->allocate();
146}
147
148void NEDepthwiseConvolutionLayer::run()
149{
150 NEScheduler::get().schedule(&_im2col_kernel, Window::DimX);
151 NEScheduler::get().schedule(&_weights_reshape_kernel, Window::DimX);
152 NEScheduler::get().schedule(&_v2mm_kernel, Window::DimX);
153 NEScheduler::get().schedule(&_vector_to_tensor_kernel, Window::DimX);
154}