blob: d1d911ac13300436224c5981e9ce6a397863dea1 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5
Matthew Benthamab8cdc12018-09-17 11:17:41 +01006#include "ClFullyConnectedWorkload.hpp"
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +00007#include <cl/ClTensorHandle.hpp>
James Conroy1f58f032021-04-27 17:13:27 +01008#include <backendsCommon/TensorHandle.hpp>
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +00009#include <aclCommon/ArmComputeTensorUtils.hpp>
10#include <aclCommon/ArmComputeUtils.hpp>
11#include <cl/ClLayerSupport.hpp>
telsoa014fcda012018-03-09 14:13:49 +000012
Matthew Bentham14e46692018-09-20 15:35:30 +010013#include "ClWorkloadUtils.hpp"
14
telsoa014fcda012018-03-09 14:13:49 +000015namespace armnn
16{
17using namespace armcomputetensorutils;
18
telsoa01c577f2c2018-08-31 09:22:23 +010019arm_compute::Status ClFullyConnectedWorkloadValidate(const TensorInfo& input,
20 const TensorInfo& output,
21 const TensorInfo& weights,
22 const TensorInfo& biases,
Mike Kelly07810fc2020-11-12 10:58:48 +000023 const FullyConnectedDescriptor& descriptor,
24 const ActivationDescriptor* activationDescriptor)
telsoa01c577f2c2018-08-31 09:22:23 +010025{
26 const arm_compute::TensorInfo aclInput = BuildArmComputeTensorInfo(input);
27 const arm_compute::TensorInfo aclOutput = BuildArmComputeTensorInfo(output);
28 const arm_compute::TensorInfo aclWeights = BuildArmComputeTensorInfo(weights);
29
30 arm_compute::TensorInfo aclBiases;
31 arm_compute::TensorInfo *optionalAclBiases = nullptr;
32 if (descriptor.m_BiasEnabled)
33 {
34 aclBiases = BuildArmComputeTensorInfo(biases);
35 optionalAclBiases = &aclBiases;
36 }
37
38 const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo =
Mike Kelly07810fc2020-11-12 10:58:48 +000039 ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor, activationDescriptor);
telsoa01c577f2c2018-08-31 09:22:23 +010040
41 return arm_compute::CLFullyConnectedLayer::validate(&aclInput,
42 &aclWeights,
43 optionalAclBiases,
44 &aclOutput,
45 fullyConnectedLayerInfo);
46}
47
Sadik Armagane9444752020-12-02 11:28:58 +000048ClFullyConnectedWorkload::ClFullyConnectedWorkload(
49 const FullyConnectedQueueDescriptor& descriptor,
50 const WorkloadInfo& info,
51 std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager,
52 const arm_compute::CLCompileContext& clCompileContext)
Matthew Benthamab8cdc12018-09-17 11:17:41 +010053 : BaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info)
telsoa01c577f2c2018-08-31 09:22:23 +010054 , m_FullyConnectedLayer(memoryManager)
telsoa014fcda012018-03-09 14:13:49 +000055{
telsoa01c577f2c2018-08-31 09:22:23 +010056 m_WeightsTensor = std::make_unique<arm_compute::CLTensor>();
57 BuildArmComputeTensor(*m_WeightsTensor, m_Data.m_Weight->GetTensorInfo());
telsoa014fcda012018-03-09 14:13:49 +000058
telsoa014fcda012018-03-09 14:13:49 +000059 if (m_Data.m_Parameters.m_BiasEnabled)
60 {
telsoa01c577f2c2018-08-31 09:22:23 +010061 m_BiasesTensor = std::make_unique<arm_compute::CLTensor>();
62 BuildArmComputeTensor(*m_BiasesTensor, m_Data.m_Bias->GetTensorInfo());
telsoa014fcda012018-03-09 14:13:49 +000063 }
64
Matthew Benthamab8cdc12018-09-17 11:17:41 +010065 m_Data.ValidateInputsOutputs("ClFullyConnectedWorkload", 1, 1);
telsoa014fcda012018-03-09 14:13:49 +000066
67 arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
68 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
telsoa01c577f2c2018-08-31 09:22:23 +010069
Mike Kelly07810fc2020-11-12 10:58:48 +000070 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
71
72 arm_compute::FullyConnectedLayerInfo fc_info =
73 ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor.m_Parameters, activationInfo);
74
Sadik Armagane9444752020-12-02 11:28:58 +000075 m_FullyConnectedLayer.configure(clCompileContext,
76 &input,
77 m_WeightsTensor.get(),
78 m_BiasesTensor.get(),
79 &output,
80 fc_info);
telsoa014fcda012018-03-09 14:13:49 +000081
Matthew Bentham785df502018-09-21 10:29:58 +010082 InitializeArmComputeClTensorData(*m_WeightsTensor, m_Data.m_Weight);
telsoa014fcda012018-03-09 14:13:49 +000083
telsoa01c577f2c2018-08-31 09:22:23 +010084 if (m_BiasesTensor)
telsoa014fcda012018-03-09 14:13:49 +000085 {
Matthew Bentham785df502018-09-21 10:29:58 +010086 InitializeArmComputeClTensorData(*m_BiasesTensor, m_Data.m_Bias);
telsoa014fcda012018-03-09 14:13:49 +000087 }
telsoa01c577f2c2018-08-31 09:22:23 +010088
89 // Force Compute Library to perform the necessary copying and reshaping, after which
90 // delete all the input tensors that will no longer be needed
91 m_FullyConnectedLayer.prepare();
92 FreeUnusedTensors();
telsoa014fcda012018-03-09 14:13:49 +000093}
94
Matthew Benthamab8cdc12018-09-17 11:17:41 +010095void ClFullyConnectedWorkload::Execute() const
telsoa014fcda012018-03-09 14:13:49 +000096{
Matthew Benthamab8cdc12018-09-17 11:17:41 +010097 ARMNN_SCOPED_PROFILING_EVENT_CL("ClFullyConnectedWorkload_Execute");
Aron Virginas-Tara8e06ed2018-10-19 16:46:15 +010098 RunClFunction(m_FullyConnectedLayer, CHECK_LOCATION());
telsoa01c577f2c2018-08-31 09:22:23 +010099}
100
Matthew Benthamab8cdc12018-09-17 11:17:41 +0100101void ClFullyConnectedWorkload::FreeUnusedTensors()
telsoa01c577f2c2018-08-31 09:22:23 +0100102{
103 FreeTensorIfUnused(m_WeightsTensor);
104 FreeTensorIfUnused(m_BiasesTensor);
telsoa014fcda012018-03-09 14:13:49 +0000105}
106
surmeh013537c2c2018-05-18 16:31:43 +0100107} //namespace armnn