blob: 26c68b7d1d8b0f06c9f45153c5aa36835c534c06 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
Teresa Charlin588cbdf2022-01-19 15:55:37 +00002// Copyright © 2017 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);
33 const arm_compute::TensorInfo aclWeights = BuildArmComputeTensorInfo(weights);
34
35 arm_compute::TensorInfo aclBiases;
Keith Davis2d0679f2021-08-05 11:35:00 +010036 arm_compute::TensorInfo* optionalAclBiases = nullptr;
telsoa01c577f2c2018-08-31 09:22:23 +010037 if (descriptor.m_BiasEnabled)
38 {
Matthew Bentham67d63902022-02-08 15:03:07 +000039 ARMNN_ASSERT(biases.has_value());
40 aclBiases = BuildArmComputeTensorInfo(biases.value());
telsoa01c577f2c2018-08-31 09:22:23 +010041 optionalAclBiases = &aclBiases;
42 }
43
44 const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo =
Mike Kelly07810fc2020-11-12 10:58:48 +000045 ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor, activationDescriptor);
telsoa01c577f2c2018-08-31 09:22:23 +010046
47 return arm_compute::NEFullyConnectedLayer::validate(&aclInput,
48 &aclWeights,
49 optionalAclBiases,
50 &aclOutput,
51 fullyConnectedLayerInfo);
52}
53
kevmay01e448be32018-09-26 10:21:55 +010054NeonFullyConnectedWorkload::NeonFullyConnectedWorkload(const FullyConnectedQueueDescriptor& descriptor,
Keith Davis2d0679f2021-08-05 11:35:00 +010055 const WorkloadInfo& info,
56 ACLMemManagerOnDemand& memoryManager)
Teresa Charlin588cbdf2022-01-19 15:55:37 +000057 : NeonBaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info)
telsoa014fcda012018-03-09 14:13:49 +000058{
kevmay01e448be32018-09-26 10:21:55 +010059 m_Data.ValidateInputsOutputs("NeonFullyConnectedWorkload", 1, 1);
telsoa014fcda012018-03-09 14:13:49 +000060
Jan Eilersbb446e52020-04-02 13:56:54 +010061 arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
62 arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
telsoa014fcda012018-03-09 14:13:49 +000063
telsoa01c577f2c2018-08-31 09:22:23 +010064 m_WeightsTensor = std::make_unique<arm_compute::Tensor>();
65 BuildArmComputeTensor(*m_WeightsTensor, m_Data.m_Weight->GetTensorInfo());
telsoa014fcda012018-03-09 14:13:49 +000066
telsoa014fcda012018-03-09 14:13:49 +000067 if (m_Data.m_Parameters.m_BiasEnabled)
68 {
telsoa01c577f2c2018-08-31 09:22:23 +010069 m_BiasesTensor = std::make_unique<arm_compute::Tensor>();
70 BuildArmComputeTensor(*m_BiasesTensor, m_Data.m_Bias->GetTensorInfo());
telsoa014fcda012018-03-09 14:13:49 +000071 }
72
Mike Kelly07810fc2020-11-12 10:58:48 +000073 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
74
Keith Davis2d0679f2021-08-05 11:35:00 +010075 arm_compute::FullyConnectedLayerInfo fc_info =
76 ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor.m_Parameters, activationInfo);
Matthew Benthamd80a7122019-01-08 17:52:37 +000077
78 auto layer = std::make_unique<arm_compute::NEFullyConnectedLayer>(memoryManager);
79 layer->configure(&input, m_WeightsTensor.get(), m_BiasesTensor.get(), &output, fc_info);
80 m_FullyConnectedLayer.reset(layer.release());
telsoa014fcda012018-03-09 14:13:49 +000081
82 // Allocate
Derek Lambertif90c56d2020-01-10 17:14:08 +000083 if (m_Data.m_Weight->GetTensorInfo().GetDataType() == DataType::QAsymmU8)
kevmay01e448be32018-09-26 10:21:55 +010084 {
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +010085 InitializeArmComputeTensorData(*m_WeightsTensor, m_Data.m_Weight);
kevmay01e448be32018-09-26 10:21:55 +010086 }
87 else
88 {
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +010089 InitializeArmComputeTensorData(*m_WeightsTensor, m_Data.m_Weight);
kevmay01e448be32018-09-26 10:21:55 +010090 }
telsoa014fcda012018-03-09 14:13:49 +000091
telsoa01c577f2c2018-08-31 09:22:23 +010092 if (m_BiasesTensor)
telsoa014fcda012018-03-09 14:13:49 +000093 {
kevmay01e448be32018-09-26 10:21:55 +010094 if (m_Data.m_Bias->GetTensorInfo().GetDataType() == DataType::Signed32)
95 {
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +010096 InitializeArmComputeTensorData(*m_BiasesTensor, m_Data.m_Bias);
kevmay01e448be32018-09-26 10:21:55 +010097 }
98 else
99 {
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +0100100 InitializeArmComputeTensorData(*m_BiasesTensor, m_Data.m_Bias);
kevmay01e448be32018-09-26 10:21:55 +0100101 }
telsoa014fcda012018-03-09 14:13:49 +0000102 }
telsoa01c577f2c2018-08-31 09:22:23 +0100103
Keith Davis2d0679f2021-08-05 11:35:00 +0100104 // Add details for profiling output
105 WorkloadInfo detailsInfo;
106
107 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
108 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
109 detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Weight->GetTensorInfo());
110 if (descriptor.m_Parameters.m_BiasEnabled)
111 {
112 detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Bias->GetTensorInfo());
113 }
114
115 // Report Profiling Details
116 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonFullyConnectedWorkload_Construct",
117 descriptor.m_Parameters,
118 detailsInfo,
119 this->GetGuid());
120
telsoa01c577f2c2018-08-31 09:22:23 +0100121 // Force Compute Library to perform the necessary copying and reshaping, after which
122 // delete all the input tensors that will no longer be needed
Matthew Benthamd80a7122019-01-08 17:52:37 +0000123 m_FullyConnectedLayer->prepare();
telsoa01c577f2c2018-08-31 09:22:23 +0100124 FreeUnusedTensors();
telsoa014fcda012018-03-09 14:13:49 +0000125}
126
kevmay01e448be32018-09-26 10:21:55 +0100127void NeonFullyConnectedWorkload::Execute() const
telsoa014fcda012018-03-09 14:13:49 +0000128{
Keith Davis2d0679f2021-08-05 11:35:00 +0100129 ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID("NeonFullyConnectedWorkload_Execute", this->GetGuid());
Matthew Benthamd80a7122019-01-08 17:52:37 +0000130 m_FullyConnectedLayer->run();
telsoa014fcda012018-03-09 14:13:49 +0000131}
132
kevmay01e448be32018-09-26 10:21:55 +0100133void NeonFullyConnectedWorkload::FreeUnusedTensors()
telsoa01c577f2c2018-08-31 09:22:23 +0100134{
135 FreeTensorIfUnused(m_WeightsTensor);
136 FreeTensorIfUnused(m_BiasesTensor);
137}
138
telsoa014fcda012018-03-09 14:13:49 +0000139} //namespace armnn