blob: 12a47dcd945639520a05fa91476829d2a4b3dddc [file] [log] [blame]
Matthew Benthamd8067922018-10-03 17:18:04 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// 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>
James Conroy1f58f032021-04-27 17:13:27 +010015#include <backendsCommon/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);
33 const arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weights, descriptor.m_DataLayout);
34
Jan Eilers4b961d32019-07-11 09:19:35 +010035 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(descriptor.m_DilationX,
36 descriptor.m_DilationY);
37
Matthew Benthamd8067922018-10-03 17:18:04 +010038 arm_compute::TensorInfo aclBiasesInfo;
39 arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr;
40
41 if (descriptor.m_BiasEnabled)
42 {
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010043 ARMNN_ASSERT(biases.has_value());
Matthew Benthamd8067922018-10-03 17:18:04 +010044
David Beck5eec11d2018-10-04 15:43:17 +010045 aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
Matthew Benthamd8067922018-10-03 17:18:04 +010046 optionalAclBiasesInfo = &aclBiasesInfo;
47 }
48
49 arm_compute::PadStrideInfo layerInfo = BuildArmComputePadStrideInfo(descriptor);
50
Mike Kelly07810fc2020-11-12 10:58:48 +000051 const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
52 activationDescriptor);
53
Matthew Benthamd8067922018-10-03 17:18:04 +010054 return arm_compute::CLConvolutionLayer::validate(&aclInputInfo,
55 &aclWeightsInfo,
56 optionalAclBiasesInfo,
57 &aclOutputInfo,
Jan Eilers4b961d32019-07-11 09:19:35 +010058 layerInfo,
59 arm_compute::WeightsInfo(),
Sadik Armagan045f6be2020-09-10 13:37:32 +010060 aclDilationInfo,
Mike Kelly07810fc2020-11-12 10:58:48 +000061 activationInfo,
Sadik Armagan045f6be2020-09-10 13:37:32 +010062 isFastMathEnabled);
Matthew Benthamd8067922018-10-03 17:18:04 +010063}
64
65ClConvolution2dWorkload::ClConvolution2dWorkload(const Convolution2dQueueDescriptor& descriptor,
Sadik Armagan04a72972020-09-14 15:44:18 +010066 const WorkloadInfo& info,
67 std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager,
Sadik Armagane9444752020-12-02 11:28:58 +000068 const arm_compute::CLCompileContext& clCompileContext,
Sadik Armagan04a72972020-09-14 15:44:18 +010069 const bool isFastMathEnabled)
Matthew Benthamd8067922018-10-03 17:18:04 +010070 : BaseWorkload<Convolution2dQueueDescriptor>(descriptor, info)
71 , m_ConvolutionLayer(memoryManager)
72{
Matthew Benthamd8067922018-10-03 17:18:04 +010073 const TensorInfo& weightInfo = m_Data.m_Weight->GetTensorInfo();
74
75 m_KernelTensor = std::make_unique<arm_compute::CLTensor>();
76 BuildArmComputeTensor(*m_KernelTensor, weightInfo, m_Data.m_Parameters.m_DataLayout);
77
Jan Eilers4b961d32019-07-11 09:19:35 +010078 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(m_Data.m_Parameters.m_DilationX,
79 m_Data.m_Parameters.m_DilationY);
80
Matthew Benthamd8067922018-10-03 17:18:04 +010081 if (m_Data.m_Parameters.m_BiasEnabled)
82 {
83 m_BiasTensor = std::make_unique<arm_compute::CLTensor>();
84 BuildArmComputeTensor(*m_BiasTensor, m_Data.m_Bias->GetTensorInfo(), m_Data.m_Parameters.m_DataLayout);
85 }
86
87 m_Data.ValidateInputsOutputs("ClConvolution2dWorkload", 1, 1);
88
89 arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
90 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
91
92 arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
93 input.info()->set_data_layout(aclDataLayout);
94 output.info()->set_data_layout(aclDataLayout);
95
Aron Virginas-Tar6f3785d2019-07-22 15:30:22 +010096 arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
97
Mike Kelly07810fc2020-11-12 10:58:48 +000098 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
99
Sadik Armagane9444752020-12-02 11:28:58 +0000100 m_ConvolutionLayer.configure(clCompileContext,
101 &input,
Matthew Benthamd8067922018-10-03 17:18:04 +0100102 m_KernelTensor.get(),
103 m_BiasTensor.get(),
104 &output,
Jan Eilers4b961d32019-07-11 09:19:35 +0100105 padStrideInfo,
106 arm_compute::WeightsInfo(),
Sadik Armagan04a72972020-09-14 15:44:18 +0100107 aclDilationInfo,
Mike Kelly07810fc2020-11-12 10:58:48 +0000108 activationInfo,
Sadik Armagan04a72972020-09-14 15:44:18 +0100109 isFastMathEnabled);
110
111 m_ConvolutionMethod =
112 m_ConvolutionLayer.get_convolution_method(input.info(),
113 m_KernelTensor->info(),
114 output.info(),
115 padStrideInfo,
116 arm_compute::WeightsInfo(),
Mike Kelly07810fc2020-11-12 10:58:48 +0000117 activationInfo,
Sadik Armagan04a72972020-09-14 15:44:18 +0100118 arm_compute::CLScheduler::get().target(),
119 aclDilationInfo,
120 isFastMathEnabled);
Matthew Benthamd8067922018-10-03 17:18:04 +0100121
Keith Davis554fa092021-07-20 11:25:22 +0100122 // Add details for profiling output
Keith Davis554fa092021-07-20 11:25:22 +0100123 WorkloadInfo detailsInfo;
124
125 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
126 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
127 detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Weight->GetTensorInfo());
Keith Davis5a64f222021-08-04 10:35:20 +0100128 detailsInfo.m_ConvolutionMethod = armnn::Optional<std::string>(GetConvolutionMethodString(m_ConvolutionMethod));
Keith Davis554fa092021-07-20 11:25:22 +0100129 if (descriptor.m_Parameters.m_BiasEnabled)
130 {
131 detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Bias->GetTensorInfo());
132 }
133
134 // Report Profiling Details
Keith Davisbcd860a2021-08-05 14:20:33 +0100135 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClConvolution2dWorkload_Construct",
Keith Davis5a64f222021-08-04 10:35:20 +0100136 descriptor.m_Parameters,
137 detailsInfo,
138 this->GetGuid());
Keith Davis554fa092021-07-20 11:25:22 +0100139
Matthew Benthamd8067922018-10-03 17:18:04 +0100140 InitializeArmComputeClTensorData(*m_KernelTensor, m_Data.m_Weight);
141
142 if (m_BiasTensor)
143 {
144 InitializeArmComputeClTensorData(*m_BiasTensor, m_Data.m_Bias);
145 }
146
147 // Force Compute Library to perform the necessary copying and reshaping, after which
148 // delete all the input tensors that will no longer be needed
149 m_ConvolutionLayer.prepare();
150 FreeUnusedTensors();
151}
152
153void ClConvolution2dWorkload::Execute() const
154{
Keith Davis5a64f222021-08-04 10:35:20 +0100155 ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClConvolution2dWorkload_Execute", this->GetGuid());
Aron Virginas-Tara8e06ed2018-10-19 16:46:15 +0100156 RunClFunction(m_ConvolutionLayer, CHECK_LOCATION());
Matthew Benthamd8067922018-10-03 17:18:04 +0100157}
158
Sadik Armagan04a72972020-09-14 15:44:18 +0100159arm_compute::ConvolutionMethod ClConvolution2dWorkload::GetConvolutionMethod() const
160{
161 return m_ConvolutionMethod;
162}
163
Matthew Benthamd8067922018-10-03 17:18:04 +0100164void ClConvolution2dWorkload::FreeUnusedTensors()
165{
166 FreeTensorIfUnused(m_KernelTensor);
167 FreeTensorIfUnused(m_BiasTensor);
168}
169
170} //namespace armnn