blob: 70584a64f87b87deb028561c8ae9b993d76b60c6 [file] [log] [blame]
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +01001/*
Jonathan Deakin464ed202023-01-12 11:41:14 +00002 * Copyright (c) 2021-2023 Arm Limited.
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/operators/CpuFullyConnected.h"
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010025
26#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/ITensorPack.h"
28#include "arm_compute/core/Validate.h"
29#include "arm_compute/core/utils/misc/ShapeCalculator.h"
30#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
31#include "arm_compute/runtime/NEON/NEScheduler.h"
ramelg013ae3d882021-09-12 23:07:47 +010032#include "src/common/utils/Log.h"
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010033#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/MemoryHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010035#include "src/cpu/kernels/CpuTransposeKernel.h"
36#include "src/cpu/operators/CpuConvertFullyConnectedWeights.h"
37#include "src/cpu/operators/CpuFlatten.h"
38#include "src/cpu/operators/CpuGemm.h"
39#include "src/cpu/operators/CpuGemmLowpMatrixMultiplyCore.h"
40#include "src/cpu/utils/CpuAuxTensorHandler.h"
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010041
42namespace arm_compute
43{
44namespace cpu
45{
46using namespace arm_compute::experimental;
47using namespace arm_compute::misc::shape_calculator;
48
49namespace
50{
51// Get min, max bound of a quantized asymmetric dst tensor, with the effect of fused activation
52std::pair<PixelValue, PixelValue> get_quantized_asymmetric_output_min_max(const QuantizationInfo &q_info, const ActivationLayerInfo &act_info, DataType data_type)
53{
54 PixelValue type_min{};
55 PixelValue type_max{};
Milos Puzovic13b623e2022-07-27 17:53:21 +000056 std::tie(type_min, type_max) = get_min_max(data_type);
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010057 const UniformQuantizationInfo q_unif = q_info.uniform();
58
59 if(act_info.enabled())
60 {
61 switch(act_info.activation())
62 {
63 case ActivationLayerInfo::ActivationFunction::RELU:
64 type_min = PixelValue(q_unif.offset);
65 break;
66 case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU:
67 type_min = PixelValue(q_unif.offset);
68 type_max = PixelValue(act_info.a(), data_type, q_info);
69 break;
70 case ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU:
71 type_min = PixelValue(act_info.b(), data_type, q_info);
72 type_max = PixelValue(act_info.a(), data_type, q_info);
73 break;
74 default:
75 ARM_COMPUTE_ERROR("Activation function not supported.");
76 break;
77 }
78 }
79
80 return std::make_pair(type_min, type_max);
81}
82
83Status get_gemmlowp_output_stage_info(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const ActivationLayerInfo &act,
84 GEMMLowpOutputStageInfo &gemmlowp_output_stage_info)
85{
86 const auto data_type = src->data_type();
87 const QuantizationInfo oq_info = dst->quantization_info();
88 const UniformQuantizationInfo iq_unif = src->quantization_info().uniform();
89 const UniformQuantizationInfo wq_unif = weights->quantization_info().uniform();
90 const UniformQuantizationInfo oq_unif = oq_info.uniform();
91
92 float multiplier = (iq_unif.scale * wq_unif.scale) / oq_unif.scale;
93 int32_t output_multiplier;
94 int32_t output_shift;
95
96 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift));
97
98 PixelValue type_min{};
99 PixelValue type_max{};
100 std::tie(type_min, type_max) = get_quantized_asymmetric_output_min_max(oq_info, act, data_type);
101
102 gemmlowp_output_stage_info.gemmlowp_multiplier = output_multiplier;
103 gemmlowp_output_stage_info.gemmlowp_shift = output_shift;
104 gemmlowp_output_stage_info.gemmlowp_offset = oq_unif.offset;
105 gemmlowp_output_stage_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
106 gemmlowp_output_stage_info.gemmlowp_min_bound = type_min.get<int32_t>();
107 gemmlowp_output_stage_info.gemmlowp_max_bound = type_max.get<int32_t>();
108
109 return Status{};
110}
111
Jonathan Deakin464ed202023-01-12 11:41:14 +0000112Status validate_mm(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const ActivationLayerInfo &act, bool enable_fast_math, WeightFormat weight_format)
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100113{
114 if(is_data_type_quantized_asymmetric(src->data_type()))
115 {
116 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
117 // Extract and negate src and weights offset
118 const QuantizationInfo src_quantization_info(src->quantization_info().uniform().scale, -src->quantization_info().uniform().offset);
119 const QuantizationInfo weights_quantization_info(weights->quantization_info().uniform().scale, -weights->quantization_info().uniform().offset);
120
121 GEMMLowpOutputStageInfo gemmlowp_output_stage_info;
122 ARM_COMPUTE_RETURN_ON_ERROR(get_gemmlowp_output_stage_info(src, weights, dst, act, gemmlowp_output_stage_info));
123
124 GEMMInfo gemm_info;
125 gemm_info.set_gemmlowp_output_stage(gemmlowp_output_stage_info);
cfRodf2c022e2021-11-05 11:29:53 +0000126 gemm_info.set_fast_math(enable_fast_math);
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100127
128 // Validate gemmlowp function
129 TensorInfo src_info = src->clone()->set_quantization_info(src_quantization_info);
130 TensorInfo weights_info = weights->clone()->set_quantization_info(weights_quantization_info);
131 ARM_COMPUTE_RETURN_ON_ERROR(CpuGemmLowpMatrixMultiplyCore::validate(&src_info,
132 &weights_info,
133 biases,
134 dst,
135 gemm_info));
136 }
137 else
138 {
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +0100139 GEMMInfo gemm_info;
Jonathan Deakin464ed202023-01-12 11:41:14 +0000140 gemm_info.set_weight_format(weight_format);
141 gemm_info.set_fixed_format(weight_format != WeightFormat::UNSPECIFIED);
cfRodf2c022e2021-11-05 11:29:53 +0000142 gemm_info.set_fast_math(enable_fast_math);
143 ARM_COMPUTE_RETURN_ON_ERROR(CpuGemm::validate(src, weights, biases, dst, 1.f, 1.0f, gemm_info));
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100144 }
145
146 return Status{};
147}
148} // namespace
149
150CpuFullyConnected::CpuFullyConnected()
151 : _flatten(nullptr),
152 _convert_weights(nullptr),
153 _transpose_weights(nullptr),
154 _mm_gemm(nullptr),
155 _mm_gemmlowp(nullptr),
156 _flattened_src(),
157 _converted_weights(),
158 _reshaped_weights(),
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100159 _trans_weights(),
160 _trans_weights_idx(AuxTensorIdx::Count),
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100161 _aux_mem(Count),
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100162 _needs_weights_conversion(false),
163 _needs_weights_reshape(false),
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100164 _is_fc_after_conv(false),
165 _is_quantized_asymmetric(false),
cfRodf2c022e2021-11-05 11:29:53 +0000166 _is_prepared(false),
Milos Puzovic13b623e2022-07-27 17:53:21 +0000167 _enable_fast_math(false),
168 _fixed_format(false),
Viet-Hoa Doa3e57c22023-03-13 16:20:04 +0000169 _weight_format(arm_compute::WeightFormat::UNSPECIFIED),
170 _dynamic_weights(false)
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100171{
172}
173
174CpuFullyConnected::~CpuFullyConnected() = default;
175
176void CpuFullyConnected::configure_mm(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, const ActivationLayerInfo &act)
177{
178 if(_is_quantized_asymmetric)
179 {
180 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
181 // Extract and negate src and weights offset
182 const QuantizationInfo src_quantization_info(src->quantization_info().uniform().scale, -src->quantization_info().uniform().offset);
183 const QuantizationInfo weights_quantization_info(weights->quantization_info().uniform().scale, -weights->quantization_info().uniform().offset);
184
185 TensorInfo src_info = src->clone()->set_quantization_info(src_quantization_info);
186 TensorInfo weights_info = weights->clone()->set_quantization_info(weights_quantization_info);
187
188 // Configure gemmlowp function and output stage for asymmetric quantized types
189 GEMMLowpOutputStageInfo gemmlowp_output_stage_info;
190 const Status status = get_gemmlowp_output_stage_info(&src_info, &weights_info, dst, act, gemmlowp_output_stage_info);
191 ARM_COMPUTE_ERROR_ON(status.error_code() != ErrorCode::OK);
192
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +0100193 GEMMInfo gemm_info;
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100194 gemm_info.set_gemmlowp_output_stage(gemmlowp_output_stage_info);
195 gemm_info.set_activation_info(act);
cfRodf2c022e2021-11-05 11:29:53 +0000196 gemm_info.set_fast_math(_enable_fast_math);
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100197 _mm_gemmlowp = std::make_unique<CpuGemmLowpMatrixMultiplyCore>();
198 _mm_gemmlowp->configure(&src_info, &weights_info, biases, dst, gemm_info);
199 }
200 else
201 {
202 // Configure matrix multiply kernel
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +0100203 GEMMInfo gemm_info;
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100204 gemm_info.set_activation_info(act);
cfRodf2c022e2021-11-05 11:29:53 +0000205 gemm_info.set_fast_math(_enable_fast_math);
Milos Puzovic13b623e2022-07-27 17:53:21 +0000206 gemm_info.set_fixed_format(_fixed_format);
207 gemm_info.set_weight_format(_weight_format);
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100208 _mm_gemm = std::make_unique<CpuGemm>();
209 _mm_gemm->configure(src, weights, biases, dst, 1.f, 1.0f, gemm_info);
210 }
211}
212
213void CpuFullyConnected::configure_conv_fc(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, const ActivationLayerInfo &act)
214{
215 ARM_COMPUTE_ERROR_ON((weights->dimension(1) != (src->dimension(0) * src->dimension(1) * src->dimension(2))));
216
217 // If the fully connected layer is called after a convolution layer, the src tensor must be linearized
218
219 // Initialize output tensor for flatten
220 auto_init_if_empty(_flattened_src, src->clone()->set_tensor_shape(compute_flatten_shape(src)));
221
222 _flatten = std::make_unique<CpuFlatten>();
223 _flatten->configure(src, &_flattened_src);
224
225 // Configure matrix multiply kernel
226 configure_mm(&_flattened_src, weights, biases, dst, act);
227}
228
229void CpuFullyConnected::configure_fc_fc(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, const ActivationLayerInfo &act)
230{
231 ARM_COMPUTE_ERROR_ON(src->dimension(0) != weights->dimension(1));
232
233 // Configure matrix multiply kernel
234 configure_mm(src, weights, biases, dst, act);
235}
236
237void CpuFullyConnected::configure(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst,
Milos Puzovic13b623e2022-07-27 17:53:21 +0000238 FullyConnectedLayerInfo fc_info, const WeightsInfo &weights_info)
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100239{
240 // Perform validate step
241 ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst);
242 ARM_COMPUTE_ERROR_THROW_ON(CpuFullyConnected::validate(src,
243 weights,
244 biases != nullptr ? biases : nullptr,
245 dst,
Jonathan Deakin464ed202023-01-12 11:41:14 +0000246 fc_info,
247 weights_info));
ramelg013ae3d882021-09-12 23:07:47 +0100248 ARM_COMPUTE_LOG_PARAMS(src, weights, biases, dst, fc_info);
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100249
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100250 _needs_weights_conversion = false;
251 _needs_weights_reshape = fc_info.transpose_weights ? !fc_info.are_weights_reshaped : false;
252 _needs_weights_reshape = _needs_weights_reshape && !fc_info.retain_internal_weights;
253 _is_fc_after_conv = true;
254 _is_quantized_asymmetric = is_data_type_quantized_asymmetric(src->data_type());
255 _is_prepared = false;
256 _trans_weights_idx = AuxTensorIdx::Count;
cfRodf2c022e2021-11-05 11:29:53 +0000257 _enable_fast_math = fc_info.enable_fast_math;
Milos Puzovic13b623e2022-07-27 17:53:21 +0000258 _fixed_format = weights_info.weight_format() != WeightFormat::UNSPECIFIED;
259 _weight_format = weights_info.weight_format();
Viet-Hoa Doa3e57c22023-03-13 16:20:04 +0000260 _dynamic_weights = !weights->are_values_constant() && _needs_weights_reshape;
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100261
262 // With the Fully Connected layer we can have 4 different cases:
263 // 1) Convolution layer -> Fully Connected layer without batches
264 // 2) Fully Connected layer -> Fully Connected layer without batches
265 // 3) Convolution layer -> Fully Connected layer with batches
266 // 4) Fully Connected layer -> Fully Connected layer with batches
267
268 const ITensorInfo *weights_to_use = weights;
269
270 // Check if we have a fully connected layer with batches
271 const bool is_batched_fc_layer = dst->dimension(1) > 1;
272 if(is_batched_fc_layer)
273 {
Milos Puzovic13b623e2022-07-27 17:53:21 +0000274 _is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(src->tensor_shape().cbegin() + 3, src->tensor_shape().cend(), dst->tensor_shape().cbegin() + 1));
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100275 }
276 else
277 {
278 _is_fc_after_conv = src->num_dimensions() > 1;
279 }
280
281 // Reshape weights if needed
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100282 if(_needs_weights_reshape)
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100283 {
284 // Reshape the weights
285 _transpose_weights = std::make_unique<kernels::CpuTransposeKernel>();
286 _transpose_weights->configure(weights, &_reshaped_weights);
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +0100287 _reshaped_weights.set_are_values_constant(weights->are_values_constant());
288
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100289 weights_to_use = &_reshaped_weights;
290 _trans_weights_idx = AuxTensorIdx::TransposedWeights;
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100291 }
292
293 // Convert weights if needed
294 if(_is_fc_after_conv && (src->data_layout() != fc_info.weights_trained_layout))
295 {
296 // Convert weights
297 _convert_weights = std::make_unique<CpuConvertFullyConnectedWeights>();
298 _convert_weights->configure(weights_to_use,
299 &_converted_weights,
300 src->tensor_shape(),
301 fc_info.weights_trained_layout);
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +0100302 _converted_weights.set_are_values_constant(weights_to_use->are_values_constant());
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100303
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100304 weights_to_use = &_converted_weights;
305 _needs_weights_conversion = true;
306 _trans_weights_idx = AuxTensorIdx::ConvertedWeights;
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100307 }
308
309 if(_is_fc_after_conv)
310 {
311 // Fully Connected layer after a Convolution Layer without batches
312 configure_conv_fc(src, weights_to_use, biases, dst, fc_info.activation_info);
313 }
314 else
315 {
316 // Fully Connected layer after a Fully Connected Layer without batches
317 configure_fc_fc(src, weights_to_use, biases, dst, fc_info.activation_info);
318 }
319
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100320 // Retain the tensorinfo with the weights to use
321 if(_needs_weights_reshape || _needs_weights_conversion)
322 {
323 _trans_weights = *weights_to_use;
324 }
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100325
326 // Set auxiliary memory requirements
327 auto gemm_mem_req = (_is_quantized_asymmetric) ? _mm_gemmlowp->workspace() : _mm_gemm->workspace();
328 for(unsigned int i = 0; i < gemm_mem_req.size(); ++i)
329 {
330 _aux_mem[i] = gemm_mem_req[i];
331 }
332
333 if(_aux_mem[Pretranspose].size > 0)
334 {
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100335 // Release permuted weights at the end of prepare as they are further transposed by the assembly dispatch
336 // Do not release them if biases are dynamic and data type is quantized, since the weights tensor will be used for biases offset calculation
Viet-Hoa Doa3e57c22023-03-13 16:20:04 +0000337 // Keep all the auxiliary tensors in case of dynamic weights as they are recalculated every time.
338 _aux_mem[TransposedWeights] = MemoryInfo(
339 offset_int_vec(TransposedWeights),
340 _dynamic_weights ? MemoryLifetime::Temporary :
341 (_is_quantized_asymmetric && biases && !(biases->are_values_constant())) ? MemoryLifetime::Persistent :
342 MemoryLifetime::Prepare,
343 _reshaped_weights.total_size());
344
345 _aux_mem[ConvertedWeights] = MemoryInfo(
346 offset_int_vec(ConvertedWeights),
347 _dynamic_weights ? MemoryLifetime::Temporary : MemoryLifetime::Prepare,
348 _converted_weights.total_size());
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100349 }
350 else
351 {
Viet-Hoa Doa3e57c22023-03-13 16:20:04 +0000352 _aux_mem[TransposedWeights] = MemoryInfo(
353 offset_int_vec(TransposedWeights),
354 _dynamic_weights ? MemoryLifetime::Temporary :
355 _needs_weights_conversion ? MemoryLifetime::Prepare :
356 MemoryLifetime::Persistent,
357 _reshaped_weights.total_size());
358
359 _aux_mem[ConvertedWeights] = MemoryInfo(
360 offset_int_vec(ConvertedWeights),
361 _dynamic_weights ? MemoryLifetime::Temporary : MemoryLifetime::Persistent,
362 _converted_weights.total_size());
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100363 }
364 _aux_mem[FlattenedSrc] = MemoryInfo(offset_int_vec(FlattenedSrc), MemoryLifetime::Temporary, _flattened_src.total_size());
365}
366
Milos Puzovic13b623e2022-07-27 17:53:21 +0000367Status CpuFullyConnected::has_opt_impl(arm_compute::WeightFormat &expected_weight_format, const ITensorInfo *src, const ITensorInfo *weights,
368 const ITensorInfo *biases, const ITensorInfo *dst, FullyConnectedLayerInfo fc_info, WeightsInfo weights_info)
369{
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +0100370 GEMMInfo gemm_info;
Milos Puzovic13b623e2022-07-27 17:53:21 +0000371 gemm_info.set_activation_info(fc_info.activation_info);
372 gemm_info.set_fast_math(fc_info.enable_fast_math);
373 gemm_info.set_fixed_format(weights_info.weight_format() != WeightFormat::UNSPECIFIED);
374 gemm_info.set_weight_format(weights_info.weight_format());
375
376 return CpuGemm::has_opt_impl(expected_weight_format, src, weights, biases, dst, gemm_info);
377}
378
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100379Status CpuFullyConnected::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst,
Jonathan Deakin464ed202023-01-12 11:41:14 +0000380 FullyConnectedLayerInfo fc_info, const WeightsInfo &weights_info)
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100381{
382 ARM_COMPUTE_UNUSED(fc_info.retain_internal_weights);
383 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, weights, dst);
384 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
Jonathan Deakin464ed202023-01-12 11:41:14 +0000385
386 if (is_fixed_format_fast_math(weights_info.weight_format()))
387 {
388 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(src, DataType::F32);
389 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(weights, DataType::BFLOAT16);
390 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(dst, DataType::F32);
391 }
392 else
393 {
394 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, weights, dst);
395 }
396
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100397 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 2);
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100398 ARM_COMPUTE_RETURN_ERROR_ON(fc_info.activation_info.enabled() && is_data_type_quantized(src->data_type()) && fc_info.activation_info.activation() != ActivationLayerInfo::ActivationFunction::RELU
399 && fc_info.activation_info.activation() != ActivationLayerInfo::ActivationFunction::BOUNDED_RELU && fc_info.activation_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU);
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100400
401 bool weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true;
402 bool is_fc_after_conv = true;
403
404 const ITensorInfo &flatten_src = TensorInfo(src->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_flatten_shape(src)));
405 const ITensorInfo &reshaped_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_transposed_shape(*weights)));
406 const ITensorInfo &converted_weights = weights_reshaped ? TensorInfo(weights->clone()->set_is_resizable(true).reset_padding()) : TensorInfo(*reshaped_weights.clone());
407
408 // With the Fully Connected layer we can have 4 different cases:
409 // 1) Convolution layer -> Fully Connected layer without batches
410 // 2) Fully Connected layer -> Fully Connected layer without batches
411 // 3) Convolution layer -> Fully Connected layer with batches
412 // 4) Fully Connected layer -> Fully Connected layer with batches
413
414 const ITensorInfo *src_to_use = src;
415 const ITensorInfo *weights_to_use = weights;
416
417 // Check if we have a fully connected layer with batches
418 const bool is_batched_fc_layer = dst->dimension(1) > 1;
419
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100420 if(biases != nullptr)
421 {
422 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
423 if(is_data_type_quantized(src->data_type()))
424 {
425 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
426 }
427 else
428 {
429 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, biases);
430 }
431 }
432
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100433 if(is_batched_fc_layer)
434 {
Milos Puzovic13b623e2022-07-27 17:53:21 +0000435 is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(src->tensor_shape().cbegin() + 3, src->tensor_shape().cend(), dst->tensor_shape().cbegin() + 1));
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100436 }
437 else
438 {
439 is_fc_after_conv = src->num_dimensions() > 1;
440 }
441
442 if(!weights_reshaped)
443 {
444 // Validate reshape weights kernel
445 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuTransposeKernel::validate(weights, &reshaped_weights));
446 weights_to_use = &reshaped_weights;
447 }
448
449 if(is_fc_after_conv && (src->data_layout() != fc_info.weights_trained_layout))
450 {
451 // Validate convert weights kernel
452 ARM_COMPUTE_RETURN_ON_ERROR(CpuConvertFullyConnectedWeights::validate(weights_to_use,
453 &converted_weights,
454 src->tensor_shape(),
455 fc_info.weights_trained_layout));
456 weights_to_use = &converted_weights;
457 }
458
459 if(is_fc_after_conv)
460 {
461 // Fully Connected layer after a Convolution Layer without batches
462 ARM_COMPUTE_RETURN_ERROR_ON((weights_to_use->dimension(1) != (src->dimension(0) * src->dimension(1) * src->dimension(2))));
463
464 // Validate flatten kernel
465 ARM_COMPUTE_RETURN_ON_ERROR(CpuFlatten::validate(src, &flatten_src));
466 src_to_use = &flatten_src;
467 }
468 else
469 {
470 // Fully Connected layer after a Fully Connected Layer without batches
471 ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(0) != weights_to_use->dimension(1));
472 }
473 // Validate matrix multiply kernel
Jonathan Deakin464ed202023-01-12 11:41:14 +0000474 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(src_to_use, weights_to_use, biases, dst, fc_info.activation_info, fc_info.enable_fast_math, weights_info.weight_format()));
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100475
476 return Status{};
477}
478
479void CpuFullyConnected::run(ITensorPack &tensors)
480{
481 prepare(tensors);
482
Viet-Hoa Doa3e57c22023-03-13 16:20:04 +0000483#ifdef ARM_COMPUTE_ASSERTS_ENABLED
484 ++_asrt_run_count;
485 ARM_COMPUTE_ERROR_ON(_dynamic_weights && _asrt_prepare_count != _asrt_run_count);
486#endif // ARM_COMPUTE_ASSERTS_ENABLED
487
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100488 auto src = tensors.get_const_tensor(ACL_SRC_0);
489
490 CpuAuxTensorHandler flattened_src(offset_int_vec(FlattenedSrc), _flattened_src, tensors, false);
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100491 CpuAuxTensorHandler transformed_wei(offset_int_vec(_trans_weights_idx), _trans_weights, tensors, false);
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100492
493 // Linearize src if it comes from a convolutional layer
494 if(_is_fc_after_conv)
495 {
496 ITensorPack flatten_pack{ { ACL_SRC, src }, { ACL_DST, flattened_src.get() } };
497 _flatten->run(flatten_pack);
498 }
499
500 ITensorPack gemm_pack = tensors;
501 gemm_pack.add_const_tensor(ACL_SRC_0, (_is_fc_after_conv) ? flattened_src.get() : src);
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100502 if(_needs_weights_reshape || _needs_weights_conversion)
503 {
504 gemm_pack.add_const_tensor(ACL_SRC_1, transformed_wei.get());
505 }
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100506
507 // Run matrix multiply
508 if(_is_quantized_asymmetric)
509 {
510 _mm_gemmlowp->run(gemm_pack);
511 }
512 else
513 {
514 _mm_gemm->run(gemm_pack);
515 }
516}
517
518void CpuFullyConnected::prepare(ITensorPack &tensors)
519{
Viet-Hoa Doa3e57c22023-03-13 16:20:04 +0000520 if(!_is_prepared || _dynamic_weights)
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100521 {
Viet-Hoa Doa3e57c22023-03-13 16:20:04 +0000522#ifdef ARM_COMPUTE_ASSERTS_ENABLED
523 ++_asrt_prepare_count;
524 ARM_COMPUTE_ERROR_ON(!_dynamic_weights && _asrt_prepare_count > 1);
525#endif // ARM_COMPUTE_ASSERTS_ENABLED
526
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100527 auto weights = tensors.get_const_tensor(ACL_SRC_1);
528
529 CpuAuxTensorHandler reshaped_weights(offset_int_vec(TransposedWeights), _reshaped_weights, tensors, false);
530 CpuAuxTensorHandler converted_weights(offset_int_vec(ConvertedWeights), _converted_weights, tensors, false);
531
532 // Pointer to current weights
533 const ITensor *cur_weights = weights;
534
535 // Reshape of the weights (happens only once)
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100536 if(_needs_weights_reshape)
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100537 {
538 // Run reshape weights kernel and mark weights as unused
539 ITensorPack transpose_pack{ { ACL_SRC, weights }, { ACL_DST, reshaped_weights.get() } };
540 NEScheduler::get().schedule_op(_transpose_weights.get(), Window::DimY, _transpose_weights->window(), transpose_pack);
541
542 cur_weights->mark_as_unused();
543 cur_weights = reshaped_weights.get();
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100544 }
545
546 // Convert weights if needed (happens only once)
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100547 if(_needs_weights_conversion)
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100548 {
549 ITensorPack convert_pack{ { ACL_SRC, cur_weights }, { ACL_DST, converted_weights.get() } };
550 _convert_weights->run(convert_pack);
551
552 cur_weights->mark_as_unused();
553 cur_weights = converted_weights.get();
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100554 }
555
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100556 ITensorPack gemm_pack = tensors;
557 gemm_pack.add_const_tensor(ACL_SRC_1, cur_weights);
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100558
559 // Prepare GEMM prepare and release unused weights
560 if(!_is_quantized_asymmetric)
561 {
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100562 _mm_gemm->prepare(gemm_pack);
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100563 }
564 else
565 {
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100566 _mm_gemmlowp->prepare(gemm_pack);
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100567 }
568
569 _is_prepared = true;
570 }
571}
572
573experimental::MemoryRequirements CpuFullyConnected::workspace() const
574{
575 return _aux_mem;
576}
577} // namespace cpu
578} // namespace arm_compute