blob: 1c87f60a29e5e932939d68126d1396569db600a9 [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);
Gian Marco Iodice559d7712017-08-08 08:38:09 +010099 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
100 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101 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
135 // Check if its a "fully connected" convolution
136 _is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1));
137
138 unsigned int mat_weights_cols = weights->info()->dimension(3);
139 unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + (_has_bias ? 1 : 0);
140
141 // Reshape weights if needed
142 if(_are_weights_reshaped)
143 {
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100144 mat_weights_cols = weights_info.num_kernels();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 const unsigned int quarter_reshaped_cols = weights->info()->dimension(0) / 4;
146 mat_weights_rows = (_has_bias ? 1 + quarter_reshaped_cols : quarter_reshaped_cols);
147 }
148 else
149 {
150 if(_is_fully_connected_convolution)
151 {
152 // Create tensor to store the reshaped weights
153 TensorShape shape_wr(mat_weights_cols, mat_weights_rows);
154 TensorInfo info_wr(shape_wr, 1, dt, fixed_point_position);
155 _weights_reshaped.allocator()->init(info_wr);
156 _reshape_weights.configure(weights, biases, &_weights_reshaped, false /* 1xW transpose */);
157 }
158 else
159 {
160 // Create tensor to store transposed weights
161 const float transpose_width = 16.0f / input->info()->element_size();
162 TensorShape shape_wt(mat_weights_rows * static_cast<unsigned int>(transpose_width), static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)));
163 TensorInfo info_wt(shape_wt, 1, dt, fixed_point_position);
164 _weights_reshaped.allocator()->init(info_wt);
165 _reshape_weights.configure(weights, biases, &_weights_reshaped, true /* 1xW transpose */);
166 }
167 weights = &_weights_reshaped;
168 }
169
170 // Create tensor to store im2col reshaped inputs
171 const unsigned int mat_input_cols = mat_weights_rows;
172 const unsigned int mat_input_rows = conv_w * conv_h;
173 TensorShape shape_im2col = input->info()->tensor_shape();
174 shape_im2col.set(0, mat_input_cols);
175 shape_im2col.set(1, mat_input_rows);
176 shape_im2col.set(2, 1);
177 _input_im2col_reshaped.allocator()->init(TensorInfo(shape_im2col, 1, dt, fixed_point_position));
178
179 // Create tensor (interleave) to prepare input tensor for GEMM
180 if(!_is_fully_connected_convolution)
181 {
182 TensorShape shape_interleaved = shape_im2col;
183 shape_interleaved.set(0, shape_interleaved.x() * 4);
184 shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f));
185 _input_interleaved_reshaped.allocator()->init(TensorInfo(shape_interleaved, 1, dt, fixed_point_position));
186 }
187
188 // Create GEMM output tensor
189 TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape();
190 shape_gemm.set(0, mat_weights_cols);
191 shape_gemm.set(1, mat_input_rows);
192 _gemm_output.allocator()->init(TensorInfo(shape_gemm, 1, dt, fixed_point_position));
193
194 // Configure kernels
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100195 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _has_bias);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196 if(_is_fully_connected_convolution)
197 {
198 _mm_kernel.configure(&_input_im2col_reshaped, weights, &_gemm_output, 1.0f);
199 }
200 else
201 {
202 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
203 _mm_kernel.configure(&_input_interleaved_reshaped, weights, &_gemm_output, 1.0f);
204 }
205 _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h));
206
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100207 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");
208
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209 // Allocate intermediate tensor
210 if(!_are_weights_reshaped)
211 {
212 _weights_reshaped.allocator()->allocate();
213 }
214 _input_im2col_reshaped.allocator()->allocate();
215 if(!_is_fully_connected_convolution)
216 {
217 _input_interleaved_reshaped.allocator()->allocate();
218 }
219 _gemm_output.allocator()->allocate();
220}
221
222void NEConvolutionLayer::run()
223{
224 // Run weights reshaping (Runs once for every configure)
225 if(!_are_weights_reshaped)
226 {
227 _are_weights_reshaped = true;
228 _reshape_weights.run();
229 }
230
231 // Run input reshaping
232 NEScheduler::get().schedule(&_input_im2col_kernel, Window::DimY);
233 if(!_is_fully_connected_convolution)
234 {
235 // Run interleave
236 NEScheduler::get().schedule(&_input_interleave_kernel, Window::DimY);
237 }
238
239 // Runs matrix multiply on reshaped matrices
240 NEScheduler::get().schedule(&_mm_kernel, Window::DimY);
241
242 // Reshape output matrix
243 NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY);
244}