2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "ClFullyConnectedWorkload.hpp"
7 #include <cl/ClTensorHandle.hpp>
8 #include <backendsCommon/CpuTensorHandle.hpp>
9 #include <aclCommon/ArmComputeTensorUtils.hpp>
10 #include <aclCommon/ArmComputeUtils.hpp>
11 #include <cl/ClLayerSupport.hpp>
13 #include "ClWorkloadUtils.hpp"
17 using namespace armcomputetensorutils;
19 arm_compute::Status ClFullyConnectedWorkloadValidate(const TensorInfo& input,
20 const TensorInfo& output,
21 const TensorInfo& weights,
22 const TensorInfo& biases,
23 const FullyConnectedDescriptor& descriptor)
25 const arm_compute::TensorInfo aclInput = BuildArmComputeTensorInfo(input);
26 const arm_compute::TensorInfo aclOutput = BuildArmComputeTensorInfo(output);
27 const arm_compute::TensorInfo aclWeights = BuildArmComputeTensorInfo(weights);
29 arm_compute::TensorInfo aclBiases;
30 arm_compute::TensorInfo *optionalAclBiases = nullptr;
31 if (descriptor.m_BiasEnabled)
33 aclBiases = BuildArmComputeTensorInfo(biases);
34 optionalAclBiases = &aclBiases;
37 const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo =
38 ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor);
40 return arm_compute::CLFullyConnectedLayer::validate(&aclInput,
44 fullyConnectedLayerInfo);
47 ClFullyConnectedWorkload::ClFullyConnectedWorkload(const FullyConnectedQueueDescriptor& descriptor,
48 const WorkloadInfo& info, std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
49 : BaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info)
50 , m_FullyConnectedLayer(memoryManager)
52 m_WeightsTensor = std::make_unique<arm_compute::CLTensor>();
53 BuildArmComputeTensor(*m_WeightsTensor, m_Data.m_Weight->GetTensorInfo());
55 if (m_Data.m_Parameters.m_BiasEnabled)
57 m_BiasesTensor = std::make_unique<arm_compute::CLTensor>();
58 BuildArmComputeTensor(*m_BiasesTensor, m_Data.m_Bias->GetTensorInfo());
61 m_Data.ValidateInputsOutputs("ClFullyConnectedWorkload", 1, 1);
63 arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
64 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
67 arm_compute::FullyConnectedLayerInfo fc_info;
68 fc_info.transpose_weights = m_Data.m_Parameters.m_TransposeWeightMatrix;
69 m_FullyConnectedLayer.configure(&input, m_WeightsTensor.get(), m_BiasesTensor.get(), &output, fc_info);
71 InitializeArmComputeClTensorData(*m_WeightsTensor, m_Data.m_Weight);
75 InitializeArmComputeClTensorData(*m_BiasesTensor, m_Data.m_Bias);
78 // Force Compute Library to perform the necessary copying and reshaping, after which
79 // delete all the input tensors that will no longer be needed
80 m_FullyConnectedLayer.prepare();
84 void ClFullyConnectedWorkload::Execute() const
86 ARMNN_SCOPED_PROFILING_EVENT_CL("ClFullyConnectedWorkload_Execute");
87 RunClFunction(m_FullyConnectedLayer, CHECK_LOCATION());
90 void ClFullyConnectedWorkload::FreeUnusedTensors()
92 FreeTensorIfUnused(m_WeightsTensor);
93 FreeTensorIfUnused(m_BiasesTensor);