blob: 46a90a54b7d1a212ca67fd926db2f27e9c1939f5 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * 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"
Georgios Pinitas358ca202017-12-07 16:47:52 +000028#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000029#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/runtime/CL/CLScheduler.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010031#include "src/core/CL/kernels/CLDepthConvertLayerKernel.h"
32#include "src/core/CL/kernels/CLFillBorderKernel.h"
33#include "src/core/CL/kernels/CLGEMMLowpMatrixMultiplyNativeKernel.h"
34#include "src/core/CL/kernels/CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel.h"
35#include "src/core/CL/kernels/CLGEMMLowpOffsetContributionKernel.h"
36#include "src/core/CL/kernels/CLGEMMLowpOffsetContributionOutputStageKernel.h"
37#include "src/core/CL/kernels/CLGEMMLowpReductionKernel.h"
38#include "src/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h"
39#include "src/core/CL/kernels/CLGEMMMatrixMultiplyReshapedKernel.h"
40#include "src/core/CL/kernels/CLGEMMMatrixMultiplyReshapedOnlyRHSKernel.h"
41#include "src/core/CL/kernels/CLGEMMReshapeLHSMatrixKernel.h"
42#include "src/core/CL/kernels/CLGEMMReshapeRHSMatrixKernel.h"
43#include "src/core/CL/kernels/CLTransposeKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010044#include "support/Cast.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045
46#include <algorithm>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047
Michalis Spyroub27e13a2019-09-27 11:04:27 +010048namespace arm_compute
49{
Georgios Pinitas358ca202017-12-07 16:47:52 +000050using namespace arm_compute::misc::shape_calculator;
Michalis Spyroub27e13a2019-09-27 11:04:27 +010051using namespace arm_compute::utils::cast;
Georgios Pinitas358ca202017-12-07 16:47:52 +000052
53namespace
54{
Georgios Pinitas8b721992019-10-28 16:24:28 +000055Status construct_gemmlowp_output_stage(const ITensorInfo &input, const ITensorInfo &weights, const ITensorInfo &output,
Giorgio Arena1856ff72020-02-07 13:46:45 +000056 GEMMLowpOutputStageInfo &gemmlowp_output_stage, ActivationLayerInfo activation_info)
Georgios Pinitas358ca202017-12-07 16:47:52 +000057{
Georgios Pinitas8b721992019-10-28 16:24:28 +000058 gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
59 gemmlowp_output_stage.gemmlowp_offset = 0;
60 gemmlowp_output_stage.gemmlowp_multiplier = 0;
61 gemmlowp_output_stage.gemmlowp_shift = 0;
62
Sang-Hoon Parkb66aa3b2020-01-10 14:44:13 +000063 const auto data_type = input.data_type();
64
Georgios Pinitas8b721992019-10-28 16:24:28 +000065 // Configure output stage for quantized case
Sang-Hoon Parkb66aa3b2020-01-10 14:44:13 +000066 if(is_data_type_quantized_asymmetric(data_type))
Georgios Pinitas8b721992019-10-28 16:24:28 +000067 {
Giorgio Arena1856ff72020-02-07 13:46:45 +000068 const QuantizationInfo oq_info = output.quantization_info();
69 const UniformQuantizationInfo iq_unif = input.quantization_info().uniform();
70 const UniformQuantizationInfo wq_unif = weights.quantization_info().uniform();
71 const UniformQuantizationInfo oq_unif = oq_info.uniform();
Georgios Pinitas8b721992019-10-28 16:24:28 +000072
Giorgio Arena1856ff72020-02-07 13:46:45 +000073 const auto output_quant_info = (output.total_size() == 0) ? iq_unif : oq_unif;
Georgios Pinitas8b721992019-10-28 16:24:28 +000074
Giorgio Arena1856ff72020-02-07 13:46:45 +000075 const float multiplier = (iq_unif.scale * wq_unif.scale) / output_quant_info.scale;
Georgios Pinitas8b721992019-10-28 16:24:28 +000076 int output_multiplier = 0;
77 int output_shift = 0;
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +010078 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift));
Georgios Pinitas8b721992019-10-28 16:24:28 +000079
Sang-Hoon Parkb66aa3b2020-01-10 14:44:13 +000080 PixelValue type_min{};
81 PixelValue type_max{};
82 std::tie(type_min, type_max) = get_min_max(data_type);
83
Giorgio Arena1856ff72020-02-07 13:46:45 +000084 if(activation_info.enabled())
85 {
Michele Di Giorgio17b71022020-11-16 13:10:07 +000086 std::tie(type_min, type_max) = get_quantized_activation_min_max(activation_info, data_type, output_quant_info);
Giorgio Arena1856ff72020-02-07 13:46:45 +000087 }
88
Georgios Pinitas8b721992019-10-28 16:24:28 +000089 // Set the GEMMLowp output stage info
90 gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset;
91 gemmlowp_output_stage.gemmlowp_multiplier = output_multiplier;
92 gemmlowp_output_stage.gemmlowp_shift = output_shift;
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000093 gemmlowp_output_stage.gemmlowp_multipliers.push_back(output_multiplier);
94 gemmlowp_output_stage.gemmlowp_shifts.push_back(output_shift);
Sang-Hoon Parkb66aa3b2020-01-10 14:44:13 +000095 type_min.get(gemmlowp_output_stage.gemmlowp_min_bound);
96 type_max.get(gemmlowp_output_stage.gemmlowp_max_bound);
Georgios Pinitas8b721992019-10-28 16:24:28 +000097 }
98
99 return Status{};
100}
101
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000102Status 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 +0000103{
104 GEMMLowpOutputStageInfo gemmlowp_output_stage;
Giorgio Arena1856ff72020-02-07 13:46:45 +0000105 ARM_COMPUTE_RETURN_ON_ERROR(construct_gemmlowp_output_stage(input, weights, output, gemmlowp_output_stage, fc_info.activation_info));
Georgios Pinitas8b721992019-10-28 16:24:28 +0000106
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000107 const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped
108 false, // is_b_reshaped
109 true, // reshape_b_only_on_first_run
110 0, // depth_output_gemm3d
111 false, // reinterpret_input_as_3d
112 fc_info.retain_internal_weights, // retain_internal_weights
113 gemmlowp_output_stage, // gemmlowp_output_stage
114 fc_info.fp_mixed_precision, // fp_mixed_precision
115 true, // broadcast_bias
116 ActivationLayerInfo()); // activation_info
Georgios Pinitas8b721992019-10-28 16:24:28 +0000117
Georgios Pinitas358ca202017-12-07 16:47:52 +0000118 if(is_data_type_quantized_asymmetric(input.data_type()))
119 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100120 const UniformQuantizationInfo iq_info = input.quantization_info().uniform();
121 const UniformQuantizationInfo wq_info = weights.quantization_info().uniform();
122
Georgios Pinitas358ca202017-12-07 16:47:52 +0000123 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
124 // Extract and negate input and weights offset
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100125 const QuantizationInfo input_quantization_info(iq_info.scale, -iq_info.offset);
126 const QuantizationInfo weights_quantization_info(wq_info.scale, -wq_info.offset);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000127
128 // Validate gemmlowp function
129 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyCore::validate(&input.clone()->set_quantization_info(input_quantization_info),
130 &weights.clone()->set_quantization_info(weights_quantization_info),
Georgios Pinitas8b721992019-10-28 16:24:28 +0000131 bias,
132 &output,
133 gemm_info));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000134 }
135 else
136 {
Georgios Pinitas8b721992019-10-28 16:24:28 +0000137 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(&input, &weights, bias, &output, 1.f, 1.f, gemm_info));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000138 }
139
140 return Status{};
141}
142} // namespace
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100143
144void CLFullyConnectedLayerReshapeWeights::configure(const ICLTensor *input, ICLTensor *output)
Moritz Pflanzer768e9f12017-08-11 15:33:30 +0100145{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100146 configure(CLKernelLibrary::get().get_compile_context(), input, output);
147}
148
149void CLFullyConnectedLayerReshapeWeights::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output)
150{
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000151 auto k = std::make_unique<CLTransposeKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100152 k->configure(compile_context, input, output);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100153 _kernel = std::move(k);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154}
155
Georgios Pinitas358ca202017-12-07 16:47:52 +0000156Status CLFullyConnectedLayerReshapeWeights::validate(const ITensorInfo *input, const ITensorInfo *output)
157{
158 return CLTransposeKernel::validate(input, output);
159}
160
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100161CLFullyConnectedLayer::CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100162 : _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 +0000163 _mm_gemm(memory_manager, weights_manager), _mm_gemmlowp(memory_manager), _flatten_output(), _converted_weights_output(), _reshape_weights_output(), _are_weights_converted(true),
164 _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 +0100165{
166}
Manuel Bottini2b84be52020-04-08 10:15:51 +0100167void CLFullyConnectedLayer::configure_mm(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output,
168 const FullyConnectedLayerInfo &fc_info)
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000169{
Georgios Pinitas8b721992019-10-28 16:24:28 +0000170 GEMMLowpOutputStageInfo gemmlowp_output_stage;
Giorgio Arena1856ff72020-02-07 13:46:45 +0000171 construct_gemmlowp_output_stage(*input->info(), *weights->info(), *output->info(), gemmlowp_output_stage, fc_info.activation_info);
Georgios Pinitas8b721992019-10-28 16:24:28 +0000172
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000173 const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped
174 false, // is_b_reshaped
175 true, // reshape_b_only_on_first_run
176 0, // depth_output_gemm3d
177 false, // reinterpret_input_as_3d
178 fc_info.retain_internal_weights, // retain_internal_weights
179 gemmlowp_output_stage, // gemmlowp_output_stage
180 fc_info.fp_mixed_precision, // fp_mixed_precision
181 true, // broadcast_bias
Giorgio Arena1856ff72020-02-07 13:46:45 +0000182 fc_info.activation_info); // activation_info
Georgios Pinitas8b721992019-10-28 16:24:28 +0000183
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000184 if(_is_quantized)
185 {
Chunosov5124be52017-11-22 20:42:13 +0700186 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000187 // Extract and negate input and weights offset
Chunosov5124be52017-11-22 20:42:13 +0700188 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
189 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
190
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100191 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset));
192 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset));
Chunosov5124be52017-11-22 20:42:13 +0700193
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000194 // Configure gemmlowp function
Manuel Bottini2b84be52020-04-08 10:15:51 +0100195 _mm_gemmlowp.configure(compile_context, input, weights, bias, output, gemm_info);
Chunosov5124be52017-11-22 20:42:13 +0700196
197 // Revert back QuantizatioInfo as input and weights could be used in other fully connected layers
198 input->info()->set_quantization_info(input_quantization_info);
199 weights->info()->set_quantization_info(weights_quantization_info);
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000200 }
201 else
202 {
203 // Configure matrix multiply kernel
Manuel Bottini2b84be52020-04-08 10:15:51 +0100204 _mm_gemm.configure(compile_context, input, weights, bias, output, 1.f, 1.f, gemm_info);
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000205 }
206}
207
Manuel Bottini2b84be52020-04-08 10:15:51 +0100208void CLFullyConnectedLayer::configure_conv_fc(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output,
209 const FullyConnectedLayerInfo &fc_info)
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100210{
211 ARM_COMPUTE_ERROR_ON((weights->info()->dimension(1) != (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2))));
212
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100213 // If the fully connected layer is called after a convolution layer, the input tensor must be linearized
214
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100215 // Initialize output tensor for flatten
216 TensorShape shape_flatten = compute_flatten_shape(input->info());
217 _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 +0100218
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100219 // Configure flatten kernel
220 _memory_group.manage(&_flatten_output);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100221 _flatten_layer.configure(compile_context, input, &_flatten_output);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100222
223 // Configure matrix multiply kernel
Manuel Bottini2b84be52020-04-08 10:15:51 +0100224 configure_mm(compile_context, &_flatten_output, weights, bias, output, fc_info);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100225
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100226 // Allocate the output tensor for flatten once all the configure methods have been called
227 _flatten_output.allocator()->allocate();
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100228}
229
Manuel Bottini2b84be52020-04-08 10:15:51 +0100230void CLFullyConnectedLayer::configure_fc_fc(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output,
231 const FullyConnectedLayerInfo &fc_info)
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100232{
233 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != weights->info()->dimension(1));
234
235 // Configure matrix multiply kernel
Manuel Bottini2b84be52020-04-08 10:15:51 +0100236 configure_mm(compile_context, input, weights, bias, output, fc_info);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100237}
238
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100239void CLFullyConnectedLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
240 FullyConnectedLayerInfo fc_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100241{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100242 configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, fc_info);
243}
244
245void CLFullyConnectedLayer::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
246 FullyConnectedLayerInfo fc_info)
247{
Georgios Pinitas358ca202017-12-07 16:47:52 +0000248 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
249
250 // Perform validate step
251 ARM_COMPUTE_ERROR_THROW_ON(CLFullyConnectedLayer::validate(input->info(),
252 weights->info(),
253 biases != nullptr ? biases->info() : nullptr,
254 output->info(),
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100255 fc_info));
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100256
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100257 _are_weights_converted = true;
258 _are_weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true;
259 _is_fc_after_conv = true;
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100260 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Michele Di Giorgioba1ffe92018-08-22 14:28:30 +0100261 _is_prepared = fc_info.retain_internal_weights;
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100262 _original_weights = weights;
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100263
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100264 if(_weights_manager)
265 {
266 _weights_manager->manage(weights);
267 }
268
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100269 const ICLTensor *weights_to_use = weights;
270
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100271 // With the Fully Connected layer we can have 4 different cases:
272 // 1) Convolution layer -> Fully Connected layer without batches
273 // 2) Fully Connected layer -> Fully Connected layer without batches
274 // 3) Convolution layer -> Fully Connected layer with batches
275 // 4) Fully Connected layer -> Fully Connected layer with batches
276
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100277 // Check if we have a fully connected layer with batches
278 const bool is_batched_fc_layer = output->info()->dimension(1) > 1;
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100279 if(is_batched_fc_layer)
Moritz Pflanzer768e9f12017-08-11 15:33:30 +0100280 {
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100281 _is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->info()->tensor_shape().cbegin() + 3,
282 input->info()->tensor_shape().cend(),
283 output->info()->tensor_shape().cbegin() + 1));
Moritz Pflanzer768e9f12017-08-11 15:33:30 +0100284 }
285 else
286 {
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100287 _is_fc_after_conv = input->info()->num_dimensions() > 1;
Moritz Pflanzer768e9f12017-08-11 15:33:30 +0100288 }
289
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100290 // Reshape weights if needed
291 if(!_are_weights_reshaped)
292 {
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100293 if(_weights_manager && _weights_manager->are_weights_managed(weights))
294 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100295 _reshape_weights_managed_function.configure(compile_context, weights);
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100296 weights_to_use = utils::cast::polymorphic_downcast<ICLTensor *>(_weights_manager->acquire(weights, &_reshape_weights_managed_function));
297 }
298 else
299 {
300 // Reshape the weights
Manuel Bottini2b84be52020-04-08 10:15:51 +0100301 _reshape_weights_function.configure(compile_context, weights, &_reshape_weights_output);
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100302 weights_to_use = &_reshape_weights_output;
303 }
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100304 }
305
306 // Convert weights if needed
307 if(_is_fc_after_conv && (input->info()->data_layout() != fc_info.weights_trained_layout))
308 {
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100309 if(_weights_manager && _weights_manager->are_weights_managed(weights_to_use))
310 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100311 _convert_weights_managed.configure(compile_context, weights_to_use,
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100312 input->info()->tensor_shape(),
313 fc_info.weights_trained_layout);
314 weights_to_use = utils::cast::polymorphic_downcast<ICLTensor *>(_weights_manager->acquire(weights, &_convert_weights_managed));
315 }
316 else
317 {
318 // Convert weights
Manuel Bottini2b84be52020-04-08 10:15:51 +0100319 _convert_weights.configure(compile_context, weights_to_use,
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100320 &_converted_weights_output,
321 input->info()->tensor_shape(),
322 fc_info.weights_trained_layout);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100323
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100324 weights_to_use = &_converted_weights_output;
325 }
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100326 _are_weights_converted = false;
327 }
328
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100329 if(_is_fc_after_conv)
Moritz Pflanzer768e9f12017-08-11 15:33:30 +0100330 {
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100331 // Fully Connected layer after a Convolution Layer without batches
Manuel Bottini2b84be52020-04-08 10:15:51 +0100332 configure_conv_fc(compile_context, input, weights_to_use, biases, output, fc_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100333 }
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100334 else
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100335 {
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100336 // Fully Connected layer after a Fully Connected Layer without batches
Manuel Bottini2b84be52020-04-08 10:15:51 +0100337 configure_fc_fc(compile_context, input, weights_to_use, biases, output, fc_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100338 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100339}
340
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100341Status CLFullyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
342 FullyConnectedLayerInfo fc_info)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000343{
344 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Sang-Hoon Parkb66aa3b2020-01-10 14:44:13 +0000345 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 +0000346 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
347 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 2);
Giorgio Arena1856ff72020-02-07 13:46:45 +0000348 ARM_COMPUTE_RETURN_ERROR_ON(fc_info.activation_info.enabled() && is_data_type_quantized(input->data_type()) && fc_info.activation_info.activation() != ActivationLayerInfo::ActivationFunction::RELU
349 && fc_info.activation_info.activation() != ActivationLayerInfo::ActivationFunction::BOUNDED_RELU && fc_info.activation_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000350
Georgios Pinitas8b721992019-10-28 16:24:28 +0000351 bool weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true;
352 bool is_fc_after_conv = true;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000353
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100354 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 +0100355 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 +0100356 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 +0000357
358 // With the Fully Connected layer we can have 4 different cases:
359 // 1) Convolution layer -> Fully Connected layer without batches
360 // 2) Fully Connected layer -> Fully Connected layer without batches
361 // 3) Convolution layer -> Fully Connected layer with batches
362 // 4) Fully Connected layer -> Fully Connected layer with batches
363
364 const ITensorInfo *input_to_use = input;
365 const ITensorInfo *weights_to_use = weights;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000366
Georgios Pinitas358ca202017-12-07 16:47:52 +0000367 // Check if we have a fully connected layer with batches
368 const bool is_batched_fc_layer = output->dimension(1) > 1;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000369 if(is_batched_fc_layer)
370 {
371 is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->tensor_shape().cbegin() + 3,
372 input->tensor_shape().cend(),
373 output->tensor_shape().cbegin() + 1));
374 }
375 else
376 {
377 is_fc_after_conv = input->num_dimensions() > 1;
378 }
379
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100380 if(!weights_reshaped)
381 {
382 // Validate reshape weights kernel
383 ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayerReshapeWeights::validate(weights, &reshaped_weights));
384 weights_to_use = &reshaped_weights;
385 }
386
387 if(is_fc_after_conv && (input->data_layout() != fc_info.weights_trained_layout))
388 {
389 // Validate convert weights kernel
390 ARM_COMPUTE_RETURN_ON_ERROR(CLConvertFullyConnectedWeights::validate(weights_to_use,
391 &converted_weights,
392 input->tensor_shape(),
393 fc_info.weights_trained_layout));
394 weights_to_use = &converted_weights;
395 }
396
Georgios Pinitas358ca202017-12-07 16:47:52 +0000397 if(is_fc_after_conv)
398 {
399 // Fully Connected layer after a Convolution Layer without batches
400 ARM_COMPUTE_RETURN_ERROR_ON((weights_to_use->dimension(1) != (input->dimension(0) * input->dimension(1) * input->dimension(2))));
401
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100402 // Validate flatten kernel
403 ARM_COMPUTE_RETURN_ON_ERROR(CLFlattenLayer::validate(input, &flatten_input));
404 input_to_use = &flatten_input;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000405 }
406 else
407 {
408 // Fully Connected layer after a Fully Connected Layer without batches
409 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != weights_to_use->dimension(1));
410 }
Georgios Pinitas8b721992019-10-28 16:24:28 +0000411
Georgios Pinitas358ca202017-12-07 16:47:52 +0000412 // Validate matrix multiply kernel
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000413 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(*input_to_use, *weights_to_use, biases, *output, fc_info));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000414
415 return Status{};
416}
417
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100418void CLFullyConnectedLayer::run()
419{
Georgios Pinitase0437672018-05-02 14:07:55 +0100420 prepare();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100421
Georgios Pinitasda953f22019-04-02 17:27:03 +0100422 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100423
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100424 // Linearize input if it comes from a convolutional layer
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100425 if(_is_fc_after_conv)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100426 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100427 _flatten_layer.run();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100428 }
429
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100430 // Run matrix multiply
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000431 if(_is_quantized)
432 {
433 _mm_gemmlowp.run();
434 }
435 else
436 {
Gian Marco Iodicec9c62c22018-04-06 10:00:10 +0100437 _mm_gemm.run();
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000438 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100439}
Georgios Pinitase0437672018-05-02 14:07:55 +0100440
441void CLFullyConnectedLayer::prepare()
442{
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100443 if(!_is_prepared)
Georgios Pinitase0437672018-05-02 14:07:55 +0100444 {
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100445 if(!_weights_manager)
446 {
447 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
448 }
Georgios Pinitase0437672018-05-02 14:07:55 +0100449
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100450 auto release_unused = [](CLTensor * w)
451 {
452 if(!w->is_used())
453 {
454 CLScheduler::get().queue().finish();
455 w->allocator()->free();
456 }
457 };
458
459 // Pointer to current weights
460 const ICLTensor *cur_weights = _original_weights;
461
462 // Reshape of the weights if needed (happens only once)
463 if(!_are_weights_reshaped)
464 {
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100465 if(_weights_manager && _weights_manager->are_weights_managed(_original_weights))
466 {
467 cur_weights = utils::cast::polymorphic_downcast<ICLTensor *>(_weights_manager->run(cur_weights, &_reshape_weights_managed_function));
468 }
469 else
470 {
471 // Run reshape weights kernel and mark weights as unused
472 _reshape_weights_output.allocator()->allocate();
473 _reshape_weights_function.run();
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100474
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100475 cur_weights->mark_as_unused();
476 cur_weights = &_reshape_weights_output;
477 }
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100478 _are_weights_reshaped = true;
479 }
480
481 // Convert weights if needed (happens only once)
482 if(!_are_weights_converted)
483 {
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100484 if(_weights_manager && _weights_manager->are_weights_managed(cur_weights))
485 {
486 _weights_manager->run(cur_weights, &_convert_weights_managed);
487 }
488 else
489 {
490 _converted_weights_output.allocator()->allocate();
491 _convert_weights.run();
492 cur_weights->mark_as_unused();
493 }
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100494
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100495 _are_weights_converted = true;
496 }
497
498 // Release reshaped weights if unused
499 release_unused(&_reshape_weights_output);
Georgios Pinitase0437672018-05-02 14:07:55 +0100500
501 // Prepare GEMM prepare and release unused weights
502 if(!_is_quantized)
503 {
504 _mm_gemm.prepare();
Georgios Pinitase0437672018-05-02 14:07:55 +0100505 }
506
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100507 // Release converted weights if unused
508 release_unused(&_reshape_weights_output);
509 release_unused(&_converted_weights_output);
510
511 _is_prepared = true;
Georgios Pinitase0437672018-05-02 14:07:55 +0100512 }
513}
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100514} // namespace arm_compute