ArmNN
 22.05
NeonFullyConnectedWorkload.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 
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  // The NEON implemented workload does support both const and non const
32  // weights. However, in the case of non const weights we'd have to call
33  // prepare or configure for each inference which we're not setup to do just yet.
34  if (!weights.IsConstant())
35  {
36  return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
37  "Arm NN NeonFullyConnectedWorkload does not support non constant weights."};
38  }
39  const arm_compute::TensorInfo aclInput = BuildArmComputeTensorInfo(input);
40  const arm_compute::TensorInfo aclOutput = BuildArmComputeTensorInfo(output);
41  arm_compute::TensorInfo aclWeights = BuildArmComputeTensorInfo(weights);
42  aclWeights.set_are_values_constant(weights.IsConstant());
43 
44  arm_compute::TensorInfo aclBiases;
45  arm_compute::TensorInfo* optionalAclBiases = nullptr;
46  if (descriptor.m_BiasEnabled)
47  {
48  ARMNN_ASSERT(biases.has_value());
49  // Same for bias as weights. We don't currently support non const.
50  if (!biases.value().IsConstant())
51  {
52  return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
53  "Arm NN NeonFullyConnectedWorkload does not support non constant bias."};
54  }
55  aclBiases = BuildArmComputeTensorInfo(biases.value());
56  aclBiases.set_are_values_constant(biases.value().IsConstant());
57  optionalAclBiases = &aclBiases;
58  }
59 
60  const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo =
61  ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor, activationDescriptor);
62  return arm_compute::NEFullyConnectedLayer::validate(&aclInput,
63  &aclWeights,
64  optionalAclBiases,
65  &aclOutput,
66  fullyConnectedLayerInfo);
67 }
68 
70  const WorkloadInfo& info,
71  ACLMemManagerOnDemand& memoryManager)
73 {
74  m_Data.ValidateInputsOutputs("NeonFullyConnectedWorkload", 1, 1);
75 
76  arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
77  arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
78 
79  // Copy the weights' tensor into arm_compute tensor.
80  m_WeightsTensor = std::make_unique<arm_compute::Tensor>();
81  BuildArmComputeTensor(*m_WeightsTensor, m_Data.m_Weight->GetTensorInfo());
83 
85  {
86  // Copy the biases tensor into arm_compute tensor.
87  m_BiasesTensor = std::make_unique<arm_compute::Tensor>();
88  BuildArmComputeTensor(*m_BiasesTensor, m_Data.m_Bias->GetTensorInfo());
90  }
91 
92  const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
93  arm_compute::FullyConnectedLayerInfo fc_info =
95 
96  auto layer = std::make_unique<arm_compute::NEFullyConnectedLayer>(memoryManager);
97  layer->configure(&input, m_WeightsTensor.get(), m_BiasesTensor.get(), &output, fc_info);
98  m_FullyConnectedLayer.reset(layer.release());
99 
100  // Add details for profiling output
101  WorkloadInfo detailsInfo;
102 
103  detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
104  detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
106  if (descriptor.m_Parameters.m_BiasEnabled)
107  {
109  }
110 
111  // Report Profiling Details
112  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonFullyConnectedWorkload_Construct",
113  descriptor.m_Parameters,
114  detailsInfo,
115  this->GetGuid());
116 
117  // Force Compute Library to perform the necessary copying and reshaping.
118  m_FullyConnectedLayer->prepare();
119  FreeTensorIfUnused(m_WeightsTensor);
120  FreeTensorIfUnused(m_BiasesTensor);
121 }
122 
124 {
125  ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID("NeonFullyConnectedWorkload_Execute", this->GetGuid());
126  m_FullyConnectedLayer->run();
127 }
128 
129 } //namespace armnn
bool IsConstant() const
Definition: Tensor.cpp:509
arm_compute::ActivationLayerInfo ConvertAdditionalInfoToAclActivationLayerInfo(const QueueDescriptor &queueDescriptor)
arm::pipe::ProfilingGuid GetGuid() const final
Definition: Workload.hpp:59
arm_compute::Status NeonFullyConnectedWorkloadValidate(const TensorInfo &input, const TensorInfo &output, const TensorInfo &weights, const Optional< TensorInfo > &biases, const FullyConnectedDescriptor &descriptor, const ActivationDescriptor *activationDescriptor)
NeonFullyConnectedWorkload(const FullyConnectedQueueDescriptor &descriptor, const WorkloadInfo &info, std::shared_ptr< arm_compute::MemoryManagerOnDemand > &memoryManager)
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
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:42
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
std::vector< TensorInfo > m_OutputTensorInfos
const ConstTensorHandle * m_Weight
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:36
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