ArmNN
 21.11
ClDepthwiseConvolutionWorkload.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 <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's weight format is usually [ M, I, H, W ] but for depthwise its [ 1, H, W, I*M]
37  // Permute to [ 1, I * M, H, W ] (if NCHW) as required by the compute library
38  unsigned int aclDepthMultiplier;
39  TensorInfo weightsPermuted;
40  std::tie(weightsPermuted, aclDepthMultiplier) = Convert1HWOTensorInfoToAcl(weights, input,descriptor.m_DataLayout);
41 
42  // Convert the weights into the compute library format
43  const arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weightsPermuted, descriptor.m_DataLayout);
44 
45  arm_compute::TensorInfo aclBiasesInfo;
46  arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr;
47 
48  if (descriptor.m_BiasEnabled)
49  {
50  ARMNN_ASSERT(biases.has_value());
51 
52  aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
53  optionalAclBiasesInfo = &aclBiasesInfo;
54  }
55 
56  const arm_compute::PadStrideInfo aclPadStrideInfo = BuildArmComputePadStrideInfo(descriptor);
57  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
58  descriptor.m_DilationX,
59  descriptor.m_DilationY);
60 
61  const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
62  activationDescriptor);
63 
64  return arm_compute::CLDepthwiseConvolutionLayer::validate(&aclInputInfo,
65  &aclWeightsInfo,
66  optionalAclBiasesInfo,
67  &aclOutputInfo,
68  aclPadStrideInfo,
69  aclDepthMultiplier,
70  activationInfo,
71  aclDilationInfo);
72 
73 }
74 
76  const DepthwiseConvolution2dQueueDescriptor& descriptor,
77  const WorkloadInfo& info,
78  const arm_compute::CLCompileContext& clCompileContext)
80 {
81  // Add details for profiling output
82  WorkloadInfo detailsInfo;
83 
84  detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
85  detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
87  if (descriptor.m_Parameters.m_BiasEnabled)
88  {
90  }
91 
92  // Report Profiling Details
93  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClDepthwiseConvolutionWorkload_Construct",
94  descriptor.m_Parameters,
95  detailsInfo,
96  this->GetGuid());
97 
98  // ArmNN's weight format is usually [ M, I, H, W ] but for depthwise its [ 1, H, W, I*M]
99  // Permute to [ 1, I * M, H, W ] (if NCHW), as required by the compute library
100  ConstTensor weightPermuted;
101  unsigned int depthMultiplier;
102  std::unique_ptr<unsigned char[]> permuteBuffer(new unsigned char[m_Data.m_Weight->GetTensorInfo().GetNumBytes()]);
103  std::tie(weightPermuted, depthMultiplier) = Convert1HWOTensorToAcl(m_Data.m_Weight,
104  info.m_InputTensorInfos[0],
106  permuteBuffer.get());
107 
108  // Convert the weights into the compute library format
109  m_KernelTensor = std::make_unique<arm_compute::CLTensor>();
110  BuildArmComputeTensor(*m_KernelTensor, weightPermuted.GetInfo(), m_Data.m_Parameters.m_DataLayout);
111 
113  {
114  m_BiasTensor = std::make_unique<arm_compute::CLTensor>();
116  }
117 
118  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
121 
122 
123  std::string name = std::string("ClDepthwiseConvolutionWorkload");
124  m_Data.ValidateInputsOutputs(name, 1, 1);
125 
126  arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
127  arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
128 
129  arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
130  input.info()->set_data_layout(aclDataLayout);
131  output.info()->set_data_layout(aclDataLayout);
132 
133  arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
134 
135  const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
136 
137  m_DepthwiseConvolutionLayer = std::make_unique<arm_compute::CLDepthwiseConvolutionLayer>();
138 
139  {
140  ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClDepthwiseConvolutionWorkload_configure");
141  static_cast<arm_compute::CLDepthwiseConvolutionLayer*>(m_DepthwiseConvolutionLayer.get())->configure(
142  clCompileContext,
143  &input,
144  m_KernelTensor.get(),
145  m_BiasTensor.get(),
146  &output,
147  padStrideInfo,
148  depthMultiplier,
149  activationInfo,
150  aclDilationInfo);
151  }
153 
154  ScopedTensorHandle weightsPermutedHandle(weightPermuted);
155  InitializeArmComputeClTensorData(*m_KernelTensor, &weightsPermutedHandle);
156 
157  if (m_BiasTensor)
158  {
160  }
161 
162  m_DepthwiseConvolutionLayer->prepare();
164 }
165 
167 {
168  FreeTensorIfUnused(m_KernelTensor);
169  FreeTensorIfUnused(m_BiasTensor);
170 }
171 
173 {
174  ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClDepthwiseConvolutionWorkload_Execute", this->GetGuid());
176 
178 }
179 
180 } // namespace armnn
#define ARMNN_SCOPED_PROFILING_EVENT_CL_GUID(name, guid)
bool m_BiasEnabled
Enable/disable bias.
DataLayout
Definition: Types.hpp:49
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
void RunClFunction(arm_compute::IFunction &function, const CheckLocation &location)
unsigned int GetNumBytes() const
Definition: Tensor.cpp:429
std::unique_ptr< arm_compute::IFunction > m_DepthwiseConvolutionLayer
arm_compute::ActivationLayerInfo ConvertAdditionalInfoToAclActivationLayerInfo(const QueueDescriptor &queueDescriptor)
void ValidateInputsOutputs(const std::string &descName, unsigned int numExpectedIn, unsigned int numExpectedOut) const
std::unique_ptr< arm_compute::CLTensor > m_BiasTensor
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
std::tuple< ConstTensor, unsigned int > Convert1HWOTensorToAcl(const ConstTensorHandle *weightTensor, const TensorInfo &inputInfo, const DataLayout dataLayout, void *permuteBuffer)
Weights for depthwise have a datalayout of [1,H,W,O] = [1,H,W,I*M] This function coverts a ConstCpuTe...
uint32_t m_DilationX
Dilation factor value for width dimension.
DepthwiseConvolution2dQueueDescriptor m_Data
Definition: Workload.hpp:58
bool has_value() const noexcept
Definition: Optional.hpp:53
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:327
Status
enumeration
Definition: Types.hpp:29
#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:25
#define CHECK_LOCATION()
Definition: Exceptions.hpp:209
profiling::ProfilingGuid GetGuid() const final
Definition: Workload.hpp:55
Optional< TensorInfo > m_BiasTensorInfo
std::unique_ptr< arm_compute::CLTensor > m_KernelTensor
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
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)