ArmNN
 26.01
Loading...
Searching...
No Matches
QuantizedLstmLayer.cpp
Go to the documentation of this file.
1//
2// Copyright © 2017,2019-2024 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
6
7#include "LayerCloneBase.hpp"
8
10#include <armnn/TypesUtils.hpp>
13
14namespace armnn
15{
16
18 : Layer(3, 2, LayerType::QuantizedLstm, name)
19{
20}
21
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
81std::vector<TensorShape> QuantizedLstmLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
82{
83 if (inputShapes.size() != 3)
84 {
85 throw armnn::Exception("inputShapes' size is \"" + std::to_string(inputShapes.size()) +
86 "\" - should be \"3\".");
87 }
88
89 // Get input values for validation
90 unsigned int numBatches = inputShapes[0][0];
91 unsigned int outputSize = inputShapes[1][1];
92
93 std::vector<TensorShape> outShapes;
94 outShapes.push_back(TensorShape({numBatches, outputSize})); // cellStateOut
95 outShapes.push_back(TensorShape({numBatches, outputSize})); // output
96
97 return outShapes;
98}
99
101{
103
104 const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
105
107
108 auto inferredShapes = InferOutputShapes(
109 {
110 GetInputSlot(0).GetTensorInfo().GetShape(), // input
111 GetInputSlot(1).GetTensorInfo().GetShape(), // previousCellStateIn
112 GetInputSlot(2).GetTensorInfo().GetShape() // previousOutputIn
113 });
114
115 if (inferredShapes.size() != 2)
116 {
117 throw armnn::LayerValidationException("inferredShapes has "
118 + std::to_string(inferredShapes.size()) +
119 " element(s) - should only have 2.");
120 }
121
122 // Check weights and bias for nullptr
124 {
125 throw armnn::LayerValidationException("QuantizedLstmLayer: "
126 "m_QuantizedLstmParameters.m_InputToInputWeights "
127 "should not be null.");
128 }
129
131 {
132 throw armnn::LayerValidationException("QuantizedLstmLayer: "
133 "m_QuantizedLstmParameters.m_InputToForgetWeights "
134 "should not be null.");
135 }
136
138 {
139 throw armnn::LayerValidationException("QuantizedLstmLayer: "
140 "m_QuantizedLstmParameters.m_InputToCellWeights "
141 "should not be null.");
142 }
143
145 {
146 throw armnn::LayerValidationException("QuantizedLstmLayer: "
147 "m_QuantizedLstmParameters.m_InputToOutputWeights "
148 "should not be null.");
149 }
150
152 {
153 throw armnn::LayerValidationException("QuantizedLstmLayer: "
154 "m_QuantizedLstmParameters.m_RecurrentToInputWeights "
155 "should not be null.");
156 }
157
159 {
160 throw armnn::LayerValidationException("QuantizedLstmLayer: "
161 "m_QuantizedLstmParameters.m_RecurrentToForgetWeights "
162 "should not be null.");
163 }
164
166 {
167 throw armnn::LayerValidationException("QuantizedLstmLayer: "
168 "m_QuantizedLstmParameters.m_RecurrentToCellWeights "
169 "should not be null.");
170 }
171
173 {
174 throw armnn::LayerValidationException("QuantizedLstmLayer: "
175 "m_QuantizedLstmParameters.m_RecurrentToOutputWeights "
176 "should not be null.");
177 }
178
180 {
181 throw armnn::LayerValidationException("QuantizedLstmLayer: "
182 "m_QuantizedLstmParameters.m_InputGateBias "
183 "should not be null.");
184 }
185
187 {
188 throw armnn::LayerValidationException("QuantizedLstmLayer: "
189 "m_QuantizedLstmParameters.m_ForgetGateBias "
190 "should not be null.");
191 }
192
194 {
195 throw armnn::LayerValidationException("QuantizedLstmLayer: "
196 "m_QuantizedLstmParameters.m_CellBias "
197 "should not be null.");
198 }
199
201 {
202 throw armnn::LayerValidationException("QuantizedLstmLayer: "
203 "m_QuantizedLstmParameters.m_OutputGateBias "
204 "should not be null.");
205 }
206
207 // Check output TensorShape(s) match inferred shape
208 ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "QuantizedLstmLayer");
209
211 inferredShapes[1],
213 "QuantizedLstmLayer",
214 1);
215}
216
238
240{
241 std::vector<ConstTensor> constTensors;
242
247
252
257
258 // InputToX weight tensors
260 {
261 constTensors.emplace_back(ConstTensor(managedInputToInputWeights.GetTensorInfo(),
262 managedInputToInputWeights.Map()));
263 }
264
266 {
267 constTensors.emplace_back(ConstTensor(managedInputToForgetWeights.GetTensorInfo(),
268 managedInputToForgetWeights.Map()));
269 }
270
272 {
273 constTensors.emplace_back(ConstTensor(managedInputToCellWeights.GetTensorInfo(),
274 managedInputToCellWeights.Map()));
275 }
276
278 {
279 constTensors.emplace_back(ConstTensor(managedInputToOutputWeights.GetTensorInfo(),
280 managedInputToOutputWeights.Map()));
281 }
282
283 // RecurrentToX weight tensors
285 {
286 constTensors.emplace_back(ConstTensor(
287 managedRecurrentToInputWeights.GetTensorInfo(),
288 managedRecurrentToInputWeights.Map()));
289 }
290
292 {
293 constTensors.emplace_back(ConstTensor(
294 managedRecurrentToForgetWeights.GetTensorInfo(),
295 managedRecurrentToForgetWeights.Map()));
296 }
297
299 {
300 constTensors.emplace_back(ConstTensor(
301 managedRecurrentToCellWeights.GetTensorInfo(),
302 managedRecurrentToCellWeights.Map()));
303 }
304
306 {
307 constTensors.emplace_back(ConstTensor(
308 managedRecurrentToOutputWeights.GetTensorInfo(),
309 managedRecurrentToOutputWeights.Map()));
310 }
311
312 // Bias tensors
314 {
315 constTensors.emplace_back(ConstTensor(managedInputGateBias.GetTensorInfo(),
316 managedInputGateBias.Map()));
317 }
318
320 {
321 constTensors.emplace_back(ConstTensor(managedForgetGateBias.GetTensorInfo(),
322 managedForgetGateBias.Map()));
323 }
324
326 {
327 constTensors.emplace_back(ConstTensor(managedCellBias.GetTensorInfo(),
328 managedCellBias.Map()));
329 }
330
332 {
333 constTensors.emplace_back(ConstTensor(managedOutputGateBias.GetTensorInfo(),
334 managedOutputGateBias.Map()));
335 }
336
337
338 strategy.ExecuteStrategy(this, BaseDescriptor(), constTensors, GetName());
339}
340
341} // namespace armnn
#define CHECK_LOCATION()
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition Tensor.hpp:330
Base class for all ArmNN exceptions so that users can filter to just those.
std::vector< std::reference_wrapper< const std::shared_ptr< ConstTensorHandle > > > ImmutableConstantTensors
Definition INetwork.hpp:141
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
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.
const TensorInfo & GetTensorInfo() const override
Gets the TensorInfo for this InputSlot.
Definition Layer.cpp:614
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition Layer.cpp:410
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition Layer.hpp:337
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
Helper function to reduce duplication in *LayerCreateWorkload.
Definition Layer.hpp:409
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition Layer.cpp:526
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition Layer.hpp:339
const char * GetName() const override
Returns the name of the layer.
Definition Layer.hpp:332
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition Layer.cpp:457
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition Layer.cpp:303
ShapeInferenceMethod m_ShapeInferenceMethod
Definition Layer.hpp:441
const void * Map(bool blocking=true)
RAII Managed resource Unmaps MemoryArea once out of scope.
const TensorInfo & GetTensorInfo() const
const TensorInfo & GetTensorInfo() const override
Definition Layer.cpp:100
This layer represents a QuantizedLstm operation.
QuantizedLstmLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
QuantizedLstmLayer(const char *name)
Constructor to create a QuantizedLstmLayer.
Layer::ImmutableConstantTensors GetConstantTensorsByRef() const override
Retrieve the handles to the constant values stored by the layer.
void ExecuteStrategy(IStrategy &strategy) const override
Apply a visitor to this layer.
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,...
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of QuantizedLstmLayer.
QuantizedLstmParameters m_QuantizedLstmParameters
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the QuantizedLstm type.
const TensorShape & GetShape() const
Definition Tensor.hpp:193
Copyright (c) 2021 ARM Limited and Contributors.
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below.
Definition Types.hpp:494
armnn::TensorInfo GetTensorInfo(unsigned int numberOfBatches, unsigned int numberOfChannels, unsigned int height, unsigned int width, const armnn::DataLayout dataLayout, const armnn::DataType dataType)
Base class for all descriptors.
std::shared_ptr< ConstTensorHandle > m_RecurrentToForgetWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, outputSize] (QAsymm8).
std::shared_ptr< ConstTensorHandle > m_CellBias
A unique pointer to represent 1D bias tensor with dimensions [outputSize] (int32).
std::shared_ptr< ConstTensorHandle > m_InputToInputWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, inputSize] (QAsymm8).
std::shared_ptr< ConstTensorHandle > m_InputToOutputWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, inputSize] (QAsymm8).
std::shared_ptr< ConstTensorHandle > m_RecurrentToCellWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, outputSize] (QAsymm8).
std::shared_ptr< ConstTensorHandle > m_InputGateBias
A unique pointer to represent 1D bias tensor with dimensions [outputSize] (int32).
std::shared_ptr< ConstTensorHandle > m_OutputGateBias
A unique pointer to represent 1D bias tensor with dimensions [outputSize] (int32).
std::shared_ptr< ConstTensorHandle > m_InputToForgetWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, inputSize] (QAsymm8).
std::shared_ptr< ConstTensorHandle > m_InputToCellWeights
A unique pointer to represent 2D weights tensor with dimensions [outputSize, inputSize] (QAsymm8).
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).
std::shared_ptr< ConstTensorHandle > m_ForgetGateBias
A unique pointer to represent 1D bias tensor with dimensions [outputSize] (int32).
const ConstTensorHandle * m_InputToOutputWeights
const ConstTensorHandle * m_RecurrentToInputWeights
const ConstTensorHandle * m_ForgetGateBias
const ConstTensorHandle * m_InputGateBias
const ConstTensorHandle * m_RecurrentToOutputWeights
const ConstTensorHandle * m_OutputGateBias
const ConstTensorHandle * m_CellBias
const ConstTensorHandle * m_InputToCellWeights
const ConstTensorHandle * m_InputToForgetWeights
const ConstTensorHandle * m_InputToInputWeights
const ConstTensorHandle * m_RecurrentToCellWeights
const ConstTensorHandle * m_RecurrentToForgetWeights