blob: a8167ce8f74ffe0cfa45e4728661429f305ef20f [file] [log] [blame]
Anthony Barbier871448e2017-03-24 14:54:29 +00001/*
Jenkins4ba87db2019-05-23 17:11:51 +01002 * Copyright (c) 2017-2019 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/CL/functions/CLFullyConnectedLayer.h"
25
Kaizen8938bd32017-09-28 14:38:23 +010026#include "arm_compute/core/Size2D.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000027#include "arm_compute/core/Validate.h"
Jenkins0e205f72019-11-28 16:53:35 +000028#include "arm_compute/core/utils/misc/Cast.h"
Anthony Barbierf45d5a92018-01-24 16:23:15 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000030#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000031#include "arm_compute/runtime/CL/CLScheduler.h"
Kaizen8938bd32017-09-28 14:38:23 +010032#include "support/ToolchainSupport.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000033
Anthony Barbiera4376382017-04-12 15:12:46 +010034#include <algorithm>
Anthony Barbiera4376382017-04-12 15:12:46 +010035
Jenkins0e205f72019-11-28 16:53:35 +000036namespace arm_compute
37{
Anthony Barbierf45d5a92018-01-24 16:23:15 +000038using namespace arm_compute::misc::shape_calculator;
Jenkins0e205f72019-11-28 16:53:35 +000039using namespace arm_compute::utils::cast;
Anthony Barbierf45d5a92018-01-24 16:23:15 +000040
41namespace
42{
Jenkins0e205f72019-11-28 16:53:35 +000043Status construct_gemmlowp_output_stage(const ITensorInfo &input, const ITensorInfo &weights, const ITensorInfo &output,
44 GEMMLowpOutputStageInfo &gemmlowp_output_stage)
Anthony Barbierf45d5a92018-01-24 16:23:15 +000045{
Jenkins0e205f72019-11-28 16:53:35 +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 gemmlowp_output_stage.gemmlowp_multipliers.push_back(output_multiplier);
72 gemmlowp_output_stage.gemmlowp_shifts.push_back(output_shift);
73 }
74
75 return Status{};
76}
77
78Status validate_mm(const ITensorInfo &input, const ITensorInfo &weights, const ITensorInfo *bias, const ITensorInfo &output, const FullyConnectedLayerInfo &fc_info)
79{
80 GEMMLowpOutputStageInfo gemmlowp_output_stage;
81 ARM_COMPUTE_RETURN_ON_ERROR(construct_gemmlowp_output_stage(input, weights, output, gemmlowp_output_stage));
82
83 const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped
84 false, // is_b_reshaped
85 true, // reshape_b_only_on_first_run
86 0, // depth_output_gemm3d
87 false, // reinterpret_input_as_3d
88 fc_info.retain_internal_weights, // retain_internal_weights
89 gemmlowp_output_stage, // gemmlowp_output_stage
90 fc_info.fp_mixed_precision, // fp_mixed_precision
91 true, // broadcast_bias
92 ActivationLayerInfo()); // activation_info
93
Anthony Barbierf45d5a92018-01-24 16:23:15 +000094 if(is_data_type_quantized_asymmetric(input.data_type()))
95 {
Jenkins975dfe12019-09-02 11:47:54 +010096 const UniformQuantizationInfo iq_info = input.quantization_info().uniform();
97 const UniformQuantizationInfo wq_info = weights.quantization_info().uniform();
98
Anthony Barbierf45d5a92018-01-24 16:23:15 +000099 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
100 // Extract and negate input and weights offset
Jenkins975dfe12019-09-02 11:47:54 +0100101 const QuantizationInfo input_quantization_info(iq_info.scale, -iq_info.offset);
102 const QuantizationInfo weights_quantization_info(wq_info.scale, -wq_info.offset);
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000103
104 // Validate gemmlowp function
105 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyCore::validate(&input.clone()->set_quantization_info(input_quantization_info),
106 &weights.clone()->set_quantization_info(weights_quantization_info),
Jenkins0e205f72019-11-28 16:53:35 +0000107 bias,
108 &output,
109 gemm_info));
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000110 }
111 else
112 {
Jenkins0e205f72019-11-28 16:53:35 +0000113 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(&input, &weights, bias, &output, 1.f, 1.f, gemm_info));
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000114 }
115
116 return Status{};
117}
118} // namespace
Anthony Barbier871448e2017-03-24 14:54:29 +0000119
Kaizen8938bd32017-09-28 14:38:23 +0100120void CLFullyConnectedLayerReshapeWeights::configure(const ICLTensor *input, ICLTensor *output)
121{
122 auto k = arm_compute::support::cpp14::make_unique<CLTransposeKernel>();
123 k->configure(input, output);
124 _kernel = std::move(k);
125}
126
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000127Status CLFullyConnectedLayerReshapeWeights::validate(const ITensorInfo *input, const ITensorInfo *output)
128{
129 return CLTransposeKernel::validate(input, output);
130}
131
Jenkins0e205f72019-11-28 16:53:35 +0000132CLFullyConnectedLayer::CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
133 : _memory_group(memory_manager), _weights_manager(weights_manager), _convert_weights(), _convert_weights_managed(), _reshape_weights_managed_function(), _flatten_layer(), _reshape_weights_function(),
134 _mm_gemm(memory_manager, weights_manager), _mm_gemmlowp(memory_manager), _flatten_output(), _converted_weights_output(), _reshape_weights_output(), _are_weights_converted(true),
135 _are_weights_reshaped(true), _is_fc_after_conv(true), _is_quantized(false), _is_prepared(false), _original_weights(nullptr)
Anthony Barbierdbdab852017-06-23 15:42:00 +0100136{
137}
Jenkins0e205f72019-11-28 16:53:35 +0000138void CLFullyConnectedLayer::configure_mm(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000139{
Jenkins0e205f72019-11-28 16:53:35 +0000140 GEMMLowpOutputStageInfo gemmlowp_output_stage;
141 construct_gemmlowp_output_stage(*input->info(), *weights->info(), *output->info(), gemmlowp_output_stage);
142
143 const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped
144 false, // is_b_reshaped
145 true, // reshape_b_only_on_first_run
146 0, // depth_output_gemm3d
147 false, // reinterpret_input_as_3d
148 fc_info.retain_internal_weights, // retain_internal_weights
149 gemmlowp_output_stage, // gemmlowp_output_stage
150 fc_info.fp_mixed_precision, // fp_mixed_precision
151 true, // broadcast_bias
152 ActivationLayerInfo()); // activation_info
153
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000154 if(_is_quantized)
155 {
156 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
157 // Extract and negate input and weights offset
158 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
159 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
160
Jenkins975dfe12019-09-02 11:47:54 +0100161 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset));
162 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000163
164 // Configure gemmlowp function
Jenkins0e205f72019-11-28 16:53:35 +0000165 _mm_gemmlowp.configure(input, weights, bias, output, gemm_info);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000166
167 // Revert back QuantizatioInfo as input and weights could be used in other fully connected layers
168 input->info()->set_quantization_info(input_quantization_info);
169 weights->info()->set_quantization_info(weights_quantization_info);
170 }
171 else
172 {
173 // Configure matrix multiply kernel
Jenkins0e205f72019-11-28 16:53:35 +0000174 _mm_gemm.configure(input, weights, bias, output, 1.f, 1.f, gemm_info);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000175 }
176}
177
Jenkins0e205f72019-11-28 16:53:35 +0000178void CLFullyConnectedLayer::configure_conv_fc(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info)
Anthony Barbierdbdab852017-06-23 15:42:00 +0100179{
Kaizen8938bd32017-09-28 14:38:23 +0100180 ARM_COMPUTE_ERROR_ON((weights->info()->dimension(1) != (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2))));
Anthony Barbierdbdab852017-06-23 15:42:00 +0100181
Anthony Barbiera4376382017-04-12 15:12:46 +0100182 // If the fully connected layer is called after a convolution layer, the input tensor must be linearized
Anthony Barbier871448e2017-03-24 14:54:29 +0000183
Jenkins52ba29e2018-08-29 15:32:11 +0000184 // Initialize output tensor for flatten
185 TensorShape shape_flatten = compute_flatten_shape(input->info());
186 _flatten_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_flatten).set_data_layout(DataLayout::NCHW));
Anthony Barbier871448e2017-03-24 14:54:29 +0000187
Jenkins52ba29e2018-08-29 15:32:11 +0000188 // Configure flatten kernel
189 _memory_group.manage(&_flatten_output);
190 _flatten_layer.configure(input, &_flatten_output);
Anthony Barbiera4376382017-04-12 15:12:46 +0100191
Anthony Barbiera4376382017-04-12 15:12:46 +0100192 // Configure matrix multiply kernel
Jenkins0e205f72019-11-28 16:53:35 +0000193 configure_mm(&_flatten_output, weights, bias, output, fc_info);
Anthony Barbiera4376382017-04-12 15:12:46 +0100194
Jenkins52ba29e2018-08-29 15:32:11 +0000195 // Allocate the output tensor for flatten once all the configure methods have been called
196 _flatten_output.allocator()->allocate();
Anthony Barbiera4376382017-04-12 15:12:46 +0100197}
198
Jenkins0e205f72019-11-28 16:53:35 +0000199void CLFullyConnectedLayer::configure_fc_fc(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info)
Anthony Barbiera4376382017-04-12 15:12:46 +0100200{
201 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != weights->info()->dimension(1));
202
203 // Configure matrix multiply kernel
Jenkins0e205f72019-11-28 16:53:35 +0000204 configure_mm(input, weights, bias, output, fc_info);
Anthony Barbiera4376382017-04-12 15:12:46 +0100205}
206
Jenkins52ba29e2018-08-29 15:32:11 +0000207void CLFullyConnectedLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
208 FullyConnectedLayerInfo fc_info)
Anthony Barbiera4376382017-04-12 15:12:46 +0100209{
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000210 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
211
212 // Perform validate step
213 ARM_COMPUTE_ERROR_THROW_ON(CLFullyConnectedLayer::validate(input->info(),
214 weights->info(),
215 biases != nullptr ? biases->info() : nullptr,
216 output->info(),
Jenkins52ba29e2018-08-29 15:32:11 +0000217 fc_info));
Anthony Barbiera4376382017-04-12 15:12:46 +0100218
Jenkins52ba29e2018-08-29 15:32:11 +0000219 _are_weights_converted = true;
220 _are_weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true;
221 _is_fc_after_conv = true;
Jenkins52ba29e2018-08-29 15:32:11 +0000222 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
223 _is_prepared = fc_info.retain_internal_weights;
224 _original_weights = weights;
Anthony Barbiera4376382017-04-12 15:12:46 +0100225
Jenkins0e205f72019-11-28 16:53:35 +0000226 if(_weights_manager)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000227 {
Jenkins0e205f72019-11-28 16:53:35 +0000228 _weights_manager->manage(weights);
Anthony Barbier871448e2017-03-24 14:54:29 +0000229 }
230
Jenkins52ba29e2018-08-29 15:32:11 +0000231 const ICLTensor *weights_to_use = weights;
232
Anthony Barbiera4376382017-04-12 15:12:46 +0100233 // With the Fully Connected layer we can have 4 different cases:
234 // 1) Convolution layer -> Fully Connected layer without batches
235 // 2) Fully Connected layer -> Fully Connected layer without batches
236 // 3) Convolution layer -> Fully Connected layer with batches
237 // 4) Fully Connected layer -> Fully Connected layer with batches
Anthony Barbier871448e2017-03-24 14:54:29 +0000238
Kaizen8938bd32017-09-28 14:38:23 +0100239 // Check if we have a fully connected layer with batches
240 const bool is_batched_fc_layer = output->info()->dimension(1) > 1;
Kaizen8938bd32017-09-28 14:38:23 +0100241 if(is_batched_fc_layer)
Anthony Barbierdbdab852017-06-23 15:42:00 +0100242 {
243 _is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->info()->tensor_shape().cbegin() + 3,
244 input->info()->tensor_shape().cend(),
245 output->info()->tensor_shape().cbegin() + 1));
Anthony Barbiera4376382017-04-12 15:12:46 +0100246 }
247 else
248 {
Kaizen8938bd32017-09-28 14:38:23 +0100249 _is_fc_after_conv = input->info()->num_dimensions() > 1;
250 }
Anthony Barbiera4376382017-04-12 15:12:46 +0100251
Jenkins52ba29e2018-08-29 15:32:11 +0000252 // Reshape weights if needed
253 if(!_are_weights_reshaped)
254 {
Jenkins0e205f72019-11-28 16:53:35 +0000255 if(_weights_manager && _weights_manager->are_weights_managed(weights))
256 {
257 _reshape_weights_managed_function.configure(weights);
258 weights_to_use = utils::cast::polymorphic_downcast<ICLTensor *>(_weights_manager->acquire(weights, &_reshape_weights_managed_function));
259 }
260 else
261 {
262 // Reshape the weights
263 _reshape_weights_function.configure(weights, &_reshape_weights_output);
264 weights_to_use = &_reshape_weights_output;
265 }
Jenkins52ba29e2018-08-29 15:32:11 +0000266 }
267
268 // Convert weights if needed
269 if(_is_fc_after_conv && (input->info()->data_layout() != fc_info.weights_trained_layout))
270 {
Jenkins0e205f72019-11-28 16:53:35 +0000271 if(_weights_manager && _weights_manager->are_weights_managed(weights_to_use))
272 {
273 _convert_weights_managed.configure(weights_to_use,
274 input->info()->tensor_shape(),
275 fc_info.weights_trained_layout);
276 weights_to_use = utils::cast::polymorphic_downcast<ICLTensor *>(_weights_manager->acquire(weights, &_convert_weights_managed));
277 }
278 else
279 {
280 // Convert weights
281 _convert_weights.configure(weights_to_use,
282 &_converted_weights_output,
283 input->info()->tensor_shape(),
284 fc_info.weights_trained_layout);
Jenkins52ba29e2018-08-29 15:32:11 +0000285
Jenkins0e205f72019-11-28 16:53:35 +0000286 weights_to_use = &_converted_weights_output;
287 }
Jenkins52ba29e2018-08-29 15:32:11 +0000288 _are_weights_converted = false;
289 }
290
Kaizen8938bd32017-09-28 14:38:23 +0100291 if(_is_fc_after_conv)
292 {
293 // Fully Connected layer after a Convolution Layer without batches
Jenkins0e205f72019-11-28 16:53:35 +0000294 configure_conv_fc(input, weights_to_use, biases, output, fc_info);
Kaizen8938bd32017-09-28 14:38:23 +0100295 }
296 else
297 {
298 // Fully Connected layer after a Fully Connected Layer without batches
Jenkins0e205f72019-11-28 16:53:35 +0000299 configure_fc_fc(input, weights_to_use, biases, output, fc_info);
Anthony Barbiera4376382017-04-12 15:12:46 +0100300 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000301}
302
Jenkins52ba29e2018-08-29 15:32:11 +0000303Status CLFullyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
304 FullyConnectedLayerInfo fc_info)
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000305{
306 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Jenkins52ba29e2018-08-29 15:32:11 +0000307 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000308 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
309 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 2);
310
Jenkins0e205f72019-11-28 16:53:35 +0000311 bool weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true;
312 bool is_fc_after_conv = true;
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000313
Jenkins52ba29e2018-08-29 15:32:11 +0000314 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));
315 const ITensorInfo &reshaped_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_transposed_shape(*weights)));
316 const ITensorInfo &converted_weights = weights_reshaped ? TensorInfo(weights->clone()->set_is_resizable(true).reset_padding()) : TensorInfo(*reshaped_weights.clone());
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000317
318 // With the Fully Connected layer we can have 4 different cases:
319 // 1) Convolution layer -> Fully Connected layer without batches
320 // 2) Fully Connected layer -> Fully Connected layer without batches
321 // 3) Convolution layer -> Fully Connected layer with batches
322 // 4) Fully Connected layer -> Fully Connected layer with batches
323
324 const ITensorInfo *input_to_use = input;
325 const ITensorInfo *weights_to_use = weights;
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000326
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000327 // Check if we have a fully connected layer with batches
328 const bool is_batched_fc_layer = output->dimension(1) > 1;
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000329 if(is_batched_fc_layer)
330 {
331 is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->tensor_shape().cbegin() + 3,
332 input->tensor_shape().cend(),
333 output->tensor_shape().cbegin() + 1));
334 }
335 else
336 {
337 is_fc_after_conv = input->num_dimensions() > 1;
338 }
339
Jenkins52ba29e2018-08-29 15:32:11 +0000340 if(!weights_reshaped)
341 {
342 // Validate reshape weights kernel
343 ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayerReshapeWeights::validate(weights, &reshaped_weights));
344 weights_to_use = &reshaped_weights;
345 }
346
347 if(is_fc_after_conv && (input->data_layout() != fc_info.weights_trained_layout))
348 {
349 // Validate convert weights kernel
350 ARM_COMPUTE_RETURN_ON_ERROR(CLConvertFullyConnectedWeights::validate(weights_to_use,
351 &converted_weights,
352 input->tensor_shape(),
353 fc_info.weights_trained_layout));
354 weights_to_use = &converted_weights;
355 }
356
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000357 if(is_fc_after_conv)
358 {
359 // Fully Connected layer after a Convolution Layer without batches
360 ARM_COMPUTE_RETURN_ERROR_ON((weights_to_use->dimension(1) != (input->dimension(0) * input->dimension(1) * input->dimension(2))));
361
Jenkins52ba29e2018-08-29 15:32:11 +0000362 // Validate flatten kernel
363 ARM_COMPUTE_RETURN_ON_ERROR(CLFlattenLayer::validate(input, &flatten_input));
364 input_to_use = &flatten_input;
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000365 }
366 else
367 {
368 // Fully Connected layer after a Fully Connected Layer without batches
369 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != weights_to_use->dimension(1));
370 }
Jenkins0e205f72019-11-28 16:53:35 +0000371
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000372 // Validate matrix multiply kernel
Jenkins0e205f72019-11-28 16:53:35 +0000373 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(*input_to_use, *weights_to_use, biases, *output, fc_info));
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000374
375 return Status{};
376}
377
Anthony Barbier871448e2017-03-24 14:54:29 +0000378void CLFullyConnectedLayer::run()
379{
Jenkinsb3a371b2018-05-23 11:36:53 +0100380 prepare();
Anthony Barbiera4376382017-04-12 15:12:46 +0100381
Jenkins4ba87db2019-05-23 17:11:51 +0100382 MemoryGroupResourceScope scope_mg(_memory_group);
Kaizen8938bd32017-09-28 14:38:23 +0100383
Anthony Barbiera4376382017-04-12 15:12:46 +0100384 // Linearize input if it comes from a convolutional layer
Anthony Barbierdbdab852017-06-23 15:42:00 +0100385 if(_is_fc_after_conv)
Anthony Barbiera4376382017-04-12 15:12:46 +0100386 {
Jenkins52ba29e2018-08-29 15:32:11 +0000387 _flatten_layer.run();
Anthony Barbiera4376382017-04-12 15:12:46 +0100388 }
389
Anthony Barbiera4376382017-04-12 15:12:46 +0100390 // Run matrix multiply
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000391 if(_is_quantized)
392 {
393 _mm_gemmlowp.run();
394 }
395 else
396 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100397 _mm_gemm.run();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000398 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000399}
Jenkinsb3a371b2018-05-23 11:36:53 +0100400
401void CLFullyConnectedLayer::prepare()
402{
Jenkins52ba29e2018-08-29 15:32:11 +0000403 if(!_is_prepared)
Jenkinsb3a371b2018-05-23 11:36:53 +0100404 {
Jenkins0e205f72019-11-28 16:53:35 +0000405 if(!_weights_manager)
406 {
407 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
408 }
Jenkinsb3a371b2018-05-23 11:36:53 +0100409
Jenkins52ba29e2018-08-29 15:32:11 +0000410 auto release_unused = [](CLTensor * w)
411 {
412 if(!w->is_used())
413 {
414 CLScheduler::get().queue().finish();
415 w->allocator()->free();
416 }
417 };
418
419 // Pointer to current weights
420 const ICLTensor *cur_weights = _original_weights;
421
422 // Reshape of the weights if needed (happens only once)
423 if(!_are_weights_reshaped)
424 {
Jenkins0e205f72019-11-28 16:53:35 +0000425 if(_weights_manager && _weights_manager->are_weights_managed(_original_weights))
426 {
427 cur_weights = utils::cast::polymorphic_downcast<ICLTensor *>(_weights_manager->run(cur_weights, &_reshape_weights_managed_function));
428 }
429 else
430 {
431 // Run reshape weights kernel and mark weights as unused
432 _reshape_weights_output.allocator()->allocate();
433 _reshape_weights_function.run();
Jenkins52ba29e2018-08-29 15:32:11 +0000434
Jenkins0e205f72019-11-28 16:53:35 +0000435 cur_weights->mark_as_unused();
436 cur_weights = &_reshape_weights_output;
437 }
Jenkins52ba29e2018-08-29 15:32:11 +0000438 _are_weights_reshaped = true;
439 }
440
441 // Convert weights if needed (happens only once)
442 if(!_are_weights_converted)
443 {
Jenkins0e205f72019-11-28 16:53:35 +0000444 if(_weights_manager && _weights_manager->are_weights_managed(cur_weights))
445 {
446 _weights_manager->run(cur_weights, &_convert_weights_managed);
447 }
448 else
449 {
450 _converted_weights_output.allocator()->allocate();
451 _convert_weights.run();
452 cur_weights->mark_as_unused();
453 }
Jenkins52ba29e2018-08-29 15:32:11 +0000454
Jenkins52ba29e2018-08-29 15:32:11 +0000455 _are_weights_converted = true;
456 }
457
458 // Release reshaped weights if unused
459 release_unused(&_reshape_weights_output);
Jenkinsb3a371b2018-05-23 11:36:53 +0100460
461 // Prepare GEMM prepare and release unused weights
462 if(!_is_quantized)
463 {
464 _mm_gemm.prepare();
Jenkinsb3a371b2018-05-23 11:36:53 +0100465 }
466
Jenkins52ba29e2018-08-29 15:32:11 +0000467 // Release converted weights if unused
468 release_unused(&_reshape_weights_output);
469 release_unused(&_converted_weights_output);
470
471 _is_prepared = true;
Jenkinsb3a371b2018-05-23 11:36:53 +0100472 }
473}
Jenkins0e205f72019-11-28 16:53:35 +0000474} // namespace arm_compute