telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. 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 | |
| 10 | #include <DataLayoutIndexed.hpp> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 11 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
| 12 | #include <neon/NeonLayerSupport.hpp> |
| 13 | #include <backendsCommon/CpuTensorHandle.hpp> |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 14 | #include <backendsCommon/WorkloadUtils.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 15 | |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 16 | #include <arm_compute/runtime/NEON/functions/NEDepthwiseConvolutionLayer.h> |
| 17 | |
| 18 | using namespace armnnUtils; |
| 19 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 20 | namespace armnn |
| 21 | { |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 22 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 23 | using namespace armcomputetensorutils; |
| 24 | |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 25 | arm_compute::Status NeonDepthwiseConvolutionWorkloadValidate(const TensorInfo& input, |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 26 | const TensorInfo& output, |
| 27 | const DepthwiseConvolution2dDescriptor& descriptor, |
| 28 | const TensorInfo& weights, |
| 29 | const Optional<TensorInfo>& biases) |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 30 | { |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 31 | const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout); |
| 32 | const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout); |
| 33 | |
| 34 | // ArmNN's weight format is [ M, I, H, W ] |
| 35 | const unsigned int aclDepthMultiplier = weights.GetShape()[0]; |
| 36 | |
| 37 | // Convert the weight format from ArmNN's [ M, I, H, W ] (does NOT depend on the data layout) to either |
| 38 | // [ 1, H, W, I * M ] (if NHWC) or [ 1, I * M, H, W ] (if NCHW), as required by the compute library |
| 39 | TensorInfo weightsPermuted = ConvertWeightTensorInfoFromArmnnToAcl(weights, descriptor.m_DataLayout); |
| 40 | |
| 41 | // Convert the weights into the compute library format |
| 42 | const arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weightsPermuted, descriptor.m_DataLayout); |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 43 | |
| 44 | arm_compute::TensorInfo aclBiasesInfo; |
| 45 | arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr; |
| 46 | |
| 47 | if (descriptor.m_BiasEnabled) |
| 48 | { |
| 49 | BOOST_ASSERT(biases.has_value()); |
| 50 | |
| 51 | aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout); |
| 52 | optionalAclBiasesInfo = &aclBiasesInfo; |
| 53 | } |
| 54 | |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 55 | arm_compute::PadStrideInfo aclPadStrideInfo = BuildArmComputePadStrideInfo(descriptor); |
| 56 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D( |
| 57 | descriptor.m_DilationX,descriptor.m_DilationY); |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 58 | |
| 59 | return arm_compute::NEDepthwiseConvolutionLayer::validate(&aclInputInfo, |
| 60 | &aclWeightsInfo, |
| 61 | optionalAclBiasesInfo, |
| 62 | &aclOutputInfo, |
| 63 | aclPadStrideInfo, |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 64 | aclDepthMultiplier, |
| 65 | arm_compute::ActivationLayerInfo(), |
| 66 | aclDilationInfo); |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | NeonDepthwiseConvolutionWorkload::NeonDepthwiseConvolutionWorkload( |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 70 | const DepthwiseConvolution2dQueueDescriptor& descriptor, |
| 71 | const WorkloadInfo& info) |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 72 | : BaseWorkload<DepthwiseConvolution2dQueueDescriptor>(descriptor, info) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 73 | { |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 74 | // ArmNN's weight format is [ M, I, H, W ] |
| 75 | auto& weightInfo = m_Data.m_Weight->GetTensorInfo(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 76 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 77 | // Allocate a buffer for the swizzling of the weight tensor |
| 78 | std::unique_ptr<unsigned char[]> permuteBuffer(new unsigned char[m_Data.m_Weight->GetTensorInfo().GetNumBytes()]); |
| 79 | |
| 80 | // Convert the weight format from ArmNN's [ M, I, H, W ] (does NOT depend on the data layout) to either |
| 81 | // [ 1, H, W, I * M ] (if NHWC) or [ 1, I * M, H, W ] (if NCHW), as required by the compute library |
| 82 | ConstTensor weightPermuted = ConvertWeightTensorFromArmnnToAcl(m_Data.m_Weight, |
| 83 | m_Data.m_Parameters.m_DataLayout, |
| 84 | permuteBuffer.get()); |
| 85 | |
| 86 | // Convert the weights into the compute library format |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 87 | m_KernelTensor = std::make_unique<arm_compute::Tensor>(); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 88 | BuildArmComputeTensor(*m_KernelTensor, weightPermuted.GetInfo(), m_Data.m_Parameters.m_DataLayout); |
Mohamed Nour Abouelseoud | 7e7261e | 2018-11-27 17:35:35 +0000 | [diff] [blame] | 89 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 90 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 91 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 92 | m_BiasTensor = std::make_unique<arm_compute::Tensor>(); |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 93 | 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] | 94 | } |
| 95 | |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 96 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D( |
| 97 | m_Data.m_Parameters.m_DilationX, m_Data.m_Parameters.m_DilationY); |
| 98 | |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 99 | m_Data.ValidateInputsOutputs("NeonDepthwiseConvolutionWorkload", 1, 1); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 100 | |
Derek Lamberti | c81855f | 2019-06-13 17:34:19 +0100 | [diff] [blame] | 101 | IAclTensorHandle* inputTensorHandle = static_cast<IAclTensorHandle*>(m_Data.m_Inputs[0]); |
| 102 | IAclTensorHandle* outputTensorHandle = static_cast<IAclTensorHandle*>(m_Data.m_Outputs[0]); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 103 | |
Mohamed Nour Abouelseoud | 7e7261e | 2018-11-27 17:35:35 +0000 | [diff] [blame] | 104 | arm_compute::ITensor& input = inputTensorHandle->GetTensor(); |
| 105 | arm_compute::ITensor& output = outputTensorHandle->GetTensor(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 106 | |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 107 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
| 108 | input.info()->set_data_layout(aclDataLayout); |
| 109 | output.info()->set_data_layout(aclDataLayout); |
| 110 | |
Bruno Goncalves | 22972f0 | 2019-04-26 21:03:24 -0300 | [diff] [blame] | 111 | // Get the depth multiplier |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 112 | const unsigned int depthMultiplier = weightInfo.GetShape()[0]; |
Mohamed Nour Abouelseoud | 7e7261e | 2018-11-27 17:35:35 +0000 | [diff] [blame] | 113 | |
Aron Virginas-Tar | 6f3785d | 2019-07-22 15:30:22 +0100 | [diff] [blame] | 114 | arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters); |
| 115 | |
Matthew Jackson | 5488cfa | 2019-08-02 14:53:10 +0100 | [diff] [blame] | 116 | const arm_compute::ITensorInfo* inputInfo = input.info(); |
| 117 | const arm_compute::ITensorInfo* kernelInfo = m_KernelTensor->info(); |
| 118 | const arm_compute::ITensorInfo* biasInfo = m_BiasTensor ? m_BiasTensor->info() : nullptr; |
| 119 | const arm_compute::ITensorInfo* outputInfo = output.info(); |
Pablo Tello | b38ed40 | 2019-07-26 13:45:48 +0100 | [diff] [blame] | 120 | |
Matthew Jackson | 5488cfa | 2019-08-02 14:53:10 +0100 | [diff] [blame] | 121 | // Check for optimisation opportunities |
| 122 | arm_compute::Status optimizationStatus = |
| 123 | arm_compute::NEDepthwiseConvolutionLayerOptimized::validate(inputInfo, |
| 124 | kernelInfo, |
| 125 | biasInfo, |
| 126 | outputInfo, |
| 127 | padStrideInfo, |
| 128 | depthMultiplier, |
| 129 | arm_compute::ActivationLayerInfo(), |
| 130 | aclDilationInfo); |
| 131 | |
| 132 | if (optimizationStatus.error_code() == arm_compute::ErrorCode::OK) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 133 | { |
Aron Virginas-Tar | 974e5b6 | 2019-07-11 14:57:01 +0100 | [diff] [blame] | 134 | m_pDepthwiseConvolutionLayer = std::make_unique<arm_compute::NEDepthwiseConvolutionLayerOptimized>(); |
| 135 | static_cast<arm_compute::NEDepthwiseConvolutionLayerOptimized*>( |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 136 | m_pDepthwiseConvolutionLayer.get())->configure(&input, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 137 | m_KernelTensor.get(), |
| 138 | m_BiasTensor.get(), |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 139 | &output, |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 140 | padStrideInfo, |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 141 | depthMultiplier, |
| 142 | arm_compute::ActivationLayerInfo(), |
| 143 | aclDilationInfo); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 144 | } |
| 145 | else |
| 146 | { |
| 147 | m_pDepthwiseConvolutionLayer = std::make_unique<arm_compute::NEDepthwiseConvolutionLayer>(); |
| 148 | static_cast<arm_compute::NEDepthwiseConvolutionLayer*>( |
| 149 | m_pDepthwiseConvolutionLayer.get())->configure(&input, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 150 | m_KernelTensor.get(), |
| 151 | m_BiasTensor.get(), |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 152 | &output, |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 153 | padStrideInfo, |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 154 | depthMultiplier, |
| 155 | arm_compute::ActivationLayerInfo(), |
| 156 | aclDilationInfo); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | BOOST_ASSERT(m_pDepthwiseConvolutionLayer); |
| 160 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 161 | ScopedCpuTensorHandle weightsPermutedHandle(weightPermuted); |
| 162 | InitializeArmComputeTensorData(*m_KernelTensor, &weightsPermutedHandle); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 163 | |
Mohamed Nour Abouelseoud | 7e7261e | 2018-11-27 17:35:35 +0000 | [diff] [blame] | 164 | if (m_Data.m_Parameters.m_BiasEnabled) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 165 | { |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 166 | InitializeArmComputeTensorData(*m_BiasTensor, m_Data.m_Bias); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 167 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 168 | |
| 169 | m_pDepthwiseConvolutionLayer->prepare(); |
| 170 | FreeUnusedTensors(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 173 | void NeonDepthwiseConvolutionWorkload::Execute() const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 174 | { |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 175 | ARMNN_SCOPED_PROFILING_EVENT_NEON("NeonDepthwiseConvolutionWorkload_Execute"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 176 | BOOST_ASSERT(m_pDepthwiseConvolutionLayer); |
| 177 | |
| 178 | m_pDepthwiseConvolutionLayer->run(); |
| 179 | } |
| 180 | |
Nattapat Chaimanowong | 7714088 | 2018-10-17 11:12:19 +0100 | [diff] [blame] | 181 | void NeonDepthwiseConvolutionWorkload::FreeUnusedTensors() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 182 | { |
| 183 | FreeTensorIfUnused(m_KernelTensor); |
| 184 | FreeTensorIfUnused(m_BiasTensor); |
| 185 | } |
| 186 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 187 | } //namespace armnn |