1 //
2 // Copyright © 2017,2022 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 <armnn/backends/TensorHandle.hpp>
11 #include <armnn/backends/WorkloadData.hpp>
12 #include <armnn/backends/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(param.GetNumInputs(), 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 FullyConnectedQueueDescriptor descriptor;
25 SetAdditionalInfo(descriptor);
26 return factory.CreateWorkload(LayerType::FullyConnected, descriptor, PrepInfoAndDesc(descriptor));
27 }
28
Clone(Graph & graph) const29 FullyConnectedLayer* FullyConnectedLayer::Clone(Graph& graph) const
30 {
31 auto layer = CloneBase<FullyConnectedLayer>(graph, m_Param, GetName());
32 return std::move(layer);
33 }
34
InferOutputShapes(const std::vector<TensorShape> & inputShapes) const35 std::vector<TensorShape> FullyConnectedLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
36 {
37 ARMNN_ASSERT(inputShapes.size() == 2);
38 const TensorShape& inputShape = inputShapes[0];
39 const TensorShape weightShape = inputShapes[1];
40
41 // Output for FC is [1, w[1]].
42 unsigned int batches = inputShape[0];
43 unsigned int dimIdx = m_Param.m_TransposeWeightMatrix ? 0 : 1;
44
45 return std::vector<TensorShape>({ TensorShape({batches, weightShape[dimIdx]})});
46 }
47
ValidateTensorShapesFromInputs()48 void FullyConnectedLayer::ValidateTensorShapesFromInputs()
49 {
50 const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
51
52 VerifyShapeInferenceType(outputShape, m_ShapeInferenceMethod);
53
54 std::vector<TensorShape> inferredShapes = InferOutputShapes(
55 {GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(),
56 GetInputSlot(1).GetConnection()->GetTensorInfo().GetShape()});
57
58 ARMNN_ASSERT(inferredShapes.size() == 1);
59 ARMNN_ASSERT(inferredShapes[0].GetDimensionality() == Dimensionality::Specified);
60
61 ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "FullyConnectedLayer");
62 }
63
GetConstantTensorsByRef() const64 Layer::ImmutableConstantTensors FullyConnectedLayer::GetConstantTensorsByRef() const
65 {
66 Layer::ImmutableConstantTensors tensors = GetConnectedConstantAsInputTensors();
67 return tensors;
68 }
69
ExecuteStrategy(IStrategy & strategy) const70 void FullyConnectedLayer::ExecuteStrategy(IStrategy& strategy) const
71 {
72 strategy.ExecuteStrategy(this, GetParameters(), {}, GetName());
73 }
74
75 } // namespace armnn
76