ArmNN
 22.05
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  // The CL implemented workload does support both const and non const
34  // weights. However, in the case of non const weights we'd have to call
35  // prepare or configure for each inference which we're not setup to do just yet.
36  if (!weights.IsConstant())
37  {
38  return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
39  "ArmNN ClDepthwiseConv2dWorkload does not support non constant weights."};
40  }
41 
42  const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
43  const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
44 
45  // ArmNN format for weights for depthwise is [1, H, W, C] independently of the input/output layout
46  //
47  // ACL format for weights for depthwise is:
48  // - [1, H, W, C] for [N, H, W, C] input/output layout (matches with ArmNN)
49  // - [1, C, H, W] for [N, C, H, W] input/output layout
50  //
51  // Therefore ArmNN weights have to be permuted when input/output layout is [N, C, H, W] to pass them to ACL.
52  // The PermuteDepthwiseConv2dWeights backend optimization takes care of this, but it has not been performed yet,
53  // so we do the permute here for the TensorInfo weights.
54  unsigned int aclDepthMultiplier;
55  TensorInfo weightsPermuted;
56  std::tie(weightsPermuted, aclDepthMultiplier) = Convert1HWOTensorInfoToAcl(weights, input,descriptor.m_DataLayout);
57 
58  // Convert the weights into the compute library format
59  arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weightsPermuted, descriptor.m_DataLayout);
60  aclWeightsInfo.set_are_values_constant(weights.IsConstant());
61 
62  arm_compute::TensorInfo aclBiasesInfo;
63  arm_compute::TensorInfo* optionalAclBiasesInfo = nullptr;
64  if (descriptor.m_BiasEnabled)
65  {
66  ARMNN_ASSERT(biases.has_value());
67  // Same for bias as weights. We don't currently support non const.
68  if (!biases.value().IsConstant())
69  {
70  return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
71  "ArmNN ClDepthwiseConv2dWorkload does not support non constant bias."};
72  }
73  aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
74  aclBiasesInfo.set_are_values_constant(biases.value().IsConstant());
75  optionalAclBiasesInfo = &aclBiasesInfo;
76  }
77 
78  const arm_compute::PadStrideInfo aclPadStrideInfo = BuildArmComputePadStrideInfo(descriptor);
79  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
80  descriptor.m_DilationX,
81  descriptor.m_DilationY);
82 
83  const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
84  activationDescriptor);
85 
86  return arm_compute::CLDepthwiseConvolutionLayer::validate(&aclInputInfo,
87  &aclWeightsInfo,
88  optionalAclBiasesInfo,
89  &aclOutputInfo,
90  aclPadStrideInfo,
91  aclDepthMultiplier,
92  activationInfo,
93  aclDilationInfo);
94 
95 }
96 
98  const DepthwiseConvolution2dQueueDescriptor& descriptor,
99  const WorkloadInfo& info,
100  const arm_compute::CLCompileContext& clCompileContext)
102 {
103  // Add details for profiling output
104  WorkloadInfo detailsInfo;
105 
106  detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
107  detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
109  if (descriptor.m_Parameters.m_BiasEnabled)
110  {
112  }
113 
114  // Report Profiling Details
115  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClDepthwiseConvolutionWorkload_Construct",
116  descriptor.m_Parameters,
117  detailsInfo,
118  GetGuid());
119 
120  m_Data.ValidateInputsOutputs("ClDepthwiseConv2dWorkload", descriptor.m_Parameters.GetNumInputs(), 1);
121 
122  arm_compute::ICLTensor& input = PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
123  arm_compute::ICLTensor& output = PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
124  arm_compute::ICLTensor& weights = PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
125  arm_compute::ITensorInfo* weightsInfo = weights.info();
126  arm_compute::ITensorInfo* inputInfo = input.info();
127  auto weightsShape = weightsInfo->tensor_shape();
128  auto inputShape = inputInfo->tensor_shape();
129 
130  // The PermuteDepthwiseConv2dWeights backend optimization has been performed,
131  // converting weights to have the same data layout as input.
132  unsigned int depthMultiplier =
133  ComputeDepthwiseConv2dDepthMultiplier(m_Data.m_Parameters.m_DataLayout, weightsShape, inputShape);
134 
135  arm_compute::ICLTensor* bias = nullptr;
137  {
138  bias = &PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor();
139  }
140 
141  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
144 
145  arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
146  input.info()->set_data_layout(aclDataLayout);
147  weights.info()->set_data_layout(aclDataLayout);
148  output.info()->set_data_layout(aclDataLayout);
149 
150  arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
151 
152  const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
153 
154  m_DepthwiseConvolutionLayer = std::make_unique<arm_compute::CLDepthwiseConvolutionLayer>();
155 
156  {
157  ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClDepthwiseConvolutionWorkload_configure");
158  static_cast<arm_compute::CLDepthwiseConvolutionLayer*>(m_DepthwiseConvolutionLayer.get())->configure(
159  clCompileContext,
160  &input,
161  &weights,
162  bias,
163  &output,
164  padStrideInfo,
165  depthMultiplier,
166  activationInfo,
167  aclDilationInfo);
168  }
170 }
171 
173 {
174  ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClDepthwiseConvolutionWorkload_Execute", GetGuid());
176 
178 }
179 
180 } // 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:59
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:81
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)