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 | |
| 8 | #include "TypeUtils.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); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 55 | |
| 56 | return arm_compute::CLDepthwiseConvolutionLayer::validate(&aclInputInfo, |
| 57 | &aclWeightsInfo, |
| 58 | optionalAclBiasesInfo, |
| 59 | &aclOutputInfo, |
| 60 | aclPadStrideInfo, |
| 61 | aclDepthMultiplier); |
| 62 | } |
| 63 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 64 | ClDepthwiseConvolutionWorkload::ClDepthwiseConvolutionWorkload( |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 65 | const DepthwiseConvolution2dQueueDescriptor& descriptor, |
| 66 | const WorkloadInfo& info) |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 67 | : BaseWorkload<DepthwiseConvolution2dQueueDescriptor>(descriptor, info) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 68 | { |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 69 | // Allocate a buffer for the swizzling of the weight tensor |
| 70 | 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] | 71 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 72 | // Convert the weight format from ArmNN's [ M, I, H, W ] (does NOT depend on the data layout) to either |
| 73 | // [ 1, H, W, I * M ] (if NHWC) or [ 1, I * M, H, W ] (if NCHW), as required by the compute library |
| 74 | ConstTensor weightPermuted = ConvertWeightTensorFromArmnnToAcl(m_Data.m_Weight, |
| 75 | m_Data.m_Parameters.m_DataLayout, |
| 76 | permuteBuffer.get()); |
| 77 | |
| 78 | // Convert the weights into the compute library format |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 79 | m_KernelTensor = std::make_unique<arm_compute::CLTensor>(); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 80 | BuildArmComputeTensor(*m_KernelTensor, weightPermuted.GetInfo(), m_Data.m_Parameters.m_DataLayout); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 81 | |
| 82 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 83 | { |
| 84 | m_BiasTensor = std::make_unique<arm_compute::CLTensor>(); |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 85 | 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] | 86 | } |
| 87 | |
| 88 | arm_compute::PadStrideInfo padStrideInfo(m_Data.m_Parameters.m_StrideX, |
| 89 | m_Data.m_Parameters.m_StrideY, |
| 90 | m_Data.m_Parameters.m_PadLeft, |
| 91 | m_Data.m_Parameters.m_PadRight, |
| 92 | m_Data.m_Parameters.m_PadTop, |
| 93 | m_Data.m_Parameters.m_PadBottom, |
| 94 | arm_compute::DimensionRoundingType::FLOOR); |
| 95 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 96 | std::string name = std::string("ClDepthwiseConvolutionWorkload"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 97 | m_Data.ValidateInputsOutputs(name, 1, 1); |
| 98 | |
| 99 | arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 100 | arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
| 101 | |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 102 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
| 103 | input.info()->set_data_layout(aclDataLayout); |
| 104 | output.info()->set_data_layout(aclDataLayout); |
| 105 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 106 | // ArmNN's weight format is [ M, I, H, W ] |
| 107 | auto& weightInfo = m_Data.m_Weight->GetTensorInfo(); |
| 108 | |
| 109 | // Get the depth multiplier |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 110 | const unsigned int depthMultiplier = weightInfo.GetShape()[0]; |
| 111 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 112 | // Check for optimisation opportunities. |
| 113 | bool use3x3Optimisation = (weightInfo.GetShape()[2] == 3) && (weightInfo.GetShape()[3] == 3); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 114 | if (use3x3Optimisation) |
| 115 | { |
| 116 | m_DepthwiseConvolutionLayer = std::make_unique<arm_compute::CLDepthwiseConvolutionLayer3x3>(); |
| 117 | static_cast<arm_compute::CLDepthwiseConvolutionLayer3x3*>(m_DepthwiseConvolutionLayer.get())->configure( |
| 118 | &input, |
| 119 | m_KernelTensor.get(), |
| 120 | m_BiasTensor.get(), |
| 121 | &output, |
| 122 | padStrideInfo, |
| 123 | depthMultiplier); |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | m_DepthwiseConvolutionLayer = std::make_unique<arm_compute::CLDepthwiseConvolutionLayer>(); |
| 128 | static_cast<arm_compute::CLDepthwiseConvolutionLayer*>(m_DepthwiseConvolutionLayer.get())->configure( |
| 129 | &input, |
| 130 | m_KernelTensor.get(), |
| 131 | m_BiasTensor.get(), |
| 132 | &output, |
| 133 | padStrideInfo, |
| 134 | depthMultiplier); |
| 135 | } |
| 136 | |
| 137 | BOOST_ASSERT(m_DepthwiseConvolutionLayer); |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 138 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 139 | ScopedCpuTensorHandle weightsPermutedHandle(weightPermuted); |
| 140 | InitializeArmComputeClTensorData(*m_KernelTensor, &weightsPermutedHandle); |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 141 | |
| 142 | if (m_BiasTensor) |
| 143 | { |
| 144 | InitializeArmComputeClTensorData(*m_BiasTensor, m_Data.m_Bias); |
| 145 | } |
| 146 | |
| 147 | m_DepthwiseConvolutionLayer->prepare(); |
| 148 | FreeUnusedTensors(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 151 | void ClDepthwiseConvolutionWorkload::FreeUnusedTensors() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 152 | { |
| 153 | FreeTensorIfUnused(m_KernelTensor); |
| 154 | FreeTensorIfUnused(m_BiasTensor); |
| 155 | } |
| 156 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 157 | void ClDepthwiseConvolutionWorkload::Execute() const |
| 158 | { |
| 159 | ARMNN_SCOPED_PROFILING_EVENT_CL("ClDepthwiseConvolutionWorkload_Execute"); |
| 160 | BOOST_ASSERT(m_DepthwiseConvolutionLayer); |
| 161 | |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 162 | RunClFunction(*m_DepthwiseConvolutionLayer, CHECK_LOCATION()); |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 163 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 164 | |
| 165 | } // namespace armnn |