blob: 5bcf38d1c4bcc070a4570ede2842c08195608cbf [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitasda953f22019-04-02 17:27:03 +01002 * Copyright (c) 2017-2019 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/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"
Michalis Spyroub27e13a2019-09-27 11:04:27 +010028#include "arm_compute/core/utils/misc/Cast.h"
Georgios Pinitas358ca202017-12-07 16:47:52 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000030#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/runtime/CL/CLScheduler.h"
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010032#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033
34#include <algorithm>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
Michalis Spyroub27e13a2019-09-27 11:04:27 +010036namespace arm_compute
37{
Georgios Pinitas358ca202017-12-07 16:47:52 +000038using namespace arm_compute::misc::shape_calculator;
Michalis Spyroub27e13a2019-09-27 11:04:27 +010039using namespace arm_compute::utils::cast;
Georgios Pinitas358ca202017-12-07 16:47:52 +000040
41namespace
42{
Georgios Pinitas8b721992019-10-28 16:24:28 +000043Status construct_gemmlowp_output_stage(const ITensorInfo &input, const ITensorInfo &weights, const ITensorInfo &output,
44 GEMMLowpOutputStageInfo &gemmlowp_output_stage)
Georgios Pinitas358ca202017-12-07 16:47:52 +000045{
Georgios Pinitas8b721992019-10-28 16:24:28 +000046 gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
47 gemmlowp_output_stage.gemmlowp_offset = 0;
48 gemmlowp_output_stage.gemmlowp_multiplier = 0;
49 gemmlowp_output_stage.gemmlowp_shift = 0;
50
51 // Configure output stage for quantized case
52 if(is_data_type_quantized_asymmetric(input.data_type()))
53 {
54 const UniformQuantizationInfo iq_info = input.quantization_info().uniform();
55 const UniformQuantizationInfo wq_info = weights.quantization_info().uniform();
56 const UniformQuantizationInfo oq_info = output.quantization_info().uniform();
57
58 const auto output_quant_info = (output.total_size() == 0) ? iq_info : oq_info;
59
60 const float multiplier = (iq_info.scale * wq_info.scale) / output_quant_info.scale;
61 int output_multiplier = 0;
62 int output_shift = 0;
63 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift));
64
65 // Set the GEMMLowp output stage info
66 gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset;
67 gemmlowp_output_stage.gemmlowp_multiplier = output_multiplier;
68 gemmlowp_output_stage.gemmlowp_shift = output_shift;
69 gemmlowp_output_stage.gemmlowp_min_bound = 0;
70 gemmlowp_output_stage.gemmlowp_max_bound = 255;
71 }
72
73 return Status{};
74}
75
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +000076Status validate_mm(const ITensorInfo &input, const ITensorInfo &weights, const ITensorInfo *bias, const ITensorInfo &output, const FullyConnectedLayerInfo &fc_info)
Georgios Pinitas8b721992019-10-28 16:24:28 +000077{
78 GEMMLowpOutputStageInfo gemmlowp_output_stage;
79 ARM_COMPUTE_RETURN_ON_ERROR(construct_gemmlowp_output_stage(input, weights, output, gemmlowp_output_stage));
80
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +000081 const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped
82 false, // is_b_reshaped
83 true, // reshape_b_only_on_first_run
84 0, // depth_output_gemm3d
85 false, // reinterpret_input_as_3d
86 fc_info.retain_internal_weights, // retain_internal_weights
87 gemmlowp_output_stage, // gemmlowp_output_stage
88 fc_info.fp_mixed_precision, // fp_mixed_precision
89 true, // broadcast_bias
90 ActivationLayerInfo()); // activation_info
Georgios Pinitas8b721992019-10-28 16:24:28 +000091
Georgios Pinitas358ca202017-12-07 16:47:52 +000092 if(is_data_type_quantized_asymmetric(input.data_type()))
93 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010094 const UniformQuantizationInfo iq_info = input.quantization_info().uniform();
95 const UniformQuantizationInfo wq_info = weights.quantization_info().uniform();
96
Georgios Pinitas358ca202017-12-07 16:47:52 +000097 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
98 // Extract and negate input and weights offset
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010099 const QuantizationInfo input_quantization_info(iq_info.scale, -iq_info.offset);
100 const QuantizationInfo weights_quantization_info(wq_info.scale, -wq_info.offset);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000101
102 // Validate gemmlowp function
103 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyCore::validate(&input.clone()->set_quantization_info(input_quantization_info),
104 &weights.clone()->set_quantization_info(weights_quantization_info),
Georgios Pinitas8b721992019-10-28 16:24:28 +0000105 bias,
106 &output,
107 gemm_info));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000108 }
109 else
110 {
Georgios Pinitas8b721992019-10-28 16:24:28 +0000111 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(&input, &weights, bias, &output, 1.f, 1.f, gemm_info));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000112 }
113
114 return Status{};
115}
116} // namespace
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100117
118void CLFullyConnectedLayerReshapeWeights::configure(const ICLTensor *input, ICLTensor *output)
Moritz Pflanzer768e9f12017-08-11 15:33:30 +0100119{
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100120 auto k = arm_compute::support::cpp14::make_unique<CLTransposeKernel>();
121 k->configure(input, output);
122 _kernel = std::move(k);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123}
124
Georgios Pinitas358ca202017-12-07 16:47:52 +0000125Status CLFullyConnectedLayerReshapeWeights::validate(const ITensorInfo *input, const ITensorInfo *output)
126{
127 return CLTransposeKernel::validate(input, output);
128}
129
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100130CLFullyConnectedLayer::CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100131 : _memory_group(memory_manager), _weights_manager(weights_manager), _convert_weights(), _convert_weights_managed(), _reshape_weights_managed_function(), _flatten_layer(), _reshape_weights_function(),
Georgios Pinitas8b721992019-10-28 16:24:28 +0000132 _mm_gemm(memory_manager, weights_manager), _mm_gemmlowp(memory_manager), _flatten_output(), _converted_weights_output(), _reshape_weights_output(), _are_weights_converted(true),
133 _are_weights_reshaped(true), _is_fc_after_conv(true), _is_quantized(false), _is_prepared(false), _original_weights(nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134{
135}
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000136void CLFullyConnectedLayer::configure_mm(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info)
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000137{
Georgios Pinitas8b721992019-10-28 16:24:28 +0000138 GEMMLowpOutputStageInfo gemmlowp_output_stage;
139 construct_gemmlowp_output_stage(*input->info(), *weights->info(), *output->info(), gemmlowp_output_stage);
140
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000141 const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped
142 false, // is_b_reshaped
143 true, // reshape_b_only_on_first_run
144 0, // depth_output_gemm3d
145 false, // reinterpret_input_as_3d
146 fc_info.retain_internal_weights, // retain_internal_weights
147 gemmlowp_output_stage, // gemmlowp_output_stage
148 fc_info.fp_mixed_precision, // fp_mixed_precision
149 true, // broadcast_bias
150 ActivationLayerInfo()); // activation_info
Georgios Pinitas8b721992019-10-28 16:24:28 +0000151
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000152 if(_is_quantized)
153 {
Chunosov5124be52017-11-22 20:42:13 +0700154 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000155 // Extract and negate input and weights offset
Chunosov5124be52017-11-22 20:42:13 +0700156 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
157 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
158
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100159 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset));
160 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset));
Chunosov5124be52017-11-22 20:42:13 +0700161
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000162 // Configure gemmlowp function
Georgios Pinitas8b721992019-10-28 16:24:28 +0000163 _mm_gemmlowp.configure(input, weights, bias, output, gemm_info);
Chunosov5124be52017-11-22 20:42:13 +0700164
165 // Revert back QuantizatioInfo as input and weights could be used in other fully connected layers
166 input->info()->set_quantization_info(input_quantization_info);
167 weights->info()->set_quantization_info(weights_quantization_info);
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000168 }
169 else
170 {
171 // Configure matrix multiply kernel
Georgios Pinitas8b721992019-10-28 16:24:28 +0000172 _mm_gemm.configure(input, weights, bias, output, 1.f, 1.f, gemm_info);
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000173 }
174}
175
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000176void CLFullyConnectedLayer::configure_conv_fc(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info)
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100177{
178 ARM_COMPUTE_ERROR_ON((weights->info()->dimension(1) != (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2))));
179
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100180 // If the fully connected layer is called after a convolution layer, the input tensor must be linearized
181
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100182 // Initialize output tensor for flatten
183 TensorShape shape_flatten = compute_flatten_shape(input->info());
184 _flatten_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_flatten).set_data_layout(DataLayout::NCHW));
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100185
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100186 // Configure flatten kernel
187 _memory_group.manage(&_flatten_output);
188 _flatten_layer.configure(input, &_flatten_output);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100189
190 // Configure matrix multiply kernel
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000191 configure_mm(&_flatten_output, weights, bias, output, fc_info);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100192
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100193 // Allocate the output tensor for flatten once all the configure methods have been called
194 _flatten_output.allocator()->allocate();
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100195}
196
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000197void CLFullyConnectedLayer::configure_fc_fc(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info)
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100198{
199 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != weights->info()->dimension(1));
200
201 // Configure matrix multiply kernel
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000202 configure_mm(input, weights, bias, output, fc_info);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100203}
204
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100205void CLFullyConnectedLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
206 FullyConnectedLayerInfo fc_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100207{
Georgios Pinitas358ca202017-12-07 16:47:52 +0000208 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
209
210 // Perform validate step
211 ARM_COMPUTE_ERROR_THROW_ON(CLFullyConnectedLayer::validate(input->info(),
212 weights->info(),
213 biases != nullptr ? biases->info() : nullptr,
214 output->info(),
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100215 fc_info));
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100216
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100217 _are_weights_converted = true;
218 _are_weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true;
219 _is_fc_after_conv = true;
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100220 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Michele Di Giorgioba1ffe92018-08-22 14:28:30 +0100221 _is_prepared = fc_info.retain_internal_weights;
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100222 _original_weights = weights;
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100223
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100224 if(_weights_manager)
225 {
226 _weights_manager->manage(weights);
227 }
228
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100229 const ICLTensor *weights_to_use = weights;
230
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100231 // With the Fully Connected layer we can have 4 different cases:
232 // 1) Convolution layer -> Fully Connected layer without batches
233 // 2) Fully Connected layer -> Fully Connected layer without batches
234 // 3) Convolution layer -> Fully Connected layer with batches
235 // 4) Fully Connected layer -> Fully Connected layer with batches
236
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100237 // Check if we have a fully connected layer with batches
238 const bool is_batched_fc_layer = output->info()->dimension(1) > 1;
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100239 if(is_batched_fc_layer)
Moritz Pflanzer768e9f12017-08-11 15:33:30 +0100240 {
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100241 _is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->info()->tensor_shape().cbegin() + 3,
242 input->info()->tensor_shape().cend(),
243 output->info()->tensor_shape().cbegin() + 1));
Moritz Pflanzer768e9f12017-08-11 15:33:30 +0100244 }
245 else
246 {
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100247 _is_fc_after_conv = input->info()->num_dimensions() > 1;
Moritz Pflanzer768e9f12017-08-11 15:33:30 +0100248 }
249
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100250 // Reshape weights if needed
251 if(!_are_weights_reshaped)
252 {
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100253 if(_weights_manager && _weights_manager->are_weights_managed(weights))
254 {
255 _reshape_weights_managed_function.configure(weights);
256 weights_to_use = utils::cast::polymorphic_downcast<ICLTensor *>(_weights_manager->acquire(weights, &_reshape_weights_managed_function));
257 }
258 else
259 {
260 // Reshape the weights
261 _reshape_weights_function.configure(weights, &_reshape_weights_output);
262 weights_to_use = &_reshape_weights_output;
263 }
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100264 }
265
266 // Convert weights if needed
267 if(_is_fc_after_conv && (input->info()->data_layout() != fc_info.weights_trained_layout))
268 {
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100269 if(_weights_manager && _weights_manager->are_weights_managed(weights_to_use))
270 {
271 _convert_weights_managed.configure(weights_to_use,
272 input->info()->tensor_shape(),
273 fc_info.weights_trained_layout);
274 weights_to_use = utils::cast::polymorphic_downcast<ICLTensor *>(_weights_manager->acquire(weights, &_convert_weights_managed));
275 }
276 else
277 {
278 // Convert weights
279 _convert_weights.configure(weights_to_use,
280 &_converted_weights_output,
281 input->info()->tensor_shape(),
282 fc_info.weights_trained_layout);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100283
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100284 weights_to_use = &_converted_weights_output;
285 }
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100286 _are_weights_converted = false;
287 }
288
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100289 if(_is_fc_after_conv)
Moritz Pflanzer768e9f12017-08-11 15:33:30 +0100290 {
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100291 // Fully Connected layer after a Convolution Layer without batches
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000292 configure_conv_fc(input, weights_to_use, biases, output, fc_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100293 }
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100294 else
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100295 {
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100296 // Fully Connected layer after a Fully Connected Layer without batches
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000297 configure_fc_fc(input, weights_to_use, biases, output, fc_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100298 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100299}
300
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100301Status CLFullyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
302 FullyConnectedLayerInfo fc_info)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000303{
304 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100305 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000306 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
307 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 2);
308
Georgios Pinitas8b721992019-10-28 16:24:28 +0000309 bool weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true;
310 bool is_fc_after_conv = true;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000311
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100312 const ITensorInfo &flatten_input = TensorInfo(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_flatten_shape(input)).set_data_layout(DataLayout::NCHW));
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100313 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 +0100314 const ITensorInfo &converted_weights = weights_reshaped ? TensorInfo(weights->clone()->set_is_resizable(true).reset_padding()) : TensorInfo(*reshaped_weights.clone());
Georgios Pinitas358ca202017-12-07 16:47:52 +0000315
316 // With the Fully Connected layer we can have 4 different cases:
317 // 1) Convolution layer -> Fully Connected layer without batches
318 // 2) Fully Connected layer -> Fully Connected layer without batches
319 // 3) Convolution layer -> Fully Connected layer with batches
320 // 4) Fully Connected layer -> Fully Connected layer with batches
321
322 const ITensorInfo *input_to_use = input;
323 const ITensorInfo *weights_to_use = weights;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000324
Georgios Pinitas358ca202017-12-07 16:47:52 +0000325 // Check if we have a fully connected layer with batches
326 const bool is_batched_fc_layer = output->dimension(1) > 1;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000327 if(is_batched_fc_layer)
328 {
329 is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->tensor_shape().cbegin() + 3,
330 input->tensor_shape().cend(),
331 output->tensor_shape().cbegin() + 1));
332 }
333 else
334 {
335 is_fc_after_conv = input->num_dimensions() > 1;
336 }
337
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100338 if(!weights_reshaped)
339 {
340 // Validate reshape weights kernel
341 ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayerReshapeWeights::validate(weights, &reshaped_weights));
342 weights_to_use = &reshaped_weights;
343 }
344
345 if(is_fc_after_conv && (input->data_layout() != fc_info.weights_trained_layout))
346 {
347 // Validate convert weights kernel
348 ARM_COMPUTE_RETURN_ON_ERROR(CLConvertFullyConnectedWeights::validate(weights_to_use,
349 &converted_weights,
350 input->tensor_shape(),
351 fc_info.weights_trained_layout));
352 weights_to_use = &converted_weights;
353 }
354
Georgios Pinitas358ca202017-12-07 16:47:52 +0000355 if(is_fc_after_conv)
356 {
357 // Fully Connected layer after a Convolution Layer without batches
358 ARM_COMPUTE_RETURN_ERROR_ON((weights_to_use->dimension(1) != (input->dimension(0) * input->dimension(1) * input->dimension(2))));
359
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100360 // Validate flatten kernel
361 ARM_COMPUTE_RETURN_ON_ERROR(CLFlattenLayer::validate(input, &flatten_input));
362 input_to_use = &flatten_input;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000363 }
364 else
365 {
366 // Fully Connected layer after a Fully Connected Layer without batches
367 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != weights_to_use->dimension(1));
368 }
Georgios Pinitas8b721992019-10-28 16:24:28 +0000369
Georgios Pinitas358ca202017-12-07 16:47:52 +0000370 // Validate matrix multiply kernel
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000371 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(*input_to_use, *weights_to_use, biases, *output, fc_info));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000372
373 return Status{};
374}
375
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100376void CLFullyConnectedLayer::run()
377{
Georgios Pinitase0437672018-05-02 14:07:55 +0100378 prepare();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100379
Georgios Pinitasda953f22019-04-02 17:27:03 +0100380 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100381
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100382 // Linearize input if it comes from a convolutional layer
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100383 if(_is_fc_after_conv)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100384 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100385 _flatten_layer.run();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100386 }
387
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100388 // Run matrix multiply
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000389 if(_is_quantized)
390 {
391 _mm_gemmlowp.run();
392 }
393 else
394 {
Gian Marco Iodicec9c62c22018-04-06 10:00:10 +0100395 _mm_gemm.run();
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000396 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100397}
Georgios Pinitase0437672018-05-02 14:07:55 +0100398
399void CLFullyConnectedLayer::prepare()
400{
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100401 if(!_is_prepared)
Georgios Pinitase0437672018-05-02 14:07:55 +0100402 {
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100403 if(!_weights_manager)
404 {
405 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
406 }
Georgios Pinitase0437672018-05-02 14:07:55 +0100407
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100408 auto release_unused = [](CLTensor * w)
409 {
410 if(!w->is_used())
411 {
412 CLScheduler::get().queue().finish();
413 w->allocator()->free();
414 }
415 };
416
417 // Pointer to current weights
418 const ICLTensor *cur_weights = _original_weights;
419
420 // Reshape of the weights if needed (happens only once)
421 if(!_are_weights_reshaped)
422 {
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100423 if(_weights_manager && _weights_manager->are_weights_managed(_original_weights))
424 {
425 cur_weights = utils::cast::polymorphic_downcast<ICLTensor *>(_weights_manager->run(cur_weights, &_reshape_weights_managed_function));
426 }
427 else
428 {
429 // Run reshape weights kernel and mark weights as unused
430 _reshape_weights_output.allocator()->allocate();
431 _reshape_weights_function.run();
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100432
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100433 cur_weights->mark_as_unused();
434 cur_weights = &_reshape_weights_output;
435 }
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100436 _are_weights_reshaped = true;
437 }
438
439 // Convert weights if needed (happens only once)
440 if(!_are_weights_converted)
441 {
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100442 if(_weights_manager && _weights_manager->are_weights_managed(cur_weights))
443 {
444 _weights_manager->run(cur_weights, &_convert_weights_managed);
445 }
446 else
447 {
448 _converted_weights_output.allocator()->allocate();
449 _convert_weights.run();
450 cur_weights->mark_as_unused();
451 }
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100452
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100453 _are_weights_converted = true;
454 }
455
456 // Release reshaped weights if unused
457 release_unused(&_reshape_weights_output);
Georgios Pinitase0437672018-05-02 14:07:55 +0100458
459 // Prepare GEMM prepare and release unused weights
460 if(!_is_quantized)
461 {
462 _mm_gemm.prepare();
Georgios Pinitase0437672018-05-02 14:07:55 +0100463 }
464
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100465 // Release converted weights if unused
466 release_unused(&_reshape_weights_output);
467 release_unused(&_converted_weights_output);
468
469 _is_prepared = true;
Georgios Pinitase0437672018-05-02 14:07:55 +0100470 }
471}
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100472} // namespace arm_compute