ArmNN
 20.08
QuantizedLstmLayer.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 //
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  return factory.CreateQuantizedLstm(descriptor, PrepInfoAndDesc(descriptor));
43 }
44 
46 {
47  auto layer = CloneBase<QuantizedLstmLayer>(graph, GetName());
48 
50  std::make_unique<ScopedCpuTensorHandle>(*m_QuantizedLstmParameters.m_InputToInputWeights) : nullptr;
51  layer->m_QuantizedLstmParameters.m_InputToForgetWeights = m_QuantizedLstmParameters.m_InputToForgetWeights ?
52  std::make_unique<ScopedCpuTensorHandle>(*m_QuantizedLstmParameters.m_InputToForgetWeights) : nullptr;
53  layer->m_QuantizedLstmParameters.m_InputToCellWeights = m_QuantizedLstmParameters.m_InputToCellWeights ?
54  std::make_unique<ScopedCpuTensorHandle>(*m_QuantizedLstmParameters.m_InputToCellWeights) : nullptr;
55  layer->m_QuantizedLstmParameters.m_InputToOutputWeights = m_QuantizedLstmParameters.m_InputToOutputWeights ?
56  std::make_unique<ScopedCpuTensorHandle>(*m_QuantizedLstmParameters.m_InputToOutputWeights) : nullptr;
57 
58  layer->m_QuantizedLstmParameters.m_RecurrentToInputWeights = m_QuantizedLstmParameters.m_RecurrentToInputWeights ?
59  std::make_unique<ScopedCpuTensorHandle>(*m_QuantizedLstmParameters.m_RecurrentToInputWeights) : nullptr;
60  layer->m_QuantizedLstmParameters.m_RecurrentToForgetWeights = m_QuantizedLstmParameters.m_RecurrentToForgetWeights
61  ? std::make_unique<ScopedCpuTensorHandle>(*m_QuantizedLstmParameters.m_RecurrentToForgetWeights) : nullptr;
62  layer->m_QuantizedLstmParameters.m_RecurrentToCellWeights = m_QuantizedLstmParameters.m_RecurrentToCellWeights ?
63  std::make_unique<ScopedCpuTensorHandle>(*m_QuantizedLstmParameters.m_RecurrentToCellWeights) : nullptr;
64  layer->m_QuantizedLstmParameters.m_RecurrentToOutputWeights = m_QuantizedLstmParameters.m_RecurrentToOutputWeights
65  ? std::make_unique<ScopedCpuTensorHandle>(*m_QuantizedLstmParameters.m_RecurrentToOutputWeights) : nullptr;
66 
67  layer->m_QuantizedLstmParameters.m_InputGateBias = m_QuantizedLstmParameters.m_InputGateBias ?
68  std::make_unique<ScopedCpuTensorHandle>(*m_QuantizedLstmParameters.m_InputGateBias) : nullptr;
69  layer->m_QuantizedLstmParameters.m_ForgetGateBias = m_QuantizedLstmParameters.m_ForgetGateBias ?
70  std::make_unique<ScopedCpuTensorHandle>(*m_QuantizedLstmParameters.m_ForgetGateBias) : nullptr;
71  layer->m_QuantizedLstmParameters.m_CellBias = m_QuantizedLstmParameters.m_CellBias ?
72  std::make_unique<ScopedCpuTensorHandle>(*m_QuantizedLstmParameters.m_CellBias) : nullptr;
73  layer->m_QuantizedLstmParameters.m_OutputGateBias = m_QuantizedLstmParameters.m_OutputGateBias ?
74  std::make_unique<ScopedCpuTensorHandle>(*m_QuantizedLstmParameters.m_OutputGateBias) : nullptr;
75 
76  return std::move(layer);
77 }
78 
79 std::vector<TensorShape> QuantizedLstmLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
80 {
81  ARMNN_ASSERT(inputShapes.size() == 3);
82 
83  // Get input values for validation
84  unsigned int numBatches = inputShapes[0][0];
85  unsigned int outputSize = inputShapes[1][1];
86 
87  std::vector<TensorShape> outShapes;
88  outShapes.push_back(TensorShape({numBatches, outputSize})); // cellStateOut
89  outShapes.push_back(TensorShape({numBatches, outputSize})); // output
90 
91  return outShapes;
92 }
93 
95 {
97 
98  const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
99 
101 
102  auto inferredShapes = InferOutputShapes(
103  {
105  GetInputSlot(1).GetConnection()->GetTensorInfo().GetShape(), // previousCellStateIn
106  GetInputSlot(2).GetConnection()->GetTensorInfo().GetShape() // previousOutputIn
107  });
108 
109  ARMNN_ASSERT(inferredShapes.size() == 2);
110 
111  // Check weights and bias for nullptr
113  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_InputToInputWeights should not be null.");
115  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_InputToForgetWeights should not be null.");
117  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_InputToCellWeights should not be null.");
119  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_InputToOutputWeights should not be null.");
120 
122  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_RecurrentToInputWeights should not be null.");
124  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_RecurrentToForgetWeights should not be null.");
126  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_RecurrentToCellWeights should not be null.");
128  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_RecurrentToOutputWeights should not be null.");
129 
131  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_InputGateBias should not be null.");
133  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_ForgetGateBias should not be null.");
135  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_CellBias should not be null.");
137  "QuantizedLstmLayer: m_QuantizedLstmParameters.m_OutputGateBias should not be null.");
138 
139  // Check output TensorShape(s) match inferred shape
140  ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "QuantizedLstmLayer");
141 
143  inferredShapes[1],
145  "QuantizedLstmLayer",
146  1);
147 }
148 
150 {
151  return
152  {
157 
162 
167  };
168 }
169 
171 {
172  QuantizedLstmInputParams inputParams;
173 
174  // InputToX weight tensors
175  ConstTensor inputToInputWeightsTensor;
177  {
178  ConstTensor inputToInputWeightsTensorCopy(m_QuantizedLstmParameters.m_InputToInputWeights->GetTensorInfo(),
180  inputToInputWeightsTensor = inputToInputWeightsTensorCopy;
181  inputParams.m_InputToInputWeights = &inputToInputWeightsTensor;
182  }
183 
184  ConstTensor inputToForgetWeightsTensor;
186  {
187  ConstTensor inputToForgetWeightsTensorCopy(m_QuantizedLstmParameters.m_InputToForgetWeights->GetTensorInfo(),
189  inputToForgetWeightsTensor = inputToForgetWeightsTensorCopy;
190  inputParams.m_InputToForgetWeights = &inputToForgetWeightsTensor;
191  }
192 
193  ConstTensor inputToCellWeightsTensor;
195  {
196  ConstTensor inputToCellWeightsTensorCopy(m_QuantizedLstmParameters.m_InputToCellWeights->GetTensorInfo(),
198  inputToCellWeightsTensor = inputToCellWeightsTensorCopy;
199  inputParams.m_InputToCellWeights = &inputToCellWeightsTensor;
200  }
201 
202  ConstTensor inputToOutputWeightsTensor;
204  {
205  ConstTensor inputToOutputWeightsTensorCopy(m_QuantizedLstmParameters.m_InputToOutputWeights->GetTensorInfo(),
207  inputToOutputWeightsTensor = inputToOutputWeightsTensorCopy;
208  inputParams.m_InputToOutputWeights = &inputToOutputWeightsTensor;
209  }
210 
211  // RecurrentToX weight tensors
212  ConstTensor recurrentToInputWeightsTensor;
214  {
215  ConstTensor recurrentToInputWeightsTensorCopy(
218  recurrentToInputWeightsTensor = recurrentToInputWeightsTensorCopy;
219  inputParams.m_RecurrentToInputWeights = &recurrentToInputWeightsTensor;
220  }
221 
222  ConstTensor recurrentToForgetWeightsTensor;
224  {
225  ConstTensor recurrentToForgetWeightsTensorCopy(
228  recurrentToForgetWeightsTensor = recurrentToForgetWeightsTensorCopy;
229  inputParams.m_RecurrentToForgetWeights = &recurrentToForgetWeightsTensor;
230  }
231 
232  ConstTensor recurrentToCellWeightsTensor;
234  {
235  ConstTensor recurrentToCellWeightsTensorCopy(
238  recurrentToCellWeightsTensor = recurrentToCellWeightsTensorCopy;
239  inputParams.m_RecurrentToCellWeights = &recurrentToCellWeightsTensor;
240  }
241 
242  ConstTensor recurrentToOutputWeightsTensor;
244  {
245  ConstTensor recurrentToOutputWeightsTensorCopy(
248  recurrentToOutputWeightsTensor = recurrentToOutputWeightsTensorCopy;
249  inputParams.m_RecurrentToOutputWeights = &recurrentToOutputWeightsTensor;
250  }
251 
252  // Bias tensors
253  ConstTensor inputGateBiasTensor;
255  {
256  ConstTensor inputGateBiasTensorCopy(m_QuantizedLstmParameters.m_InputGateBias->GetTensorInfo(),
258  inputGateBiasTensor = inputGateBiasTensorCopy;
259  inputParams.m_InputGateBias = &inputGateBiasTensor;
260  }
261 
262  ConstTensor forgetGateBiasTensor;
264  {
265  ConstTensor forgetGateBiasTensorCopy(m_QuantizedLstmParameters.m_ForgetGateBias->GetTensorInfo(),
267  forgetGateBiasTensor = forgetGateBiasTensorCopy;
268  inputParams.m_ForgetGateBias = &forgetGateBiasTensor;
269  }
270 
271  ConstTensor cellBiasTensor;
272  if (m_QuantizedLstmParameters.m_CellBias != nullptr)
273  {
274  ConstTensor cellBiasTensorCopy(m_QuantizedLstmParameters.m_CellBias->GetTensorInfo(),
276  cellBiasTensor = cellBiasTensorCopy;
277  inputParams.m_CellBias = &cellBiasTensor;
278  }
279 
280  ConstTensor outputGateBiasTensor;
282  {
283  ConstTensor outputGateBiasCopy(m_QuantizedLstmParameters.m_OutputGateBias->GetTensorInfo(),
285  outputGateBiasTensor = outputGateBiasCopy;
286  inputParams.m_OutputGateBias = &outputGateBiasTensor;
287  }
288 
289  visitor.VisitQuantizedLstmLayer(this, inputParams, GetName());
290 }
291 
292 } // namespace armnn
const ConstCpuTensorHandle * m_RecurrentToForgetWeights
std::unique_ptr< ScopedCpuTensorHandle > m_RecurrentToInputWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, outputSize] (QAsymm8)...
Layer::ConstantTensors GetConstantTensorsByRef() override
Retrieve the handles to the constant values stored by the layer.
const ConstCpuTensorHandle * m_InputGateBias
QuantizedLstmParameters m_QuantizedLstmParameters
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
std::unique_ptr< ScopedCpuTensorHandle > m_InputToForgetWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, inputSize] (QAsymm8)...
const ConstTensor * m_RecurrentToOutputWeights
std::unique_ptr< ScopedCpuTensorHandle > m_RecurrentToOutputWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, outputSize] (QAsymm8)...
const ConstTensor * m_RecurrentToForgetWeights
QuantizedLstmLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
const ConstCpuTensorHandle * m_InputToCellWeights
virtual std::unique_ptr< IWorkload > CreateQuantizedLstm(const QuantizedLstmQueueDescriptor &descriptor, const WorkloadInfo &info) const
std::unique_ptr< ScopedCpuTensorHandle > m_InputToInputWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, inputSize] (QAsymm8)...
std::unique_ptr< ScopedCpuTensorHandle > m_RecurrentToForgetWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, outputSize] (QAsymm8)...
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:432
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of QuantizedLstmLayer.
Copyright (c) 2020 ARM Limited.
const IOutputSlot * GetConnection() const override
Definition: Layer.hpp:199
QuantizedLstmLayer(const char *name)
Constructor to create a QuantizedLstmLayer.
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition: Layer.cpp:392
std::unique_ptr< ScopedCpuTensorHandle > m_InputGateBias
A unique pointer to represent 1D bias tensor with dimensions [outputSize] (int32).
std::unique_ptr< ScopedCpuTensorHandle > m_CellBias
A unique pointer to represent 1D bias tensor with dimensions [outputSize] (int32).
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:344
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:312
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
This layer represents a QuantizedLstm operation.
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
Helper function to reduce duplication in *LayerCreateWorkload.
Definition: Layer.hpp:366
std::unique_ptr< ScopedCpuTensorHandle > m_ForgetGateBias
A unique pointer to represent 1D bias tensor with dimensions [outputSize] (int32).
const ConstTensor * m_InputToForgetWeights
const ConstCpuTensorHandle * m_ForgetGateBias
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:298
const ConstCpuTensorHandle * m_RecurrentToInputWeights
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
void Accept(ILayerVisitor &visitor) const override
Apply a visitor to this layer.
const ConstCpuTensorHandle * m_RecurrentToCellWeights
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the QuantizedLstm type.
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197
const ConstCpuTensorHandle * m_RecurrentToOutputWeights
const ConstTensor * m_RecurrentToInputWeights
virtual void VisitQuantizedLstmLayer(const IConnectableLayer *layer, const QuantizedLstmInputParams &params, const char *name=nullptr)=0
Function a QuantizedLstm layer should call back to when its Accept(ILayerVisitor&) function is invoke...
std::unique_ptr< ScopedCpuTensorHandle > m_OutputGateBias
A unique pointer to represent 1D bias tensor with dimensions [outputSize] (int32).
const ConstCpuTensorHandle * m_CellBias
const ConstTensor * m_RecurrentToCellWeights
const ConstCpuTensorHandle * m_OutputGateBias
const ConstTensor * m_InputToOutputWeights
std::unique_ptr< ScopedCpuTensorHandle > m_InputToCellWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, inputSize] (QAsymm8)...
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:314
virtual const TensorInfo & GetTensorInfo() const =0
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:307
const ConstCpuTensorHandle * m_InputToForgetWeights
std::unique_ptr< ScopedCpuTensorHandle > m_InputToOutputWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, inputSize] (QAsymm8)...
const ConstCpuTensorHandle * m_InputToOutputWeights
std::vector< std::reference_wrapper< std::unique_ptr< ScopedCpuTensorHandle > >> ConstantTensors
Definition: Layer.hpp:378
const TensorInfo & GetTensorInfo(const ITensorHandle *tensorHandle)
float32 helpers
const ConstCpuTensorHandle * m_InputToInputWeights
std::unique_ptr< ScopedCpuTensorHandle > m_RecurrentToCellWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, outputSize] (QAsymm8)...
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:63
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.
ShapeInferenceMethod m_ShapeInferenceMethod
Definition: Layer.hpp:387