telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [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 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
| 5 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 6 | #include "ClDepthwiseConvolutionWorkload.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 7 | |
Aron Virginas-Tar | d4f0fea | 2019-04-09 14:08:06 +0100 | [diff] [blame] | 8 | #include <ResolveType.hpp> |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 9 | #include "ClWorkloadUtils.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 10 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 11 | #include <aclCommon/ArmComputeUtils.hpp> |
| 12 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
| 13 | #include <cl/ClTensorHandle.hpp> |
| 14 | #include <backendsCommon/CpuTensorHandle.hpp> |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 15 | #include <backendsCommon/WorkloadUtils.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 16 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 17 | #include <arm_compute/runtime/CL/functions/CLDepthwiseConvolutionLayer.h> |
| 18 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 19 | namespace armnn |
| 20 | { |
| 21 | |
| 22 | using namespace armcomputetensorutils; |
| 23 | |
| 24 | arm_compute::Status ClDepthwiseConvolutionWorkloadValidate(const TensorInfo& input, |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 25 | const TensorInfo& output, |
| 26 | const DepthwiseConvolution2dDescriptor& descriptor, |
| 27 | const TensorInfo& weights, |
| 28 | const Optional<TensorInfo>& biases) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 29 | { |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 30 | const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout); |
Nikhil Raj | a05c210 | 2018-09-25 16:16:13 +0100 | [diff] [blame] | 31 | const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 32 | |
| 33 | // ArmNN's weight format is [ M, I, H, W ] |
| 34 | const unsigned int aclDepthMultiplier = weights.GetShape()[0]; |
| 35 | |
| 36 | // Convert the weight format from ArmNN's [ M, I, H, W ] (does NOT depend on the data layout) to either |
| 37 | // [ 1, H, W, I * M ] (if NHWC) or [ 1, I * M, H, W ] (if NCHW), as required by the compute library |
| 38 | TensorInfo weightsPermuted = ConvertWeightTensorInfoFromArmnnToAcl(weights, descriptor.m_DataLayout); |
| 39 | |
| 40 | // Convert the weights into the compute library format |
| 41 | const arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weightsPermuted, descriptor.m_DataLayout); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 42 | |
| 43 | arm_compute::TensorInfo aclBiasesInfo; |
| 44 | arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr; |
arovir01 | a682410 | 2018-08-28 17:40:45 +0100 | [diff] [blame] | 45 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 46 | if (descriptor.m_BiasEnabled) |
| 47 | { |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 48 | BOOST_ASSERT(biases.has_value()); |
arovir01 | a682410 | 2018-08-28 17:40:45 +0100 | [diff] [blame] | 49 | |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 50 | aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 51 | optionalAclBiasesInfo = &aclBiasesInfo; |
| 52 | } |
| 53 | |
| 54 | const arm_compute::PadStrideInfo aclPadStrideInfo = BuildArmComputePadStrideInfo(descriptor); |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 55 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D( |
| 56 | descriptor.m_DilationX, |
| 57 | descriptor.m_DilationY); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 58 | |
| 59 | return arm_compute::CLDepthwiseConvolutionLayer::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); |
| 67 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 70 | ClDepthwiseConvolutionWorkload::ClDepthwiseConvolutionWorkload( |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 71 | const DepthwiseConvolution2dQueueDescriptor& descriptor, |
| 72 | const WorkloadInfo& info) |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 73 | : BaseWorkload<DepthwiseConvolution2dQueueDescriptor>(descriptor, info) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 74 | { |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 75 | // Allocate a buffer for the swizzling of the weight tensor |
| 76 | std::unique_ptr<unsigned char[]> permuteBuffer(new unsigned char[m_Data.m_Weight->GetTensorInfo().GetNumBytes()]); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 77 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 78 | // Convert the weight format from ArmNN's [ M, I, H, W ] (does NOT depend on the data layout) to either |
| 79 | // [ 1, H, W, I * M ] (if NHWC) or [ 1, I * M, H, W ] (if NCHW), as required by the compute library |
| 80 | ConstTensor weightPermuted = ConvertWeightTensorFromArmnnToAcl(m_Data.m_Weight, |
| 81 | m_Data.m_Parameters.m_DataLayout, |
| 82 | permuteBuffer.get()); |
| 83 | |
| 84 | // Convert the weights into the compute library format |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 85 | m_KernelTensor = std::make_unique<arm_compute::CLTensor>(); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 86 | BuildArmComputeTensor(*m_KernelTensor, weightPermuted.GetInfo(), m_Data.m_Parameters.m_DataLayout); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 87 | |
| 88 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 89 | { |
| 90 | m_BiasTensor = std::make_unique<arm_compute::CLTensor>(); |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 91 | BuildArmComputeTensor(*m_BiasTensor, m_Data.m_Bias->GetTensorInfo(), m_Data.m_Parameters.m_DataLayout); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 94 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D( |
| 95 | m_Data.m_Parameters.m_DilationX, |
| 96 | m_Data.m_Parameters.m_DilationY); |
| 97 | |
| 98 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 99 | std::string name = std::string("ClDepthwiseConvolutionWorkload"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 100 | m_Data.ValidateInputsOutputs(name, 1, 1); |
| 101 | |
| 102 | arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 103 | arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
| 104 | |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 105 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
| 106 | input.info()->set_data_layout(aclDataLayout); |
| 107 | output.info()->set_data_layout(aclDataLayout); |
| 108 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 109 | // ArmNN's weight format is [ M, I, H, W ] |
| 110 | auto& weightInfo = m_Data.m_Weight->GetTensorInfo(); |
| 111 | |
| 112 | // Get the depth multiplier |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 113 | const unsigned int depthMultiplier = weightInfo.GetShape()[0]; |
| 114 | |
Aron Virginas-Tar | 6f3785d | 2019-07-22 15:30:22 +0100 | [diff] [blame] | 115 | arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters); |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 116 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 117 | // Check for optimisation opportunities. |
| 118 | bool use3x3Optimisation = (weightInfo.GetShape()[2] == 3) && (weightInfo.GetShape()[3] == 3); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 119 | if (use3x3Optimisation) |
| 120 | { |
| 121 | m_DepthwiseConvolutionLayer = std::make_unique<arm_compute::CLDepthwiseConvolutionLayer3x3>(); |
| 122 | static_cast<arm_compute::CLDepthwiseConvolutionLayer3x3*>(m_DepthwiseConvolutionLayer.get())->configure( |
| 123 | &input, |
| 124 | m_KernelTensor.get(), |
| 125 | m_BiasTensor.get(), |
| 126 | &output, |
| 127 | padStrideInfo, |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 128 | depthMultiplier, |
| 129 | arm_compute::ActivationLayerInfo(), |
| 130 | aclDilationInfo); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 131 | } |
| 132 | else |
| 133 | { |
| 134 | m_DepthwiseConvolutionLayer = std::make_unique<arm_compute::CLDepthwiseConvolutionLayer>(); |
| 135 | static_cast<arm_compute::CLDepthwiseConvolutionLayer*>(m_DepthwiseConvolutionLayer.get())->configure( |
| 136 | &input, |
| 137 | m_KernelTensor.get(), |
| 138 | m_BiasTensor.get(), |
| 139 | &output, |
| 140 | padStrideInfo, |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 141 | depthMultiplier, |
| 142 | arm_compute::ActivationLayerInfo(), |
| 143 | aclDilationInfo); |
| 144 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | BOOST_ASSERT(m_DepthwiseConvolutionLayer); |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 148 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 149 | ScopedCpuTensorHandle weightsPermutedHandle(weightPermuted); |
| 150 | InitializeArmComputeClTensorData(*m_KernelTensor, &weightsPermutedHandle); |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 151 | |
| 152 | if (m_BiasTensor) |
| 153 | { |
| 154 | InitializeArmComputeClTensorData(*m_BiasTensor, m_Data.m_Bias); |
| 155 | } |
| 156 | |
| 157 | m_DepthwiseConvolutionLayer->prepare(); |
| 158 | FreeUnusedTensors(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 161 | void ClDepthwiseConvolutionWorkload::FreeUnusedTensors() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 162 | { |
| 163 | FreeTensorIfUnused(m_KernelTensor); |
| 164 | FreeTensorIfUnused(m_BiasTensor); |
| 165 | } |
| 166 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 167 | void ClDepthwiseConvolutionWorkload::Execute() const |
| 168 | { |
| 169 | ARMNN_SCOPED_PROFILING_EVENT_CL("ClDepthwiseConvolutionWorkload_Execute"); |
| 170 | BOOST_ASSERT(m_DepthwiseConvolutionLayer); |
| 171 | |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 172 | RunClFunction(*m_DepthwiseConvolutionLayer, CHECK_LOCATION()); |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 173 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 174 | |
| 175 | } // namespace armnn |