blob: 2fc174cfc2c41d6479ef4f89da6ad30a5605c011 [file] [log] [blame]
Matthew Benthamd8067922018-10-03 17:18:04 +01001//
Mike Kelly7cbe7812023-07-25 17:37:33 +01002// Copyright © 2017-2023 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.
102 ARMNN_ASSERT(info.m_InputTensorInfos[2].IsConstant() == true);
Mike Kellyec67a0f2022-11-25 13:55:24 +0000103 m_BiasProxy = std::make_unique<ICLTensorProxy>(&bias);
Cathal Corbett8bd53602022-05-12 15:54:58 +0100104 }
Matthew Benthamd8067922018-10-03 17:18:04 +0100105
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000106 // Create Proxy tensor and set the initial tensor handle to it
107 m_InputProxy = std::make_unique<ICLTensorProxy>(&input);
108 m_OutputProxy = std::make_unique<ICLTensorProxy>(&output);
Mike Kellyec67a0f2022-11-25 13:55:24 +0000109 m_WeightsProxy = std::make_unique<ICLTensorProxy>(&weights);
Cathal Corbett8bd53602022-05-12 15:54:58 +0100110
Matthew Benthamd8067922018-10-03 17:18:04 +0100111 arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
112 input.info()->set_data_layout(aclDataLayout);
113 output.info()->set_data_layout(aclDataLayout);
Cathal Corbett8bd53602022-05-12 15:54:58 +0100114 weights.info()->set_data_layout(aclDataLayout);
Matthew Benthamd8067922018-10-03 17:18:04 +0100115
Aron Virginas-Tar6f3785d2019-07-22 15:30:22 +0100116 arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
117
Mike Kelly07810fc2020-11-12 10:58:48 +0000118 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
119
Kevin May4692e112021-10-18 14:41:50 +0100120 {
Mike Kelly7cbe7812023-07-25 17:37:33 +0100121 ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClConvolution2dWorkload_configure");
Kevin May4692e112021-10-18 14:41:50 +0100122 m_ConvolutionLayer.configure(clCompileContext,
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000123 m_InputProxy.get(),
Mike Kellyec67a0f2022-11-25 13:55:24 +0000124 m_WeightsProxy.get(),
125 m_BiasProxy.get(),
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000126 m_OutputProxy.get(),
Kevin May4692e112021-10-18 14:41:50 +0100127 padStrideInfo,
128 arm_compute::WeightsInfo(),
129 aclDilationInfo,
130 activationInfo,
131 isFastMathEnabled);
132 }
Sadik Armagan04a72972020-09-14 15:44:18 +0100133
134 m_ConvolutionMethod =
135 m_ConvolutionLayer.get_convolution_method(input.info(),
Cathal Corbett8bd53602022-05-12 15:54:58 +0100136 weights.info(),
Sadik Armagan04a72972020-09-14 15:44:18 +0100137 output.info(),
138 padStrideInfo,
139 arm_compute::WeightsInfo(),
Mike Kelly07810fc2020-11-12 10:58:48 +0000140 activationInfo,
Sadik Armagan04a72972020-09-14 15:44:18 +0100141 arm_compute::CLScheduler::get().target(),
142 aclDilationInfo,
143 isFastMathEnabled);
Matthew Benthamd8067922018-10-03 17:18:04 +0100144
Keith Davis554fa092021-07-20 11:25:22 +0100145 // Add details for profiling output
Keith Davis554fa092021-07-20 11:25:22 +0100146 WorkloadInfo detailsInfo;
147
148 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
149 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
Keith Davis5a64f222021-08-04 10:35:20 +0100150 detailsInfo.m_ConvolutionMethod = armnn::Optional<std::string>(GetConvolutionMethodString(m_ConvolutionMethod));
Keith Davis554fa092021-07-20 11:25:22 +0100151
152 // Report Profiling Details
Keith Davisbcd860a2021-08-05 14:20:33 +0100153 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClConvolution2dWorkload_Construct",
Keith Davis5a64f222021-08-04 10:35:20 +0100154 descriptor.m_Parameters,
155 detailsInfo,
Cathal Corbett8bd53602022-05-12 15:54:58 +0100156 GetGuid());
Matthew Benthamd8067922018-10-03 17:18:04 +0100157}
158
159void ClConvolution2dWorkload::Execute() const
160{
Mike Kelly7cbe7812023-07-25 17:37:33 +0100161 ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClConvolution2dWorkload_Execute");
Aron Virginas-Tara8e06ed2018-10-19 16:46:15 +0100162 RunClFunction(m_ConvolutionLayer, CHECK_LOCATION());
Matthew Benthamd8067922018-10-03 17:18:04 +0100163}
164
Sadik Armagan04a72972020-09-14 15:44:18 +0100165arm_compute::ConvolutionMethod ClConvolution2dWorkload::GetConvolutionMethod() const
166{
167 return m_ConvolutionMethod;
168}
169
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000170void ClConvolution2dWorkload::Reconfigure()
171{
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000172 arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
173 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
David Monahanec819992022-02-10 14:47:13 +0000174
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000175 m_InputProxy->set(&input);
176 m_OutputProxy->set(&output);
177}
178
Matthew Benthamd8067922018-10-03 17:18:04 +0100179} //namespace armnn