ArmNN
 20.02
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 
13 #include <cl/ClTensorHandle.hpp>
16 
17 #include <arm_compute/runtime/CL/functions/CLDepthwiseConvolutionLayer.h>
18 
19 namespace armnn
20 {
21 
22 using namespace armcomputetensorutils;
23 
25  const TensorInfo& output,
26  const DepthwiseConvolution2dDescriptor& descriptor,
27  const TensorInfo& weights,
28  const Optional<TensorInfo>& biases)
29 {
30  const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
31  const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
32 
33  // ArmNN's weight format is [ M, I, H, W ]
34  const unsigned int aclDepthMultiplier = weights.GetShape()[0];
35 
36  // Convert the weight format from ArmNN's [ M, I, H, W ] (does NOT depend on the data layout) to either
37  // [ 1, H, W, I * M ] (if NHWC) or [ 1, I * M, H, W ] (if NCHW), as required by the compute library
38  TensorInfo weightsPermuted = ConvertWeightTensorInfoFromArmnnToAcl(weights, descriptor.m_DataLayout);
39 
40  // Convert the weights into the compute library format
41  const arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weightsPermuted, descriptor.m_DataLayout);
42 
43  arm_compute::TensorInfo aclBiasesInfo;
44  arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr;
45 
46  if (descriptor.m_BiasEnabled)
47  {
48  BOOST_ASSERT(biases.has_value());
49 
50  aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
51  optionalAclBiasesInfo = &aclBiasesInfo;
52  }
53 
54  const arm_compute::PadStrideInfo aclPadStrideInfo = BuildArmComputePadStrideInfo(descriptor);
55  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
56  descriptor.m_DilationX,
57  descriptor.m_DilationY);
58 
59  return arm_compute::CLDepthwiseConvolutionLayer::validate(&aclInputInfo,
60  &aclWeightsInfo,
61  optionalAclBiasesInfo,
62  &aclOutputInfo,
63  aclPadStrideInfo,
64  aclDepthMultiplier,
65  arm_compute::ActivationLayerInfo(),
66  aclDilationInfo);
67 
68 }
69 
71  const DepthwiseConvolution2dQueueDescriptor& descriptor,
72  const WorkloadInfo& info)
74 {
75  // Allocate a buffer for the swizzling of the weight tensor
76  std::unique_ptr<unsigned char[]> permuteBuffer(new unsigned char[m_Data.m_Weight->GetTensorInfo().GetNumBytes()]);
77 
78  // Convert the weight format from ArmNN's [ M, I, H, W ] (does NOT depend on the data layout) to either
79  // [ 1, H, W, I * M ] (if NHWC) or [ 1, I * M, H, W ] (if NCHW), as required by the compute library
82  permuteBuffer.get());
83 
84  // Convert the weights into the compute library format
85  m_KernelTensor = std::make_unique<arm_compute::CLTensor>();
86  BuildArmComputeTensor(*m_KernelTensor, weightPermuted.GetInfo(), m_Data.m_Parameters.m_DataLayout);
87 
89  {
90  m_BiasTensor = std::make_unique<arm_compute::CLTensor>();
92  }
93 
94  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
97 
98 
99  std::string name = std::string("ClDepthwiseConvolutionWorkload");
100  m_Data.ValidateInputsOutputs(name, 1, 1);
101 
102  arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
103  arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
104 
105  arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
106  input.info()->set_data_layout(aclDataLayout);
107  output.info()->set_data_layout(aclDataLayout);
108 
109  // ArmNN's weight format is [ M, I, H, W ]
110  auto& weightInfo = m_Data.m_Weight->GetTensorInfo();
111 
112  // Get the depth multiplier
113  const unsigned int depthMultiplier = weightInfo.GetShape()[0];
114 
115  arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
116 
117  m_DepthwiseConvolutionLayer = std::make_unique<arm_compute::CLDepthwiseConvolutionLayer>();
118  static_cast<arm_compute::CLDepthwiseConvolutionLayer*>(m_DepthwiseConvolutionLayer.get())->configure(
119  &input,
120  m_KernelTensor.get(),
121  m_BiasTensor.get(),
122  &output,
123  padStrideInfo,
124  depthMultiplier,
125  arm_compute::ActivationLayerInfo(),
126  aclDilationInfo);
127 
128  BOOST_ASSERT(m_DepthwiseConvolutionLayer);
129 
130  ScopedCpuTensorHandle weightsPermutedHandle(weightPermuted);
131  InitializeArmComputeClTensorData(*m_KernelTensor, &weightsPermutedHandle);
132 
133  if (m_BiasTensor)
134  {
136  }
137 
138  m_DepthwiseConvolutionLayer->prepare();
140 }
141 
143 {
144  FreeTensorIfUnused(m_KernelTensor);
145  FreeTensorIfUnused(m_BiasTensor);
146 }
147 
149 {
150  ARMNN_SCOPED_PROFILING_EVENT_CL("ClDepthwiseConvolutionWorkload_Execute");
151  BOOST_ASSERT(m_DepthwiseConvolutionLayer);
152 
154 }
155 
156 } // namespace armnn
bool m_BiasEnabled
Enable/disable bias.
DataLayout
Definition: Types.hpp:49
const TensorShape & GetShape() const
Definition: Tensor.hpp:88
armnn::ConstTensor ConvertWeightTensorFromArmnnToAcl(const ConstCpuTensorHandle *weightTensor, DataLayout dataLayout, void *permuteBuffer)
void InitializeArmComputeClTensorData(arm_compute::CLTensor &clTensor, const ConstCpuTensorHandle *handle)
TensorInfo ConvertWeightTensorInfoFromArmnnToAcl(const TensorInfo &weightInfo, DataLayout dataLayout)
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
#define ARMNN_SCOPED_PROFILING_EVENT_CL(name)
void RunClFunction(arm_compute::IFunction &function, const CheckLocation &location)
const DepthwiseConvolution2dQueueDescriptor m_Data
Definition: Workload.hpp:46
unsigned int GetNumBytes() const
Definition: Tensor.cpp:213
std::unique_ptr< arm_compute::IFunction > m_DepthwiseConvolutionLayer
void ValidateInputsOutputs(const std::string &descName, unsigned int numExpectedIn, unsigned int numExpectedOut) const
std::unique_ptr< arm_compute::CLTensor > m_BiasTensor
Copyright (c) 2020 ARM Limited.
ClDepthwiseConvolutionWorkload(const DepthwiseConvolution2dQueueDescriptor &descriptor, const WorkloadInfo &info)
uint32_t m_DilationY
Dilation factor value for height dimension.
uint32_t m_DilationX
Dilation factor value for width dimension.
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:199
Status
enumeration
Definition: Types.hpp:26
#define CHECK_LOCATION()
Definition: Exceptions.hpp:192
std::unique_ptr< arm_compute::CLTensor > m_KernelTensor
std::vector< ITensorHandle * > m_Outputs
arm_compute::Status ClDepthwiseConvolutionWorkloadValidate(const TensorInfo &input, const TensorInfo &output, const DepthwiseConvolution2dDescriptor &descriptor, const TensorInfo &weights, const Optional< TensorInfo > &biases)
Contains information about inputs and outputs to a layer.
std::vector< ITensorHandle * > m_Inputs
const TensorInfo & GetTensorInfo() const
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.