telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [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 | 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> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 15 | #include <armnn/backends/TensorHandle.hpp> |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 16 | #include <backendsCommon/WorkloadUtils.hpp> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 17 | #include <armnn/backends/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 | |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame^] | 36 | // ArmNN format for weights for depthwise is [1, H, W, C] independently of the input/output layout |
| 37 | // |
| 38 | // ACL format for weights for depthwise is: |
| 39 | // - [1, H, W, C] for [N, H, W, C] input/output layout (matches with ArmNN) |
| 40 | // - [1, C, H, W] for [N, C, H, W] input/output layout |
| 41 | // |
| 42 | // Therefore ArmNN weights have to be permuted when input/output layout is [N, C, H, W] to pass them to ACL. |
| 43 | // The PermuteDepthwiseConv2dWeights backend optimization takes care of this, but it has not been performed yet, |
| 44 | // so we do the permute here for the TensorInfo weights. |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 45 | unsigned int aclDepthMultiplier; |
| 46 | TensorInfo weightsPermuted; |
| 47 | std::tie(weightsPermuted, aclDepthMultiplier) = Convert1HWOTensorInfoToAcl(weights, input,descriptor.m_DataLayout); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 48 | |
| 49 | // Convert the weights into the compute library format |
| 50 | const arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weightsPermuted, descriptor.m_DataLayout); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 51 | |
| 52 | arm_compute::TensorInfo aclBiasesInfo; |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame^] | 53 | arm_compute::TensorInfo* optionalAclBiasesInfo = nullptr; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 54 | if (descriptor.m_BiasEnabled) |
| 55 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 56 | ARMNN_ASSERT(biases.has_value()); |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 57 | aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 58 | optionalAclBiasesInfo = &aclBiasesInfo; |
| 59 | } |
| 60 | |
| 61 | const arm_compute::PadStrideInfo aclPadStrideInfo = BuildArmComputePadStrideInfo(descriptor); |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 62 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D( |
| 63 | descriptor.m_DilationX, |
| 64 | descriptor.m_DilationY); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 65 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 66 | const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo( |
| 67 | activationDescriptor); |
| 68 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 69 | return arm_compute::CLDepthwiseConvolutionLayer::validate(&aclInputInfo, |
| 70 | &aclWeightsInfo, |
| 71 | optionalAclBiasesInfo, |
| 72 | &aclOutputInfo, |
| 73 | aclPadStrideInfo, |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 74 | aclDepthMultiplier, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 75 | activationInfo, |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 76 | aclDilationInfo); |
| 77 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 80 | ClDepthwiseConvolutionWorkload::ClDepthwiseConvolutionWorkload( |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 81 | const DepthwiseConvolution2dQueueDescriptor& descriptor, |
Sadik Armagan | e944475 | 2020-12-02 11:28:58 +0000 | [diff] [blame] | 82 | const WorkloadInfo& info, |
| 83 | const arm_compute::CLCompileContext& clCompileContext) |
Teresa Charlin | 588cbdf | 2022-01-19 15:55:37 +0000 | [diff] [blame] | 84 | : ClBaseWorkload<DepthwiseConvolution2dQueueDescriptor>(descriptor, info) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 85 | { |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 86 | // Add details for profiling output |
| 87 | WorkloadInfo detailsInfo; |
| 88 | |
| 89 | detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos; |
| 90 | detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos; |
| 91 | detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Weight->GetTensorInfo()); |
| 92 | if (descriptor.m_Parameters.m_BiasEnabled) |
| 93 | { |
| 94 | detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Bias->GetTensorInfo()); |
| 95 | } |
| 96 | |
| 97 | // Report Profiling Details |
| 98 | ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClDepthwiseConvolutionWorkload_Construct", |
| 99 | descriptor.m_Parameters, |
| 100 | detailsInfo, |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame^] | 101 | GetGuid()); |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 102 | |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame^] | 103 | m_Data.ValidateInputsOutputs("ClDepthwiseConv2dWorkload", descriptor.m_Parameters.GetNumInputs(), 1); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 104 | |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame^] | 105 | arm_compute::ICLTensor& input = PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 106 | arm_compute::ICLTensor& output = PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
| 107 | arm_compute::ICLTensor& weights = PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor(); |
| 108 | arm_compute::ITensorInfo* weightsInfo = weights.info(); |
| 109 | arm_compute::ITensorInfo* inputInfo = input.info(); |
| 110 | auto weightsShape = weightsInfo->tensor_shape(); |
| 111 | auto inputShape = inputInfo->tensor_shape(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 112 | |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame^] | 113 | // The PermuteDepthwiseConv2dWeights backend optimization has been performed, |
| 114 | // converting weights to have the same data layout as input. |
| 115 | unsigned int depthMultiplier = |
| 116 | ComputeDepthwiseConv2dDepthMultiplier(m_Data.m_Parameters.m_DataLayout, weightsShape, inputShape); |
| 117 | |
| 118 | arm_compute::ICLTensor* bias = nullptr; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 119 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 120 | { |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame^] | 121 | bias = &PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 124 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D( |
| 125 | m_Data.m_Parameters.m_DilationX, |
| 126 | m_Data.m_Parameters.m_DilationY); |
| 127 | |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 128 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
| 129 | input.info()->set_data_layout(aclDataLayout); |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame^] | 130 | weights.info()->set_data_layout(aclDataLayout); |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 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>(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 138 | |
Kevin May | 9f6862d | 2021-10-22 15:42:28 +0100 | [diff] [blame] | 139 | { |
| 140 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClDepthwiseConvolutionWorkload_configure"); |
| 141 | static_cast<arm_compute::CLDepthwiseConvolutionLayer*>(m_DepthwiseConvolutionLayer.get())->configure( |
| 142 | clCompileContext, |
| 143 | &input, |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame^] | 144 | &weights, |
| 145 | bias, |
Kevin May | 9f6862d | 2021-10-22 15:42:28 +0100 | [diff] [blame] | 146 | &output, |
| 147 | padStrideInfo, |
| 148 | depthMultiplier, |
| 149 | activationInfo, |
| 150 | aclDilationInfo); |
| 151 | } |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 152 | ARMNN_ASSERT(m_DepthwiseConvolutionLayer); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 155 | void ClDepthwiseConvolutionWorkload::Execute() const |
| 156 | { |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame^] | 157 | ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClDepthwiseConvolutionWorkload_Execute", GetGuid()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 158 | ARMNN_ASSERT(m_DepthwiseConvolutionLayer); |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 159 | |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 160 | RunClFunction(*m_DepthwiseConvolutionLayer, CHECK_LOCATION()); |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 161 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 162 | |
| 163 | } // namespace armnn |