ArmNN
 22.05
ClFullyConnectedWorkload.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 #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 Optional<TensorInfo>& biases,
23  const FullyConnectedDescriptor& descriptor,
24  const ActivationDescriptor* activationDescriptor)
25 {
26  // The CL implemented workload does support both const and non const
27  // weights. However, in the case of non const weights we'd have to call
28  // prepare or configure for each inference which we're not setup to do just yet.
29  if (!weights.IsConstant())
30  {
31  return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
32  "Arm NN ClFullyConnectedWorkload does not support non constant weights."};
33  }
34  const arm_compute::TensorInfo aclInput = BuildArmComputeTensorInfo(input);
35  const arm_compute::TensorInfo aclOutput = BuildArmComputeTensorInfo(output);
36  arm_compute::TensorInfo aclWeights = BuildArmComputeTensorInfo(weights);
37  aclWeights.set_are_values_constant(weights.IsConstant());
38 
39  arm_compute::TensorInfo aclBiases;
40  arm_compute::TensorInfo* optionalAclBiases = nullptr;
41  if (descriptor.m_BiasEnabled)
42  {
43  ARMNN_ASSERT(biases.has_value());
44  // Same for bias as weights. We don't currently support non const.
45  if (!biases.value().IsConstant())
46  {
47  return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
48  "Arm NN ClFullyConnectedWorkload does not support non constant bias."};
49  }
50  aclBiases = BuildArmComputeTensorInfo(biases.value());
51  aclBiases.set_are_values_constant(biases.value().IsConstant());
52  optionalAclBiases = &aclBiases;
53  }
54 
55  const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo =
56  ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor, activationDescriptor);
57  return arm_compute::CLFullyConnectedLayer::validate(&aclInput,
58  &aclWeights,
59  optionalAclBiases,
60  &aclOutput,
61  fullyConnectedLayerInfo);
62 }
63 
65  const FullyConnectedQueueDescriptor& descriptor,
66  const WorkloadInfo& info,
67  std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager,
68  const arm_compute::CLCompileContext& clCompileContext)
69  : ClBaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info), m_FullyConnectedLayer(memoryManager)
70 {
71  // Add details for profiling output
72  WorkloadInfo detailsInfo;
73 
74  detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
75  detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
77  if (descriptor.m_Parameters.m_BiasEnabled)
78  {
80  }
81 
82  // Report Profiling Details
83  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClFullyConnectedWorkload_Construct",
84  descriptor.m_Parameters,
85  detailsInfo,
86  this->GetGuid());
87 
88  m_Data.ValidateInputsOutputs("ClFullyConnectedWorkload", descriptor.m_Parameters.GetNumInputs(),
89  1);
90 
91  arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
92  arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
93  arm_compute::ICLTensor& weights = PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
94 
95  arm_compute::ICLTensor* bias = nullptr;
97  {
98  bias = &PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor();
99  }
100 
101  const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
102 
103  arm_compute::FullyConnectedLayerInfo fc_info =
105  activationInfo);
106 
107  {
108  ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClFullyConnectedWorkload_configure");
109  m_FullyConnectedLayer.configure(clCompileContext,
110  &input,
111  &weights,
112  bias,
113  &output,
114  fc_info);
115  }
116 }
117 
119 {
120  ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClFullyConnectedWorkload_Execute", this->GetGuid());
121  RunClFunction(m_FullyConnectedLayer, CHECK_LOCATION());
122 }
123 
124 } //namespace armnn
bool IsConstant() const
Definition: Tensor.cpp:509
#define ARMNN_SCOPED_PROFILING_EVENT_CL_GUID(name, guid)
void RunClFunction(arm_compute::IFunction &function, const CheckLocation &location)
arm_compute::ActivationLayerInfo ConvertAdditionalInfoToAclActivationLayerInfo(const QueueDescriptor &queueDescriptor)
arm::pipe::ProfilingGuid GetGuid() const final
Definition: Workload.hpp:59
void ValidateInputsOutputs(const std::string &descName, unsigned int numExpectedIn, unsigned int numExpectedOut) const
Copyright (c) 2021 ARM Limited and Contributors.
const ConstTensorHandle * m_Bias
#define ARMNN_SCOPED_PROFILING_EVENT(backendId, name)
Definition: Profiling.hpp:220
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
arm_compute::Status ClFullyConnectedWorkloadValidate(const TensorInfo &input, const TensorInfo &output, const TensorInfo &weights, const Optional< TensorInfo > &biases, const FullyConnectedDescriptor &descriptor, const ActivationDescriptor *activationDescriptor)
const ConstTensorHandle * m_Weight
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:36
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
Optional< TensorInfo > m_BiasTensorInfo
uint32_t GetNumInputs() const
Get the number of views/inputs.
std::vector< ITensorHandle * > m_Outputs
#define ARMNN_REPORT_PROFILING_WORKLOAD_DESC(name, desc, infos, guid)
Definition: Profiling.hpp:227
Contains information about TensorInfos of a layer.
ClFullyConnectedWorkload(const FullyConnectedQueueDescriptor &descriptor, const WorkloadInfo &info, std::shared_ptr< arm_compute::MemoryManagerOnDemand > &memoryManager, const arm_compute::CLCompileContext &clCompileContext)
std::vector< ITensorHandle * > m_Inputs
Optional< TensorInfo > m_WeightsTensorInfo