telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 6 | #include "NeonFullyConnectedWorkload.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 7 | |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 8 | #include "NeonWorkloadUtils.hpp" |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 9 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
| 10 | #include <aclCommon/ArmComputeUtils.hpp> |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 11 | #include <armnn/utility/PolymorphicDowncast.hpp> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 12 | #include <backendsCommon/CpuTensorHandle.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 13 | |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 14 | #include <arm_compute/runtime/NEON/functions/NEFullyConnectedLayer.h> |
| 15 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 16 | namespace armnn |
| 17 | { |
| 18 | using namespace armcomputetensorutils; |
| 19 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 20 | arm_compute::Status NeonFullyConnectedWorkloadValidate(const TensorInfo& input, |
| 21 | const TensorInfo& output, |
| 22 | const TensorInfo& weights, |
| 23 | const TensorInfo& biases, |
| 24 | const FullyConnectedDescriptor& descriptor) |
| 25 | { |
| 26 | const arm_compute::TensorInfo aclInput = BuildArmComputeTensorInfo(input); |
| 27 | const arm_compute::TensorInfo aclOutput = BuildArmComputeTensorInfo(output); |
| 28 | const arm_compute::TensorInfo aclWeights = BuildArmComputeTensorInfo(weights); |
| 29 | |
| 30 | arm_compute::TensorInfo aclBiases; |
| 31 | arm_compute::TensorInfo *optionalAclBiases = nullptr; |
| 32 | if (descriptor.m_BiasEnabled) |
| 33 | { |
| 34 | aclBiases = BuildArmComputeTensorInfo(biases); |
| 35 | optionalAclBiases = &aclBiases; |
| 36 | } |
| 37 | |
| 38 | const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo = |
| 39 | ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor); |
| 40 | |
| 41 | |
| 42 | return arm_compute::NEFullyConnectedLayer::validate(&aclInput, |
| 43 | &aclWeights, |
| 44 | optionalAclBiases, |
| 45 | &aclOutput, |
| 46 | fullyConnectedLayerInfo); |
| 47 | } |
| 48 | |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 49 | NeonFullyConnectedWorkload::NeonFullyConnectedWorkload(const FullyConnectedQueueDescriptor& descriptor, |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 50 | const WorkloadInfo& info, std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager) |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 51 | : BaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 52 | { |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 53 | m_Data.ValidateInputsOutputs("NeonFullyConnectedWorkload", 1, 1); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 54 | |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 55 | arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 56 | arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 57 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 58 | m_WeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 59 | BuildArmComputeTensor(*m_WeightsTensor, m_Data.m_Weight->GetTensorInfo()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 60 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 61 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 62 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 63 | m_BiasesTensor = std::make_unique<arm_compute::Tensor>(); |
| 64 | BuildArmComputeTensor(*m_BiasesTensor, m_Data.m_Bias->GetTensorInfo()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // Construct |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 68 | arm_compute::FullyConnectedLayerInfo fc_info; |
| 69 | fc_info.transpose_weights = m_Data.m_Parameters.m_TransposeWeightMatrix; |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 70 | |
| 71 | auto layer = std::make_unique<arm_compute::NEFullyConnectedLayer>(memoryManager); |
| 72 | layer->configure(&input, m_WeightsTensor.get(), m_BiasesTensor.get(), &output, fc_info); |
| 73 | m_FullyConnectedLayer.reset(layer.release()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 74 | |
| 75 | // Allocate |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 76 | if (m_Data.m_Weight->GetTensorInfo().GetDataType() == DataType::QAsymmU8) |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 77 | { |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 78 | InitializeArmComputeTensorData(*m_WeightsTensor, m_Data.m_Weight); |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 79 | } |
| 80 | else |
| 81 | { |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 82 | InitializeArmComputeTensorData(*m_WeightsTensor, m_Data.m_Weight); |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 83 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 84 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 85 | if (m_BiasesTensor) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 86 | { |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 87 | if (m_Data.m_Bias->GetTensorInfo().GetDataType() == DataType::Signed32) |
| 88 | { |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 89 | InitializeArmComputeTensorData(*m_BiasesTensor, m_Data.m_Bias); |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 90 | } |
| 91 | else |
| 92 | { |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 93 | InitializeArmComputeTensorData(*m_BiasesTensor, m_Data.m_Bias); |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 94 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 95 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 96 | |
| 97 | // Force Compute Library to perform the necessary copying and reshaping, after which |
| 98 | // delete all the input tensors that will no longer be needed |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 99 | m_FullyConnectedLayer->prepare(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 100 | FreeUnusedTensors(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 101 | } |
| 102 | |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 103 | void NeonFullyConnectedWorkload::Execute() const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 104 | { |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 105 | ARMNN_SCOPED_PROFILING_EVENT_NEON("NeonFullyConnectedWorkload_Execute"); |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 106 | m_FullyConnectedLayer->run(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 107 | } |
| 108 | |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 109 | void NeonFullyConnectedWorkload::FreeUnusedTensors() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 110 | { |
| 111 | FreeTensorIfUnused(m_WeightsTensor); |
| 112 | FreeTensorIfUnused(m_BiasesTensor); |
| 113 | } |
| 114 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 115 | } //namespace armnn |