blob: 8cebb4f48f97981cc728ce4491849455862d075d [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
David Beck711fa312018-09-24 10:46:38 +01008#include <backends/aclCommon/ArmComputeTensorUtils.hpp>
9#include <backends/aclCommon/ArmComputeUtils.hpp>
10#include <backends/CpuTensorHandle.hpp>
telsoa014fcda012018-03-09 14:13:49 +000011
telsoa014fcda012018-03-09 14:13:49 +000012namespace armnn
13{
14using namespace armcomputetensorutils;
15
telsoa01c577f2c2018-08-31 09:22:23 +010016arm_compute::Status NeonFullyConnectedWorkloadValidate(const TensorInfo& input,
17 const TensorInfo& output,
18 const TensorInfo& weights,
19 const TensorInfo& biases,
20 const FullyConnectedDescriptor& descriptor)
21{
22 const arm_compute::TensorInfo aclInput = BuildArmComputeTensorInfo(input);
23 const arm_compute::TensorInfo aclOutput = BuildArmComputeTensorInfo(output);
24 const arm_compute::TensorInfo aclWeights = BuildArmComputeTensorInfo(weights);
25
26 arm_compute::TensorInfo aclBiases;
27 arm_compute::TensorInfo *optionalAclBiases = nullptr;
28 if (descriptor.m_BiasEnabled)
29 {
30 aclBiases = BuildArmComputeTensorInfo(biases);
31 optionalAclBiases = &aclBiases;
32 }
33
34 const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo =
35 ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor);
36
37
38 return arm_compute::NEFullyConnectedLayer::validate(&aclInput,
39 &aclWeights,
40 optionalAclBiases,
41 &aclOutput,
42 fullyConnectedLayerInfo);
43}
44
kevmay01e448be32018-09-26 10:21:55 +010045NeonFullyConnectedWorkload::NeonFullyConnectedWorkload(const FullyConnectedQueueDescriptor& descriptor,
surmeh013537c2c2018-05-18 16:31:43 +010046 const WorkloadInfo& info, std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
kevmay01e448be32018-09-26 10:21:55 +010047 : BaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info)
surmeh013537c2c2018-05-18 16:31:43 +010048 , m_FullyConnectedLayer(memoryManager)
telsoa014fcda012018-03-09 14:13:49 +000049{
kevmay01e448be32018-09-26 10:21:55 +010050 m_Data.ValidateInputsOutputs("NeonFullyConnectedWorkload", 1, 1);
telsoa014fcda012018-03-09 14:13:49 +000051
52 arm_compute::ITensor& input = boost::polymorphic_downcast<INeonTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
53 arm_compute::ITensor& output = boost::polymorphic_downcast<INeonTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
54
telsoa01c577f2c2018-08-31 09:22:23 +010055 m_WeightsTensor = std::make_unique<arm_compute::Tensor>();
56 BuildArmComputeTensor(*m_WeightsTensor, m_Data.m_Weight->GetTensorInfo());
telsoa014fcda012018-03-09 14:13:49 +000057
telsoa014fcda012018-03-09 14:13:49 +000058 if (m_Data.m_Parameters.m_BiasEnabled)
59 {
telsoa01c577f2c2018-08-31 09:22:23 +010060 m_BiasesTensor = std::make_unique<arm_compute::Tensor>();
61 BuildArmComputeTensor(*m_BiasesTensor, m_Data.m_Bias->GetTensorInfo());
telsoa014fcda012018-03-09 14:13:49 +000062 }
63
64 // Construct
telsoa01c577f2c2018-08-31 09:22:23 +010065 arm_compute::FullyConnectedLayerInfo fc_info;
66 fc_info.transpose_weights = m_Data.m_Parameters.m_TransposeWeightMatrix;
67 m_FullyConnectedLayer.configure(&input, m_WeightsTensor.get(), m_BiasesTensor.get(), &output, fc_info);
telsoa014fcda012018-03-09 14:13:49 +000068
69 // Allocate
kevmay01e448be32018-09-26 10:21:55 +010070 if (m_Data.m_Weight->GetTensorInfo().GetDataType() == DataType::QuantisedAsymm8)
71 {
72 InitialiseArmComputeTensorData(*m_WeightsTensor, m_Data.m_Weight->GetConstTensor<uint8_t>());
73 }
74 else
75 {
76 InitializeArmComputeTensorDataForFloatTypes(*m_WeightsTensor, m_Data.m_Weight);
77 }
telsoa014fcda012018-03-09 14:13:49 +000078
telsoa01c577f2c2018-08-31 09:22:23 +010079 if (m_BiasesTensor)
telsoa014fcda012018-03-09 14:13:49 +000080 {
kevmay01e448be32018-09-26 10:21:55 +010081 if (m_Data.m_Bias->GetTensorInfo().GetDataType() == DataType::Signed32)
82 {
83 InitialiseArmComputeTensorData(*m_BiasesTensor, m_Data.m_Bias->GetConstTensor<int32_t>());
84 }
85 else
86 {
87 InitializeArmComputeTensorDataForFloatTypes(*m_BiasesTensor, m_Data.m_Bias);
88 }
telsoa014fcda012018-03-09 14:13:49 +000089 }
telsoa01c577f2c2018-08-31 09:22:23 +010090
91 // Force Compute Library to perform the necessary copying and reshaping, after which
92 // delete all the input tensors that will no longer be needed
93 m_FullyConnectedLayer.prepare();
94 FreeUnusedTensors();
telsoa014fcda012018-03-09 14:13:49 +000095}
96
kevmay01e448be32018-09-26 10:21:55 +010097void NeonFullyConnectedWorkload::Execute() const
telsoa014fcda012018-03-09 14:13:49 +000098{
kevmay01e448be32018-09-26 10:21:55 +010099 ARMNN_SCOPED_PROFILING_EVENT_NEON("NeonFullyConnectedWorkload_Execute");
telsoa014fcda012018-03-09 14:13:49 +0000100 m_FullyConnectedLayer.run();
101}
102
kevmay01e448be32018-09-26 10:21:55 +0100103void NeonFullyConnectedWorkload::FreeUnusedTensors()
telsoa01c577f2c2018-08-31 09:22:23 +0100104{
105 FreeTensorIfUnused(m_WeightsTensor);
106 FreeTensorIfUnused(m_BiasesTensor);
107}
108
telsoa014fcda012018-03-09 14:13:49 +0000109} //namespace armnn
110