Matthew Bentham | d806792 | 2018-10-03 17:18:04 +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. |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ClConvolution2dWorkload.hpp" |
| 7 | |
| 8 | #include "ClWorkloadUtils.hpp" |
| 9 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 10 | #include <cl/ClLayerSupport.hpp> |
| 11 | #include <cl/ClTensorHandle.hpp> |
| 12 | #include <cl/ClLayerSupport.hpp> |
| 13 | #include <aclCommon/ArmComputeUtils.hpp> |
| 14 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 15 | #include <armnn/backends/TensorHandle.hpp> |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 16 | |
| 17 | #include <arm_compute/runtime/CL/functions/CLConvolutionLayer.h> |
| 18 | |
| 19 | namespace armnn |
| 20 | { |
| 21 | using namespace armcomputetensorutils; |
| 22 | |
| 23 | arm_compute::Status ClConvolution2dWorkloadValidate(const TensorInfo& input, |
| 24 | const TensorInfo& output, |
| 25 | const Convolution2dDescriptor& descriptor, |
| 26 | const TensorInfo& weights, |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 27 | const Optional<TensorInfo>& biases, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 28 | bool isFastMathEnabled, |
| 29 | const ActivationDescriptor* activationDescriptor) |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 30 | { |
| 31 | const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout); |
| 32 | const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout); |
| 33 | const arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weights, descriptor.m_DataLayout); |
| 34 | |
Jan Eilers | 4b961d3 | 2019-07-11 09:19:35 +0100 | [diff] [blame] | 35 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(descriptor.m_DilationX, |
| 36 | descriptor.m_DilationY); |
| 37 | |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 38 | arm_compute::TensorInfo aclBiasesInfo; |
| 39 | arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr; |
| 40 | |
| 41 | if (descriptor.m_BiasEnabled) |
| 42 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 43 | ARMNN_ASSERT(biases.has_value()); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 44 | |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 45 | aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 46 | optionalAclBiasesInfo = &aclBiasesInfo; |
| 47 | } |
| 48 | |
| 49 | arm_compute::PadStrideInfo layerInfo = BuildArmComputePadStrideInfo(descriptor); |
| 50 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 51 | const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo( |
| 52 | activationDescriptor); |
| 53 | |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 54 | return arm_compute::CLConvolutionLayer::validate(&aclInputInfo, |
| 55 | &aclWeightsInfo, |
| 56 | optionalAclBiasesInfo, |
| 57 | &aclOutputInfo, |
Jan Eilers | 4b961d3 | 2019-07-11 09:19:35 +0100 | [diff] [blame] | 58 | layerInfo, |
| 59 | arm_compute::WeightsInfo(), |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 60 | aclDilationInfo, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 61 | activationInfo, |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 62 | isFastMathEnabled); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | ClConvolution2dWorkload::ClConvolution2dWorkload(const Convolution2dQueueDescriptor& descriptor, |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 66 | const WorkloadInfo& info, |
| 67 | std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager, |
Sadik Armagan | e944475 | 2020-12-02 11:28:58 +0000 | [diff] [blame] | 68 | const arm_compute::CLCompileContext& clCompileContext, |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 69 | const bool isFastMathEnabled) |
Teresa Charlin | 588cbdf | 2022-01-19 15:55:37 +0000 | [diff] [blame] | 70 | : ClBaseWorkload<Convolution2dQueueDescriptor>(descriptor, info) |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 71 | , m_ConvolutionLayer(memoryManager) |
| 72 | { |
Kevin May | 4692e11 | 2021-10-18 14:41:50 +0100 | [diff] [blame] | 73 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload"); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 74 | const TensorInfo& weightInfo = m_Data.m_Weight->GetTensorInfo(); |
| 75 | |
| 76 | m_KernelTensor = std::make_unique<arm_compute::CLTensor>(); |
| 77 | BuildArmComputeTensor(*m_KernelTensor, weightInfo, m_Data.m_Parameters.m_DataLayout); |
| 78 | |
Jan Eilers | 4b961d3 | 2019-07-11 09:19:35 +0100 | [diff] [blame] | 79 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(m_Data.m_Parameters.m_DilationX, |
| 80 | m_Data.m_Parameters.m_DilationY); |
| 81 | |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 82 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 83 | { |
| 84 | m_BiasTensor = std::make_unique<arm_compute::CLTensor>(); |
| 85 | BuildArmComputeTensor(*m_BiasTensor, m_Data.m_Bias->GetTensorInfo(), m_Data.m_Parameters.m_DataLayout); |
| 86 | } |
| 87 | |
| 88 | m_Data.ValidateInputsOutputs("ClConvolution2dWorkload", 1, 1); |
| 89 | |
| 90 | arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 91 | arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
| 92 | |
Narumol Prangnawarat | e2af6f4 | 2022-01-28 17:59:18 +0000 | [diff] [blame] | 93 | // Create Proxy tensor and set the initial tensor handle to it |
| 94 | m_InputProxy = std::make_unique<ICLTensorProxy>(&input); |
| 95 | m_OutputProxy = std::make_unique<ICLTensorProxy>(&output); |
| 96 | |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 97 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
| 98 | input.info()->set_data_layout(aclDataLayout); |
| 99 | output.info()->set_data_layout(aclDataLayout); |
| 100 | |
Aron Virginas-Tar | 6f3785d | 2019-07-22 15:30:22 +0100 | [diff] [blame] | 101 | arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters); |
| 102 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 103 | const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor); |
| 104 | |
Kevin May | 4692e11 | 2021-10-18 14:41:50 +0100 | [diff] [blame] | 105 | { |
| 106 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload_configure"); |
| 107 | m_ConvolutionLayer.configure(clCompileContext, |
Narumol Prangnawarat | e2af6f4 | 2022-01-28 17:59:18 +0000 | [diff] [blame] | 108 | m_InputProxy.get(), |
Kevin May | 4692e11 | 2021-10-18 14:41:50 +0100 | [diff] [blame] | 109 | m_KernelTensor.get(), |
| 110 | m_BiasTensor.get(), |
Narumol Prangnawarat | e2af6f4 | 2022-01-28 17:59:18 +0000 | [diff] [blame] | 111 | m_OutputProxy.get(), |
Kevin May | 4692e11 | 2021-10-18 14:41:50 +0100 | [diff] [blame] | 112 | padStrideInfo, |
| 113 | arm_compute::WeightsInfo(), |
| 114 | aclDilationInfo, |
| 115 | activationInfo, |
| 116 | isFastMathEnabled); |
| 117 | } |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 118 | |
| 119 | m_ConvolutionMethod = |
| 120 | m_ConvolutionLayer.get_convolution_method(input.info(), |
| 121 | m_KernelTensor->info(), |
| 122 | output.info(), |
| 123 | padStrideInfo, |
| 124 | arm_compute::WeightsInfo(), |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 125 | activationInfo, |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 126 | arm_compute::CLScheduler::get().target(), |
| 127 | aclDilationInfo, |
| 128 | isFastMathEnabled); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 129 | |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 130 | // Add details for profiling output |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 131 | WorkloadInfo detailsInfo; |
| 132 | |
| 133 | detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos; |
| 134 | detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos; |
| 135 | detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Weight->GetTensorInfo()); |
Keith Davis | 5a64f22 | 2021-08-04 10:35:20 +0100 | [diff] [blame] | 136 | detailsInfo.m_ConvolutionMethod = armnn::Optional<std::string>(GetConvolutionMethodString(m_ConvolutionMethod)); |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 137 | if (descriptor.m_Parameters.m_BiasEnabled) |
| 138 | { |
| 139 | detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Bias->GetTensorInfo()); |
| 140 | } |
| 141 | |
| 142 | // Report Profiling Details |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 143 | ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClConvolution2dWorkload_Construct", |
Keith Davis | 5a64f22 | 2021-08-04 10:35:20 +0100 | [diff] [blame] | 144 | descriptor.m_Parameters, |
| 145 | detailsInfo, |
| 146 | this->GetGuid()); |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 147 | |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 148 | InitializeArmComputeClTensorData(*m_KernelTensor, m_Data.m_Weight); |
| 149 | |
| 150 | if (m_BiasTensor) |
| 151 | { |
| 152 | InitializeArmComputeClTensorData(*m_BiasTensor, m_Data.m_Bias); |
| 153 | } |
| 154 | |
| 155 | // Force Compute Library to perform the necessary copying and reshaping, after which |
| 156 | // delete all the input tensors that will no longer be needed |
Kevin May | 4692e11 | 2021-10-18 14:41:50 +0100 | [diff] [blame] | 157 | { |
| 158 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload_prepare"); |
| 159 | m_ConvolutionLayer.prepare(); |
| 160 | } |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 161 | FreeUnusedTensors(); |
| 162 | } |
| 163 | |
| 164 | void ClConvolution2dWorkload::Execute() const |
| 165 | { |
Keith Davis | 5a64f22 | 2021-08-04 10:35:20 +0100 | [diff] [blame] | 166 | ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClConvolution2dWorkload_Execute", this->GetGuid()); |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 167 | RunClFunction(m_ConvolutionLayer, CHECK_LOCATION()); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 170 | arm_compute::ConvolutionMethod ClConvolution2dWorkload::GetConvolutionMethod() const |
| 171 | { |
| 172 | return m_ConvolutionMethod; |
| 173 | } |
| 174 | |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 175 | void ClConvolution2dWorkload::FreeUnusedTensors() |
| 176 | { |
| 177 | FreeTensorIfUnused(m_KernelTensor); |
| 178 | FreeTensorIfUnused(m_BiasTensor); |
| 179 | } |
| 180 | |
Narumol Prangnawarat | e2af6f4 | 2022-01-28 17:59:18 +0000 | [diff] [blame] | 181 | void ClConvolution2dWorkload::Reconfigure() |
| 182 | { |
Narumol Prangnawarat | e2af6f4 | 2022-01-28 17:59:18 +0000 | [diff] [blame] | 183 | arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 184 | arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
David Monahan | ec81999 | 2022-02-10 14:47:13 +0000 | [diff] [blame] | 185 | |
Narumol Prangnawarat | e2af6f4 | 2022-01-28 17:59:18 +0000 | [diff] [blame] | 186 | m_InputProxy->set(&input); |
| 187 | m_OutputProxy->set(&output); |
| 188 | } |
| 189 | |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 190 | } //namespace armnn |