telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
Teresa Charlin | 588cbdf | 2022-01-19 15:55:37 +0000 | [diff] [blame] | 2 | // Copyright © 2017 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 | { |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 36 | const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 37 | const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout); |
| 38 | |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 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; |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 43 | std::tie(weightsPermuted, aclDepthMultiplier) = Convert1HWOTensorInfoToAcl(weights, input, descriptor.m_DataLayout); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 44 | |
| 45 | // Convert the weights into the compute library format |
| 46 | const arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weightsPermuted, descriptor.m_DataLayout); |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 47 | |
| 48 | arm_compute::TensorInfo aclBiasesInfo; |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 49 | arm_compute::TensorInfo* optionalAclBiasesInfo = nullptr; |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 50 | |
| 51 | if (descriptor.m_BiasEnabled) |
| 52 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 53 | ARMNN_ASSERT(biases.has_value()); |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 54 | |
| 55 | aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout); |
| 56 | optionalAclBiasesInfo = &aclBiasesInfo; |
| 57 | } |
| 58 | |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 59 | arm_compute::PadStrideInfo aclPadStrideInfo = BuildArmComputePadStrideInfo(descriptor); |
| 60 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D( |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 61 | descriptor.m_DilationX, descriptor.m_DilationY); |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 62 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 63 | const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo( |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 64 | activationDescriptor); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 65 | |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 66 | return arm_compute::NEDepthwiseConvolutionLayer::validate(&aclInputInfo, |
| 67 | &aclWeightsInfo, |
| 68 | optionalAclBiasesInfo, |
| 69 | &aclOutputInfo, |
| 70 | aclPadStrideInfo, |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 71 | aclDepthMultiplier, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 72 | activationInfo, |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 73 | aclDilationInfo); |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | NeonDepthwiseConvolutionWorkload::NeonDepthwiseConvolutionWorkload( |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 77 | const DepthwiseConvolution2dQueueDescriptor& descriptor, |
| 78 | const WorkloadInfo& info) |
Teresa Charlin | 588cbdf | 2022-01-19 15:55:37 +0000 | [diff] [blame] | 79 | : NeonBaseWorkload<DepthwiseConvolution2dQueueDescriptor>(descriptor, info) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 80 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 81 | // ArmNN's weight format for depthwise is [ 1, H, W, I*M ] |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 82 | auto& weightInfo = m_Data.m_Weight->GetTensorInfo(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 83 | |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 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, |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 88 | info.m_InputTensorInfos[0], |
| 89 | m_Data.m_Parameters.m_DataLayout, |
| 90 | permuteBuffer.get()); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 91 | |
| 92 | // Convert the weights into the compute library format |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 93 | m_KernelTensor = std::make_unique<arm_compute::Tensor>(); |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 94 | BuildArmComputeTensor(*m_KernelTensor, weightsPermuted.GetInfo(), m_Data.m_Parameters.m_DataLayout); |
Mohamed Nour Abouelseoud | 7e7261e | 2018-11-27 17:35:35 +0000 | [diff] [blame] | 95 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 96 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 97 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 98 | m_BiasTensor = std::make_unique<arm_compute::Tensor>(); |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 99 | BuildArmComputeTensor(*m_BiasTensor, m_Data.m_Bias->GetTensorInfo(), m_Data.m_Parameters.m_DataLayout); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 102 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D( |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 103 | m_Data.m_Parameters.m_DilationX, m_Data.m_Parameters.m_DilationY); |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 104 | |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 105 | m_Data.ValidateInputsOutputs("NeonDepthwiseConvolutionWorkload", 1, 1); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 106 | |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 107 | IAclTensorHandle* inputTensorHandle = static_cast<IAclTensorHandle*>(m_Data.m_Inputs[0]); |
Derek Lamberti | c81855f | 2019-06-13 17:34:19 +0100 | [diff] [blame] | 108 | IAclTensorHandle* outputTensorHandle = static_cast<IAclTensorHandle*>(m_Data.m_Outputs[0]); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 109 | |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 110 | arm_compute::ITensor& input = inputTensorHandle->GetTensor(); |
Mohamed Nour Abouelseoud | 7e7261e | 2018-11-27 17:35:35 +0000 | [diff] [blame] | 111 | arm_compute::ITensor& output = outputTensorHandle->GetTensor(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 112 | |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 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 | |
Aron Virginas-Tar | 6f3785d | 2019-07-22 15:30:22 +0100 | [diff] [blame] | 117 | arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters); |
| 118 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 119 | const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor); |
| 120 | |
Aron Virginas-Tar | f4c502f | 2019-11-14 16:21:38 +0000 | [diff] [blame] | 121 | m_pDepthwiseConvolutionLayer = std::make_unique<arm_compute::NEDepthwiseConvolutionLayer>(); |
| 122 | static_cast<arm_compute::NEDepthwiseConvolutionLayer*>( |
| 123 | m_pDepthwiseConvolutionLayer.get())->configure(&input, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 124 | m_KernelTensor.get(), |
| 125 | m_BiasTensor.get(), |
| 126 | &output, |
| 127 | padStrideInfo, |
| 128 | depthMultiplier, |
| 129 | activationInfo, |
| 130 | aclDilationInfo); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 131 | |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 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 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 149 | ARMNN_ASSERT(m_pDepthwiseConvolutionLayer); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 150 | |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 151 | ScopedTensorHandle weightsPermutedHandle(weightsPermuted); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 152 | InitializeArmComputeTensorData(*m_KernelTensor, &weightsPermutedHandle); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 153 | |
Mohamed Nour Abouelseoud | 7e7261e | 2018-11-27 17:35:35 +0000 | [diff] [blame] | 154 | if (m_Data.m_Parameters.m_BiasEnabled) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 155 | { |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 156 | InitializeArmComputeTensorData(*m_BiasTensor, m_Data.m_Bias); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 157 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 158 | |
| 159 | m_pDepthwiseConvolutionLayer->prepare(); |
| 160 | FreeUnusedTensors(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 163 | void NeonDepthwiseConvolutionWorkload::Execute() const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 164 | { |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 165 | ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID("NeonDepthwiseConvolutionWorkload_Execute", this->GetGuid()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 166 | ARMNN_ASSERT(m_pDepthwiseConvolutionLayer); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 167 | |
| 168 | m_pDepthwiseConvolutionLayer->run(); |
| 169 | } |
| 170 | |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 171 | void NeonDepthwiseConvolutionWorkload::FreeUnusedTensors() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 172 | { |
| 173 | FreeTensorIfUnused(m_KernelTensor); |
| 174 | FreeTensorIfUnused(m_BiasTensor); |
| 175 | } |
| 176 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 177 | } //namespace armnn |