blob: d6a72e6488616be8492baf478c3210a4c83fab1f [file] [log] [blame]
Matthew Benthamd8067922018-10-03 17:18:04 +01001//
Teresa Charlinee1497c2023-03-30 13:56:34 +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 {
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010044 ARMNN_ASSERT(biases.has_value());
TeresaARMacedd852023-05-11 15:16:39 +000045 // 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 Beck5eec11d2018-10-04 15:43:17 +010051 aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
Cathal Corbett8bd53602022-05-12 15:54:58 +010052 aclBiasesInfo.set_are_values_constant(biases.value().IsConstant());
Matthew Benthamd8067922018-10-03 17:18:04 +010053 optionalAclBiasesInfo = &aclBiasesInfo;
54 }
55
56 arm_compute::PadStrideInfo layerInfo = BuildArmComputePadStrideInfo(descriptor);
57
Mike Kelly07810fc2020-11-12 10:58:48 +000058 const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
59 activationDescriptor);
60
Matthew Benthamd8067922018-10-03 17:18:04 +010061 return arm_compute::CLConvolutionLayer::validate(&aclInputInfo,
62 &aclWeightsInfo,
63 optionalAclBiasesInfo,
64 &aclOutputInfo,
Jan Eilers4b961d32019-07-11 09:19:35 +010065 layerInfo,
66 arm_compute::WeightsInfo(),
Sadik Armagan045f6be2020-09-10 13:37:32 +010067 aclDilationInfo,
Mike Kelly07810fc2020-11-12 10:58:48 +000068 activationInfo,
Sadik Armagan045f6be2020-09-10 13:37:32 +010069 isFastMathEnabled);
Matthew Benthamd8067922018-10-03 17:18:04 +010070}
71
72ClConvolution2dWorkload::ClConvolution2dWorkload(const Convolution2dQueueDescriptor& descriptor,
Sadik Armagan04a72972020-09-14 15:44:18 +010073 const WorkloadInfo& info,
74 std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager,
Sadik Armagane9444752020-12-02 11:28:58 +000075 const arm_compute::CLCompileContext& clCompileContext,
Sadik Armagan04a72972020-09-14 15:44:18 +010076 const bool isFastMathEnabled)
Teresa Charlin588cbdf2022-01-19 15:55:37 +000077 : ClBaseWorkload<Convolution2dQueueDescriptor>(descriptor, info)
Matthew Benthamd8067922018-10-03 17:18:04 +010078 , m_ConvolutionLayer(memoryManager)
79{
Kevin May4692e112021-10-18 14:41:50 +010080 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload");
Matthew Benthamd8067922018-10-03 17:18:04 +010081
Jan Eilers4b961d32019-07-11 09:19:35 +010082 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(m_Data.m_Parameters.m_DilationX,
83 m_Data.m_Parameters.m_DilationY);
84
Cathal Corbett8bd53602022-05-12 15:54:58 +010085 uint32_t numInputs = m_Data.m_Parameters.m_BiasEnabled ? 3: 2;
86 m_Data.ValidateInputsOutputs("ClConvolution2dWorkload", numInputs, 1);
Matthew Benthamd8067922018-10-03 17:18:04 +010087
Matthew Benthamd8067922018-10-03 17:18:04 +010088 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 Corbett8bd53602022-05-12 15:54:58 +010090 arm_compute::ICLTensor& weights = static_cast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
Teresa Charlinee1497c2023-03-30 13:56:34 +010091 weights.info()->set_are_values_constant(info.m_InputTensorInfos[1].IsConstant());
92
Cathal Corbett8bd53602022-05-12 15:54:58 +010093 if (m_Data.m_Parameters.m_BiasEnabled)
94 {
Mike Kellyec67a0f2022-11-25 13:55:24 +000095 arm_compute::ICLTensor& bias = static_cast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor();
Teresa Charlinee1497c2023-03-30 13:56:34 +010096 bias.info()->set_are_values_constant(info.m_InputTensorInfos[2].IsConstant());
TeresaARMacedd852023-05-11 15:16:39 +000097 // We do not support dynamic bias
98 ARMNN_ASSERT(info.m_InputTensorInfos[2].IsConstant() == true);
Teresa Charlinee1497c2023-03-30 13:56:34 +010099
Mike Kellyec67a0f2022-11-25 13:55:24 +0000100 m_BiasProxy = std::make_unique<ICLTensorProxy>(&bias);
Cathal Corbett8bd53602022-05-12 15:54:58 +0100101 }
Matthew Benthamd8067922018-10-03 17:18:04 +0100102
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000103 // Create Proxy tensor and set the initial tensor handle to it
104 m_InputProxy = std::make_unique<ICLTensorProxy>(&input);
105 m_OutputProxy = std::make_unique<ICLTensorProxy>(&output);
Mike Kellyec67a0f2022-11-25 13:55:24 +0000106 m_WeightsProxy = std::make_unique<ICLTensorProxy>(&weights);
Cathal Corbett8bd53602022-05-12 15:54:58 +0100107
Matthew Benthamd8067922018-10-03 17:18:04 +0100108 arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
109 input.info()->set_data_layout(aclDataLayout);
110 output.info()->set_data_layout(aclDataLayout);
Cathal Corbett8bd53602022-05-12 15:54:58 +0100111 weights.info()->set_data_layout(aclDataLayout);
Matthew Benthamd8067922018-10-03 17:18:04 +0100112
Aron Virginas-Tar6f3785d2019-07-22 15:30:22 +0100113 arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
114
Mike Kelly07810fc2020-11-12 10:58:48 +0000115 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
116
Kevin May4692e112021-10-18 14:41:50 +0100117 {
118 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload_configure");
119 m_ConvolutionLayer.configure(clCompileContext,
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000120 m_InputProxy.get(),
Mike Kellyec67a0f2022-11-25 13:55:24 +0000121 m_WeightsProxy.get(),
122 m_BiasProxy.get(),
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000123 m_OutputProxy.get(),
Kevin May4692e112021-10-18 14:41:50 +0100124 padStrideInfo,
125 arm_compute::WeightsInfo(),
126 aclDilationInfo,
127 activationInfo,
128 isFastMathEnabled);
129 }
Sadik Armagan04a72972020-09-14 15:44:18 +0100130
131 m_ConvolutionMethod =
132 m_ConvolutionLayer.get_convolution_method(input.info(),
Cathal Corbett8bd53602022-05-12 15:54:58 +0100133 weights.info(),
Sadik Armagan04a72972020-09-14 15:44:18 +0100134 output.info(),
135 padStrideInfo,
136 arm_compute::WeightsInfo(),
Mike Kelly07810fc2020-11-12 10:58:48 +0000137 activationInfo,
Sadik Armagan04a72972020-09-14 15:44:18 +0100138 arm_compute::CLScheduler::get().target(),
139 aclDilationInfo,
140 isFastMathEnabled);
Matthew Benthamd8067922018-10-03 17:18:04 +0100141
Keith Davis554fa092021-07-20 11:25:22 +0100142 // Add details for profiling output
Keith Davis554fa092021-07-20 11:25:22 +0100143 WorkloadInfo detailsInfo;
144
145 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
146 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
Mike Kellyec67a0f2022-11-25 13:55:24 +0000147 detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[1]);
Keith Davis5a64f222021-08-04 10:35:20 +0100148 detailsInfo.m_ConvolutionMethod = armnn::Optional<std::string>(GetConvolutionMethodString(m_ConvolutionMethod));
Keith Davis554fa092021-07-20 11:25:22 +0100149 if (descriptor.m_Parameters.m_BiasEnabled)
150 {
Mike Kellyec67a0f2022-11-25 13:55:24 +0000151 detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[2]);
Keith Davis554fa092021-07-20 11:25:22 +0100152 }
153
154 // Report Profiling Details
Keith Davisbcd860a2021-08-05 14:20:33 +0100155 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClConvolution2dWorkload_Construct",
Keith Davis5a64f222021-08-04 10:35:20 +0100156 descriptor.m_Parameters,
157 detailsInfo,
Cathal Corbett8bd53602022-05-12 15:54:58 +0100158 GetGuid());
Matthew Benthamd8067922018-10-03 17:18:04 +0100159}
160
161void ClConvolution2dWorkload::Execute() const
162{
Cathal Corbett8bd53602022-05-12 15:54:58 +0100163 ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClConvolution2dWorkload_Execute", GetGuid());
Aron Virginas-Tara8e06ed2018-10-19 16:46:15 +0100164 RunClFunction(m_ConvolutionLayer, CHECK_LOCATION());
Matthew Benthamd8067922018-10-03 17:18:04 +0100165}
166
Sadik Armagan04a72972020-09-14 15:44:18 +0100167arm_compute::ConvolutionMethod ClConvolution2dWorkload::GetConvolutionMethod() const
168{
169 return m_ConvolutionMethod;
170}
171
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000172void ClConvolution2dWorkload::Reconfigure()
173{
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000174 arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
175 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
David Monahanec819992022-02-10 14:47:13 +0000176
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000177 m_InputProxy->set(&input);
178 m_OutputProxy->set(&output);
179}
180
Matthew Benthamd8067922018-10-03 17:18:04 +0100181} //namespace armnn