blob: cdfa885f67c8bbd636cf788e57f0813e77c4ee72 [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);
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)
Teresa Charlin588cbdf2022-01-19 15:55:37 +000070 : ClBaseWorkload<Convolution2dQueueDescriptor>(descriptor, info)
Matthew Benthamd8067922018-10-03 17:18:04 +010071 , 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
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +000093 // Create Proxy tensor and set the initial tensor handle to it
94 m_InputProxy = std::make_unique<ICLTensorProxy>(&input);
95 m_OutputProxy = std::make_unique<ICLTensorProxy>(&output);
96
Matthew Benthamd8067922018-10-03 17:18:04 +010097 arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
98 input.info()->set_data_layout(aclDataLayout);
99 output.info()->set_data_layout(aclDataLayout);
100
Aron Virginas-Tar6f3785d2019-07-22 15:30:22 +0100101 arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
102
Mike Kelly07810fc2020-11-12 10:58:48 +0000103 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
104
Kevin May4692e112021-10-18 14:41:50 +0100105 {
106 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload_configure");
107 m_ConvolutionLayer.configure(clCompileContext,
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000108 m_InputProxy.get(),
Kevin May4692e112021-10-18 14:41:50 +0100109 m_KernelTensor.get(),
110 m_BiasTensor.get(),
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000111 m_OutputProxy.get(),
Kevin May4692e112021-10-18 14:41:50 +0100112 padStrideInfo,
113 arm_compute::WeightsInfo(),
114 aclDilationInfo,
115 activationInfo,
116 isFastMathEnabled);
117 }
Sadik Armagan04a72972020-09-14 15:44:18 +0100118
119 m_ConvolutionMethod =
120 m_ConvolutionLayer.get_convolution_method(input.info(),
121 m_KernelTensor->info(),
122 output.info(),
123 padStrideInfo,
124 arm_compute::WeightsInfo(),
Mike Kelly07810fc2020-11-12 10:58:48 +0000125 activationInfo,
Sadik Armagan04a72972020-09-14 15:44:18 +0100126 arm_compute::CLScheduler::get().target(),
127 aclDilationInfo,
128 isFastMathEnabled);
Matthew Benthamd8067922018-10-03 17:18:04 +0100129
Keith Davis554fa092021-07-20 11:25:22 +0100130 // Add details for profiling output
Keith Davis554fa092021-07-20 11:25:22 +0100131 WorkloadInfo detailsInfo;
132
133 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
134 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
135 detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Weight->GetTensorInfo());
Keith Davis5a64f222021-08-04 10:35:20 +0100136 detailsInfo.m_ConvolutionMethod = armnn::Optional<std::string>(GetConvolutionMethodString(m_ConvolutionMethod));
Keith Davis554fa092021-07-20 11:25:22 +0100137 if (descriptor.m_Parameters.m_BiasEnabled)
138 {
139 detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Bias->GetTensorInfo());
140 }
141
142 // Report Profiling Details
Keith Davisbcd860a2021-08-05 14:20:33 +0100143 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClConvolution2dWorkload_Construct",
Keith Davis5a64f222021-08-04 10:35:20 +0100144 descriptor.m_Parameters,
145 detailsInfo,
146 this->GetGuid());
Keith Davis554fa092021-07-20 11:25:22 +0100147
Matthew Benthamd8067922018-10-03 17:18:04 +0100148 InitializeArmComputeClTensorData(*m_KernelTensor, m_Data.m_Weight);
149
150 if (m_BiasTensor)
151 {
152 InitializeArmComputeClTensorData(*m_BiasTensor, m_Data.m_Bias);
153 }
154
155 // Force Compute Library to perform the necessary copying and reshaping, after which
156 // delete all the input tensors that will no longer be needed
Kevin May4692e112021-10-18 14:41:50 +0100157 {
158 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload_prepare");
159 m_ConvolutionLayer.prepare();
160 }
Matthew Benthamd8067922018-10-03 17:18:04 +0100161 FreeUnusedTensors();
162}
163
164void ClConvolution2dWorkload::Execute() const
165{
Keith Davis5a64f222021-08-04 10:35:20 +0100166 ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClConvolution2dWorkload_Execute", this->GetGuid());
Aron Virginas-Tara8e06ed2018-10-19 16:46:15 +0100167 RunClFunction(m_ConvolutionLayer, CHECK_LOCATION());
Matthew Benthamd8067922018-10-03 17:18:04 +0100168}
169
Sadik Armagan04a72972020-09-14 15:44:18 +0100170arm_compute::ConvolutionMethod ClConvolution2dWorkload::GetConvolutionMethod() const
171{
172 return m_ConvolutionMethod;
173}
174
Matthew Benthamd8067922018-10-03 17:18:04 +0100175void ClConvolution2dWorkload::FreeUnusedTensors()
176{
177 FreeTensorIfUnused(m_KernelTensor);
178 FreeTensorIfUnused(m_BiasTensor);
179}
180
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000181void ClConvolution2dWorkload::Reconfigure()
182{
183 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload_Reconfigure");
184 arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
185 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
186 m_InputProxy->set(&input);
187 m_OutputProxy->set(&output);
188}
189
Matthew Benthamd8067922018-10-03 17:18:04 +0100190} //namespace armnn