ArmNN
 23.05
NeonFullyConnectedWorkload.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017,2022-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  // Same for bias as weights. We don't currently support non const.
42  if (!biases.value().IsConstant())
43  {
44  return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
45  "Arm NN NeonFullyConnectedWorkload does not support non constant bias."};
46  }
47  aclBiases = BuildArmComputeTensorInfo(biases.value());
48  aclBiases.set_are_values_constant(biases.value().IsConstant());
49  optionalAclBiases = &aclBiases;
50  }
51 
52  const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo =
53  ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor, activationDescriptor);
54  return arm_compute::NEFullyConnectedLayer::validate(&aclInput,
55  &aclWeights,
56  optionalAclBiases,
57  &aclOutput,
58  fullyConnectedLayerInfo);
59 }
60 
62  const WorkloadInfo& info,
63  ACLMemManagerOnDemand& memoryManager)
65 {
66  m_Data.ValidateInputsOutputs("NeonFullyConnectedWorkload", descriptor.m_Parameters.GetNumInputs(), 1);
67 
68  arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
69  arm_compute::ITensor& weights = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
70  m_WeightsTensorInfo = info.m_InputTensorInfos[1];
71  weights.info()->set_are_values_constant(m_WeightsTensorInfo.IsConstant());
72  arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
73  if (m_WeightsTensorInfo.IsConstant())
74  {
75  // Copy the weights' tensor into arm_compute tensor.
76  m_WeightsTensor = std::make_unique<arm_compute::Tensor>();
77  BuildArmComputeTensor(*m_WeightsTensor, m_WeightsTensorInfo);
78  m_WeightsTensor->info()->set_are_values_constant(m_WeightsTensorInfo.IsConstant());
79  }
80 
82  {
83  // Copy the biases tensor into arm_compute tensor.
84  m_BiasesTensor = std::make_unique<arm_compute::Tensor>();
85  m_BiasesTensorInfo = info.m_InputTensorInfos[2];
86  BuildArmComputeTensor(*m_BiasesTensor, m_BiasesTensorInfo);
87  m_BiasesTensor->info()->set_are_values_constant(m_BiasesTensorInfo.IsConstant());
88 
89  // We do not support dynamic bias
90  ARMNN_ASSERT(m_BiasesTensorInfo.IsConstant() == true);
91  }
92 
93  const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
94  arm_compute::FullyConnectedLayerInfo fc_info =
96 
97  auto layer = std::make_unique<arm_compute::NEFullyConnectedLayer>(memoryManager);
98  if (m_WeightsTensorInfo.IsConstant())
99  {
100  layer->configure(&input, m_WeightsTensor.get(), m_BiasesTensor.get(), &output, fc_info);
101  }
102  else
103  {
104  layer->configure(&input, &weights, m_BiasesTensor.get(), &output, fc_info);
105  }
106  m_FullyConnectedLayer.reset(layer.release());
107 
108  // Add details for profiling output
109  WorkloadInfo detailsInfo;
110 
111  detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
112  detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
113  detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[1]);
114  if (descriptor.m_Parameters.m_BiasEnabled)
115  {
116  detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[2]);
117  }
118 
119  // Report Profiling Details
120  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonFullyConnectedWorkload_Construct",
121  descriptor.m_Parameters,
122  detailsInfo,
123  this->GetGuid());
124 }
125 
127 {
128  ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID("NeonFullyConnectedWorkload_Execute", this->GetGuid());
129  // The constant tensors may not be fully in place until the workload is Executed
130  if (!prepared)
131  {
132  if (m_WeightsTensorInfo.IsConstant())
133  {
134  InitializeArmComputeTensorData(*m_WeightsTensor, m_WeightsTensorInfo, m_Data.m_Inputs[1]);
135  m_WeightsTensor->info()->set_are_values_constant(m_WeightsTensorInfo.IsConstant());
136  }
137 
139  {
140  InitializeArmComputeTensorData(*m_BiasesTensor, m_BiasesTensorInfo, m_Data.m_Inputs[2]);
141  m_BiasesTensor->info()->set_are_values_constant(m_BiasesTensorInfo.IsConstant());
142  }
143  if (m_WeightsTensorInfo.IsConstant())
144  {
145  FreeTensorIfUnused(m_WeightsTensor);
146  }
147  if (m_BiasesTensorInfo.IsConstant())
148  {
149  FreeTensorIfUnused(m_BiasesTensor);
150  }
151  prepared = true;
152  }
153  m_FullyConnectedLayer->run();
154 }
155 
156 } //namespace armnn
armnn::WorkloadInfo::m_BiasTensorInfo
Optional< TensorInfo > m_BiasTensorInfo
Definition: WorkloadInfo.hpp:21
armnn::FullyConnectedDescriptor::m_BiasEnabled
bool m_BiasEnabled
Enable/disable bias.
Definition: Descriptors.hpp:514
armnn::BaseWorkload< FullyConnectedQueueDescriptor >::GetGuid
arm::pipe::ProfilingGuid GetGuid() const final
Definition: Workload.hpp:61
armnn::QueueDescriptor::ValidateInputsOutputs
void ValidateInputsOutputs(const std::string &descName, unsigned int numExpectedIn, unsigned int numExpectedOut) const
Definition: WorkloadData.cpp:472
armnn::ActivationDescriptor
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:36
armnn::FullyConnectedDescriptor
A FullyConnectedDescriptor for the FullyConnectedLayer.
Definition: Descriptors.hpp:495
PolymorphicDowncast.hpp
TensorHandle.hpp
armnn::ACLMemManagerOnDemand
std::shared_ptr< arm_compute::MemoryManagerOnDemand > ACLMemManagerOnDemand
Definition: NeonFullyConnectedWorkload.cpp:22
armnn::BaseWorkload< FullyConnectedQueueDescriptor >::m_Data
FullyConnectedQueueDescriptor m_Data
Definition: Workload.hpp:83
armnn::WorkloadInfo::m_WeightsTensorInfo
Optional< TensorInfo > m_WeightsTensorInfo
Definition: WorkloadInfo.hpp:20
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
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::InitializeArmComputeTensorData
void InitializeArmComputeTensorData(arm_compute::Tensor &tensor, TensorInfo tensorInfo, const ITensorHandle *handle)
Definition: NeonWorkloadUtils.hpp:60
armnn::OptionalReferenceSwitch< std::is_reference< T >::value, T >::value
const T & value() const
Definition: Optional.hpp:146
armnn::ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo
arm_compute::FullyConnectedLayerInfo ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(const FullyConnectedDescriptor &fullyConnectedDesc, const ActivationDescriptor *activationDesc)
Definition: ArmComputeUtils.hpp:192
armnn::FullyConnectedQueueDescriptor
Definition: WorkloadData.hpp:180
ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID
#define ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID(name, guid)
Definition: NeonWorkloadUtils.hpp:24
armnn::NeonFullyConnectedWorkload::NeonFullyConnectedWorkload
NeonFullyConnectedWorkload(const FullyConnectedQueueDescriptor &descriptor, const WorkloadInfo &info, std::shared_ptr< arm_compute::MemoryManagerOnDemand > &memoryManager)
Definition: NeonFullyConnectedWorkload.cpp:61
NeonFullyConnectedWorkload.hpp
armnn::WorkloadInfo::m_OutputTensorInfos
std::vector< TensorInfo > m_OutputTensorInfos
Definition: WorkloadInfo.hpp:19
armnn::TensorInfo::IsConstant
bool IsConstant() const
Definition: Tensor.cpp:509
ArmComputeTensorUtils.hpp
ArmComputeUtils.hpp
armnn::TensorInfo
Definition: Tensor.hpp:152
NeonWorkloadUtils.hpp
armnn::OptionalBase::has_value
bool has_value() const noexcept
Definition: Optional.hpp:53
armnn::Status
Status
Definition: Types.hpp:42
armnn::ConvertAdditionalInfoToAclActivationLayerInfo
arm_compute::ActivationLayerInfo ConvertAdditionalInfoToAclActivationLayerInfo(const QueueDescriptor &queueDescriptor)
Definition: ArmComputeUtils.hpp:103
armnn::WorkloadInfo
Contains information about TensorInfos of a layer.
Definition: WorkloadInfo.hpp:16
armnn::QueueDescriptorWithParameters::m_Parameters
LayerDescriptor m_Parameters
Definition: WorkloadData.hpp:66
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
ARMNN_REPORT_PROFILING_WORKLOAD_DESC
#define ARMNN_REPORT_PROFILING_WORKLOAD_DESC(name, desc, infos, guid)
Definition: Profiling.hpp:227
armnn::NeonFullyConnectedWorkload::Execute
virtual void Execute() const override
Definition: NeonFullyConnectedWorkload.cpp:126
armnn::Optional
Definition: Optional.hpp:270
armnn::QueueDescriptor::m_Outputs
std::vector< ITensorHandle * > m_Outputs
Definition: WorkloadData.hpp:27
armnn::NeonBaseWorkload
Definition: NeonBaseWorkload.hpp:13
armnn::WorkloadInfo::m_InputTensorInfos
std::vector< TensorInfo > m_InputTensorInfos
Definition: WorkloadInfo.hpp:18
armnn::FullyConnectedDescriptor::GetNumInputs
uint32_t GetNumInputs() const
Get the number of inputs.
Definition: Descriptors.cpp:448
armnn::QueueDescriptor::m_Inputs
std::vector< ITensorHandle * > m_Inputs
Definition: WorkloadData.hpp:26
armnn::BoostLogSeverityMapping::info
@ info