ArmNN
 24.02
ClConvolution2dWorkload.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 
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_ASSERT(info.m_InputTensorInfos[2].IsConstant() == true);
103  m_BiasProxy = std::make_unique<ICLTensorProxy>(&bias);
104  }
105 
106  // Create Proxy tensor and set the initial tensor handle to it
107  m_InputProxy = std::make_unique<ICLTensorProxy>(&input);
108  m_OutputProxy = std::make_unique<ICLTensorProxy>(&output);
109  m_WeightsProxy = std::make_unique<ICLTensorProxy>(&weights);
110 
111  arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
112  input.info()->set_data_layout(aclDataLayout);
113  output.info()->set_data_layout(aclDataLayout);
114  weights.info()->set_data_layout(aclDataLayout);
115 
116  arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
117 
118  const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
119 
120  {
121  ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClConvolution2dWorkload_configure");
122  m_ConvolutionLayer.configure(clCompileContext,
123  m_InputProxy.get(),
124  m_WeightsProxy.get(),
125  m_BiasProxy.get(),
126  m_OutputProxy.get(),
127  padStrideInfo,
128  arm_compute::WeightsInfo(),
129  aclDilationInfo,
130  activationInfo,
131  isFastMathEnabled);
132  }
133 
134  m_ConvolutionMethod =
135  m_ConvolutionLayer.get_convolution_method(input.info(),
136  weights.info(),
137  output.info(),
138  padStrideInfo,
139  arm_compute::WeightsInfo(),
140  activationInfo,
141  arm_compute::CLScheduler::get().target(),
142  aclDilationInfo,
143  isFastMathEnabled);
144 
145  // Add details for profiling output
146  WorkloadInfo detailsInfo;
147 
148  detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
149  detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
151 
152  // Report Profiling Details
153  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClConvolution2dWorkload_Construct",
154  descriptor.m_Parameters,
155  detailsInfo,
156  GetGuid());
157 }
158 
160 {
161  ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClConvolution2dWorkload_Execute");
162  RunClFunction(m_ConvolutionLayer, CHECK_LOCATION());
163 }
164 
165 arm_compute::ConvolutionMethod ClConvolution2dWorkload::GetConvolutionMethod() const
166 {
167  return m_ConvolutionMethod;
168 }
169 
171 {
172  arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
173  arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
174 
175  m_InputProxy->set(&input);
176  m_OutputProxy->set(&output);
177 }
178 
179 } //namespace armnn
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
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:165
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
ClLayerSupport.hpp
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnn::ClConvolution2dWorkload::Execute
void Execute() const override
Definition: ClConvolution2dWorkload.cpp:159
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:509
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:168
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:170
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