ArmNN
 24.02
QuantizedLstmLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017,2019-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include "QuantizedLstmLayer.hpp"
6 
7 #include "LayerCloneBase.hpp"
8 
10 #include <armnn/TypesUtils.hpp>
13 
14 namespace armnn
15 {
16 
18  : Layer(3, 2, LayerType::QuantizedLstm, name)
19 {
20 }
21 
22 std::unique_ptr<IWorkload> QuantizedLstmLayer::CreateWorkload(const IWorkloadFactory& factory) const
23 {
25 
26  // QuantizedLstmLayer parameters - there are no optional params
31 
36 
41 
42  SetAdditionalInfo(descriptor);
43 
44  return factory.CreateWorkload(LayerType::QuantizedLstm, descriptor, PrepInfoAndDesc(descriptor));
45 }
46 
48 {
49  auto layer = CloneBase<QuantizedLstmLayer>(graph, GetName());
50 
51  layer->m_QuantizedLstmParameters.m_InputToInputWeights = m_QuantizedLstmParameters.m_InputToInputWeights ?
53  layer->m_QuantizedLstmParameters.m_InputToForgetWeights = m_QuantizedLstmParameters.m_InputToForgetWeights ?
55  layer->m_QuantizedLstmParameters.m_InputToCellWeights = m_QuantizedLstmParameters.m_InputToCellWeights ?
57  layer->m_QuantizedLstmParameters.m_InputToOutputWeights = m_QuantizedLstmParameters.m_InputToOutputWeights ?
59 
60  layer->m_QuantizedLstmParameters.m_RecurrentToInputWeights = m_QuantizedLstmParameters.m_RecurrentToInputWeights ?
62  layer->m_QuantizedLstmParameters.m_RecurrentToForgetWeights = m_QuantizedLstmParameters.m_RecurrentToForgetWeights
64  layer->m_QuantizedLstmParameters.m_RecurrentToCellWeights = m_QuantizedLstmParameters.m_RecurrentToCellWeights ?
66  layer->m_QuantizedLstmParameters.m_RecurrentToOutputWeights = m_QuantizedLstmParameters.m_RecurrentToOutputWeights
68 
69  layer->m_QuantizedLstmParameters.m_InputGateBias = m_QuantizedLstmParameters.m_InputGateBias ?
71  layer->m_QuantizedLstmParameters.m_ForgetGateBias = m_QuantizedLstmParameters.m_ForgetGateBias ?
73  layer->m_QuantizedLstmParameters.m_CellBias = m_QuantizedLstmParameters.m_CellBias ?
75  layer->m_QuantizedLstmParameters.m_OutputGateBias = m_QuantizedLstmParameters.m_OutputGateBias ?
77 
78  return std::move(layer);
79 }
80 
81 std::vector<TensorShape> QuantizedLstmLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
82 {
83  ARMNN_ASSERT(inputShapes.size() == 3);
84 
85  // Get input values for validation
86  unsigned int numBatches = inputShapes[0][0];
87  unsigned int outputSize = inputShapes[1][1];
88 
89  std::vector<TensorShape> outShapes;
90  outShapes.push_back(TensorShape({numBatches, outputSize})); // cellStateOut
91  outShapes.push_back(TensorShape({numBatches, outputSize})); // output
92 
93  return outShapes;
94 }
95 
97 {
99 
100  const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
101 
103 
104  auto inferredShapes = InferOutputShapes(
105  {
106  GetInputSlot(0).GetTensorInfo().GetShape(), // input
107  GetInputSlot(1).GetTensorInfo().GetShape(), // previousCellStateIn
108  GetInputSlot(2).GetTensorInfo().GetShape() // previousOutputIn
109  });
110 
111  ARMNN_ASSERT(inferredShapes.size() == 2);
112 
113  // Check weights and bias for nullptr
115  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_InputToInputWeights should not be null.");
117  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_InputToForgetWeights should not be null.");
119  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_InputToCellWeights should not be null.");
121  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_InputToOutputWeights should not be null.");
122 
124  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_RecurrentToInputWeights should not be null.");
126  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_RecurrentToForgetWeights should not be null.");
128  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_RecurrentToCellWeights should not be null.");
130  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_RecurrentToOutputWeights should not be null.");
131 
133  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_InputGateBias should not be null.");
135  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_ForgetGateBias should not be null.");
137  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_CellBias should not be null.");
139  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_OutputGateBias should not be null.");
140 
141  // Check output TensorShape(s) match inferred shape
142  ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "QuantizedLstmLayer");
143 
145  inferredShapes[1],
147  "QuantizedLstmLayer",
148  1);
149 }
150 
152 {
153  // For API stability DO NOT ALTER order and add new members to the end of vector
154  return
155  {
160 
165 
170  };
171 }
172 
174 {
175  std::vector<ConstTensor> constTensors;
176 
181 
186 
191 
192  // InputToX weight tensors
194  {
195  constTensors.emplace_back(ConstTensor(managedInputToInputWeights.GetTensorInfo(),
196  managedInputToInputWeights.Map()));
197  }
198 
200  {
201  constTensors.emplace_back(ConstTensor(managedInputToForgetWeights.GetTensorInfo(),
202  managedInputToForgetWeights.Map()));
203  }
204 
206  {
207  constTensors.emplace_back(ConstTensor(managedInputToCellWeights.GetTensorInfo(),
208  managedInputToCellWeights.Map()));
209  }
210 
212  {
213  constTensors.emplace_back(ConstTensor(managedInputToOutputWeights.GetTensorInfo(),
214  managedInputToOutputWeights.Map()));
215  }
216 
217  // RecurrentToX weight tensors
219  {
220  constTensors.emplace_back(ConstTensor(
221  managedRecurrentToInputWeights.GetTensorInfo(),
222  managedRecurrentToInputWeights.Map()));
223  }
224 
226  {
227  constTensors.emplace_back(ConstTensor(
228  managedRecurrentToForgetWeights.GetTensorInfo(),
229  managedRecurrentToForgetWeights.Map()));
230  }
231 
233  {
234  constTensors.emplace_back(ConstTensor(
235  managedRecurrentToCellWeights.GetTensorInfo(),
236  managedRecurrentToCellWeights.Map()));
237  }
238 
240  {
241  constTensors.emplace_back(ConstTensor(
242  managedRecurrentToOutputWeights.GetTensorInfo(),
243  managedRecurrentToOutputWeights.Map()));
244  }
245 
246  // Bias tensors
248  {
249  constTensors.emplace_back(ConstTensor(managedInputGateBias.GetTensorInfo(),
250  managedInputGateBias.Map()));
251  }
252 
254  {
255  constTensors.emplace_back(ConstTensor(managedForgetGateBias.GetTensorInfo(),
256  managedForgetGateBias.Map()));
257  }
258 
259  if (m_QuantizedLstmParameters.m_CellBias != nullptr)
260  {
261  constTensors.emplace_back(ConstTensor(managedCellBias.GetTensorInfo(),
262  managedCellBias.Map()));
263  }
264 
266  {
267  constTensors.emplace_back(ConstTensor(managedOutputGateBias.GetTensorInfo(),
268  managedOutputGateBias.Map()));
269  }
270 
271 
272  strategy.ExecuteStrategy(this, BaseDescriptor(), constTensors, GetName());
273 }
274 
275 } // namespace armnn
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
armnn::QuantizedLstmQueueDescriptor::m_CellBias
const ConstTensorHandle * m_CellBias
Definition: WorkloadData.hpp:645
armnn::QuantizedLstmParameters::m_InputToInputWeights
std::shared_ptr< ConstTensorHandle > m_InputToInputWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, inputSize] (QAsymm8).
Definition: QuantizedLstmLayer.hpp:17
armnn::QuantizedLstmQueueDescriptor::m_RecurrentToInputWeights
const ConstTensorHandle * m_RecurrentToInputWeights
Definition: WorkloadData.hpp:638
armnn::QuantizedLstmParameters::m_CellBias
std::shared_ptr< ConstTensorHandle > m_CellBias
A unique pointer to represent 1D bias tensor with dimensions [outputSize] (int32).
Definition: QuantizedLstmLayer.hpp:39
armnn::QuantizedLstmQueueDescriptor::m_InputToForgetWeights
const ConstTensorHandle * m_InputToForgetWeights
Definition: WorkloadData.hpp:634
armnn::QuantizedLstmLayer::ValidateTensorShapesFromInputs
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of QuantizedLstmLayer.
Definition: QuantizedLstmLayer.cpp:96
armnn::OutputSlot::GetTensorInfo
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:92
armnn::QuantizedLstmLayer::GetConstantTensorsByRef
Layer::ImmutableConstantTensors GetConstantTensorsByRef() const override
Retrieve the handles to the constant values stored by the layer.
Definition: QuantizedLstmLayer.cpp:151
TypesUtils.hpp
armnn::QuantizedLstmLayer::Clone
QuantizedLstmLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
Definition: QuantizedLstmLayer.cpp:47
armnn::QuantizedLstmParameters::m_OutputGateBias
std::shared_ptr< ConstTensorHandle > m_OutputGateBias
A unique pointer to represent 1D bias tensor with dimensions [outputSize] (int32).
Definition: QuantizedLstmLayer.hpp:41
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnn::Layer::ValidateAndCopyShape
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition: Layer.cpp:435
armnn::Layer::GetOutputSlot
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:339
armnn::QuantizedLstmQueueDescriptor::m_RecurrentToOutputWeights
const ConstTensorHandle * m_RecurrentToOutputWeights
Definition: WorkloadData.hpp:641
armnn::QuantizedLstmQueueDescriptor::m_InputGateBias
const ConstTensorHandle * m_InputGateBias
Definition: WorkloadData.hpp:643
armnn::LayerType::QuantizedLstm
@ QuantizedLstm
armnn::IStrategy
Definition: IStrategy.hpp:16
ARMNN_ASSERT_MSG
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
armnn::ManagedConstTensorHandle
Definition: TensorHandle.hpp:187
armnn::Layer::GetInputSlot
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:337
WorkloadFactory.hpp
armnn::Layer::GetName
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:332
armnn::QuantizedLstmParameters::m_RecurrentToOutputWeights
std::shared_ptr< ConstTensorHandle > m_RecurrentToOutputWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, outputSize] (QAsymm8).
Definition: QuantizedLstmLayer.hpp:32
armnn::QuantizedLstmParameters::m_InputToForgetWeights
std::shared_ptr< ConstTensorHandle > m_InputToForgetWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, inputSize] (QAsymm8).
Definition: QuantizedLstmLayer.hpp:19
armnn::Layer
Definition: Layer.hpp:230
armnn::IConnectableLayer::ImmutableConstantTensors
std::vector< std::reference_wrapper< const std::shared_ptr< ConstTensorHandle > >> ImmutableConstantTensors
Definition: INetwork.hpp:141
armnn::InputSlot::GetTensorInfo
const TensorInfo & GetTensorInfo() const override
Gets the TensorInfo for this InputSlot.
Definition: Layer.cpp:592
armnn::QuantizedLstmParameters::m_InputToCellWeights
std::shared_ptr< ConstTensorHandle > m_InputToCellWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, inputSize] (QAsymm8).
Definition: QuantizedLstmLayer.hpp:21
armnn::TensorShape
Definition: Tensor.hpp:20
armnn::QuantizedLstmQueueDescriptor::m_RecurrentToCellWeights
const ConstTensorHandle * m_RecurrentToCellWeights
Definition: WorkloadData.hpp:640
armnn::QuantizedLstmQueueDescriptor::m_InputToInputWeights
const ConstTensorHandle * m_InputToInputWeights
Definition: WorkloadData.hpp:633
armnn::QuantizedLstmParameters::m_ForgetGateBias
std::shared_ptr< ConstTensorHandle > m_ForgetGateBias
A unique pointer to represent 1D bias tensor with dimensions [outputSize] (int32).
Definition: QuantizedLstmLayer.hpp:37
armnn::Layer::PrepInfoAndDesc
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
Helper function to reduce duplication in *Layer::CreateWorkload.
Definition: Layer.hpp:409
QuantizedLstmLayer.hpp
armnn::QuantizedLstmParameters::m_InputToOutputWeights
std::shared_ptr< ConstTensorHandle > m_InputToOutputWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, inputSize] (QAsymm8).
Definition: QuantizedLstmLayer.hpp:23
armnn::IWorkloadFactory
Definition: WorkloadFactory.hpp:22
armnn::QuantizedLstmQueueDescriptor::m_RecurrentToForgetWeights
const ConstTensorHandle * m_RecurrentToForgetWeights
Definition: WorkloadData.hpp:639
armnn::Layer::VerifyShapeInferenceType
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:504
armnn::GetTensorInfo
const TensorInfo & GetTensorInfo(const ITensorHandle *tensorHandle)
float32 helpers
Definition: RefWorkloadUtils.hpp:33
armnn::ManagedConstTensorHandle::Map
const void * Map(bool blocking=true)
RAII Managed resource Unmaps MemoryArea once out of scope.
Definition: TensorHandle.hpp:196
armnn::Layer::SetAdditionalInfo
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition: Layer.cpp:287
armnn::QuantizedLstmQueueDescriptor::m_InputToCellWeights
const ConstTensorHandle * m_InputToCellWeights
Definition: WorkloadData.hpp:635
armnn::QuantizedLstmLayer
This layer represents a QuantizedLstm operation.
Definition: QuantizedLstmLayer.hpp:45
armnn::BaseDescriptor
Base class for all descriptors.
Definition: Descriptors.hpp:22
armnn::QuantizedLstmQueueDescriptor::m_InputToOutputWeights
const ConstTensorHandle * m_InputToOutputWeights
Definition: WorkloadData.hpp:636
armnn::QuantizedLstmQueueDescriptor
Definition: WorkloadData.hpp:614
armnn::QuantizedLstmLayer::m_QuantizedLstmParameters
QuantizedLstmParameters m_QuantizedLstmParameters
Definition: QuantizedLstmLayer.hpp:49
TensorHandle.hpp
armnn::QuantizedLstmLayer::CreateWorkload
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the QuantizedLstm type.
Definition: QuantizedLstmLayer.cpp:22
armnn::QuantizedLstmLayer::InferOutputShapes
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,...
Definition: QuantizedLstmLayer.cpp:81
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:193
armnn::QuantizedLstmLayer::ExecuteStrategy
void ExecuteStrategy(IStrategy &strategy) const override
Apply a visitor to this layer.
Definition: QuantizedLstmLayer.cpp:173
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
QuantizedLstmParams.hpp
armnn::QuantizedLstmParameters::m_RecurrentToCellWeights
std::shared_ptr< ConstTensorHandle > m_RecurrentToCellWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, outputSize] (QAsymm8).
Definition: QuantizedLstmLayer.hpp:30
armnn::ConstTensor
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:329
armnn::Layer::VerifyLayerConnections
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:391
armnn::QuantizedLstmParameters::m_InputGateBias
std::shared_ptr< ConstTensorHandle > m_InputGateBias
A unique pointer to represent 1D bias tensor with dimensions [outputSize] (int32).
Definition: QuantizedLstmLayer.hpp:35
armnn::Layer::m_ShapeInferenceMethod
ShapeInferenceMethod m_ShapeInferenceMethod
Definition: Layer.hpp:441
armnn::QuantizedLstmLayer::QuantizedLstmLayer
QuantizedLstmLayer(const char *name)
Constructor to create a QuantizedLstmLayer.
Definition: QuantizedLstmLayer.cpp:17
armnn::LayerType
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below.
Definition: Types.hpp:491
armnn::QuantizedLstmQueueDescriptor::m_OutputGateBias
const ConstTensorHandle * m_OutputGateBias
Definition: WorkloadData.hpp:646
armnn::Graph
Definition: Graph.hpp:30
armnn::IWorkloadFactory::CreateWorkload
virtual std::unique_ptr< IWorkload > CreateWorkload(LayerType type, const QueueDescriptor &descriptor, const WorkloadInfo &info) const =0
Backends should implement their own CreateWorkload function with a switch statement.
armnn::IStrategy::ExecuteStrategy
virtual void ExecuteStrategy(const IConnectableLayer *layer, const armnn::BaseDescriptor &descriptor, const std::vector< armnn::ConstTensor > &constants, const char *name, const armnn::LayerBindingId id=0)=0
armnn::QuantizedLstmParameters::m_RecurrentToInputWeights
std::shared_ptr< ConstTensorHandle > m_RecurrentToInputWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, outputSize] (QAsymm8).
Definition: QuantizedLstmLayer.hpp:26
armnn::QuantizedLstmParameters::m_RecurrentToForgetWeights
std::shared_ptr< ConstTensorHandle > m_RecurrentToForgetWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, outputSize] (QAsymm8).
Definition: QuantizedLstmLayer.hpp:28
LayerCloneBase.hpp
armnn::ManagedConstTensorHandle::GetTensorInfo
const TensorInfo & GetTensorInfo() const
Definition: TensorHandle.hpp:239
armnn::QuantizedLstmQueueDescriptor::m_ForgetGateBias
const ConstTensorHandle * m_ForgetGateBias
Definition: WorkloadData.hpp:644