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" |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 9 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 10 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
| 11 | #include <aclCommon/ArmComputeUtils.hpp> |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 12 | |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 13 | #include <armnn/utility/PolymorphicDowncast.hpp> |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 14 | |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 15 | #include <backendsCommon/TensorHandle.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 16 | |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 17 | #include <arm_compute/runtime/NEON/functions/NEFullyConnectedLayer.h> |
| 18 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 19 | namespace armnn |
| 20 | { |
| 21 | using namespace armcomputetensorutils; |
| 22 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 23 | arm_compute::Status NeonFullyConnectedWorkloadValidate(const TensorInfo& input, |
| 24 | const TensorInfo& output, |
| 25 | const TensorInfo& weights, |
| 26 | const TensorInfo& biases, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 27 | const FullyConnectedDescriptor& descriptor, |
| 28 | const ActivationDescriptor* activationDescriptor) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 29 | { |
| 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 Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 43 | ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor, activationDescriptor); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 44 | |
| 45 | return arm_compute::NEFullyConnectedLayer::validate(&aclInput, |
| 46 | &aclWeights, |
| 47 | optionalAclBiases, |
| 48 | &aclOutput, |
| 49 | fullyConnectedLayerInfo); |
| 50 | } |
| 51 | |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 52 | NeonFullyConnectedWorkload::NeonFullyConnectedWorkload(const FullyConnectedQueueDescriptor& descriptor, |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 53 | const WorkloadInfo& info, std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager) |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 54 | : BaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 55 | { |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 56 | m_Data.ValidateInputsOutputs("NeonFullyConnectedWorkload", 1, 1); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 57 | |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 58 | 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(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 60 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 61 | m_WeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 62 | BuildArmComputeTensor(*m_WeightsTensor, m_Data.m_Weight->GetTensorInfo()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 63 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 64 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 65 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 66 | m_BiasesTensor = std::make_unique<arm_compute::Tensor>(); |
| 67 | BuildArmComputeTensor(*m_BiasesTensor, m_Data.m_Bias->GetTensorInfo()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 70 | const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor); |
| 71 | |
| 72 | arm_compute::FullyConnectedLayerInfo fc_info = |
| 73 | ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor.m_Parameters, activationInfo); |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 74 | |
| 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()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 78 | |
| 79 | // Allocate |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 80 | if (m_Data.m_Weight->GetTensorInfo().GetDataType() == DataType::QAsymmU8) |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 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 | } |
| 84 | else |
| 85 | { |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 86 | InitializeArmComputeTensorData(*m_WeightsTensor, m_Data.m_Weight); |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 87 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 88 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 89 | if (m_BiasesTensor) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 90 | { |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 91 | if (m_Data.m_Bias->GetTensorInfo().GetDataType() == DataType::Signed32) |
| 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 | } |
| 95 | else |
| 96 | { |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 97 | InitializeArmComputeTensorData(*m_BiasesTensor, m_Data.m_Bias); |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 98 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 99 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 100 | |
| 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 Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 103 | m_FullyConnectedLayer->prepare(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 104 | FreeUnusedTensors(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 105 | } |
| 106 | |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 107 | void NeonFullyConnectedWorkload::Execute() const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 108 | { |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 109 | ARMNN_SCOPED_PROFILING_EVENT_NEON("NeonFullyConnectedWorkload_Execute"); |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 110 | m_FullyConnectedLayer->run(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 111 | } |
| 112 | |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 113 | void NeonFullyConnectedWorkload::FreeUnusedTensors() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 114 | { |
| 115 | FreeTensorIfUnused(m_WeightsTensor); |
| 116 | FreeTensorIfUnused(m_BiasesTensor); |
| 117 | } |
| 118 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 119 | } //namespace armnn |