blob: b289cc0104c43854b29eb71f343c4081a78dc5a4 [file] [log] [blame]
Georgios Pinitas529b5a22021-07-27 15:55:30 +01001/*
Jakub Sujak617ed502023-03-29 11:16:18 +01002 * Copyright (c) 2017-2021, 2023 Arm Limited.
Georgios Pinitas529b5a22021-07-27 15:55:30 +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/gpu/cl/operators/ClFullyConnected.h"
Georgios Pinitas529b5a22021-07-27 15:55:30 +010025
26#include "arm_compute/core/Size2D.h"
27#include "arm_compute/core/Validate.h"
28#include "arm_compute/core/utils/misc/ShapeCalculator.h"
29#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
30#include "arm_compute/runtime/CL/CLScheduler.h"
31#include "src/core/CL/kernels/CLFillBorderKernel.h"
32
33#include "src/core/helpers/MemoryHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010034#include "src/gpu/cl/operators/ClConvertFullyConnectedWeights.h"
35#include "src/gpu/cl/operators/ClFlatten.h"
36#include "src/gpu/cl/operators/ClGemm.h"
37#include "src/gpu/cl/operators/ClGemmLowpMatrixMultiplyCore.h"
38#include "src/gpu/cl/operators/ClTranspose.h"
39#include "src/gpu/cl/utils/ClAuxTensorHandler.h"
Georgios Pinitas529b5a22021-07-27 15:55:30 +010040
ramelg012e53f172021-09-22 10:48:25 +010041#include "src/common/utils/Log.h"
Georgios Pinitas529b5a22021-07-27 15:55:30 +010042#include "support/Cast.h"
43
44#include <algorithm>
45
46namespace arm_compute
47{
48namespace opencl
49{
50using namespace arm_compute::experimental;
51using namespace arm_compute::misc::shape_calculator;
52
53namespace
54{
55Status construct_gemmlowp_output_stage(const ITensorInfo &src, const ITensorInfo &weights, const ITensorInfo &dst,
56 GEMMLowpOutputStageInfo &gemmlowp_output_stage, ActivationLayerInfo activation_info)
57{
58 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
63 const auto data_type = src.data_type();
64
65 // Configure output stage for quantized case
66 if(is_data_type_quantized_asymmetric(data_type))
67 {
68 const QuantizationInfo oq_info = dst.quantization_info();
69 const UniformQuantizationInfo iq_unif = src.quantization_info().uniform();
70 const UniformQuantizationInfo wq_unif = weights.quantization_info().uniform();
71 const UniformQuantizationInfo oq_unif = oq_info.uniform();
72
73 const auto output_quant_info = (dst.total_size() == 0) ? iq_unif : oq_unif;
74
75 const float multiplier = (iq_unif.scale * wq_unif.scale) / output_quant_info.scale;
76 int output_multiplier = 0;
77 int output_shift = 0;
78 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift));
79
80 PixelValue type_min{};
81 PixelValue type_max{};
82 std::tie(type_min, type_max) = get_min_max(data_type);
83
84 if(activation_info.enabled())
85 {
86 std::tie(type_min, type_max) = get_quantized_activation_min_max(activation_info, data_type, output_quant_info);
87 }
88
89 // 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;
93 gemmlowp_output_stage.gemmlowp_multipliers.push_back(output_multiplier);
94 gemmlowp_output_stage.gemmlowp_shifts.push_back(output_shift);
95 type_min.get(gemmlowp_output_stage.gemmlowp_min_bound);
96 type_max.get(gemmlowp_output_stage.gemmlowp_max_bound);
97 }
98
99 return Status{};
100}
101
102Status validate_mm(const ITensorInfo &src, const ITensorInfo &weights, const ITensorInfo *bias, const ITensorInfo &dst, const FullyConnectedLayerInfo &fc_info)
103{
104 GEMMLowpOutputStageInfo gemmlowp_output_stage;
105 ARM_COMPUTE_RETURN_ON_ERROR(construct_gemmlowp_output_stage(src, weights, dst, gemmlowp_output_stage, fc_info.activation_info));
106
107 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 false, // fast_math
116 true, // broadcast_bias
117 ActivationLayerInfo()); // activation_info
118
119 if(is_data_type_quantized_asymmetric(src.data_type()))
120 {
121 const UniformQuantizationInfo iq_info = src.quantization_info().uniform();
122 const UniformQuantizationInfo wq_info = weights.quantization_info().uniform();
123
124 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
125 // Extract and negate src and weights offset
126 const QuantizationInfo src_quantization_info(iq_info.scale, -iq_info.offset);
127 const QuantizationInfo weights_quantization_info(wq_info.scale, -wq_info.offset);
128
129 // Validate gemmlowp function
130 ARM_COMPUTE_RETURN_ON_ERROR(ClGemmLowpMatrixMultiplyCore::validate(&src.clone()->set_quantization_info(src_quantization_info),
131 &weights.clone()->set_quantization_info(weights_quantization_info),
132 bias,
133 &dst,
134 gemm_info));
135 }
136 else
137 {
138 ARM_COMPUTE_RETURN_ON_ERROR(ClGemm::validate(&src, &weights, bias, &dst, 1.f, 1.f, gemm_info));
139 }
140
141 return Status{};
142}
143} // namespace
144
145ClFullyConnected::ClFullyConnected()
146 : _convert_weights(nullptr),
147 _flatten(nullptr),
148 _reshape_weights(nullptr),
149 _mm_gemm(nullptr),
150 _mm_gemmlowp(nullptr),
151 _aux_mem(Count)
152{
153}
154
155ClFullyConnected::~ClFullyConnected() = default;
156
157void ClFullyConnected::configure_mm(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *bias, ITensorInfo *dst,
158 const FullyConnectedLayerInfo &fc_info)
159{
160 GEMMLowpOutputStageInfo gemmlowp_output_stage;
161 construct_gemmlowp_output_stage(*src, *weights, *dst, gemmlowp_output_stage, fc_info.activation_info);
162
163 const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped
164 false, // is_b_reshaped
Jakub Sujak617ed502023-03-29 11:16:18 +0100165 !_dynamic_weights, // reshape_b_only_on_first_run
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100166 0, // depth_output_gemm3d
167 false, // reinterpret_input_as_3d
168 fc_info.retain_internal_weights, // retain_internal_weights
169 gemmlowp_output_stage, // gemmlowp_output_stage
170 fc_info.fp_mixed_precision, // fp_mixed_precision
171 false, // fast_math
172 true, // broadcast_bias
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100173 fc_info.activation_info); // activation_info
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100174
175 if(_is_quantized)
176 {
177 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
178 // Extract and negate input and weights offset
179 const QuantizationInfo src_quantization_info = src->quantization_info();
180 const QuantizationInfo weights_quantization_info = weights->quantization_info();
181
182 TensorInfo src_info = src->clone()->set_quantization_info(src_quantization_info);
183 TensorInfo weights_info = weights->clone()->set_quantization_info(weights_quantization_info);
184
185 src_info.set_quantization_info(QuantizationInfo(src_quantization_info.uniform().scale, -src_quantization_info.uniform().offset));
186 weights_info.set_quantization_info(QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset));
187
188 // Configure gemmlowp function
189 _mm_gemmlowp = std::make_unique<ClGemmLowpMatrixMultiplyCore>();
190 _mm_gemmlowp->configure(compile_context, &src_info, &weights_info, bias, dst, gemm_info);
191 }
192 else
193 {
194 // Configure matrix multiply kernel
195 _mm_gemm = std::make_unique<ClGemm>();
196 _mm_gemm->configure(compile_context, src, weights, bias, dst, 1.f, 1.f, gemm_info);
197 }
198}
199
200void ClFullyConnected::configure_conv_fc(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *bias, ITensorInfo *dst,
201 const FullyConnectedLayerInfo &fc_info)
202{
203 ARM_COMPUTE_ERROR_ON((weights->dimension(1) != (src->dimension(0) * src->dimension(1) * src->dimension(2))));
204
205 // If the fully connected layer is called after a convolution layer, the input tensor must be linearized
206
207 // Initialize output tensor for flatten
208 _flattened_src = src->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_flatten_shape(src)).set_data_layout(DataLayout::NCHW);
209
210 // Configure flatten kernel
211 _flatten = std::make_unique<ClFlatten>();
212 _flatten->configure(compile_context, src, &_flattened_src);
213
214 // Configure matrix multiply kernel
215 configure_mm(compile_context, &_flattened_src, weights, bias, dst, fc_info);
216}
217
218void ClFullyConnected::configure_fc_fc(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *bias, ITensorInfo *dst,
219 const FullyConnectedLayerInfo &fc_info)
220{
221 ARM_COMPUTE_ERROR_ON(src->dimension(0) != weights->dimension(1));
222
223 // Configure matrix multiply kernel
224 configure_mm(compile_context, src, weights, bias, dst, fc_info);
225}
226
227void ClFullyConnected::configure(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *dst,
228 FullyConnectedLayerInfo fc_info)
229{
230 ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst);
231
232 // Perform validate step
233 ARM_COMPUTE_ERROR_THROW_ON(ClFullyConnected::validate(src, weights, biases, dst, fc_info));
ramelg012e53f172021-09-22 10:48:25 +0100234 ARM_COMPUTE_LOG_PARAMS(src, weights, biases, dst, fc_info);
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100235
236 _are_weights_converted = true;
237 _are_weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true;
238 _is_fc_after_conv = true;
239 _is_quantized = is_data_type_quantized_asymmetric(src->data_type());
240 _is_prepared = fc_info.retain_internal_weights;
241 _weights_to_use = TensorInfo(*weights);
242 _weights_to_use_idx = ACL_SRC_1;
Jakub Sujak617ed502023-03-29 11:16:18 +0100243 _dynamic_weights = !weights->are_values_constant() && !_are_weights_reshaped;
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100244
245 // With the Fully Connected layer we can have 4 different cases:
246 // 1) Convolution layer -> Fully Connected layer without batches
247 // 2) Fully Connected layer -> Fully Connected layer without batches
248 // 3) Convolution layer -> Fully Connected layer with batches
249 // 4) Fully Connected layer -> Fully Connected layer with batches
250
251 // Check if we have a fully connected layer with batches
252 const bool is_batched_fc_layer = dst->dimension(1) > 1;
253 if(is_batched_fc_layer)
254 {
255 _is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(src->tensor_shape().cbegin() + 3,
256 src->tensor_shape().cend(),
257 dst->tensor_shape().cbegin() + 1));
258 }
259 else
260 {
261 _is_fc_after_conv = src->num_dimensions() > 1;
262 }
263
264 ITensorInfo *weights_used = weights;
265
266 // Reshape weights if needed
267 if(!_are_weights_reshaped)
268 {
269 // Reshape the weights
270 _reshape_weights = std::make_unique<ClTranspose>();
271 _reshape_weights->configure(compile_context, weights, &_reshaped_weights);
272 weights_used = &_reshaped_weights;
273 _weights_to_use_idx = offset_int_vec(TransposedWeights);
274 }
275
276 // Convert weights if needed
277 if(_is_fc_after_conv && (src->data_layout() != fc_info.weights_trained_layout))
278 {
279 // Convert weights
280 _convert_weights = std::make_unique<ClConvertFullyConnectedWeights>();
281 _convert_weights->configure(compile_context,
282 weights_used,
283 &_converted_weights,
284 src->tensor_shape(),
285 fc_info.weights_trained_layout);
286
287 weights_used = &_converted_weights;
288 _weights_to_use_idx = offset_int_vec(ConvertedWeights);
289 _are_weights_converted = false;
290 }
291
292 if(_is_fc_after_conv)
293 {
294 // Fully Connected layer after a Convolution Layer without batches
295 configure_conv_fc(compile_context, src, weights_used, biases, dst, fc_info);
296 }
297 else
298 {
299 // Fully Connected layer after a Fully Connected Layer without batches
300 configure_fc_fc(compile_context, src, weights_used, biases, dst, fc_info);
301 }
302 // Update TensorInfo of final weights used (Need to be done in the end due to padding expansion)
303 _weights_to_use = *weights_used;
304
305 // Set auxiliary memory requirements
306 auto gemm_mem_req = (_is_quantized) ? _mm_gemmlowp->workspace() : _mm_gemm->workspace();
307 for(unsigned int i = 0; i < gemm_mem_req.size(); ++i)
308 {
309 _aux_mem[i] = gemm_mem_req[i];
310 }
311 if(_aux_mem[1].size > 0 || _aux_mem[2].size > 0) // Persistent weights memory on GEMMs
312 {
313 // Release permuted weights at the of prepare as they are further transposed by the assembly dispatch
Jakub Sujak617ed502023-03-29 11:16:18 +0100314 // Keep all the auxiliary tensors in case of dynamic weights as they are recalculated every time
315 _aux_mem[TransposedWeights] = MemoryInfo(
316 offset_int_vec(TransposedWeights),
317 _dynamic_weights ? MemoryLifetime::Temporary : MemoryLifetime::Prepare,
318 _reshaped_weights.total_size());
319 _aux_mem[ConvertedWeights] = MemoryInfo(
320 offset_int_vec(ConvertedWeights),
321 _dynamic_weights ? MemoryLifetime::Temporary : MemoryLifetime::Prepare,
322 _converted_weights.total_size());
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100323 }
324 else
325 {
326 // Release permuted weights at the of prepare as they are further transposed by the assembly dispatch
327 const auto transposed_wei_lft = (_weights_to_use_idx == offset_int_vec(TransposedWeights)) ? MemoryLifetime::Persistent : MemoryLifetime::Prepare;
328 const auto converted_wei_lft = (_weights_to_use_idx == offset_int_vec(ConvertedWeights)) ? MemoryLifetime::Persistent : MemoryLifetime::Prepare;
329
Jakub Sujak617ed502023-03-29 11:16:18 +0100330 _aux_mem[TransposedWeights] = MemoryInfo(
331 offset_int_vec(TransposedWeights),
332 _dynamic_weights ? MemoryLifetime::Temporary : transposed_wei_lft,
333 _reshaped_weights.total_size());
334 _aux_mem[ConvertedWeights] = MemoryInfo(
335 offset_int_vec(ConvertedWeights),
336 _dynamic_weights ? MemoryLifetime::Temporary : converted_wei_lft,
337 _converted_weights.total_size());
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100338 }
339 _aux_mem[FlattenedSrc] = MemoryInfo(offset_int_vec(FlattenedSrc), MemoryLifetime::Temporary, _flattened_src.total_size());
340}
341
342Status ClFullyConnected::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst,
343 FullyConnectedLayerInfo fc_info)
344{
345 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, weights, dst);
346 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
347 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, weights, dst);
348 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 2);
349 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
350 && fc_info.activation_info.activation() != ActivationLayerInfo::ActivationFunction::BOUNDED_RELU && fc_info.activation_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU);
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100351
352 bool weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true;
353 bool is_fc_after_conv = true;
354
355 const ITensorInfo &flatten_src = TensorInfo(src->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_flatten_shape(src)).set_data_layout(DataLayout::NCHW));
356 const ITensorInfo &reshaped_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_transposed_shape(*weights)));
357 const ITensorInfo &converted_weights = weights_reshaped ? TensorInfo(weights->clone()->set_is_resizable(true).reset_padding()) : TensorInfo(*reshaped_weights.clone());
358
359 // With the Fully Connected layer we can have 4 different cases:
360 // 1) Convolution layer -> Fully Connected layer without batches
361 // 2) Fully Connected layer -> Fully Connected layer without batches
362 // 3) Convolution layer -> Fully Connected layer with batches
363 // 4) Fully Connected layer -> Fully Connected layer with batches
364
365 const ITensorInfo *src_to_use = src;
366 const ITensorInfo *weights_to_use = weights;
367
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100368 if(biases != nullptr)
369 {
370 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
371 if(is_data_type_quantized(src->data_type()))
372 {
373 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
374 }
375 else
376 {
377 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, biases);
378 }
379 }
380
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100381 // Check if we have a fully connected layer with batches
382 const bool is_batched_fc_layer = dst->dimension(1) > 1;
383 if(is_batched_fc_layer)
384 {
385 is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(src->tensor_shape().cbegin() + 3,
386 src->tensor_shape().cend(),
387 dst->tensor_shape().cbegin() + 1));
388 }
389 else
390 {
391 is_fc_after_conv = src->num_dimensions() > 1;
392 }
393
394 if(!weights_reshaped)
395 {
396 // Validate reshape weights kernel
397 ARM_COMPUTE_RETURN_ON_ERROR(ClTranspose::validate(weights, &reshaped_weights));
398 weights_to_use = &reshaped_weights;
399 }
400
401 if(is_fc_after_conv && (src->data_layout() != fc_info.weights_trained_layout))
402 {
403 // Validate convert weights kernel
404 ARM_COMPUTE_RETURN_ON_ERROR(ClConvertFullyConnectedWeights::validate(weights_to_use,
405 &converted_weights,
406 src->tensor_shape(),
407 fc_info.weights_trained_layout));
408 weights_to_use = &converted_weights;
409 }
410
411 if(is_fc_after_conv)
412 {
413 // Fully Connected layer after a Convolution Layer without batches
414 ARM_COMPUTE_RETURN_ERROR_ON((weights_to_use->dimension(1) != (src->dimension(0) * src->dimension(1) * src->dimension(2))));
415
416 // Validate flatten kernel
417 ARM_COMPUTE_RETURN_ON_ERROR(ClFlatten::validate(src, &flatten_src));
418 src_to_use = &flatten_src;
419 }
420 else
421 {
422 // Fully Connected layer after a Fully Connected Layer without batches
423 ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(0) != weights_to_use->dimension(1));
424 }
425
426 // Validate matrix multiply kernel
427 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(*src_to_use, *weights_to_use, biases, *dst, fc_info));
428
429 return Status{};
430}
431
432void ClFullyConnected::run(ITensorPack &tensors)
433{
434 prepare(tensors);
435
Jakub Sujak617ed502023-03-29 11:16:18 +0100436#ifdef ARM_COMPUTE_ASSERTS_ENABLED
437 ++_asrt_run_count;
438 ARM_COMPUTE_ERROR_ON(_dynamic_weights && _asrt_prepare_count != _asrt_run_count);
439#endif // ARM_COMPUTE_ASSERTS_ENABLED
440
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100441 auto src = tensors.get_const_tensor(ACL_SRC_0);
442
443 CLAuxTensorHandler flattened_src(offset_int_vec(FlattenedSrc), _flattened_src, tensors, false);
444 CLAuxTensorHandler weights(_weights_to_use_idx, _weights_to_use, tensors, false);
445
446 // Linearize input if it comes from a convolutional layer
447 if(_is_fc_after_conv)
448 {
449 ITensorPack flatten_pack{ { ACL_SRC, src }, { ACL_DST, flattened_src.get() } };
450 _flatten->run(flatten_pack);
451 }
452
453 ITensorPack gemm_pack = tensors;
454 gemm_pack.add_const_tensor(ACL_SRC_0, (_is_fc_after_conv) ? flattened_src.get() : src);
455 if(_weights_to_use_idx != ACL_SRC_1)
456 {
457 gemm_pack.add_const_tensor(ACL_SRC_1, weights.get());
458 }
459
460 // Run matrix multiply
461 if(_is_quantized)
462 {
463 _mm_gemmlowp->run(gemm_pack);
464 }
465 else
466 {
467 _mm_gemm->run(gemm_pack);
468 }
469}
470
471void ClFullyConnected::prepare(ITensorPack &tensors)
472{
Jakub Sujak617ed502023-03-29 11:16:18 +0100473 if(!_is_prepared || _dynamic_weights)
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100474 {
Jakub Sujak617ed502023-03-29 11:16:18 +0100475#ifdef ARM_COMPUTE_ASSERTS_ENABLED
476 ++_asrt_prepare_count;
477 ARM_COMPUTE_ERROR_ON(!_dynamic_weights && _asrt_prepare_count > 1);
478#endif // ARM_COMPUTE_ASSERTS_ENABLED
479
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100480 auto weights = tensors.get_const_tensor(ACL_SRC_1);
481
482 CLAuxTensorHandler reshaped_weights(offset_int_vec(TransposedWeights), _reshaped_weights, tensors, false);
483 CLAuxTensorHandler converted_weights(offset_int_vec(ConvertedWeights), _converted_weights, tensors, false);
484
485 // Pointer to current weights
486 const ITensor *cur_weights = weights;
487
Jakub Sujak617ed502023-03-29 11:16:18 +0100488 // Reshape of the weights if needed
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100489 if(!_are_weights_reshaped)
490 {
491 // Run reshape weights kernel and mark weights as unused
492 ITensorPack transpose_pack{ { ACL_SRC, weights }, { ACL_DST, reshaped_weights.get() } };
493 _reshape_weights->run(transpose_pack);
494
495 cur_weights->mark_as_unused();
496 cur_weights = reshaped_weights.get();
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100497 }
498
Jakub Sujak617ed502023-03-29 11:16:18 +0100499 // Convert weights if needed
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100500 if(!_are_weights_converted)
501 {
502 ITensorPack convert_pack{ { ACL_SRC, cur_weights }, { ACL_DST, converted_weights.get() } };
503 _convert_weights->run(convert_pack);
504
505 cur_weights->mark_as_unused();
506 cur_weights = converted_weights.get();
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100507 }
508
Jakub Sujak617ed502023-03-29 11:16:18 +0100509 ITensorPack gemm_pack = tensors;
510 gemm_pack.add_const_tensor(ACL_SRC_1, cur_weights);
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100511
512 // Prepare GEMM prepare and release unused weights
513 if(!_is_quantized)
514 {
Jakub Sujak617ed502023-03-29 11:16:18 +0100515 _mm_gemm->prepare(gemm_pack);
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100516 }
517 else
518 {
Jakub Sujak617ed502023-03-29 11:16:18 +0100519 _mm_gemmlowp->prepare(gemm_pack);
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100520 }
521 _is_prepared = true;
522 }
523}
524
525experimental::MemoryRequirements ClFullyConnected::workspace() const
526{
527 return _aux_mem;
528}
529} // namespace opencl
530} // namespace arm_compute