ArmNN
 21.11
ClConvolution2dWorkload.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. 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  : BaseWorkload<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  arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
94  input.info()->set_data_layout(aclDataLayout);
95  output.info()->set_data_layout(aclDataLayout);
96 
97  arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
98 
99  const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
100 
101  {
102  ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload_configure");
103  m_ConvolutionLayer.configure(clCompileContext,
104  &input,
105  m_KernelTensor.get(),
106  m_BiasTensor.get(),
107  &output,
108  padStrideInfo,
109  arm_compute::WeightsInfo(),
110  aclDilationInfo,
111  activationInfo,
112  isFastMathEnabled);
113  }
114 
115  m_ConvolutionMethod =
116  m_ConvolutionLayer.get_convolution_method(input.info(),
117  m_KernelTensor->info(),
118  output.info(),
119  padStrideInfo,
120  arm_compute::WeightsInfo(),
121  activationInfo,
122  arm_compute::CLScheduler::get().target(),
123  aclDilationInfo,
124  isFastMathEnabled);
125 
126  // Add details for profiling output
127  WorkloadInfo detailsInfo;
128 
129  detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
130  detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
133  if (descriptor.m_Parameters.m_BiasEnabled)
134  {
136  }
137 
138  // Report Profiling Details
139  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClConvolution2dWorkload_Construct",
140  descriptor.m_Parameters,
141  detailsInfo,
142  this->GetGuid());
143 
145 
146  if (m_BiasTensor)
147  {
149  }
150 
151  // Force Compute Library to perform the necessary copying and reshaping, after which
152  // delete all the input tensors that will no longer be needed
153  {
154  ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload_prepare");
155  m_ConvolutionLayer.prepare();
156  }
157  FreeUnusedTensors();
158 }
159 
161 {
162  ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClConvolution2dWorkload_Execute", this->GetGuid());
163  RunClFunction(m_ConvolutionLayer, CHECK_LOCATION());
164 }
165 
166 arm_compute::ConvolutionMethod ClConvolution2dWorkload::GetConvolutionMethod() const
167 {
168  return m_ConvolutionMethod;
169 }
170 
171 void ClConvolution2dWorkload::FreeUnusedTensors()
172 {
173  FreeTensorIfUnused(m_KernelTensor);
174  FreeTensorIfUnused(m_BiasTensor);
175 }
176 
177 } //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:25
#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)