1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include "FullyConnectedLayer.hpp"
6
7 #include "LayerCloneBase.hpp"
8
9 #include <armnn/TypesUtils.hpp>
10 #include <backendsCommon/CpuTensorHandle.hpp>
11 #include <backendsCommon/WorkloadData.hpp>
12 #include <backendsCommon/WorkloadFactory.hpp>
13
14 namespace armnn
15 {
16
FullyConnectedLayer(const FullyConnectedDescriptor & param,const char * name)17 FullyConnectedLayer::FullyConnectedLayer(const FullyConnectedDescriptor& param, const char* name)
18 : LayerWithParameters(1, 1, LayerType::FullyConnected, param, name)
19 {
20 }
21
CreateWorkload(const IWorkloadFactory & factory) const22 std::unique_ptr<IWorkload> FullyConnectedLayer::CreateWorkload(const IWorkloadFactory& factory) const
23 {
24 // on this level constant data should not be released..
25 ARMNN_ASSERT_MSG(m_Weight != nullptr, "FullyConnectedLayer: Weights data should not be null.");
26
27 FullyConnectedQueueDescriptor descriptor;
28
29 descriptor.m_Weight = m_Weight.get();
30 if (m_Param.m_BiasEnabled)
31 {
32 ARMNN_ASSERT_MSG(m_Bias != nullptr, "FullyConnectedLayer: Bias data should not be null.");
33 descriptor.m_Bias = m_Bias.get();
34 }
35
36 SetAdditionalInfo(descriptor);
37
38 return factory.CreateFullyConnected(descriptor, PrepInfoAndDesc(descriptor));
39 }
40
Clone(Graph & graph) const41 FullyConnectedLayer* FullyConnectedLayer::Clone(Graph& graph) const
42 {
43 auto layer = CloneBase<FullyConnectedLayer>(graph, m_Param, GetName());
44
45 layer->m_Weight = m_Weight ? std::make_unique<ScopedCpuTensorHandle>(*m_Weight) : nullptr;
46 if (layer->m_Param.m_BiasEnabled)
47 {
48 layer->m_Bias = m_Bias ? std::make_unique<ScopedCpuTensorHandle>(*m_Bias) : nullptr;
49 }
50
51 return std::move(layer);
52 }
53
InferOutputShapes(const std::vector<TensorShape> & inputShapes) const54 std::vector<TensorShape> FullyConnectedLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
55 {
56 ARMNN_ASSERT(inputShapes.size() == 2);
57 const TensorShape& inputShape = inputShapes[0];
58 const TensorShape weightShape = inputShapes[1];
59
60 // Output for FC is [1, w[1]].
61 unsigned int batches = inputShape[0];
62 unsigned int dimIdx = m_Param.m_TransposeWeightMatrix ? 0 : 1;
63
64 return std::vector<TensorShape>({ TensorShape({batches, weightShape[dimIdx]})});
65 }
66
ValidateTensorShapesFromInputs()67 void FullyConnectedLayer::ValidateTensorShapesFromInputs()
68 {
69 const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
70
71 VerifyShapeInferenceType(outputShape, m_ShapeInferenceMethod);
72
73 // check if we m_Weight data is not nullptr
74 ARMNN_ASSERT_MSG(m_Weight != nullptr, "FullyConnectedLayer: Weights data should not be null.");
75
76 auto inferredShapes = InferOutputShapes({GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(),
77 m_Weight->GetTensorInfo().GetShape() });
78
79 ARMNN_ASSERT(inferredShapes.size() == 1);
80 ARMNN_ASSERT(inferredShapes[0].GetDimensionality() == Dimensionality::Specified);
81
82 ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "FullyConnectedLayer");
83 }
84
GetConstantTensorsByRef()85 Layer::ConstantTensors FullyConnectedLayer::GetConstantTensorsByRef()
86 {
87 return {m_Weight, m_Bias};
88 }
89
Accept(ILayerVisitor & visitor) const90 void FullyConnectedLayer::Accept(ILayerVisitor& visitor) const
91 {
92 ConstTensor weightsTensor(m_Weight->GetTensorInfo(), m_Weight->Map(true));
93 Optional<ConstTensor> optionalBiasTensor = EmptyOptional();
94
95 if (GetParameters().m_BiasEnabled)
96 {
97 ConstTensor biasTensor(m_Bias->GetTensorInfo(), m_Bias->GetConstTensor<void>());
98 optionalBiasTensor = Optional<ConstTensor>(biasTensor);
99 }
100
101 visitor.VisitFullyConnectedLayer(this, GetParameters(), weightsTensor, optionalBiasTensor, GetName());
102 }
103
104 } // namespace armnn
105