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 | |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 6 | #include "NeonFullyConnectedFloatWorkload.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 7 | |
David Beck | 711fa31 | 2018-09-24 10:46:38 +0100 | [diff] [blame] | 8 | #include <backends/aclCommon/ArmComputeTensorUtils.hpp> |
| 9 | #include <backends/aclCommon/ArmComputeUtils.hpp> |
| 10 | #include <backends/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 | |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 45 | NeonFullyConnectedFloatWorkload::NeonFullyConnectedFloatWorkload(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) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 47 | : FloatWorkload<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 | { |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 50 | m_Data.ValidateInputsOutputs("NeonFullyConnectedFloatWorkload", 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 |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 70 | InitializeArmComputeTensorDataForFloatTypes(*m_WeightsTensor, m_Data.m_Weight); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 71 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 72 | if (m_BiasesTensor) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 73 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 74 | InitializeArmComputeTensorDataForFloatTypes(*m_BiasesTensor, m_Data.m_Bias); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 75 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 76 | |
| 77 | // Force Compute Library to perform the necessary copying and reshaping, after which |
| 78 | // delete all the input tensors that will no longer be needed |
| 79 | m_FullyConnectedLayer.prepare(); |
| 80 | FreeUnusedTensors(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 81 | } |
| 82 | |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 83 | void NeonFullyConnectedFloatWorkload::Execute() const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 84 | { |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 85 | ARMNN_SCOPED_PROFILING_EVENT_NEON("NeonFullyConnectedFloatWorkload_Execute"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 86 | m_FullyConnectedLayer.run(); |
| 87 | } |
| 88 | |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 89 | void NeonFullyConnectedFloatWorkload::FreeUnusedTensors() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 90 | { |
| 91 | FreeTensorIfUnused(m_WeightsTensor); |
| 92 | FreeTensorIfUnused(m_BiasesTensor); |
| 93 | } |
| 94 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 95 | } //namespace armnn |
| 96 | |