ArmNN
 21.08
ClFullyConnectedWorkload.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. 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 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  aclBiases = BuildArmComputeTensorInfo(biases);
35  optionalAclBiases = &aclBiases;
36  }
37 
38  const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo =
39  ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor, activationDescriptor);
40 
41  return arm_compute::CLFullyConnectedLayer::validate(&aclInput,
42  &aclWeights,
43  optionalAclBiases,
44  &aclOutput,
45  fullyConnectedLayerInfo);
46 }
47 
49  const FullyConnectedQueueDescriptor& descriptor,
50  const WorkloadInfo& info,
51  std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager,
52  const arm_compute::CLCompileContext& clCompileContext)
53  : BaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info), m_FullyConnectedLayer(memoryManager)
54 {
55  // Add details for profiling output
56  WorkloadInfo detailsInfo;
57 
58  detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
59  detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
61  if (descriptor.m_Parameters.m_BiasEnabled)
62  {
64  }
65 
66  // Report Profiling Details
67  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClFullyConnectedWorkload_Construct",
68  descriptor.m_Parameters,
69  detailsInfo,
70  this->GetGuid());
71 
72  m_WeightsTensor = std::make_unique<arm_compute::CLTensor>();
73  BuildArmComputeTensor(*m_WeightsTensor, m_Data.m_Weight->GetTensorInfo());
74 
76  {
77  m_BiasesTensor = std::make_unique<arm_compute::CLTensor>();
78  BuildArmComputeTensor(*m_BiasesTensor, m_Data.m_Bias->GetTensorInfo());
79  }
80 
81  m_Data.ValidateInputsOutputs("ClFullyConnectedWorkload", 1, 1);
82 
83  arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
84  arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
85 
86  const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
87 
88  arm_compute::FullyConnectedLayerInfo fc_info =
90 
91  m_FullyConnectedLayer.configure(clCompileContext,
92  &input,
93  m_WeightsTensor.get(),
94  m_BiasesTensor.get(),
95  &output,
96  fc_info);
97 
99 
100  if (m_BiasesTensor)
101  {
103  }
104 
105  // Force Compute Library to perform the necessary copying and reshaping, after which
106  // delete all the input tensors that will no longer be needed
107  m_FullyConnectedLayer.prepare();
108  FreeUnusedTensors();
109 }
110 
112 {
113  ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClFullyConnectedWorkload_Execute", this->GetGuid());
114  RunClFunction(m_FullyConnectedLayer, CHECK_LOCATION());
115 }
116 
117 void ClFullyConnectedWorkload::FreeUnusedTensors()
118 {
119  FreeTensorIfUnused(m_WeightsTensor);
120  FreeTensorIfUnused(m_BiasesTensor);
121 }
122 
123 } //namespace armnn
#define ARMNN_SCOPED_PROFILING_EVENT_CL_GUID(name, guid)
arm_compute::Status ClFullyConnectedWorkloadValidate(const TensorInfo &input, const TensorInfo &output, const TensorInfo &weights, const TensorInfo &biases, const FullyConnectedDescriptor &descriptor, const ActivationDescriptor *activationDescriptor)
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.
ClFullyConnectedWorkload(const armnn::FullyConnectedQueueDescriptor &descriptor, const armnn::WorkloadInfo &info, std::shared_ptr< arm_compute::MemoryManagerOnDemand > &memoryManager, const arm_compute::CLCompileContext &clCompileContext)
const ConstTensorHandle * m_Bias
const TensorInfo & GetTensorInfo() const
std::vector< TensorInfo > m_InputTensorInfos
armnn::FullyConnectedQueueDescriptor m_Data
Definition: Workload.hpp:58
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
std::vector< TensorInfo > m_OutputTensorInfos
const ConstTensorHandle * m_Weight
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:25
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197
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:226
Contains information about TensorInfos of a layer.
void InitializeArmComputeClTensorData(arm_compute::CLTensor &clTensor, const ConstTensorHandle *handle)
std::vector< ITensorHandle * > m_Inputs
Optional< TensorInfo > m_WeightsTensorInfo