blob: 11e670c98e99901891d19d3c6e07be50f44e4174 [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/CL/functions/CLFullyConnectedLayer.h"
25
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010026#include "arm_compute/core/Size2D.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Validate.h"
28#include "arm_compute/runtime/CL/CLScheduler.h"
29
30#include <algorithm>
31#include <cmath>
32
33using namespace arm_compute;
34
35CLFullyConnectedLayerReshapeWeights::CLFullyConnectedLayerReshapeWeights()
36 : _transpose_kernel(), _transpose1xW_kernel(), _transpose_output(), _transpose_weights(false), _is_batched_fc_layer(false)
37{
38}
39
40void CLFullyConnectedLayerReshapeWeights::configure(const ICLTensor *input, ICLTensor *output, bool transpose_weights, bool is_batched_fc_layer)
41{
Gian Marco Iodice368da832017-07-03 12:33:49 +010042 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::F16, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043 ARM_COMPUTE_ERROR_ON(output == nullptr);
44 ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() != 2);
45 ARM_COMPUTE_ERROR_ON((transpose_weights == false) && (is_batched_fc_layer == false));
46
47 const DataType dt = input->info()->data_type();
48 const int fixed_point_position = input->info()->fixed_point_position();
49
50 _transpose_weights = transpose_weights;
51 _is_batched_fc_layer = is_batched_fc_layer;
52
53 // Check if we need to transpose the weights
54 if(_transpose_weights)
55 {
56 if(_is_batched_fc_layer)
57 {
58 // Initialize the output tensor for transpose
59 TensorShape shape_transposed(input->info()->dimension(1), input->info()->dimension(0));
60 _transpose_output.allocator()->init(TensorInfo(shape_transposed, 1, dt, fixed_point_position));
61 _transpose_kernel.configure(input, &_transpose_output);
62
63 // Configure transpose 1xW kernel
64 _transpose1xW_kernel.configure(&_transpose_output, output);
65
66 // Allocate temporary tensor used for transposing the weights
67 _transpose_output.allocator()->allocate();
68 }
69 else
70 {
71 _transpose_kernel.configure(input, output);
72 }
73 }
74 else
75 {
76 if(_is_batched_fc_layer)
77 {
78 // Configure transpose 1xW kernel
79 _transpose1xW_kernel.configure(input, output);
80 }
81 else
82 {
83 ARM_COMPUTE_ERROR("Configuration transpose_weights=false & is_batched_fc_layer=false not supported");
84 }
85 }
86}
87
88void CLFullyConnectedLayerReshapeWeights::run()
89{
90 if(_transpose_weights)
91 {
92 CLScheduler::get().enqueue(_transpose_kernel, _is_batched_fc_layer);
93 }
94 if(_is_batched_fc_layer)
95 {
96 CLScheduler::get().enqueue(_transpose1xW_kernel);
97 }
98}
99
100CLFullyConnectedLayer::CLFullyConnectedLayer()
101 : _im2col_kernel(), _reshape_weights_kernel(), _interleave4x4_kernel(), _mm_kernel(), _accumulate_biases_kernel(), _im2col_output(), _interleave4x4_output(), _reshape_weights_output(),
102 _are_weights_reshaped(true), _is_fc_after_conv(true), _is_batched_fc_layer(false), _accumulate_biases(false)
103{
104}
105
106void CLFullyConnectedLayer::configure_conv_fc_wb(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output)
107{
108 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(0) != (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2) * (16 / weights->info()->element_size())));
109
110 const DataType dt = input->info()->data_type();
111 const int fixed_point_position = input->info()->fixed_point_position();
112
113 // If the fully connected layer is called after a convolution layer, the input tensor must be linearized
114
115 // Initialize output tensor for im2col
116 TensorShape shape_im2col;
117 shape_im2col.set(0, input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2));
118 shape_im2col.set(1, input->info()->dimension(3));
119 shape_im2col.set(2, input->info()->dimension(4));
120 shape_im2col.set(3, input->info()->dimension(5));
121 _im2col_output.allocator()->init(TensorInfo(shape_im2col, 1, dt, fixed_point_position));
122
123 // Initialize output tensor for interleave 4x4
124 TensorShape shape_interleaved = _im2col_output.info()->tensor_shape();
125 shape_interleaved.set(0, shape_interleaved.x() * 4);
126 shape_interleaved.set(1, std::ceil(static_cast<float>(shape_interleaved.y()) / 4));
127 _interleave4x4_output.allocator()->init(TensorInfo(shape_interleaved, 1, dt, fixed_point_position));
128
129 // Configure im2col kernel
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100130 _im2col_kernel.configure(input, &_im2col_output, Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131
132 // Configure interleave4x4 kernel
133 _interleave4x4_kernel.configure(&_im2col_output, &_interleave4x4_output);
134
135 // Configure matrix multiply kernel
136 _mm_kernel.configure(&_interleave4x4_output, weights, output, 1.0f);
137
138 // Allocate the tensors once all the configure methods have been called
139 _im2col_output.allocator()->allocate();
140 _interleave4x4_output.allocator()->allocate();
141}
142
143void CLFullyConnectedLayer::configure_fc_fc_wb(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output)
144{
145 const DataType dt = input->info()->data_type();
146 const int fixed_point_position = input->info()->fixed_point_position();
147
148 // Initialize output tensor for interleave 4x4
149 TensorShape shape_interleaved = input->info()->tensor_shape();
150 shape_interleaved.set(0, shape_interleaved.x() * 4);
151 shape_interleaved.set(1, std::ceil(static_cast<float>(shape_interleaved.y()) / 4));
152 _interleave4x4_output.allocator()->init(TensorInfo(shape_interleaved, 1, dt, fixed_point_position));
153
154 // Configure interleave4x4 kernel
155 _interleave4x4_kernel.configure(input, &_interleave4x4_output);
156
157 // Configure matrix multiply kernel
158 _mm_kernel.configure(&_interleave4x4_output, weights, output, 1.0f);
159
160 // Allocate the tensors once all the configure methods have been called
161 _interleave4x4_output.allocator()->allocate();
162}
163
164void CLFullyConnectedLayer::configure_conv_fc_nb(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output)
165{
166 ARM_COMPUTE_ERROR_ON((weights->info()->dimension(1) != (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2))));
167
168 const DataType dt = input->info()->data_type();
169 const int fixed_point_position = input->info()->fixed_point_position();
170
171 // If the fully connected layer is called after a convolution layer, the input tensor must be linearized
172
173 // Initialize output tensor for im2col
174 TensorShape shape_im2col;
175 shape_im2col.set(0, input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2));
176 shape_im2col.set(1, 1);
177 _im2col_output.allocator()->init(TensorInfo(shape_im2col, 1, dt, fixed_point_position));
178
179 // Configure im2col kernel
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100180 _im2col_kernel.configure(input, &_im2col_output, Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181
182 // Configure matrix multiply kernel
183 _mm_kernel.configure(&_im2col_output, weights, output, 1.0f);
184
185 // Allocate the output tensor for im2col once all the configure methods have been called
186 _im2col_output.allocator()->allocate();
187}
188
189void CLFullyConnectedLayer::configure_fc_fc_nb(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output)
190{
191 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != weights->info()->dimension(1));
192
193 // Configure matrix multiply kernel
194 _mm_kernel.configure(input, weights, output, 1.0f);
195}
196
197void CLFullyConnectedLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, bool transpose_weights, bool are_weights_reshaped)
198{
Gian Marco Iodice368da832017-07-03 12:33:49 +0100199 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::F16, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
201 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() != 2);
202
203 const DataType dt = input->info()->data_type();
204 const int fixed_point_position = input->info()->fixed_point_position();
205
206 _are_weights_reshaped = are_weights_reshaped;
207 _is_fc_after_conv = true;
208 _is_batched_fc_layer = false;
209 _accumulate_biases = false;
210
211 if(biases != nullptr)
212 {
213 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
214
215 _accumulate_biases = true;
216
217 // Configure accumulate biases kernel
218 _accumulate_biases_kernel.configure(output, biases);
219 }
220
221 // With the Fully Connected layer we can have 4 different cases:
222 // 1) Convolution layer -> Fully Connected layer without batches
223 // 2) Fully Connected layer -> Fully Connected layer without batches
224 // 3) Convolution layer -> Fully Connected layer with batches
225 // 4) Fully Connected layer -> Fully Connected layer with batches
226
227 // Check if we have a fully connected layer with batches
228 _is_batched_fc_layer = (output->info()->dimension(1) > 1);
229
230 const ICLTensor *weights_to_use = weights;
231
232 if(!are_weights_reshaped)
233 {
234 if((transpose_weights || _is_batched_fc_layer))
235 {
236 weights_to_use = &_reshape_weights_output;
237
238 if(transpose_weights)
239 {
240 if(_is_batched_fc_layer)
241 {
242 const float transpose_width = 16.0f / input->info()->element_size();
243 TensorShape shape_wt(weights->info()->dimension(0) * static_cast<unsigned int>(transpose_width), static_cast<unsigned int>(std::ceil(weights->info()->dimension(1) / transpose_width)));
244 TensorInfo info_wt(shape_wt, 1, dt, fixed_point_position);
245 _reshape_weights_output.allocator()->init(info_wt);
246 }
247 else
248 {
249 TensorShape shape_wt(weights->info()->dimension(1), weights->info()->dimension(0));
250 TensorInfo info_wt(shape_wt, 1, dt, fixed_point_position);
251 _reshape_weights_output.allocator()->init(info_wt);
252 }
253 }
254 else
255 {
256 ARM_COMPUTE_ERROR_ON(!_is_batched_fc_layer);
257
258 const float transpose_width = 16.0f / input->info()->element_size();
259 TensorShape shape_wt(weights->info()->dimension(1) * static_cast<unsigned int>(transpose_width), static_cast<unsigned int>(std::ceil(weights->info()->dimension(0) / transpose_width)));
260 TensorInfo info_wt(shape_wt, 1, dt, fixed_point_position);
261 _reshape_weights_output.allocator()->init(info_wt);
262 }
263
264 // Reshape the weights
265 _reshape_weights_kernel.configure(weights, &_reshape_weights_output, transpose_weights, _is_batched_fc_layer);
266 }
267 }
268
269 if(_is_batched_fc_layer)
270 {
271 _is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->info()->tensor_shape().cbegin() + 3,
272 input->info()->tensor_shape().cend(),
273 output->info()->tensor_shape().cbegin() + 1));
274
275 if(_is_fc_after_conv)
276 {
277 // Fully Connected layer after a Convolution Layer with batches
278 configure_conv_fc_wb(input, weights_to_use, output);
279 }
280 else
281 {
282 // Fully Connected layer after a Fully Connected Layer with batches
283 configure_fc_fc_wb(input, weights_to_use, output);
284 }
285 }
286 else
287 {
288 // In case of not batched fully connected layer, the weights will not be reshaped using transposed1xW
289 _is_fc_after_conv = ((weights_to_use->info()->dimension(1)) == (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2)));
290
291 if(_is_fc_after_conv)
292 {
293 // Fully Connected layer after a Convolution Layer without batches
294 configure_conv_fc_nb(input, weights_to_use, output);
295 }
296 else
297 {
298 // Fully Connected layer after a Fully Connected Layer without batches
299 configure_fc_fc_nb(input, weights_to_use, output);
300 }
301 }
302
303 // Allocate the transpose tensor if the are_weights_reshaped flag is false and once all the configure methods have been called
304 if(!are_weights_reshaped)
305 {
306 if(transpose_weights || _is_batched_fc_layer)
307 {
308 // Allocate the tensor for the weights reshaped
309 _reshape_weights_output.allocator()->allocate();
310 }
311 }
312}
313
314void CLFullyConnectedLayer::run()
315{
316 // Reshape of the weights (happens only once)
317 if(!_are_weights_reshaped)
318 {
319 _are_weights_reshaped = true;
320 _reshape_weights_kernel.run();
321 }
322
323 // Linearize input if it comes from a convolutional layer
324 if(_is_fc_after_conv)
325 {
326 CLScheduler::get().enqueue(_im2col_kernel, false);
327 }
328
329 // Interleave input
330 if(_is_batched_fc_layer)
331 {
332 CLScheduler::get().enqueue(_interleave4x4_kernel, false);
333 }
334
335 // Run matrix multiply
336 CLScheduler::get().enqueue(_mm_kernel, !_accumulate_biases);
337
338 // Accumulate biases if provided
339 if(_accumulate_biases)
340 {
341 CLScheduler::get().enqueue(_accumulate_biases_kernel);
342 }
343}