blob: 2bccfa8db2dafd80866a0a4b8eaf8fd44cd5102b [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
Teresa Charlin6fba2942023-04-18 12:48:46 +01002// Copyright © 2017,2022-2023 Arm Ltd and Contributors. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5
Nattapat Chaimanowong77140882018-10-17 11:12:19 +01006#include "NeonDepthwiseConvolutionWorkload.hpp"
7
Matthew Benthamd80a7122019-01-08 17:52:37 +00008#include "NeonWorkloadUtils.hpp"
9
Matteo Martincighe011d202019-11-28 11:35:47 +000010#include <armnnUtils/DataLayoutIndexed.hpp>
11
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +000012#include <aclCommon/ArmComputeTensorUtils.hpp>
Mike Kelly07810fc2020-11-12 10:58:48 +000013#include <aclCommon/ArmComputeUtils.hpp>
Matteo Martincighe011d202019-11-28 11:35:47 +000014
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +000015#include <neon/NeonLayerSupport.hpp>
Matteo Martincighe011d202019-11-28 11:35:47 +000016
Colm Donelan0c479742021-12-10 12:43:54 +000017#include <armnn/backends/TensorHandle.hpp>
Matteo Martincigh747ef822018-12-18 09:26:39 +000018#include <backendsCommon/WorkloadUtils.hpp>
telsoa014fcda012018-03-09 14:13:49 +000019
Matthew Benthamd80a7122019-01-08 17:52:37 +000020#include <arm_compute/runtime/NEON/functions/NEDepthwiseConvolutionLayer.h>
21
22using namespace armnnUtils;
23
telsoa014fcda012018-03-09 14:13:49 +000024namespace armnn
25{
Nattapat Chaimanowong77140882018-10-17 11:12:19 +010026
telsoa014fcda012018-03-09 14:13:49 +000027using namespace armcomputetensorutils;
28
Nattapat Chaimanowong77140882018-10-17 11:12:19 +010029arm_compute::Status NeonDepthwiseConvolutionWorkloadValidate(const TensorInfo& input,
Matteo Martincigh747ef822018-12-18 09:26:39 +000030 const TensorInfo& output,
31 const DepthwiseConvolution2dDescriptor& descriptor,
32 const TensorInfo& weights,
Mike Kelly07810fc2020-11-12 10:58:48 +000033 const Optional<TensorInfo>& biases,
34 const ActivationDescriptor* activationDescriptor)
Nattapat Chaimanowong77140882018-10-17 11:12:19 +010035{
Cathal Corbett4b19d222022-05-11 20:12:17 +010036 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
37 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
Matteo Martincigh747ef822018-12-18 09:26:39 +000038
Cathal Corbett4b19d222022-05-11 20:12:17 +010039 // ArmNN format for weights for depthwise is [1, H, W, C] independently of the input/output layout
40 //
41 // ACL format for weights for depthwise is:
42 // - [1, H, W, C] for [N, H, W, C] input/output layout (matches with ArmNN)
43 // - [1, C, H, W] for [N, C, H, W] input/output layout
44 //
45 // Therefore ArmNN weights have to be permuted when input/output layout is [N, C, H, W] to pass them to ACL.
46 // The PermuteDepthwiseConv2dWeights backend optimization takes care of this, but it has not been performed yet,
47 // so we do the permute here for the TensorInfo weights.
Jan Eilers53ef7952021-06-02 12:01:25 +010048 unsigned int aclDepthMultiplier;
49 TensorInfo weightsPermuted;
Keith Davis2d0679f2021-08-05 11:35:00 +010050 std::tie(weightsPermuted, aclDepthMultiplier) = Convert1HWOTensorInfoToAcl(weights, input, descriptor.m_DataLayout);
Matteo Martincigh747ef822018-12-18 09:26:39 +000051
52 // Convert the weights into the compute library format
Cathal Corbett4452baf2022-05-13 09:55:59 +010053 arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weightsPermuted, descriptor.m_DataLayout);
54 aclWeightsInfo.set_are_values_constant(weights.IsConstant());
Nattapat Chaimanowong77140882018-10-17 11:12:19 +010055
56 arm_compute::TensorInfo aclBiasesInfo;
Keith Davis2d0679f2021-08-05 11:35:00 +010057 arm_compute::TensorInfo* optionalAclBiasesInfo = nullptr;
Nattapat Chaimanowong77140882018-10-17 11:12:19 +010058 if (descriptor.m_BiasEnabled)
59 {
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010060 ARMNN_ASSERT(biases.has_value());
Nattapat Chaimanowong77140882018-10-17 11:12:19 +010061 aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
Cathal Corbett4452baf2022-05-13 09:55:59 +010062 aclBiasesInfo.set_are_values_constant(biases.value().IsConstant());
Nattapat Chaimanowong77140882018-10-17 11:12:19 +010063 optionalAclBiasesInfo = &aclBiasesInfo;
64 }
65
Pablo Tellof0bd6832019-04-26 17:58:13 +010066 arm_compute::PadStrideInfo aclPadStrideInfo = BuildArmComputePadStrideInfo(descriptor);
67 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
Keith Davis2d0679f2021-08-05 11:35:00 +010068 descriptor.m_DilationX, descriptor.m_DilationY);
Nattapat Chaimanowong77140882018-10-17 11:12:19 +010069
Mike Kelly07810fc2020-11-12 10:58:48 +000070 const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
Keith Davis2d0679f2021-08-05 11:35:00 +010071 activationDescriptor);
Mike Kelly07810fc2020-11-12 10:58:48 +000072
Nattapat Chaimanowong77140882018-10-17 11:12:19 +010073 return arm_compute::NEDepthwiseConvolutionLayer::validate(&aclInputInfo,
74 &aclWeightsInfo,
75 optionalAclBiasesInfo,
76 &aclOutputInfo,
77 aclPadStrideInfo,
Pablo Tellof0bd6832019-04-26 17:58:13 +010078 aclDepthMultiplier,
Mike Kelly07810fc2020-11-12 10:58:48 +000079 activationInfo,
Pablo Tellof0bd6832019-04-26 17:58:13 +010080 aclDilationInfo);
Nattapat Chaimanowong77140882018-10-17 11:12:19 +010081}
82
83NeonDepthwiseConvolutionWorkload::NeonDepthwiseConvolutionWorkload(
telsoa014fcda012018-03-09 14:13:49 +000084 const DepthwiseConvolution2dQueueDescriptor& descriptor,
85 const WorkloadInfo& info)
Teresa Charlin588cbdf2022-01-19 15:55:37 +000086 : NeonBaseWorkload<DepthwiseConvolution2dQueueDescriptor>(descriptor, info)
telsoa014fcda012018-03-09 14:13:49 +000087{
Cathal Corbett4b19d222022-05-11 20:12:17 +010088 arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
89 arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
90 arm_compute::ITensor& weights = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
91 arm_compute::ITensor* biasesPtr = nullptr;
Teresa Charlin6fba2942023-04-18 12:48:46 +010092 weights.info()->set_are_values_constant(info.m_InputTensorInfos[1].IsConstant());
telsoa014fcda012018-03-09 14:13:49 +000093 if (m_Data.m_Parameters.m_BiasEnabled)
94 {
Cathal Corbett4b19d222022-05-11 20:12:17 +010095 biasesPtr = &PolymorphicDowncast<IAclTensorHandle *>(m_Data.m_Inputs[2])->GetTensor();
Teresa Charlin6fba2942023-04-18 12:48:46 +010096 biasesPtr->info()->set_are_values_constant(info.m_InputTensorInfos[2].IsConstant());
telsoa014fcda012018-03-09 14:13:49 +000097 }
98
Cathal Corbett4b19d222022-05-11 20:12:17 +010099 arm_compute::ITensorInfo* weightsInfo = weights.info();
100 arm_compute::ITensorInfo* inputInfo = input.info();
101 auto weightsShape = weightsInfo->tensor_shape();
102 auto inputShape = inputInfo->tensor_shape();
103
104 // The PermuteDepthwiseConv2dWeights backend optimization has been performed,
105 // converting weights to have the same data layout as input.
106 unsigned int depthMultiplier =
107 ComputeDepthwiseConv2dDepthMultiplier(m_Data.m_Parameters.m_DataLayout, weightsShape, inputShape);
108
Pablo Tellof0bd6832019-04-26 17:58:13 +0100109 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
Keith Davis2d0679f2021-08-05 11:35:00 +0100110 m_Data.m_Parameters.m_DilationX, m_Data.m_Parameters.m_DilationY);
Pablo Tellof0bd6832019-04-26 17:58:13 +0100111
Cathal Corbett4b19d222022-05-11 20:12:17 +0100112 uint32_t numInputs = m_Data.m_Parameters.m_BiasEnabled ? 3: 2;
113 m_Data.ValidateInputsOutputs("NeonDepthwiseConvolutionWorkload", numInputs, 1);
telsoa014fcda012018-03-09 14:13:49 +0000114
Nikhil Rajcec6b652018-10-12 13:51:57 +0100115 arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
116 input.info()->set_data_layout(aclDataLayout);
Cathal Corbett4b19d222022-05-11 20:12:17 +0100117 weights.info()->set_data_layout(aclDataLayout);
Nikhil Rajcec6b652018-10-12 13:51:57 +0100118 output.info()->set_data_layout(aclDataLayout);
119
Aron Virginas-Tar6f3785d2019-07-22 15:30:22 +0100120 arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
121
Mike Kelly07810fc2020-11-12 10:58:48 +0000122 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
123
Aron Virginas-Tarf4c502f2019-11-14 16:21:38 +0000124 m_pDepthwiseConvolutionLayer = std::make_unique<arm_compute::NEDepthwiseConvolutionLayer>();
125 static_cast<arm_compute::NEDepthwiseConvolutionLayer*>(
126 m_pDepthwiseConvolutionLayer.get())->configure(&input,
Cathal Corbett4b19d222022-05-11 20:12:17 +0100127 &weights,
128 biasesPtr,
Mike Kelly07810fc2020-11-12 10:58:48 +0000129 &output,
130 padStrideInfo,
131 depthMultiplier,
132 activationInfo,
133 aclDilationInfo);
telsoa014fcda012018-03-09 14:13:49 +0000134
Keith Davis2d0679f2021-08-05 11:35:00 +0100135 // Add details for profiling output
136 WorkloadInfo detailsInfo;
137
138 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
139 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
Mike Kellyec67a0f2022-11-25 13:55:24 +0000140 detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[1]);
Keith Davis2d0679f2021-08-05 11:35:00 +0100141 if (descriptor.m_Parameters.m_BiasEnabled)
142 {
Mike Kellyec67a0f2022-11-25 13:55:24 +0000143 detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[2]);
Keith Davis2d0679f2021-08-05 11:35:00 +0100144 }
145
146 // Report Profiling Details
147 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonDepthwiseConvolution2dWorkload_Construct",
148 descriptor.m_Parameters,
149 detailsInfo,
Cathal Corbett4b19d222022-05-11 20:12:17 +0100150 GetGuid());
Keith Davis2d0679f2021-08-05 11:35:00 +0100151
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100152 ARMNN_ASSERT(m_pDepthwiseConvolutionLayer);
telsoa014fcda012018-03-09 14:13:49 +0000153
telsoa01c577f2c2018-08-31 09:22:23 +0100154 m_pDepthwiseConvolutionLayer->prepare();
telsoa014fcda012018-03-09 14:13:49 +0000155}
156
Nattapat Chaimanowong77140882018-10-17 11:12:19 +0100157void NeonDepthwiseConvolutionWorkload::Execute() const
telsoa014fcda012018-03-09 14:13:49 +0000158{
Cathal Corbett4b19d222022-05-11 20:12:17 +0100159 ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID("NeonDepthwiseConvolutionWorkload_Execute", GetGuid());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100160 ARMNN_ASSERT(m_pDepthwiseConvolutionLayer);
telsoa014fcda012018-03-09 14:13:49 +0000161
162 m_pDepthwiseConvolutionLayer->run();
163}
164
telsoa014fcda012018-03-09 14:13:49 +0000165} //namespace armnn