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