blob: ee3de807e660e1e79b465977ad1173ff26a8b231 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
Teresa Charlin6fba2942023-04-18 12:48:46 +01002// Copyright © 2017,2022-2023 Arm Ltd and Contributors. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5
kevmay01e448be32018-09-26 10:21:55 +01006#include "NeonFullyConnectedWorkload.hpp"
telsoa01c577f2c2018-08-31 09:22:23 +01007
Matthew Benthamd80a7122019-01-08 17:52:37 +00008#include "NeonWorkloadUtils.hpp"
Mike Kelly07810fc2020-11-12 10:58:48 +00009
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +000010#include <aclCommon/ArmComputeTensorUtils.hpp>
11#include <aclCommon/ArmComputeUtils.hpp>
Mike Kelly07810fc2020-11-12 10:58:48 +000012
Jan Eilersbb446e52020-04-02 13:56:54 +010013#include <armnn/utility/PolymorphicDowncast.hpp>
Mike Kelly07810fc2020-11-12 10:58:48 +000014
Colm Donelan0c479742021-12-10 12:43:54 +000015#include <armnn/backends/TensorHandle.hpp>
telsoa014fcda012018-03-09 14:13:49 +000016
Matthew Benthamd80a7122019-01-08 17:52:37 +000017#include <arm_compute/runtime/NEON/functions/NEFullyConnectedLayer.h>
18
telsoa014fcda012018-03-09 14:13:49 +000019namespace armnn
20{
21using namespace armcomputetensorutils;
Keith Davis2d0679f2021-08-05 11:35:00 +010022using ACLMemManagerOnDemand = std::shared_ptr<arm_compute::MemoryManagerOnDemand>;
telsoa014fcda012018-03-09 14:13:49 +000023
telsoa01c577f2c2018-08-31 09:22:23 +010024arm_compute::Status NeonFullyConnectedWorkloadValidate(const TensorInfo& input,
25 const TensorInfo& output,
26 const TensorInfo& weights,
Matthew Bentham67d63902022-02-08 15:03:07 +000027 const Optional<TensorInfo>& biases,
Mike Kelly07810fc2020-11-12 10:58:48 +000028 const FullyConnectedDescriptor& descriptor,
29 const ActivationDescriptor* activationDescriptor)
telsoa01c577f2c2018-08-31 09:22:23 +010030{
31 const arm_compute::TensorInfo aclInput = BuildArmComputeTensorInfo(input);
32 const arm_compute::TensorInfo aclOutput = BuildArmComputeTensorInfo(output);
Cathal Corbett4452baf2022-05-13 09:55:59 +010033 arm_compute::TensorInfo aclWeights = BuildArmComputeTensorInfo(weights);
34 aclWeights.set_are_values_constant(weights.IsConstant());
telsoa01c577f2c2018-08-31 09:22:23 +010035
36 arm_compute::TensorInfo aclBiases;
Keith Davis2d0679f2021-08-05 11:35:00 +010037 arm_compute::TensorInfo* optionalAclBiases = nullptr;
telsoa01c577f2c2018-08-31 09:22:23 +010038 if (descriptor.m_BiasEnabled)
39 {
Matthew Bentham67d63902022-02-08 15:03:07 +000040 ARMNN_ASSERT(biases.has_value());
TeresaARM6d63e222023-05-11 15:16:39 +000041 // Same for bias as weights. We don't currently support non const.
42 if (!biases.value().IsConstant())
43 {
44 return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
45 "Arm NN NeonFullyConnectedWorkload does not support non constant bias."};
46 }
Matthew Bentham67d63902022-02-08 15:03:07 +000047 aclBiases = BuildArmComputeTensorInfo(biases.value());
Cathal Corbett4452baf2022-05-13 09:55:59 +010048 aclBiases.set_are_values_constant(biases.value().IsConstant());
telsoa01c577f2c2018-08-31 09:22:23 +010049 optionalAclBiases = &aclBiases;
50 }
51
52 const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo =
Mike Kelly07810fc2020-11-12 10:58:48 +000053 ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor, activationDescriptor);
telsoa01c577f2c2018-08-31 09:22:23 +010054 return arm_compute::NEFullyConnectedLayer::validate(&aclInput,
55 &aclWeights,
56 optionalAclBiases,
57 &aclOutput,
58 fullyConnectedLayerInfo);
59}
60
kevmay01e448be32018-09-26 10:21:55 +010061NeonFullyConnectedWorkload::NeonFullyConnectedWorkload(const FullyConnectedQueueDescriptor& descriptor,
Keith Davis2d0679f2021-08-05 11:35:00 +010062 const WorkloadInfo& info,
63 ACLMemManagerOnDemand& memoryManager)
Teresa Charlin588cbdf2022-01-19 15:55:37 +000064 : NeonBaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info)
telsoa014fcda012018-03-09 14:13:49 +000065{
Teresa Charlin6fba2942023-04-18 12:48:46 +010066 m_Data.ValidateInputsOutputs("NeonFullyConnectedWorkload", descriptor.m_Parameters.GetNumInputs(), 1);
telsoa014fcda012018-03-09 14:13:49 +000067
Jan Eilersbb446e52020-04-02 13:56:54 +010068 arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
Teresa Charlin6fba2942023-04-18 12:48:46 +010069 arm_compute::ITensor& weights = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
Mike Kellyec67a0f2022-11-25 13:55:24 +000070 m_WeightsTensorInfo = info.m_InputTensorInfos[1];
Teresa Charlin6fba2942023-04-18 12:48:46 +010071 weights.info()->set_are_values_constant(m_WeightsTensorInfo.IsConstant());
72 arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
73 if (m_WeightsTensorInfo.IsConstant())
74 {
75 // Copy the weights' tensor into arm_compute tensor.
76 m_WeightsTensor = std::make_unique<arm_compute::Tensor>();
77 BuildArmComputeTensor(*m_WeightsTensor, m_WeightsTensorInfo);
78 m_WeightsTensor->info()->set_are_values_constant(m_WeightsTensorInfo.IsConstant());
79 }
Mike Kellyec67a0f2022-11-25 13:55:24 +000080
telsoa014fcda012018-03-09 14:13:49 +000081 if (m_Data.m_Parameters.m_BiasEnabled)
82 {
Cathal Corbett4452baf2022-05-13 09:55:59 +010083 // Copy the biases tensor into arm_compute tensor.
telsoa01c577f2c2018-08-31 09:22:23 +010084 m_BiasesTensor = std::make_unique<arm_compute::Tensor>();
Mike Kellyec67a0f2022-11-25 13:55:24 +000085 m_BiasesTensorInfo = info.m_InputTensorInfos[2];
86 BuildArmComputeTensor(*m_BiasesTensor, m_BiasesTensorInfo);
Teresa Charlin6fba2942023-04-18 12:48:46 +010087 m_BiasesTensor->info()->set_are_values_constant(m_BiasesTensorInfo.IsConstant());
TeresaARM6d63e222023-05-11 15:16:39 +000088
89 // We do not support dynamic bias
90 ARMNN_ASSERT(m_BiasesTensorInfo.IsConstant() == true);
telsoa014fcda012018-03-09 14:13:49 +000091 }
92
Mike Kelly07810fc2020-11-12 10:58:48 +000093 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
Keith Davis2d0679f2021-08-05 11:35:00 +010094 arm_compute::FullyConnectedLayerInfo fc_info =
95 ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor.m_Parameters, activationInfo);
Matthew Benthamd80a7122019-01-08 17:52:37 +000096
97 auto layer = std::make_unique<arm_compute::NEFullyConnectedLayer>(memoryManager);
Teresa Charlin6fba2942023-04-18 12:48:46 +010098 if (m_WeightsTensorInfo.IsConstant())
99 {
100 layer->configure(&input, m_WeightsTensor.get(), m_BiasesTensor.get(), &output, fc_info);
101 }
102 else
103 {
104 layer->configure(&input, &weights, m_BiasesTensor.get(), &output, fc_info);
105 }
Matthew Benthamd80a7122019-01-08 17:52:37 +0000106 m_FullyConnectedLayer.reset(layer.release());
telsoa014fcda012018-03-09 14:13:49 +0000107
Keith Davis2d0679f2021-08-05 11:35:00 +0100108 // Add details for profiling output
109 WorkloadInfo detailsInfo;
110
111 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
112 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
Mike Kellyec67a0f2022-11-25 13:55:24 +0000113 detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[1]);
Keith Davis2d0679f2021-08-05 11:35:00 +0100114 if (descriptor.m_Parameters.m_BiasEnabled)
115 {
Mike Kellyec67a0f2022-11-25 13:55:24 +0000116 detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[2]);
Keith Davis2d0679f2021-08-05 11:35:00 +0100117 }
118
119 // Report Profiling Details
120 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonFullyConnectedWorkload_Construct",
121 descriptor.m_Parameters,
122 detailsInfo,
123 this->GetGuid());
telsoa014fcda012018-03-09 14:13:49 +0000124}
125
kevmay01e448be32018-09-26 10:21:55 +0100126void NeonFullyConnectedWorkload::Execute() const
telsoa014fcda012018-03-09 14:13:49 +0000127{
Keith Davis2d0679f2021-08-05 11:35:00 +0100128 ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID("NeonFullyConnectedWorkload_Execute", this->GetGuid());
Mike Kellyec67a0f2022-11-25 13:55:24 +0000129 // The constant tensors may not be fully in place until the workload is Executed
130 if (!prepared)
131 {
Teresa Charlin6fba2942023-04-18 12:48:46 +0100132 if (m_WeightsTensorInfo.IsConstant())
133 {
134 InitializeArmComputeTensorData(*m_WeightsTensor, m_WeightsTensorInfo, m_Data.m_Inputs[1]);
135 m_WeightsTensor->info()->set_are_values_constant(m_WeightsTensorInfo.IsConstant());
136 }
Mike Kellyec67a0f2022-11-25 13:55:24 +0000137
138 if (m_Data.m_Parameters.m_BiasEnabled)
139 {
140 InitializeArmComputeTensorData(*m_BiasesTensor, m_BiasesTensorInfo, m_Data.m_Inputs[2]);
Teresa Charlin6fba2942023-04-18 12:48:46 +0100141 m_BiasesTensor->info()->set_are_values_constant(m_BiasesTensorInfo.IsConstant());
Mike Kellyec67a0f2022-11-25 13:55:24 +0000142 }
Teresa Charlin6fba2942023-04-18 12:48:46 +0100143 if (m_WeightsTensorInfo.IsConstant())
144 {
145 FreeTensorIfUnused(m_WeightsTensor);
146 }
147 if (m_BiasesTensorInfo.IsConstant())
148 {
149 FreeTensorIfUnused(m_BiasesTensor);
150 }
Mike Kellyec67a0f2022-11-25 13:55:24 +0000151 prepared = true;
152 }
Matthew Benthamd80a7122019-01-08 17:52:37 +0000153 m_FullyConnectedLayer->run();
telsoa014fcda012018-03-09 14:13:49 +0000154}
155
156} //namespace armnn