ArmNN
 24.08
ClDepthwiseConvolutionWorkload.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 <ResolveType.hpp>
9 #include "ClWorkloadUtils.hpp"
10 
11 #include <armnn/Exceptions.hpp>
14 #include <cl/ClTensorHandle.hpp>
18 
19 #include <arm_compute/runtime/CL/functions/CLDepthwiseConvolutionLayer.h>
20 
21 namespace armnn
22 {
23 
24 using namespace armcomputetensorutils;
25 
27  const TensorInfo& output,
28  const DepthwiseConvolution2dDescriptor& descriptor,
29  const TensorInfo& weights,
30  const Optional<TensorInfo>& biases,
31  const ActivationDescriptor* activationDescriptor)
32 {
33  const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
34  const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
35 
36  // ArmNN format for weights for depthwise is [1, H, W, C] independently of the input/output layout
37  //
38  // ACL format for weights for depthwise is:
39  // - [1, H, W, C] for [N, H, W, C] input/output layout (matches with ArmNN)
40  // - [1, C, H, W] for [N, C, H, W] input/output layout
41  //
42  // Therefore ArmNN weights have to be permuted when input/output layout is [N, C, H, W] to pass them to ACL.
43  // The PermuteDepthwiseConv2dWeights backend optimization takes care of this, but it has not been performed yet,
44  // so we do the permute here for the TensorInfo weights.
45  unsigned int aclDepthMultiplier;
46  TensorInfo weightsPermuted;
47  std::tie(weightsPermuted, aclDepthMultiplier) = Convert1HWOTensorInfoToAcl(weights, input,descriptor.m_DataLayout);
48 
49  // Convert the weights into the compute library format
50  arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weightsPermuted, descriptor.m_DataLayout);
51  aclWeightsInfo.set_are_values_constant(weights.IsConstant());
52 
53  arm_compute::TensorInfo aclBiasesInfo;
54  arm_compute::TensorInfo* optionalAclBiasesInfo = nullptr;
55  if (descriptor.m_BiasEnabled)
56  {
57  if (!biases.has_value())
58  {
59  return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
60  "ArmNN ClDepthwiseConv2dWorkload has empty bias value."};
61  }
62  aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
63  aclBiasesInfo.set_are_values_constant(biases.value().IsConstant());
64  optionalAclBiasesInfo = &aclBiasesInfo;
65  }
66 
67  const arm_compute::PadStrideInfo aclPadStrideInfo = BuildArmComputePadStrideInfo(descriptor);
68  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
69  descriptor.m_DilationX,
70  descriptor.m_DilationY);
71 
72  const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
73  activationDescriptor);
74 
75  return arm_compute::CLDepthwiseConvolutionLayer::validate(&aclInputInfo,
76  &aclWeightsInfo,
77  optionalAclBiasesInfo,
78  &aclOutputInfo,
79  aclPadStrideInfo,
80  aclDepthMultiplier,
81  activationInfo,
82  aclDilationInfo);
83 
84 }
85 
87  const DepthwiseConvolution2dQueueDescriptor& descriptor,
88  const WorkloadInfo& info,
89  const arm_compute::CLCompileContext& clCompileContext)
91 {
92  m_Data.ValidateInputsOutputs("ClDepthwiseConv2dWorkload", descriptor.m_Parameters.GetNumInputs(), 1);
93 
94  arm_compute::ICLTensor& input = PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
95  arm_compute::ICLTensor& output = PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
96  arm_compute::ICLTensor& weights = PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
97  weights.info()->set_are_values_constant(info.m_InputTensorInfos[1].IsConstant());
98  arm_compute::ITensorInfo* weightsInfo = weights.info();
99  arm_compute::ITensorInfo* inputInfo = input.info();
100  auto weightsShape = weightsInfo->tensor_shape();
101  auto inputShape = inputInfo->tensor_shape();
102 
103  // The PermuteDepthwiseConv2dWeights backend optimization has been performed,
104  // converting weights to have the same data layout as input.
105  unsigned int depthMultiplier =
106  ComputeDepthwiseConv2dDepthMultiplier(m_Data.m_Parameters.m_DataLayout, weightsShape, inputShape);
107 
108  arm_compute::ICLTensor* bias = nullptr;
110  {
111  bias = &PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor();
112  bias->info()->set_are_values_constant(info.m_InputTensorInfos[2].IsConstant());
113  }
114 
115  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
118 
119  arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
120  input.info()->set_data_layout(aclDataLayout);
121  weights.info()->set_data_layout(aclDataLayout);
122  output.info()->set_data_layout(aclDataLayout);
123 
124  arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
125 
126  const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
127 
128  m_DepthwiseConvolutionLayer = std::make_unique<arm_compute::CLDepthwiseConvolutionLayer>();
129 
130  {
131  ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClDepthwiseConvolutionWorkload_configure");
132  static_cast<arm_compute::CLDepthwiseConvolutionLayer*>(m_DepthwiseConvolutionLayer.get())->configure(
133  clCompileContext,
134  &input,
135  &weights,
136  bias,
137  &output,
138  padStrideInfo,
139  depthMultiplier,
140  activationInfo,
141  aclDilationInfo);
142  }
143 
144  // Add details for profiling output
145  WorkloadInfo detailsInfo;
146 
147  detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
148  detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
149 
150  // Report Profiling Details
151  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClDepthwiseConvolutionWorkload_Construct",
152  descriptor.m_Parameters,
153  detailsInfo,
154  GetGuid());
155 }
156 
158 {
159  ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClDepthwiseConvolutionWorkload_Execute");
160 
162 }
163 
164 } // namespace armnn
armnn::ActivationDescriptor
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:36
armnn::Convert1HWOTensorInfoToAcl
std::tuple< TensorInfo, unsigned int > Convert1HWOTensorInfoToAcl(const TensorInfo &weightInfo, const TensorInfo &inputInfo, const DataLayout dataLayout)
Weights for depthwise have a datalayout of [1,H,W,O] = [1,H,W,I*M] This function coverts a TensorInfo...
Definition: WorkloadUtils.cpp:177
ClDepthwiseConvolutionWorkload.hpp
armnn::Optional
Definition: Optional.hpp:270
armnn::ClDepthwiseConvolutionWorkload::ClDepthwiseConvolutionWorkload
ClDepthwiseConvolutionWorkload(const DepthwiseConvolution2dQueueDescriptor &descriptor, const WorkloadInfo &info, const arm_compute::CLCompileContext &clCompileContext)
Definition: ClDepthwiseConvolutionWorkload.cpp:86
armnn::DataLayout
DataLayout
Definition: Types.hpp:62
WorkloadData.hpp
armnn::DepthwiseConvolution2dDescriptor::m_BiasEnabled
bool m_BiasEnabled
Enable/disable bias.
Definition: Descriptors.hpp:708
armnn::ClDepthwiseConvolutionWorkload::Execute
void Execute() const override
Definition: ClDepthwiseConvolutionWorkload.cpp:157
WorkloadUtils.hpp
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::DepthwiseConvolution2dDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:710
armnn::TensorInfo
Definition: Tensor.hpp:152
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnn::ClBaseWorkload
Definition: ClBaseWorkload.hpp:13
ResolveType.hpp
armnn::TensorInfo::IsConstant
bool IsConstant() const
Definition: Tensor.cpp:513
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::DepthwiseConvolution2dDescriptor::m_DilationY
uint32_t m_DilationY
Dilation factor value for height dimension.
Definition: Descriptors.hpp:706
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
ClWorkloadUtils.hpp
ArmComputeUtils.hpp
armnn::ConvertActivationDescriptorToAclActivationLayerInfo
arm_compute::ActivationLayerInfo ConvertActivationDescriptorToAclActivationLayerInfo(const ActivationDescriptor &actDesc)
Definition: ArmComputeUtils.hpp:87
armnn::BoostLogSeverityMapping::info
@ info
armnn::ClDepthwiseConvolutionWorkload::m_DepthwiseConvolutionLayer
std::unique_ptr< arm_compute::IFunction > m_DepthwiseConvolutionLayer
Definition: ClDepthwiseConvolutionWorkload.hpp:36
armnn::QueueDescriptor::m_Outputs
std::vector< ITensorHandle * > m_Outputs
Definition: WorkloadData.hpp:27
armnn::DepthwiseConvolution2dQueueDescriptor
Depthwise Convolution 2D layer workload data.
Definition: WorkloadData.hpp:234
ARMNN_REPORT_PROFILING_WORKLOAD_DESC
#define ARMNN_REPORT_PROFILING_WORKLOAD_DESC(name, desc, infos, guid)
Definition: Profiling.hpp:227
armnn::BaseWorkload< DepthwiseConvolution2dQueueDescriptor >::GetGuid
arm::pipe::ProfilingGuid GetGuid() const final
Definition: Workload.hpp:67
TensorHandle.hpp
armnn::Status
Status
Definition: Types.hpp:42
ClTensorHandle.hpp
armnn::BaseWorkload< DepthwiseConvolution2dQueueDescriptor >::m_Data
DepthwiseConvolution2dQueueDescriptor m_Data
Definition: Workload.hpp:89
armnn::RunClFunction
void RunClFunction(arm_compute::IFunction &function, const CheckLocation &location)
Definition: ClWorkloadUtils.hpp:167
armnn::WorkloadInfo::m_InputTensorInfos
std::vector< TensorInfo > m_InputTensorInfos
Definition: WorkloadInfo.hpp:18
Exceptions.hpp
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
ArmComputeTensorUtils.hpp
armnn::DepthwiseConvolution2dDescriptor
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
Definition: Descriptors.hpp:659
armnn::DepthwiseConvolution2dDescriptor::m_DilationX
uint32_t m_DilationX
Dilation factor value for width dimension.
Definition: Descriptors.hpp:704
armnn::ClDepthwiseConvolutionWorkloadValidate
arm_compute::Status ClDepthwiseConvolutionWorkloadValidate(const TensorInfo &input, const TensorInfo &output, const DepthwiseConvolution2dDescriptor &descriptor, const TensorInfo &weights, const Optional< TensorInfo > &biases, const ActivationDescriptor *activationDescriptor)
Definition: ClDepthwiseConvolutionWorkload.cpp:26
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::DepthwiseConvolution2dDescriptor::GetNumInputs
uint32_t GetNumInputs() const
Get the number of views/inputs.
Definition: Descriptors.cpp:480