IVGCVSW-1946: Remove armnn/src from the include paths
[platform/upstream/armnn.git] / src / backends / cl / workloads / ClFullyConnectedWorkload.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "ClFullyConnectedWorkload.hpp"
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>
12
13 #include "ClWorkloadUtils.hpp"
14
15 namespace armnn
16 {
17 using namespace armcomputetensorutils;
18
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
47 ClFullyConnectedWorkload::ClFullyConnectedWorkload(const FullyConnectedQueueDescriptor& descriptor,
48     const WorkloadInfo& info, std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
49     : BaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info)
50     , m_FullyConnectedLayer(memoryManager)
51 {
52     m_WeightsTensor = std::make_unique<arm_compute::CLTensor>();
53     BuildArmComputeTensor(*m_WeightsTensor, m_Data.m_Weight->GetTensorInfo());
54
55     if (m_Data.m_Parameters.m_BiasEnabled)
56     {
57         m_BiasesTensor = std::make_unique<arm_compute::CLTensor>();
58         BuildArmComputeTensor(*m_BiasesTensor, m_Data.m_Bias->GetTensorInfo());
59     }
60
61     m_Data.ValidateInputsOutputs("ClFullyConnectedWorkload", 1, 1);
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();
65
66     // Construct
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);
70
71     InitializeArmComputeClTensorData(*m_WeightsTensor, m_Data.m_Weight);
72
73     if (m_BiasesTensor)
74     {
75         InitializeArmComputeClTensorData(*m_BiasesTensor, m_Data.m_Bias);
76     }
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();
82 }
83
84 void ClFullyConnectedWorkload::Execute() const
85 {
86     ARMNN_SCOPED_PROFILING_EVENT_CL("ClFullyConnectedWorkload_Execute");
87     RunClFunction(m_FullyConnectedLayer, CHECK_LOCATION());
88 }
89
90 void ClFullyConnectedWorkload::FreeUnusedTensors()
91 {
92     FreeTensorIfUnused(m_WeightsTensor);
93     FreeTensorIfUnused(m_BiasesTensor);
94 }
95
96 } //namespace armnn