1 // 2 // Copyright © 2017 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 ScopedCpuTensorHandle; 13 14 struct LstmOptLayerNormParameters 15 { 16 /// A unique pointer to represent 1D weights tensor with dimensions [num_units]. 17 std::unique_ptr<ScopedCpuTensorHandle> m_InputLayerNormWeights; 18 /// A unique pointer to represent 1D weights tensor with dimensions [num_units]. 19 std::unique_ptr<ScopedCpuTensorHandle> m_ForgetLayerNormWeights; 20 /// A unique pointer to represent 1D weights tensor with dimensions [num_units]. 21 std::unique_ptr<ScopedCpuTensorHandle> m_CellLayerNormWeights; 22 /// A unique pointer to represent 1D weights tensor with dimensions [num_units]. 23 std::unique_ptr<ScopedCpuTensorHandle> m_OutputLayerNormWeights; 24 }; 25 26 struct LstmOptCifgParameters 27 { 28 /// A unique pointer to represent 2D weights tensor with dimensions [input_size, num_units]. 29 std::unique_ptr<ScopedCpuTensorHandle> m_InputToInputWeights; 30 /// A unique pointer to represent 2D weights tensor with dimensions [input_size, num_units]. 31 std::unique_ptr<ScopedCpuTensorHandle> m_RecurrentToInputWeights; 32 /// A unique pointer to represent 1D weights tensor with dimensions [num_units]. 33 std::unique_ptr<ScopedCpuTensorHandle> m_InputGateBias; 34 }; 35 36 struct LstmOptProjectionParameters 37 { 38 /// A unique pointer to represent 2D weights tensor with dimensions [output_size, num_units]. 39 std::unique_ptr<ScopedCpuTensorHandle> m_ProjectionWeights; 40 /// A unique pointer to represent 1D weights tensor with dimensions [output_size]. 41 std::unique_ptr<ScopedCpuTensorHandle> m_ProjectionBias; 42 }; 43 44 struct LstmOptPeepholeParameters 45 { 46 /// A unique pointer to represent 1D weights tensor with dimensions [num_units]. 47 std::unique_ptr<ScopedCpuTensorHandle> m_CellToInputWeights; 48 /// A unique pointer to represent 1D weights tensor with dimensions [num_units]. 49 std::unique_ptr<ScopedCpuTensorHandle> m_CellToForgetWeights; 50 /// A unique pointer to represent 1D weights tensor with dimensions [num_units]. 51 std::unique_ptr<ScopedCpuTensorHandle> m_CellToOutputWeights; 52 }; 53 54 struct LstmBasicParameters 55 { 56 /// A unique pointer to represent 2D weights tensor with dimensions [input_size, num_units]. 57 std::unique_ptr<ScopedCpuTensorHandle> m_InputToForgetWeights; 58 /// A unique pointer to represent 2D weights tensor with dimensions [input_size, num_units]. 59 std::unique_ptr<ScopedCpuTensorHandle> m_InputToCellWeights; 60 /// A unique pointer to represent 2D weights tensor with dimensions [input_size, num_units]. 61 std::unique_ptr<ScopedCpuTensorHandle> m_InputToOutputWeights; 62 /// A unique pointer to represent 2D weights tensor with dimensions [output_size, num_units]. 63 std::unique_ptr<ScopedCpuTensorHandle> m_RecurrentToForgetWeights; 64 /// A unique pointer to represent 2D weights tensor with dimensions [output_size, num_units]. 65 std::unique_ptr<ScopedCpuTensorHandle> m_RecurrentToCellWeights; 66 /// A unique pointer to represent 2D weights tensor with dimensions [output_size, num_units]. 67 std::unique_ptr<ScopedCpuTensorHandle> m_RecurrentToOutputWeights; 68 /// A unique pointer to represent 1D weights tensor with dimensions [num_units]. 69 std::unique_ptr<ScopedCpuTensorHandle> m_ForgetGateBias; 70 /// A unique pointer to represent 1D weights tensor with dimensions [num_units]. 71 std::unique_ptr<ScopedCpuTensorHandle> m_CellBias; 72 /// A unique pointer to represent 1D weights tensor with dimensions [num_units]. 73 std::unique_ptr<ScopedCpuTensorHandle> m_OutputGateBias; 74 }; 75 76 /// This layer represents a LSTM operation. 77 class LstmLayer : public LayerWithParameters<LstmDescriptor> 78 { 79 public: 80 81 LstmBasicParameters m_BasicParameters; 82 LstmOptCifgParameters m_CifgParameters; 83 LstmOptProjectionParameters m_ProjectionParameters; 84 LstmOptPeepholeParameters m_PeepholeParameters; 85 LstmOptLayerNormParameters m_LayerNormParameters; 86 87 /// Makes a workload for the LSTM type. 88 /// @param [in] graph The graph where this layer can be found. 89 /// @param [in] factory The workload factory which will create the workload. 90 /// @return A pointer to the created workload, or nullptr if not created. 91 virtual std::unique_ptr<IWorkload> CreateWorkload(const IWorkloadFactory& factory) const override; 92 93 /// Creates a dynamically-allocated copy of this layer. 94 /// @param [in] graph The graph into which this layer is being cloned. 95 LstmLayer* Clone(Graph& graph) const override; 96 97 /// Check if the input tensor shape(s) 98 /// will lead to a valid configuration of @ref LstmLayer. 99 /// @param [in] shapeInferenceMethod Indicates if output shape shall be overwritten or just validated. 100 void ValidateTensorShapesFromInputs() override; 101 102 /// By default returns inputShapes if the number of inputs are equal to number of outputs, 103 /// otherwise infers the output shapes from given input shapes and layer properties. 104 /// @param [in] inputShapes The input shapes layer has. 105 /// @return A vector to the inferred output shape. 106 std::vector<TensorShape> InferOutputShapes(const std::vector<TensorShape>& inputShapes) const override; 107 108 void Accept(ILayerVisitor& visitor) const override; 109 110 protected: 111 /// Constructor to create a LstmLayer. 112 /// @param [in] param LstmDescriptor to configure the lstm operation. 113 /// @param [in] name Optional name for the layer. 114 LstmLayer(const LstmDescriptor& param, const char* name); 115 116 /// Default destructor 117 ~LstmLayer() = default; 118 119 /// Retrieve the handles to the constant values stored by the layer. 120 /// @return A vector of the constant tensors stored by this layer. 121 Layer::ConstantTensors GetConstantTensorsByRef() override; 122 }; 123 124 } // namespace 125