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