blob: f4c073668add3f58b89c8961174a20415979efc6 [file] [log] [blame]
Stephen Lie855c232018-01-04 14:13:22 +08001/*
2 * Copyright (c) 2017-2018 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
25#include "arm_compute/runtime/GLES_COMPUTE/functions/GCConvolutionLayer.h"
26
27#include "arm_compute/core/PixelValue.h"
28#include "arm_compute/core/Size2D.h"
29#include "arm_compute/core/Utils.h"
30#include "arm_compute/core/Validate.h"
31#include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h"
32
33#include <cmath>
34#include <memory>
35#include <tuple>
36
37using namespace arm_compute;
38
39GCConvolutionLayerReshapeWeights::GCConvolutionLayerReshapeWeights()
40 : _weights_reshape_kernel(), _weights_transposed_kernel(), _weights_reshaped(), _transpose1xW(false)
41{
42}
43
44void GCConvolutionLayerReshapeWeights::configure(const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output, bool transpose1xW)
45{
46 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::F16, DataType::F32);
47 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output);
48 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4);
49
50 if(biases != nullptr)
51 {
52 ARM_COMPUTE_ERROR_ON(is_data_type_quantized_asymmetric(weights->info()->data_type()));
53 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(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 const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type());
59 const unsigned bias_element = (append_biases) ? 1 : 0;
60 const IGCTensor *biases_to_use = (append_biases) ? biases : nullptr;
61
62 _transpose1xW = transpose1xW;
63
64 if(transpose1xW)
65 {
66 // Create tensor to store the reshaped weights
67 const unsigned int mat_weights_cols = weights->info()->dimension(3);
68 const unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + bias_element;
69 TensorShape shape_wr(mat_weights_cols, mat_weights_rows);
70 const DataType dt = weights->info()->data_type();
71 const int fixed_point_position = weights->info()->fixed_point_position();
72 TensorInfo info_wr(shape_wr, 1, dt, fixed_point_position);
73
74 _weights_reshaped.allocator()->init(info_wr);
75 _weights_reshape_kernel.configure(weights, biases_to_use, &_weights_reshaped);
76 _weights_transposed_kernel.configure(&_weights_reshaped, output);
77 _weights_reshaped.allocator()->allocate();
78 }
79 else
80 {
81 _weights_reshape_kernel.configure(weights, biases_to_use, output);
82 }
83}
84
85void GCConvolutionLayerReshapeWeights::run()
86{
87 GCScheduler::get().dispatch(_weights_reshape_kernel);
88 if(_transpose1xW)
89 {
90 GCScheduler::get().dispatch(_weights_transposed_kernel);
91 }
92}
93
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000094GCConvolutionLayer::GCConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
95 : _memory_group(std::move(memory_manager)), _reshape_weights(), _input_im2col_kernel(), _input_interleave_kernel(), _mm_kernel(), _output_col2im_kernel(), _fill_border(), _input_im2col_reshaped(),
96 _input_interleaved_reshaped(), _weights_reshaped(), _weights_transposed(), _gemm_output(), _tmp_output(), _append_bias(false), _is_fully_connected_convolution(false), _are_weights_reshaped(false)
Stephen Lie855c232018-01-04 14:13:22 +080097{
98}
99
100void GCConvolutionLayer::configure_mm(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output, bool is_interleaved_transposed)
101{
102 _mm_kernel.configure(input, weights, output, 1.f, is_interleaved_transposed);
103}
104
105void GCConvolutionLayer::configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info)
106{
107 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
108 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
109 ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && weights->info()->dimension(2) != input->info()->dimension(2));
110 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4);
111
112 if(biases != nullptr)
113 {
114 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
115 ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && biases->info()->dimension(0) != weights->info()->dimension(3));
116 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
117 }
118
119 const DataType dt = input->info()->data_type();
120
121 _append_bias = (biases != nullptr);
122 _are_weights_reshaped = weights_info.are_reshaped();
123
124 const unsigned bias_element = (_append_bias) ? 1 : 0;
125 const IGCTensor *biases_to_use = (_append_bias) ? biases : nullptr;
126
127 // Get parameters from conv_info
128 unsigned int stride_x = 0;
129 unsigned int stride_y = 0;
130 std::tie(stride_x, stride_y) = conv_info.stride();
131
132 // Get convolved dimensions
133 unsigned int conv_w = 0;
134 unsigned int conv_h = 0;
135
136 const unsigned int kernel_width = (_are_weights_reshaped) ? weights_info.kernel_size().first : weights->info()->dimension(0);
137 const unsigned int kernel_height = (_are_weights_reshaped) ? weights_info.kernel_size().second : weights->info()->dimension(1);
138 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height,
139 conv_info);
140
141 // Check if its a "fully connected" convolution
142 _is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1));
143 const bool run_interleaved = (!_is_fully_connected_convolution);
144
145 unsigned int mat_weights_cols = weights->info()->dimension(3);
146 unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + bias_element;
147
148 // Reshape weights if needed
149 if(_are_weights_reshaped)
150 {
151 if(_is_fully_connected_convolution)
152 {
153 mat_weights_cols = weights->info()->dimension(0);
154 mat_weights_rows = weights->info()->dimension(1);
155 }
156 else
157 {
158 mat_weights_cols = weights_info.num_kernels();
159 const unsigned int quarter_reshaped_cols = weights->info()->dimension(0) / 4;
160 mat_weights_rows = quarter_reshaped_cols + bias_element;
161 }
162 }
163 else
164 {
165 if(_is_fully_connected_convolution)
166 {
167 // Create tensor to store the reshaped weights
168 int num_elems_read_per_iteration_x = 1;
169 if(dt == DataType::F16)
170 {
171 num_elems_read_per_iteration_x = 2;
172 }
173 TensorShape shape_wr((ceil_to_multiple(mat_weights_cols, num_elems_read_per_iteration_x)), mat_weights_rows);
174 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_wr));
175 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped, false /* 1xW transpose */);
176 }
177 else
178 {
179 // Create tensor to store transposed weights
180 const float transpose_width = 16.0f / input->info()->element_size();
181 TensorShape shape_wt(mat_weights_rows * static_cast<unsigned int>(transpose_width), static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)));
182 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_wt));
183 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped, true /* 1xW transpose */);
184 }
185 weights = &_weights_reshaped;
186 }
187
188 // Create tensor to store im2col reshaped inputs
189 const unsigned int mat_input_cols = mat_weights_rows;
190 const unsigned int mat_input_rows = conv_w * conv_h;
191 TensorShape shape_im2col = input->info()->tensor_shape();
192 shape_im2col.set(0, mat_input_cols);
193 shape_im2col.set(1, mat_input_rows);
194 shape_im2col.set(2, 1);
195
196 // FIXME: input->clone() doesn't work with subtensors for grouped convolutions.
197 TensorInfo im2col_reshaped_info(shape_im2col, 1, dt, input->info()->fixed_point_position());
198 _input_im2col_reshaped.allocator()->init(im2col_reshaped_info);
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000199 _memory_group.manage(&_input_im2col_reshaped);
Stephen Lie855c232018-01-04 14:13:22 +0800200
201 // Create tensor (interleave) to prepare input tensor for GEMM
202 if(run_interleaved)
203 {
204 TensorShape shape_interleaved = shape_im2col;
205 shape_interleaved.set(0, shape_interleaved.x() * 4);
206 shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f));
207
208 // FIXME: input->clone() doesn't work with subtensors for grouped convolutions.
209 TensorInfo interleaved_info(shape_interleaved, 1, dt, input->info()->fixed_point_position());
210 _input_interleaved_reshaped.allocator()->init(interleaved_info);
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000211 _memory_group.manage(&_input_interleaved_reshaped);
Stephen Lie855c232018-01-04 14:13:22 +0800212 }
213
214 // Create GEMM output tensor
215 TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape();
216 shape_gemm.set(0, mat_weights_cols);
217 shape_gemm.set(1, mat_input_rows);
218 const DataType gemm_data_type = dt;
219
220 // FIXME: input->clone() doesn't work with subtensors for grouped convolutions.
221 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type, input->info()->fixed_point_position());
222 _gemm_output.allocator()->init(info_gemm);
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000223 _memory_group.manage(&_gemm_output);
Stephen Lie855c232018-01-04 14:13:22 +0800224
225 // Configure kernels
226 if(dt == DataType::F16)
227 {
228 BorderSize border_size = BorderSize(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left());
229 input->info()->extend_padding(border_size);
230 _fill_border.configure(input, border_size, BorderMode::CONSTANT, PixelValue(0)); // for PAD of im2col fp16: consider it as border
231 }
232 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _append_bias);
233
234 // Configure matrix multiply
235 if(run_interleaved)
236 {
237 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
238 configure_mm(&_input_interleaved_reshaped, weights, &_gemm_output);
239 _input_interleaved_reshaped.allocator()->allocate();
240 }
241 else
242 {
243 configure_mm(&_input_im2col_reshaped, weights, &_gemm_output, false);
244 }
245 _input_im2col_reshaped.allocator()->allocate();
246
247 // Configure Col2Im
248 _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h));
249 _gemm_output.allocator()->allocate();
250
251 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");
252
253 // Allocate intermediate tensor
254 if(!_are_weights_reshaped)
255 {
256 _weights_reshaped.allocator()->allocate();
257 }
258}
259
260void GCConvolutionLayer::run()
261{
262 // Run weights reshaping (Runs once for every configure)
263 if(!_are_weights_reshaped)
264 {
265 _are_weights_reshaped = true;
266 _reshape_weights.run();
267 }
268
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000269 _memory_group.acquire();
270
Stephen Lie855c232018-01-04 14:13:22 +0800271 // Run im2col
272 GCScheduler::get().dispatch(_fill_border);
273 GCScheduler::get().memory_barrier();
274 GCScheduler::get().dispatch(_input_im2col_kernel);
275
276 if(!_is_fully_connected_convolution)
277 {
278 GCScheduler::get().memory_barrier();
279 // Run interleave4x4
280 GCScheduler::get().dispatch(_input_interleave_kernel);
281 }
282
283 GCScheduler::get().memory_barrier();
284 // Runs matrix multiply on reshaped matrices
285 GCScheduler::get().dispatch(_mm_kernel);
286
287 GCScheduler::get().memory_barrier();
288 // Reshape output matrix
289 GCScheduler::get().dispatch(_output_col2im_kernel, false);
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000290
291 _memory_group.release();
Stephen Lie855c232018-01-04 14:13:22 +0800292}