LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
convert_from_torch.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_LIBS_CONVERT_FROM_TORCH_H
7#define DEEPLIMA_LIBS_CONVERT_FROM_TORCH_H
8
9#include <torch/torch.h>
10
11#include "static_graph/dict.h"
15
16namespace deeplima
17{
18namespace convert_from_torch
19{
20
21template<class V, class S>
22void copy_vector(const torch::Tensor& src, V& dst)
23{
24 assert(src.size(0) >= 0);
25 dst.resize(src.size(0));
26 for (long i = 0; i < src.size(0); i++)
27 {
28 dst(i) = src[i].item<S>();
29 }
30}
31
32template<class M, class S>
33void copy_matrix(const torch::Tensor& src, M& dst)
34{
35 assert(src.size(0) >= 0);
36 assert(src.size(1) >= 0);
37 dst.resize(src.size(0), src.size(1));
38 auto accs = src.accessor<S,2>();
39 for (long i = 0; i < src.size(0); i++)
40 {
41 for (long j = 0; j < src.size(1); j++)
42 {
43 dst(i, j) = accs[i][j];
44 }
45 }
46}
47
48template <class M, class V, class S=float>
50{
51 const std::vector<torch::Tensor> v = src->all_weights();
52
53 if (v.size() >= 4)
54 {
55 copy_matrix<M, S>(v[0], dst.fw.weight_ih);
56 copy_matrix<M, S>(v[1], dst.fw.weight_hh);
57 copy_vector<V, S>(v[2], dst.fw.bias_ih);
58 copy_vector<V, S>(v[3], dst.fw.bias_hh);
59 }
60 else
61 {
62 throw std::runtime_error("Unexpected set of weights in LSTM. Must be 4 (fw only) or 8 (fw + bw).");
63 }
64
65 if (8 == v.size())
66 {
67 copy_matrix<M, S>(v[4], dst.bw.weight_ih);
68 copy_matrix<M, S>(v[5], dst.bw.weight_hh);
69 copy_vector<V, S>(v[6], dst.bw.bias_ih);
70 copy_vector<V, S>(v[7], dst.bw.bias_hh);
71 }
72}
73
74template <class M, class V, class S=float>
75void convert_module_from_torch(const torch::nn::Linear& src, eigen_impl::params_linear_t<M, V>& dst)
76{
77 copy_matrix<M, S>(src->weight, dst.weight);
78 copy_vector<V, S>(src->bias, dst.bias);
79}
80
81template <class M, class V, class S=float>
82void convert_module_from_torch(const deeplima::nets::torch_modules::DeepBiaffineAttentionDecoder& src,
84{
85 copy_matrix<M, S>(src->U1, dst.m_U1);
86 copy_vector<V, S>(src->u2, dst.m_u2);
87
88 copy_matrix<M, S>(src->mlp_head->weight, dst.m_weight_head);
89 copy_vector<V, S>(src->mlp_head->bias, dst.m_bias_head);
90
91 copy_matrix<M, S>(src->mlp_dep->weight, dst.m_weight_dep);
92 copy_vector<V, S>(src->mlp_dep->bias, dst.m_bias_dep);
93}
94
95template <class M, class V, class S=float>
96void convert_module_from_torch(const deeplima::nets::torch_modules::DeepBiaffineAttentionLabelDecoder& src,
98{
99 copy_matrix<M, S>(src->mlp_head->weight, dst.m_weight_head);
100 copy_vector<V, S>(src->mlp_head->bias, dst.m_bias_head);
101
102 copy_matrix<M, S>(src->mlp_dep->weight, dst.m_weight_dep);
103 copy_vector<V, S>(src->mlp_dep->bias, dst.m_bias_dep);
104
105 dst.m_input_includes_root = src->input_includes_root();
106
107 const int64_t num_labels = src->U.size(0);
108 dst.m_U.resize(num_labels);
109 for (int64_t l = 0; l < num_labels; ++l)
110 {
111 copy_matrix<M, S>(src->U[l], dst.m_U[l]);
112 }
113 dst.build_stacked_U();
114 copy_vector<V, S>(src->root.reshape({-1}), dst.m_root);
115}
116
117template <class M, class S=float>
118void convert_module_from_torch(const torch::nn::Embedding& src, std::shared_ptr<DictBase> sp_dict, EmbdDict<uint64_t, M>& dst)
119{
120 std::shared_ptr<UInt64Dict> sp_uint64_dict = std::dynamic_pointer_cast<UInt64Dict, DictBase>(sp_dict);
121 std::shared_ptr<Char32Dict> sp_char32_dict = std::dynamic_pointer_cast<Char32Dict, DictBase>(sp_dict);
122 assert(sp_uint64_dict || sp_char32_dict);
123 M tmp;
124 copy_matrix<M, S>(src->weight, tmp);
125 if (sp_uint64_dict)
126 {
127 dst.init(sp_uint64_dict, tmp);
128 }
129 else if (sp_char32_dict)
130 {
131 dst.init(sp_char32_dict, tmp);
132 }
133 else
134 {
135 throw std::runtime_error("Unknown dict type.");
136 }
137}
138
139template <class M, class S=float>
140void convert_module_from_torch(const torch::nn::Embedding& src, std::shared_ptr<DictBase> sp_dict, EmbdDict<std::string, M>& dst)
141{
142 std::shared_ptr<StringDict> sp_str_dict = std::dynamic_pointer_cast<StringDict, DictBase>(sp_dict);
143 assert(sp_str_dict);
144 M tmp;
145 copy_matrix<M, S>(src->weight, tmp);
146 dst.init(sp_str_dict, tmp);
147}
148
149} // namespace convert_from_torch
150} // namespace deeplima
151
152#endif
void init(std::shared_ptr< Dict< T > > dict, const M &tensor, bool transpose=true)
Definition embd_dict.h:34
void copy_matrix(const torch::Tensor &src, M &dst)
void convert_module_from_torch(const torch::nn::LSTM &src, eigen_impl::params_bilstm_t< M, V > &dst)
void copy_vector(const torch::Tensor &src, V &dst)
params_lstm_t< M, V > fw
Definition bilstm.h:51
params_lstm_t< M, V > bw
Definition bilstm.h:52