blob: 0466a4a501586db799fb2a46180b13f46d0f9b90 [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
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010037NEConvolutionLayerReshapeWeights::NEConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager)
38 : _memory_group(std::move(memory_manager)), _weights_reshape_kernel(), _weights_transposed_kernel(), _weights_reshaped(), _transpose1xW(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039{
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);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010071 _memory_group.manage(&_weights_reshaped);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 _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{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010084 _memory_group.acquire();
85
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086 NEScheduler::get().schedule(&_weights_reshape_kernel, 3);
87 if(_transpose1xW)
88 {
89 NEScheduler::get().schedule(&_weights_transposed_kernel, Window::DimY);
90 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010091
92 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093}
94
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010095NEConvolutionLayer::NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
96 : _memory_group(std::move(memory_manager)), _input_im2col_kernel(), _input_interleave_kernel(), _reshape_weights(), _mm_kernel(), _output_col2im_kernel(), _input_im2col_reshaped(),
97 _input_interleaved_reshaped(), _weights_reshaped(), _gemm_output(), _has_bias(false), _is_fully_connected_convolution(false), _are_weights_reshaped(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098{
99}
100
101void NEConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info)
102{
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100103 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 +0100104 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
105 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106 ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && weights->info()->dimension(2) != input->info()->dimension(2));
107 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4);
108
109 if(biases != nullptr)
110 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
112 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases);
113 ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && biases->info()->dimension(0) != weights->info()->dimension(3));
114 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
115 }
116
117 const DataType dt = input->info()->data_type();
118 const int fixed_point_position = input->info()->fixed_point_position();
119
120 _has_bias = (biases != nullptr);
121 _are_weights_reshaped = weights_info.are_reshaped();
122
123 // Get parameters from conv_info
124 unsigned int stride_x = 0;
125 unsigned int stride_y = 0;
126 unsigned int pad_x = 0;
127 unsigned int pad_y = 0;
128 std::tie(stride_x, stride_y) = conv_info.stride();
129 std::tie(pad_x, pad_y) = conv_info.pad();
130
131 // Get convolved dimensions
132 unsigned int conv_w = 0;
133 unsigned int conv_h = 0;
134
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100135 const unsigned int kernel_width = (_are_weights_reshaped) ? weights_info.kernel_size().first : weights->info()->dimension(0);
136 const unsigned int kernel_height = (_are_weights_reshaped) ? weights_info.kernel_size().second : weights->info()->dimension(1);
137 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height,
138 conv_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139
140 // Check if its a "fully connected" convolution
141 _is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1));
142
143 unsigned int mat_weights_cols = weights->info()->dimension(3);
144 unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + (_has_bias ? 1 : 0);
145
146 // Reshape weights if needed
147 if(_are_weights_reshaped)
148 {
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100149 mat_weights_cols = weights_info.num_kernels();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 const unsigned int quarter_reshaped_cols = weights->info()->dimension(0) / 4;
151 mat_weights_rows = (_has_bias ? 1 + quarter_reshaped_cols : quarter_reshaped_cols);
152 }
153 else
154 {
155 if(_is_fully_connected_convolution)
156 {
157 // Create tensor to store the reshaped weights
158 TensorShape shape_wr(mat_weights_cols, mat_weights_rows);
159 TensorInfo info_wr(shape_wr, 1, dt, fixed_point_position);
160 _weights_reshaped.allocator()->init(info_wr);
161 _reshape_weights.configure(weights, biases, &_weights_reshaped, false /* 1xW transpose */);
162 }
163 else
164 {
165 // Create tensor to store transposed weights
166 const float transpose_width = 16.0f / input->info()->element_size();
167 TensorShape shape_wt(mat_weights_rows * static_cast<unsigned int>(transpose_width), static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)));
168 TensorInfo info_wt(shape_wt, 1, dt, fixed_point_position);
169 _weights_reshaped.allocator()->init(info_wt);
170 _reshape_weights.configure(weights, biases, &_weights_reshaped, true /* 1xW transpose */);
171 }
172 weights = &_weights_reshaped;
173 }
174
175 // Create tensor to store im2col reshaped inputs
176 const unsigned int mat_input_cols = mat_weights_rows;
177 const unsigned int mat_input_rows = conv_w * conv_h;
178 TensorShape shape_im2col = input->info()->tensor_shape();
179 shape_im2col.set(0, mat_input_cols);
180 shape_im2col.set(1, mat_input_rows);
181 shape_im2col.set(2, 1);
182 _input_im2col_reshaped.allocator()->init(TensorInfo(shape_im2col, 1, dt, fixed_point_position));
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100183 _memory_group.manage(&_input_im2col_reshaped);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184
185 // Create tensor (interleave) to prepare input tensor for GEMM
186 if(!_is_fully_connected_convolution)
187 {
188 TensorShape shape_interleaved = shape_im2col;
189 shape_interleaved.set(0, shape_interleaved.x() * 4);
190 shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f));
191 _input_interleaved_reshaped.allocator()->init(TensorInfo(shape_interleaved, 1, dt, fixed_point_position));
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100192 _memory_group.manage(&_input_interleaved_reshaped);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100193 }
194
195 // Create GEMM output tensor
196 TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape();
197 shape_gemm.set(0, mat_weights_cols);
198 shape_gemm.set(1, mat_input_rows);
199 _gemm_output.allocator()->init(TensorInfo(shape_gemm, 1, dt, fixed_point_position));
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100200 _memory_group.manage(&_gemm_output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201
202 // Configure kernels
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100203 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _has_bias);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204 if(_is_fully_connected_convolution)
205 {
206 _mm_kernel.configure(&_input_im2col_reshaped, weights, &_gemm_output, 1.0f);
207 }
208 else
209 {
210 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
211 _mm_kernel.configure(&_input_interleaved_reshaped, weights, &_gemm_output, 1.0f);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100212 _input_interleaved_reshaped.allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100213 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100214 _input_im2col_reshaped.allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215 _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h));
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100216 _gemm_output.allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100217
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100218 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");
219
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100220 // Allocate intermediate tensor
221 if(!_are_weights_reshaped)
222 {
223 _weights_reshaped.allocator()->allocate();
224 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100225}
226
227void NEConvolutionLayer::run()
228{
229 // Run weights reshaping (Runs once for every configure)
230 if(!_are_weights_reshaped)
231 {
232 _are_weights_reshaped = true;
233 _reshape_weights.run();
234 }
235
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100236 _memory_group.acquire();
237
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100238 // Run input reshaping
239 NEScheduler::get().schedule(&_input_im2col_kernel, Window::DimY);
240 if(!_is_fully_connected_convolution)
241 {
242 // Run interleave
243 NEScheduler::get().schedule(&_input_interleave_kernel, Window::DimY);
244 }
245
246 // Runs matrix multiply on reshaped matrices
247 NEScheduler::get().schedule(&_mm_kernel, Window::DimY);
248
249 // Reshape output matrix
250 NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100251
252 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100253}