blob: 95753053dd547edb8fc568b8106bbde1fd61aa93 [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>
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);
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{
Kevin May4692e112021-10-18 14:41:50 +010073 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload");
Matthew Benthamd8067922018-10-03 17:18:04 +010074 const TensorInfo& weightInfo = m_Data.m_Weight->GetTensorInfo();
75
76 m_KernelTensor = std::make_unique<arm_compute::CLTensor>();
77 BuildArmComputeTensor(*m_KernelTensor, weightInfo, m_Data.m_Parameters.m_DataLayout);
78
Jan Eilers4b961d32019-07-11 09:19:35 +010079 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(m_Data.m_Parameters.m_DilationX,
80 m_Data.m_Parameters.m_DilationY);
81
Matthew Benthamd8067922018-10-03 17:18:04 +010082 if (m_Data.m_Parameters.m_BiasEnabled)
83 {
84 m_BiasTensor = std::make_unique<arm_compute::CLTensor>();
85 BuildArmComputeTensor(*m_BiasTensor, m_Data.m_Bias->GetTensorInfo(), m_Data.m_Parameters.m_DataLayout);
86 }
87
88 m_Data.ValidateInputsOutputs("ClConvolution2dWorkload", 1, 1);
89
90 arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
91 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
92
93 arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
94 input.info()->set_data_layout(aclDataLayout);
95 output.info()->set_data_layout(aclDataLayout);
96
Aron Virginas-Tar6f3785d2019-07-22 15:30:22 +010097 arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
98
Mike Kelly07810fc2020-11-12 10:58:48 +000099 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
100
Kevin May4692e112021-10-18 14:41:50 +0100101 {
102 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload_configure");
103 m_ConvolutionLayer.configure(clCompileContext,
104 &input,
105 m_KernelTensor.get(),
106 m_BiasTensor.get(),
107 &output,
108 padStrideInfo,
109 arm_compute::WeightsInfo(),
110 aclDilationInfo,
111 activationInfo,
112 isFastMathEnabled);
113 }
Sadik Armagan04a72972020-09-14 15:44:18 +0100114
115 m_ConvolutionMethod =
116 m_ConvolutionLayer.get_convolution_method(input.info(),
117 m_KernelTensor->info(),
118 output.info(),
119 padStrideInfo,
120 arm_compute::WeightsInfo(),
Mike Kelly07810fc2020-11-12 10:58:48 +0000121 activationInfo,
Sadik Armagan04a72972020-09-14 15:44:18 +0100122 arm_compute::CLScheduler::get().target(),
123 aclDilationInfo,
124 isFastMathEnabled);
Matthew Benthamd8067922018-10-03 17:18:04 +0100125
Keith Davis554fa092021-07-20 11:25:22 +0100126 // Add details for profiling output
Keith Davis554fa092021-07-20 11:25:22 +0100127 WorkloadInfo detailsInfo;
128
129 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
130 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
131 detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Weight->GetTensorInfo());
Keith Davis5a64f222021-08-04 10:35:20 +0100132 detailsInfo.m_ConvolutionMethod = armnn::Optional<std::string>(GetConvolutionMethodString(m_ConvolutionMethod));
Keith Davis554fa092021-07-20 11:25:22 +0100133 if (descriptor.m_Parameters.m_BiasEnabled)
134 {
135 detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Bias->GetTensorInfo());
136 }
137
138 // Report Profiling Details
Keith Davisbcd860a2021-08-05 14:20:33 +0100139 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClConvolution2dWorkload_Construct",
Keith Davis5a64f222021-08-04 10:35:20 +0100140 descriptor.m_Parameters,
141 detailsInfo,
142 this->GetGuid());
Keith Davis554fa092021-07-20 11:25:22 +0100143
Matthew Benthamd8067922018-10-03 17:18:04 +0100144 InitializeArmComputeClTensorData(*m_KernelTensor, m_Data.m_Weight);
145
146 if (m_BiasTensor)
147 {
148 InitializeArmComputeClTensorData(*m_BiasTensor, m_Data.m_Bias);
149 }
150
151 // Force Compute Library to perform the necessary copying and reshaping, after which
152 // delete all the input tensors that will no longer be needed
Kevin May4692e112021-10-18 14:41:50 +0100153 {
154 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload_prepare");
155 m_ConvolutionLayer.prepare();
156 }
Matthew Benthamd8067922018-10-03 17:18:04 +0100157 FreeUnusedTensors();
158}
159
160void ClConvolution2dWorkload::Execute() const
161{
Keith Davis5a64f222021-08-04 10:35:20 +0100162 ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClConvolution2dWorkload_Execute", this->GetGuid());
Aron Virginas-Tara8e06ed2018-10-19 16:46:15 +0100163 RunClFunction(m_ConvolutionLayer, CHECK_LOCATION());
Matthew Benthamd8067922018-10-03 17:18:04 +0100164}
165
Sadik Armagan04a72972020-09-14 15:44:18 +0100166arm_compute::ConvolutionMethod ClConvolution2dWorkload::GetConvolutionMethod() const
167{
168 return m_ConvolutionMethod;
169}
170
Matthew Benthamd8067922018-10-03 17:18:04 +0100171void ClConvolution2dWorkload::FreeUnusedTensors()
172{
173 FreeTensorIfUnused(m_KernelTensor);
174 FreeTensorIfUnused(m_BiasTensor);
175}
176
177} //namespace armnn