blob: 5c731aa0a1f85ca9a7ede0c840f180f5fcd0ecb3 [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{
73 // todo: check tensor shapes match.
74 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
Sadik Armagane9444752020-12-02 11:28:58 +0000101 m_ConvolutionLayer.configure(clCompileContext,
102 &input,
Matthew Benthamd8067922018-10-03 17:18:04 +0100103 m_KernelTensor.get(),
104 m_BiasTensor.get(),
105 &output,
Jan Eilers4b961d32019-07-11 09:19:35 +0100106 padStrideInfo,
107 arm_compute::WeightsInfo(),
Sadik Armagan04a72972020-09-14 15:44:18 +0100108 aclDilationInfo,
Mike Kelly07810fc2020-11-12 10:58:48 +0000109 activationInfo,
Sadik Armagan04a72972020-09-14 15:44:18 +0100110 isFastMathEnabled);
111
112 m_ConvolutionMethod =
113 m_ConvolutionLayer.get_convolution_method(input.info(),
114 m_KernelTensor->info(),
115 output.info(),
116 padStrideInfo,
117 arm_compute::WeightsInfo(),
Mike Kelly07810fc2020-11-12 10:58:48 +0000118 activationInfo,
Sadik Armagan04a72972020-09-14 15:44:18 +0100119 arm_compute::CLScheduler::get().target(),
120 aclDilationInfo,
121 isFastMathEnabled);
Matthew Benthamd8067922018-10-03 17:18:04 +0100122
123 InitializeArmComputeClTensorData(*m_KernelTensor, m_Data.m_Weight);
124
125 if (m_BiasTensor)
126 {
127 InitializeArmComputeClTensorData(*m_BiasTensor, m_Data.m_Bias);
128 }
129
130 // Force Compute Library to perform the necessary copying and reshaping, after which
131 // delete all the input tensors that will no longer be needed
132 m_ConvolutionLayer.prepare();
133 FreeUnusedTensors();
134}
135
136void ClConvolution2dWorkload::Execute() const
137{
138 ARMNN_SCOPED_PROFILING_EVENT_CL("ClConvolution2dWorkload_Execute");
Aron Virginas-Tara8e06ed2018-10-19 16:46:15 +0100139 RunClFunction(m_ConvolutionLayer, CHECK_LOCATION());
Matthew Benthamd8067922018-10-03 17:18:04 +0100140}
141
Sadik Armagan04a72972020-09-14 15:44:18 +0100142arm_compute::ConvolutionMethod ClConvolution2dWorkload::GetConvolutionMethod() const
143{
144 return m_ConvolutionMethod;
145}
146
Matthew Benthamd8067922018-10-03 17:18:04 +0100147void ClConvolution2dWorkload::FreeUnusedTensors()
148{
149 FreeTensorIfUnused(m_KernelTensor);
150 FreeTensorIfUnused(m_BiasTensor);
151}
152
153} //namespace armnn