blob: 4c264e48323aaa174a80e6ecbf602ec2ff67869b [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgio807ce592020-01-03 14:39:37 +00002 * Copyright (c) 2017-2020 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"
Giorgio Arenaa855af12018-07-16 17:20:38 +010030#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/runtime/NEON/NEScheduler.h"
32
33#include <algorithm>
34#include <cmath>
35
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +000036namespace arm_compute
37{
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000038using namespace arm_compute::misc::shape_calculator;
39
Giorgio Arenaa855af12018-07-16 17:20:38 +010040namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041{
Giorgio Arenaa855af12018-07-16 17:20:38 +010042Status validate_mm(const ITensorInfo &input, const ITensorInfo &weights, const ITensorInfo &output)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043{
Giorgio Arenaa855af12018-07-16 17:20:38 +010044 if(is_data_type_quantized_asymmetric(input.data_type()))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045 {
Giorgio Arenaa855af12018-07-16 17:20:38 +010046 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
47 // Extract and negate input and weights offset
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010048 const QuantizationInfo input_quantization_info(input.quantization_info().uniform().scale, -input.quantization_info().uniform().offset);
49 const QuantizationInfo weights_quantization_info(weights.quantization_info().uniform().scale, -weights.quantization_info().uniform().offset);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050
Giorgio Arenaa855af12018-07-16 17:20:38 +010051 // Validate gemmlowp function
52 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyCore::validate(&input.clone()->set_quantization_info(input_quantization_info),
53 &weights.clone()->set_quantization_info(weights_quantization_info),
Gian Marco Iodice4b908652018-10-18 10:21:02 +010054 nullptr,
Giorgio Arenaa855af12018-07-16 17:20:38 +010055 &output));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056 }
57 else
58 {
Giorgio Arenaa855af12018-07-16 17:20:38 +010059 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMM::validate(&input, &weights, nullptr, &output, 1.f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run */)));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000060 }
61
62 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063}
Giorgio Arenaa855af12018-07-16 17:20:38 +010064} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065
Giorgio Arenaa855af12018-07-16 17:20:38 +010066void NEFullyConnectedLayerReshapeWeights::configure(const ITensor *input, ITensor *output)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067{
Giorgio Arenaa855af12018-07-16 17:20:38 +010068 auto k = arm_compute::support::cpp14::make_unique<NETransposeKernel>();
69 k->configure(input, output);
70 _kernel = std::move(k);
71}
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010072
Giorgio Arenaa855af12018-07-16 17:20:38 +010073Status NEFullyConnectedLayerReshapeWeights::validate(const ITensorInfo *input, const ITensorInfo *output)
74{
75 return NETransposeKernel::validate(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076}
77
Michalis Spyrou1a569a32019-09-10 17:20:34 +010078NEFullyConnectedLayer::NEFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
79 : _memory_group(std::move(memory_manager)), _weights_manager(weights_manager), _flatten_kernel(), _convert_weights(), _convert_weights_managed(), _reshape_weights_function(),
80 _reshape_weights_managed_function(), _mm_gemm(nullptr, weights_manager), _mm_gemmlowp(), _gemmlowp_output_stage(), _accumulate_biases_kernel(), _flatten_output(), _gemmlowp_output(),
81 _converted_weights_output(), _reshape_weights_output(), _original_weights(nullptr), _are_weights_converted(true), _are_weights_reshaped(false), _is_fc_after_conv(false), _accumulate_biases(false),
82 _is_quantized(false), _is_prepared(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083{
84}
85
Giorgio Arenaa855af12018-07-16 17:20:38 +010086void NEFullyConnectedLayer::configure_mm(const ITensor *input, const ITensor *weights, ITensor *output)
87{
88 if(_is_quantized)
89 {
90 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
91 // Extract and negate input and weights offset
92 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
93 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
94
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010095 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset));
96 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset));
Giorgio Arenaa855af12018-07-16 17:20:38 +010097
98 // Configure gemmlowp function
Gian Marco Iodice4b908652018-10-18 10:21:02 +010099 _mm_gemmlowp.configure(input, weights, nullptr, output);
Giorgio Arenaa855af12018-07-16 17:20:38 +0100100
101 // Revert back QuantizatioInfo as input and weights could be used in other fully connected layers
102 input->info()->set_quantization_info(input_quantization_info);
103 weights->info()->set_quantization_info(weights_quantization_info);
104 }
105 else
106 {
107 // Configure matrix multiply kernel
108 _mm_gemm.configure(input, weights, nullptr, output, 1.f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run */));
109 }
110}
111
112void NEFullyConnectedLayer::configure_conv_fc(const ITensor *input, const ITensor *weights, ITensor *output)
113{
114 ARM_COMPUTE_ERROR_ON((weights->info()->dimension(1) != (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2))));
115
116 // If the fully connected layer is called after a convolution layer, the input tensor must be linearized
117
Giorgio Arena368e6352018-08-20 15:06:07 +0100118 // Initialize output tensor for flatten
119 TensorShape shape_flatten = compute_flatten_shape(input->info());
120 _flatten_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_flatten));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100121
Giorgio Arena368e6352018-08-20 15:06:07 +0100122 // Configure flatten kernel
123 _memory_group.manage(&_flatten_output);
124 _flatten_kernel.configure(input, &_flatten_output);
Giorgio Arenaa855af12018-07-16 17:20:38 +0100125
126 // Configure matrix multiply kernel
Giorgio Arena368e6352018-08-20 15:06:07 +0100127 configure_mm(&_flatten_output, weights, output);
Giorgio Arenaa855af12018-07-16 17:20:38 +0100128
Giorgio Arena368e6352018-08-20 15:06:07 +0100129 // Allocate the output tensor for flatten once all the configure methods have been called
130 _flatten_output.allocator()->allocate();
Giorgio Arenaa855af12018-07-16 17:20:38 +0100131}
132
133void NEFullyConnectedLayer::configure_fc_fc(const ITensor *input, const ITensor *weights, ITensor *output)
134{
135 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != weights->info()->dimension(1));
136
137 // Configure matrix multiply kernel
138 configure_mm(input, weights, output);
139}
140
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100141void NEFullyConnectedLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output,
142 FullyConnectedLayerInfo fc_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143{
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000144 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000146 // Perform validate step
147 ARM_COMPUTE_ERROR_THROW_ON(NEFullyConnectedLayer::validate(input->info(),
148 weights->info(),
149 biases != nullptr ? biases->info() : nullptr,
150 output->info(),
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100151 fc_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152
Georgios Pinitasef776a82018-07-25 17:57:49 +0100153 _are_weights_converted = true;
154 _are_weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true;
155 _is_fc_after_conv = true;
156 _accumulate_biases = false;
157 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
158 _original_weights = weights;
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100159
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100160 if(_weights_manager)
161 {
162 _weights_manager->manage(weights);
163 }
164
Giorgio Arenaa855af12018-07-16 17:20:38 +0100165 // Configure gemmlowp output
166 if(_is_quantized)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100168 _gemmlowp_output.allocator()->init(output->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(DataType::S32));
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100169 }
170
Giorgio Arenaa855af12018-07-16 17:20:38 +0100171 // Configure accumulate biases kernel for non quantized asymmetric types
172 if(biases != nullptr && !_is_quantized)
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100173 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100174 _accumulate_biases = true;
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100175
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100176 // Configure accumulate biases kernel
177 _accumulate_biases_kernel.configure(output, biases);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178 }
179
Giorgio Arenaa855af12018-07-16 17:20:38 +0100180 // With the Fully Connected layer we can have 4 different cases:
181 // 1) Convolution layer -> Fully Connected layer without batches
182 // 2) Fully Connected layer -> Fully Connected layer without batches
183 // 3) Convolution layer -> Fully Connected layer with batches
184 // 4) Fully Connected layer -> Fully Connected layer with batches
185
186 const ITensor *weights_to_use = weights;
187
Giorgio Arenaa855af12018-07-16 17:20:38 +0100188 // Check if we have a fully connected layer with batches
189 const bool is_batched_fc_layer = output->info()->dimension(1) > 1;
Giorgio Arenaa855af12018-07-16 17:20:38 +0100190 if(is_batched_fc_layer)
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100191 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100192 _is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->info()->tensor_shape().cbegin() + 3,
193 input->info()->tensor_shape().cend(),
194 output->info()->tensor_shape().cbegin() + 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100195 }
Giorgio Arenaa855af12018-07-16 17:20:38 +0100196 else
197 {
198 _is_fc_after_conv = input->info()->num_dimensions() > 1;
199 }
200
Georgios Pinitasef776a82018-07-25 17:57:49 +0100201 // Reshape weights if needed
202 if(!_are_weights_reshaped)
203 {
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100204 if(_weights_manager && _weights_manager->are_weights_managed(weights))
205 {
206 _reshape_weights_managed_function.configure(weights);
207 weights_to_use = _weights_manager->acquire(weights, &_reshape_weights_managed_function);
208 }
209 else
210 {
211 // Reshape the weights
212 _reshape_weights_function.configure(weights, &_reshape_weights_output);
213 weights_to_use = &_reshape_weights_output;
214 }
Georgios Pinitasef776a82018-07-25 17:57:49 +0100215 }
216
217 // Convert weights if needed
218 if(_is_fc_after_conv && (input->info()->data_layout() != fc_info.weights_trained_layout))
219 {
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100220 if(_weights_manager && _weights_manager->are_weights_managed(weights_to_use))
221 {
222 _convert_weights_managed.configure(weights_to_use,
223 input->info()->tensor_shape(),
224 fc_info.weights_trained_layout);
225 weights_to_use = _weights_manager->acquire(weights, &_convert_weights_managed);
226 }
227 else
228 {
229 // Convert weights
230 _convert_weights.configure(weights_to_use,
231 &_converted_weights_output,
232 input->info()->tensor_shape(),
233 fc_info.weights_trained_layout);
Georgios Pinitasef776a82018-07-25 17:57:49 +0100234
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100235 weights_to_use = &_converted_weights_output;
236 }
Georgios Pinitasef776a82018-07-25 17:57:49 +0100237 _are_weights_converted = false;
238 }
239
Giorgio Arenaa855af12018-07-16 17:20:38 +0100240 ITensor *tmp_output = (_is_quantized) ? &_gemmlowp_output : output;
241 if(_is_fc_after_conv)
242 {
243 // Fully Connected layer after a Convolution Layer without batches
244 configure_conv_fc(input, weights_to_use, tmp_output);
245 }
246 else
247 {
248 // Fully Connected layer after a Fully Connected Layer without batches
249 configure_fc_fc(input, weights_to_use, tmp_output);
250 }
251
252 // Configure output stage for asymmetric quantized types
253 if(_is_quantized)
254 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100255 const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform();
256 const UniformQuantizationInfo wq_info = weights->info()->quantization_info().uniform();
257 const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform();
258
Michalis Spyroue7be8a02019-12-12 16:16:09 +0000259 float multiplier = (iq_info.scale * wq_info.scale) / oq_info.scale;
260 int32_t output_multiplier;
261 int32_t output_shift;
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000262 quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100263 _gemmlowp_output_stage.configure(&_gemmlowp_output, biases, output, output_multiplier, output_shift, oq_info.offset);
Giorgio Arenaa855af12018-07-16 17:20:38 +0100264 _gemmlowp_output.allocator()->allocate();
265 }
266
267 _are_weights_reshaped = _are_weights_reshaped || fc_info.retain_internal_weights;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100268}
269
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100270Status NEFullyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
271 FullyConnectedLayerInfo fc_info)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000272{
Giorgio Arenaa855af12018-07-16 17:20:38 +0100273 ARM_COMPUTE_UNUSED(fc_info.retain_internal_weights);
274 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
275 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000276 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000277 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 2);
278
Giorgio Arenaa855af12018-07-16 17:20:38 +0100279 bool weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true;
280 bool is_fc_after_conv = true;
281 bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000282
Giorgio Arena368e6352018-08-20 15:06:07 +0100283 const ITensorInfo &flatten_input = TensorInfo(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_flatten_shape(input)));
Georgios Pinitasef776a82018-07-25 17:57:49 +0100284 const ITensorInfo &reshaped_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_transposed_shape(*weights)));
Georgios Pinitas195b0ba2018-08-02 17:18:51 +0100285 const ITensorInfo &converted_weights = weights_reshaped ? TensorInfo(weights->clone()->set_is_resizable(true).reset_padding()) : TensorInfo(*reshaped_weights.clone());
Georgios Pinitasef776a82018-07-25 17:57:49 +0100286 const ITensorInfo &gemmlowp_output = TensorInfo(output->clone()->set_is_resizable(true).reset_padding().set_data_type(DataType::S32));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100287
288 // Configure accumulate biases kernel for non quantized asymmetric types
289 if(biases != nullptr && !is_quantized)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000290 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100291 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
292 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixAccumulateBiasesKernel::validate(output, biases));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000293 }
294
Giorgio Arenaa855af12018-07-16 17:20:38 +0100295 // With the Fully Connected layer we can have 4 different cases:
296 // 1) Convolution layer -> Fully Connected layer without batches
297 // 2) Fully Connected layer -> Fully Connected layer without batches
298 // 3) Convolution layer -> Fully Connected layer with batches
299 // 4) Fully Connected layer -> Fully Connected layer with batches
300
301 const ITensorInfo *input_to_use = input;
302 const ITensorInfo *weights_to_use = weights;
303 const ITensorInfo *tmp_output = (is_quantized) ? &gemmlowp_output : output;
304
Giorgio Arenaa855af12018-07-16 17:20:38 +0100305 // Check if we have a fully connected layer with batches
306 const bool is_batched_fc_layer = output->dimension(1) > 1;
307
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000308 if(is_batched_fc_layer)
309 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100310 is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->tensor_shape().cbegin() + 3,
311 input->tensor_shape().cend(),
312 output->tensor_shape().cbegin() + 1));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000313 }
314 else
315 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100316 is_fc_after_conv = input->num_dimensions() > 1;
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000317 }
318
Georgios Pinitasef776a82018-07-25 17:57:49 +0100319 if(!weights_reshaped)
320 {
321 // Validate reshape weights kernel
322 ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayerReshapeWeights::validate(weights, &reshaped_weights));
323 weights_to_use = &reshaped_weights;
324 }
325
326 if(is_fc_after_conv && (input->data_layout() != fc_info.weights_trained_layout))
327 {
328 // Validate convert weights kernel
329 ARM_COMPUTE_RETURN_ON_ERROR(NEConvertFullyConnectedWeights::validate(weights_to_use,
330 &converted_weights,
331 input->tensor_shape(),
332 fc_info.weights_trained_layout));
333 weights_to_use = &converted_weights;
334 }
335
Giorgio Arenaa855af12018-07-16 17:20:38 +0100336 if(is_fc_after_conv)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000337 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100338 // Fully Connected layer after a Convolution Layer without batches
339 ARM_COMPUTE_RETURN_ERROR_ON((weights_to_use->dimension(1) != (input->dimension(0) * input->dimension(1) * input->dimension(2))));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000340
Giorgio Arena368e6352018-08-20 15:06:07 +0100341 // Validate flatten kernel
342 ARM_COMPUTE_RETURN_ON_ERROR(NEFlattenLayerKernel::validate(input, &flatten_input));
343 input_to_use = &flatten_input;
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000344 }
Giorgio Arenaa855af12018-07-16 17:20:38 +0100345 else
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000346 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100347 // Fully Connected layer after a Fully Connected Layer without batches
348 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != weights_to_use->dimension(1));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000349 }
Giorgio Arenaa855af12018-07-16 17:20:38 +0100350 // Validate matrix multiply kernel
351 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(*input_to_use, *weights_to_use, *tmp_output));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000352
Giorgio Arenaa855af12018-07-16 17:20:38 +0100353 // Validate output stage for asymmetric quantized types
354 if(is_quantized)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000355 {
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000356 const UniformQuantizationInfo iq_info = input->quantization_info().uniform();
357 const UniformQuantizationInfo wq_info = weights->quantization_info().uniform();
358 const UniformQuantizationInfo oq_info = output->quantization_info().uniform();
Gian Marco Iodiceec8cce82019-08-21 17:01:53 +0100359
Michele Di Giorgio807ce592020-01-03 14:39:37 +0000360 float multiplier = (iq_info.scale * wq_info.scale) / oq_info.scale;
361 int32_t output_multiplier;
362 int32_t output_shift;
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000363 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100364 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(&gemmlowp_output, biases, output));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000365 }
366
367 return Status{};
368}
369
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100370void NEFullyConnectedLayer::run()
371{
Georgios Pinitas72219332018-06-05 14:56:06 +0100372 prepare();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100373
Georgios Pinitasda953f22019-04-02 17:27:03 +0100374 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100375
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100376 // Linearize input if it comes from a convolutional layer
Giorgio Arenaa855af12018-07-16 17:20:38 +0100377 if(_is_fc_after_conv)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100378 {
Giorgio Arena368e6352018-08-20 15:06:07 +0100379 NEScheduler::get().schedule(&_flatten_kernel, Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100380 }
381
Giorgio Arenaa855af12018-07-16 17:20:38 +0100382 // Run matrix multiply
383 if(_is_quantized)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100384 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100385 _mm_gemmlowp.run();
386 }
387 else
388 {
389 _mm_gemm.run();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100390 }
391
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100392 // Accumulate biases if provided
Giorgio Arenaa855af12018-07-16 17:20:38 +0100393 if(_is_quantized)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100394 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100395 _gemmlowp_output_stage.run();
396 }
397 else
398 {
399 if(_accumulate_biases)
400 {
401 NEScheduler::get().schedule(&_accumulate_biases_kernel, Window::DimY);
402 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100403 }
404}
Georgios Pinitas72219332018-06-05 14:56:06 +0100405
406void NEFullyConnectedLayer::prepare()
407{
Georgios Pinitas72219332018-06-05 14:56:06 +0100408 if(!_is_prepared)
409 {
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100410 if(!_weights_manager)
411 {
412 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
413 }
Georgios Pinitasef776a82018-07-25 17:57:49 +0100414
415 auto release_unused = [](Tensor * w)
416 {
417 if(!w->is_used())
418 {
419 w->allocator()->free();
420 }
421 };
422
423 // Pointer to current weights
424 const ITensor *cur_weights = _original_weights;
425
Giorgio Arenaa855af12018-07-16 17:20:38 +0100426 // Reshape of the weights (happens only once)
427 if(!_are_weights_reshaped)
428 {
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100429 if(_weights_manager && _weights_manager->are_weights_managed(_original_weights))
430 {
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100431 cur_weights = _weights_manager->run(cur_weights, &_reshape_weights_managed_function);
432 }
433 else
434 {
435 // Reshape of the weights (happens only once)
436 if(!_are_weights_reshaped)
437 {
438 // Run reshape weights kernel and mark weights as unused
439 _reshape_weights_output.allocator()->allocate();
440 _reshape_weights_function.run();
441 }
442 cur_weights->mark_as_unused();
443 cur_weights = &_reshape_weights_output;
444 }
Giorgio Arenaa855af12018-07-16 17:20:38 +0100445 _are_weights_reshaped = true;
446 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100447
Georgios Pinitasef776a82018-07-25 17:57:49 +0100448 // Convert weights if needed (happens only once)
449 if(!_are_weights_converted)
450 {
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100451 if(_weights_manager && _weights_manager->are_weights_managed(cur_weights))
452 {
453 _weights_manager->run(cur_weights, &_convert_weights_managed);
454 }
455 else
456 {
457 _converted_weights_output.allocator()->allocate();
458 _convert_weights.run();
Michalis Spyrou20c2b502019-10-01 15:39:42 +0100459 cur_weights->mark_as_unused();
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100460 }
Georgios Pinitasef776a82018-07-25 17:57:49 +0100461
Georgios Pinitasef776a82018-07-25 17:57:49 +0100462 _are_weights_converted = true;
463 }
464
465 // Release reshaped weights if unused
466 release_unused(&_reshape_weights_output);
467
468 // Prepare GEMM prepare and release unused weights
469 if(!_is_quantized)
470 {
471 _mm_gemm.prepare();
472 }
473
474 // Release converted weights if unused
475 release_unused(&_reshape_weights_output);
476 release_unused(&_converted_weights_output);
477
Georgios Pinitas72219332018-06-05 14:56:06 +0100478 _is_prepared = true;
479 }
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000480}
481} // namespace arm_compute