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 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 11 | #include <armnn/Exceptions.hpp> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 12 | #include <aclCommon/ArmComputeUtils.hpp> |
| 13 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
| 14 | #include <cl/ClTensorHandle.hpp> |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 15 | #include <backendsCommon/TensorHandle.hpp> |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 16 | #include <backendsCommon/WorkloadUtils.hpp> |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 17 | #include <backendsCommon/WorkloadData.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 18 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 19 | #include <arm_compute/runtime/CL/functions/CLDepthwiseConvolutionLayer.h> |
| 20 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 21 | namespace armnn |
| 22 | { |
| 23 | |
| 24 | using namespace armcomputetensorutils; |
| 25 | |
| 26 | arm_compute::Status ClDepthwiseConvolutionWorkloadValidate(const TensorInfo& input, |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 27 | const TensorInfo& output, |
| 28 | const DepthwiseConvolution2dDescriptor& descriptor, |
| 29 | const TensorInfo& weights, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 30 | const Optional<TensorInfo>& biases, |
| 31 | const ActivationDescriptor* activationDescriptor) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 32 | { |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 33 | const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout); |
Nikhil Raj | a05c210 | 2018-09-25 16:16:13 +0100 | [diff] [blame] | 34 | const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 35 | |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 36 | // ArmNN's weight format is usually [ M, I, H, W ] but for depthwise its [ 1, H, W, I*M] |
| 37 | // Permute to [ 1, I * M, H, W ] (if NCHW) as required by the compute library |
| 38 | unsigned int aclDepthMultiplier; |
| 39 | TensorInfo weightsPermuted; |
| 40 | std::tie(weightsPermuted, aclDepthMultiplier) = Convert1HWOTensorInfoToAcl(weights, input,descriptor.m_DataLayout); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 41 | |
| 42 | // Convert the weights into the compute library format |
| 43 | const arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weightsPermuted, descriptor.m_DataLayout); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 44 | |
| 45 | arm_compute::TensorInfo aclBiasesInfo; |
| 46 | arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr; |
arovir01 | a682410 | 2018-08-28 17:40:45 +0100 | [diff] [blame] | 47 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 48 | if (descriptor.m_BiasEnabled) |
| 49 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 50 | ARMNN_ASSERT(biases.has_value()); |
arovir01 | a682410 | 2018-08-28 17:40:45 +0100 | [diff] [blame] | 51 | |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 52 | aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 53 | optionalAclBiasesInfo = &aclBiasesInfo; |
| 54 | } |
| 55 | |
| 56 | const arm_compute::PadStrideInfo aclPadStrideInfo = BuildArmComputePadStrideInfo(descriptor); |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 57 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D( |
| 58 | descriptor.m_DilationX, |
| 59 | descriptor.m_DilationY); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 60 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 61 | const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo( |
| 62 | activationDescriptor); |
| 63 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 64 | return arm_compute::CLDepthwiseConvolutionLayer::validate(&aclInputInfo, |
| 65 | &aclWeightsInfo, |
| 66 | optionalAclBiasesInfo, |
| 67 | &aclOutputInfo, |
| 68 | aclPadStrideInfo, |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 69 | aclDepthMultiplier, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 70 | activationInfo, |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 71 | aclDilationInfo); |
| 72 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 75 | ClDepthwiseConvolutionWorkload::ClDepthwiseConvolutionWorkload( |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 76 | const DepthwiseConvolution2dQueueDescriptor& descriptor, |
Sadik Armagan | e944475 | 2020-12-02 11:28:58 +0000 | [diff] [blame] | 77 | const WorkloadInfo& info, |
| 78 | const arm_compute::CLCompileContext& clCompileContext) |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 79 | : BaseWorkload<DepthwiseConvolution2dQueueDescriptor>(descriptor, info) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 80 | { |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 81 | // Add details for profiling output |
| 82 | WorkloadInfo detailsInfo; |
| 83 | |
| 84 | detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos; |
| 85 | detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos; |
| 86 | detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Weight->GetTensorInfo()); |
| 87 | if (descriptor.m_Parameters.m_BiasEnabled) |
| 88 | { |
| 89 | detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Bias->GetTensorInfo()); |
| 90 | } |
| 91 | |
| 92 | // Report Profiling Details |
| 93 | ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClDepthwiseConvolutionWorkload_Construct", |
| 94 | descriptor.m_Parameters, |
| 95 | detailsInfo, |
| 96 | this->GetGuid()); |
| 97 | |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 98 | // ArmNN's weight format is usually [ M, I, H, W ] but for depthwise its [ 1, H, W, I*M] |
| 99 | // Permute to [ 1, I * M, H, W ] (if NCHW), as required by the compute library |
| 100 | ConstTensor weightPermuted; |
| 101 | unsigned int depthMultiplier; |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 102 | std::unique_ptr<unsigned char[]> permuteBuffer(new unsigned char[m_Data.m_Weight->GetTensorInfo().GetNumBytes()]); |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 103 | std::tie(weightPermuted, depthMultiplier) = Convert1HWOTensorToAcl(m_Data.m_Weight, |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 104 | info.m_InputTensorInfos[0], |
| 105 | m_Data.m_Parameters.m_DataLayout, |
| 106 | permuteBuffer.get()); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 107 | |
| 108 | // Convert the weights into the compute library format |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 109 | m_KernelTensor = std::make_unique<arm_compute::CLTensor>(); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 110 | BuildArmComputeTensor(*m_KernelTensor, weightPermuted.GetInfo(), m_Data.m_Parameters.m_DataLayout); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 111 | |
| 112 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 113 | { |
| 114 | m_BiasTensor = std::make_unique<arm_compute::CLTensor>(); |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 115 | 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] | 116 | } |
| 117 | |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 118 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D( |
| 119 | m_Data.m_Parameters.m_DilationX, |
| 120 | m_Data.m_Parameters.m_DilationY); |
| 121 | |
| 122 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 123 | std::string name = std::string("ClDepthwiseConvolutionWorkload"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 124 | m_Data.ValidateInputsOutputs(name, 1, 1); |
| 125 | |
| 126 | arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 127 | arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
| 128 | |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 129 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
| 130 | input.info()->set_data_layout(aclDataLayout); |
| 131 | output.info()->set_data_layout(aclDataLayout); |
| 132 | |
Aron Virginas-Tar | 6f3785d | 2019-07-22 15:30:22 +0100 | [diff] [blame] | 133 | arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters); |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 134 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 135 | const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor); |
| 136 | |
Aron Virginas-Tar | f4c502f | 2019-11-14 16:21:38 +0000 | [diff] [blame] | 137 | m_DepthwiseConvolutionLayer = std::make_unique<arm_compute::CLDepthwiseConvolutionLayer>(); |
| 138 | static_cast<arm_compute::CLDepthwiseConvolutionLayer*>(m_DepthwiseConvolutionLayer.get())->configure( |
Sadik Armagan | e944475 | 2020-12-02 11:28:58 +0000 | [diff] [blame] | 139 | clCompileContext, |
Aron Virginas-Tar | f4c502f | 2019-11-14 16:21:38 +0000 | [diff] [blame] | 140 | &input, |
| 141 | m_KernelTensor.get(), |
| 142 | m_BiasTensor.get(), |
| 143 | &output, |
| 144 | padStrideInfo, |
| 145 | depthMultiplier, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 146 | activationInfo, |
Aron Virginas-Tar | f4c502f | 2019-11-14 16:21:38 +0000 | [diff] [blame] | 147 | aclDilationInfo); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 148 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 149 | ARMNN_ASSERT(m_DepthwiseConvolutionLayer); |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 150 | |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 151 | ScopedTensorHandle weightsPermutedHandle(weightPermuted); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 152 | InitializeArmComputeClTensorData(*m_KernelTensor, &weightsPermutedHandle); |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 153 | |
| 154 | if (m_BiasTensor) |
| 155 | { |
| 156 | InitializeArmComputeClTensorData(*m_BiasTensor, m_Data.m_Bias); |
| 157 | } |
| 158 | |
| 159 | m_DepthwiseConvolutionLayer->prepare(); |
| 160 | FreeUnusedTensors(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 163 | void ClDepthwiseConvolutionWorkload::FreeUnusedTensors() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 164 | { |
| 165 | FreeTensorIfUnused(m_KernelTensor); |
| 166 | FreeTensorIfUnused(m_BiasTensor); |
| 167 | } |
| 168 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 169 | void ClDepthwiseConvolutionWorkload::Execute() const |
| 170 | { |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 171 | ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClDepthwiseConvolutionWorkload_Execute", this->GetGuid()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 172 | ARMNN_ASSERT(m_DepthwiseConvolutionLayer); |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 173 | |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 174 | RunClFunction(*m_DepthwiseConvolutionLayer, CHECK_LOCATION()); |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 175 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 176 | |
| 177 | } // namespace armnn |