LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
lemmatization_eigen_inference_impl.h
Go to the documentation of this file.
1// Copyright 2002-2021 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#ifndef DEEPLIMA_LEMMATIZATION_EIGEN_INFERENCE_IMPL_H
7#define DEEPLIMA_LEMMATIZATION_EIGEN_INFERENCE_IMPL_H
8
9#include <eigen3/Eigen/Dense>
10
11#include "bilstm.h"
12#include "bilstm_and_dense.h"
13#include "lstm_beam_decoder.h"
14
15#include "embd_dict.h"
16
19
20//#define LEMM_INFERENCE_PROFILE
21
22#ifdef LEMM_INFERENCE_PROFILE
23#include <chrono>
24#endif
25
26namespace deeplima
27{
29{
30namespace eigen_impl
31{
32
33#ifdef WIN32
34#ifdef LEMM_EXPORTING
35 #define LEMM_EXPORT __declspec(dllexport)
36#elif defined(DEEPLIMA_CONVERTFROMTORCH_SHARED) // users of the DLL
37 #define LEMM_EXPORT __declspec(dllimport)
38#else // static linking
39 #define LEMM_EXPORT
40#endif
41#else
42 #define LEMM_EXPORT
43#endif
44
45
48{
49public:
51
53
54 virtual void load(const std::string& fn) override
55 {
56 try
57 {
59 }
60 catch (const std::runtime_error& e)
61 {
62 throw;
63 }
64 }
65
66 virtual size_t init_new_worker(size_t input_len, bool precomputed_input=false) override
67 {
68 assert(Parent::m_wb.size() > 0);
69 assert(Parent::m_ops.size() > 0);
70 assert(Parent::m_ops.size() == Parent::m_params.size());
71
72 size_t new_worker_idx = Parent::m_wb[0].size();
73 for (size_t i = 0; i < Parent::m_ops.size(); i++)
74 {
75 assert(Parent::m_wb[i].size() == new_worker_idx);
76 assert(nullptr != Parent::m_params[i]);
77 Parent::m_wb[i].push_back(Parent::m_ops[i]->create_workbench(input_len, Parent::m_params[i], precomputed_input));
78 }
79
80 m_workbenches.push_back(std::make_shared<workbench_t>());
81
82 return new_worker_idx;
83 }
84
85 virtual size_t get_precomputed_dim() const
86 {
87 auto p_params = std::dynamic_pointer_cast<typename deeplima::eigen_impl::Op_BiLSTM<Eigen::MatrixXf, Eigen::VectorXf, float>::params_t>(Parent::m_params[0]);
88
89 assert(p_params->layers.size() > 0);
90 const auto& layer = p_params->layers[0];
91 size_t hidden_size = layer.fw.weight_ih.rows() + layer.bw.weight_ih.rows();
92 return hidden_size;
93 }
94
95 virtual void precompute_inputs(
96 const Eigen::MatrixXf& inputs,
97 Eigen::MatrixXf& outputs,
98 int64_t input_size
99 ) override
100 {
101 auto p_op = std::dynamic_pointer_cast<deeplima::eigen_impl::Op_BiLSTM<Eigen::MatrixXf, Eigen::VectorXf, float>>(Parent::m_ops[0]);
102
103 p_op->precompute_inputs(Parent::m_params[0], inputs, outputs, input_size);
104 }
105
106 virtual void predict(
107 size_t /*worker_id*/,
108 const Eigen::MatrixXf& /*inputs*/,
109 int64_t /*input_begin*/,
110 int64_t /*input_end*/,
111 int64_t /*output_begin*/,
112 int64_t /*output_end*/,
113 std::shared_ptr< StdMatrix<uint8_t> >& /*output*/,
114 const std::vector<std::string>& /*outputs_names*/
115 ) override
116 {
117 assert(false);
118 }
119
121 {
124 Eigen::VectorXf fw_h, fw_c, bw_h, bw_c; // encoder's output
125 Eigen::VectorXf encoder_state; // too bad: this is a copy of previous things
127 };
128
129 virtual void predict(
130 size_t worker_id,
131 const Eigen::MatrixXf& inputs,
132 const Eigen::MatrixXf& input_feats,
133 int64_t input_len,
134 int64_t output_max_len,
135 int64_t /*beam_size*/,
136 std::vector< uint32_t >& output,
137 const std::vector<std::string>& /*output_names*/
138 )
139 {
140 assert(Parent::m_wb.size() > 0);
141 assert(worker_id < Parent::m_wb[0].size());
142 assert(m_workbenches.size() > worker_id);
143 workbench_t& wb = *(m_workbenches[worker_id]);
144
145#ifdef LEMM_INFERENCE_PROFILE
146 using clock = std::chrono::system_clock;
147 using ms = std::chrono::duration<double, std::milli>;
148#endif
149
150 // Features encoders
151 // for encoder
152 auto p_linear_feats_enc = std::dynamic_pointer_cast<deeplima::eigen_impl::Op_Linear<Eigen::MatrixXf, Eigen::VectorXf, float>>(Parent::m_ops[5]);
153
154#ifdef LEMM_INFERENCE_PROFILE
155 const auto before = clock::now();
156#endif
157
158 p_linear_feats_enc->execute(Parent::m_wb[5][worker_id],
159 input_feats,
162
163#ifdef LEMM_INFERENCE_PROFILE
164 const auto exec1 = clock::now();
165#endif
166
167 // for decoder
168 auto p_linear_feats_dec = std::dynamic_pointer_cast<deeplima::eigen_impl::Op_Linear<Eigen::MatrixXf, Eigen::VectorXf, float>>(Parent::m_ops[3]);
169
170 p_linear_feats_dec->execute(Parent::m_wb[3][worker_id],
171 input_feats,
174#ifdef LEMM_INFERENCE_PROFILE
175 const auto exec2 = clock::now();
176#endif
177 auto p_encoder = std::dynamic_pointer_cast<deeplima::eigen_impl::Op_BiLSTM<Eigen::MatrixXf, Eigen::VectorXf, float>>(Parent::m_ops[0]);
178
179 auto p_decoder = std::dynamic_pointer_cast<deeplima::eigen_impl::Op_LSTM_Beam_Decoder<Eigen::MatrixXf, Eigen::VectorXf, float>>(Parent::m_ops[4]);
180
181 auto enc_mutlilayer_bilstm
182 = std::dynamic_pointer_cast<const deeplima::eigen_impl::params_multilayer_bilstm_t<Eigen::MatrixXf, Eigen::VectorXf>>(Parent::m_params[0]);
183 size_t hidden_size = enc_mutlilayer_bilstm->layers[0].fw.weight_ih.rows() / 4;
184
185 if (true)
186 {
187 // layout of encoded_feats_for_encoder:
188 //
189 // encoder_init_state_ = forward module=fc_cat2encoder input=categories_enc_embd
190 // encoder_init_state = reshape input=encoder_init_state_ dims=2,-1,(encoder_input_size / 2)
191 wb.fw_h = wb.encoded_feats_for_encoder.head(hidden_size);
192 wb.fw_c = wb.fw_h;
193 wb.bw_h = wb.encoded_feats_for_encoder.tail(hidden_size);
194 wb.bw_c = wb.bw_h;
195 }
196
197#ifdef LEMM_INFERENCE_PROFILE
198 std::cerr << "inputs.cols()=" << inputs.cols() << std::endl;
199 std::cerr << "inputs.rows()=" << inputs.rows() << std::endl;
200
201 const auto exec3 = clock::now();
202#endif
203
204 p_encoder->execute(Parent::m_wb[0][worker_id],
205 inputs, Parent::m_params[0],
206 0, input_len, wb.fw_h, wb.fw_c, wb.bw_h, wb.bw_c);
207
208#ifdef LEMM_INFERENCE_PROFILE
209 const auto exec4 = clock::now();
210#endif
211
212 if (wb.encoder_state.rows() == 0)
213 {
214 wb.encoder_state = Eigen::VectorXf(hidden_size * 4 + wb.encoded_feats_for_decoder.rows());
215 }
216 wb.encoder_state << wb.fw_h, wb.fw_c, wb.bw_h, wb.bw_c, wb.encoded_feats_for_decoder;
217
218 auto p_linear_h = std::dynamic_pointer_cast<deeplima::eigen_impl::Op_Linear<Eigen::MatrixXf, Eigen::VectorXf, float>>(Parent::m_ops[1]);
219 auto p_linear_c = std::dynamic_pointer_cast<deeplima::eigen_impl::Op_Linear<Eigen::MatrixXf, Eigen::VectorXf, float>>(Parent::m_ops[2]);
220
221#ifdef LEMM_INFERENCE_PROFILE
222 const auto exec5 = clock::now();
223#endif
224
225 p_linear_h->execute(Parent::m_wb[1][worker_id], wb.encoder_state, Parent::m_params[1], wb.decoder_initial_h);
226
227#ifdef LEMM_INFERENCE_PROFILE
228 const auto exec6 = clock::now();
229#endif
230
231 p_linear_c->execute(Parent::m_wb[2][worker_id], wb.encoder_state, Parent::m_params[2], wb.decoder_initial_c);
232
233#ifdef LEMM_INFERENCE_PROFILE
234 const auto exec7 = clock::now();
235#endif
236
237 const EmbdUInt64Float& decoder_embd = Parent::m_input_uint_dicts[1];
238 p_decoder->execute(Parent::m_wb[4][worker_id], decoder_embd,
240 Parent::m_params[4], 0x10FFFE, 0x10FFFF, 9, output, output_max_len);
241
242#ifdef LEMM_INFERENCE_PROFILE
243 const auto exec8 = clock::now();
244
245 std::cerr << "feats for enc : " << (exec1 - before).count() << std::endl;
246 std::cerr << "feats for dec : " << (exec2 - exec1).count() << std::endl;
247
248 std::cerr << "copy : " << (exec3 - exec2).count() << std::endl;
249 std::cerr << "encoder : " << (exec4 - exec3).count() << std::endl;
250 std::cerr << "copy : " << (exec5 - exec4).count() << std::endl;
251
252 std::cerr << "prep h : " << (exec6 - exec5).count() << std::endl;
253 std::cerr << "prep c : " << (exec7 - exec6).count() << std::endl;
254 std::cerr << "decoder : " << (exec8 - exec7).count() << std::endl;
255 throw;
256#endif
257 }
258
259 const std::vector<std::vector<std::string>>& get_classes() const
260 {
261 return m_classes;
262 }
263
264 const std::vector<std::string>& get_class_names() const
265 {
266 return m_class_names;
267 }
268
270 {
271 return m_morph_model;
272 }
273
274 inline const std::string& get_embd_fn(size_t idx) const
275 {
276 return m_embd_fn[idx];
277 }
278
279 const std::vector<size_t>& get_fixed_upos() const
280 {
281 return m_fixed_upos;
282 }
283
284protected:
285 std::vector<std::string> m_class_names;
286 std::vector<std::vector<std::string>> m_classes;
287 std::vector<std::string> m_embd_fn;
289 std::vector<size_t> m_fixed_upos;
290
291 std::vector<std::shared_ptr<workbench_t>> m_workbenches;
292
293 virtual void convert_from_torch(const std::string& fn) override;
294};
295
296
297} // namespace eigen_impl
298} // namespace lemmatization
299} // namespace deeplima
300
301#endif // DEEPLIMA_LEMMATIZATION_EIGEN_INFERENCE_IMPL_H
302
std::vector< std::vector< std::shared_ptr< Op_Base::workbench_t > > > m_wb
std::vector< std::shared_ptr< Op_Base > > m_ops
std::vector< std::shared_ptr< param_base_t > > m_params
virtual void predict(size_t, const Eigen::MatrixXf &, int64_t, int64_t, int64_t, int64_t, std::shared_ptr< StdMatrix< uint8_t > > &, const std::vector< std::string > &) override
virtual size_t init_new_worker(size_t input_len, bool precomputed_input=false) override
virtual void predict(size_t worker_id, const Eigen::MatrixXf &inputs, const Eigen::MatrixXf &input_feats, int64_t input_len, int64_t output_max_len, int64_t, std::vector< uint32_t > &output, const std::vector< std::string > &)
virtual void precompute_inputs(const Eigen::MatrixXf &inputs, Eigen::MatrixXf &outputs, int64_t input_size) override
Helper class for morphology data (upos, features) binarization.
Definition morph_model.h:93
Edit rules for lemmatizing words absent from the lemma dictionary.
EmbdDict< uint64_t, Eigen::MatrixXf, Eigen::Index > EmbdUInt64Float
Definition embd_dict.h:208