blob: 7ae09e3eef076428058f0c5093b946e413f5b5c9 [file] [log] [blame]
Matthew Benthamd8067922018-10-03 17:18:04 +01001//
Colm Donelanb4ef1632024-02-01 15:00:43 +00002// Copyright © 2017-2024 Arm Ltd and Contributors. All rights reserved.
Matthew Benthamd8067922018-10-03 17:18:04 +01003// SPDX-License-Identifier: MIT
4//
5
6#include "ClConvolution2dWorkload.hpp"
7
8#include "ClWorkloadUtils.hpp"
9
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +000010#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 Donelan0c479742021-12-10 12:43:54 +000015#include <armnn/backends/TensorHandle.hpp>
Matthew Benthamd8067922018-10-03 17:18:04 +010016
17#include <arm_compute/runtime/CL/functions/CLConvolutionLayer.h>
18
19namespace armnn
20{
21using namespace armcomputetensorutils;
22
23arm_compute::Status ClConvolution2dWorkloadValidate(const TensorInfo& input,
24 const TensorInfo& output,
25 const Convolution2dDescriptor& descriptor,
26 const TensorInfo& weights,
Sadik Armagan045f6be2020-09-10 13:37:32 +010027 const Optional<TensorInfo>& biases,
Mike Kelly07810fc2020-11-12 10:58:48 +000028 bool isFastMathEnabled,
29 const ActivationDescriptor* activationDescriptor)
Matthew Benthamd8067922018-10-03 17:18:04 +010030{
31 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
32 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
Cathal Corbett8bd53602022-05-12 15:54:58 +010033 arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weights, descriptor.m_DataLayout);
34 aclWeightsInfo.set_are_values_constant(weights.IsConstant());
Matthew Benthamd8067922018-10-03 17:18:04 +010035
Jan Eilers4b961d32019-07-11 09:19:35 +010036 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(descriptor.m_DilationX,
37 descriptor.m_DilationY);
38
Matthew Benthamd8067922018-10-03 17:18:04 +010039 arm_compute::TensorInfo aclBiasesInfo;
40 arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr;
41
42 if (descriptor.m_BiasEnabled)
43 {
Kevin May8eece0a2023-06-06 17:19:06 +010044 if (!biases.has_value())
TeresaARMacedd852023-05-11 15:16:39 +000045 {
46 return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
Kevin May8eece0a2023-06-06 17:19:06 +010047 "ArmNN ClConvolution2dWorkload has empty bias value."};
TeresaARMacedd852023-05-11 15:16:39 +000048 }
Teresa Charlin9edde5d2023-07-20 16:00:30 +010049 // 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 Beck5eec11d2018-10-04 15:43:17 +010055 aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
Cathal Corbett8bd53602022-05-12 15:54:58 +010056 aclBiasesInfo.set_are_values_constant(biases.value().IsConstant());
Matthew Benthamd8067922018-10-03 17:18:04 +010057 optionalAclBiasesInfo = &aclBiasesInfo;
58 }
59
60 arm_compute::PadStrideInfo layerInfo = BuildArmComputePadStrideInfo(descriptor);
61
Mike Kelly07810fc2020-11-12 10:58:48 +000062 const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
63 activationDescriptor);
64
Matthew Benthamd8067922018-10-03 17:18:04 +010065 return arm_compute::CLConvolutionLayer::validate(&aclInputInfo,
66 &aclWeightsInfo,
67 optionalAclBiasesInfo,
68 &aclOutputInfo,
Jan Eilers4b961d32019-07-11 09:19:35 +010069 layerInfo,
70 arm_compute::WeightsInfo(),
Sadik Armagan045f6be2020-09-10 13:37:32 +010071 aclDilationInfo,
Mike Kelly07810fc2020-11-12 10:58:48 +000072 activationInfo,
Sadik Armagan045f6be2020-09-10 13:37:32 +010073 isFastMathEnabled);
Matthew Benthamd8067922018-10-03 17:18:04 +010074}
75
76ClConvolution2dWorkload::ClConvolution2dWorkload(const Convolution2dQueueDescriptor& descriptor,
Sadik Armagan04a72972020-09-14 15:44:18 +010077 const WorkloadInfo& info,
78 std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager,
Sadik Armagane9444752020-12-02 11:28:58 +000079 const arm_compute::CLCompileContext& clCompileContext,
Sadik Armagan04a72972020-09-14 15:44:18 +010080 const bool isFastMathEnabled)
Teresa Charlin588cbdf2022-01-19 15:55:37 +000081 : ClBaseWorkload<Convolution2dQueueDescriptor>(descriptor, info)
Matthew Benthamd8067922018-10-03 17:18:04 +010082 , m_ConvolutionLayer(memoryManager)
83{
Mike Kelly7cbe7812023-07-25 17:37:33 +010084 ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClConvolution2dWorkload");
Matthew Benthamd8067922018-10-03 17:18:04 +010085
Jan Eilers4b961d32019-07-11 09:19:35 +010086 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(m_Data.m_Parameters.m_DilationX,
87 m_Data.m_Parameters.m_DilationY);
88
Cathal Corbett8bd53602022-05-12 15:54:58 +010089 uint32_t numInputs = m_Data.m_Parameters.m_BiasEnabled ? 3: 2;
90 m_Data.ValidateInputsOutputs("ClConvolution2dWorkload", numInputs, 1);
Matthew Benthamd8067922018-10-03 17:18:04 +010091
Matthew Benthamd8067922018-10-03 17:18:04 +010092 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 Corbett8bd53602022-05-12 15:54:58 +010094 arm_compute::ICLTensor& weights = static_cast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
Teresa Charlinee1497c2023-03-30 13:56:34 +010095 weights.info()->set_are_values_constant(info.m_InputTensorInfos[1].IsConstant());
96
Cathal Corbett8bd53602022-05-12 15:54:58 +010097 if (m_Data.m_Parameters.m_BiasEnabled)
98 {
Mike Kellyec67a0f2022-11-25 13:55:24 +000099 arm_compute::ICLTensor& bias = static_cast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor();
Teresa Charlinee1497c2023-03-30 13:56:34 +0100100 bias.info()->set_are_values_constant(info.m_InputTensorInfos[2].IsConstant());
Teresa Charlin9edde5d2023-07-20 16:00:30 +0100101 // We assume here that NeonConvolution2dWorkloadValidate has been called before the constructor.
Colm Donelanb4ef1632024-02-01 15:00:43 +0000102 ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(info.m_InputTensorInfos[2].IsConstant() == true,
103 "The bias tensor must be constant.");
Mike Kellyec67a0f2022-11-25 13:55:24 +0000104 m_BiasProxy = std::make_unique<ICLTensorProxy>(&bias);
Cathal Corbett8bd53602022-05-12 15:54:58 +0100105 }
Matthew Benthamd8067922018-10-03 17:18:04 +0100106
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000107 // 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 Kellyec67a0f2022-11-25 13:55:24 +0000110 m_WeightsProxy = std::make_unique<ICLTensorProxy>(&weights);
Cathal Corbett8bd53602022-05-12 15:54:58 +0100111
Matthew Benthamd8067922018-10-03 17:18:04 +0100112 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 Corbett8bd53602022-05-12 15:54:58 +0100115 weights.info()->set_data_layout(aclDataLayout);
Matthew Benthamd8067922018-10-03 17:18:04 +0100116
Aron Virginas-Tar6f3785d2019-07-22 15:30:22 +0100117 arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
118
Mike Kelly07810fc2020-11-12 10:58:48 +0000119 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
120
Kevin May4692e112021-10-18 14:41:50 +0100121 {
Mike Kelly7cbe7812023-07-25 17:37:33 +0100122 ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClConvolution2dWorkload_configure");
Kevin May4692e112021-10-18 14:41:50 +0100123 m_ConvolutionLayer.configure(clCompileContext,
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000124 m_InputProxy.get(),
Mike Kellyec67a0f2022-11-25 13:55:24 +0000125 m_WeightsProxy.get(),
126 m_BiasProxy.get(),
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000127 m_OutputProxy.get(),
Kevin May4692e112021-10-18 14:41:50 +0100128 padStrideInfo,
129 arm_compute::WeightsInfo(),
130 aclDilationInfo,
131 activationInfo,
132 isFastMathEnabled);
133 }
Sadik Armagan04a72972020-09-14 15:44:18 +0100134
135 m_ConvolutionMethod =
136 m_ConvolutionLayer.get_convolution_method(input.info(),
Cathal Corbett8bd53602022-05-12 15:54:58 +0100137 weights.info(),
Sadik Armagan04a72972020-09-14 15:44:18 +0100138 output.info(),
139 padStrideInfo,
140 arm_compute::WeightsInfo(),
Mike Kelly07810fc2020-11-12 10:58:48 +0000141 activationInfo,
Sadik Armagan04a72972020-09-14 15:44:18 +0100142 arm_compute::CLScheduler::get().target(),
143 aclDilationInfo,
144 isFastMathEnabled);
Matthew Benthamd8067922018-10-03 17:18:04 +0100145
Keith Davis554fa092021-07-20 11:25:22 +0100146 // Add details for profiling output
Keith Davis554fa092021-07-20 11:25:22 +0100147 WorkloadInfo detailsInfo;
148
149 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
150 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
Keith Davis5a64f222021-08-04 10:35:20 +0100151 detailsInfo.m_ConvolutionMethod = armnn::Optional<std::string>(GetConvolutionMethodString(m_ConvolutionMethod));
Keith Davis554fa092021-07-20 11:25:22 +0100152
153 // Report Profiling Details
Keith Davisbcd860a2021-08-05 14:20:33 +0100154 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClConvolution2dWorkload_Construct",
Keith Davis5a64f222021-08-04 10:35:20 +0100155 descriptor.m_Parameters,
156 detailsInfo,
Cathal Corbett8bd53602022-05-12 15:54:58 +0100157 GetGuid());
Matthew Benthamd8067922018-10-03 17:18:04 +0100158}
159
160void ClConvolution2dWorkload::Execute() const
161{
Mike Kelly7cbe7812023-07-25 17:37:33 +0100162 ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClConvolution2dWorkload_Execute");
Aron Virginas-Tara8e06ed2018-10-19 16:46:15 +0100163 RunClFunction(m_ConvolutionLayer, CHECK_LOCATION());
Matthew Benthamd8067922018-10-03 17:18:04 +0100164}
165
Sadik Armagan04a72972020-09-14 15:44:18 +0100166arm_compute::ConvolutionMethod ClConvolution2dWorkload::GetConvolutionMethod() const
167{
168 return m_ConvolutionMethod;
169}
170
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000171void ClConvolution2dWorkload::Reconfigure()
172{
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000173 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 Monahanec819992022-02-10 14:47:13 +0000175
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000176 m_InputProxy->set(&input);
177 m_OutputProxy->set(&output);
178}
179
Matthew Benthamd8067922018-10-03 17:18:04 +0100180} //namespace armnn