blob: 713771be916804ee3a475dc4e70bb11ecc2820cb [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;
22
telsoa01c577f2c2018-08-31 09:22:23 +010023arm_compute::Status NeonFullyConnectedWorkloadValidate(const TensorInfo& input,
24 const TensorInfo& output,
25 const TensorInfo& weights,
26 const TensorInfo& biases,
Mike Kelly07810fc2020-11-12 10:58:48 +000027 const FullyConnectedDescriptor& descriptor,
28 const ActivationDescriptor* activationDescriptor)
telsoa01c577f2c2018-08-31 09:22:23 +010029{
30 const arm_compute::TensorInfo aclInput = BuildArmComputeTensorInfo(input);
31 const arm_compute::TensorInfo aclOutput = BuildArmComputeTensorInfo(output);
32 const arm_compute::TensorInfo aclWeights = BuildArmComputeTensorInfo(weights);
33
34 arm_compute::TensorInfo aclBiases;
35 arm_compute::TensorInfo *optionalAclBiases = nullptr;
36 if (descriptor.m_BiasEnabled)
37 {
38 aclBiases = BuildArmComputeTensorInfo(biases);
39 optionalAclBiases = &aclBiases;
40 }
41
42 const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo =
Mike Kelly07810fc2020-11-12 10:58:48 +000043 ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor, activationDescriptor);
telsoa01c577f2c2018-08-31 09:22:23 +010044
45 return arm_compute::NEFullyConnectedLayer::validate(&aclInput,
46 &aclWeights,
47 optionalAclBiases,
48 &aclOutput,
49 fullyConnectedLayerInfo);
50}
51
kevmay01e448be32018-09-26 10:21:55 +010052NeonFullyConnectedWorkload::NeonFullyConnectedWorkload(const FullyConnectedQueueDescriptor& descriptor,
surmeh013537c2c2018-05-18 16:31:43 +010053 const WorkloadInfo& info, std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
kevmay01e448be32018-09-26 10:21:55 +010054 : BaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info)
telsoa014fcda012018-03-09 14:13:49 +000055{
kevmay01e448be32018-09-26 10:21:55 +010056 m_Data.ValidateInputsOutputs("NeonFullyConnectedWorkload", 1, 1);
telsoa014fcda012018-03-09 14:13:49 +000057
Jan Eilersbb446e52020-04-02 13:56:54 +010058 arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
59 arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
telsoa014fcda012018-03-09 14:13:49 +000060
telsoa01c577f2c2018-08-31 09:22:23 +010061 m_WeightsTensor = std::make_unique<arm_compute::Tensor>();
62 BuildArmComputeTensor(*m_WeightsTensor, m_Data.m_Weight->GetTensorInfo());
telsoa014fcda012018-03-09 14:13:49 +000063
telsoa014fcda012018-03-09 14:13:49 +000064 if (m_Data.m_Parameters.m_BiasEnabled)
65 {
telsoa01c577f2c2018-08-31 09:22:23 +010066 m_BiasesTensor = std::make_unique<arm_compute::Tensor>();
67 BuildArmComputeTensor(*m_BiasesTensor, m_Data.m_Bias->GetTensorInfo());
telsoa014fcda012018-03-09 14:13:49 +000068 }
69
Mike Kelly07810fc2020-11-12 10:58:48 +000070 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
71
72 arm_compute::FullyConnectedLayerInfo fc_info =
73 ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor.m_Parameters, activationInfo);
Matthew Benthamd80a7122019-01-08 17:52:37 +000074
75 auto layer = std::make_unique<arm_compute::NEFullyConnectedLayer>(memoryManager);
76 layer->configure(&input, m_WeightsTensor.get(), m_BiasesTensor.get(), &output, fc_info);
77 m_FullyConnectedLayer.reset(layer.release());
telsoa014fcda012018-03-09 14:13:49 +000078
79 // Allocate
Derek Lambertif90c56d2020-01-10 17:14:08 +000080 if (m_Data.m_Weight->GetTensorInfo().GetDataType() == DataType::QAsymmU8)
kevmay01e448be32018-09-26 10:21:55 +010081 {
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +010082 InitializeArmComputeTensorData(*m_WeightsTensor, m_Data.m_Weight);
kevmay01e448be32018-09-26 10:21:55 +010083 }
84 else
85 {
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +010086 InitializeArmComputeTensorData(*m_WeightsTensor, m_Data.m_Weight);
kevmay01e448be32018-09-26 10:21:55 +010087 }
telsoa014fcda012018-03-09 14:13:49 +000088
telsoa01c577f2c2018-08-31 09:22:23 +010089 if (m_BiasesTensor)
telsoa014fcda012018-03-09 14:13:49 +000090 {
kevmay01e448be32018-09-26 10:21:55 +010091 if (m_Data.m_Bias->GetTensorInfo().GetDataType() == DataType::Signed32)
92 {
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +010093 InitializeArmComputeTensorData(*m_BiasesTensor, m_Data.m_Bias);
kevmay01e448be32018-09-26 10:21:55 +010094 }
95 else
96 {
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +010097 InitializeArmComputeTensorData(*m_BiasesTensor, m_Data.m_Bias);
kevmay01e448be32018-09-26 10:21:55 +010098 }
telsoa014fcda012018-03-09 14:13:49 +000099 }
telsoa01c577f2c2018-08-31 09:22:23 +0100100
101 // Force Compute Library to perform the necessary copying and reshaping, after which
102 // delete all the input tensors that will no longer be needed
Matthew Benthamd80a7122019-01-08 17:52:37 +0000103 m_FullyConnectedLayer->prepare();
telsoa01c577f2c2018-08-31 09:22:23 +0100104 FreeUnusedTensors();
telsoa014fcda012018-03-09 14:13:49 +0000105}
106
kevmay01e448be32018-09-26 10:21:55 +0100107void NeonFullyConnectedWorkload::Execute() const
telsoa014fcda012018-03-09 14:13:49 +0000108{
kevmay01e448be32018-09-26 10:21:55 +0100109 ARMNN_SCOPED_PROFILING_EVENT_NEON("NeonFullyConnectedWorkload_Execute");
Matthew Benthamd80a7122019-01-08 17:52:37 +0000110 m_FullyConnectedLayer->run();
telsoa014fcda012018-03-09 14:13:49 +0000111}
112
kevmay01e448be32018-09-26 10:21:55 +0100113void NeonFullyConnectedWorkload::FreeUnusedTensors()
telsoa01c577f2c2018-08-31 09:22:23 +0100114{
115 FreeTensorIfUnused(m_WeightsTensor);
116 FreeTensorIfUnused(m_BiasesTensor);
117}
118
telsoa014fcda012018-03-09 14:13:49 +0000119} //namespace armnn