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#else
28 #define DEP_PARSING_EXPORT __declspec(dllimport)
29#endif
30#else
31 #define DEP_PARSING_EXPORT
32#endif
33
36{
37public:
38 typedef Eigen::MatrixXf Matrix;
39 typedef Eigen::VectorXf Vector;
40 typedef float Scalar;
41 typedef Eigen::MatrixXf tensor_t;
44
46
47 virtual void load(const std::string& fn)
48 {
49 convert_from_torch(fn);
50 }
51
52 virtual size_t get_precomputed_dim() const
53 {
54 auto p_params = std::dynamic_pointer_cast<typename deeplima::eigen_impl::Op_BiLSTM<Eigen::MatrixXf, Eigen::VectorXf, float>::params_t>(Parent::m_params[0]);
55 const auto& layer = p_params->layers[0];
56 auto hidden_size = layer.fw.weight_ih.rows() + layer.bw.weight_ih.rows();
57 return hidden_size;
58 }
59
60 virtual void precompute_inputs(
61 const Eigen::MatrixXf& inputs,
62 Eigen::MatrixXf& outputs,
63 int64_t input_size
64 )
65 {
66 auto p_op = std::dynamic_pointer_cast<deeplima::eigen_impl::Op_BiLSTM<Eigen::MatrixXf, Eigen::VectorXf, float>>(Parent::m_ops[0]);
67
68 p_op->precompute_inputs(Parent::m_params[0], inputs, outputs, input_size);
69 }
70
71 virtual void predict(
72 size_t /*worker_id*/,
73 const Eigen::MatrixXf& /*inputs*/,
74 int64_t /*input_begin*/,
75 int64_t /*input_end*/,
76 int64_t /*output_begin*/,
77 int64_t /*output_end*/,
78 std::shared_ptr< StdMatrix<uint8_t> >& /*output*/,
79 const std::vector<std::string>& /*outputs_names*/
80 )
81 {
82// TODO to be implemented?
83 assert(false);
84 }
85
86 virtual void predict(
87 size_t worker_id,
88 const Eigen::MatrixXf& inputs,
89 int64_t input_begin,
90 int64_t /*input_end*/,
91 int64_t /*output_begin*/,
92 int64_t /*output_end*/,
93 std::shared_ptr< StdMatrix<uint32_t> >& output,
94 const std::vector<size_t>& lengths,
95 const std::vector<std::string>& /*outputs_names*/
96 )
97 {
98 // std::cerr << "BiRnnAndDeepBiaffineAttentionEigenInference<Eigen::MatrixXf, Eigen::VectorXf, float>::predict "
99 // << "input_begin=" << input_begin
100 // << ", output dim=" << output->dim() << ", lengths=" << lengths << std::endl;
101 auto p_encoder = std::dynamic_pointer_cast<deeplima::eigen_impl::Op_BiLSTM<Eigen::MatrixXf, Eigen::VectorXf, float>>(Parent::m_ops[0]);
102
103 // const typename deeplima::eigen_impl::Op_BiLSTM<Eigen::MatrixXf, Eigen::VectorXf, float>::params_t *plstm
104 // = static_cast<const typename deeplima::eigen_impl::Op_BiLSTM<Eigen::MatrixXf, Eigen::VectorXf, float>::params_t*>(Parent::m_params[0]);
105
106 assert(Parent::m_wb.size() > 0);
107 assert(worker_id < Parent::m_wb[0].size());
108
109 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]);
110
111 auto p_decoder = std::dynamic_pointer_cast<typename deeplima::eigen_impl::Op_DeepBiaffineAttnDecoder<Eigen::MatrixXf, Eigen::VectorXf, float>>(
112 Parent::m_ops[1]);
113
114 const bool predict_labels =
115 !m_deep_biaffine_attn_label_decoder.empty() && output->size() >= 2;
117
118 size_t start = input_begin;
119 for (size_t i = 0; i < lengths.size(); ++i)
120 {
121 p_encoder->execute(Parent::m_wb[0][worker_id],
122 inputs, Parent::m_params[0],
123 start, start + lengths[i]);
124
125 p_decoder->execute(
126 Parent::m_wb[1][worker_id],
127 wb->get_last_output(),
128 Parent::m_params[1],
129 start,
130 start + lengths[i],
131 (*output)[0]);
132
133 if (predict_labels)
134 {
135 // Score deprels at the heads just predicted by the arc decoder.
136 const Eigen::MatrixXf& enc = wb->get_last_output();
137 const Eigen::MatrixXf sent_input = enc.block(0, 0, enc.rows(), lengths[i]);
138 label_op.predict_labels(*m_deep_biaffine_attn_label_decoder[0],
139 sent_input, (*output)[0], start, (*output)[1]);
140 }
141
142 start += lengths[i];
143 }
144 // std::cerr << "BiRnnAndDeepBiaffineAttentionEigenInference<Eigen::MatrixXf, Eigen::VectorXf, float>::predict executes done " << start << std::endl;
145 // arborescence<uint32_t, typename Eigen::MatrixXf::Scalar>((*output)[0], start);
146 // std::cerr << "BiRnnAndDeepBiaffineAttentionEigenInference<Eigen::MatrixXf, Eigen::VectorXf, float>::predict after correcting arborescence: " << (*output)[0] << std::endl;
147 }
148
149 inline const std::string& get_embd_fn(size_t idx) const
150 {
151 return m_embd_fn[idx];
152 }
153
154 // deprel id -> string; empty if the model has no label decoder.
155 const std::vector<std::string>& get_rel_class_names() const
156 {
157 return m_rel_class_names;
158 }
159
160 bool has_label_decoder() const
161 {
162 return !m_deep_biaffine_attn_label_decoder.empty();
163 }
164
165protected:
166 std::vector<std::string> m_embd_fn;
167
168 std::vector<std::shared_ptr<deeplima::eigen_impl::params_deep_biaffine_attn_decoder_t<Eigen::MatrixXf, Eigen::VectorXf>>> m_deep_biaffine_attn_decoder;
169 std::map<std::string, size_t> m_deep_biaffine_attn_decoder_idx;
170
171 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;
172 std::vector<std::string> m_rel_class_names;
173
174 virtual void convert_from_torch(const std::string& fn);
175};
176
177
178} // namespace eigen_impl
179} // namespace graph_dp
180} // namespace deeplima
181
182#endif
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