ArmNN
 20.02
NeonDepthwiseConvolutionWorkload.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 "NeonWorkloadUtils.hpp"
9 
11 
13 
15 
18 
19 #include <arm_compute/runtime/NEON/functions/NEDepthwiseConvolutionLayer.h>
20 
21 using namespace armnnUtils;
22 
23 namespace armnn
24 {
25 
26 using namespace armcomputetensorutils;
27 
29  const TensorInfo& output,
30  const DepthwiseConvolution2dDescriptor& descriptor,
31  const TensorInfo& weights,
32  const Optional<TensorInfo>& biases)
33 {
34  const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
35  const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
36 
37  // ArmNN's weight format is [ M, I, H, W ]
38  const unsigned int aclDepthMultiplier = weights.GetShape()[0];
39 
40  // Convert the weight format from ArmNN's [ M, I, H, W ] (does NOT depend on the data layout) to either
41  // [ 1, H, W, I * M ] (if NHWC) or [ 1, I * M, H, W ] (if NCHW), as required by the compute library
42  TensorInfo weightsPermuted = ConvertWeightTensorInfoFromArmnnToAcl(weights, descriptor.m_DataLayout);
43 
44  // Convert the weights into the compute library format
45  const arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weightsPermuted, descriptor.m_DataLayout);
46 
47  arm_compute::TensorInfo aclBiasesInfo;
48  arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr;
49 
50  if (descriptor.m_BiasEnabled)
51  {
52  BOOST_ASSERT(biases.has_value());
53 
54  aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
55  optionalAclBiasesInfo = &aclBiasesInfo;
56  }
57 
58  arm_compute::PadStrideInfo aclPadStrideInfo = BuildArmComputePadStrideInfo(descriptor);
59  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
60  descriptor.m_DilationX,descriptor.m_DilationY);
61 
62  return arm_compute::NEDepthwiseConvolutionLayer::validate(&aclInputInfo,
63  &aclWeightsInfo,
64  optionalAclBiasesInfo,
65  &aclOutputInfo,
66  aclPadStrideInfo,
67  aclDepthMultiplier,
68  arm_compute::ActivationLayerInfo(),
69  aclDilationInfo);
70 }
71 
73  const DepthwiseConvolution2dQueueDescriptor& descriptor,
74  const WorkloadInfo& info)
76 {
77  // ArmNN's weight format is [ M, I, H, W ]
78  auto& weightInfo = m_Data.m_Weight->GetTensorInfo();
79 
80  // Allocate a buffer for the swizzling of the weight tensor
81  std::unique_ptr<unsigned char[]> permuteBuffer(new unsigned char[m_Data.m_Weight->GetTensorInfo().GetNumBytes()]);
82 
83  // Convert the weight format from ArmNN's [ M, I, H, W ] (does NOT depend on the data layout) to either
84  // [ 1, H, W, I * M ] (if NHWC) or [ 1, I * M, H, W ] (if NCHW), as required by the compute library
87  permuteBuffer.get());
88 
89  // Convert the weights into the compute library format
90  m_KernelTensor = std::make_unique<arm_compute::Tensor>();
91  BuildArmComputeTensor(*m_KernelTensor, weightPermuted.GetInfo(), m_Data.m_Parameters.m_DataLayout);
92 
94  {
95  m_BiasTensor = std::make_unique<arm_compute::Tensor>();
96  BuildArmComputeTensor(*m_BiasTensor, m_Data.m_Bias->GetTensorInfo(), m_Data.m_Parameters.m_DataLayout);
97  }
98 
99  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
101 
102  m_Data.ValidateInputsOutputs("NeonDepthwiseConvolutionWorkload", 1, 1);
103 
104  IAclTensorHandle* inputTensorHandle = static_cast<IAclTensorHandle*>(m_Data.m_Inputs[0]);
105  IAclTensorHandle* outputTensorHandle = static_cast<IAclTensorHandle*>(m_Data.m_Outputs[0]);
106 
107  arm_compute::ITensor& input = inputTensorHandle->GetTensor();
108  arm_compute::ITensor& output = outputTensorHandle->GetTensor();
109 
110  arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
111  input.info()->set_data_layout(aclDataLayout);
112  output.info()->set_data_layout(aclDataLayout);
113 
114  // Get the depth multiplier
115  const unsigned int depthMultiplier = weightInfo.GetShape()[0];
116 
117  arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
118 
119  m_pDepthwiseConvolutionLayer = std::make_unique<arm_compute::NEDepthwiseConvolutionLayer>();
120  static_cast<arm_compute::NEDepthwiseConvolutionLayer*>(
121  m_pDepthwiseConvolutionLayer.get())->configure(&input,
122  m_KernelTensor.get(),
123  m_BiasTensor.get(),
124  &output,
125  padStrideInfo,
126  depthMultiplier,
127  arm_compute::ActivationLayerInfo(),
128  aclDilationInfo);
129 
130  BOOST_ASSERT(m_pDepthwiseConvolutionLayer);
131 
132  ScopedCpuTensorHandle weightsPermutedHandle(weightPermuted);
133  InitializeArmComputeTensorData(*m_KernelTensor, &weightsPermutedHandle);
134 
136  {
138  }
139 
140  m_pDepthwiseConvolutionLayer->prepare();
141  FreeUnusedTensors();
142 }
143 
145 {
146  ARMNN_SCOPED_PROFILING_EVENT_NEON("NeonDepthwiseConvolutionWorkload_Execute");
147  BOOST_ASSERT(m_pDepthwiseConvolutionLayer);
148 
149  m_pDepthwiseConvolutionLayer->run();
150 }
151 
152 void NeonDepthwiseConvolutionWorkload::FreeUnusedTensors()
153 {
154  FreeTensorIfUnused(m_KernelTensor);
155  FreeTensorIfUnused(m_BiasTensor);
156 }
157 
158 } //namespace armnn
virtual arm_compute::ITensor & GetTensor()=0
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)
TensorInfo ConvertWeightTensorInfoFromArmnnToAcl(const TensorInfo &weightInfo, DataLayout dataLayout)
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
const DepthwiseConvolution2dQueueDescriptor m_Data
Definition: Workload.hpp:46
#define ARMNN_SCOPED_PROFILING_EVENT_NEON(name)
unsigned int GetNumBytes() const
Definition: Tensor.cpp:213
void ValidateInputsOutputs(const std::string &descName, unsigned int numExpectedIn, unsigned int numExpectedOut) const
Copyright (c) 2020 ARM Limited.
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
void InitializeArmComputeTensorData(arm_compute::Tensor &tensor, const ConstCpuTensorHandle *handle)
arm_compute::Status NeonDepthwiseConvolutionWorkloadValidate(const TensorInfo &input, const TensorInfo &output, const DepthwiseConvolution2dDescriptor &descriptor, const TensorInfo &weights, const Optional< TensorInfo > &biases)
std::vector< ITensorHandle * > m_Outputs
Contains information about inputs and outputs to a layer.
std::vector< ITensorHandle * > m_Inputs
NeonDepthwiseConvolutionWorkload(const DepthwiseConvolution2dQueueDescriptor &descriptor, const WorkloadInfo &info)
const TensorInfo & GetTensorInfo() const
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.