xref: /aosp_15_r20/external/armnn/src/armnn/layers/QLstmLayer.hpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include "LayerWithParameters.hpp"
8 
9 namespace armnn
10 {
11 
12 class ScopedTensorHandle;
13 
14 struct QLstmBasicParameters
15 {
16     /// A unique pointer to represent 2D weights tensor with dimensions [num_units, inputSize] (QSymmS8).
17     std::shared_ptr<ConstTensorHandle> m_InputToForgetWeights;
18     /// A unique pointer to represent 2D weights tensor with dimensions [num_units, inputSize] (QSymmS8).
19     std::shared_ptr<ConstTensorHandle> m_InputToCellWeights;
20     /// A unique pointer to represent 2D weights tensor with dimensions [num_units, inputSize] (QSymmS8).
21     std::shared_ptr<ConstTensorHandle> m_InputToOutputWeights;
22 
23     /// A unique pointer to represent 2D weights tensor with dimensions [num_units, outputSize] (QSymmS8).
24     std::shared_ptr<ConstTensorHandle> m_RecurrentToForgetWeights;
25     /// A unique pointer to represent 2D weights tensor with dimensions [num_units, outputSize] (QSymmS8).
26     std::shared_ptr<ConstTensorHandle> m_RecurrentToCellWeights;
27     /// A unique pointer to represent 2D weights tensor with dimensions [num_units, outputSize] (QSymmS8).
28     std::shared_ptr<ConstTensorHandle> m_RecurrentToOutputWeights;
29 
30     /// A unique pointer to represent 1D bias tensor with dimensions [num_units] (int32).
31     std::shared_ptr<ConstTensorHandle> m_ForgetGateBias;
32     /// A unique pointer to represent 1D bias tensor with dimensions [num_units] (int32).
33     std::shared_ptr<ConstTensorHandle> m_CellBias;
34     /// A unique pointer to represent 1D bias tensor with dimensions [num_units] (int32).
35     std::shared_ptr<ConstTensorHandle> m_OutputGateBias;
36 };
37 
38 struct QLstmOptProjectionParameters
39 {
40     /// A unique pointer to represent 2D weights tensor with dimensions [output_size, num_units] (QSymmS8).
41     std::shared_ptr<ConstTensorHandle> m_ProjectionWeights;
42     /// A unique pointer to represent 1D weights tensor with dimensions [output_size] (int32).
43     std::shared_ptr<ConstTensorHandle> m_ProjectionBias;
44 };
45 
46 struct QLstmOptPeepholeParameters
47 {
48     /// A unique pointer to represent 1D weights tensor with dimensions [num_units] (QSymmS16).
49     std::shared_ptr<ConstTensorHandle> m_CellToInputWeights;
50     /// A unique pointer to represent 1D weights tensor with dimensions [num_units] (QSymmS16).
51     std::shared_ptr<ConstTensorHandle> m_CellToForgetWeights;
52     /// A unique pointer to represent 1D weights tensor with dimensions [num_units] (QSymmS16).
53     std::shared_ptr<ConstTensorHandle> m_CellToOutputWeights;
54 };
55 
56 struct QLstmOptCifgParameters
57 {
58     /// A unique pointer to represent 2D weights tensor with dimensions [input_size, num_units] (QSymmS8).
59     std::shared_ptr<ConstTensorHandle> m_InputToInputWeights;
60     /// A unique pointer to represent 2D weights tensor with dimensions [input_size, num_units] (QSymmS8).
61     std::shared_ptr<ConstTensorHandle> m_RecurrentToInputWeights;
62     /// A unique pointer to represent 1D weights tensor with dimensions [num_units] (int32).
63     std::shared_ptr<ConstTensorHandle> m_InputGateBias;
64 };
65 
66 struct QLstmOptLayerNormParameters
67 {
68     /// A unique pointer to represent 1D weights tensor with dimensions [num_units] (QSymmS16).
69     std::shared_ptr<ConstTensorHandle> m_InputLayerNormWeights;
70     /// A unique pointer to represent 1D weights tensor with dimensions [num_units] (QSymmS16).
71     std::shared_ptr<ConstTensorHandle> m_ForgetLayerNormWeights;
72     /// A unique pointer to represent 1D weights tensor with dimensions [num_units] (QSymmS16).
73     std::shared_ptr<ConstTensorHandle> m_CellLayerNormWeights;
74     /// A unique pointer to represent 1D weights tensor with dimensions [num_units] (QSymmS16).
75     std::shared_ptr<ConstTensorHandle> m_OutputLayerNormWeights;
76 };
77 
78 /// This layer represents a QLstm operation.
79 class QLstmLayer : public LayerWithParameters<QLstmDescriptor>
80 {
81 public:
82 
83     QLstmBasicParameters m_BasicParameters;
84     QLstmOptCifgParameters m_CifgParameters;
85     QLstmOptProjectionParameters m_ProjectionParameters;
86     QLstmOptPeepholeParameters m_PeepholeParameters;
87     QLstmOptLayerNormParameters m_LayerNormParameters;
88 
89     /// Makes a workload for the QLstm type.
90     /// @param [in] graph The graph where this layer can be found.
91     /// @param [in] factory The workload factory which will create the workload.
92     /// @return A pointer to the created workload, or nullptr if not created.
93     virtual std::unique_ptr<IWorkload> CreateWorkload(const IWorkloadFactory& factory) const override;
94 
95     /// Creates a dynamically-allocated copy of this layer.
96     /// @param [in] graph The graph into which this layer is being cloned.
97     QLstmLayer* Clone(Graph& graph) const override;
98 
99     /// Check if the input tensor shape(s)
100     /// will lead to a valid configuration of @ref QLstmLayer.
101     /// @param [in] shapeInferenceMethod Indicates if output shape shall be overwritten or just validated.
102     void ValidateTensorShapesFromInputs() override;
103 
104     /// By default returns inputShapes if the number of inputs are equal to number of outputs,
105     /// otherwise infers the output shapes from given input shapes and layer properties.
106     /// @param [in] inputShapes The input shapes layer has.
107     /// @return A vector to the inferred output shape.
108     std::vector<TensorShape> InferOutputShapes(const std::vector<TensorShape>& inputShapes) const override;
109 
110     void ExecuteStrategy(IStrategy& strategy) const override;
111 
112 protected:
113     /// Constructor to create a QLstmLayer.
114     /// @param [in] name Optional name for the layer.
115     QLstmLayer(const QLstmDescriptor& param, const char* name);
116 
117     /// Default destructor
118     ~QLstmLayer() = default;
119 
120     /// Retrieve the handles to the constant values stored by the layer.
121     /// @return A vector of the constant tensors stored by this layer.
122     Layer::ImmutableConstantTensors GetConstantTensorsByRef() const override;
123 };
124 
125 } // namespace armnn
126