blob: 26b72717109c21c6012ce965a43cb9b70cb2ba84 [file] [log] [blame]
Anthony Barbier871448e2017-03-24 14:54:29 +00001/*
Anthony Barbier06ea0482018-02-22 15:45:35 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier871448e2017-03-24 14:54:29 +00003 *
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
Anthony Barbier06ea0482018-02-22 15:45:35 +000026#include "arm_compute/core/Helpers.h"
Kaizen8938bd32017-09-28 14:38:23 +010027#include "arm_compute/core/Size2D.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000028#include "arm_compute/core/Validate.h"
Anthony Barbier06ea0482018-02-22 15:45:35 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000030#include "arm_compute/runtime/NEON/NEScheduler.h"
31
Anthony Barbiera4376382017-04-12 15:12:46 +010032#include <algorithm>
33#include <cmath>
34
Anthony Barbier06ea0482018-02-22 15:45:35 +000035using namespace arm_compute;
36using namespace arm_compute::misc::shape_calculator;
37
Kaizen8938bd32017-09-28 14:38:23 +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 Barbierdbdab852017-06-23 15:42:00 +010040{
41}
42
43void NEFullyConnectedLayerReshapeWeights::configure(const ITensor *input, ITensor *output, bool transpose_weights, bool is_batched_fc_layer)
44{
Anthony Barbier06ea0482018-02-22 15:45:35 +000045 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbierdbdab852017-06-23 15:42:00 +010046
Anthony Barbier06ea0482018-02-22 15:45:35 +000047 // Perform validate step
48 ARM_COMPUTE_ERROR_THROW_ON(NEFullyConnectedLayerReshapeWeights::validate(input->info(), output->info(), transpose_weights, is_batched_fc_layer));
Anthony Barbierdbdab852017-06-23 15:42:00 +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
Anthony Barbier06ea0482018-02-22 15:45:35 +000059 _transpose_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_transposed_shape(*input->info())));
Kaizen8938bd32017-09-28 14:38:23 +010060 _memory_group.manage(&_transpose_output);
Anthony Barbierdbdab852017-06-23 15:42:00 +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 }
Anthony Barbier06ea0482018-02-22 15:45:35 +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 Barbierdbdab852017-06-23 15:42:00 +0100100 else
101 {
Anthony Barbier06ea0482018-02-22 15:45:35 +0000102 ARM_COMPUTE_RETURN_ON_ERROR(NETransposeKernel::validate(input, output));
Anthony Barbierdbdab852017-06-23 15:42:00 +0100103 }
104 }
Anthony Barbier06ea0482018-02-22 15:45:35 +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 Barbierdbdab852017-06-23 15:42:00 +0100114}
115
116void NEFullyConnectedLayerReshapeWeights::run()
117{
Kaizen8938bd32017-09-28 14:38:23 +0100118 _memory_group.acquire();
119
Anthony Barbierdbdab852017-06-23 15:42:00 +0100120 if(_transpose_weights)
121 {
122 NEScheduler::get().schedule(&_transpose_kernel, Window::DimY);
123 }
Kaizen8938bd32017-09-28 14:38:23 +0100124
Anthony Barbierdbdab852017-06-23 15:42:00 +0100125 if(_is_batched_fc_layer)
126 {
127 NEScheduler::get().schedule(&_transpose1xW_kernel, Window::DimY);
128 }
Kaizen8938bd32017-09-28 14:38:23 +0100129
130 _memory_group.release();
Anthony Barbierdbdab852017-06-23 15:42:00 +0100131}
132
Kaizen8938bd32017-09-28 14:38:23 +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(),
135 _reshape_weights_output(), _are_weights_reshaped(false), _is_batched_fc_layer(false), _linearize_input(false), _accumulate_biases(false)
Anthony Barbier871448e2017-03-24 14:54:29 +0000136{
137}
138
Anthony Barbierdbdab852017-06-23 15:42:00 +0100139void NEFullyConnectedLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose_weights, bool are_weights_reshaped)
Anthony Barbiera4376382017-04-12 15:12:46 +0100140{
Anthony Barbiera4376382017-04-12 15:12:46 +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
Anthony Barbier871448e2017-03-24 14:54:29 +0000146
Kaizen8938bd32017-09-28 14:38:23 +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)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000152 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Anthony Barbiera4376382017-04-12 15:12:46 +0100153
Anthony Barbier06ea0482018-02-22 15:45:35 +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 Barbierdbdab852017-06-23 15:42:00 +0100161
Anthony Barbier06ea0482018-02-22 15:45:35 +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);
Kaizen8938bd32017-09-28 14:38:23 +0100165
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000166 _linearize_input = (input->info()->tensor_shape().x() != linear_input_size) || (num_input_dimensions > 1 && linear_input_size == 1);
Kaizen8938bd32017-09-28 14:38:23 +0100167 _are_weights_reshaped = are_weights_reshaped;
168 _accumulate_biases = biases != nullptr;
169 _is_batched_fc_layer = num_batch_dimensions > 0;
170
Kaizen8938bd32017-09-28 14:38:23 +0100171 const size_t interleave_width = 16 / input->info()->element_size();
172 const ITensor *weights_to_use = weights;
173
174 if(!are_weights_reshaped && (transpose_weights || _is_batched_fc_layer))
Anthony Barbier871448e2017-03-24 14:54:29 +0000175 {
Kaizen8938bd32017-09-28 14:38:23 +0100176 weights_to_use = &_reshape_weights_output;
177
Anthony Barbier06ea0482018-02-22 15:45:35 +0000178 _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(),
179 transpose_weights,
180 _is_batched_fc_layer, interleave_width)));
Kaizen8938bd32017-09-28 14:38:23 +0100181
182 // Reshape the weights
183 _reshape_weights_kernel.configure(weights, &_reshape_weights_output, transpose_weights, _is_batched_fc_layer);
184 }
185
Kaizen8938bd32017-09-28 14:38:23 +0100186 const ITensor *multiply_input = input;
187
188 if(_linearize_input)
189 {
Anthony Barbier06ea0482018-02-22 15:45:35 +0000190 _im2col_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_im2col_shape(input->info(), num_input_dimensions)));
Kaizen8938bd32017-09-28 14:38:23 +0100191
192 // Configure im2col kernel
193 _memory_group.manage(&_im2col_output);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000194 _im2col_kernel.configure(input, &_im2col_output, Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false, true);
Kaizen8938bd32017-09-28 14:38:23 +0100195
196 multiply_input = &_im2col_output;
Anthony Barbierdbdab852017-06-23 15:42:00 +0100197 }
198
Anthony Barbier06ea0482018-02-22 15:45:35 +0000199 int m = multiply_input->info()->dimension(1);
200 int k = multiply_input->info()->dimension(0);
201
Anthony Barbierdbdab852017-06-23 15:42:00 +0100202 if(_is_batched_fc_layer)
203 {
Anthony Barbier06ea0482018-02-22 15:45:35 +0000204 _interleave4x4_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_interleaved_shape(*multiply_input->info())));
Anthony Barbierdbdab852017-06-23 15:42:00 +0100205
Kaizen8938bd32017-09-28 14:38:23 +0100206 // Configure interleave4x4 kernel
207 _memory_group.manage(&_interleave4x4_output);
208 _interleave4x4_kernel.configure(multiply_input, &_interleave4x4_output);
209
210 multiply_input = &_interleave4x4_output;
Anthony Barbiera4376382017-04-12 15:12:46 +0100211 }
Anthony Barbiera4376382017-04-12 15:12:46 +0100212
Kaizen8938bd32017-09-28 14:38:23 +0100213 // Configure matrix multiply kernel
Anthony Barbier06ea0482018-02-22 15:45:35 +0000214 _mm_kernel.configure(multiply_input, weights_to_use, output, 1.0f, _is_batched_fc_layer, GEMMReshapeInfo(m, 0 /* no transpose */, k));
Kaizen8938bd32017-09-28 14:38:23 +0100215
216 if(_accumulate_biases)
217 {
Kaizen8938bd32017-09-28 14:38:23 +0100218 // Configure accumulate biases kernel
219 _accumulate_biases_kernel.configure(output, biases);
Anthony Barbiera4376382017-04-12 15:12:46 +0100220 }
221
Anthony Barbierdbdab852017-06-23 15:42:00 +0100222 // Allocate the transpose tensor if the are_weights_reshaped flag is false and once all the configure methods have been called
Kaizen8938bd32017-09-28 14:38:23 +0100223 if(!are_weights_reshaped && (transpose_weights || _is_batched_fc_layer))
Anthony Barbiera4376382017-04-12 15:12:46 +0100224 {
Kaizen8938bd32017-09-28 14:38:23 +0100225 // Allocate the tensor for the weights reshaped
226 _reshape_weights_output.allocator()->allocate();
227 }
228
229 if(_linearize_input)
230 {
231 _im2col_output.allocator()->allocate();
232 }
233
234 if(_is_batched_fc_layer)
235 {
236 _interleave4x4_output.allocator()->allocate();
Anthony Barbier871448e2017-03-24 14:54:29 +0000237 }
238}
239
Anthony Barbier06ea0482018-02-22 15:45:35 +0000240Status NEFullyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, bool transpose_weights, bool are_weights_reshaped)
241{
242 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
243 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
244 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, weights, output);
245
246 const int num_batch_dimensions = std::max(0, static_cast<int>(output->tensor_shape().num_dimensions()) - 1);
247 const int num_input_dimensions = input->tensor_shape().num_dimensions() - num_batch_dimensions;
248 const size_t linear_input_size = input->tensor_shape().total_size_lower(num_input_dimensions);
249
250 const bool linearize_input = (input->tensor_shape().x() != linear_input_size) || (num_input_dimensions > 1 && linear_input_size == 1);
251 const bool accumulate_biases = biases != nullptr;
252 const bool is_batched_fc_layer = num_batch_dimensions > 0;
253
254 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().total_size_upper(num_input_dimensions) != output->tensor_shape().total_size_upper(1));
255 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 2);
256
257 const size_t interleave_width = 16 / input->element_size();
258 const ITensorInfo *weights_to_use = weights;
259 std::unique_ptr<ITensorInfo> reshape_weights_output = input->clone();
260
261 if(!are_weights_reshaped && (transpose_weights || is_batched_fc_layer))
262 {
263 reshape_weights_output->set_tensor_shape(compute_fully_connected_reshaped_weights_shape(weights, transpose_weights, is_batched_fc_layer, interleave_width));
264
265 ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayerReshapeWeights::validate(weights, reshape_weights_output.get(), transpose_weights, is_batched_fc_layer));
266
267 weights_to_use = reshape_weights_output.get();
268 }
269
270 // Check correct shape of weights
271 if(is_batched_fc_layer)
272 {
273 // Transpose + Transpose1xW
274 ARM_COMPUTE_RETURN_ERROR_ON(weights_to_use->tensor_shape().x() != linear_input_size * interleave_width);
275 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)));
276 }
277 else
278 {
279 // Transpose
280 ARM_COMPUTE_RETURN_ERROR_ON(weights_to_use->tensor_shape().x() != output->tensor_shape().x());
281 ARM_COMPUTE_RETURN_ERROR_ON(weights_to_use->tensor_shape().y() != linear_input_size);
282 }
283
284 const ITensorInfo *multiply_input = input;
285 std::unique_ptr<ITensorInfo> im2col_output = input->clone();
286 std::unique_ptr<ITensorInfo> interleave4x4_output = input->clone();
287
288 if(linearize_input)
289 {
290 im2col_output->set_tensor_shape(compute_im2col_shape(input, num_input_dimensions));
291
292 ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, im2col_output.get(), Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false, true));
293
294 multiply_input = im2col_output.get();
295 }
296
297 int m = multiply_input->dimension(1);
298 int k = multiply_input->dimension(0);
299
300 if(is_batched_fc_layer)
301 {
302 interleave4x4_output->set_tensor_shape(compute_interleaved_shape(*multiply_input));
303
304 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(multiply_input, interleave4x4_output.get()));
305
306 multiply_input = interleave4x4_output.get();
307 }
308
309 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)));
310
311 if(accumulate_biases)
312 {
313 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
314 ARM_COMPUTE_RETURN_ERROR_ON(biases->tensor_shape().x() != output->tensor_shape().x());
315
316 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixAccumulateBiasesKernel::validate(output, biases));
317 }
318
319 return Status{};
320}
321
Anthony Barbier871448e2017-03-24 14:54:29 +0000322void NEFullyConnectedLayer::run()
323{
Anthony Barbiera4376382017-04-12 15:12:46 +0100324 // Reshape of the weights (happens only once)
Anthony Barbierdbdab852017-06-23 15:42:00 +0100325 if(!_are_weights_reshaped)
Anthony Barbiera4376382017-04-12 15:12:46 +0100326 {
Anthony Barbierdbdab852017-06-23 15:42:00 +0100327 _are_weights_reshaped = true;
328 _reshape_weights_kernel.run();
Anthony Barbiera4376382017-04-12 15:12:46 +0100329 }
330
Kaizen8938bd32017-09-28 14:38:23 +0100331 _memory_group.acquire();
332
333 // Linearize input if it comes from a convolutional layer
334 if(_linearize_input)
Anthony Barbiera4376382017-04-12 15:12:46 +0100335 {
Anthony Barbierdbdab852017-06-23 15:42:00 +0100336 NEScheduler::get().schedule(&_im2col_kernel, Window::DimY);
Anthony Barbiera4376382017-04-12 15:12:46 +0100337 }
338
339 // Interleave input
Anthony Barbierdbdab852017-06-23 15:42:00 +0100340 if(_is_batched_fc_layer)
Anthony Barbiera4376382017-04-12 15:12:46 +0100341 {
Anthony Barbierdbdab852017-06-23 15:42:00 +0100342 NEScheduler::get().schedule(&_interleave4x4_kernel, Window::DimY);
Anthony Barbiera4376382017-04-12 15:12:46 +0100343 }
344
345 // Run matrix multiply
Anthony Barbierdbdab852017-06-23 15:42:00 +0100346 NEScheduler::get().schedule(&_mm_kernel, _is_batched_fc_layer ? Window::DimY : Window::DimX);
Anthony Barbiera4376382017-04-12 15:12:46 +0100347
348 // Accumulate biases if provided
349 if(_accumulate_biases)
350 {
Anthony Barbierdbdab852017-06-23 15:42:00 +0100351 NEScheduler::get().schedule(&_accumulate_biases_kernel, Window::DimY);
Anthony Barbiera4376382017-04-12 15:12:46 +0100352 }
Kaizen8938bd32017-09-28 14:38:23 +0100353
354 _memory_group.release();
Anthony Barbier871448e2017-03-24 14:54:29 +0000355}