blob: 1920f2d20bff72e428a6e8ce409ae1259e097a62 [file] [log] [blame]
Matthew Benthamd8067922018-10-03 17:18:04 +01001//
Teresa Charlin588cbdf2022-01-19 15:55:37 +00002// Copyright © 2017 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());
Keith Davisb4dd5cc2022-04-07 11:32:00 +010045 // 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();
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();
94 m_BiasProxy = std::make_unique<ICLTensorProxy>(&bias);
Cathal Corbett8bd53602022-05-12 15:54:58 +010095 }
Matthew Benthamd8067922018-10-03 17:18:04 +010096
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +000097 // 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);
Mike Kellyec67a0f2022-11-25 13:55:24 +0000100 m_WeightsProxy = std::make_unique<ICLTensorProxy>(&weights);
Cathal Corbett8bd53602022-05-12 15:54:58 +0100101
Matthew Benthamd8067922018-10-03 17:18:04 +0100102 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 Corbett8bd53602022-05-12 15:54:58 +0100105 weights.info()->set_data_layout(aclDataLayout);
Matthew Benthamd8067922018-10-03 17:18:04 +0100106
Aron Virginas-Tar6f3785d2019-07-22 15:30:22 +0100107 arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
108
Mike Kelly07810fc2020-11-12 10:58:48 +0000109 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
110
Kevin May4692e112021-10-18 14:41:50 +0100111 {
112 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload_configure");
113 m_ConvolutionLayer.configure(clCompileContext,
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000114 m_InputProxy.get(),
Mike Kellyec67a0f2022-11-25 13:55:24 +0000115 m_WeightsProxy.get(),
116 m_BiasProxy.get(),
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000117 m_OutputProxy.get(),
Kevin May4692e112021-10-18 14:41:50 +0100118 padStrideInfo,
119 arm_compute::WeightsInfo(),
120 aclDilationInfo,
121 activationInfo,
122 isFastMathEnabled);
123 }
Sadik Armagan04a72972020-09-14 15:44:18 +0100124
125 m_ConvolutionMethod =
126 m_ConvolutionLayer.get_convolution_method(input.info(),
Cathal Corbett8bd53602022-05-12 15:54:58 +0100127 weights.info(),
Sadik Armagan04a72972020-09-14 15:44:18 +0100128 output.info(),
129 padStrideInfo,
130 arm_compute::WeightsInfo(),
Mike Kelly07810fc2020-11-12 10:58:48 +0000131 activationInfo,
Sadik Armagan04a72972020-09-14 15:44:18 +0100132 arm_compute::CLScheduler::get().target(),
133 aclDilationInfo,
134 isFastMathEnabled);
Matthew Benthamd8067922018-10-03 17:18:04 +0100135
Keith Davis554fa092021-07-20 11:25:22 +0100136 // Add details for profiling output
Keith Davis554fa092021-07-20 11:25:22 +0100137 WorkloadInfo detailsInfo;
138
139 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
140 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
Mike Kellyec67a0f2022-11-25 13:55:24 +0000141 detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[1]);
Keith Davis5a64f222021-08-04 10:35:20 +0100142 detailsInfo.m_ConvolutionMethod = armnn::Optional<std::string>(GetConvolutionMethodString(m_ConvolutionMethod));
Keith Davis554fa092021-07-20 11:25:22 +0100143 if (descriptor.m_Parameters.m_BiasEnabled)
144 {
Mike Kellyec67a0f2022-11-25 13:55:24 +0000145 detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[2]);
Keith Davis554fa092021-07-20 11:25:22 +0100146 }
147
148 // Report Profiling Details
Keith Davisbcd860a2021-08-05 14:20:33 +0100149 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClConvolution2dWorkload_Construct",
Keith Davis5a64f222021-08-04 10:35:20 +0100150 descriptor.m_Parameters,
151 detailsInfo,
Cathal Corbett8bd53602022-05-12 15:54:58 +0100152 GetGuid());
Matthew Benthamd8067922018-10-03 17:18:04 +0100153}
154
155void ClConvolution2dWorkload::Execute() const
156{
Cathal Corbett8bd53602022-05-12 15:54:58 +0100157 ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClConvolution2dWorkload_Execute", GetGuid());
Aron Virginas-Tara8e06ed2018-10-19 16:46:15 +0100158 RunClFunction(m_ConvolutionLayer, CHECK_LOCATION());
Matthew Benthamd8067922018-10-03 17:18:04 +0100159}
160
Sadik Armagan04a72972020-09-14 15:44:18 +0100161arm_compute::ConvolutionMethod ClConvolution2dWorkload::GetConvolutionMethod() const
162{
163 return m_ConvolutionMethod;
164}
165
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000166void ClConvolution2dWorkload::Reconfigure()
167{
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000168 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 Monahanec819992022-02-10 14:47:13 +0000170
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000171 m_InputProxy->set(&input);
172 m_OutputProxy->set(&output);
173}
174
Matthew Benthamd8067922018-10-03 17:18:04 +0100175} //namespace armnn