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 | |
Matthew Bentham | ab8cdc1 | 2018-09-17 11:17:41 +0100 | [diff] [blame] | 6 | #include "ClFullyConnectedWorkload.hpp" |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 7 | #include <cl/ClTensorHandle.hpp> |
| 8 | #include <backendsCommon/CpuTensorHandle.hpp> |
| 9 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
| 10 | #include <aclCommon/ArmComputeUtils.hpp> |
| 11 | #include <cl/ClLayerSupport.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 12 | |
Matthew Bentham | 14e4669 | 2018-09-20 15:35:30 +0100 | [diff] [blame] | 13 | #include "ClWorkloadUtils.hpp" |
| 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 ClFullyConnectedWorkloadValidate(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 | return arm_compute::CLFullyConnectedLayer::validate(&aclInput, |
| 41 | &aclWeights, |
| 42 | optionalAclBiases, |
| 43 | &aclOutput, |
| 44 | fullyConnectedLayerInfo); |
| 45 | } |
| 46 | |
Matthew Bentham | ab8cdc1 | 2018-09-17 11:17:41 +0100 | [diff] [blame] | 47 | ClFullyConnectedWorkload::ClFullyConnectedWorkload(const FullyConnectedQueueDescriptor& descriptor, |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 48 | const WorkloadInfo& info, std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager) |
Matthew Bentham | ab8cdc1 | 2018-09-17 11:17:41 +0100 | [diff] [blame] | 49 | : BaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 50 | , m_FullyConnectedLayer(memoryManager) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 51 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 52 | m_WeightsTensor = std::make_unique<arm_compute::CLTensor>(); |
| 53 | BuildArmComputeTensor(*m_WeightsTensor, m_Data.m_Weight->GetTensorInfo()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 54 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 55 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 56 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 57 | m_BiasesTensor = std::make_unique<arm_compute::CLTensor>(); |
| 58 | BuildArmComputeTensor(*m_BiasesTensor, m_Data.m_Bias->GetTensorInfo()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Matthew Bentham | ab8cdc1 | 2018-09-17 11:17:41 +0100 | [diff] [blame] | 61 | m_Data.ValidateInputsOutputs("ClFullyConnectedWorkload", 1, 1); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 62 | |
| 63 | arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 64 | arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 65 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 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; |
| 69 | m_FullyConnectedLayer.configure(&input, m_WeightsTensor.get(), m_BiasesTensor.get(), &output, fc_info); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 70 | |
Matthew Bentham | 785df50 | 2018-09-21 10:29:58 +0100 | [diff] [blame] | 71 | InitializeArmComputeClTensorData(*m_WeightsTensor, m_Data.m_Weight); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 72 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 73 | if (m_BiasesTensor) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 74 | { |
Matthew Bentham | 785df50 | 2018-09-21 10:29:58 +0100 | [diff] [blame] | 75 | InitializeArmComputeClTensorData(*m_BiasesTensor, m_Data.m_Bias); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 76 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 77 | |
| 78 | // Force Compute Library to perform the necessary copying and reshaping, after which |
| 79 | // delete all the input tensors that will no longer be needed |
| 80 | m_FullyConnectedLayer.prepare(); |
| 81 | FreeUnusedTensors(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Matthew Bentham | ab8cdc1 | 2018-09-17 11:17:41 +0100 | [diff] [blame] | 84 | void ClFullyConnectedWorkload::Execute() const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 85 | { |
Matthew Bentham | ab8cdc1 | 2018-09-17 11:17:41 +0100 | [diff] [blame] | 86 | ARMNN_SCOPED_PROFILING_EVENT_CL("ClFullyConnectedWorkload_Execute"); |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 87 | RunClFunction(m_FullyConnectedLayer, CHECK_LOCATION()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Matthew Bentham | ab8cdc1 | 2018-09-17 11:17:41 +0100 | [diff] [blame] | 90 | void ClFullyConnectedWorkload::FreeUnusedTensors() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 91 | { |
| 92 | FreeTensorIfUnused(m_WeightsTensor); |
| 93 | FreeTensorIfUnused(m_BiasesTensor); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 94 | } |
| 95 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 96 | } //namespace armnn |