blob: bd688cffb64d984248c2040aa864c8c83647caab [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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/NEConvolutionLayer.h"
25
26#include "arm_compute/core/PixelValue.h"
27#include "arm_compute/core/Utils.h"
28#include "arm_compute/core/Validate.h"
29#include "arm_compute/runtime/NEON/NEScheduler.h"
30
31#include <cmath>
32#include <tuple>
33
34using namespace arm_compute;
35
36NEConvolutionLayerReshapeWeights::NEConvolutionLayerReshapeWeights()
37 : _weights_reshape_kernel(), _weights_transposed_kernel(), _weights_reshaped(), _transpose1xW(false)
38{
39}
40
41void NEConvolutionLayerReshapeWeights::configure(const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose1xW)
42{
43 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QS8, DataType::F32);
44 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QS8, DataType::F32);
45 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output);
46 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(weights, output);
47 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4);
48
49 if(biases != nullptr)
50 {
51 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::QS8, DataType::F32);
52 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
53 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(weights, biases);
54 ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3));
55 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
56 }
57
58 // Check if bias are present, if yes they will be embedded to the weights matrix
59 const bool _has_bias = (biases != nullptr);
60
61 _transpose1xW = transpose1xW;
62
63 if(transpose1xW)
64 {
65 // Create tensor to store the reshaped weights
66 const unsigned int mat_weights_cols = weights->info()->dimension(3);
67 const unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + (_has_bias ? 1 : 0);
68 TensorShape shape_wr(mat_weights_cols, mat_weights_rows);
69 TensorInfo info_wr(shape_wr, 1, weights->info()->data_type(), weights->info()->fixed_point_position());
70
71 _weights_reshaped.allocator()->init(info_wr);
72 _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped);
73 _weights_transposed_kernel.configure(&_weights_reshaped, output);
74 _weights_reshaped.allocator()->allocate();
75 }
76 else
77 {
78 _weights_reshape_kernel.configure(weights, biases, output);
79 }
80}
81
82void NEConvolutionLayerReshapeWeights::run()
83{
84 NEScheduler::get().schedule(&_weights_reshape_kernel, 3);
85 if(_transpose1xW)
86 {
87 NEScheduler::get().schedule(&_weights_transposed_kernel, Window::DimY);
88 }
89}
90
91NEConvolutionLayer::NEConvolutionLayer()
92 : _input_im2col_kernel(), _input_interleave_kernel(), _reshape_weights(), _mm_kernel(), _output_col2im_kernel(), _input_im2col_reshaped(), _input_interleaved_reshaped(), _weights_reshaped(),
93 _gemm_output(), _has_bias(false), _is_fully_connected_convolution(false), _are_weights_reshaped(false)
94{
95}
96
97void NEConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info)
98{
99 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::F32);
100 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QS8, DataType::F32);
101 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QS8, DataType::F32);
102 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
103 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights, output);
104 ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && weights->info()->dimension(2) != input->info()->dimension(2));
105 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4);
106
107 if(biases != nullptr)
108 {
109 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::QS8, DataType::F32);
110 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
111 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases);
112 ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && biases->info()->dimension(0) != weights->info()->dimension(3));
113 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
114 }
115
116 const DataType dt = input->info()->data_type();
117 const int fixed_point_position = input->info()->fixed_point_position();
118
119 _has_bias = (biases != nullptr);
120 _are_weights_reshaped = weights_info.are_reshaped();
121
122 // Get parameters from conv_info
123 unsigned int stride_x = 0;
124 unsigned int stride_y = 0;
125 unsigned int pad_x = 0;
126 unsigned int pad_y = 0;
127 std::tie(stride_x, stride_y) = conv_info.stride();
128 std::tie(pad_x, pad_y) = conv_info.pad();
129
130 // Get convolved dimensions
131 unsigned int conv_w = 0;
132 unsigned int conv_h = 0;
133
134 const unsigned int kernel_width = (_are_weights_reshaped) ? weights_info.kernel_size() : weights->info()->dimension(0);
135 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width,
136 stride_x, stride_y, pad_x, pad_y, conv_info.round());
137 ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(0) != conv_w) || (output->info()->dimension(1) != conv_h), "Output shape does not match the expected one");
138
139 // Check if its a "fully connected" convolution
140 _is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1));
141
142 unsigned int mat_weights_cols = weights->info()->dimension(3);
143 unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + (_has_bias ? 1 : 0);
144
145 // Reshape weights if needed
146 if(_are_weights_reshaped)
147 {
148 mat_weights_cols = output->info()->dimension(2);
149 const unsigned int quarter_reshaped_cols = weights->info()->dimension(0) / 4;
150 mat_weights_rows = (_has_bias ? 1 + quarter_reshaped_cols : quarter_reshaped_cols);
151 }
152 else
153 {
154 if(_is_fully_connected_convolution)
155 {
156 // Create tensor to store the reshaped weights
157 TensorShape shape_wr(mat_weights_cols, mat_weights_rows);
158 TensorInfo info_wr(shape_wr, 1, dt, fixed_point_position);
159 _weights_reshaped.allocator()->init(info_wr);
160 _reshape_weights.configure(weights, biases, &_weights_reshaped, false /* 1xW transpose */);
161 }
162 else
163 {
164 // Create tensor to store transposed weights
165 const float transpose_width = 16.0f / input->info()->element_size();
166 TensorShape shape_wt(mat_weights_rows * static_cast<unsigned int>(transpose_width), static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)));
167 TensorInfo info_wt(shape_wt, 1, dt, fixed_point_position);
168 _weights_reshaped.allocator()->init(info_wt);
169 _reshape_weights.configure(weights, biases, &_weights_reshaped, true /* 1xW transpose */);
170 }
171 weights = &_weights_reshaped;
172 }
173
174 // Create tensor to store im2col reshaped inputs
175 const unsigned int mat_input_cols = mat_weights_rows;
176 const unsigned int mat_input_rows = conv_w * conv_h;
177 TensorShape shape_im2col = input->info()->tensor_shape();
178 shape_im2col.set(0, mat_input_cols);
179 shape_im2col.set(1, mat_input_rows);
180 shape_im2col.set(2, 1);
181 _input_im2col_reshaped.allocator()->init(TensorInfo(shape_im2col, 1, dt, fixed_point_position));
182
183 // Create tensor (interleave) to prepare input tensor for GEMM
184 if(!_is_fully_connected_convolution)
185 {
186 TensorShape shape_interleaved = shape_im2col;
187 shape_interleaved.set(0, shape_interleaved.x() * 4);
188 shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f));
189 _input_interleaved_reshaped.allocator()->init(TensorInfo(shape_interleaved, 1, dt, fixed_point_position));
190 }
191
192 // Create GEMM output tensor
193 TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape();
194 shape_gemm.set(0, mat_weights_cols);
195 shape_gemm.set(1, mat_input_rows);
196 _gemm_output.allocator()->init(TensorInfo(shape_gemm, 1, dt, fixed_point_position));
197
198 // Configure kernels
199 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, std::make_pair(conv_w, conv_h), conv_info, _has_bias);
200 if(_is_fully_connected_convolution)
201 {
202 _mm_kernel.configure(&_input_im2col_reshaped, weights, &_gemm_output, 1.0f);
203 }
204 else
205 {
206 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
207 _mm_kernel.configure(&_input_interleaved_reshaped, weights, &_gemm_output, 1.0f);
208 }
209 _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h));
210
211 // Allocate intermediate tensor
212 if(!_are_weights_reshaped)
213 {
214 _weights_reshaped.allocator()->allocate();
215 }
216 _input_im2col_reshaped.allocator()->allocate();
217 if(!_is_fully_connected_convolution)
218 {
219 _input_interleaved_reshaped.allocator()->allocate();
220 }
221 _gemm_output.allocator()->allocate();
222}
223
224void NEConvolutionLayer::run()
225{
226 // Run weights reshaping (Runs once for every configure)
227 if(!_are_weights_reshaped)
228 {
229 _are_weights_reshaped = true;
230 _reshape_weights.run();
231 }
232
233 // Run input reshaping
234 NEScheduler::get().schedule(&_input_im2col_kernel, Window::DimY);
235 if(!_is_fully_connected_convolution)
236 {
237 // Run interleave
238 NEScheduler::get().schedule(&_input_interleave_kernel, Window::DimY);
239 }
240
241 // Runs matrix multiply on reshaped matrices
242 NEScheduler::get().schedule(&_mm_kernel, Window::DimY);
243
244 // Reshape output matrix
245 NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY);
246}