ArmNN
 22.02
ClFullyConnectedWorkload.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 #include <cl/ClTensorHandle.hpp>
11 #include <cl/ClLayerSupport.hpp>
12 
13 #include "ClWorkloadUtils.hpp"
14 
15 namespace armnn
16 {
17 using namespace armcomputetensorutils;
18 
20  const TensorInfo& output,
21  const TensorInfo& weights,
22  const Optional<TensorInfo>& biases,
23  const FullyConnectedDescriptor& descriptor,
24  const ActivationDescriptor* activationDescriptor)
25 {
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  ARMNN_ASSERT(biases.has_value());
35  aclBiases = BuildArmComputeTensorInfo(biases.value());
36  optionalAclBiases = &aclBiases;
37  }
38 
39  const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo =
40  ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor, activationDescriptor);
41 
42  return arm_compute::CLFullyConnectedLayer::validate(&aclInput,
43  &aclWeights,
44  optionalAclBiases,
45  &aclOutput,
46  fullyConnectedLayerInfo);
47 }
48 
50  const FullyConnectedQueueDescriptor& descriptor,
51  const WorkloadInfo& info,
52  std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager,
53  const arm_compute::CLCompileContext& clCompileContext)
54  : ClBaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info), m_FullyConnectedLayer(memoryManager)
55 {
56  // Add details for profiling output
57  WorkloadInfo detailsInfo;
58 
59  detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
60  detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
62  if (descriptor.m_Parameters.m_BiasEnabled)
63  {
65  }
66 
67  // Report Profiling Details
68  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClFullyConnectedWorkload_Construct",
69  descriptor.m_Parameters,
70  detailsInfo,
71  this->GetGuid());
72 
73  m_WeightsTensor = std::make_unique<arm_compute::CLTensor>();
74  BuildArmComputeTensor(*m_WeightsTensor, m_Data.m_Weight->GetTensorInfo());
75 
77  {
78  m_BiasesTensor = std::make_unique<arm_compute::CLTensor>();
79  BuildArmComputeTensor(*m_BiasesTensor, m_Data.m_Bias->GetTensorInfo());
80  }
81 
82  m_Data.ValidateInputsOutputs("ClFullyConnectedWorkload", 1, 1);
83 
84  arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
85  arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
86 
87  const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
88 
89  arm_compute::FullyConnectedLayerInfo fc_info =
91 
92  {
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  }
101 
103 
104  if (m_BiasesTensor)
105  {
107  }
108 
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();
113 }
114 
116 {
117  ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClFullyConnectedWorkload_Execute", this->GetGuid());
118  RunClFunction(m_FullyConnectedLayer, CHECK_LOCATION());
119 }
120 
121 void ClFullyConnectedWorkload::FreeUnusedTensors()
122 {
123  FreeTensorIfUnused(m_WeightsTensor);
124  FreeTensorIfUnused(m_BiasesTensor);
125 }
126 
127 } //namespace armnn
#define ARMNN_SCOPED_PROFILING_EVENT_CL_GUID(name, guid)
void RunClFunction(arm_compute::IFunction &function, const CheckLocation &location)
arm_compute::ActivationLayerInfo ConvertAdditionalInfoToAclActivationLayerInfo(const QueueDescriptor &queueDescriptor)
void ValidateInputsOutputs(const std::string &descName, unsigned int numExpectedIn, unsigned int numExpectedOut) const
Copyright (c) 2021 ARM Limited and Contributors.
const ConstTensorHandle * m_Bias
#define ARMNN_SCOPED_PROFILING_EVENT(backendId, name)
Definition: Profiling.hpp:220
const TensorInfo & GetTensorInfo() const
std::vector< TensorInfo > m_InputTensorInfos
bool has_value() const noexcept
Definition: Optional.hpp:53
A FullyConnectedDescriptor for the FullyConnectedLayer.
bool m_BiasEnabled
Enable/disable bias.
arm_compute::FullyConnectedLayerInfo ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(const FullyConnectedDescriptor &fullyConnectedDesc, const ActivationDescriptor *activationDesc)
Status
enumeration
Definition: Types.hpp:29
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
std::vector< TensorInfo > m_OutputTensorInfos
arm_compute::Status ClFullyConnectedWorkloadValidate(const TensorInfo &input, const TensorInfo &output, const TensorInfo &weights, const Optional< TensorInfo > &biases, const FullyConnectedDescriptor &descriptor, const ActivationDescriptor *activationDescriptor)
const ConstTensorHandle * m_Weight
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:36
#define CHECK_LOCATION()
Definition: Exceptions.hpp:209
profiling::ProfilingGuid GetGuid() const final
Definition: Workload.hpp:55
Optional< TensorInfo > m_BiasTensorInfo
std::vector< ITensorHandle * > m_Outputs
#define ARMNN_REPORT_PROFILING_WORKLOAD_DESC(name, desc, infos, guid)
Definition: Profiling.hpp:227
Contains information about TensorInfos of a layer.
ClFullyConnectedWorkload(const FullyConnectedQueueDescriptor &descriptor, const WorkloadInfo &info, std::shared_ptr< arm_compute::MemoryManagerOnDemand > &memoryManager, const arm_compute::CLCompileContext &clCompileContext)
void InitializeArmComputeClTensorData(arm_compute::CLTensor &clTensor, const ConstTensorHandle *handle)
std::vector< ITensorHandle * > m_Inputs
Optional< TensorInfo > m_WeightsTensorInfo