ArmNN
 22.02
NeonDepthwiseConvolutionWorkload.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 "NeonWorkloadUtils.hpp"
9 
11 
14 
16 
19 
20 #include <arm_compute/runtime/NEON/functions/NEDepthwiseConvolutionLayer.h>
21 
22 using namespace armnnUtils;
23 
24 namespace armnn
25 {
26 
27 using namespace armcomputetensorutils;
28 
30  const TensorInfo& output,
31  const DepthwiseConvolution2dDescriptor& descriptor,
32  const TensorInfo& weights,
33  const Optional<TensorInfo>& biases,
34  const ActivationDescriptor* activationDescriptor)
35 {
36  const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
37  const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
38 
39  // ArmNN's weight format is usually [ M, I, H, W ] but for depthwise its [ 1, H, W, I*M]
40  // Permute to [ 1, I * M, H, W ] (if NCHW), as required by the compute library
41  unsigned int aclDepthMultiplier;
42  TensorInfo weightsPermuted;
43  std::tie(weightsPermuted, aclDepthMultiplier) = Convert1HWOTensorInfoToAcl(weights, input, descriptor.m_DataLayout);
44 
45  // Convert the weights into the compute library format
46  const arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weightsPermuted, descriptor.m_DataLayout);
47 
48  arm_compute::TensorInfo aclBiasesInfo;
49  arm_compute::TensorInfo* optionalAclBiasesInfo = nullptr;
50 
51  if (descriptor.m_BiasEnabled)
52  {
53  ARMNN_ASSERT(biases.has_value());
54 
55  aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
56  optionalAclBiasesInfo = &aclBiasesInfo;
57  }
58 
59  arm_compute::PadStrideInfo aclPadStrideInfo = BuildArmComputePadStrideInfo(descriptor);
60  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
61  descriptor.m_DilationX, descriptor.m_DilationY);
62 
63  const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
64  activationDescriptor);
65 
66  return arm_compute::NEDepthwiseConvolutionLayer::validate(&aclInputInfo,
67  &aclWeightsInfo,
68  optionalAclBiasesInfo,
69  &aclOutputInfo,
70  aclPadStrideInfo,
71  aclDepthMultiplier,
72  activationInfo,
73  aclDilationInfo);
74 }
75 
77  const DepthwiseConvolution2dQueueDescriptor& descriptor,
78  const WorkloadInfo& info)
80 {
81  // ArmNN's weight format for depthwise is [ 1, H, W, I*M ]
82  auto& weightInfo = m_Data.m_Weight->GetTensorInfo();
83 
84  ConstTensor weightsPermuted;
85  unsigned int depthMultiplier;
86  std::unique_ptr<unsigned char[]> permuteBuffer(new unsigned char[weightInfo.GetNumBytes()]);
87  std::tie(weightsPermuted, depthMultiplier) = Convert1HWOTensorToAcl(m_Data.m_Weight,
88  info.m_InputTensorInfos[0],
90  permuteBuffer.get());
91 
92  // Convert the weights into the compute library format
93  m_KernelTensor = std::make_unique<arm_compute::Tensor>();
94  BuildArmComputeTensor(*m_KernelTensor, weightsPermuted.GetInfo(), m_Data.m_Parameters.m_DataLayout);
95 
97  {
98  m_BiasTensor = std::make_unique<arm_compute::Tensor>();
99  BuildArmComputeTensor(*m_BiasTensor, m_Data.m_Bias->GetTensorInfo(), m_Data.m_Parameters.m_DataLayout);
100  }
101 
102  const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
104 
105  m_Data.ValidateInputsOutputs("NeonDepthwiseConvolutionWorkload", 1, 1);
106 
107  IAclTensorHandle* inputTensorHandle = static_cast<IAclTensorHandle*>(m_Data.m_Inputs[0]);
108  IAclTensorHandle* outputTensorHandle = static_cast<IAclTensorHandle*>(m_Data.m_Outputs[0]);
109 
110  arm_compute::ITensor& input = inputTensorHandle->GetTensor();
111  arm_compute::ITensor& output = outputTensorHandle->GetTensor();
112 
113  arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
114  input.info()->set_data_layout(aclDataLayout);
115  output.info()->set_data_layout(aclDataLayout);
116 
117  arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
118 
119  const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
120 
121  m_pDepthwiseConvolutionLayer = std::make_unique<arm_compute::NEDepthwiseConvolutionLayer>();
122  static_cast<arm_compute::NEDepthwiseConvolutionLayer*>(
123  m_pDepthwiseConvolutionLayer.get())->configure(&input,
124  m_KernelTensor.get(),
125  m_BiasTensor.get(),
126  &output,
127  padStrideInfo,
128  depthMultiplier,
129  activationInfo,
130  aclDilationInfo);
131 
132  // Add details for profiling output
133  WorkloadInfo detailsInfo;
134 
135  detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
136  detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
137  detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Weight->GetTensorInfo());
138  if (descriptor.m_Parameters.m_BiasEnabled)
139  {
140  detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Bias->GetTensorInfo());
141  }
142 
143  // Report Profiling Details
144  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonDepthwiseConvolution2dWorkload_Construct",
145  descriptor.m_Parameters,
146  detailsInfo,
147  this->GetGuid());
148 
149  ARMNN_ASSERT(m_pDepthwiseConvolutionLayer);
150 
151  ScopedTensorHandle weightsPermutedHandle(weightsPermuted);
152  InitializeArmComputeTensorData(*m_KernelTensor, &weightsPermutedHandle);
153 
155  {
157  }
158 
159  m_pDepthwiseConvolutionLayer->prepare();
160  FreeUnusedTensors();
161 }
162 
164 {
165  ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID("NeonDepthwiseConvolutionWorkload_Execute", this->GetGuid());
166  ARMNN_ASSERT(m_pDepthwiseConvolutionLayer);
167 
168  m_pDepthwiseConvolutionLayer->run();
169 }
170 
171 void NeonDepthwiseConvolutionWorkload::FreeUnusedTensors()
172 {
173  FreeTensorIfUnused(m_KernelTensor);
174  FreeTensorIfUnused(m_BiasTensor);
175 }
176 
177 } //namespace armnn
virtual arm_compute::ITensor & GetTensor()=0
bool m_BiasEnabled
Enable/disable bias.
DataLayout
Definition: Types.hpp:49
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
arm_compute::Status NeonDepthwiseConvolutionWorkloadValidate(const TensorInfo &input, const TensorInfo &output, const DepthwiseConvolution2dDescriptor &descriptor, const TensorInfo &weights, const Optional< TensorInfo > &biases, const ActivationDescriptor *activationDescriptor)
arm_compute::ActivationLayerInfo ConvertAdditionalInfoToAclActivationLayerInfo(const QueueDescriptor &queueDescriptor)
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.
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:77
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:36
const TensorInfo & GetInfo() const
Definition: Tensor.hpp:295
profiling::ProfilingGuid GetGuid() const final
Definition: Workload.hpp:55
std::vector< ITensorHandle * > m_Outputs
#define ARMNN_REPORT_PROFILING_WORKLOAD_DESC(name, desc, infos, guid)
Definition: Profiling.hpp:227
void InitializeArmComputeTensorData(arm_compute::Tensor &tensor, const ConstTensorHandle *handle)
Contains information about TensorInfos of a layer.
std::vector< ITensorHandle * > m_Inputs
NeonDepthwiseConvolutionWorkload(const DepthwiseConvolution2dQueueDescriptor &descriptor, const WorkloadInfo &info)
#define ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID(name, guid)
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
Depthwise Convolution 2D layer workload data.
arm_compute::ActivationLayerInfo ConvertActivationDescriptorToAclActivationLayerInfo(const ActivationDescriptor &actDesc)