ArmNN
 21.11
NeonFullyConnectedWorkload.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 
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 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  const arm_compute::TensorInfo aclWeights = BuildArmComputeTensorInfo(weights);
34 
35  arm_compute::TensorInfo aclBiases;
36  arm_compute::TensorInfo* optionalAclBiases = nullptr;
37  if (descriptor.m_BiasEnabled)
38  {
39  aclBiases = BuildArmComputeTensorInfo(biases);
40  optionalAclBiases = &aclBiases;
41  }
42 
43  const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo =
44  ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor, activationDescriptor);
45 
46  return arm_compute::NEFullyConnectedLayer::validate(&aclInput,
47  &aclWeights,
48  optionalAclBiases,
49  &aclOutput,
50  fullyConnectedLayerInfo);
51 }
52 
54  const WorkloadInfo& info,
55  ACLMemManagerOnDemand& memoryManager)
56  : BaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info)
57 {
58  m_Data.ValidateInputsOutputs("NeonFullyConnectedWorkload", 1, 1);
59 
60  arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
61  arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
62 
63  m_WeightsTensor = std::make_unique<arm_compute::Tensor>();
64  BuildArmComputeTensor(*m_WeightsTensor, m_Data.m_Weight->GetTensorInfo());
65 
67  {
68  m_BiasesTensor = std::make_unique<arm_compute::Tensor>();
69  BuildArmComputeTensor(*m_BiasesTensor, m_Data.m_Bias->GetTensorInfo());
70  }
71 
72  const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
73 
74  arm_compute::FullyConnectedLayerInfo fc_info =
76 
77  auto layer = std::make_unique<arm_compute::NEFullyConnectedLayer>(memoryManager);
78  layer->configure(&input, m_WeightsTensor.get(), m_BiasesTensor.get(), &output, fc_info);
79  m_FullyConnectedLayer.reset(layer.release());
80 
81  // Allocate
83  {
85  }
86  else
87  {
89  }
90 
91  if (m_BiasesTensor)
92  {
94  {
96  }
97  else
98  {
100  }
101  }
102 
103  // Add details for profiling output
104  WorkloadInfo detailsInfo;
105 
106  detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
107  detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
109  if (descriptor.m_Parameters.m_BiasEnabled)
110  {
112  }
113 
114  // Report Profiling Details
115  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonFullyConnectedWorkload_Construct",
116  descriptor.m_Parameters,
117  detailsInfo,
118  this->GetGuid());
119 
120  // Force Compute Library to perform the necessary copying and reshaping, after which
121  // delete all the input tensors that will no longer be needed
122  m_FullyConnectedLayer->prepare();
123  FreeUnusedTensors();
124 }
125 
127 {
128  ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID("NeonFullyConnectedWorkload_Execute", this->GetGuid());
129  m_FullyConnectedLayer->run();
130 }
131 
132 void NeonFullyConnectedWorkload::FreeUnusedTensors()
133 {
134  FreeTensorIfUnused(m_WeightsTensor);
135  FreeTensorIfUnused(m_BiasesTensor);
136 }
137 
138 } //namespace armnn
arm_compute::ActivationLayerInfo ConvertAdditionalInfoToAclActivationLayerInfo(const QueueDescriptor &queueDescriptor)
NeonFullyConnectedWorkload(const FullyConnectedQueueDescriptor &descriptor, const WorkloadInfo &info, std::shared_ptr< arm_compute::MemoryManagerOnDemand > &memoryManager)
arm_compute::Status NeonFullyConnectedWorkloadValidate(const TensorInfo &input, const TensorInfo &output, const TensorInfo &weights, const TensorInfo &biases, const FullyConnectedDescriptor &descriptor, const ActivationDescriptor *activationDescriptor)
void ValidateInputsOutputs(const std::string &descName, unsigned int numExpectedIn, unsigned int numExpectedOut) const
Copyright (c) 2021 ARM Limited and Contributors.
const ConstTensorHandle * m_Bias
std::shared_ptr< arm_compute::MemoryManagerOnDemand > ACLMemManagerOnDemand
const TensorInfo & GetTensorInfo() const
std::vector< TensorInfo > m_InputTensorInfos
DataType GetDataType() const
Definition: Tensor.hpp:198
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
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
void InitializeArmComputeTensorData(arm_compute::Tensor &tensor, const ConstTensorHandle *handle)
Contains information about TensorInfos of a layer.
std::vector< ITensorHandle * > m_Inputs
#define ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID(name, guid)
Optional< TensorInfo > m_WeightsTensorInfo