blob: 1aab3a05e0aff53bbbb9ebc1be1d13a9b2dd5b10 [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{
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010086 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000087 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)
Georgios Pinitas72219332018-06-05 14:56:06 +0100134 : _memory_group(std::move(memory_manager)), _im2col_kernel(), _reshape_weights_function(), _interleave4x4_kernel(), _mm_kernel(), _accumulate_biases_kernel(), _im2col_output(),
135 _interleave4x4_output(), _reshape_weights_output(), _original_weights(nullptr), _is_batched_fc_layer(false), _linearize_input(false), _accumulate_biases(false), _is_prepared(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136{
137}
138
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100139void NEFullyConnectedLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output,
140 FullyConnectedLayerInfo fc_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142 // With the Fully Connected layer we can have 4 different cases:
143 // 1) Convolution layer -> Fully Connected layer without batches
144 // 2) Fully Connected layer -> Fully Connected layer without batches
145 // 3) Convolution layer -> Fully Connected layer with batches
146 // 4) Fully Connected layer -> Fully Connected layer with batches
147
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100148 // Expected shape before transpose and reshaping
149 // Input: In x B (In and B can be multi-dimensional)
150 // Weights: flat(In) x Out
151 // Biases: Out
152 // Output: Out x B (B can be multi-dimensional)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000153 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000155 // Perform validate step
156 ARM_COMPUTE_ERROR_THROW_ON(NEFullyConnectedLayer::validate(input->info(),
157 weights->info(),
158 biases != nullptr ? biases->info() : nullptr,
159 output->info(),
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100160 fc_info));
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 Pinitas72219332018-06-05 14:56:06 +0100166 _original_weights = weights;
167 _linearize_input = (input->info()->tensor_shape().x() != linear_input_size) || (num_input_dimensions > 1 && linear_input_size == 1);
168 _accumulate_biases = biases != nullptr;
169 _is_batched_fc_layer = num_batch_dimensions > 0;
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100170 _is_prepared = fc_info.are_weights_reshaped || (!fc_info.transpose_weights && !_is_batched_fc_layer);
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100171
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
Georgios Pinitas72219332018-06-05 14:56:06 +0100175 if(!_is_prepared)
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(),
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100180 fc_info.transpose_weights,
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000181 _is_batched_fc_layer, interleave_width)));
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100182
183 // Reshape the weights
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100184 _reshape_weights_function.configure(weights, &_reshape_weights_output, fc_info.transpose_weights, _is_batched_fc_layer);
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100185 }
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
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100223 if(_linearize_input)
224 {
225 _im2col_output.allocator()->allocate();
226 }
227
228 if(_is_batched_fc_layer)
229 {
230 _interleave4x4_output.allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100231 }
232}
233
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100234Status NEFullyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
235 FullyConnectedLayerInfo fc_info)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000236{
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100237 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000238 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000239
240 const int num_batch_dimensions = std::max(0, static_cast<int>(output->tensor_shape().num_dimensions()) - 1);
241 const int num_input_dimensions = input->tensor_shape().num_dimensions() - num_batch_dimensions;
242 const size_t linear_input_size = input->tensor_shape().total_size_lower(num_input_dimensions);
243
244 const bool linearize_input = (input->tensor_shape().x() != linear_input_size) || (num_input_dimensions > 1 && linear_input_size == 1);
245 const bool accumulate_biases = biases != nullptr;
246 const bool is_batched_fc_layer = num_batch_dimensions > 0;
247
248 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().total_size_upper(num_input_dimensions) != output->tensor_shape().total_size_upper(1));
249 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 2);
250
251 const size_t interleave_width = 16 / input->element_size();
252 const ITensorInfo *weights_to_use = weights;
253 std::unique_ptr<ITensorInfo> reshape_weights_output = input->clone();
254
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100255 if(!fc_info.are_weights_reshaped && (fc_info.transpose_weights || is_batched_fc_layer))
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000256 {
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100257 reshape_weights_output->set_tensor_shape(compute_fully_connected_reshaped_weights_shape(weights, fc_info.transpose_weights, is_batched_fc_layer, interleave_width));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000258
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100259 ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayerReshapeWeights::validate(weights, reshape_weights_output.get(), fc_info.transpose_weights, is_batched_fc_layer));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000260
261 weights_to_use = reshape_weights_output.get();
262 }
263
264 // Check correct shape of weights
265 if(is_batched_fc_layer)
266 {
267 // Transpose + Transpose1xW
268 ARM_COMPUTE_RETURN_ERROR_ON(weights_to_use->tensor_shape().x() != linear_input_size * interleave_width);
269 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)));
270 }
271 else
272 {
273 // Transpose
274 ARM_COMPUTE_RETURN_ERROR_ON(weights_to_use->tensor_shape().x() != output->tensor_shape().x());
275 ARM_COMPUTE_RETURN_ERROR_ON(weights_to_use->tensor_shape().y() != linear_input_size);
276 }
277
278 const ITensorInfo *multiply_input = input;
279 std::unique_ptr<ITensorInfo> im2col_output = input->clone();
280 std::unique_ptr<ITensorInfo> interleave4x4_output = input->clone();
281
282 if(linearize_input)
283 {
Giorgio Arena156fcf32018-03-09 15:30:43 +0000284 im2col_output->set_tensor_shape(compute_im2col_fc_shape(input, num_input_dimensions));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000285
286 ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, im2col_output.get(), Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false, true));
287
288 multiply_input = im2col_output.get();
289 }
290
291 int m = multiply_input->dimension(1);
292 int k = multiply_input->dimension(0);
293
294 if(is_batched_fc_layer)
295 {
296 interleave4x4_output->set_tensor_shape(compute_interleaved_shape(*multiply_input));
297
298 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(multiply_input, interleave4x4_output.get()));
299
300 multiply_input = interleave4x4_output.get();
301 }
302
303 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)));
304
305 if(accumulate_biases)
306 {
307 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
308 ARM_COMPUTE_RETURN_ERROR_ON(biases->tensor_shape().x() != output->tensor_shape().x());
309
310 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixAccumulateBiasesKernel::validate(output, biases));
311 }
312
313 return Status{};
314}
315
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100316void NEFullyConnectedLayer::run()
317{
Georgios Pinitas72219332018-06-05 14:56:06 +0100318 prepare();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100319
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100320 _memory_group.acquire();
321
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100322 // Linearize input if it comes from a convolutional layer
323 if(_linearize_input)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100324 {
325 NEScheduler::get().schedule(&_im2col_kernel, Window::DimY);
326 }
327
328 // Interleave input
329 if(_is_batched_fc_layer)
330 {
331 NEScheduler::get().schedule(&_interleave4x4_kernel, Window::DimY);
332 }
333
334 // Run matrix multiply
335 NEScheduler::get().schedule(&_mm_kernel, _is_batched_fc_layer ? Window::DimY : Window::DimX);
336
337 // Accumulate biases if provided
338 if(_accumulate_biases)
339 {
340 NEScheduler::get().schedule(&_accumulate_biases_kernel, Window::DimY);
341 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100342
343 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100344}
Georgios Pinitas72219332018-06-05 14:56:06 +0100345
346void NEFullyConnectedLayer::prepare()
347{
348 // Reshape of the weights (happens only once)
349 if(!_is_prepared)
350 {
351 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
352
353 // Run weights reshape, clean internal tensors and mark original weights tensor as unused
354 _reshape_weights_output.allocator()->allocate();
355 _reshape_weights_function.run();
356 _reshape_weights_function = NEFullyConnectedLayerReshapeWeights();
357 _original_weights->mark_as_unused();
358
359 _is_prepared = true;
360 }
361}