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