Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 1 | // |
Colm Donelan | b4ef163 | 2024-02-01 15:00:43 +0000 | [diff] [blame^] | 2 | // Copyright © 2017-2024 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); |
Cathal Corbett | 8bd5360 | 2022-05-12 15:54:58 +0100 | [diff] [blame] | 33 | arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weights, descriptor.m_DataLayout); |
| 34 | aclWeightsInfo.set_are_values_constant(weights.IsConstant()); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 35 | |
Jan Eilers | 4b961d3 | 2019-07-11 09:19:35 +0100 | [diff] [blame] | 36 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(descriptor.m_DilationX, |
| 37 | descriptor.m_DilationY); |
| 38 | |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 39 | arm_compute::TensorInfo aclBiasesInfo; |
| 40 | arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr; |
| 41 | |
| 42 | if (descriptor.m_BiasEnabled) |
| 43 | { |
Kevin May | 8eece0a | 2023-06-06 17:19:06 +0100 | [diff] [blame] | 44 | if (!biases.has_value()) |
TeresaARM | acedd85 | 2023-05-11 15:16:39 +0000 | [diff] [blame] | 45 | { |
| 46 | return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR, |
Kevin May | 8eece0a | 2023-06-06 17:19:06 +0100 | [diff] [blame] | 47 | "ArmNN ClConvolution2dWorkload has empty bias value."}; |
TeresaARM | acedd85 | 2023-05-11 15:16:39 +0000 | [diff] [blame] | 48 | } |
Teresa Charlin | 9edde5d | 2023-07-20 16:00:30 +0100 | [diff] [blame] | 49 | // There's currently a problem with non const bias, so we'll explicitly block it here. |
| 50 | if (!biases.value().IsConstant()) |
| 51 | { |
| 52 | return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR, |
| 53 | "ArmNN ClDepthwiseConv2dWorkload does not support non constant bias."}; |
| 54 | } |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 55 | aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout); |
Cathal Corbett | 8bd5360 | 2022-05-12 15:54:58 +0100 | [diff] [blame] | 56 | aclBiasesInfo.set_are_values_constant(biases.value().IsConstant()); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 57 | optionalAclBiasesInfo = &aclBiasesInfo; |
| 58 | } |
| 59 | |
| 60 | arm_compute::PadStrideInfo layerInfo = BuildArmComputePadStrideInfo(descriptor); |
| 61 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 62 | const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo( |
| 63 | activationDescriptor); |
| 64 | |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 65 | return arm_compute::CLConvolutionLayer::validate(&aclInputInfo, |
| 66 | &aclWeightsInfo, |
| 67 | optionalAclBiasesInfo, |
| 68 | &aclOutputInfo, |
Jan Eilers | 4b961d3 | 2019-07-11 09:19:35 +0100 | [diff] [blame] | 69 | layerInfo, |
| 70 | arm_compute::WeightsInfo(), |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 71 | aclDilationInfo, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 72 | activationInfo, |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 73 | isFastMathEnabled); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | ClConvolution2dWorkload::ClConvolution2dWorkload(const Convolution2dQueueDescriptor& descriptor, |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 77 | const WorkloadInfo& info, |
| 78 | std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager, |
Sadik Armagan | e944475 | 2020-12-02 11:28:58 +0000 | [diff] [blame] | 79 | const arm_compute::CLCompileContext& clCompileContext, |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 80 | const bool isFastMathEnabled) |
Teresa Charlin | 588cbdf | 2022-01-19 15:55:37 +0000 | [diff] [blame] | 81 | : ClBaseWorkload<Convolution2dQueueDescriptor>(descriptor, info) |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 82 | , m_ConvolutionLayer(memoryManager) |
| 83 | { |
Mike Kelly | 7cbe781 | 2023-07-25 17:37:33 +0100 | [diff] [blame] | 84 | ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClConvolution2dWorkload"); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 85 | |
Jan Eilers | 4b961d3 | 2019-07-11 09:19:35 +0100 | [diff] [blame] | 86 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(m_Data.m_Parameters.m_DilationX, |
| 87 | m_Data.m_Parameters.m_DilationY); |
| 88 | |
Cathal Corbett | 8bd5360 | 2022-05-12 15:54:58 +0100 | [diff] [blame] | 89 | uint32_t numInputs = m_Data.m_Parameters.m_BiasEnabled ? 3: 2; |
| 90 | m_Data.ValidateInputsOutputs("ClConvolution2dWorkload", numInputs, 1); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 91 | |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 92 | arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 93 | arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
Cathal Corbett | 8bd5360 | 2022-05-12 15:54:58 +0100 | [diff] [blame] | 94 | arm_compute::ICLTensor& weights = static_cast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor(); |
Teresa Charlin | ee1497c | 2023-03-30 13:56:34 +0100 | [diff] [blame] | 95 | weights.info()->set_are_values_constant(info.m_InputTensorInfos[1].IsConstant()); |
| 96 | |
Cathal Corbett | 8bd5360 | 2022-05-12 15:54:58 +0100 | [diff] [blame] | 97 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 98 | { |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 99 | arm_compute::ICLTensor& bias = static_cast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor(); |
Teresa Charlin | ee1497c | 2023-03-30 13:56:34 +0100 | [diff] [blame] | 100 | bias.info()->set_are_values_constant(info.m_InputTensorInfos[2].IsConstant()); |
Teresa Charlin | 9edde5d | 2023-07-20 16:00:30 +0100 | [diff] [blame] | 101 | // We assume here that NeonConvolution2dWorkloadValidate has been called before the constructor. |
Colm Donelan | b4ef163 | 2024-02-01 15:00:43 +0000 | [diff] [blame^] | 102 | ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(info.m_InputTensorInfos[2].IsConstant() == true, |
| 103 | "The bias tensor must be constant."); |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 104 | m_BiasProxy = std::make_unique<ICLTensorProxy>(&bias); |
Cathal Corbett | 8bd5360 | 2022-05-12 15:54:58 +0100 | [diff] [blame] | 105 | } |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 106 | |
Narumol Prangnawarat | e2af6f4 | 2022-01-28 17:59:18 +0000 | [diff] [blame] | 107 | // Create Proxy tensor and set the initial tensor handle to it |
| 108 | m_InputProxy = std::make_unique<ICLTensorProxy>(&input); |
| 109 | m_OutputProxy = std::make_unique<ICLTensorProxy>(&output); |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 110 | m_WeightsProxy = std::make_unique<ICLTensorProxy>(&weights); |
Cathal Corbett | 8bd5360 | 2022-05-12 15:54:58 +0100 | [diff] [blame] | 111 | |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 112 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
| 113 | input.info()->set_data_layout(aclDataLayout); |
| 114 | output.info()->set_data_layout(aclDataLayout); |
Cathal Corbett | 8bd5360 | 2022-05-12 15:54:58 +0100 | [diff] [blame] | 115 | weights.info()->set_data_layout(aclDataLayout); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 116 | |
Aron Virginas-Tar | 6f3785d | 2019-07-22 15:30:22 +0100 | [diff] [blame] | 117 | arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters); |
| 118 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 119 | const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor); |
| 120 | |
Kevin May | 4692e11 | 2021-10-18 14:41:50 +0100 | [diff] [blame] | 121 | { |
Mike Kelly | 7cbe781 | 2023-07-25 17:37:33 +0100 | [diff] [blame] | 122 | ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClConvolution2dWorkload_configure"); |
Kevin May | 4692e11 | 2021-10-18 14:41:50 +0100 | [diff] [blame] | 123 | m_ConvolutionLayer.configure(clCompileContext, |
Narumol Prangnawarat | e2af6f4 | 2022-01-28 17:59:18 +0000 | [diff] [blame] | 124 | m_InputProxy.get(), |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 125 | m_WeightsProxy.get(), |
| 126 | m_BiasProxy.get(), |
Narumol Prangnawarat | e2af6f4 | 2022-01-28 17:59:18 +0000 | [diff] [blame] | 127 | m_OutputProxy.get(), |
Kevin May | 4692e11 | 2021-10-18 14:41:50 +0100 | [diff] [blame] | 128 | padStrideInfo, |
| 129 | arm_compute::WeightsInfo(), |
| 130 | aclDilationInfo, |
| 131 | activationInfo, |
| 132 | isFastMathEnabled); |
| 133 | } |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 134 | |
| 135 | m_ConvolutionMethod = |
| 136 | m_ConvolutionLayer.get_convolution_method(input.info(), |
Cathal Corbett | 8bd5360 | 2022-05-12 15:54:58 +0100 | [diff] [blame] | 137 | weights.info(), |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 138 | output.info(), |
| 139 | padStrideInfo, |
| 140 | arm_compute::WeightsInfo(), |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 141 | activationInfo, |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 142 | arm_compute::CLScheduler::get().target(), |
| 143 | aclDilationInfo, |
| 144 | isFastMathEnabled); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 145 | |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 146 | // Add details for profiling output |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 147 | WorkloadInfo detailsInfo; |
| 148 | |
| 149 | detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos; |
| 150 | detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos; |
Keith Davis | 5a64f22 | 2021-08-04 10:35:20 +0100 | [diff] [blame] | 151 | detailsInfo.m_ConvolutionMethod = armnn::Optional<std::string>(GetConvolutionMethodString(m_ConvolutionMethod)); |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 152 | |
| 153 | // Report Profiling Details |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 154 | ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClConvolution2dWorkload_Construct", |
Keith Davis | 5a64f22 | 2021-08-04 10:35:20 +0100 | [diff] [blame] | 155 | descriptor.m_Parameters, |
| 156 | detailsInfo, |
Cathal Corbett | 8bd5360 | 2022-05-12 15:54:58 +0100 | [diff] [blame] | 157 | GetGuid()); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void ClConvolution2dWorkload::Execute() const |
| 161 | { |
Mike Kelly | 7cbe781 | 2023-07-25 17:37:33 +0100 | [diff] [blame] | 162 | ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClConvolution2dWorkload_Execute"); |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 163 | RunClFunction(m_ConvolutionLayer, CHECK_LOCATION()); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 166 | arm_compute::ConvolutionMethod ClConvolution2dWorkload::GetConvolutionMethod() const |
| 167 | { |
| 168 | return m_ConvolutionMethod; |
| 169 | } |
| 170 | |
Narumol Prangnawarat | e2af6f4 | 2022-01-28 17:59:18 +0000 | [diff] [blame] | 171 | void ClConvolution2dWorkload::Reconfigure() |
| 172 | { |
Narumol Prangnawarat | e2af6f4 | 2022-01-28 17:59:18 +0000 | [diff] [blame] | 173 | arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 174 | 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] | 175 | |
Narumol Prangnawarat | e2af6f4 | 2022-01-28 17:59:18 +0000 | [diff] [blame] | 176 | m_InputProxy->set(&input); |
| 177 | m_OutputProxy->set(&output); |
| 178 | } |
| 179 | |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 180 | } //namespace armnn |