ArmNN
 22.05.01
ClConvolution2dWorkload.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 
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  // The arm_compute::CLConvolutionLayer supports both const and non const
32  // weights. However, in the case of non const weights we'd have to call
33  // prepare or configure for each inference which we're not setup to do just yet.
34  if (!weights.IsConstant())
35  {
36  return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
37  "ArmNN ClConvolution2dWorkload does not support non constant weights."};
38  }
39 
40  const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
41  const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
42  arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weights, descriptor.m_DataLayout);
43  aclWeightsInfo.set_are_values_constant(weights.IsConstant());
44 
45  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(descriptor.m_DilationX,
46  descriptor.m_DilationY);
47 
48  arm_compute::TensorInfo aclBiasesInfo;
49  arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr;
50 
51  if (descriptor.m_BiasEnabled)
52  {
53  ARMNN_ASSERT(biases.has_value());
54  // Same for bias as weights. We don't currently support non const.
55  if (!biases.value().IsConstant())
56  {
57  return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
58  "ArmNN ClConvolution2dWorkload does not support non constant bias."};
59  }
60  aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
61  aclBiasesInfo.set_are_values_constant(biases.value().IsConstant());
62  optionalAclBiasesInfo = &aclBiasesInfo;
63  }
64 
65  arm_compute::PadStrideInfo layerInfo = BuildArmComputePadStrideInfo(descriptor);
66 
67  const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
68  activationDescriptor);
69 
70  return arm_compute::CLConvolutionLayer::validate(&aclInputInfo,
71  &aclWeightsInfo,
72  optionalAclBiasesInfo,
73  &aclOutputInfo,
74  layerInfo,
75  arm_compute::WeightsInfo(),
76  aclDilationInfo,
77  activationInfo,
78  isFastMathEnabled);
79 }
80 
82  const WorkloadInfo& info,
83  std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager,
84  const arm_compute::CLCompileContext& clCompileContext,
85  const bool isFastMathEnabled)
86  : ClBaseWorkload<Convolution2dQueueDescriptor>(descriptor, info)
87  , m_ConvolutionLayer(memoryManager)
88 {
89  ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload");
90 
91  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(m_Data.m_Parameters.m_DilationX,
93 
94  uint32_t numInputs = m_Data.m_Parameters.m_BiasEnabled ? 3: 2;
95  m_Data.ValidateInputsOutputs("ClConvolution2dWorkload", numInputs, 1);
96 
97  arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
98  arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
99  arm_compute::ICLTensor& weights = static_cast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
100  arm_compute::ICLTensor* bias = nullptr;
102  {
103  bias = &static_cast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor();
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 
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(Compute::Undefined, "ClConvolution2dWorkload_configure");
122  m_ConvolutionLayer.configure(clCompileContext,
123  m_InputProxy.get(),
124  &weights,
125  bias,
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;
152  if (descriptor.m_Parameters.m_BiasEnabled)
153  {
155  }
156 
157  // Report Profiling Details
158  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClConvolution2dWorkload_Construct",
159  descriptor.m_Parameters,
160  detailsInfo,
161  GetGuid());
162 }
163 
165 {
166  ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClConvolution2dWorkload_Execute", GetGuid());
167  RunClFunction(m_ConvolutionLayer, CHECK_LOCATION());
168 }
169 
170 arm_compute::ConvolutionMethod ClConvolution2dWorkload::GetConvolutionMethod() const
171 {
172  return m_ConvolutionMethod;
173 }
174 
176 {
177  arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
178  arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
179 
180  m_InputProxy->set(&input);
181  m_OutputProxy->set(&output);
182 }
183 
184 } //namespace armnn
bool m_BiasEnabled
Enable/disable bias.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
bool IsConstant() const
Definition: Tensor.cpp:509
#define ARMNN_SCOPED_PROFILING_EVENT_CL_GUID(name, guid)
DataLayout
Definition: Types.hpp:62
std::string GetConvolutionMethodString(arm_compute::ConvolutionMethod &convolutionMethod)
Optional< std::string > m_ConvolutionMethod
void RunClFunction(arm_compute::IFunction &function, const CheckLocation &location)
A Convolution2dDescriptor for the Convolution2dLayer.
arm_compute::ActivationLayerInfo ConvertAdditionalInfoToAclActivationLayerInfo(const QueueDescriptor &queueDescriptor)
arm::pipe::ProfilingGuid GetGuid() const final
Definition: Workload.hpp:59
const ConstTensorHandle * m_Weight
const ConstTensorHandle * m_Bias
void ValidateInputsOutputs(const std::string &descName, unsigned int numExpectedIn, unsigned int numExpectedOut) const
Copyright (c) 2021 ARM Limited and Contributors.
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)
uint32_t m_DilationY
Dilation along y axis.
#define ARMNN_SCOPED_PROFILING_EVENT(backendId, name)
Definition: Profiling.hpp:220
const TensorInfo & GetTensorInfo() const
std::vector< TensorInfo > m_InputTensorInfos
arm_compute::ConvolutionMethod GetConvolutionMethod() const
ClConvolution2dWorkload(const Convolution2dQueueDescriptor &descriptor, const WorkloadInfo &info, std::shared_ptr< arm_compute::MemoryManagerOnDemand > &memoryManager, const arm_compute::CLCompileContext &clCompileContext, const bool isFastMathEnabled=false)
bool has_value() const noexcept
Definition: Optional.hpp:53
Status
enumeration
Definition: Types.hpp:42
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
std::vector< TensorInfo > m_OutputTensorInfos
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:36
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
Optional< TensorInfo > m_BiasTensorInfo
uint32_t m_DilationX
Dilation along x axis.
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.
std::vector< ITensorHandle * > m_Inputs
Optional< TensorInfo > m_WeightsTensorInfo
arm_compute::ActivationLayerInfo ConvertActivationDescriptorToAclActivationLayerInfo(const ActivationDescriptor &actDesc)