Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_RNN.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_RNN
2#define TMVA_SOFIE_ROPERATOR_RNN
3
4#include "TMVA/RModel.hxx"
5#include "TMVA/ROperator.hxx"
7
8#include <memory>
9#include <sstream>
10#include <vector>
11
12namespace TMVA {
13namespace Experimental {
14namespace SOFIE {
15
16/*! \brief Recurrent Neural Network operator
17 *
18 * Inference code generation for one-layer vanilla RNN. Supports forward, reverse and bidirectional RNNs.
19 * See the <a href="https://github.com/onnx/onnx/blob/master/docs/Operators.md#RNN">ONNX documentation</a>
20 * for details about the supported RNN architectures.
21 */
22template <typename T> class ROperator_RNN final : public ROperator {
23 private:
24 std::vector<float> fAttrActivationAlpha; ///< Scaling values used by some activation functions
25 std::vector<float> fAttrActivationBeta; ///< Scaling values used by some activation functions
26 std::vector<std::string> fAttrActivations; ///< Activation functions
27 float fAttrClip; ///< Clip threshold
28 std::string fAttrDirection; ///< Direction of processing
29 size_t fAttrHiddenSize; ///< Number of the hidden layers
30 size_t fAttrLayout; ///< Data layout
31
32 std::string fNX; ///< Name of the input
33 std::string fNW; ///< Name of the weights
34 std::string fNR; ///< Name of the recurrence
35 std::string fNB; ///< Name of the bias
36 std::string fNSequence_lens; ///< Name of the length of the sequences
37 std::string fNInitial_h; ///< Name of the initial value of the hidden states
38 std::string fNY; ///< Name of the output
39 std::string fNY_h; ///< Name of the last sequence of the output
40
41 std::vector<size_t> fShapeX; ///< Shape of the input
42 std::vector<size_t> fShapeW; ///< Shape of the weights
43 std::vector<size_t> fShapeR; ///< Shape of the recurrence
44 std::vector<size_t> fShapeB; ///< Shape of the bias
45 std::vector<size_t> fShapeSequence_lens; ///< Shape of the length of the sequences
46 std::vector<size_t> fShapeInitial_h; ///< Shape of the initial value of the hidden states
47 std::vector<size_t> fShapeY; ///< Shape of the output
48 std::vector<size_t> fShapeY_h; ///< Shape of the last sequence of the output
49
50 std::string fType; ///< Type of the tensors
51
52 public:
53 /*! Default constructor of ROperator_RNN */
55
56 /*! \brief Constructor of ROperator_RNN from the attributes
57 *
58 * \param activation_alpha scaling values used by some activation functions
59 * \param activation_beta scaling values used by some activation functions
60 * \param activations activation functions
61 * \param clip clip threshold
62 * \param direction direction of processing of the sequneces
63 * \param hidden_size number of hidden layers
64 * \param layout data layout
65 * \param nameX name of the input tensor
66 * \param nameW name of the weight tensor
67 * \param nameR name of the recurrence tensor
68 * \param nameB name of the bias tensor
69 * \param nameSequence_lens name of the length of the sequences
70 * \param nameInitial_h name of the initial value of the hidden states
71 * \param nameY name of the output
72 * \param nameY_h name of the last sequence of the output
73 */
74 ROperator_RNN(std::vector<float> activation_alpha,
75 std::vector<float> activation_beta,
76 std::vector<std::string> activations, float clip,
77 std::string direction, size_t hidden_size, size_t layout,
78 std::string nameX, std::string nameW, std::string nameR,
79 std::string nameB, std::string nameSequence_lens,
80 std::string nameInitial_h, std::string nameY,
81 std::string nameY_h)
82 : fAttrActivationAlpha(activation_alpha),
83 fAttrActivationBeta(activation_beta), fAttrActivations(activations),
84 fAttrClip(clip), fAttrDirection(direction),
85 fAttrHiddenSize(hidden_size), fAttrLayout(layout),
86 fNX(UTILITY::Clean_name(nameX)), fNW(UTILITY::Clean_name(nameW)),
87 fNR(UTILITY::Clean_name(nameR)), fNB(UTILITY::Clean_name(nameB)),
88 fNSequence_lens(UTILITY::Clean_name(nameSequence_lens)),
89 fNInitial_h(UTILITY::Clean_name(nameInitial_h)),
90 fNY(UTILITY::Clean_name(nameY)), fNY_h(UTILITY::Clean_name(nameY_h)) {
91 if (std::is_same<T, float>::value) {
92 fType = "float";
93 } else {
94 throw std::runtime_error(
95 "TMVA SOFIE Encountered unsupported type parsing a RNN operator");
96 }
97 }
98
99 /*! \brief Infers the type of the output tensors
100 *
101 * \param input type of the input tensors
102 */
103 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input);
104
105 /*! \brief Infers the shape of the output tensors
106 *
107 * \param input shape of the input tensors
108 */
109 std::vector<std::vector<size_t>>
110 ShapeInference(std::vector<std::vector<size_t>> input);
111
112 /*! \brief Initialize the model
113 *
114 * \param model Model
115 */
116 void Initialize(RModel &model);
117
118 /*! \brief Generates the inference code
119 *
120 * \param OpName name of the operator
121 */
122 std::string Generate(std::string OpName);
123
124 // generate code for Session data members (e.g. internal vectors)
125 std::string GenerateSessionMembersCode(std::string opName);
126
127 /*! \brief Returns the blas routines needed to compile the generated code
128 */
129 std::vector<std::string> GetBlasRoutines() { return { std::string("Gemm"), std::string("Axpy") }; }
130};
131
132} // namespace SOFIE
133} // namespace Experimental
134} // namespace TMVA
135
136// Implementation of the ROperator_RNN class
137#include "TMVA/ROperator_RNN.icc"
138
139#endif
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Recurrent Neural Network operator.
std::vector< size_t > fShapeB
Shape of the bias.
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
Infers the type of the output tensors.
std::vector< float > fAttrActivationBeta
Scaling values used by some activation functions.
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input)
Infers the shape of the output tensors.
std::vector< std::string > GetBlasRoutines()
Returns the blas routines needed to compile the generated code.
size_t fAttrHiddenSize
Number of the hidden layers.
std::string fNInitial_h
Name of the initial value of the hidden states.
ROperator_RNN(std::vector< float > activation_alpha, std::vector< float > activation_beta, std::vector< std::string > activations, float clip, std::string direction, size_t hidden_size, size_t layout, std::string nameX, std::string nameW, std::string nameR, std::string nameB, std::string nameSequence_lens, std::string nameInitial_h, std::string nameY, std::string nameY_h)
Constructor of ROperator_RNN from the attributes.
std::vector< size_t > fShapeR
Shape of the recurrence.
std::string fNW
Name of the weights.
std::vector< size_t > fShapeY
Shape of the output.
std::string fType
Type of the tensors.
std::string fNY
Name of the output.
std::string fNSequence_lens
Name of the length of the sequences.
std::vector< size_t > fShapeSequence_lens
Shape of the length of the sequences.
std::string Generate(std::string OpName)
Generates the inference code.
std::string fNR
Name of the recurrence.
std::vector< float > fAttrActivationAlpha
Scaling values used by some activation functions.
ROperator_RNN()
Default constructor of ROperator_RNN.
std::vector< size_t > fShapeX
Shape of the input.
std::string fAttrDirection
Direction of processing.
std::string fNX
Name of the input.
void Initialize(RModel &model)
Initialize the model.
std::string GenerateSessionMembersCode(std::string opName)
std::string fNY_h
Name of the last sequence of the output.
std::vector< size_t > fShapeInitial_h
Shape of the initial value of the hidden states.
std::vector< size_t > fShapeW
Shape of the weights.
std::vector< std::string > fAttrActivations
Activation functions.
std::vector< size_t > fShapeY_h
Shape of the last sequence of the output.
create variable transformations