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