ArmNN
 21.11
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  SetAdditionalInfo(descriptor);
43 
44  return factory.CreateQuantizedLstm(descriptor, PrepInfoAndDesc(descriptor));
45 }
46 
48 {
49  auto layer = CloneBase<QuantizedLstmLayer>(graph, GetName());
50 
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  {
107  GetInputSlot(1).GetConnection()->GetTensorInfo().GetShape(), // previousCellStateIn
108  GetInputSlot(2).GetConnection()->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  return
154  {
159 
164 
169  };
170 }
171 
173 void QuantizedLstmLayer::Accept(ILayerVisitor& visitor) const
174 {
175  QuantizedLstmInputParams inputParams;
176 
181 
186 
191 
192  // InputToX weight tensors
193  ConstTensor inputToInputWeightsTensor;
195  {
196  ConstTensor inputToInputWeightsTensorCopy(managedInputToInputWeights.GetTensorInfo(),
197  managedInputToInputWeights.Map());
198  inputToInputWeightsTensor = inputToInputWeightsTensorCopy;
199  inputParams.m_InputToInputWeights = &inputToInputWeightsTensor;
200  }
201 
202  ConstTensor inputToForgetWeightsTensor;
204  {
205  ConstTensor inputToForgetWeightsTensorCopy(managedInputToForgetWeights.GetTensorInfo(),
206  managedInputToForgetWeights.Map());
207  inputToForgetWeightsTensor = inputToForgetWeightsTensorCopy;
208  inputParams.m_InputToForgetWeights = &inputToForgetWeightsTensor;
209  }
210 
211  ConstTensor inputToCellWeightsTensor;
213  {
214  ConstTensor inputToCellWeightsTensorCopy(managedInputToCellWeights.GetTensorInfo(),
215  managedInputToCellWeights.Map());
216  inputToCellWeightsTensor = inputToCellWeightsTensorCopy;
217  inputParams.m_InputToCellWeights = &inputToCellWeightsTensor;
218  }
219 
220  ConstTensor inputToOutputWeightsTensor;
222  {
223  ConstTensor inputToOutputWeightsTensorCopy(managedInputToOutputWeights.GetTensorInfo(),
224  managedInputToOutputWeights.Map());
225  inputToOutputWeightsTensor = inputToOutputWeightsTensorCopy;
226  inputParams.m_InputToOutputWeights = &inputToOutputWeightsTensor;
227  }
228 
229  // RecurrentToX weight tensors
230  ConstTensor recurrentToInputWeightsTensor;
232  {
233  ConstTensor recurrentToInputWeightsTensorCopy(
234  managedRecurrentToInputWeights.GetTensorInfo(),
235  managedRecurrentToInputWeights.Map());
236  recurrentToInputWeightsTensor = recurrentToInputWeightsTensorCopy;
237  inputParams.m_RecurrentToInputWeights = &recurrentToInputWeightsTensor;
238  }
239 
240  ConstTensor recurrentToForgetWeightsTensor;
242  {
243  ConstTensor recurrentToForgetWeightsTensorCopy(
244  managedRecurrentToForgetWeights.GetTensorInfo(),
245  managedRecurrentToForgetWeights.Map());
246  recurrentToForgetWeightsTensor = recurrentToForgetWeightsTensorCopy;
247  inputParams.m_RecurrentToForgetWeights = &recurrentToForgetWeightsTensor;
248  }
249 
250  ConstTensor recurrentToCellWeightsTensor;
252  {
253  ConstTensor recurrentToCellWeightsTensorCopy(
254  managedRecurrentToCellWeights.GetTensorInfo(),
255  managedRecurrentToCellWeights.Map());
256  recurrentToCellWeightsTensor = recurrentToCellWeightsTensorCopy;
257  inputParams.m_RecurrentToCellWeights = &recurrentToCellWeightsTensor;
258  }
259 
260  ConstTensor recurrentToOutputWeightsTensor;
262  {
263  ConstTensor recurrentToOutputWeightsTensorCopy(
264  managedRecurrentToOutputWeights.GetTensorInfo(),
265  managedRecurrentToOutputWeights.Map());
266  recurrentToOutputWeightsTensor = recurrentToOutputWeightsTensorCopy;
267  inputParams.m_RecurrentToOutputWeights = &recurrentToOutputWeightsTensor;
268  }
269 
270  // Bias tensors
271  ConstTensor inputGateBiasTensor;
273  {
274  ConstTensor inputGateBiasTensorCopy(managedInputGateBias.GetTensorInfo(),
275  managedInputGateBias.Map());
276  inputGateBiasTensor = inputGateBiasTensorCopy;
277  inputParams.m_InputGateBias = &inputGateBiasTensor;
278  }
279 
280  ConstTensor forgetGateBiasTensor;
282  {
283  ConstTensor forgetGateBiasTensorCopy(managedForgetGateBias.GetTensorInfo(),
284  managedForgetGateBias.Map());
285  forgetGateBiasTensor = forgetGateBiasTensorCopy;
286  inputParams.m_ForgetGateBias = &forgetGateBiasTensor;
287  }
288 
289  ConstTensor cellBiasTensor;
290  if (m_QuantizedLstmParameters.m_CellBias != nullptr)
291  {
292  ConstTensor cellBiasTensorCopy(managedCellBias.GetTensorInfo(),
293  managedCellBias.Map());
294  cellBiasTensor = cellBiasTensorCopy;
295  inputParams.m_CellBias = &cellBiasTensor;
296  }
297 
298  ConstTensor outputGateBiasTensor;
300  {
301  ConstTensor outputGateBiasCopy(managedOutputGateBias.GetTensorInfo(),
302  managedOutputGateBias.Map());
303  outputGateBiasTensor = outputGateBiasCopy;
304  inputParams.m_OutputGateBias = &outputGateBiasTensor;
305  }
306 
307  visitor.VisitQuantizedLstmLayer(this, inputParams, GetName());
308 }
310 
312 {
313  std::vector<ConstTensor> constTensors;
314 
319 
324 
329 
330  // InputToX weight tensors
332  {
333  constTensors.emplace_back(ConstTensor(managedInputToInputWeights.GetTensorInfo(),
334  managedInputToInputWeights.Map()));
335  }
336 
338  {
339  constTensors.emplace_back(ConstTensor(managedInputToForgetWeights.GetTensorInfo(),
340  managedInputToForgetWeights.Map()));
341  }
342 
344  {
345  constTensors.emplace_back(ConstTensor(managedInputToCellWeights.GetTensorInfo(),
346  managedInputToCellWeights.Map()));
347  }
348 
350  {
351  constTensors.emplace_back(ConstTensor(managedInputToOutputWeights.GetTensorInfo(),
352  managedInputToOutputWeights.Map()));
353  }
354 
355  // RecurrentToX weight tensors
357  {
358  constTensors.emplace_back(ConstTensor(
359  managedRecurrentToInputWeights.GetTensorInfo(),
360  managedRecurrentToInputWeights.Map()));
361  }
362 
364  {
365  constTensors.emplace_back(ConstTensor(
366  managedRecurrentToForgetWeights.GetTensorInfo(),
367  managedRecurrentToForgetWeights.Map()));
368  }
369 
371  {
372  constTensors.emplace_back(ConstTensor(
373  managedRecurrentToCellWeights.GetTensorInfo(),
374  managedRecurrentToCellWeights.Map()));
375  }
376 
378  {
379  constTensors.emplace_back(ConstTensor(
380  managedRecurrentToOutputWeights.GetTensorInfo(),
381  managedRecurrentToOutputWeights.Map()));
382  }
383 
384  // Bias tensors
386  {
387  constTensors.emplace_back(ConstTensor(managedInputGateBias.GetTensorInfo(),
388  managedInputGateBias.Map()));
389  }
390 
392  {
393  constTensors.emplace_back(ConstTensor(managedForgetGateBias.GetTensorInfo(),
394  managedForgetGateBias.Map()));
395  }
396 
397  if (m_QuantizedLstmParameters.m_CellBias != nullptr)
398  {
399  constTensors.emplace_back(ConstTensor(managedCellBias.GetTensorInfo(),
400  managedCellBias.Map()));
401  }
402 
404  {
405  constTensors.emplace_back(ConstTensor(managedOutputGateBias.GetTensorInfo(),
406  managedOutputGateBias.Map()));
407  }
408 
409 
410  strategy.ExecuteStrategy(this, BaseDescriptor(), constTensors, GetName());
411 }
412 
413 } // namespace armnn
Layer::ConstantTensors GetConstantTensorsByRef() override
Retrieve the handles to the constant values stored by the layer.
std::shared_ptr< ConstTensorHandle > m_ForgetGateBias
A unique pointer to represent 1D bias tensor with dimensions [outputSize] (int32).
QuantizedLstmParameters m_QuantizedLstmParameters
const TensorShape & GetShape() const
Definition: Tensor.hpp:191
const ConstTensor * m_RecurrentToOutputWeights
std::shared_ptr< ConstTensorHandle > m_InputToForgetWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, inputSize] (QAsymm8)...
#define ARMNN_NO_DEPRECATE_WARN_BEGIN
Definition: Deprecated.hpp:33
const ConstTensor * m_RecurrentToForgetWeights
std::shared_ptr< ConstTensorHandle > m_InputGateBias
A unique pointer to represent 1D bias tensor with dimensions [outputSize] (int32).
QuantizedLstmLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this 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
virtual std::unique_ptr< IWorkload > CreateQuantizedLstm(const QuantizedLstmQueueDescriptor &descriptor, const WorkloadInfo &info) const
const ConstTensorHandle * m_InputGateBias
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:433
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of QuantizedLstmLayer.
const TensorInfo & GetTensorInfo() const
Copyright (c) 2021 ARM Limited and Contributors.
std::shared_ptr< ConstTensorHandle > m_InputToOutputWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, inputSize] (QAsymm8)...
const IOutputSlot * GetConnection() const override
Definition: Layer.hpp:199
const ConstTensorHandle * m_RecurrentToInputWeights
QuantizedLstmLayer(const char *name)
Constructor to create a QuantizedLstmLayer.
std::shared_ptr< ConstTensorHandle > m_CellBias
A unique pointer to represent 1D bias tensor with dimensions [outputSize] (int32).
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition: Layer.cpp:393
std::shared_ptr< ConstTensorHandle > m_RecurrentToOutputWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, outputSize] (QAsymm8)...
std::shared_ptr< ConstTensorHandle > m_RecurrentToInputWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, outputSize] (QAsymm8)...
Base class for all descriptors.
Definition: Descriptors.hpp:22
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:349
std::shared_ptr< ConstTensorHandle > m_InputToCellWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, inputSize] (QAsymm8)...
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:316
const ConstTensorHandle * m_InputToForgetWeights
std::vector< std::reference_wrapper< std::shared_ptr< ConstTensorHandle > >> ConstantTensors
Definition: Layer.hpp:393
#define ARMNN_NO_DEPRECATE_WARN_END
Definition: Deprecated.hpp:34
#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:381
const ConstTensor * m_InputToForgetWeights
const ConstTensorHandle * m_RecurrentToCellWeights
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:327
const ConstTensorHandle * m_ForgetGateBias
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
const ConstTensorHandle * m_RecurrentToOutputWeights
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the QuantizedLstm type.
const ConstTensorHandle * m_OutputGateBias
#define CHECK_LOCATION()
Definition: Exceptions.hpp:209
const ConstTensorHandle * m_RecurrentToForgetWeights
const ConstTensor * m_RecurrentToInputWeights
std::shared_ptr< ConstTensorHandle > m_OutputGateBias
A unique pointer to represent 1D bias tensor with dimensions [outputSize] (int32).
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition: Layer.cpp:245
std::shared_ptr< ConstTensorHandle > m_RecurrentToCellWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, outputSize] (QAsymm8)...
ARMNN_NO_DEPRECATE_WARN_BEGIN void Accept(ILayerVisitor &visitor) const override
const ConstTensorHandle * m_InputToOutputWeights
const ConstTensorHandle * m_InputToInputWeights
const ConstTensorHandle * m_CellBias
const ConstTensor * m_RecurrentToCellWeights
const ConstTensor * m_InputToOutputWeights
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
const void * Map(bool blocking=true)
RAII Managed resource Unmaps MemoryArea once out of scope.
const TensorInfo & GetTensorInfo(const ITensorHandle *tensorHandle)
float32 helpers
std::shared_ptr< ConstTensorHandle > m_RecurrentToForgetWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, outputSize] (QAsymm8)...
const ConstTensorHandle * m_InputToCellWeights
std::shared_ptr< ConstTensorHandle > m_InputToInputWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, inputSize] (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.
ARMNN_NO_DEPRECATE_WARN_END void ExecuteStrategy(IStrategy &strategy) const override
Apply a visitor to this layer.
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:443