1 //
2 // Copyright © 2017,2022 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "NeonConvolution2dWorkload.hpp"
7
8 #include <aclCommon/ArmComputeTensorUtils.hpp>
9 #include <aclCommon/ArmComputeUtils.hpp>
10 #include <armnn/utility/PolymorphicDowncast.hpp>
11 #include <armnn/backends/TensorHandle.hpp>
12 #include <neon/workloads/NeonWorkloadUtils.hpp>
13
14 #include <arm_compute/runtime/NEON/functions/NEConvolutionLayer.h>
15
16 #include <armnn/Types.hpp>
17 #include <Half.hpp>
18
19 namespace armnn
20 {
21
22 using namespace armcomputetensorutils;
23
NeonConvolution2dWorkloadValidate(const TensorInfo & input,const TensorInfo & output,const Convolution2dDescriptor & descriptor,const TensorInfo & weights,const Optional<TensorInfo> & biases,bool isFastMathEnabled,const ActivationDescriptor * activationDescriptor)24 arm_compute::Status NeonConvolution2dWorkloadValidate(const TensorInfo& input,
25 const TensorInfo& output,
26 const Convolution2dDescriptor& descriptor,
27 const TensorInfo& weights,
28 const Optional<TensorInfo>& biases,
29 bool isFastMathEnabled,
30 const ActivationDescriptor* activationDescriptor)
31 {
32 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
33 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
34 arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weights, descriptor.m_DataLayout);
35 aclWeightsInfo.set_are_values_constant(weights.IsConstant());
36
37 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(descriptor.m_DilationX,
38 descriptor.m_DilationY);
39
40 arm_compute::TensorInfo aclBiasesInfo;
41 arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr;
42
43 if (descriptor.m_BiasEnabled)
44 {
45 ARMNN_ASSERT(biases.has_value());
46 // Same for bias as weights. We don't currently support non const.
47 if (!biases.value().IsConstant())
48 {
49 return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
50 "ArmNN NeonConvolution2dWorkload does not support non constant bias."};
51 }
52 aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
53 aclBiasesInfo.set_are_values_constant(biases.value().IsConstant());
54 optionalAclBiasesInfo = &aclBiasesInfo;
55 }
56
57 arm_compute::PadStrideInfo layerInfo = BuildArmComputePadStrideInfo(descriptor);
58
59 const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
60 activationDescriptor);
61
62 return arm_compute::NEConvolutionLayer::validate(&aclInputInfo,
63 &aclWeightsInfo,
64 optionalAclBiasesInfo,
65 &aclOutputInfo,
66 layerInfo,
67 arm_compute::WeightsInfo(),
68 aclDilationInfo,
69 activationInfo,
70 isFastMathEnabled);
71 }
72
NeonConvolution2dWorkload(const Convolution2dQueueDescriptor & descriptor,const WorkloadInfo & info,std::shared_ptr<arm_compute::MemoryManagerOnDemand> & memoryManager,const bool isFastMathEnabled)73 NeonConvolution2dWorkload::NeonConvolution2dWorkload(
74 const Convolution2dQueueDescriptor& descriptor,
75 const WorkloadInfo& info,
76 std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager,
77 const bool isFastMathEnabled)
78 : NeonBaseWorkload<Convolution2dQueueDescriptor>(descriptor, info)
79 {
80 using arm_compute::NEConvolutionLayer;
81
82 uint32_t numInputs = m_Data.m_Parameters.m_BiasEnabled ? 3: 2;
83 m_Data.ValidateInputsOutputs("NeonConvolution2dWorkload", numInputs, 1);
84
85 arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
86 arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
87
88 arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
89 input.info()->set_data_layout(aclDataLayout);
90 output.info()->set_data_layout(aclDataLayout);
91
92 m_KernelTensor = std::make_unique<arm_compute::Tensor>();
93 BuildArmComputeTensor(*m_KernelTensor, info.m_InputTensorInfos[1], m_Data.m_Parameters.m_DataLayout);
94 if (m_Data.m_Parameters.m_BiasEnabled)
95 {
96 m_BiasTensor = std::make_unique<arm_compute::Tensor>();
97 BuildArmComputeTensor(*m_BiasTensor, info.m_InputTensorInfos[2], m_Data.m_Parameters.m_DataLayout);
98 }
99
100 arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
101
102 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(m_Data.m_Parameters.m_DilationX,
103 m_Data.m_Parameters.m_DilationY);
104
105 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
106
107 auto convolutionLayer = std::make_unique<arm_compute::NEConvolutionLayer>(memoryManager);
108 convolutionLayer->configure(&input,
109 m_KernelTensor.get(),
110 m_BiasTensor.get(),
111 &output,
112 padStrideInfo,
113 arm_compute::WeightsInfo(),
114 aclDilationInfo,
115 activationInfo,
116 isFastMathEnabled);
117
118 m_ConvolutionMethod =
119 convolutionLayer->get_convolution_method(input.info(),
120 m_KernelTensor->info(),
121 output.info(),
122 padStrideInfo,
123 arm_compute::WeightsInfo(),
124 aclDilationInfo,
125 activationInfo,
126 isFastMathEnabled);
127
128 // Add details for profiling output
129 WorkloadInfo detailsInfo;
130
131 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
132 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
133 detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[1]);
134 detailsInfo.m_ConvolutionMethod = armnn::Optional<std::string>(GetConvolutionMethodString(m_ConvolutionMethod));
135
136 if (descriptor.m_Parameters.m_BiasEnabled)
137 {
138 detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[2]);
139 }
140
141 // Report Profiling Details
142 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonConvolution2dWorkload_Construct",
143 descriptor.m_Parameters,
144 detailsInfo,
145 GetGuid());
146
147 m_ConvolutionLayer.reset(convolutionLayer.release());
148
149 ARMNN_ASSERT(m_ConvolutionLayer);
150 m_KernelTensorInfo = info.m_InputTensorInfos[1];
151
152 if (m_Data.m_Parameters.m_BiasEnabled)
153 {
154 m_BiasTensorInfo = info.m_InputTensorInfos[2];
155 }
156 }
157
Execute() const158 void NeonConvolution2dWorkload::Execute() const
159 {
160 ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID("NeonConvolution2dWorkload_Execute", this->GetGuid());
161 // The constant tensors may not be fully in place until the workload is Executed
162 if (!prepared)
163 {
164 InitializeArmComputeTensorData(*m_KernelTensor, m_KernelTensorInfo, m_Data.m_Inputs[1]);
165
166 if (m_Data.m_Parameters.m_BiasEnabled)
167 {
168 InitializeArmComputeTensorData(*m_BiasTensor, m_BiasTensorInfo, m_Data.m_Inputs[2]);
169 }
170 m_ConvolutionLayer->prepare();
171 FreeTensorIfUnused(m_KernelTensor);
172 FreeTensorIfUnused(m_BiasTensor);
173 prepared = true;
174 }
175 m_ConvolutionLayer->run();
176 }
177
GetConvolutionMethod() const178 arm_compute::ConvolutionMethod NeonConvolution2dWorkload::GetConvolutionMethod() const
179 {
180 return m_ConvolutionMethod;
181 }
182
183 } //namespace armnn
184