ArmNN
 21.05
FullyConnectedLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
6 
7 #include "LayerCloneBase.hpp"
8 
9 #include <armnn/TypesUtils.hpp>
13 
14 namespace armnn
15 {
16 
18  : LayerWithParameters(param.GetNumViews(), 1, LayerType::FullyConnected, param, name)
19 {
20 }
21 
22 std::unique_ptr<IWorkload> FullyConnectedLayer::CreateWorkload(const IWorkloadFactory& factory) const
23 {
24  // on this level constant data should not be released..
27  {
28  ARMNN_ASSERT_MSG(m_Weight != nullptr, "FullyConnectedLayer: Weights data should not be null.");
29  descriptor.m_Weight = m_Weight.get();
30 
32  {
33  ARMNN_ASSERT_MSG(m_Bias != nullptr, "FullyConnectedLayer: Bias data should not be null.");
34  descriptor.m_Bias = m_Bias.get();
35  }
36  }
37  SetAdditionalInfo(descriptor);
38 
39  return factory.CreateFullyConnected(descriptor, PrepInfoAndDesc(descriptor));
40 }
41 
43 {
44  auto layer = CloneBase<FullyConnectedLayer>(graph, m_Param, GetName());
45  if (m_Param.m_ConstantWeights)
46  {
47  layer->m_Weight = m_Weight ? m_Weight : nullptr;
48 
49  if (layer->m_Param.m_BiasEnabled)
50  {
51  layer->m_Bias = m_Bias ? m_Bias : nullptr;
52  }
53  }
54  return std::move(layer);
55 }
56 
57 std::vector<TensorShape> FullyConnectedLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
58 {
59  ARMNN_ASSERT(inputShapes.size() == 2);
60  const TensorShape& inputShape = inputShapes[0];
61  const TensorShape weightShape = inputShapes[1];
62 
63  // Output for FC is [1, w[1]].
64  unsigned int batches = inputShape[0];
65  unsigned int dimIdx = m_Param.m_TransposeWeightMatrix ? 0 : 1;
66 
67  return std::vector<TensorShape>({ TensorShape({batches, weightShape[dimIdx]})});
68 }
69 
71 {
72  const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
73 
75 
76  std::vector<TensorShape> inferredShapes;
78  {
79  // check if m_Weight data is not nullptr
80  ARMNN_ASSERT_MSG(m_Weight != nullptr, "FullyConnectedLayer: Weights data should not be null.");
81 
83  m_Weight->GetTensorInfo().GetShape()});
84  }
85  else
86  {
89  }
90 
91  ARMNN_ASSERT(inferredShapes.size() == 1);
92  ARMNN_ASSERT(inferredShapes[0].GetDimensionality() == Dimensionality::Specified);
93 
94  ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "FullyConnectedLayer");
95 }
96 
98 {
99  return {m_Weight, m_Bias};
100 }
101 
103 {
104  Optional<ConstTensor> optionalWeightsTensor = EmptyOptional();
105  Optional<ConstTensor> optionalBiasTensor = EmptyOptional();
106 
107  ManagedConstTensorHandle managedWeight(m_Weight);
108  ManagedConstTensorHandle managedBias(m_Bias);
109  if (GetParameters().m_ConstantWeights)
110  {
111  ConstTensor weightsTensor(managedWeight.GetTensorInfo(), managedWeight.Map());
112  optionalWeightsTensor = Optional<ConstTensor>(weightsTensor);
113 
115  {
116  ConstTensor biasTensor(managedBias.GetTensorInfo(), managedBias.Map());
117  optionalBiasTensor = Optional<ConstTensor>(biasTensor);
118  }
119  }
120 
121  visitor.VisitFullyConnectedLayer(this,
122  GetParameters(),
123  optionalWeightsTensor.value(),
124  optionalBiasTensor,
125  GetName());
126 }
127 
129 {
130  std::vector <armnn::ConstTensor> constTensors;
131  ManagedConstTensorHandle managedWeight(m_Weight);
132  ManagedConstTensorHandle managedBias(m_Bias);
133 
134  if(GetParameters().m_ConstantWeights)
135  {
136  constTensors.emplace_back(ConstTensor(managedWeight.GetTensorInfo(), managedWeight.Map()));
138  {
139  constTensors.emplace_back(ConstTensor(managedBias.GetTensorInfo(), managedBias.Map()));
140  }
141  }
142  strategy.ExecuteStrategy(this, GetParameters(), constTensors, GetName());
143 }
144 
145 } // namespace armnn
void Accept(ILayerVisitor &visitor) const override
Apply a visitor to this layer.
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the FullyConnected type.
std::vector< TensorShape > InferOutputShapes(const std::vector< TensorShape > &inputShapes) const override
By default returns inputShapes if the number of inputs are equal to number of outputs, otherwise infers the output shapes from given input shapes and layer properties.
FullyConnectedDescriptor m_Param
The parameters for the layer (not including tensor-valued weights etc.).
const FullyConnectedDescriptor & GetParameters() const
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
void ExecuteStrategy(IStrategy &strategy) const override
Apply a visitor to this layer.
bool m_TransposeWeightMatrix
Enable/disable transpose weight matrix.
ConstantTensors GetConstantTensorsByRef() override
Retrieve the handles to the constant values stored by the layer.
virtual void ExecuteStrategy(const armnn::IConnectableLayer *layer, const armnn::BaseDescriptor &descriptor, const std::vector< armnn::ConstTensor > &constants, const char *name, const armnn::LayerBindingId id=0)=0
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of FullyConnectedLayer.
FullyConnectedLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:433
const TensorInfo & GetTensorInfo() const
Copyright (c) 2021 ARM Limited and Contributors.
FullyConnectedLayer(const FullyConnectedDescriptor &param, const char *name)
Constructor to create a FullyConnectedLayer.
const IOutputSlot * GetConnection() const override
Definition: Layer.hpp:199
const ConstTensorHandle * m_Bias
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition: Layer.cpp:393
void FullyConnected(const TensorShape &rInputShape, Decoder< float > &rInputDecoder, const TensorShape &rOutputShape, Encoder< float > &rOutputEncoder, const TensorShape &rWeightsShape, Decoder< float > &rWeightDecoder, Decoder< float > &rBiasDecoder, const bool biasEnabled, const unsigned int K, const bool transposeWeights)
Performs a matrix multiplication and optionally adds a bias.
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:316
virtual void VisitFullyConnectedLayer(const IConnectableLayer *layer, const FullyConnectedDescriptor &fullyConnectedDescriptor, const ConstTensor &weights, const Optional< ConstTensor > &biases, const char *name=nullptr)=0
Function that a fully connected layer should call back to when its Accept(ILayerVisitor&) function is...
std::vector< std::reference_wrapper< std::shared_ptr< ConstTensorHandle > >> ConstantTensors
Definition: Layer.hpp:393
This layer represents a fully connected operation.
std::shared_ptr< ConstTensorHandle > m_Weight
A unique pointer to store Weight values.
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
A FullyConnectedDescriptor for the FullyConnectedLayer.
bool m_BiasEnabled
Enable/disable bias.
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:314
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
const ConstTensorHandle * m_Weight
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition: Layer.cpp:245
EmptyOptional is used to initialize the Optional class in case we want to have default value for an O...
Definition: Optional.hpp:32
std::shared_ptr< ConstTensorHandle > m_Bias
A unique pointer to store Bias values.
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
Helper function to reduce duplication in *LayerCreateWorkload.
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:318
virtual const TensorInfo & GetTensorInfo() const =0
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:311
virtual std::unique_ptr< IWorkload > CreateFullyConnected(const FullyConnectedQueueDescriptor &descriptor, const WorkloadInfo &info) const
const void * Map(bool blocking=true)
RAII Managed resource Unmaps MemoryArea once out of scope.
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:63
ShapeInferenceMethod m_ShapeInferenceMethod
Definition: Layer.hpp:408
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below...
Definition: Types.hpp:455
bool m_ConstantWeights
Enable/disable constant weights and biases.