telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
Mike Kelly | 7cbe781 | 2023-07-25 17:37:33 +0100 | [diff] [blame^] | 2 | // Copyright © 2017-2023 Arm Ltd and Contributors. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 6 | #include "NeonDepthwiseConvolutionWorkload.hpp" |
| 7 | |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 8 | #include "NeonWorkloadUtils.hpp" |
| 9 | |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 10 | #include <armnnUtils/DataLayoutIndexed.hpp> |
| 11 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 12 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 13 | #include <aclCommon/ArmComputeUtils.hpp> |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 14 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 15 | #include <neon/NeonLayerSupport.hpp> |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 16 | |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 17 | #include <armnn/backends/TensorHandle.hpp> |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 18 | #include <backendsCommon/WorkloadUtils.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 19 | |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 20 | #include <arm_compute/runtime/NEON/functions/NEDepthwiseConvolutionLayer.h> |
| 21 | |
| 22 | using namespace armnnUtils; |
| 23 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 24 | namespace armnn |
| 25 | { |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 26 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 27 | using namespace armcomputetensorutils; |
| 28 | |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 29 | arm_compute::Status NeonDepthwiseConvolutionWorkloadValidate(const TensorInfo& input, |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 30 | const TensorInfo& output, |
| 31 | const DepthwiseConvolution2dDescriptor& descriptor, |
| 32 | const TensorInfo& weights, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 33 | const Optional<TensorInfo>& biases, |
| 34 | const ActivationDescriptor* activationDescriptor) |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 35 | { |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame] | 36 | const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout); |
| 37 | const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 38 | |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame] | 39 | // 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 Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 48 | unsigned int aclDepthMultiplier; |
| 49 | TensorInfo weightsPermuted; |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 50 | std::tie(weightsPermuted, aclDepthMultiplier) = Convert1HWOTensorInfoToAcl(weights, input, descriptor.m_DataLayout); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 51 | |
| 52 | // Convert the weights into the compute library format |
Cathal Corbett | 4452baf | 2022-05-13 09:55:59 +0100 | [diff] [blame] | 53 | arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weightsPermuted, descriptor.m_DataLayout); |
| 54 | aclWeightsInfo.set_are_values_constant(weights.IsConstant()); |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 55 | |
| 56 | arm_compute::TensorInfo aclBiasesInfo; |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 57 | arm_compute::TensorInfo* optionalAclBiasesInfo = nullptr; |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 58 | if (descriptor.m_BiasEnabled) |
| 59 | { |
Teresa Charlin | 5b70184 | 2023-05-16 12:27:28 +0100 | [diff] [blame] | 60 | if(!biases.has_value()) |
TeresaARM | acedd85 | 2023-05-11 15:16:39 +0000 | [diff] [blame] | 61 | { |
| 62 | return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR, |
Teresa Charlin | 5b70184 | 2023-05-16 12:27:28 +0100 | [diff] [blame] | 63 | "ArmNN NeonDepthwiseConvolutionWorkload has empty bias value."}; |
TeresaARM | acedd85 | 2023-05-11 15:16:39 +0000 | [diff] [blame] | 64 | } |
Colm Donelan | 47016c0 | 2023-07-20 10:08:03 +0100 | [diff] [blame] | 65 | // There's currently a problem with non const bias, so we'll explicitly block it here. |
| 66 | if (!biases.value().IsConstant()) |
| 67 | { |
| 68 | return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR, |
| 69 | "ArmNN NeonDepthwiseConvolutionWorkload does not support non constant bias."}; |
| 70 | } |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 71 | aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout); |
Cathal Corbett | 4452baf | 2022-05-13 09:55:59 +0100 | [diff] [blame] | 72 | aclBiasesInfo.set_are_values_constant(biases.value().IsConstant()); |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 73 | optionalAclBiasesInfo = &aclBiasesInfo; |
| 74 | } |
| 75 | |
Teresa Charlin | 5b70184 | 2023-05-16 12:27:28 +0100 | [diff] [blame] | 76 | const arm_compute::PadStrideInfo aclPadStrideInfo = BuildArmComputePadStrideInfo(descriptor); |
| 77 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(descriptor.m_DilationX, |
| 78 | descriptor.m_DilationY); |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 79 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 80 | const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo( |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 81 | activationDescriptor); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 82 | |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 83 | return arm_compute::NEDepthwiseConvolutionLayer::validate(&aclInputInfo, |
| 84 | &aclWeightsInfo, |
| 85 | optionalAclBiasesInfo, |
| 86 | &aclOutputInfo, |
| 87 | aclPadStrideInfo, |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 88 | aclDepthMultiplier, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 89 | activationInfo, |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 90 | aclDilationInfo); |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | NeonDepthwiseConvolutionWorkload::NeonDepthwiseConvolutionWorkload( |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 94 | const DepthwiseConvolution2dQueueDescriptor& descriptor, |
| 95 | const WorkloadInfo& info) |
Teresa Charlin | 588cbdf | 2022-01-19 15:55:37 +0000 | [diff] [blame] | 96 | : NeonBaseWorkload<DepthwiseConvolution2dQueueDescriptor>(descriptor, info) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 97 | { |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame] | 98 | arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 99 | arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
| 100 | arm_compute::ITensor& weights = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[1])->GetTensor(); |
Teresa Charlin | 6fba294 | 2023-04-18 12:48:46 +0100 | [diff] [blame] | 101 | weights.info()->set_are_values_constant(info.m_InputTensorInfos[1].IsConstant()); |
Teresa Charlin | 5b70184 | 2023-05-16 12:27:28 +0100 | [diff] [blame] | 102 | arm_compute::ITensor* biasesPtr = nullptr; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 103 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 104 | { |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame] | 105 | biasesPtr = &PolymorphicDowncast<IAclTensorHandle *>(m_Data.m_Inputs[2])->GetTensor(); |
Teresa Charlin | 6fba294 | 2023-04-18 12:48:46 +0100 | [diff] [blame] | 106 | biasesPtr->info()->set_are_values_constant(info.m_InputTensorInfos[2].IsConstant()); |
Colm Donelan | 47016c0 | 2023-07-20 10:08:03 +0100 | [diff] [blame] | 107 | // We assume here that NeonDepthwiseConvolutionWorkloadValidate has been called before the constructor. |
| 108 | ARMNN_ASSERT(info.m_InputTensorInfos[2].IsConstant() == true); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Teresa Charlin | 5b70184 | 2023-05-16 12:27:28 +0100 | [diff] [blame] | 111 | arm_compute::TensorShape weightsShape = weights.info()->tensor_shape(); |
| 112 | arm_compute::TensorShape inputShape = input.info()->tensor_shape(); |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame] | 113 | |
| 114 | // The PermuteDepthwiseConv2dWeights backend optimization has been performed, |
| 115 | // converting weights to have the same data layout as input. |
| 116 | unsigned int depthMultiplier = |
| 117 | ComputeDepthwiseConv2dDepthMultiplier(m_Data.m_Parameters.m_DataLayout, weightsShape, inputShape); |
| 118 | |
Teresa Charlin | 5b70184 | 2023-05-16 12:27:28 +0100 | [diff] [blame] | 119 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(m_Data.m_Parameters.m_DilationX, |
| 120 | m_Data.m_Parameters.m_DilationY); |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 121 | |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame] | 122 | uint32_t numInputs = m_Data.m_Parameters.m_BiasEnabled ? 3: 2; |
| 123 | m_Data.ValidateInputsOutputs("NeonDepthwiseConvolutionWorkload", numInputs, 1); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 124 | |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 125 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
| 126 | input.info()->set_data_layout(aclDataLayout); |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame] | 127 | weights.info()->set_data_layout(aclDataLayout); |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 128 | output.info()->set_data_layout(aclDataLayout); |
| 129 | |
Teresa Charlin | 5b70184 | 2023-05-16 12:27:28 +0100 | [diff] [blame] | 130 | const arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters); |
Aron Virginas-Tar | 6f3785d | 2019-07-22 15:30:22 +0100 | [diff] [blame] | 131 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 132 | const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor); |
| 133 | |
Aron Virginas-Tar | f4c502f | 2019-11-14 16:21:38 +0000 | [diff] [blame] | 134 | m_pDepthwiseConvolutionLayer = std::make_unique<arm_compute::NEDepthwiseConvolutionLayer>(); |
| 135 | static_cast<arm_compute::NEDepthwiseConvolutionLayer*>( |
| 136 | m_pDepthwiseConvolutionLayer.get())->configure(&input, |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame] | 137 | &weights, |
| 138 | biasesPtr, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 139 | &output, |
| 140 | padStrideInfo, |
| 141 | depthMultiplier, |
| 142 | activationInfo, |
| 143 | aclDilationInfo); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 144 | |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 145 | // Add details for profiling output |
| 146 | WorkloadInfo detailsInfo; |
| 147 | |
| 148 | detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos; |
| 149 | detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos; |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 150 | detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[1]); |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 151 | if (descriptor.m_Parameters.m_BiasEnabled) |
| 152 | { |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 153 | detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[2]); |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // Report Profiling Details |
| 157 | ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonDepthwiseConvolution2dWorkload_Construct", |
| 158 | descriptor.m_Parameters, |
| 159 | detailsInfo, |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame] | 160 | GetGuid()); |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 161 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 162 | ARMNN_ASSERT(m_pDepthwiseConvolutionLayer); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 163 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 164 | m_pDepthwiseConvolutionLayer->prepare(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 167 | void NeonDepthwiseConvolutionWorkload::Execute() const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 168 | { |
Mike Kelly | 7cbe781 | 2023-07-25 17:37:33 +0100 | [diff] [blame^] | 169 | ARMNN_SCOPED_PROFILING_EVENT_NEON_NAME_GUID("NeonDepthwiseConvolutionWorkload_Execute"); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 170 | ARMNN_ASSERT(m_pDepthwiseConvolutionLayer); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 171 | |
| 172 | m_pDepthwiseConvolutionLayer->run(); |
| 173 | } |
| 174 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 175 | } //namespace armnn |