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; |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 22 | using ACLMemManagerOnDemand = std::shared_ptr<arm_compute::MemoryManagerOnDemand>; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 23 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 24 | arm_compute::Status NeonFullyConnectedWorkloadValidate(const TensorInfo& input, |
| 25 | const TensorInfo& output, |
| 26 | const TensorInfo& weights, |
| 27 | const TensorInfo& biases, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 28 | const FullyConnectedDescriptor& descriptor, |
| 29 | const ActivationDescriptor* activationDescriptor) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 30 | { |
| 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 Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 36 | arm_compute::TensorInfo* optionalAclBiases = nullptr; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 37 | if (descriptor.m_BiasEnabled) |
| 38 | { |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 39 | aclBiases = BuildArmComputeTensorInfo(biases); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 40 | optionalAclBiases = &aclBiases; |
| 41 | } |
| 42 | |
| 43 | const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo = |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 44 | ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor, activationDescriptor); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 45 | |
| 46 | return arm_compute::NEFullyConnectedLayer::validate(&aclInput, |
| 47 | &aclWeights, |
| 48 | optionalAclBiases, |
| 49 | &aclOutput, |
| 50 | fullyConnectedLayerInfo); |
| 51 | } |
| 52 | |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 53 | NeonFullyConnectedWorkload::NeonFullyConnectedWorkload(const FullyConnectedQueueDescriptor& descriptor, |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 54 | const WorkloadInfo& info, |
| 55 | ACLMemManagerOnDemand& memoryManager) |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 56 | : BaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 57 | { |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 58 | m_Data.ValidateInputsOutputs("NeonFullyConnectedWorkload", 1, 1); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 59 | |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 60 | 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(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 62 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 63 | m_WeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 64 | BuildArmComputeTensor(*m_WeightsTensor, m_Data.m_Weight->GetTensorInfo()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 65 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 66 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 67 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 68 | m_BiasesTensor = std::make_unique<arm_compute::Tensor>(); |
| 69 | BuildArmComputeTensor(*m_BiasesTensor, m_Data.m_Bias->GetTensorInfo()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 72 | const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor); |
| 73 | |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 74 | arm_compute::FullyConnectedLayerInfo fc_info = |
| 75 | ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor.m_Parameters, activationInfo); |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 76 | |
| 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()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 80 | |
| 81 | // Allocate |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 82 | if (m_Data.m_Weight->GetTensorInfo().GetDataType() == DataType::QAsymmU8) |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 83 | { |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 84 | InitializeArmComputeTensorData(*m_WeightsTensor, m_Data.m_Weight); |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 85 | } |
| 86 | else |
| 87 | { |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 88 | InitializeArmComputeTensorData(*m_WeightsTensor, m_Data.m_Weight); |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 89 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 90 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 91 | if (m_BiasesTensor) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 92 | { |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 93 | if (m_Data.m_Bias->GetTensorInfo().GetDataType() == DataType::Signed32) |
| 94 | { |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 95 | InitializeArmComputeTensorData(*m_BiasesTensor, m_Data.m_Bias); |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 96 | } |
| 97 | else |
| 98 | { |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 99 | InitializeArmComputeTensorData(*m_BiasesTensor, m_Data.m_Bias); |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 100 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 101 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 102 | |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 103 | // 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 120 | // 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 Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 122 | m_FullyConnectedLayer->prepare(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 123 | FreeUnusedTensors(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 124 | } |
| 125 | |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 126 | void NeonFullyConnectedWorkload::Execute() const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 127 | { |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 128 | ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID("NeonFullyConnectedWorkload_Execute", this->GetGuid()); |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 129 | m_FullyConnectedLayer->run(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 130 | } |
| 131 | |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 132 | void NeonFullyConnectedWorkload::FreeUnusedTensors() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 133 | { |
| 134 | FreeTensorIfUnused(m_WeightsTensor); |
| 135 | FreeTensorIfUnused(m_BiasesTensor); |
| 136 | } |
| 137 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 138 | } //namespace armnn |