ArmNN
 24.02
NeonConvolution2dWorkload.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017-2023 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  // We assume here that NeonConvolution2dWorkloadValidate has been called before the constructor.
99  ARMNN_ASSERT(info.m_InputTensorInfos[2].IsConstant() == true);
100  }
101 
102  arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
103 
104  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(m_Data.m_Parameters.m_DilationX,
106 
107  const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
108 
109  auto convolutionLayer = std::make_unique<arm_compute::NEConvolutionLayer>(memoryManager);
110  convolutionLayer->configure(&input,
111  m_KernelTensor.get(),
112  m_BiasTensor.get(),
113  &output,
114  padStrideInfo,
115  arm_compute::WeightsInfo(),
116  aclDilationInfo,
117  activationInfo,
118  isFastMathEnabled);
119 
120  m_ConvolutionMethod =
121  convolutionLayer->get_convolution_method(input.info(),
122  m_KernelTensor->info(),
123  output.info(),
124  padStrideInfo,
125  arm_compute::WeightsInfo(),
126  aclDilationInfo,
127  activationInfo,
128  isFastMathEnabled);
129 
130  // Add details for profiling output
131  WorkloadInfo detailsInfo;
132 
133  detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
134  detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
136 
137  // Report Profiling Details
138  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonConvolution2dWorkload_Construct",
139  descriptor.m_Parameters,
140  detailsInfo,
141  GetGuid());
142 
143  m_ConvolutionLayer.reset(convolutionLayer.release());
144 
145  ARMNN_ASSERT(m_ConvolutionLayer);
146  m_KernelTensorInfo = info.m_InputTensorInfos[1];
147 
149  {
150  m_BiasTensorInfo = info.m_InputTensorInfos[2];
151  }
152 }
153 
155 {
156  ARMNN_SCOPED_PROFILING_EVENT_NEON_NAME_GUID("NeonConvolution2dWorkload_Execute");
157  // The constant tensors may not be fully in place until the workload is Executed
158  if (!prepared)
159  {
160  InitializeArmComputeTensorData(*m_KernelTensor, m_KernelTensorInfo, m_Data.m_Inputs[1]);
161 
163  {
164  InitializeArmComputeTensorData(*m_BiasTensor, m_BiasTensorInfo, m_Data.m_Inputs[2]);
165  }
166  m_ConvolutionLayer->prepare();
167  FreeTensorIfUnused(m_KernelTensor);
168  FreeTensorIfUnused(m_BiasTensor);
169  prepared = true;
170  }
171  m_ConvolutionLayer->run();
172 }
173 
174 arm_compute::ConvolutionMethod NeonConvolution2dWorkload::GetConvolutionMethod() const
175 {
176  return m_ConvolutionMethod;
177 }
178 
179 } //namespace armnn
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
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:446
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:509
armnn::InitializeArmComputeTensorData
void InitializeArmComputeTensorData(arm_compute::Tensor &tensor, TensorInfo tensorInfo, const ITensorHandle *handle)
Definition: NeonWorkloadUtils.hpp:68
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:174
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:32
armnn::NeonConvolution2dWorkload::Execute
void Execute() const override
Definition: NeonConvolution2dWorkload.cpp:154
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