blob: 9503abdee8c2eec3db58942e816c215b4ab9a544 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
Mike Kelly7cbe7812023-07-25 17:37:33 +01002// Copyright © 2017-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());
41 aclBiases = BuildArmComputeTensorInfo(biases.value());
Cathal Corbett4452baf2022-05-13 09:55:59 +010042 aclBiases.set_are_values_constant(biases.value().IsConstant());
telsoa01c577f2c2018-08-31 09:22:23 +010043 optionalAclBiases = &aclBiases;
44 }
45
46 const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo =
Mike Kelly07810fc2020-11-12 10:58:48 +000047 ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor, activationDescriptor);
telsoa01c577f2c2018-08-31 09:22:23 +010048 return arm_compute::NEFullyConnectedLayer::validate(&aclInput,
49 &aclWeights,
50 optionalAclBiases,
51 &aclOutput,
52 fullyConnectedLayerInfo);
53}
54
kevmay01e448be32018-09-26 10:21:55 +010055NeonFullyConnectedWorkload::NeonFullyConnectedWorkload(const FullyConnectedQueueDescriptor& descriptor,
Keith Davis2d0679f2021-08-05 11:35:00 +010056 const WorkloadInfo& info,
57 ACLMemManagerOnDemand& memoryManager)
Teresa Charlin588cbdf2022-01-19 15:55:37 +000058 : NeonBaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info)
telsoa014fcda012018-03-09 14:13:49 +000059{
Teresa Charlin6fba2942023-04-18 12:48:46 +010060 m_Data.ValidateInputsOutputs("NeonFullyConnectedWorkload", descriptor.m_Parameters.GetNumInputs(), 1);
telsoa014fcda012018-03-09 14:13:49 +000061
Jan Eilersbb446e52020-04-02 13:56:54 +010062 arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
Teresa Charlin6fba2942023-04-18 12:48:46 +010063 arm_compute::ITensor& weights = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
Mike Kellyec67a0f2022-11-25 13:55:24 +000064 m_WeightsTensorInfo = info.m_InputTensorInfos[1];
Teresa Charlin6fba2942023-04-18 12:48:46 +010065 weights.info()->set_are_values_constant(m_WeightsTensorInfo.IsConstant());
66 arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
67 if (m_WeightsTensorInfo.IsConstant())
68 {
69 // Copy the weights' tensor into arm_compute tensor.
70 m_WeightsTensor = std::make_unique<arm_compute::Tensor>();
71 BuildArmComputeTensor(*m_WeightsTensor, m_WeightsTensorInfo);
72 m_WeightsTensor->info()->set_are_values_constant(m_WeightsTensorInfo.IsConstant());
73 }
Mike Kellyec67a0f2022-11-25 13:55:24 +000074
telsoa014fcda012018-03-09 14:13:49 +000075 if (m_Data.m_Parameters.m_BiasEnabled)
76 {
Cathal Corbett4452baf2022-05-13 09:55:59 +010077 // Copy the biases tensor into arm_compute tensor.
telsoa01c577f2c2018-08-31 09:22:23 +010078 m_BiasesTensor = std::make_unique<arm_compute::Tensor>();
Mike Kellyec67a0f2022-11-25 13:55:24 +000079 m_BiasesTensorInfo = info.m_InputTensorInfos[2];
80 BuildArmComputeTensor(*m_BiasesTensor, m_BiasesTensorInfo);
Teresa Charlin6fba2942023-04-18 12:48:46 +010081 m_BiasesTensor->info()->set_are_values_constant(m_BiasesTensorInfo.IsConstant());
telsoa014fcda012018-03-09 14:13:49 +000082 }
83
Mike Kelly07810fc2020-11-12 10:58:48 +000084 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
Keith Davis2d0679f2021-08-05 11:35:00 +010085 arm_compute::FullyConnectedLayerInfo fc_info =
86 ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor.m_Parameters, activationInfo);
Matthew Benthamd80a7122019-01-08 17:52:37 +000087
88 auto layer = std::make_unique<arm_compute::NEFullyConnectedLayer>(memoryManager);
Teresa Charlin6fba2942023-04-18 12:48:46 +010089 if (m_WeightsTensorInfo.IsConstant())
90 {
91 layer->configure(&input, m_WeightsTensor.get(), m_BiasesTensor.get(), &output, fc_info);
92 }
93 else
94 {
95 layer->configure(&input, &weights, m_BiasesTensor.get(), &output, fc_info);
96 }
Matthew Benthamd80a7122019-01-08 17:52:37 +000097 m_FullyConnectedLayer.reset(layer.release());
telsoa014fcda012018-03-09 14:13:49 +000098
Keith Davis2d0679f2021-08-05 11:35:00 +010099 // Add details for profiling output
100 WorkloadInfo detailsInfo;
101
102 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
103 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
Keith Davis2d0679f2021-08-05 11:35:00 +0100104
105 // Report Profiling Details
106 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonFullyConnectedWorkload_Construct",
107 descriptor.m_Parameters,
108 detailsInfo,
109 this->GetGuid());
telsoa014fcda012018-03-09 14:13:49 +0000110}
111
kevmay01e448be32018-09-26 10:21:55 +0100112void NeonFullyConnectedWorkload::Execute() const
telsoa014fcda012018-03-09 14:13:49 +0000113{
Mike Kelly7cbe7812023-07-25 17:37:33 +0100114 ARMNN_SCOPED_PROFILING_EVENT_NEON_NAME_GUID("NeonFullyConnectedWorkload_Execute");
Mike Kellyec67a0f2022-11-25 13:55:24 +0000115 // The constant tensors may not be fully in place until the workload is Executed
116 if (!prepared)
117 {
Teresa Charlin6fba2942023-04-18 12:48:46 +0100118 if (m_WeightsTensorInfo.IsConstant())
119 {
120 InitializeArmComputeTensorData(*m_WeightsTensor, m_WeightsTensorInfo, m_Data.m_Inputs[1]);
121 m_WeightsTensor->info()->set_are_values_constant(m_WeightsTensorInfo.IsConstant());
122 }
Mike Kellyec67a0f2022-11-25 13:55:24 +0000123
124 if (m_Data.m_Parameters.m_BiasEnabled)
125 {
126 InitializeArmComputeTensorData(*m_BiasesTensor, m_BiasesTensorInfo, m_Data.m_Inputs[2]);
Teresa Charlin6fba2942023-04-18 12:48:46 +0100127 m_BiasesTensor->info()->set_are_values_constant(m_BiasesTensorInfo.IsConstant());
Mike Kellyec67a0f2022-11-25 13:55:24 +0000128 }
Teresa Charlin6fba2942023-04-18 12:48:46 +0100129 if (m_WeightsTensorInfo.IsConstant())
130 {
131 FreeTensorIfUnused(m_WeightsTensor);
132 }
133 if (m_BiasesTensorInfo.IsConstant())
134 {
135 FreeTensorIfUnused(m_BiasesTensor);
136 }
Mike Kellyec67a0f2022-11-25 13:55:24 +0000137 prepared = true;
138 }
Matthew Benthamd80a7122019-01-08 17:52:37 +0000139 m_FullyConnectedLayer->run();
telsoa014fcda012018-03-09 14:13:49 +0000140}
141
142} //namespace armnn