Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "RefFullyConnectedWorkload.hpp" |
| 7 | |
| 8 | #include "FullyConnected.hpp" |
| 9 | #include "RefWorkloadUtils.hpp" |
| 10 | |
| 11 | #include "Profiling.hpp" |
| 12 | |
| 13 | namespace armnn |
| 14 | { |
| 15 | RefFullyConnectedWorkload::RefFullyConnectedWorkload( |
| 16 | const FullyConnectedQueueDescriptor& descriptor, const WorkloadInfo& info) |
Sadik Armagan | f0a6dec | 2021-03-25 07:46:55 +0000 | [diff] [blame] | 17 | : BaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info) |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 18 | { |
Sadik Armagan | f0a6dec | 2021-03-25 07:46:55 +0000 | [diff] [blame] | 19 | if (descriptor.m_Parameters.m_ConstantWeights) |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 20 | { |
Sadik Armagan | f0a6dec | 2021-03-25 07:46:55 +0000 | [diff] [blame] | 21 | m_Weight = std::make_unique<ScopedCpuTensorHandle>(*(descriptor.m_Weight)); |
| 22 | const TensorInfo& rWeightInfo = m_Weight->GetTensorInfo(); |
| 23 | m_WeightShape = rWeightInfo.GetShape(); |
| 24 | m_WeightDecoder = MakeDecoder<float>(rWeightInfo, m_Weight->Map(true)); |
| 25 | |
| 26 | if (descriptor.m_Parameters.m_BiasEnabled) |
| 27 | { |
| 28 | m_Bias = std::make_unique<ScopedCpuTensorHandle>(*(descriptor.m_Bias)); |
| 29 | const TensorInfo& biasInfo = m_Bias->GetTensorInfo(); |
| 30 | m_BiasDecoder = MakeDecoder<float>(biasInfo, m_Bias->Map(true)); |
| 31 | } |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
| 35 | void RefFullyConnectedWorkload::PostAllocationConfigure() |
| 36 | { |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 37 | PostAllocationConfigure(m_Data.m_Inputs, m_Data.m_Outputs); |
| 38 | } |
| 39 | |
| 40 | void RefFullyConnectedWorkload::PostAllocationConfigure(std::vector<ITensorHandle*> inputs, |
| 41 | std::vector<ITensorHandle*> outputs) |
| 42 | { |
| 43 | const TensorInfo& inputInfo = GetTensorInfo(inputs[0]); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 44 | ARMNN_ASSERT(inputInfo.GetNumDimensions() > 1); |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 45 | m_InputShape = inputInfo.GetShape(); |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 46 | |
Sadik Armagan | f0a6dec | 2021-03-25 07:46:55 +0000 | [diff] [blame] | 47 | if (!m_Data.m_Parameters.m_ConstantWeights) |
| 48 | { |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 49 | const TensorInfo& rWeightInfo = GetTensorInfo(inputs[1]); |
Sadik Armagan | f0a6dec | 2021-03-25 07:46:55 +0000 | [diff] [blame] | 50 | ARMNN_ASSERT(inputInfo.GetNumDimensions() > 1); |
| 51 | m_WeightShape = rWeightInfo.GetShape(); |
| 52 | m_WeightDecoder = MakeDecoder<float>(rWeightInfo); |
| 53 | |
| 54 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 55 | { |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 56 | const TensorInfo& biasInfo = GetTensorInfo(inputs[2]); |
Sadik Armagan | f0a6dec | 2021-03-25 07:46:55 +0000 | [diff] [blame] | 57 | m_BiasDecoder = MakeDecoder<float>(biasInfo); |
| 58 | } |
| 59 | } |
| 60 | |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 61 | const TensorInfo& outputInfo = GetTensorInfo(outputs[0]); |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 62 | m_OutputShape = outputInfo.GetShape(); |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 63 | |
| 64 | m_NumActivations = 1; // Total number of activations in the input. |
| 65 | for (unsigned int i = 1; i < inputInfo.GetNumDimensions(); i++) |
| 66 | { |
| 67 | m_NumActivations *= inputInfo.GetShape()[i]; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void RefFullyConnectedWorkload::Execute() const |
| 72 | { |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 73 | Execute(m_Data.m_Inputs, m_Data.m_Outputs); |
| 74 | } |
| 75 | |
| 76 | void RefFullyConnectedWorkload::ExecuteAsync(WorkingMemDescriptor &workingMemDescriptor) |
| 77 | { |
| 78 | PostAllocationConfigure(workingMemDescriptor.m_Inputs, workingMemDescriptor.m_Outputs); |
| 79 | |
| 80 | Execute(workingMemDescriptor.m_Inputs, workingMemDescriptor.m_Outputs); |
| 81 | } |
| 82 | |
| 83 | void RefFullyConnectedWorkload::Execute(std::vector<ITensorHandle*> inputs, std::vector<ITensorHandle*> outputs) const |
| 84 | { |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 85 | ARMNN_SCOPED_PROFILING_EVENT(Compute::CpuRef, "RefFullyConnectedWorkload_Execute"); |
| 86 | |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 87 | std::unique_ptr<Decoder<float>> inputDecoder = MakeDecoder<float>(GetTensorInfo(inputs[0]), inputs[0]->Map()); |
| 88 | std::unique_ptr<Encoder<float>> OutputEncoder = MakeEncoder<float>(GetTensorInfo(outputs[0]), outputs[0]->Map()); |
| 89 | |
Sadik Armagan | f0a6dec | 2021-03-25 07:46:55 +0000 | [diff] [blame] | 90 | if (!m_Data.m_Parameters.m_ConstantWeights) |
| 91 | { |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 92 | m_WeightDecoder->Reset(inputs[1]->Map()); |
Sadik Armagan | f0a6dec | 2021-03-25 07:46:55 +0000 | [diff] [blame] | 93 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 94 | { |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 95 | m_BiasDecoder->Reset(inputs[2]->Map()); |
Sadik Armagan | f0a6dec | 2021-03-25 07:46:55 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
Matthew Bentham | c394a6d | 2019-06-24 12:51:25 +0100 | [diff] [blame] | 98 | |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 99 | FullyConnected(m_InputShape, |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 100 | *inputDecoder, |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 101 | m_OutputShape, |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 102 | *OutputEncoder, |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 103 | m_WeightShape, |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 104 | *m_WeightDecoder, |
| 105 | *m_BiasDecoder, |
| 106 | m_Data.m_Parameters.m_BiasEnabled, |
| 107 | m_NumActivations, |
| 108 | m_Data.m_Parameters.m_TransposeWeightMatrix); |
| 109 | } |
| 110 | |
| 111 | } //namespace armnn |