blob: 7bb23f870bf1b93f51ca60c8189cae57c23796ef [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
Mike Kellyec67a0f2022-11-25 13:55:24 +00002// Copyright © 2017,2022 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());
Cathal Corbett4452baf2022-05-13 09:55:59 +010041 // 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{
kevmay01e448be32018-09-26 10:21:55 +010066 m_Data.ValidateInputsOutputs("NeonFullyConnectedWorkload", 1, 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();
69 arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
telsoa014fcda012018-03-09 14:13:49 +000070
Cathal Corbett4452baf2022-05-13 09:55:59 +010071 // Copy the weights' tensor into arm_compute tensor.
telsoa01c577f2c2018-08-31 09:22:23 +010072 m_WeightsTensor = std::make_unique<arm_compute::Tensor>();
Mike Kellyec67a0f2022-11-25 13:55:24 +000073 m_WeightsTensorInfo = info.m_InputTensorInfos[1];
74 BuildArmComputeTensor(*m_WeightsTensor, m_WeightsTensorInfo);
75
telsoa014fcda012018-03-09 14:13:49 +000076 if (m_Data.m_Parameters.m_BiasEnabled)
77 {
Cathal Corbett4452baf2022-05-13 09:55:59 +010078 // Copy the biases tensor into arm_compute tensor.
telsoa01c577f2c2018-08-31 09:22:23 +010079 m_BiasesTensor = std::make_unique<arm_compute::Tensor>();
Mike Kellyec67a0f2022-11-25 13:55:24 +000080 m_BiasesTensorInfo = info.m_InputTensorInfos[2];
81 BuildArmComputeTensor(*m_BiasesTensor, m_BiasesTensorInfo);
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);
89 layer->configure(&input, m_WeightsTensor.get(), m_BiasesTensor.get(), &output, fc_info);
90 m_FullyConnectedLayer.reset(layer.release());
telsoa014fcda012018-03-09 14:13:49 +000091
Keith Davis2d0679f2021-08-05 11:35:00 +010092 // Add details for profiling output
93 WorkloadInfo detailsInfo;
94
95 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
96 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
Mike Kellyec67a0f2022-11-25 13:55:24 +000097 detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[1]);
Keith Davis2d0679f2021-08-05 11:35:00 +010098 if (descriptor.m_Parameters.m_BiasEnabled)
99 {
Mike Kellyec67a0f2022-11-25 13:55:24 +0000100 detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[2]);
Keith Davis2d0679f2021-08-05 11:35:00 +0100101 }
102
103 // Report Profiling Details
104 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonFullyConnectedWorkload_Construct",
105 descriptor.m_Parameters,
106 detailsInfo,
107 this->GetGuid());
108
Cathal Corbett4452baf2022-05-13 09:55:59 +0100109 // Force Compute Library to perform the necessary copying and reshaping.
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{
Keith Davis2d0679f2021-08-05 11:35:00 +0100114 ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID("NeonFullyConnectedWorkload_Execute", this->GetGuid());
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 {
118 InitializeArmComputeTensorData(*m_WeightsTensor, m_WeightsTensorInfo, m_Data.m_Inputs[1]);
119
120 if (m_Data.m_Parameters.m_BiasEnabled)
121 {
122 InitializeArmComputeTensorData(*m_BiasesTensor, m_BiasesTensorInfo, m_Data.m_Inputs[2]);
123 }
124 m_FullyConnectedLayer->prepare();
125 FreeTensorIfUnused(m_WeightsTensor);
126 FreeTensorIfUnused(m_BiasesTensor);
127 prepared = true;
128 }
Matthew Benthamd80a7122019-01-08 17:52:37 +0000129 m_FullyConnectedLayer->run();
telsoa014fcda012018-03-09 14:13:49 +0000130}
131
132} //namespace armnn