telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
Teresa Charlin | 6fba294 | 2023-04-18 12:48:46 +0100 | [diff] [blame] | 2 | // Copyright © 2017,2022-2023 Arm Ltd and Contributors. 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 | |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 15 | #include <armnn/backends/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, |
Matthew Bentham | 67d6390 | 2022-02-08 15:03:07 +0000 | [diff] [blame] | 27 | const Optional<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); |
Cathal Corbett | 4452baf | 2022-05-13 09:55:59 +0100 | [diff] [blame] | 33 | arm_compute::TensorInfo aclWeights = BuildArmComputeTensorInfo(weights); |
| 34 | aclWeights.set_are_values_constant(weights.IsConstant()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 35 | |
| 36 | arm_compute::TensorInfo aclBiases; |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 37 | arm_compute::TensorInfo* optionalAclBiases = nullptr; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 38 | if (descriptor.m_BiasEnabled) |
| 39 | { |
Matthew Bentham | 67d6390 | 2022-02-08 15:03:07 +0000 | [diff] [blame] | 40 | ARMNN_ASSERT(biases.has_value()); |
TeresaARM | 6d63e22 | 2023-05-11 15:16:39 +0000 | [diff] [blame^] | 41 | // Same for bias as weights. We don't currently support non const. |
| 42 | if (!biases.value().IsConstant()) |
| 43 | { |
| 44 | return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR, |
| 45 | "Arm NN NeonFullyConnectedWorkload does not support non constant bias."}; |
| 46 | } |
Matthew Bentham | 67d6390 | 2022-02-08 15:03:07 +0000 | [diff] [blame] | 47 | aclBiases = BuildArmComputeTensorInfo(biases.value()); |
Cathal Corbett | 4452baf | 2022-05-13 09:55:59 +0100 | [diff] [blame] | 48 | aclBiases.set_are_values_constant(biases.value().IsConstant()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 49 | optionalAclBiases = &aclBiases; |
| 50 | } |
| 51 | |
| 52 | const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo = |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 53 | ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor, activationDescriptor); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 54 | return arm_compute::NEFullyConnectedLayer::validate(&aclInput, |
| 55 | &aclWeights, |
| 56 | optionalAclBiases, |
| 57 | &aclOutput, |
| 58 | fullyConnectedLayerInfo); |
| 59 | } |
| 60 | |
kevmay01 | e448be3 | 2018-09-26 10:21:55 +0100 | [diff] [blame] | 61 | NeonFullyConnectedWorkload::NeonFullyConnectedWorkload(const FullyConnectedQueueDescriptor& descriptor, |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 62 | const WorkloadInfo& info, |
| 63 | ACLMemManagerOnDemand& memoryManager) |
Teresa Charlin | 588cbdf | 2022-01-19 15:55:37 +0000 | [diff] [blame] | 64 | : NeonBaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 65 | { |
Teresa Charlin | 6fba294 | 2023-04-18 12:48:46 +0100 | [diff] [blame] | 66 | m_Data.ValidateInputsOutputs("NeonFullyConnectedWorkload", descriptor.m_Parameters.GetNumInputs(), 1); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 67 | |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 68 | arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
Teresa Charlin | 6fba294 | 2023-04-18 12:48:46 +0100 | [diff] [blame] | 69 | arm_compute::ITensor& weights = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[1])->GetTensor(); |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 70 | m_WeightsTensorInfo = info.m_InputTensorInfos[1]; |
Teresa Charlin | 6fba294 | 2023-04-18 12:48:46 +0100 | [diff] [blame] | 71 | weights.info()->set_are_values_constant(m_WeightsTensorInfo.IsConstant()); |
| 72 | arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
| 73 | if (m_WeightsTensorInfo.IsConstant()) |
| 74 | { |
| 75 | // Copy the weights' tensor into arm_compute tensor. |
| 76 | m_WeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 77 | BuildArmComputeTensor(*m_WeightsTensor, m_WeightsTensorInfo); |
| 78 | m_WeightsTensor->info()->set_are_values_constant(m_WeightsTensorInfo.IsConstant()); |
| 79 | } |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 80 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 81 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 82 | { |
Cathal Corbett | 4452baf | 2022-05-13 09:55:59 +0100 | [diff] [blame] | 83 | // Copy the biases tensor into arm_compute tensor. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 84 | m_BiasesTensor = std::make_unique<arm_compute::Tensor>(); |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 85 | m_BiasesTensorInfo = info.m_InputTensorInfos[2]; |
| 86 | BuildArmComputeTensor(*m_BiasesTensor, m_BiasesTensorInfo); |
Teresa Charlin | 6fba294 | 2023-04-18 12:48:46 +0100 | [diff] [blame] | 87 | m_BiasesTensor->info()->set_are_values_constant(m_BiasesTensorInfo.IsConstant()); |
TeresaARM | 6d63e22 | 2023-05-11 15:16:39 +0000 | [diff] [blame^] | 88 | |
| 89 | // We do not support dynamic bias |
| 90 | ARMNN_ASSERT(m_BiasesTensorInfo.IsConstant() == true); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 93 | const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor); |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 94 | arm_compute::FullyConnectedLayerInfo fc_info = |
| 95 | ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor.m_Parameters, activationInfo); |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 96 | |
| 97 | auto layer = std::make_unique<arm_compute::NEFullyConnectedLayer>(memoryManager); |
Teresa Charlin | 6fba294 | 2023-04-18 12:48:46 +0100 | [diff] [blame] | 98 | if (m_WeightsTensorInfo.IsConstant()) |
| 99 | { |
| 100 | layer->configure(&input, m_WeightsTensor.get(), m_BiasesTensor.get(), &output, fc_info); |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | layer->configure(&input, &weights, m_BiasesTensor.get(), &output, fc_info); |
| 105 | } |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 106 | m_FullyConnectedLayer.reset(layer.release()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 107 | |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 108 | // Add details for profiling output |
| 109 | WorkloadInfo detailsInfo; |
| 110 | |
| 111 | detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos; |
| 112 | detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos; |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 113 | detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[1]); |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 114 | if (descriptor.m_Parameters.m_BiasEnabled) |
| 115 | { |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 116 | detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[2]); |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Report Profiling Details |
| 120 | ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonFullyConnectedWorkload_Construct", |
| 121 | descriptor.m_Parameters, |
| 122 | detailsInfo, |
| 123 | this->GetGuid()); |
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()); |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 129 | // The constant tensors may not be fully in place until the workload is Executed |
| 130 | if (!prepared) |
| 131 | { |
Teresa Charlin | 6fba294 | 2023-04-18 12:48:46 +0100 | [diff] [blame] | 132 | if (m_WeightsTensorInfo.IsConstant()) |
| 133 | { |
| 134 | InitializeArmComputeTensorData(*m_WeightsTensor, m_WeightsTensorInfo, m_Data.m_Inputs[1]); |
| 135 | m_WeightsTensor->info()->set_are_values_constant(m_WeightsTensorInfo.IsConstant()); |
| 136 | } |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 137 | |
| 138 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 139 | { |
| 140 | InitializeArmComputeTensorData(*m_BiasesTensor, m_BiasesTensorInfo, m_Data.m_Inputs[2]); |
Teresa Charlin | 6fba294 | 2023-04-18 12:48:46 +0100 | [diff] [blame] | 141 | m_BiasesTensor->info()->set_are_values_constant(m_BiasesTensorInfo.IsConstant()); |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 142 | } |
Teresa Charlin | 6fba294 | 2023-04-18 12:48:46 +0100 | [diff] [blame] | 143 | if (m_WeightsTensorInfo.IsConstant()) |
| 144 | { |
| 145 | FreeTensorIfUnused(m_WeightsTensor); |
| 146 | } |
| 147 | if (m_BiasesTensorInfo.IsConstant()) |
| 148 | { |
| 149 | FreeTensorIfUnused(m_BiasesTensor); |
| 150 | } |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 151 | prepared = true; |
| 152 | } |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 153 | m_FullyConnectedLayer->run(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | } //namespace armnn |