ArmNN
 22.02
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  const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
32  const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
33  const arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weights, descriptor.m_DataLayout);
34 
35  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(descriptor.m_DilationX,
36  descriptor.m_DilationY);
37 
38  arm_compute::TensorInfo aclBiasesInfo;
39  arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr;
40 
41  if (descriptor.m_BiasEnabled)
42  {
43  ARMNN_ASSERT(biases.has_value());
44 
45  aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
46  optionalAclBiasesInfo = &aclBiasesInfo;
47  }
48 
49  arm_compute::PadStrideInfo layerInfo = BuildArmComputePadStrideInfo(descriptor);
50 
51  const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
52  activationDescriptor);
53 
54  return arm_compute::CLConvolutionLayer::validate(&aclInputInfo,
55  &aclWeightsInfo,
56  optionalAclBiasesInfo,
57  &aclOutputInfo,
58  layerInfo,
59  arm_compute::WeightsInfo(),
60  aclDilationInfo,
61  activationInfo,
62  isFastMathEnabled);
63 }
64 
66  const WorkloadInfo& info,
67  std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager,
68  const arm_compute::CLCompileContext& clCompileContext,
69  const bool isFastMathEnabled)
70  : ClBaseWorkload<Convolution2dQueueDescriptor>(descriptor, info)
71  , m_ConvolutionLayer(memoryManager)
72 {
73  ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload");
74  const TensorInfo& weightInfo = m_Data.m_Weight->GetTensorInfo();
75 
76  m_KernelTensor = std::make_unique<arm_compute::CLTensor>();
77  BuildArmComputeTensor(*m_KernelTensor, weightInfo, m_Data.m_Parameters.m_DataLayout);
78 
79  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(m_Data.m_Parameters.m_DilationX,
81 
83  {
84  m_BiasTensor = std::make_unique<arm_compute::CLTensor>();
85  BuildArmComputeTensor(*m_BiasTensor, m_Data.m_Bias->GetTensorInfo(), m_Data.m_Parameters.m_DataLayout);
86  }
87 
88  m_Data.ValidateInputsOutputs("ClConvolution2dWorkload", 1, 1);
89 
90  arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
91  arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
92 
93  // Create Proxy tensor and set the initial tensor handle to it
94  m_InputProxy = std::make_unique<ICLTensorProxy>(&input);
95  m_OutputProxy = std::make_unique<ICLTensorProxy>(&output);
96 
97  arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
98  input.info()->set_data_layout(aclDataLayout);
99  output.info()->set_data_layout(aclDataLayout);
100 
101  arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
102 
103  const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
104 
105  {
106  ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload_configure");
107  m_ConvolutionLayer.configure(clCompileContext,
108  m_InputProxy.get(),
109  m_KernelTensor.get(),
110  m_BiasTensor.get(),
111  m_OutputProxy.get(),
112  padStrideInfo,
113  arm_compute::WeightsInfo(),
114  aclDilationInfo,
115  activationInfo,
116  isFastMathEnabled);
117  }
118 
119  m_ConvolutionMethod =
120  m_ConvolutionLayer.get_convolution_method(input.info(),
121  m_KernelTensor->info(),
122  output.info(),
123  padStrideInfo,
124  arm_compute::WeightsInfo(),
125  activationInfo,
126  arm_compute::CLScheduler::get().target(),
127  aclDilationInfo,
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;
137  if (descriptor.m_Parameters.m_BiasEnabled)
138  {
140  }
141 
142  // Report Profiling Details
143  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClConvolution2dWorkload_Construct",
144  descriptor.m_Parameters,
145  detailsInfo,
146  this->GetGuid());
147 
149 
150  if (m_BiasTensor)
151  {
153  }
154 
155  // Force Compute Library to perform the necessary copying and reshaping, after which
156  // delete all the input tensors that will no longer be needed
157  {
158  ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload_prepare");
159  m_ConvolutionLayer.prepare();
160  }
161  FreeUnusedTensors();
162 }
163 
165 {
166  ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClConvolution2dWorkload_Execute", this->GetGuid());
167  RunClFunction(m_ConvolutionLayer, CHECK_LOCATION());
168 }
169 
170 arm_compute::ConvolutionMethod ClConvolution2dWorkload::GetConvolutionMethod() const
171 {
172  return m_ConvolutionMethod;
173 }
174 
175 void ClConvolution2dWorkload::FreeUnusedTensors()
176 {
177  FreeTensorIfUnused(m_KernelTensor);
178  FreeTensorIfUnused(m_BiasTensor);
179 }
180 
182 {
183  arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
184  arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
185 
186  m_InputProxy->set(&input);
187  m_OutputProxy->set(&output);
188 }
189 
190 } //namespace armnn
bool m_BiasEnabled
Enable/disable bias.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
#define ARMNN_SCOPED_PROFILING_EVENT_CL_GUID(name, guid)
DataLayout
Definition: Types.hpp:49
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)
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:29
#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:209
profiling::ProfilingGuid GetGuid() const final
Definition: Workload.hpp:55
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.
void InitializeArmComputeClTensorData(arm_compute::CLTensor &clTensor, const ConstTensorHandle *handle)
std::vector< ITensorHandle * > m_Inputs
Optional< TensorInfo > m_WeightsTensorInfo
arm_compute::ActivationLayerInfo ConvertActivationDescriptorToAclActivationLayerInfo(const ActivationDescriptor &actDesc)