ArmNN
 22.08
ClDepthwiseConvolutionWorkload.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 <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  ARMNN_ASSERT(biases.has_value());
58  // Same for bias as weights. We don't currently support non const.
59  if (!biases.value().IsConstant())
60  {
61  return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
62  "ArmNN ClDepthwiseConv2dWorkload does not support non constant bias."};
63  }
64  aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
65  aclBiasesInfo.set_are_values_constant(biases.value().IsConstant());
66  optionalAclBiasesInfo = &aclBiasesInfo;
67  }
68 
69  const arm_compute::PadStrideInfo aclPadStrideInfo = BuildArmComputePadStrideInfo(descriptor);
70  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
71  descriptor.m_DilationX,
72  descriptor.m_DilationY);
73 
74  const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
75  activationDescriptor);
76 
77  return arm_compute::CLDepthwiseConvolutionLayer::validate(&aclInputInfo,
78  &aclWeightsInfo,
79  optionalAclBiasesInfo,
80  &aclOutputInfo,
81  aclPadStrideInfo,
82  aclDepthMultiplier,
83  activationInfo,
84  aclDilationInfo);
85 
86 }
87 
89  const DepthwiseConvolution2dQueueDescriptor& descriptor,
90  const WorkloadInfo& info,
91  const arm_compute::CLCompileContext& clCompileContext)
93 {
94  // Add details for profiling output
95  WorkloadInfo detailsInfo;
96 
97  detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
98  detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
100  if (descriptor.m_Parameters.m_BiasEnabled)
101  {
103  }
104 
105  // Report Profiling Details
106  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClDepthwiseConvolutionWorkload_Construct",
107  descriptor.m_Parameters,
108  detailsInfo,
109  GetGuid());
110 
111  m_Data.ValidateInputsOutputs("ClDepthwiseConv2dWorkload", descriptor.m_Parameters.GetNumInputs(), 1);
112 
113  arm_compute::ICLTensor& input = PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
114  arm_compute::ICLTensor& output = PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
115  arm_compute::ICLTensor& weights = PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
116  arm_compute::ITensorInfo* weightsInfo = weights.info();
117  arm_compute::ITensorInfo* inputInfo = input.info();
118  auto weightsShape = weightsInfo->tensor_shape();
119  auto inputShape = inputInfo->tensor_shape();
120 
121  // The PermuteDepthwiseConv2dWeights backend optimization has been performed,
122  // converting weights to have the same data layout as input.
123  unsigned int depthMultiplier =
124  ComputeDepthwiseConv2dDepthMultiplier(m_Data.m_Parameters.m_DataLayout, weightsShape, inputShape);
125 
126  arm_compute::ICLTensor* bias = nullptr;
128  {
129  bias = &PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor();
130  }
131 
132  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
135 
136  arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
137  input.info()->set_data_layout(aclDataLayout);
138  weights.info()->set_data_layout(aclDataLayout);
139  output.info()->set_data_layout(aclDataLayout);
140 
141  arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
142 
143  const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
144 
145  m_DepthwiseConvolutionLayer = std::make_unique<arm_compute::CLDepthwiseConvolutionLayer>();
146 
147  {
148  ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClDepthwiseConvolutionWorkload_configure");
149  static_cast<arm_compute::CLDepthwiseConvolutionLayer*>(m_DepthwiseConvolutionLayer.get())->configure(
150  clCompileContext,
151  &input,
152  &weights,
153  bias,
154  &output,
155  padStrideInfo,
156  depthMultiplier,
157  activationInfo,
158  aclDilationInfo);
159  }
161 }
162 
164 {
165  ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClDepthwiseConvolutionWorkload_Execute", GetGuid());
167 
169 }
170 
171 } // namespace armnn
bool IsConstant() const
Definition: Tensor.cpp:509
#define ARMNN_SCOPED_PROFILING_EVENT_CL_GUID(name, guid)
bool m_BiasEnabled
Enable/disable bias.
DataLayout
Definition: Types.hpp:62
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
void RunClFunction(arm_compute::IFunction &function, const CheckLocation &location)
uint32_t GetNumInputs() const
Get the number of views/inputs.
std::unique_ptr< arm_compute::IFunction > m_DepthwiseConvolutionLayer
arm_compute::ActivationLayerInfo ConvertAdditionalInfoToAclActivationLayerInfo(const QueueDescriptor &queueDescriptor)
arm::pipe::ProfilingGuid GetGuid() const final
Definition: Workload.hpp:61
void ValidateInputsOutputs(const std::string &descName, unsigned int numExpectedIn, unsigned int numExpectedOut) const
Copyright (c) 2021 ARM Limited and Contributors.
uint32_t m_DilationY
Dilation factor value for height dimension.
#define ARMNN_SCOPED_PROFILING_EVENT(backendId, name)
Definition: Profiling.hpp:220
const TensorInfo & GetTensorInfo() const
std::vector< TensorInfo > m_InputTensorInfos
uint32_t m_DilationX
Dilation factor value for width dimension.
DepthwiseConvolution2dQueueDescriptor m_Data
Definition: Workload.hpp:83
bool has_value() const noexcept
Definition: Optional.hpp:53
Status
enumeration
Definition: Types.hpp:42
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
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...
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
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
arm_compute::Status ClDepthwiseConvolutionWorkloadValidate(const TensorInfo &input, const TensorInfo &output, const DepthwiseConvolution2dDescriptor &descriptor, const TensorInfo &weights, const Optional< TensorInfo > &biases, const ActivationDescriptor *activationDescriptor)
ClDepthwiseConvolutionWorkload(const DepthwiseConvolution2dQueueDescriptor &descriptor, const WorkloadInfo &info, const arm_compute::CLCompileContext &clCompileContext)
Optional< TensorInfo > m_WeightsTensorInfo
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
Depthwise Convolution 2D layer workload data.
arm_compute::ActivationLayerInfo ConvertActivationDescriptorToAclActivationLayerInfo(const ActivationDescriptor &actDesc)