ArmNN
 24.08
ClConvolution2dWorkload.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 
8 #include "ClWorkloadUtils.hpp"
9 
10 #include <cl/ClLayerSupport.hpp>
11 #include <cl/ClTensorHandle.hpp>
12 #include <cl/ClLayerSupport.hpp>
16 
17 #include <arm_compute/runtime/CL/functions/CLConvolutionLayer.h>
18 
19 namespace armnn
20 {
21 using namespace armcomputetensorutils;
22 
24  const TensorInfo& output,
25  const Convolution2dDescriptor& descriptor,
26  const TensorInfo& weights,
27  const Optional<TensorInfo>& biases,
28  bool isFastMathEnabled,
29  const ActivationDescriptor* activationDescriptor)
30 {
31  const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
32  const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
33  arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weights, descriptor.m_DataLayout);
34  aclWeightsInfo.set_are_values_constant(weights.IsConstant());
35 
36  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(descriptor.m_DilationX,
37  descriptor.m_DilationY);
38 
39  arm_compute::TensorInfo aclBiasesInfo;
40  arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr;
41 
42  if (descriptor.m_BiasEnabled)
43  {
44  if (!biases.has_value())
45  {
46  return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
47  "ArmNN ClConvolution2dWorkload has empty bias value."};
48  }
49  // There's currently a problem with non const bias, so we'll explicitly block it here.
50  if (!biases.value().IsConstant())
51  {
52  return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
53  "ArmNN ClDepthwiseConv2dWorkload does not support non constant bias."};
54  }
55  aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
56  aclBiasesInfo.set_are_values_constant(biases.value().IsConstant());
57  optionalAclBiasesInfo = &aclBiasesInfo;
58  }
59 
60  arm_compute::PadStrideInfo layerInfo = BuildArmComputePadStrideInfo(descriptor);
61 
62  const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
63  activationDescriptor);
64 
65  return arm_compute::CLConvolutionLayer::validate(&aclInputInfo,
66  &aclWeightsInfo,
67  optionalAclBiasesInfo,
68  &aclOutputInfo,
69  layerInfo,
70  arm_compute::WeightsInfo(),
71  aclDilationInfo,
72  activationInfo,
73  isFastMathEnabled);
74 }
75 
77  const WorkloadInfo& info,
78  std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager,
79  const arm_compute::CLCompileContext& clCompileContext,
80  const bool isFastMathEnabled)
82  , m_ConvolutionLayer(memoryManager)
83 {
84  ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClConvolution2dWorkload");
85 
86  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(m_Data.m_Parameters.m_DilationX,
88 
89  uint32_t numInputs = m_Data.m_Parameters.m_BiasEnabled ? 3: 2;
90  m_Data.ValidateInputsOutputs("ClConvolution2dWorkload", numInputs, 1);
91 
92  arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
93  arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
94  arm_compute::ICLTensor& weights = static_cast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
95  weights.info()->set_are_values_constant(info.m_InputTensorInfos[1].IsConstant());
96 
98  {
99  arm_compute::ICLTensor& bias = static_cast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor();
100  bias.info()->set_are_values_constant(info.m_InputTensorInfos[2].IsConstant());
101  // We assume here that NeonConvolution2dWorkloadValidate has been called before the constructor.
102  ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(info.m_InputTensorInfos[2].IsConstant() == true,
103  "The bias tensor must be constant.");
104  m_BiasProxy = std::make_unique<ICLTensorProxy>(&bias);
105  }
106 
107  // Create Proxy tensor and set the initial tensor handle to it
108  m_InputProxy = std::make_unique<ICLTensorProxy>(&input);
109  m_OutputProxy = std::make_unique<ICLTensorProxy>(&output);
110  m_WeightsProxy = std::make_unique<ICLTensorProxy>(&weights);
111 
112  arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
113  input.info()->set_data_layout(aclDataLayout);
114  output.info()->set_data_layout(aclDataLayout);
115  weights.info()->set_data_layout(aclDataLayout);
116 
117  arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
118 
119  const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
120 
121  {
122  ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClConvolution2dWorkload_configure");
123  m_ConvolutionLayer.configure(clCompileContext,
124  m_InputProxy.get(),
125  m_WeightsProxy.get(),
126  m_BiasProxy.get(),
127  m_OutputProxy.get(),
128  padStrideInfo,
129  arm_compute::WeightsInfo(),
130  aclDilationInfo,
131  activationInfo,
132  isFastMathEnabled);
133  }
134 
135  m_ConvolutionMethod =
136  m_ConvolutionLayer.get_convolution_method(input.info(),
137  weights.info(),
138  output.info(),
139  padStrideInfo,
140  arm_compute::WeightsInfo(),
141  activationInfo,
142  arm_compute::CLScheduler::get().target(),
143  aclDilationInfo,
144  isFastMathEnabled);
145 
146  // Add details for profiling output
147  WorkloadInfo detailsInfo;
148 
149  detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
150  detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
152 
153  // Report Profiling Details
154  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClConvolution2dWorkload_Construct",
155  descriptor.m_Parameters,
156  detailsInfo,
157  GetGuid());
158 }
159 
161 {
162  ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClConvolution2dWorkload_Execute");
163  RunClFunction(m_ConvolutionLayer, CHECK_LOCATION());
164 }
165 
166 arm_compute::ConvolutionMethod ClConvolution2dWorkload::GetConvolutionMethod() const
167 {
168  return m_ConvolutionMethod;
169 }
170 
172 {
173  arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
174  arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
175 
176  m_InputProxy->set(&input);
177  m_OutputProxy->set(&output);
178 }
179 
180 } //namespace armnn
armnn::ActivationDescriptor
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:36
armnn::ClConvolution2dWorkload::ClConvolution2dWorkload
ClConvolution2dWorkload(const Convolution2dQueueDescriptor &descriptor, const WorkloadInfo &info, std::shared_ptr< arm_compute::MemoryManagerOnDemand > &memoryManager, const arm_compute::CLCompileContext &clCompileContext, const bool isFastMathEnabled=false)
Definition: ClConvolution2dWorkload.cpp:76
armnn::Optional
Definition: Optional.hpp:270
armnn::DataLayout
DataLayout
Definition: Types.hpp:62
armnn::ClConvolution2dWorkload::GetConvolutionMethod
arm_compute::ConvolutionMethod GetConvolutionMethod() const
Definition: ClConvolution2dWorkload.cpp:166
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
ClLayerSupport.hpp
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnn::ClConvolution2dWorkload::Execute
void Execute() const override
Definition: ClConvolution2dWorkload.cpp:160
armnn::ClBaseWorkload
Definition: ClBaseWorkload.hpp:13
armnn::ClConvolution2dWorkloadValidate
arm_compute::Status ClConvolution2dWorkloadValidate(const TensorInfo &input, const TensorInfo &output, const Convolution2dDescriptor &descriptor, const TensorInfo &weights, const Optional< TensorInfo > &biases, bool isFastMathEnabled, const ActivationDescriptor *activationDescriptor)
Definition: ClConvolution2dWorkload.cpp:23
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::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_SCOPED_PROFILING_EVENT_CL_NAME_GUID
#define ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID(label)
Creates a profiling event that uses GetGuid() and GetName() from the calling class.
Definition: ClWorkloadUtils.hpp:36
armnn::WorkloadInfo
Contains information about TensorInfos of a layer.
Definition: WorkloadInfo.hpp:16
armnn::Convolution2dDescriptor::m_BiasEnabled
bool m_BiasEnabled
Enable/disable bias.
Definition: Descriptors.hpp:582
ClWorkloadUtils.hpp
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
ClConvolution2dWorkload.hpp
ARMNN_REPORT_PROFILING_WORKLOAD_DESC
#define ARMNN_REPORT_PROFILING_WORKLOAD_DESC(name, desc, infos, guid)
Definition: Profiling.hpp:227
armnn::BaseWorkload< Convolution2dQueueDescriptor >::GetGuid
arm::pipe::ProfilingGuid GetGuid() const final
Definition: Workload.hpp:67
armnn::IClTensorHandle
Definition: IClTensorHandle.hpp:13
TensorHandle.hpp
armnn::Status
Status
Definition: Types.hpp:42
ClTensorHandle.hpp
armnn::BaseWorkload< Convolution2dQueueDescriptor >::m_Data
Convolution2dQueueDescriptor m_Data
Definition: Workload.hpp:89
armnn::RunClFunction
void RunClFunction(arm_compute::IFunction &function, const CheckLocation &location)
Definition: ClWorkloadUtils.hpp:167
armnn::Convolution2dDescriptor::m_DilationX
uint32_t m_DilationX
Dilation along x axis.
Definition: Descriptors.hpp:578
armnn::WorkloadInfo::m_InputTensorInfos
std::vector< TensorInfo > m_InputTensorInfos
Definition: WorkloadInfo.hpp:18
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
ArmComputeTensorUtils.hpp
armnn::ClConvolution2dWorkload::Reconfigure
void Reconfigure() override
Definition: ClConvolution2dWorkload.cpp:171
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
ARMNN_THROW_INVALIDARG_MSG_IF_FALSE
#define ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(_cond, _str)
Definition: Exceptions.hpp:210