blob: 017f4fff6b71a2fc86062051fd5bf65af5240e8d [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
Teresa Charlin588cbdf2022-01-19 15:55:37 +00002// Copyright © 2017 Arm Ltd and Contributors. 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>
Colm Donelan0c479742021-12-10 12:43:54 +00008#include <armnn/backends/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,
Matthew Bentham67d63902022-02-08 15:03:07 +000022 const Optional<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;
Keith Davisbcd860a2021-08-05 14:20:33 +010031 arm_compute::TensorInfo* optionalAclBiases = nullptr;
telsoa01c577f2c2018-08-31 09:22:23 +010032 if (descriptor.m_BiasEnabled)
33 {
Matthew Bentham67d63902022-02-08 15:03:07 +000034 ARMNN_ASSERT(biases.has_value());
35 aclBiases = BuildArmComputeTensorInfo(biases.value());
telsoa01c577f2c2018-08-31 09:22:23 +010036 optionalAclBiases = &aclBiases;
37 }
38
39 const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo =
Mike Kelly07810fc2020-11-12 10:58:48 +000040 ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor, activationDescriptor);
telsoa01c577f2c2018-08-31 09:22:23 +010041
42 return arm_compute::CLFullyConnectedLayer::validate(&aclInput,
43 &aclWeights,
44 optionalAclBiases,
45 &aclOutput,
46 fullyConnectedLayerInfo);
47}
48
Sadik Armagane9444752020-12-02 11:28:58 +000049ClFullyConnectedWorkload::ClFullyConnectedWorkload(
50 const FullyConnectedQueueDescriptor& descriptor,
51 const WorkloadInfo& info,
52 std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager,
53 const arm_compute::CLCompileContext& clCompileContext)
Teresa Charlin588cbdf2022-01-19 15:55:37 +000054 : ClBaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info), m_FullyConnectedLayer(memoryManager)
telsoa014fcda012018-03-09 14:13:49 +000055{
Keith Davisbcd860a2021-08-05 14:20:33 +010056 // Add details for profiling output
57 WorkloadInfo detailsInfo;
58
59 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
60 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
61 detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Weight->GetTensorInfo());
62 if (descriptor.m_Parameters.m_BiasEnabled)
63 {
64 detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Bias->GetTensorInfo());
65 }
66
67 // Report Profiling Details
68 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClFullyConnectedWorkload_Construct",
69 descriptor.m_Parameters,
70 detailsInfo,
71 this->GetGuid());
72
telsoa01c577f2c2018-08-31 09:22:23 +010073 m_WeightsTensor = std::make_unique<arm_compute::CLTensor>();
74 BuildArmComputeTensor(*m_WeightsTensor, m_Data.m_Weight->GetTensorInfo());
telsoa014fcda012018-03-09 14:13:49 +000075
telsoa014fcda012018-03-09 14:13:49 +000076 if (m_Data.m_Parameters.m_BiasEnabled)
77 {
telsoa01c577f2c2018-08-31 09:22:23 +010078 m_BiasesTensor = std::make_unique<arm_compute::CLTensor>();
79 BuildArmComputeTensor(*m_BiasesTensor, m_Data.m_Bias->GetTensorInfo());
telsoa014fcda012018-03-09 14:13:49 +000080 }
81
Matthew Benthamab8cdc12018-09-17 11:17:41 +010082 m_Data.ValidateInputsOutputs("ClFullyConnectedWorkload", 1, 1);
telsoa014fcda012018-03-09 14:13:49 +000083
Keith Davisbcd860a2021-08-05 14:20:33 +010084 arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
telsoa014fcda012018-03-09 14:13:49 +000085 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
telsoa01c577f2c2018-08-31 09:22:23 +010086
Mike Kelly07810fc2020-11-12 10:58:48 +000087 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
88
89 arm_compute::FullyConnectedLayerInfo fc_info =
Keith Davisbcd860a2021-08-05 14:20:33 +010090 ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor.m_Parameters, activationInfo);
Mike Kelly07810fc2020-11-12 10:58:48 +000091
Kevin May9f6862d2021-10-22 15:42:28 +010092 {
93 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClFullyConnectedWorkload_configure");
94 m_FullyConnectedLayer.configure(clCompileContext,
95 &input,
96 m_WeightsTensor.get(),
97 m_BiasesTensor.get(),
98 &output,
99 fc_info);
100 }
telsoa014fcda012018-03-09 14:13:49 +0000101
Matthew Bentham785df502018-09-21 10:29:58 +0100102 InitializeArmComputeClTensorData(*m_WeightsTensor, m_Data.m_Weight);
telsoa014fcda012018-03-09 14:13:49 +0000103
telsoa01c577f2c2018-08-31 09:22:23 +0100104 if (m_BiasesTensor)
telsoa014fcda012018-03-09 14:13:49 +0000105 {
Matthew Bentham785df502018-09-21 10:29:58 +0100106 InitializeArmComputeClTensorData(*m_BiasesTensor, m_Data.m_Bias);
telsoa014fcda012018-03-09 14:13:49 +0000107 }
telsoa01c577f2c2018-08-31 09:22:23 +0100108
109 // Force Compute Library to perform the necessary copying and reshaping, after which
110 // delete all the input tensors that will no longer be needed
111 m_FullyConnectedLayer.prepare();
112 FreeUnusedTensors();
telsoa014fcda012018-03-09 14:13:49 +0000113}
114
Matthew Benthamab8cdc12018-09-17 11:17:41 +0100115void ClFullyConnectedWorkload::Execute() const
telsoa014fcda012018-03-09 14:13:49 +0000116{
Keith Davisbcd860a2021-08-05 14:20:33 +0100117 ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClFullyConnectedWorkload_Execute", this->GetGuid());
Aron Virginas-Tara8e06ed2018-10-19 16:46:15 +0100118 RunClFunction(m_FullyConnectedLayer, CHECK_LOCATION());
telsoa01c577f2c2018-08-31 09:22:23 +0100119}
120
Matthew Benthamab8cdc12018-09-17 11:17:41 +0100121void ClFullyConnectedWorkload::FreeUnusedTensors()
telsoa01c577f2c2018-08-31 09:22:23 +0100122{
123 FreeTensorIfUnused(m_WeightsTensor);
124 FreeTensorIfUnused(m_BiasesTensor);
telsoa014fcda012018-03-09 14:13:49 +0000125}
126
surmeh013537c2c2018-05-18 16:31:43 +0100127} //namespace armnn