LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
graph_dp_eigen_inference_impl.h
Go to the documentation of this file.
1// Copyright 2021 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#ifndef DEEPLIMA_GRAPH_DP_EIGEN_INFERENCE_IMPL_H
7#define DEEPLIMA_GRAPH_DP_EIGEN_INFERENCE_IMPL_H
8
9#include <eigen3/Eigen/Dense>
10
12#include "bilstm.h"
15// #include "deeplima/graph_dp/impl/arborescence.h"
16
17namespace deeplima
18{
19namespace graph_dp
20{
21namespace eigen_impl
22{
23
24#ifdef WIN32
25#ifdef DEP_PARSING_EXPORTING
26 #define DEP_PARSING_EXPORT __declspec(dllexport)
27#elif defined(DEEPLIMA_CONVERTFROMTORCH_SHARED) // users of the DLL
28 #define DEP_PARSING_EXPORT __declspec(dllimport)
29#else // static linking
30 #define DEP_PARSING_EXPORT
31#endif
32#else
33 #define DEP_PARSING_EXPORT
34#endif
35
38{
39public:
40 typedef Eigen::MatrixXf Matrix;
41 typedef Eigen::VectorXf Vector;
42 typedef float Scalar;
43 typedef Eigen::MatrixXf tensor_t;
46
48
49 virtual void load(const std::string& fn)
50 {
52 }
53
54 virtual size_t get_precomputed_dim() const
55 {
56 auto p_params = std::dynamic_pointer_cast<typename deeplima::eigen_impl::Op_BiLSTM<Eigen::MatrixXf, Eigen::VectorXf, float>::params_t>(Parent::m_params[0]);
57 const auto& layer = p_params->layers[0];
58 auto hidden_size = layer.fw.weight_ih.rows() + layer.bw.weight_ih.rows();
59 return hidden_size;
60 }
61
62 virtual void precompute_inputs(
63 const Eigen::MatrixXf& inputs,
64 Eigen::MatrixXf& outputs,
65 int64_t input_size
66 )
67 {
68 auto p_op = std::dynamic_pointer_cast<deeplima::eigen_impl::Op_BiLSTM<Eigen::MatrixXf, Eigen::VectorXf, float>>(Parent::m_ops[0]);
69
70 p_op->precompute_inputs(Parent::m_params[0], inputs, outputs, input_size);
71 }
72
73 virtual void predict(
74 size_t /*worker_id*/,
75 const Eigen::MatrixXf& /*inputs*/,
76 int64_t /*input_begin*/,
77 int64_t /*input_end*/,
78 int64_t /*output_begin*/,
79 int64_t /*output_end*/,
80 std::shared_ptr< StdMatrix<uint8_t> >& /*output*/,
81 const std::vector<std::string>& /*outputs_names*/
82 )
83 {
84// TODO to be implemented?
85 assert(false);
86 }
87
88 virtual void predict(
89 size_t worker_id,
90 const Eigen::MatrixXf& inputs,
91 int64_t input_begin,
92 int64_t /*input_end*/,
93 int64_t /*output_begin*/,
94 int64_t /*output_end*/,
95 std::shared_ptr< StdMatrix<uint32_t> >& output,
96 const std::vector<size_t>& lengths,
97 const std::vector<std::string>& /*outputs_names*/
98 )
99 {
100 // std::cerr << "BiRnnAndDeepBiaffineAttentionEigenInference<Eigen::MatrixXf, Eigen::VectorXf, float>::predict "
101 // << "input_begin=" << input_begin
102 // << ", output dim=" << output->dim() << ", lengths=" << lengths << std::endl;
103 auto p_encoder = std::dynamic_pointer_cast<deeplima::eigen_impl::Op_BiLSTM<Eigen::MatrixXf, Eigen::VectorXf, float>>(Parent::m_ops[0]);
104
105 // const typename deeplima::eigen_impl::Op_BiLSTM<Eigen::MatrixXf, Eigen::VectorXf, float>::params_t *plstm
106 // = static_cast<const typename deeplima::eigen_impl::Op_BiLSTM<Eigen::MatrixXf, Eigen::VectorXf, float>::params_t*>(Parent::m_params[0]);
107
108 assert(Parent::m_wb.size() > 0);
109 assert(worker_id < Parent::m_wb[0].size());
110
111 auto wb = std::dynamic_pointer_cast<typename deeplima::eigen_impl::Op_BiLSTM<Eigen::MatrixXf, Eigen::VectorXf, float>::workbench_t>(Parent::m_wb[0][worker_id]);
112
113 auto p_decoder = std::dynamic_pointer_cast<typename deeplima::eigen_impl::Op_DeepBiaffineAttnDecoder<Eigen::MatrixXf, Eigen::VectorXf, float>>(
114 Parent::m_ops[1]);
115
116 const bool predict_labels =
117 !m_deep_biaffine_attn_label_decoder.empty() && output->size() >= 2;
119
120 size_t start = input_begin;
121 for (size_t i = 0; i < lengths.size(); ++i)
122 {
123 p_encoder->execute(Parent::m_wb[0][worker_id],
124 inputs, Parent::m_params[0],
125 start, start + lengths[i]);
126
127 p_decoder->execute(
128 Parent::m_wb[1][worker_id],
129 wb->get_last_output(),
131 start,
132 start + lengths[i],
133 (*output)[0]);
134
135 if (predict_labels)
136 {
137 // Score deprels at the heads just predicted by the arc decoder.
138 const Eigen::MatrixXf& enc = wb->get_last_output();
139 const Eigen::MatrixXf sent_input = enc.block(0, 0, enc.rows(), lengths[i]);
141 sent_input, (*output)[0], start, (*output)[1]);
142 }
143
144 start += lengths[i];
145 }
146 // std::cerr << "BiRnnAndDeepBiaffineAttentionEigenInference<Eigen::MatrixXf, Eigen::VectorXf, float>::predict executes done " << start << std::endl;
147 // arborescence<uint32_t, typename Eigen::MatrixXf::Scalar>((*output)[0], start);
148 // std::cerr << "BiRnnAndDeepBiaffineAttentionEigenInference<Eigen::MatrixXf, Eigen::VectorXf, float>::predict after correcting arborescence: " << (*output)[0] << std::endl;
149 }
150
151 inline const std::string& get_embd_fn(size_t idx) const
152 {
153 return m_embd_fn[idx];
154 }
155
156 // deprel id -> string; empty if the model has no label decoder.
157 const std::vector<std::string>& get_rel_class_names() const
158 {
159 return m_rel_class_names;
160 }
161
162 bool has_label_decoder() const
163 {
165 }
166
167protected:
168 std::vector<std::string> m_embd_fn;
169
170 std::vector<std::shared_ptr<deeplima::eigen_impl::params_deep_biaffine_attn_decoder_t<Eigen::MatrixXf, Eigen::VectorXf>>> m_deep_biaffine_attn_decoder;
171 std::map<std::string, size_t> m_deep_biaffine_attn_decoder_idx;
172
173 std::vector<std::shared_ptr<deeplima::eigen_impl::params_deep_biaffine_attn_label_decoder_t<Eigen::MatrixXf, Eigen::VectorXf>>> m_deep_biaffine_attn_label_decoder;
174 std::vector<std::string> m_rel_class_names;
175
176 virtual void convert_from_torch(const std::string& fn);
177};
178
179
180} // namespace eigen_impl
181} // namespace graph_dp
182} // namespace deeplima
183
184#endif
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
void predict_labels(const params_t &p, const M &input, const std::vector< uint32_t > &heads, size_t input_begin, std::vector< uint32_t > &output) const
virtual void predict(size_t worker_id, const Eigen::MatrixXf &inputs, int64_t input_begin, int64_t, int64_t, int64_t, std::shared_ptr< StdMatrix< uint32_t > > &output, const std::vector< size_t > &lengths, const std::vector< std::string > &)
virtual void precompute_inputs(const Eigen::MatrixXf &inputs, Eigen::MatrixXf &outputs, int64_t input_size)
std::vector< std::shared_ptr< deeplima::eigen_impl::params_deep_biaffine_attn_decoder_t< Eigen::MatrixXf, Eigen::VectorXf > > > m_deep_biaffine_attn_decoder
std::vector< std::shared_ptr< deeplima::eigen_impl::params_deep_biaffine_attn_label_decoder_t< Eigen::MatrixXf, Eigen::VectorXf > > > m_deep_biaffine_attn_label_decoder
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 > &)
#define DEP_PARSING_EXPORT
Edit rules for lemmatizing words absent from the lemma dictionary.
DictsHolderImpl< EmbdUInt64Float > EmbdUInt64FloatHolder
Definition embd_dict.h:210