blob: 32152b5834706aca1a9c75101d752b1b2aa421b9 [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 {
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 }
David Beck5eec11d2018-10-04 15:43:17 +010049 aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
Cathal Corbett8bd53602022-05-12 15:54:58 +010050 aclBiasesInfo.set_are_values_constant(biases.value().IsConstant());
Matthew Benthamd8067922018-10-03 17:18:04 +010051 optionalAclBiasesInfo = &aclBiasesInfo;
52 }
53
54 arm_compute::PadStrideInfo layerInfo = BuildArmComputePadStrideInfo(descriptor);
55
Mike Kelly07810fc2020-11-12 10:58:48 +000056 const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
57 activationDescriptor);
58
Matthew Benthamd8067922018-10-03 17:18:04 +010059 return arm_compute::CLConvolutionLayer::validate(&aclInputInfo,
60 &aclWeightsInfo,
61 optionalAclBiasesInfo,
62 &aclOutputInfo,
Jan Eilers4b961d32019-07-11 09:19:35 +010063 layerInfo,
64 arm_compute::WeightsInfo(),
Sadik Armagan045f6be2020-09-10 13:37:32 +010065 aclDilationInfo,
Mike Kelly07810fc2020-11-12 10:58:48 +000066 activationInfo,
Sadik Armagan045f6be2020-09-10 13:37:32 +010067 isFastMathEnabled);
Matthew Benthamd8067922018-10-03 17:18:04 +010068}
69
70ClConvolution2dWorkload::ClConvolution2dWorkload(const Convolution2dQueueDescriptor& descriptor,
Sadik Armagan04a72972020-09-14 15:44:18 +010071 const WorkloadInfo& info,
72 std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager,
Sadik Armagane9444752020-12-02 11:28:58 +000073 const arm_compute::CLCompileContext& clCompileContext,
Sadik Armagan04a72972020-09-14 15:44:18 +010074 const bool isFastMathEnabled)
Teresa Charlin588cbdf2022-01-19 15:55:37 +000075 : ClBaseWorkload<Convolution2dQueueDescriptor>(descriptor, info)
Matthew Benthamd8067922018-10-03 17:18:04 +010076 , m_ConvolutionLayer(memoryManager)
77{
Kevin May4692e112021-10-18 14:41:50 +010078 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload");
Matthew Benthamd8067922018-10-03 17:18:04 +010079
Jan Eilers4b961d32019-07-11 09:19:35 +010080 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(m_Data.m_Parameters.m_DilationX,
81 m_Data.m_Parameters.m_DilationY);
82
Cathal Corbett8bd53602022-05-12 15:54:58 +010083 uint32_t numInputs = m_Data.m_Parameters.m_BiasEnabled ? 3: 2;
84 m_Data.ValidateInputsOutputs("ClConvolution2dWorkload", numInputs, 1);
Matthew Benthamd8067922018-10-03 17:18:04 +010085
Matthew Benthamd8067922018-10-03 17:18:04 +010086 arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
87 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
Cathal Corbett8bd53602022-05-12 15:54:58 +010088 arm_compute::ICLTensor& weights = static_cast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
Teresa Charlinee1497c2023-03-30 13:56:34 +010089 weights.info()->set_are_values_constant(info.m_InputTensorInfos[1].IsConstant());
90
Cathal Corbett8bd53602022-05-12 15:54:58 +010091 if (m_Data.m_Parameters.m_BiasEnabled)
92 {
Mike Kellyec67a0f2022-11-25 13:55:24 +000093 arm_compute::ICLTensor& bias = static_cast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor();
Teresa Charlinee1497c2023-03-30 13:56:34 +010094 bias.info()->set_are_values_constant(info.m_InputTensorInfos[2].IsConstant());
Teresa Charlinee1497c2023-03-30 13:56:34 +010095
Mike Kellyec67a0f2022-11-25 13:55:24 +000096 m_BiasProxy = std::make_unique<ICLTensorProxy>(&bias);
Cathal Corbett8bd53602022-05-12 15:54:58 +010097 }
Matthew Benthamd8067922018-10-03 17:18:04 +010098
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +000099 // Create Proxy tensor and set the initial tensor handle to it
100 m_InputProxy = std::make_unique<ICLTensorProxy>(&input);
101 m_OutputProxy = std::make_unique<ICLTensorProxy>(&output);
Mike Kellyec67a0f2022-11-25 13:55:24 +0000102 m_WeightsProxy = std::make_unique<ICLTensorProxy>(&weights);
Cathal Corbett8bd53602022-05-12 15:54:58 +0100103
Matthew Benthamd8067922018-10-03 17:18:04 +0100104 arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
105 input.info()->set_data_layout(aclDataLayout);
106 output.info()->set_data_layout(aclDataLayout);
Cathal Corbett8bd53602022-05-12 15:54:58 +0100107 weights.info()->set_data_layout(aclDataLayout);
Matthew Benthamd8067922018-10-03 17:18:04 +0100108
Aron Virginas-Tar6f3785d2019-07-22 15:30:22 +0100109 arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
110
Mike Kelly07810fc2020-11-12 10:58:48 +0000111 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
112
Kevin May4692e112021-10-18 14:41:50 +0100113 {
114 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload_configure");
115 m_ConvolutionLayer.configure(clCompileContext,
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000116 m_InputProxy.get(),
Mike Kellyec67a0f2022-11-25 13:55:24 +0000117 m_WeightsProxy.get(),
118 m_BiasProxy.get(),
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000119 m_OutputProxy.get(),
Kevin May4692e112021-10-18 14:41:50 +0100120 padStrideInfo,
121 arm_compute::WeightsInfo(),
122 aclDilationInfo,
123 activationInfo,
124 isFastMathEnabled);
125 }
Sadik Armagan04a72972020-09-14 15:44:18 +0100126
127 m_ConvolutionMethod =
128 m_ConvolutionLayer.get_convolution_method(input.info(),
Cathal Corbett8bd53602022-05-12 15:54:58 +0100129 weights.info(),
Sadik Armagan04a72972020-09-14 15:44:18 +0100130 output.info(),
131 padStrideInfo,
132 arm_compute::WeightsInfo(),
Mike Kelly07810fc2020-11-12 10:58:48 +0000133 activationInfo,
Sadik Armagan04a72972020-09-14 15:44:18 +0100134 arm_compute::CLScheduler::get().target(),
135 aclDilationInfo,
136 isFastMathEnabled);
Matthew Benthamd8067922018-10-03 17:18:04 +0100137
Keith Davis554fa092021-07-20 11:25:22 +0100138 // Add details for profiling output
Keith Davis554fa092021-07-20 11:25:22 +0100139 WorkloadInfo detailsInfo;
140
141 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
142 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
Mike Kellyec67a0f2022-11-25 13:55:24 +0000143 detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[1]);
Keith Davis5a64f222021-08-04 10:35:20 +0100144 detailsInfo.m_ConvolutionMethod = armnn::Optional<std::string>(GetConvolutionMethodString(m_ConvolutionMethod));
Keith Davis554fa092021-07-20 11:25:22 +0100145 if (descriptor.m_Parameters.m_BiasEnabled)
146 {
Mike Kellyec67a0f2022-11-25 13:55:24 +0000147 detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[2]);
Keith Davis554fa092021-07-20 11:25:22 +0100148 }
149
150 // Report Profiling Details
Keith Davisbcd860a2021-08-05 14:20:33 +0100151 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClConvolution2dWorkload_Construct",
Keith Davis5a64f222021-08-04 10:35:20 +0100152 descriptor.m_Parameters,
153 detailsInfo,
Cathal Corbett8bd53602022-05-12 15:54:58 +0100154 GetGuid());
Matthew Benthamd8067922018-10-03 17:18:04 +0100155}
156
157void ClConvolution2dWorkload::Execute() const
158{
Cathal Corbett8bd53602022-05-12 15:54:58 +0100159 ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClConvolution2dWorkload_Execute", GetGuid());
Aron Virginas-Tara8e06ed2018-10-19 16:46:15 +0100160 RunClFunction(m_ConvolutionLayer, CHECK_LOCATION());
Matthew Benthamd8067922018-10-03 17:18:04 +0100161}
162
Sadik Armagan04a72972020-09-14 15:44:18 +0100163arm_compute::ConvolutionMethod ClConvolution2dWorkload::GetConvolutionMethod() const
164{
165 return m_ConvolutionMethod;
166}
167
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000168void ClConvolution2dWorkload::Reconfigure()
169{
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000170 arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
171 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
David Monahanec819992022-02-10 14:47:13 +0000172
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000173 m_InputProxy->set(&input);
174 m_OutputProxy->set(&output);
175}
176
Matthew Benthamd8067922018-10-03 17:18:04 +0100177} //namespace armnn