blob: dcaa12645eb327f80f4b482f192fad4edcd7c7b4 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sang-Hoon Parkb66aa3b2020-01-10 14:44:13 +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/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
Sang-Hoon Parkb66aa3b2020-01-10 14:44:13 +000051 const auto data_type = input.data_type();
52
Georgios Pinitas8b721992019-10-28 16:24:28 +000053 // Configure output stage for quantized case
Sang-Hoon Parkb66aa3b2020-01-10 14:44:13 +000054 if(is_data_type_quantized_asymmetric(data_type))
Georgios Pinitas8b721992019-10-28 16:24:28 +000055 {
56 const UniformQuantizationInfo iq_info = input.quantization_info().uniform();
57 const UniformQuantizationInfo wq_info = weights.quantization_info().uniform();
58 const UniformQuantizationInfo oq_info = output.quantization_info().uniform();
59
60 const auto output_quant_info = (output.total_size() == 0) ? iq_info : oq_info;
61
62 const float multiplier = (iq_info.scale * wq_info.scale) / output_quant_info.scale;
63 int output_multiplier = 0;
64 int output_shift = 0;
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +010065 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift));
Georgios Pinitas8b721992019-10-28 16:24:28 +000066
Sang-Hoon Parkb66aa3b2020-01-10 14:44:13 +000067 PixelValue type_min{};
68 PixelValue type_max{};
69 std::tie(type_min, type_max) = get_min_max(data_type);
70
Georgios Pinitas8b721992019-10-28 16:24:28 +000071 // Set the GEMMLowp output stage info
72 gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset;
73 gemmlowp_output_stage.gemmlowp_multiplier = output_multiplier;
74 gemmlowp_output_stage.gemmlowp_shift = output_shift;
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000075 gemmlowp_output_stage.gemmlowp_multipliers.push_back(output_multiplier);
76 gemmlowp_output_stage.gemmlowp_shifts.push_back(output_shift);
Sang-Hoon Parkb66aa3b2020-01-10 14:44:13 +000077 type_min.get(gemmlowp_output_stage.gemmlowp_min_bound);
78 type_max.get(gemmlowp_output_stage.gemmlowp_max_bound);
Georgios Pinitas8b721992019-10-28 16:24:28 +000079 }
80
81 return Status{};
82}
83
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +000084Status 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 +000085{
86 GEMMLowpOutputStageInfo gemmlowp_output_stage;
87 ARM_COMPUTE_RETURN_ON_ERROR(construct_gemmlowp_output_stage(input, weights, output, gemmlowp_output_stage));
88
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +000089 const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped
90 false, // is_b_reshaped
91 true, // reshape_b_only_on_first_run
92 0, // depth_output_gemm3d
93 false, // reinterpret_input_as_3d
94 fc_info.retain_internal_weights, // retain_internal_weights
95 gemmlowp_output_stage, // gemmlowp_output_stage
96 fc_info.fp_mixed_precision, // fp_mixed_precision
97 true, // broadcast_bias
98 ActivationLayerInfo()); // activation_info
Georgios Pinitas8b721992019-10-28 16:24:28 +000099
Georgios Pinitas358ca202017-12-07 16:47:52 +0000100 if(is_data_type_quantized_asymmetric(input.data_type()))
101 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100102 const UniformQuantizationInfo iq_info = input.quantization_info().uniform();
103 const UniformQuantizationInfo wq_info = weights.quantization_info().uniform();
104
Georgios Pinitas358ca202017-12-07 16:47:52 +0000105 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
106 // Extract and negate input and weights offset
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100107 const QuantizationInfo input_quantization_info(iq_info.scale, -iq_info.offset);
108 const QuantizationInfo weights_quantization_info(wq_info.scale, -wq_info.offset);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000109
110 // Validate gemmlowp function
111 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyCore::validate(&input.clone()->set_quantization_info(input_quantization_info),
112 &weights.clone()->set_quantization_info(weights_quantization_info),
Georgios Pinitas8b721992019-10-28 16:24:28 +0000113 bias,
114 &output,
115 gemm_info));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000116 }
117 else
118 {
Georgios Pinitas8b721992019-10-28 16:24:28 +0000119 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(&input, &weights, bias, &output, 1.f, 1.f, gemm_info));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000120 }
121
122 return Status{};
123}
124} // namespace
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100125
126void CLFullyConnectedLayerReshapeWeights::configure(const ICLTensor *input, ICLTensor *output)
Moritz Pflanzer768e9f12017-08-11 15:33:30 +0100127{
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100128 auto k = arm_compute::support::cpp14::make_unique<CLTransposeKernel>();
129 k->configure(input, output);
130 _kernel = std::move(k);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131}
132
Georgios Pinitas358ca202017-12-07 16:47:52 +0000133Status CLFullyConnectedLayerReshapeWeights::validate(const ITensorInfo *input, const ITensorInfo *output)
134{
135 return CLTransposeKernel::validate(input, output);
136}
137
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100138CLFullyConnectedLayer::CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100139 : _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 +0000140 _mm_gemm(memory_manager, weights_manager), _mm_gemmlowp(memory_manager), _flatten_output(), _converted_weights_output(), _reshape_weights_output(), _are_weights_converted(true),
141 _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 +0100142{
143}
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000144void 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 +0000145{
Georgios Pinitas8b721992019-10-28 16:24:28 +0000146 GEMMLowpOutputStageInfo gemmlowp_output_stage;
147 construct_gemmlowp_output_stage(*input->info(), *weights->info(), *output->info(), gemmlowp_output_stage);
148
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000149 const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped
150 false, // is_b_reshaped
151 true, // reshape_b_only_on_first_run
152 0, // depth_output_gemm3d
153 false, // reinterpret_input_as_3d
154 fc_info.retain_internal_weights, // retain_internal_weights
155 gemmlowp_output_stage, // gemmlowp_output_stage
156 fc_info.fp_mixed_precision, // fp_mixed_precision
157 true, // broadcast_bias
158 ActivationLayerInfo()); // activation_info
Georgios Pinitas8b721992019-10-28 16:24:28 +0000159
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000160 if(_is_quantized)
161 {
Chunosov5124be52017-11-22 20:42:13 +0700162 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000163 // Extract and negate input and weights offset
Chunosov5124be52017-11-22 20:42:13 +0700164 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
165 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
166
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100167 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset));
168 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset));
Chunosov5124be52017-11-22 20:42:13 +0700169
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000170 // Configure gemmlowp function
Georgios Pinitas8b721992019-10-28 16:24:28 +0000171 _mm_gemmlowp.configure(input, weights, bias, output, gemm_info);
Chunosov5124be52017-11-22 20:42:13 +0700172
173 // Revert back QuantizatioInfo as input and weights could be used in other fully connected layers
174 input->info()->set_quantization_info(input_quantization_info);
175 weights->info()->set_quantization_info(weights_quantization_info);
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000176 }
177 else
178 {
179 // Configure matrix multiply kernel
Georgios Pinitas8b721992019-10-28 16:24:28 +0000180 _mm_gemm.configure(input, weights, bias, output, 1.f, 1.f, gemm_info);
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000181 }
182}
183
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000184void 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 +0100185{
186 ARM_COMPUTE_ERROR_ON((weights->info()->dimension(1) != (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2))));
187
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100188 // If the fully connected layer is called after a convolution layer, the input tensor must be linearized
189
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100190 // Initialize output tensor for flatten
191 TensorShape shape_flatten = compute_flatten_shape(input->info());
192 _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 +0100193
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100194 // Configure flatten kernel
195 _memory_group.manage(&_flatten_output);
196 _flatten_layer.configure(input, &_flatten_output);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100197
198 // Configure matrix multiply kernel
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000199 configure_mm(&_flatten_output, weights, bias, output, fc_info);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100200
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100201 // Allocate the output tensor for flatten once all the configure methods have been called
202 _flatten_output.allocator()->allocate();
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100203}
204
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000205void 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 +0100206{
207 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != weights->info()->dimension(1));
208
209 // Configure matrix multiply kernel
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000210 configure_mm(input, weights, bias, output, fc_info);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100211}
212
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100213void CLFullyConnectedLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
214 FullyConnectedLayerInfo fc_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215{
Georgios Pinitas358ca202017-12-07 16:47:52 +0000216 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
217
218 // Perform validate step
219 ARM_COMPUTE_ERROR_THROW_ON(CLFullyConnectedLayer::validate(input->info(),
220 weights->info(),
221 biases != nullptr ? biases->info() : nullptr,
222 output->info(),
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100223 fc_info));
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100224
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100225 _are_weights_converted = true;
226 _are_weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true;
227 _is_fc_after_conv = true;
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100228 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Michele Di Giorgioba1ffe92018-08-22 14:28:30 +0100229 _is_prepared = fc_info.retain_internal_weights;
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100230 _original_weights = weights;
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100231
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100232 if(_weights_manager)
233 {
234 _weights_manager->manage(weights);
235 }
236
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100237 const ICLTensor *weights_to_use = weights;
238
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100239 // With the Fully Connected layer we can have 4 different cases:
240 // 1) Convolution layer -> Fully Connected layer without batches
241 // 2) Fully Connected layer -> Fully Connected layer without batches
242 // 3) Convolution layer -> Fully Connected layer with batches
243 // 4) Fully Connected layer -> Fully Connected layer with batches
244
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100245 // Check if we have a fully connected layer with batches
246 const bool is_batched_fc_layer = output->info()->dimension(1) > 1;
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100247 if(is_batched_fc_layer)
Moritz Pflanzer768e9f12017-08-11 15:33:30 +0100248 {
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100249 _is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->info()->tensor_shape().cbegin() + 3,
250 input->info()->tensor_shape().cend(),
251 output->info()->tensor_shape().cbegin() + 1));
Moritz Pflanzer768e9f12017-08-11 15:33:30 +0100252 }
253 else
254 {
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100255 _is_fc_after_conv = input->info()->num_dimensions() > 1;
Moritz Pflanzer768e9f12017-08-11 15:33:30 +0100256 }
257
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100258 // Reshape weights if needed
259 if(!_are_weights_reshaped)
260 {
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100261 if(_weights_manager && _weights_manager->are_weights_managed(weights))
262 {
263 _reshape_weights_managed_function.configure(weights);
264 weights_to_use = utils::cast::polymorphic_downcast<ICLTensor *>(_weights_manager->acquire(weights, &_reshape_weights_managed_function));
265 }
266 else
267 {
268 // Reshape the weights
269 _reshape_weights_function.configure(weights, &_reshape_weights_output);
270 weights_to_use = &_reshape_weights_output;
271 }
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100272 }
273
274 // Convert weights if needed
275 if(_is_fc_after_conv && (input->info()->data_layout() != fc_info.weights_trained_layout))
276 {
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100277 if(_weights_manager && _weights_manager->are_weights_managed(weights_to_use))
278 {
279 _convert_weights_managed.configure(weights_to_use,
280 input->info()->tensor_shape(),
281 fc_info.weights_trained_layout);
282 weights_to_use = utils::cast::polymorphic_downcast<ICLTensor *>(_weights_manager->acquire(weights, &_convert_weights_managed));
283 }
284 else
285 {
286 // Convert weights
287 _convert_weights.configure(weights_to_use,
288 &_converted_weights_output,
289 input->info()->tensor_shape(),
290 fc_info.weights_trained_layout);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100291
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100292 weights_to_use = &_converted_weights_output;
293 }
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100294 _are_weights_converted = false;
295 }
296
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100297 if(_is_fc_after_conv)
Moritz Pflanzer768e9f12017-08-11 15:33:30 +0100298 {
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100299 // Fully Connected layer after a Convolution Layer without batches
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000300 configure_conv_fc(input, weights_to_use, biases, output, fc_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100301 }
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100302 else
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100303 {
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100304 // Fully Connected layer after a Fully Connected Layer without batches
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000305 configure_fc_fc(input, weights_to_use, biases, output, fc_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100306 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100307}
308
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100309Status CLFullyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
310 FullyConnectedLayerInfo fc_info)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000311{
312 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Sang-Hoon Parkb66aa3b2020-01-10 14:44:13 +0000313 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000314 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
315 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 2);
316
Georgios Pinitas8b721992019-10-28 16:24:28 +0000317 bool weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true;
318 bool is_fc_after_conv = true;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000319
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100320 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 +0100321 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 +0100322 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 +0000323
324 // With the Fully Connected layer we can have 4 different cases:
325 // 1) Convolution layer -> Fully Connected layer without batches
326 // 2) Fully Connected layer -> Fully Connected layer without batches
327 // 3) Convolution layer -> Fully Connected layer with batches
328 // 4) Fully Connected layer -> Fully Connected layer with batches
329
330 const ITensorInfo *input_to_use = input;
331 const ITensorInfo *weights_to_use = weights;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000332
Georgios Pinitas358ca202017-12-07 16:47:52 +0000333 // Check if we have a fully connected layer with batches
334 const bool is_batched_fc_layer = output->dimension(1) > 1;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000335 if(is_batched_fc_layer)
336 {
337 is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->tensor_shape().cbegin() + 3,
338 input->tensor_shape().cend(),
339 output->tensor_shape().cbegin() + 1));
340 }
341 else
342 {
343 is_fc_after_conv = input->num_dimensions() > 1;
344 }
345
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100346 if(!weights_reshaped)
347 {
348 // Validate reshape weights kernel
349 ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayerReshapeWeights::validate(weights, &reshaped_weights));
350 weights_to_use = &reshaped_weights;
351 }
352
353 if(is_fc_after_conv && (input->data_layout() != fc_info.weights_trained_layout))
354 {
355 // Validate convert weights kernel
356 ARM_COMPUTE_RETURN_ON_ERROR(CLConvertFullyConnectedWeights::validate(weights_to_use,
357 &converted_weights,
358 input->tensor_shape(),
359 fc_info.weights_trained_layout));
360 weights_to_use = &converted_weights;
361 }
362
Georgios Pinitas358ca202017-12-07 16:47:52 +0000363 if(is_fc_after_conv)
364 {
365 // Fully Connected layer after a Convolution Layer without batches
366 ARM_COMPUTE_RETURN_ERROR_ON((weights_to_use->dimension(1) != (input->dimension(0) * input->dimension(1) * input->dimension(2))));
367
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100368 // Validate flatten kernel
369 ARM_COMPUTE_RETURN_ON_ERROR(CLFlattenLayer::validate(input, &flatten_input));
370 input_to_use = &flatten_input;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000371 }
372 else
373 {
374 // Fully Connected layer after a Fully Connected Layer without batches
375 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != weights_to_use->dimension(1));
376 }
Georgios Pinitas8b721992019-10-28 16:24:28 +0000377
Georgios Pinitas358ca202017-12-07 16:47:52 +0000378 // Validate matrix multiply kernel
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000379 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(*input_to_use, *weights_to_use, biases, *output, fc_info));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000380
381 return Status{};
382}
383
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100384void CLFullyConnectedLayer::run()
385{
Georgios Pinitase0437672018-05-02 14:07:55 +0100386 prepare();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100387
Georgios Pinitasda953f22019-04-02 17:27:03 +0100388 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100389
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100390 // Linearize input if it comes from a convolutional layer
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100391 if(_is_fc_after_conv)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100392 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100393 _flatten_layer.run();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100394 }
395
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100396 // Run matrix multiply
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000397 if(_is_quantized)
398 {
399 _mm_gemmlowp.run();
400 }
401 else
402 {
Gian Marco Iodicec9c62c22018-04-06 10:00:10 +0100403 _mm_gemm.run();
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000404 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100405}
Georgios Pinitase0437672018-05-02 14:07:55 +0100406
407void CLFullyConnectedLayer::prepare()
408{
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100409 if(!_is_prepared)
Georgios Pinitase0437672018-05-02 14:07:55 +0100410 {
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100411 if(!_weights_manager)
412 {
413 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
414 }
Georgios Pinitase0437672018-05-02 14:07:55 +0100415
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100416 auto release_unused = [](CLTensor * w)
417 {
418 if(!w->is_used())
419 {
420 CLScheduler::get().queue().finish();
421 w->allocator()->free();
422 }
423 };
424
425 // Pointer to current weights
426 const ICLTensor *cur_weights = _original_weights;
427
428 // Reshape of the weights if needed (happens only once)
429 if(!_are_weights_reshaped)
430 {
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100431 if(_weights_manager && _weights_manager->are_weights_managed(_original_weights))
432 {
433 cur_weights = utils::cast::polymorphic_downcast<ICLTensor *>(_weights_manager->run(cur_weights, &_reshape_weights_managed_function));
434 }
435 else
436 {
437 // Run reshape weights kernel and mark weights as unused
438 _reshape_weights_output.allocator()->allocate();
439 _reshape_weights_function.run();
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100440
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100441 cur_weights->mark_as_unused();
442 cur_weights = &_reshape_weights_output;
443 }
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100444 _are_weights_reshaped = true;
445 }
446
447 // Convert weights if needed (happens only once)
448 if(!_are_weights_converted)
449 {
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100450 if(_weights_manager && _weights_manager->are_weights_managed(cur_weights))
451 {
452 _weights_manager->run(cur_weights, &_convert_weights_managed);
453 }
454 else
455 {
456 _converted_weights_output.allocator()->allocate();
457 _convert_weights.run();
458 cur_weights->mark_as_unused();
459 }
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100460
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100461 _are_weights_converted = true;
462 }
463
464 // Release reshaped weights if unused
465 release_unused(&_reshape_weights_output);
Georgios Pinitase0437672018-05-02 14:07:55 +0100466
467 // Prepare GEMM prepare and release unused weights
468 if(!_is_quantized)
469 {
470 _mm_gemm.prepare();
Georgios Pinitase0437672018-05-02 14:07:55 +0100471 }
472
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100473 // Release converted weights if unused
474 release_unused(&_reshape_weights_output);
475 release_unused(&_converted_weights_output);
476
477 _is_prepared = true;
Georgios Pinitase0437672018-05-02 14:07:55 +0100478 }
479}
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100480} // namespace arm_compute