blob: 762645bfba891ed0f709fac8ce7e93d0c0e251c7 [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{
Cathal Corbett8bd53602022-05-12 15:54:58 +010031 // The arm_compute::CLConvolutionLayer supports both const and non const
Keith Davisb4dd5cc2022-04-07 11:32:00 +010032 // weights. However, in the case of non const weights we'd have to call
33 // prepare or configure for each inference which we're not setup to do just yet.
34 if (!weights.IsConstant())
35 {
36 return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
37 "ArmNN ClConvolution2dWorkload does not support non constant weights."};
38 }
39
Matthew Benthamd8067922018-10-03 17:18:04 +010040 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
41 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
Cathal Corbett8bd53602022-05-12 15:54:58 +010042 arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weights, descriptor.m_DataLayout);
43 aclWeightsInfo.set_are_values_constant(weights.IsConstant());
Matthew Benthamd8067922018-10-03 17:18:04 +010044
Jan Eilers4b961d32019-07-11 09:19:35 +010045 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(descriptor.m_DilationX,
46 descriptor.m_DilationY);
47
Matthew Benthamd8067922018-10-03 17:18:04 +010048 arm_compute::TensorInfo aclBiasesInfo;
49 arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr;
50
51 if (descriptor.m_BiasEnabled)
52 {
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010053 ARMNN_ASSERT(biases.has_value());
Keith Davisb4dd5cc2022-04-07 11:32:00 +010054 // Same for bias as weights. We don't currently support non const.
55 if (!biases.value().IsConstant())
56 {
57 return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
58 "ArmNN ClConvolution2dWorkload does not support non constant bias."};
59 }
David Beck5eec11d2018-10-04 15:43:17 +010060 aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
Cathal Corbett8bd53602022-05-12 15:54:58 +010061 aclBiasesInfo.set_are_values_constant(biases.value().IsConstant());
Matthew Benthamd8067922018-10-03 17:18:04 +010062 optionalAclBiasesInfo = &aclBiasesInfo;
63 }
64
65 arm_compute::PadStrideInfo layerInfo = BuildArmComputePadStrideInfo(descriptor);
66
Mike Kelly07810fc2020-11-12 10:58:48 +000067 const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
68 activationDescriptor);
69
Matthew Benthamd8067922018-10-03 17:18:04 +010070 return arm_compute::CLConvolutionLayer::validate(&aclInputInfo,
71 &aclWeightsInfo,
72 optionalAclBiasesInfo,
73 &aclOutputInfo,
Jan Eilers4b961d32019-07-11 09:19:35 +010074 layerInfo,
75 arm_compute::WeightsInfo(),
Sadik Armagan045f6be2020-09-10 13:37:32 +010076 aclDilationInfo,
Mike Kelly07810fc2020-11-12 10:58:48 +000077 activationInfo,
Sadik Armagan045f6be2020-09-10 13:37:32 +010078 isFastMathEnabled);
Matthew Benthamd8067922018-10-03 17:18:04 +010079}
80
81ClConvolution2dWorkload::ClConvolution2dWorkload(const Convolution2dQueueDescriptor& descriptor,
Sadik Armagan04a72972020-09-14 15:44:18 +010082 const WorkloadInfo& info,
83 std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager,
Sadik Armagane9444752020-12-02 11:28:58 +000084 const arm_compute::CLCompileContext& clCompileContext,
Sadik Armagan04a72972020-09-14 15:44:18 +010085 const bool isFastMathEnabled)
Teresa Charlin588cbdf2022-01-19 15:55:37 +000086 : ClBaseWorkload<Convolution2dQueueDescriptor>(descriptor, info)
Matthew Benthamd8067922018-10-03 17:18:04 +010087 , m_ConvolutionLayer(memoryManager)
88{
Kevin May4692e112021-10-18 14:41:50 +010089 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload");
Matthew Benthamd8067922018-10-03 17:18:04 +010090
Jan Eilers4b961d32019-07-11 09:19:35 +010091 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(m_Data.m_Parameters.m_DilationX,
92 m_Data.m_Parameters.m_DilationY);
93
Cathal Corbett8bd53602022-05-12 15:54:58 +010094 uint32_t numInputs = m_Data.m_Parameters.m_BiasEnabled ? 3: 2;
95 m_Data.ValidateInputsOutputs("ClConvolution2dWorkload", numInputs, 1);
Matthew Benthamd8067922018-10-03 17:18:04 +010096
Matthew Benthamd8067922018-10-03 17:18:04 +010097 arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
98 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
Cathal Corbett8bd53602022-05-12 15:54:58 +010099 arm_compute::ICLTensor& weights = static_cast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
100 arm_compute::ICLTensor* bias = nullptr;
101 if (m_Data.m_Parameters.m_BiasEnabled)
102 {
103 bias = &static_cast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor();
104 }
Matthew Benthamd8067922018-10-03 17:18:04 +0100105
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000106 // Create Proxy tensor and set the initial tensor handle to it
107 m_InputProxy = std::make_unique<ICLTensorProxy>(&input);
108 m_OutputProxy = std::make_unique<ICLTensorProxy>(&output);
109
Cathal Corbett8bd53602022-05-12 15:54:58 +0100110
Matthew Benthamd8067922018-10-03 17:18:04 +0100111 arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
112 input.info()->set_data_layout(aclDataLayout);
113 output.info()->set_data_layout(aclDataLayout);
Cathal Corbett8bd53602022-05-12 15:54:58 +0100114 weights.info()->set_data_layout(aclDataLayout);
Matthew Benthamd8067922018-10-03 17:18:04 +0100115
Aron Virginas-Tar6f3785d2019-07-22 15:30:22 +0100116 arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
117
Mike Kelly07810fc2020-11-12 10:58:48 +0000118 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
119
Kevin May4692e112021-10-18 14:41:50 +0100120 {
121 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClConvolution2dWorkload_configure");
122 m_ConvolutionLayer.configure(clCompileContext,
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000123 m_InputProxy.get(),
Cathal Corbett8bd53602022-05-12 15:54:58 +0100124 &weights,
125 bias,
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000126 m_OutputProxy.get(),
Kevin May4692e112021-10-18 14:41:50 +0100127 padStrideInfo,
128 arm_compute::WeightsInfo(),
129 aclDilationInfo,
130 activationInfo,
131 isFastMathEnabled);
132 }
Sadik Armagan04a72972020-09-14 15:44:18 +0100133
134 m_ConvolutionMethod =
135 m_ConvolutionLayer.get_convolution_method(input.info(),
Cathal Corbett8bd53602022-05-12 15:54:58 +0100136 weights.info(),
Sadik Armagan04a72972020-09-14 15:44:18 +0100137 output.info(),
138 padStrideInfo,
139 arm_compute::WeightsInfo(),
Mike Kelly07810fc2020-11-12 10:58:48 +0000140 activationInfo,
Sadik Armagan04a72972020-09-14 15:44:18 +0100141 arm_compute::CLScheduler::get().target(),
142 aclDilationInfo,
143 isFastMathEnabled);
Matthew Benthamd8067922018-10-03 17:18:04 +0100144
Keith Davis554fa092021-07-20 11:25:22 +0100145 // Add details for profiling output
Keith Davis554fa092021-07-20 11:25:22 +0100146 WorkloadInfo detailsInfo;
147
148 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
149 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
150 detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Weight->GetTensorInfo());
Keith Davis5a64f222021-08-04 10:35:20 +0100151 detailsInfo.m_ConvolutionMethod = armnn::Optional<std::string>(GetConvolutionMethodString(m_ConvolutionMethod));
Keith Davis554fa092021-07-20 11:25:22 +0100152 if (descriptor.m_Parameters.m_BiasEnabled)
153 {
154 detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Bias->GetTensorInfo());
155 }
156
157 // Report Profiling Details
Keith Davisbcd860a2021-08-05 14:20:33 +0100158 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClConvolution2dWorkload_Construct",
Keith Davis5a64f222021-08-04 10:35:20 +0100159 descriptor.m_Parameters,
160 detailsInfo,
Cathal Corbett8bd53602022-05-12 15:54:58 +0100161 GetGuid());
Matthew Benthamd8067922018-10-03 17:18:04 +0100162}
163
164void ClConvolution2dWorkload::Execute() const
165{
Cathal Corbett8bd53602022-05-12 15:54:58 +0100166 ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClConvolution2dWorkload_Execute", 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
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000175void ClConvolution2dWorkload::Reconfigure()
176{
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000177 arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
178 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
David Monahanec819992022-02-10 14:47:13 +0000179
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000180 m_InputProxy->set(&input);
181 m_OutputProxy->set(&output);
182}
183
Matthew Benthamd8067922018-10-03 17:18:04 +0100184} //namespace armnn