Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ParseLSTM.cxx
Go to the documentation of this file.
3#include "onnx_proto3.pb.h"
4
5namespace TMVA {
6namespace Experimental {
7namespace SOFIE {
8
9ParserFuncSignature ParseLSTM = [](RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto) {
10 ETensorType input_type;
11
12 auto input_name = nodeproto.input(0);
13 if (parser.IsRegisteredTensorType(input_name)) {
14 input_type = parser.GetTensorType(input_name);
15 } else {
16 throw std::runtime_error("TMVA::SOFIE ONNX Parser LSTM op has input tensor " + input_name +
17 " but its type is not yet registered");
18 }
19
20 std::unique_ptr<ROperator> op;
21
22 std::vector<float> attr_activation_alpha;
23 std::vector<float> attr_activation_beta;
24 std::vector<std::string> attr_activations;
25 float attr_clip = 0.;
26 std::string attr_direction = "forward";
27 size_t attr_hidden_size = 0;
28 size_t attr_input_forget = 0;
29 size_t attr_layout = 0;
30
31 for (int_t i = 0; i < nodeproto.attribute_size(); i++) {
32 std::string attribute_name = nodeproto.attribute(i).name();
33 if (attribute_name == "activation_alpha") {
34 attr_activation_alpha = {nodeproto.attribute(i).floats().begin(), nodeproto.attribute(i).floats().end()};
35 } else if (attribute_name == "activation_beta") {
36 attr_activation_beta = {nodeproto.attribute(i).floats().begin(), nodeproto.attribute(i).floats().end()};
37 } else if (attribute_name == "activations") {
38 attr_activations = {nodeproto.attribute(i).strings().begin(), nodeproto.attribute(i).strings().end()};
39 } else if (attribute_name == "clip") {
40 attr_clip = nodeproto.attribute(i).f();
41 } else if (attribute_name == "direction") {
42 attr_direction = nodeproto.attribute(i).s();
43 } else if (attribute_name == "hidden_size") {
44 attr_hidden_size = nodeproto.attribute(i).i();
45 } else if (attribute_name == "input_forget") {
46 attr_input_forget = nodeproto.attribute(i).i();
47 } else if (attribute_name == "layout") {
48 attr_layout = nodeproto.attribute(i).i();
49 } else {
50 std::cout << "TMVA SOFIE Warning - Model Loading - Attribute " << attribute_name << " in OperatorNode "
51 << nodeproto.name() << " is not defined in ONNX IR and not applied!\n";
52 }
53 }
54
55 // Optional inputs and outputs
56 std::string name_b;
57 std::string name_sequence_lens;
58 std::string name_initial_h;
59 std::string name_initial_c;
60 std::string name_p;
61 std::string name_y;
62 std::string name_y_h;
63 std::string name_y_c;
64 if (nodeproto.input_size() > 3) {
65 name_b = nodeproto.input(3);
66 }
67 if (nodeproto.input_size() > 4) {
68 name_sequence_lens = nodeproto.input(4);
69 }
70 if (nodeproto.input_size() > 5) {
71 name_initial_h = nodeproto.input(5);
72 }
73 if (nodeproto.input_size() > 6) {
74 name_initial_c = nodeproto.input(6);
75 }
76 if (nodeproto.input_size() > 7) {
77 name_p = nodeproto.input(7);
78 }
79 if (nodeproto.output_size() > 0) {
80 name_y = nodeproto.output(0);
81 }
82 if (nodeproto.output_size() > 1) {
83 name_y_h = nodeproto.output(1);
84 }
85 if (nodeproto.output_size() > 2) {
86 name_y_c = nodeproto.output(2);
87 }
88
89 switch (input_type) {
91 op.reset(new ROperator_LSTM<float>(
92 attr_activation_alpha, attr_activation_beta, attr_activations, attr_clip, attr_direction, attr_hidden_size,
93 attr_input_forget, attr_layout, nodeproto.input(0), nodeproto.input(1), nodeproto.input(2), name_b,
94 name_sequence_lens, name_initial_h, name_initial_c, name_p, name_y, name_y_h, name_y_c));
95 break;
96 default:
97 throw std::runtime_error("TMVA::SOFIE - Unsupported - Operator LSTM does not yet support input type " +
98 std::to_string(static_cast<int>(input_type)));
99 }
100
101 for (auto &name : {name_y, name_y_h, name_y_c}) {
102 if (!parser.IsRegisteredTensorType(name)) {
103 parser.RegisterTensorType(name, input_type);
104 }
105 }
106 return op;
107};
108
109} // namespace SOFIE
110} // namespace Experimental
111} // namespace TMVA
char name[80]
Definition TGX11.cxx:110
void RegisterTensorType(const std::string &, ETensorType)
ETensorType GetTensorType(const std::string &name)
Long Short-Term Memory operator.
std::function< std::unique_ptr< ROperator >(RModelParser_ONNX &, const onnx::NodeProto &)> ParserFuncSignature
ParserFuncSignature ParseLSTM
Definition ParseLSTM.cxx:9
create variable transformations