blob: 00d9d3340ed302627b7b5879b9b4f9128bab6199 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
Mike Kellyec67a0f2022-11-25 13:55:24 +00002// Copyright © 2017,2022 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());
Cathal Corbett4452baf2022-05-13 09:55:59 +010061 // Same for bias as weights. We don't currently support non const.
62 if (!biases.value().IsConstant())
63 {
64 return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
65 "ArmNN NeonDepthwiseConv2dWorkload does not support non constant bias."};
66 }
Nattapat Chaimanowong77140882018-10-17 11:12:19 +010067 aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
Cathal Corbett4452baf2022-05-13 09:55:59 +010068 aclBiasesInfo.set_are_values_constant(biases.value().IsConstant());
Nattapat Chaimanowong77140882018-10-17 11:12:19 +010069 optionalAclBiasesInfo = &aclBiasesInfo;
70 }
71
Pablo Tellof0bd6832019-04-26 17:58:13 +010072 arm_compute::PadStrideInfo aclPadStrideInfo = BuildArmComputePadStrideInfo(descriptor);
73 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
Keith Davis2d0679f2021-08-05 11:35:00 +010074 descriptor.m_DilationX, descriptor.m_DilationY);
Nattapat Chaimanowong77140882018-10-17 11:12:19 +010075
Mike Kelly07810fc2020-11-12 10:58:48 +000076 const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
Keith Davis2d0679f2021-08-05 11:35:00 +010077 activationDescriptor);
Mike Kelly07810fc2020-11-12 10:58:48 +000078
Nattapat Chaimanowong77140882018-10-17 11:12:19 +010079 return arm_compute::NEDepthwiseConvolutionLayer::validate(&aclInputInfo,
80 &aclWeightsInfo,
81 optionalAclBiasesInfo,
82 &aclOutputInfo,
83 aclPadStrideInfo,
Pablo Tellof0bd6832019-04-26 17:58:13 +010084 aclDepthMultiplier,
Mike Kelly07810fc2020-11-12 10:58:48 +000085 activationInfo,
Pablo Tellof0bd6832019-04-26 17:58:13 +010086 aclDilationInfo);
Nattapat Chaimanowong77140882018-10-17 11:12:19 +010087}
88
89NeonDepthwiseConvolutionWorkload::NeonDepthwiseConvolutionWorkload(
telsoa014fcda012018-03-09 14:13:49 +000090 const DepthwiseConvolution2dQueueDescriptor& descriptor,
91 const WorkloadInfo& info)
Teresa Charlin588cbdf2022-01-19 15:55:37 +000092 : NeonBaseWorkload<DepthwiseConvolution2dQueueDescriptor>(descriptor, info)
telsoa014fcda012018-03-09 14:13:49 +000093{
Cathal Corbett4b19d222022-05-11 20:12:17 +010094 arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
95 arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
96 arm_compute::ITensor& weights = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
97 arm_compute::ITensor* biasesPtr = nullptr;
telsoa014fcda012018-03-09 14:13:49 +000098 if (m_Data.m_Parameters.m_BiasEnabled)
99 {
Cathal Corbett4b19d222022-05-11 20:12:17 +0100100 biasesPtr = &PolymorphicDowncast<IAclTensorHandle *>(m_Data.m_Inputs[2])->GetTensor();
telsoa014fcda012018-03-09 14:13:49 +0000101 }
102
Cathal Corbett4b19d222022-05-11 20:12:17 +0100103 arm_compute::ITensorInfo* weightsInfo = weights.info();
104 arm_compute::ITensorInfo* inputInfo = input.info();
105 auto weightsShape = weightsInfo->tensor_shape();
106 auto inputShape = inputInfo->tensor_shape();
107
108 // The PermuteDepthwiseConv2dWeights backend optimization has been performed,
109 // converting weights to have the same data layout as input.
110 unsigned int depthMultiplier =
111 ComputeDepthwiseConv2dDepthMultiplier(m_Data.m_Parameters.m_DataLayout, weightsShape, inputShape);
112
Pablo Tellof0bd6832019-04-26 17:58:13 +0100113 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
Keith Davis2d0679f2021-08-05 11:35:00 +0100114 m_Data.m_Parameters.m_DilationX, m_Data.m_Parameters.m_DilationY);
Pablo Tellof0bd6832019-04-26 17:58:13 +0100115
Cathal Corbett4b19d222022-05-11 20:12:17 +0100116 uint32_t numInputs = m_Data.m_Parameters.m_BiasEnabled ? 3: 2;
117 m_Data.ValidateInputsOutputs("NeonDepthwiseConvolutionWorkload", numInputs, 1);
telsoa014fcda012018-03-09 14:13:49 +0000118
Nikhil Rajcec6b652018-10-12 13:51:57 +0100119 arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
120 input.info()->set_data_layout(aclDataLayout);
Cathal Corbett4b19d222022-05-11 20:12:17 +0100121 weights.info()->set_data_layout(aclDataLayout);
Nikhil Rajcec6b652018-10-12 13:51:57 +0100122 output.info()->set_data_layout(aclDataLayout);
123
Aron Virginas-Tar6f3785d2019-07-22 15:30:22 +0100124 arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
125
Mike Kelly07810fc2020-11-12 10:58:48 +0000126 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
127
Aron Virginas-Tarf4c502f2019-11-14 16:21:38 +0000128 m_pDepthwiseConvolutionLayer = std::make_unique<arm_compute::NEDepthwiseConvolutionLayer>();
129 static_cast<arm_compute::NEDepthwiseConvolutionLayer*>(
130 m_pDepthwiseConvolutionLayer.get())->configure(&input,
Cathal Corbett4b19d222022-05-11 20:12:17 +0100131 &weights,
132 biasesPtr,
Mike Kelly07810fc2020-11-12 10:58:48 +0000133 &output,
134 padStrideInfo,
135 depthMultiplier,
136 activationInfo,
137 aclDilationInfo);
telsoa014fcda012018-03-09 14:13:49 +0000138
Keith Davis2d0679f2021-08-05 11:35:00 +0100139 // Add details for profiling output
140 WorkloadInfo detailsInfo;
141
142 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
143 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
Mike Kellyec67a0f2022-11-25 13:55:24 +0000144 detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[1]);
Keith Davis2d0679f2021-08-05 11:35:00 +0100145 if (descriptor.m_Parameters.m_BiasEnabled)
146 {
Mike Kellyec67a0f2022-11-25 13:55:24 +0000147 detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[2]);
Keith Davis2d0679f2021-08-05 11:35:00 +0100148 }
149
150 // Report Profiling Details
151 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonDepthwiseConvolution2dWorkload_Construct",
152 descriptor.m_Parameters,
153 detailsInfo,
Cathal Corbett4b19d222022-05-11 20:12:17 +0100154 GetGuid());
Keith Davis2d0679f2021-08-05 11:35:00 +0100155
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100156 ARMNN_ASSERT(m_pDepthwiseConvolutionLayer);
telsoa014fcda012018-03-09 14:13:49 +0000157
telsoa01c577f2c2018-08-31 09:22:23 +0100158 m_pDepthwiseConvolutionLayer->prepare();
telsoa014fcda012018-03-09 14:13:49 +0000159}
160
Nattapat Chaimanowong77140882018-10-17 11:12:19 +0100161void NeonDepthwiseConvolutionWorkload::Execute() const
telsoa014fcda012018-03-09 14:13:49 +0000162{
Cathal Corbett4b19d222022-05-11 20:12:17 +0100163 ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID("NeonDepthwiseConvolutionWorkload_Execute", GetGuid());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100164 ARMNN_ASSERT(m_pDepthwiseConvolutionLayer);
telsoa014fcda012018-03-09 14:13:49 +0000165
166 m_pDepthwiseConvolutionLayer->run();
167}
168
telsoa014fcda012018-03-09 14:13:49 +0000169} //namespace armnn