ArmNN
 24.02
NeonFullyConnectedWorkload.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 
8 #include "NeonWorkloadUtils.hpp"
9 
12 
14 
16 
17 #include <arm_compute/runtime/NEON/functions/NEFullyConnectedLayer.h>
18 
19 namespace armnn
20 {
21 using namespace armcomputetensorutils;
22 using ACLMemManagerOnDemand = std::shared_ptr<arm_compute::MemoryManagerOnDemand>;
23 
25  const TensorInfo& output,
26  const TensorInfo& weights,
27  const Optional<TensorInfo>& biases,
28  const FullyConnectedDescriptor& descriptor,
29  const ActivationDescriptor* activationDescriptor)
30 {
31  const arm_compute::TensorInfo aclInput = BuildArmComputeTensorInfo(input);
32  const arm_compute::TensorInfo aclOutput = BuildArmComputeTensorInfo(output);
33  arm_compute::TensorInfo aclWeights = BuildArmComputeTensorInfo(weights);
34  aclWeights.set_are_values_constant(weights.IsConstant());
35 
36  arm_compute::TensorInfo aclBiases;
37  arm_compute::TensorInfo* optionalAclBiases = nullptr;
38  if (descriptor.m_BiasEnabled)
39  {
40  ARMNN_ASSERT(biases.has_value());
41  aclBiases = BuildArmComputeTensorInfo(biases.value());
42  aclBiases.set_are_values_constant(biases.value().IsConstant());
43  optionalAclBiases = &aclBiases;
44  }
45 
46  const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo =
47  ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor, activationDescriptor);
48  return arm_compute::NEFullyConnectedLayer::validate(&aclInput,
49  &aclWeights,
50  optionalAclBiases,
51  &aclOutput,
52  fullyConnectedLayerInfo);
53 }
54 
56  const WorkloadInfo& info,
57  ACLMemManagerOnDemand& memoryManager)
59 {
60  m_Data.ValidateInputsOutputs("NeonFullyConnectedWorkload", descriptor.m_Parameters.GetNumInputs(), 1);
61 
62  arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
63  arm_compute::ITensor& weights = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
64  m_WeightsTensorInfo = info.m_InputTensorInfos[1];
65  weights.info()->set_are_values_constant(m_WeightsTensorInfo.IsConstant());
66  arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
67  if (m_WeightsTensorInfo.IsConstant())
68  {
69  // Copy the weights' tensor into arm_compute tensor.
70  m_WeightsTensor = std::make_unique<arm_compute::Tensor>();
71  BuildArmComputeTensor(*m_WeightsTensor, m_WeightsTensorInfo);
72  m_WeightsTensor->info()->set_are_values_constant(m_WeightsTensorInfo.IsConstant());
73  }
74 
76  {
77  // Copy the biases tensor into arm_compute tensor.
78  m_BiasesTensor = std::make_unique<arm_compute::Tensor>();
79  m_BiasesTensorInfo = info.m_InputTensorInfos[2];
80  BuildArmComputeTensor(*m_BiasesTensor, m_BiasesTensorInfo);
81  m_BiasesTensor->info()->set_are_values_constant(m_BiasesTensorInfo.IsConstant());
82  }
83 
84  const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
85  arm_compute::FullyConnectedLayerInfo fc_info =
87 
88  auto layer = std::make_unique<arm_compute::NEFullyConnectedLayer>(memoryManager);
89  if (m_WeightsTensorInfo.IsConstant())
90  {
91  layer->configure(&input, m_WeightsTensor.get(), m_BiasesTensor.get(), &output, fc_info);
92  }
93  else
94  {
95  layer->configure(&input, &weights, m_BiasesTensor.get(), &output, fc_info);
96  }
97  m_FullyConnectedLayer.reset(layer.release());
98 
99  // Add details for profiling output
100  WorkloadInfo detailsInfo;
101 
102  detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
103  detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
104 
105  // Report Profiling Details
106  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonFullyConnectedWorkload_Construct",
107  descriptor.m_Parameters,
108  detailsInfo,
109  this->GetGuid());
110 }
111 
113 {
114  ARMNN_SCOPED_PROFILING_EVENT_NEON_NAME_GUID("NeonFullyConnectedWorkload_Execute");
115  // The constant tensors may not be fully in place until the workload is Executed
116  if (!prepared)
117  {
118  if (m_WeightsTensorInfo.IsConstant())
119  {
120  InitializeArmComputeTensorData(*m_WeightsTensor, m_WeightsTensorInfo, m_Data.m_Inputs[1]);
121  m_WeightsTensor->info()->set_are_values_constant(m_WeightsTensorInfo.IsConstant());
122  }
123 
125  {
126  InitializeArmComputeTensorData(*m_BiasesTensor, m_BiasesTensorInfo, m_Data.m_Inputs[2]);
127  m_BiasesTensor->info()->set_are_values_constant(m_BiasesTensorInfo.IsConstant());
128  }
129  if (m_WeightsTensorInfo.IsConstant())
130  {
131  FreeTensorIfUnused(m_WeightsTensor);
132  }
133  if (m_BiasesTensorInfo.IsConstant())
134  {
135  FreeTensorIfUnused(m_BiasesTensor);
136  }
137  prepared = true;
138  }
139  m_FullyConnectedLayer->run();
140 }
141 
142 } //namespace armnn
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
armnn::NeonFullyConnectedWorkloadValidate
arm_compute::Status NeonFullyConnectedWorkloadValidate(const TensorInfo &input, const TensorInfo &output, const TensorInfo &weights, const Optional< TensorInfo > &biases, const FullyConnectedDescriptor &descriptor, const ActivationDescriptor *activationDescriptor)
Definition: NeonFullyConnectedWorkload.cpp:24
armnn::ActivationDescriptor
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:36
armnn::FullyConnectedDescriptor
A FullyConnectedDescriptor for the FullyConnectedLayer.
Definition: Descriptors.hpp:507
armnn::FullyConnectedQueueDescriptor
Definition: WorkloadData.hpp:180
armnn::Optional
Definition: Optional.hpp:270
armnn::ACLMemManagerOnDemand
std::shared_ptr< arm_compute::MemoryManagerOnDemand > ACLMemManagerOnDemand
Definition: NeonFullyConnectedWorkload.cpp:22
armnn::QueueDescriptor::ValidateInputsOutputs
void ValidateInputsOutputs(const std::string &descName, unsigned int numExpectedIn, unsigned int numExpectedOut) const
Definition: WorkloadData.cpp:446
armnn::ConvertAdditionalInfoToAclActivationLayerInfo
arm_compute::ActivationLayerInfo ConvertAdditionalInfoToAclActivationLayerInfo(const QueueDescriptor &queueDescriptor)
Definition: ArmComputeUtils.hpp:105
armnn::TensorInfo
Definition: Tensor.hpp:152
armnn::NeonFullyConnectedWorkload::Execute
virtual void Execute() const override
Definition: NeonFullyConnectedWorkload.cpp:112
armnn::TensorInfo::IsConstant
bool IsConstant() const
Definition: Tensor.cpp:509
armnn::NeonFullyConnectedWorkload::NeonFullyConnectedWorkload
NeonFullyConnectedWorkload(const FullyConnectedQueueDescriptor &descriptor, const WorkloadInfo &info, std::shared_ptr< arm_compute::MemoryManagerOnDemand > &memoryManager)
Definition: NeonFullyConnectedWorkload.cpp:55
armnn::InitializeArmComputeTensorData
void InitializeArmComputeTensorData(arm_compute::Tensor &tensor, TensorInfo tensorInfo, const ITensorHandle *handle)
Definition: NeonWorkloadUtils.hpp:68
armnn::WorkloadInfo::m_OutputTensorInfos
std::vector< TensorInfo > m_OutputTensorInfos
Definition: WorkloadInfo.hpp:19
armnn::QueueDescriptorWithParameters::m_Parameters
LayerDescriptor m_Parameters
Definition: WorkloadData.hpp:66
armnn::FullyConnectedDescriptor::m_BiasEnabled
bool m_BiasEnabled
Enable/disable bias.
Definition: Descriptors.hpp:526
armnn::WorkloadInfo
Contains information about TensorInfos of a layer.
Definition: WorkloadInfo.hpp:16
PolymorphicDowncast.hpp
ArmComputeUtils.hpp
armnn::BoostLogSeverityMapping::info
@ info
armnn::QueueDescriptor::m_Outputs
std::vector< ITensorHandle * > m_Outputs
Definition: WorkloadData.hpp:27
NeonFullyConnectedWorkload.hpp
ARMNN_REPORT_PROFILING_WORKLOAD_DESC
#define ARMNN_REPORT_PROFILING_WORKLOAD_DESC(name, desc, infos, guid)
Definition: Profiling.hpp:227
TensorHandle.hpp
armnn::Status
Status
Definition: Types.hpp:42
armnn::BaseWorkload< FullyConnectedQueueDescriptor >::m_Data
FullyConnectedQueueDescriptor m_Data
Definition: Workload.hpp:89
armnn::WorkloadInfo::m_InputTensorInfos
std::vector< TensorInfo > m_InputTensorInfos
Definition: WorkloadInfo.hpp:18
NeonWorkloadUtils.hpp
armnn::FullyConnectedDescriptor::GetNumInputs
uint32_t GetNumInputs() const
Get the number of inputs.
Definition: Descriptors.cpp:474
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
ArmComputeTensorUtils.hpp
ARMNN_SCOPED_PROFILING_EVENT_NEON_NAME_GUID
#define ARMNN_SCOPED_PROFILING_EVENT_NEON_NAME_GUID(label)
Creates a profiling event that uses GetGuid() and GetName() from the calling class.
Definition: NeonWorkloadUtils.hpp:32
armnn::ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo
arm_compute::FullyConnectedLayerInfo ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(const FullyConnectedDescriptor &fullyConnectedDesc, const ActivationDescriptor *activationDesc)
Definition: ArmComputeUtils.hpp:194
armnn::NeonBaseWorkload
Definition: NeonBaseWorkload.hpp:13
armnn::OptionalReferenceSwitch< std::is_reference< T >::value, T >::value
const T & value() const
Definition: Optional.hpp:146
armnn::QueueDescriptor::m_Inputs
std::vector< ITensorHandle * > m_Inputs
Definition: WorkloadData.hpp:26
armnn::OptionalBase::has_value
bool has_value() const noexcept
Definition: Optional.hpp:53