LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
birnn_and_deep_biaffine_attention_script_generator.cpp
Go to the documentation of this file.
1// Copyright 2022 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#include <sstream>
7#include <cassert>
8
10
11using namespace std;
12using namespace deeplima::nets;
13
14namespace deeplima
15{
16namespace graph_dp
17{
18namespace train
19{
20
21string BiRnnAndDeepBiaffineAttentionImpl::generate_script(const vector<embd_descr_t>& embd_descr,
22 const vector<rnn_descr_t>& rnn_descr,
23 const vector<deep_biaffine_attention_descr_t>& decoder_descr,
24 const vector<std::string>& /*output_names*/,
25 bool input_includes_root,
26 int64_t num_labels,
27 float input_dropout/*,
28 const vector<uint32_t>& classes*/)
29{
30 stringstream ss;
31
32 // Dropout modules have no weights: they change training only, and the
33 // inference engine ignores them
34 ss << "input_dropout = def Dropout prob=" << input_dropout << std::endl;
35 for (size_t i = 0; i + 1 < rnn_descr.size(); i++)
36 {
37 if (rnn_descr[i].m_dropout > 0)
38 {
39 ss << "rnn_dropout_" << i << " = def Dropout prob=" << rnn_descr[i].m_dropout << std::endl;
40 }
41 }
42 assert(decoder_descr.size() == 1);
43 if (decoder_descr[0].m_dropout > 0)
44 {
45 ss << "decoder_dropout = def Dropout prob=" << decoder_descr[0].m_dropout << std::endl;
46 }
47
48 size_t rnn_input_size = 0;
49 for (size_t i = 0; i < embd_descr.size(); i++)
50 {
51 if (1 == embd_descr[i].m_type)
52 {
53 ss << "embd_" << embd_descr[i].m_name << " = def Embedding dict=" << i
54 << " dim=" << embd_descr[i].m_dim << std::endl;
55 }
56 rnn_input_size += embd_descr[i].m_dim;
57 }
58
59 ss << std::endl;
60
61 size_t input_size = rnn_input_size;
62 for (size_t i = 0; i < rnn_descr.size(); i++)
63 {
64 ss << "lstm_" << i << " = def LSTM input_size=" << input_size
65 << " hidden_size=" << rnn_descr[i].m_dim << " num_layers=1 bidirectional=true" << std::endl;
66 input_size = rnn_descr[i].m_dim * 2;
67 }
68
69 ss << std::endl;
70
71 ss << "decoder_0 = def DeepBiaffineAttentionDecoder input_dim=" << input_size
72 << " hidden_arc_dim=" << decoder_descr[0].m_arc_dim
73 << " input_includes_root=" << (input_includes_root ? "true" : "false")
74 << endl;
75
76 if (num_labels > 0)
77 {
78 ss << "label_decoder_0 = def DeepBiaffineAttentionLabelDecoder input_dim=" << input_size
79 << " hidden_dim=" << decoder_descr[0].m_rel_dim
80 << " num_labels=" << num_labels
81 << " input_includes_root=" << (input_includes_root ? "true" : "false")
82 << endl;
83 }
84
85 ss << std::endl;
86
87 /*for (size_t i = 0; i < output_names.size(); ++i)
88 {
89 ss << "fc_" << output_names[i] << " = def Linear input_size=" << input_size
90 << " output_size=" << classes[i] << std::endl;
91
92 ss << std::endl;
93 }*/
94
95 for (size_t i = 0; i < embd_descr.size(); i++)
96 {
97 ss << embd_descr[i].m_name << " = def Arg" << std::endl;
98 }
99
100 ss << std::endl;
101
102 for (size_t i = 0; i < embd_descr.size(); i++)
103 {
104 if (1 == embd_descr[i].m_type)
105 {
106 ss << "input_" << embd_descr[i].m_name << " = forward module=embd_"
107 << embd_descr[i].m_name
108 << " input=" << embd_descr[i].m_name << std::endl;
109 }
110 }
111
112 ss << std::endl;
113
114 ss << "input_brut = cat input=";
115 for (size_t i = 0; i < embd_descr.size(); i++)
116 {
117 if (0 == embd_descr[i].m_type)
118 {
119 ss << embd_descr[i].m_name;
120 }
121 else if (1 == embd_descr[i].m_type)
122 {
123 ss << "input_" << embd_descr[i].m_name;
124 }
125 if (i < embd_descr.size() - 1)
126 {
127 ss << ",";
128 }
129 }
130 ss << " dim=2" << std::endl;
131
132 ss << std::endl;
133
134 ss << "input = forward module=input_dropout input=input_brut" << std::endl;
135
136 ss << std::endl;
137
138 std::string last_output_name = "input";
139 for (size_t i = 0; i < rnn_descr.size(); i++)
140 {
141 ss << "rnn_out_" << i << " = forward module=lstm_" << i
142 << " input=" << last_output_name << std::endl;
143 std::stringstream t;
144 t << "rnn_out_" << i;
145 last_output_name = t.str();
146 if (i + 1 < rnn_descr.size() && rnn_descr[i].m_dropout > 0)
147 {
148 ss << "rnn_out_" << i << "_dropped = forward module=rnn_dropout_" << i
149 << " input=" << last_output_name << std::endl;
150 last_output_name += "_dropped";
151 }
152 }
153
154 ss << std::endl;
155
156 if (decoder_descr[0].m_dropout > 0)
157 {
158 ss << "decoder_input = forward module=decoder_dropout input=" << last_output_name << endl;
159 last_output_name = "decoder_input";
160 }
161
162 ss << "arc_raw = forward module=decoder_0 input=" << last_output_name << endl;
163 ss << "arc = log_softmax input=arc_raw dim=2" << endl;
164
165 if (num_labels > 0)
166 {
167 // Per (dependent, head) label logits: [batch, dep, head, num_labels].
168 // The caller selects each dependent's label scores at its head (gold while
169 // training, predicted at inference) and applies log_softmax over the labels.
170 ss << "rel_logits = forward module=label_decoder_0 input=" << last_output_name << endl;
171 }
172
173 /*for (size_t i = 0; i < output_names.size(); ++i)
174 {
175 ss << output_names[i] << "_raw = forward module=fc_" << output_names[i]
176 << " input=" << last_output_name << std::endl;
177
178 ss << output_names[i] << " = log_softmax input=" << output_names[i] << "_raw" << std::endl;
179 ss << endl;
180 }*/
181
182 return ss.str();
183}
184
185} // namespace train
186} // namespace graph_dp
187} // namespace deeplima
188
static std::string generate_script(const std::vector< nets::embd_descr_t > &embd_descr, const std::vector< nets::rnn_descr_t > &rnn_descr, const std::vector< nets::deep_biaffine_attention_descr_t > &decoder_descr, const std::vector< std::string > &output_names, bool input_includes_root=false, int64_t num_labels=0, float input_dropout=0.3)
std::vector< torch::nn::Dropout > m_dropout
Edit rules for lemmatizing words absent from the lemma dictionary.
STL namespace.