blob: 94dc07704df23acb25623baa1a3ba6ac307ac220 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. 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
James Conroy1f58f032021-04-27 17:13:27 +010015#include <backendsCommon/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,
27 const 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 {
Keith Davis2d0679f2021-08-05 11:35:00 +010039 aclBiases = BuildArmComputeTensorInfo(biases);
telsoa01c577f2c2018-08-31 09:22:23 +010040 optionalAclBiases = &aclBiases;
41 }
42
43 const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo =
Mike Kelly07810fc2020-11-12 10:58:48 +000044 ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor, activationDescriptor);
telsoa01c577f2c2018-08-31 09:22:23 +010045
46 return arm_compute::NEFullyConnectedLayer::validate(&aclInput,
47 &aclWeights,
48 optionalAclBiases,
49 &aclOutput,
50 fullyConnectedLayerInfo);
51}
52
kevmay01e448be32018-09-26 10:21:55 +010053NeonFullyConnectedWorkload::NeonFullyConnectedWorkload(const FullyConnectedQueueDescriptor& descriptor,
Keith Davis2d0679f2021-08-05 11:35:00 +010054 const WorkloadInfo& info,
55 ACLMemManagerOnDemand& memoryManager)
kevmay01e448be32018-09-26 10:21:55 +010056 : BaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info)
telsoa014fcda012018-03-09 14:13:49 +000057{
kevmay01e448be32018-09-26 10:21:55 +010058 m_Data.ValidateInputsOutputs("NeonFullyConnectedWorkload", 1, 1);
telsoa014fcda012018-03-09 14:13:49 +000059
Jan Eilersbb446e52020-04-02 13:56:54 +010060 arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
61 arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
telsoa014fcda012018-03-09 14:13:49 +000062
telsoa01c577f2c2018-08-31 09:22:23 +010063 m_WeightsTensor = std::make_unique<arm_compute::Tensor>();
64 BuildArmComputeTensor(*m_WeightsTensor, m_Data.m_Weight->GetTensorInfo());
telsoa014fcda012018-03-09 14:13:49 +000065
telsoa014fcda012018-03-09 14:13:49 +000066 if (m_Data.m_Parameters.m_BiasEnabled)
67 {
telsoa01c577f2c2018-08-31 09:22:23 +010068 m_BiasesTensor = std::make_unique<arm_compute::Tensor>();
69 BuildArmComputeTensor(*m_BiasesTensor, m_Data.m_Bias->GetTensorInfo());
telsoa014fcda012018-03-09 14:13:49 +000070 }
71
Mike Kelly07810fc2020-11-12 10:58:48 +000072 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
73
Keith Davis2d0679f2021-08-05 11:35:00 +010074 arm_compute::FullyConnectedLayerInfo fc_info =
75 ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor.m_Parameters, activationInfo);
Matthew Benthamd80a7122019-01-08 17:52:37 +000076
77 auto layer = std::make_unique<arm_compute::NEFullyConnectedLayer>(memoryManager);
78 layer->configure(&input, m_WeightsTensor.get(), m_BiasesTensor.get(), &output, fc_info);
79 m_FullyConnectedLayer.reset(layer.release());
telsoa014fcda012018-03-09 14:13:49 +000080
81 // Allocate
Derek Lambertif90c56d2020-01-10 17:14:08 +000082 if (m_Data.m_Weight->GetTensorInfo().GetDataType() == DataType::QAsymmU8)
kevmay01e448be32018-09-26 10:21:55 +010083 {
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +010084 InitializeArmComputeTensorData(*m_WeightsTensor, m_Data.m_Weight);
kevmay01e448be32018-09-26 10:21:55 +010085 }
86 else
87 {
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +010088 InitializeArmComputeTensorData(*m_WeightsTensor, m_Data.m_Weight);
kevmay01e448be32018-09-26 10:21:55 +010089 }
telsoa014fcda012018-03-09 14:13:49 +000090
telsoa01c577f2c2018-08-31 09:22:23 +010091 if (m_BiasesTensor)
telsoa014fcda012018-03-09 14:13:49 +000092 {
kevmay01e448be32018-09-26 10:21:55 +010093 if (m_Data.m_Bias->GetTensorInfo().GetDataType() == DataType::Signed32)
94 {
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +010095 InitializeArmComputeTensorData(*m_BiasesTensor, m_Data.m_Bias);
kevmay01e448be32018-09-26 10:21:55 +010096 }
97 else
98 {
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +010099 InitializeArmComputeTensorData(*m_BiasesTensor, m_Data.m_Bias);
kevmay01e448be32018-09-26 10:21:55 +0100100 }
telsoa014fcda012018-03-09 14:13:49 +0000101 }
telsoa01c577f2c2018-08-31 09:22:23 +0100102
Keith Davis2d0679f2021-08-05 11:35:00 +0100103 // Add details for profiling output
104 WorkloadInfo detailsInfo;
105
106 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
107 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
108 detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Weight->GetTensorInfo());
109 if (descriptor.m_Parameters.m_BiasEnabled)
110 {
111 detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(descriptor.m_Bias->GetTensorInfo());
112 }
113
114 // Report Profiling Details
115 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonFullyConnectedWorkload_Construct",
116 descriptor.m_Parameters,
117 detailsInfo,
118 this->GetGuid());
119
telsoa01c577f2c2018-08-31 09:22:23 +0100120 // Force Compute Library to perform the necessary copying and reshaping, after which
121 // delete all the input tensors that will no longer be needed
Matthew Benthamd80a7122019-01-08 17:52:37 +0000122 m_FullyConnectedLayer->prepare();
telsoa01c577f2c2018-08-31 09:22:23 +0100123 FreeUnusedTensors();
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());
Matthew Benthamd80a7122019-01-08 17:52:37 +0000129 m_FullyConnectedLayer->run();
telsoa014fcda012018-03-09 14:13:49 +0000130}
131
kevmay01e448be32018-09-26 10:21:55 +0100132void NeonFullyConnectedWorkload::FreeUnusedTensors()
telsoa01c577f2c2018-08-31 09:22:23 +0100133{
134 FreeTensorIfUnused(m_WeightsTensor);
135 FreeTensorIfUnused(m_BiasesTensor);
136}
137
telsoa014fcda012018-03-09 14:13:49 +0000138} //namespace armnn