blob: 958d081fd2135f93e2b333c4a751f2597c3fd8d6 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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/NEFullyConnectedLayer.h"
25
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000026#include "arm_compute/core/Helpers.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/Validate.h"
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/runtime/NEON/NEScheduler.h"
31
32#include <algorithm>
33#include <cmath>
34
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000035using namespace arm_compute;
36using namespace arm_compute::misc::shape_calculator;
37
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010038NEFullyConnectedLayerReshapeWeights::NEFullyConnectedLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager)
39 : _memory_group(std::move(memory_manager)), _transpose_kernel(), _transpose1xW_kernel(), _transpose_output(), _transpose_weights(false), _is_batched_fc_layer(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040{
41}
42
43void NEFullyConnectedLayerReshapeWeights::configure(const ITensor *input, ITensor *output, bool transpose_weights, bool is_batched_fc_layer)
44{
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000045 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000047 // Perform validate step
48 ARM_COMPUTE_ERROR_THROW_ON(NEFullyConnectedLayerReshapeWeights::validate(input->info(), output->info(), transpose_weights, is_batched_fc_layer));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049
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
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000059 _transpose_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_transposed_shape(*input->info())));
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010060 _memory_group.manage(&_transpose_output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 _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 }
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000081 }
82}
83
84Status NEFullyConnectedLayerReshapeWeights::validate(const ITensorInfo *input, const ITensorInfo *output, bool transpose_weights, bool is_batched_fc_layer)
85{
86 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
87 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 2);
88 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!transpose_weights && !is_batched_fc_layer, "Configuration transpose_weights=false & is_batched_fc_layer=false not supported");
89
90 if(transpose_weights)
91 {
92 if(is_batched_fc_layer)
93 {
94 std::unique_ptr<ITensorInfo> use_output = output->clone();
95 use_output->set_is_resizable(true).reset_padding().set_tensor_shape(compute_transposed_shape(*input));
96
97 ARM_COMPUTE_RETURN_ON_ERROR(NETransposeKernel::validate(input, use_output.get()));
98 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(use_output.get(), output));
99 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 else
101 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000102 ARM_COMPUTE_RETURN_ON_ERROR(NETransposeKernel::validate(input, output));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103 }
104 }
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000105 else
106 {
107 if(is_batched_fc_layer)
108 {
109 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(input, output));
110 }
111 }
112
113 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114}
115
116void NEFullyConnectedLayerReshapeWeights::run()
117{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100118 _memory_group.acquire();
119
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120 if(_transpose_weights)
121 {
122 NEScheduler::get().schedule(&_transpose_kernel, Window::DimY);
123 }
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100124
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125 if(_is_batched_fc_layer)
126 {
127 NEScheduler::get().schedule(&_transpose1xW_kernel, Window::DimY);
128 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100129
130 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131}
132
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100133NEFullyConnectedLayer::NEFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager)
134 : _memory_group(std::move(memory_manager)), _im2col_kernel(), _reshape_weights_kernel(), _interleave4x4_kernel(), _mm_kernel(), _accumulate_biases_kernel(), _im2col_output(), _interleave4x4_output(),
Georgios Pinitas1562be32018-03-08 19:09:19 +0000135 _reshape_weights_output(), _are_weights_reshaped(false), _is_batched_fc_layer(false), _linearize_input(false), _accumulate_biases(false), _original_weights(nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136{
137}
138
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139void NEFullyConnectedLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose_weights, bool are_weights_reshaped)
140{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141 // With the Fully Connected layer we can have 4 different cases:
142 // 1) Convolution layer -> Fully Connected layer without batches
143 // 2) Fully Connected layer -> Fully Connected layer without batches
144 // 3) Convolution layer -> Fully Connected layer with batches
145 // 4) Fully Connected layer -> Fully Connected layer with batches
146
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100147 // Expected shape before transpose and reshaping
148 // Input: In x B (In and B can be multi-dimensional)
149 // Weights: flat(In) x Out
150 // Biases: Out
151 // Output: Out x B (B can be multi-dimensional)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000152 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000154 // Perform validate step
155 ARM_COMPUTE_ERROR_THROW_ON(NEFullyConnectedLayer::validate(input->info(),
156 weights->info(),
157 biases != nullptr ? biases->info() : nullptr,
158 output->info(),
159 transpose_weights,
160 are_weights_reshaped));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000162 const int num_batch_dimensions = std::max(0, static_cast<int>(output->info()->tensor_shape().num_dimensions()) - 1);
163 const int num_input_dimensions = input->info()->tensor_shape().num_dimensions() - num_batch_dimensions;
164 const size_t linear_input_size = input->info()->tensor_shape().total_size_lower(num_input_dimensions);
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100165
Georgios Pinitas1562be32018-03-08 19:09:19 +0000166 _original_weights = weights;
Georgios Pinitas96880cf2017-10-20 18:52:20 +0100167 _linearize_input = (input->info()->tensor_shape().x() != linear_input_size) || (num_input_dimensions > 1 && linear_input_size == 1);
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100168 _are_weights_reshaped = are_weights_reshaped;
169 _accumulate_biases = biases != nullptr;
170 _is_batched_fc_layer = num_batch_dimensions > 0;
171
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100172 const size_t interleave_width = 16 / input->info()->element_size();
173 const ITensor *weights_to_use = weights;
174
175 if(!are_weights_reshaped && (transpose_weights || _is_batched_fc_layer))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176 {
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100177 weights_to_use = &_reshape_weights_output;
178
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000179 _reshape_weights_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_fully_connected_reshaped_weights_shape(weights->info(),
180 transpose_weights,
181 _is_batched_fc_layer, interleave_width)));
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100182
183 // Reshape the weights
184 _reshape_weights_kernel.configure(weights, &_reshape_weights_output, transpose_weights, _is_batched_fc_layer);
185 }
186
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100187 const ITensor *multiply_input = input;
188
189 if(_linearize_input)
190 {
Giorgio Arena156fcf32018-03-09 15:30:43 +0000191 _im2col_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_im2col_fc_shape(input->info(), num_input_dimensions)));
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100192
193 // Configure im2col kernel
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100194 _memory_group.manage(&_im2col_output);
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000195 _im2col_kernel.configure(input, &_im2col_output, Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false, true);
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100196
197 multiply_input = &_im2col_output;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198 }
199
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000200 int m = multiply_input->info()->dimension(1);
201 int k = multiply_input->info()->dimension(0);
202
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100203 if(_is_batched_fc_layer)
204 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000205 _interleave4x4_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_interleaved_shape(*multiply_input->info())));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100206
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100207 // Configure interleave4x4 kernel
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100208 _memory_group.manage(&_interleave4x4_output);
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100209 _interleave4x4_kernel.configure(multiply_input, &_interleave4x4_output);
210
211 multiply_input = &_interleave4x4_output;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100212 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100213
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100214 // Configure matrix multiply kernel
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000215 _mm_kernel.configure(multiply_input, weights_to_use, output, 1.0f, _is_batched_fc_layer, GEMMReshapeInfo(m, 0 /* no transpose */, k));
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100216
217 if(_accumulate_biases)
218 {
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100219 // Configure accumulate biases kernel
220 _accumulate_biases_kernel.configure(output, biases);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221 }
222
223 // Allocate the transpose tensor if the are_weights_reshaped flag is false and once all the configure methods have been called
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100224 if(!are_weights_reshaped && (transpose_weights || _is_batched_fc_layer))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100225 {
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100226 // Allocate the tensor for the weights reshaped
227 _reshape_weights_output.allocator()->allocate();
228 }
229
230 if(_linearize_input)
231 {
232 _im2col_output.allocator()->allocate();
233 }
234
235 if(_is_batched_fc_layer)
236 {
237 _interleave4x4_output.allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100238 }
239}
240
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000241Status NEFullyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, bool transpose_weights, bool are_weights_reshaped)
242{
243 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
244 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
245 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, weights, output);
246
247 const int num_batch_dimensions = std::max(0, static_cast<int>(output->tensor_shape().num_dimensions()) - 1);
248 const int num_input_dimensions = input->tensor_shape().num_dimensions() - num_batch_dimensions;
249 const size_t linear_input_size = input->tensor_shape().total_size_lower(num_input_dimensions);
250
251 const bool linearize_input = (input->tensor_shape().x() != linear_input_size) || (num_input_dimensions > 1 && linear_input_size == 1);
252 const bool accumulate_biases = biases != nullptr;
253 const bool is_batched_fc_layer = num_batch_dimensions > 0;
254
255 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().total_size_upper(num_input_dimensions) != output->tensor_shape().total_size_upper(1));
256 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 2);
257
258 const size_t interleave_width = 16 / input->element_size();
259 const ITensorInfo *weights_to_use = weights;
260 std::unique_ptr<ITensorInfo> reshape_weights_output = input->clone();
261
262 if(!are_weights_reshaped && (transpose_weights || is_batched_fc_layer))
263 {
264 reshape_weights_output->set_tensor_shape(compute_fully_connected_reshaped_weights_shape(weights, transpose_weights, is_batched_fc_layer, interleave_width));
265
266 ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayerReshapeWeights::validate(weights, reshape_weights_output.get(), transpose_weights, is_batched_fc_layer));
267
268 weights_to_use = reshape_weights_output.get();
269 }
270
271 // Check correct shape of weights
272 if(is_batched_fc_layer)
273 {
274 // Transpose + Transpose1xW
275 ARM_COMPUTE_RETURN_ERROR_ON(weights_to_use->tensor_shape().x() != linear_input_size * interleave_width);
276 ARM_COMPUTE_RETURN_ERROR_ON(weights_to_use->tensor_shape().y() != static_cast<unsigned int>(std::ceil(static_cast<float>(output->tensor_shape().x()) / interleave_width)));
277 }
278 else
279 {
280 // Transpose
281 ARM_COMPUTE_RETURN_ERROR_ON(weights_to_use->tensor_shape().x() != output->tensor_shape().x());
282 ARM_COMPUTE_RETURN_ERROR_ON(weights_to_use->tensor_shape().y() != linear_input_size);
283 }
284
285 const ITensorInfo *multiply_input = input;
286 std::unique_ptr<ITensorInfo> im2col_output = input->clone();
287 std::unique_ptr<ITensorInfo> interleave4x4_output = input->clone();
288
289 if(linearize_input)
290 {
Giorgio Arena156fcf32018-03-09 15:30:43 +0000291 im2col_output->set_tensor_shape(compute_im2col_fc_shape(input, num_input_dimensions));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000292
293 ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, im2col_output.get(), Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false, true));
294
295 multiply_input = im2col_output.get();
296 }
297
298 int m = multiply_input->dimension(1);
299 int k = multiply_input->dimension(0);
300
301 if(is_batched_fc_layer)
302 {
303 interleave4x4_output->set_tensor_shape(compute_interleaved_shape(*multiply_input));
304
305 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(multiply_input, interleave4x4_output.get()));
306
307 multiply_input = interleave4x4_output.get();
308 }
309
310 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(multiply_input, weights_to_use, output, 1.0f, is_batched_fc_layer, GEMMReshapeInfo(m, 0 /* no transpose */, k)));
311
312 if(accumulate_biases)
313 {
314 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
315 ARM_COMPUTE_RETURN_ERROR_ON(biases->tensor_shape().x() != output->tensor_shape().x());
316
317 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixAccumulateBiasesKernel::validate(output, biases));
318 }
319
320 return Status{};
321}
322
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100323void NEFullyConnectedLayer::run()
324{
325 // Reshape of the weights (happens only once)
326 if(!_are_weights_reshaped)
327 {
Georgios Pinitas1562be32018-03-08 19:09:19 +0000328 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
329
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100330 _are_weights_reshaped = true;
331 _reshape_weights_kernel.run();
Georgios Pinitas1562be32018-03-08 19:09:19 +0000332
333 // Mark original weights tensor as unused
334 _original_weights->mark_as_unused();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100335 }
336
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100337 _memory_group.acquire();
338
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100339 // Linearize input if it comes from a convolutional layer
340 if(_linearize_input)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100341 {
342 NEScheduler::get().schedule(&_im2col_kernel, Window::DimY);
343 }
344
345 // Interleave input
346 if(_is_batched_fc_layer)
347 {
348 NEScheduler::get().schedule(&_interleave4x4_kernel, Window::DimY);
349 }
350
351 // Run matrix multiply
352 NEScheduler::get().schedule(&_mm_kernel, _is_batched_fc_layer ? Window::DimY : Window::DimX);
353
354 // Accumulate biases if provided
355 if(_accumulate_biases)
356 {
357 NEScheduler::get().schedule(&_accumulate_biases_kernel, Window::DimY);
358 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100359
360 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100361}