ArmNN
 24.08
NeonConvolution2dWorkload.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017-2024 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 
13 
14 #include <arm_compute/runtime/NEON/functions/NEConvolutionLayer.h>
15 
16 #include <armnn/Types.hpp>
17 #include <Half.hpp>
18 
19 namespace armnn
20 {
21 
22 using namespace armcomputetensorutils;
23 
25  const TensorInfo& output,
26  const Convolution2dDescriptor& descriptor,
27  const TensorInfo& weights,
28  const Optional<TensorInfo>& biases,
29  bool isFastMathEnabled,
30  const ActivationDescriptor* activationDescriptor)
31 {
32  const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
33  const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
34  arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weights, descriptor.m_DataLayout);
35  aclWeightsInfo.set_are_values_constant(weights.IsConstant());
36 
37  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(descriptor.m_DilationX,
38  descriptor.m_DilationY);
39 
40  arm_compute::TensorInfo aclBiasesInfo;
41  arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr;
42 
43  if (descriptor.m_BiasEnabled)
44  {
45  if (!biases.has_value())
46  {
47  return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
48  "ArmNN NeonConvolution2dWorkload has empty bias value."};
49  }
50  aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
51  aclBiasesInfo.set_are_values_constant(biases.value().IsConstant());
52  optionalAclBiasesInfo = &aclBiasesInfo;
53  }
54 
55  arm_compute::PadStrideInfo layerInfo = BuildArmComputePadStrideInfo(descriptor);
56 
57  const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
58  activationDescriptor);
59 
60  return arm_compute::NEConvolutionLayer::validate(&aclInputInfo,
61  &aclWeightsInfo,
62  optionalAclBiasesInfo,
63  &aclOutputInfo,
64  layerInfo,
65  arm_compute::WeightsInfo(),
66  aclDilationInfo,
67  activationInfo,
68  isFastMathEnabled);
69 }
70 
72  const Convolution2dQueueDescriptor& descriptor,
73  const WorkloadInfo& info,
74  std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager,
75  const bool isFastMathEnabled)
77 {
78  using arm_compute::NEConvolutionLayer;
79 
80  uint32_t numInputs = m_Data.m_Parameters.m_BiasEnabled ? 3: 2;
81  m_Data.ValidateInputsOutputs("NeonConvolution2dWorkload", numInputs, 1);
82 
83  arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
84  arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
85 
86  arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
87  input.info()->set_data_layout(aclDataLayout);
88  output.info()->set_data_layout(aclDataLayout);
89 
90  m_KernelTensor = std::make_unique<arm_compute::Tensor>();
91  BuildArmComputeTensor(*m_KernelTensor, info.m_InputTensorInfos[1], m_Data.m_Parameters.m_DataLayout);
92  m_KernelTensor->info()->set_are_values_constant(info.m_InputTensorInfos[1].IsConstant());
94  {
95  m_BiasTensor = std::make_unique<arm_compute::Tensor>();
96  BuildArmComputeTensor(*m_BiasTensor, info.m_InputTensorInfos[2], m_Data.m_Parameters.m_DataLayout);
97  m_BiasTensor->info()->set_are_values_constant(info.m_InputTensorInfos[2].IsConstant());
98  }
99 
100  arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
101 
102  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(m_Data.m_Parameters.m_DilationX,
104 
105  const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
106 
107  auto convolutionLayer = std::make_unique<arm_compute::NEConvolutionLayer>(memoryManager);
108  convolutionLayer->configure(&input,
109  m_KernelTensor.get(),
110  m_BiasTensor.get(),
111  &output,
112  padStrideInfo,
113  arm_compute::WeightsInfo(),
114  aclDilationInfo,
115  activationInfo,
116  isFastMathEnabled);
117 
118  m_ConvolutionMethod =
119  convolutionLayer->get_convolution_method(input.info(),
120  m_KernelTensor->info(),
121  output.info(),
122  padStrideInfo,
123  arm_compute::WeightsInfo(),
124  aclDilationInfo,
125  activationInfo,
126  isFastMathEnabled);
127 
128  // Add details for profiling output
129  WorkloadInfo detailsInfo;
130 
131  detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
132  detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
134 
135  // Report Profiling Details
136  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonConvolution2dWorkload_Construct",
137  descriptor.m_Parameters,
138  detailsInfo,
139  GetGuid());
140 
141  m_ConvolutionLayer.reset(convolutionLayer.release());
142  m_KernelTensorInfo = info.m_InputTensorInfos[1];
143 
145  {
146  m_BiasTensorInfo = info.m_InputTensorInfos[2];
147  }
148 }
149 
151 {
152  ARMNN_SCOPED_PROFILING_EVENT_NEON_NAME_GUID("NeonConvolution2dWorkload_Execute");
153  // The constant tensors may not be fully in place until the workload is Executed
154  if (!prepared)
155  {
156  InitializeArmComputeTensorData(*m_KernelTensor, m_KernelTensorInfo, m_Data.m_Inputs[1]);
157 
159  {
160  InitializeArmComputeTensorData(*m_BiasTensor, m_BiasTensorInfo, m_Data.m_Inputs[2]);
161  }
162  m_ConvolutionLayer->prepare();
163  FreeTensorIfUnused(m_KernelTensor);
164  FreeTensorIfUnused(m_BiasTensor);
165  prepared = true;
166  }
167  m_ConvolutionLayer->run();
168 }
169 
170 arm_compute::ConvolutionMethod NeonConvolution2dWorkload::GetConvolutionMethod() const
171 {
172  return m_ConvolutionMethod;
173 }
174 
175 } //namespace armnn
armnn::NeonConvolution2dWorkload::NeonConvolution2dWorkload
NeonConvolution2dWorkload(const Convolution2dQueueDescriptor &descriptor, const WorkloadInfo &info, std::shared_ptr< arm_compute::MemoryManagerOnDemand > &memoryManager, const bool isFastMathENabled=false)
Definition: NeonConvolution2dWorkload.cpp:71
armnn::ActivationDescriptor
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:36
armnn::Optional
Definition: Optional.hpp:270
armnn::DataLayout
DataLayout
Definition: Types.hpp:62
armnn::QueueDescriptor::ValidateInputsOutputs
void ValidateInputsOutputs(const std::string &descName, unsigned int numExpectedIn, unsigned int numExpectedOut) const
Definition: WorkloadData.cpp:447
armnn::ConvertAdditionalInfoToAclActivationLayerInfo
arm_compute::ActivationLayerInfo ConvertAdditionalInfoToAclActivationLayerInfo(const QueueDescriptor &queueDescriptor)
Definition: ArmComputeUtils.hpp:105
armnn::TensorInfo
Definition: Tensor.hpp:152
NeonConvolution2dWorkload.hpp
armnn::Convolution2dDescriptor::m_DilationY
uint32_t m_DilationY
Dilation along y axis.
Definition: Descriptors.hpp:580
armnn::TensorInfo::IsConstant
bool IsConstant() const
Definition: Tensor.cpp:513
armnn::InitializeArmComputeTensorData
void InitializeArmComputeTensorData(arm_compute::Tensor &tensor, TensorInfo tensorInfo, const ITensorHandle *handle)
Definition: NeonWorkloadUtils.hpp:69
armnn::WorkloadInfo::m_ConvolutionMethod
Optional< std::string > m_ConvolutionMethod
Definition: WorkloadInfo.hpp:23
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::Convolution2dQueueDescriptor
Definition: WorkloadData.hpp:210
armnn::WorkloadInfo
Contains information about TensorInfos of a layer.
Definition: WorkloadInfo.hpp:16
PolymorphicDowncast.hpp
armnn::Convolution2dDescriptor::m_BiasEnabled
bool m_BiasEnabled
Enable/disable bias.
Definition: Descriptors.hpp:582
armnn::GetConvolutionMethodString
std::string GetConvolutionMethodString(arm_compute::ConvolutionMethod &convolutionMethod)
Definition: ClWorkloadUtils.hpp:46
armnn::Convolution2dDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:584
ArmComputeUtils.hpp
armnn::ConvertActivationDescriptorToAclActivationLayerInfo
arm_compute::ActivationLayerInfo ConvertActivationDescriptorToAclActivationLayerInfo(const ActivationDescriptor &actDesc)
Definition: ArmComputeUtils.hpp:87
armnn::BoostLogSeverityMapping::info
@ info
armnn::QueueDescriptor::m_Outputs
std::vector< ITensorHandle * > m_Outputs
Definition: WorkloadData.hpp:27
armnn::Convolution2dDescriptor
A Convolution2dDescriptor for the Convolution2dLayer.
Definition: Descriptors.hpp:534
ARMNN_REPORT_PROFILING_WORKLOAD_DESC
#define ARMNN_REPORT_PROFILING_WORKLOAD_DESC(name, desc, infos, guid)
Definition: Profiling.hpp:227
Half.hpp
armnn::BaseWorkload< Convolution2dQueueDescriptor >::GetGuid
arm::pipe::ProfilingGuid GetGuid() const final
Definition: Workload.hpp:67
armnn::NeonConvolution2dWorkload::GetConvolutionMethod
arm_compute::ConvolutionMethod GetConvolutionMethod() const
Definition: NeonConvolution2dWorkload.cpp:170
TensorHandle.hpp
armnn::Status
Status
Definition: Types.hpp:42
armnn::BaseWorkload< Convolution2dQueueDescriptor >::m_Data
Convolution2dQueueDescriptor m_Data
Definition: Workload.hpp:89
armnn::Convolution2dDescriptor::m_DilationX
uint32_t m_DilationX
Dilation along x axis.
Definition: Descriptors.hpp:578
armnn::NeonConvolution2dWorkloadValidate
arm_compute::Status NeonConvolution2dWorkloadValidate(const TensorInfo &input, const TensorInfo &output, const Convolution2dDescriptor &descriptor, const TensorInfo &weights, const Optional< TensorInfo > &biases, bool isFastMathEnabled, const ActivationDescriptor *activationDescriptor)
Definition: NeonConvolution2dWorkload.cpp:24
armnn::WorkloadInfo::m_InputTensorInfos
std::vector< TensorInfo > m_InputTensorInfos
Definition: WorkloadInfo.hpp:18
NeonWorkloadUtils.hpp
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
Types.hpp
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:33
armnn::NeonConvolution2dWorkload::Execute
void Execute() const override
Definition: NeonConvolution2dWorkload.cpp:150
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