forked from guillaume-be/rust-bert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembeddings.rs
284 lines (256 loc) · 9.48 KB
/
embeddings.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2020 The Trax Authors and The HuggingFace Inc. team.
// Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
// Copyright 2020 Guillaume Becquin
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::common::dropout::Dropout;
use crate::reformer::attention_utils::get_least_common_mult_chunk_len;
use crate::reformer::ReformerConfig;
use crate::RustBertError;
use std::borrow::Borrow;
use tch::nn::Init;
use tch::{nn, Kind, Tensor};
#[derive(Debug)]
/// # Axial position embeddings implementation for Reformer model
pub struct AxialPositionEmbeddings {
weights: Vec<Tensor>,
axial_pos_shape: Vec<i64>,
least_common_mult_chunk_length: i64,
dropout_prob: f64,
}
impl AxialPositionEmbeddings {
pub fn new<'p, P>(p: P, config: &ReformerConfig) -> Result<Self, RustBertError>
where
P: Borrow<nn::Path<'p>>,
{
let p = p.borrow();
let axial_pos_shape = config.axial_pos_shape.clone();
if config.axial_pos_embds_dim.iter().sum::<i64>() != config.hidden_size {
return Err(RustBertError::InvalidConfigurationError(format!(
"The sum of position embedding dimensions ({:?}) does not add up to the hidden size {}",
config.axial_pos_embds_dim,
config.hidden_size
)));
};
let least_common_mult_chunk_length = get_least_common_mult_chunk_len(
&config.attn_layers,
config.lsh_attn_chunk_length,
config.local_attn_chunk_length,
);
let mut weights: Vec<Tensor> = vec![];
let p_weights = p / "weights";
for (axis_index, axial_pos_embd_dim) in config.axial_pos_embds_dim.iter().enumerate() {
let mut axial_shape = vec![1i64; config.axial_pos_shape.len()];
axial_shape[axis_index] = config.axial_pos_shape[axis_index];
axial_shape.push(*axial_pos_embd_dim);
weights.push(p_weights.var(&axis_index.to_string(), &axial_shape, Init::Const(1.0)));
}
Ok(AxialPositionEmbeddings {
weights,
axial_pos_shape,
least_common_mult_chunk_length,
dropout_prob: config.hidden_dropout_prob,
})
}
pub fn forward_t(&self, position_ids: &Tensor, train: bool) -> Tensor {
let input_shape = position_ids.size();
let (batch_size, sequence_length) = (input_shape[0], input_shape[1]);
let broadcasted_weights = self
.weights
.iter()
.map(|tensor| {
let mut new_shape = vec![batch_size];
new_shape.extend(&self.axial_pos_shape);
new_shape.push(*tensor.size().last().unwrap());
tensor.expand(new_shape.as_slice(), true)
})
.collect::<Vec<Tensor>>();
if train {
if self.dropout_prob > 0.0 {
Tensor::cat(&broadcasted_weights, -1)
.transpose(2, 1)
.feature_dropout(self.dropout_prob, train)
.transpose(2, 1)
.reshape(&[batch_size, sequence_length, -1])
} else {
Tensor::cat(
&broadcasted_weights
.iter()
.map(|tensor| tensor.reshape(&[batch_size, sequence_length, -1]))
.collect::<Vec<Tensor>>(),
-1,
)
}
} else {
let max_position_id = i64::from(position_ids.max());
let required_pos_encodings_columns =
(max_position_id + 1) / self.axial_pos_shape[1] + 1;
let position_encodings = Tensor::cat(
&broadcasted_weights
.iter()
.map(|tensor| tensor.slice(1, 0, required_pos_encodings_columns, 1))
.collect::<Vec<Tensor>>(),
-1,
);
let position_encodings = position_encodings.reshape(&[
batch_size,
-1,
*position_encodings.size().last().unwrap(),
]);
let mut output_tensors = vec![];
for i in 0..batch_size {
output_tensors.push(
position_encodings
.get(i)
.index_select(0, &position_ids.get(i))
.unsqueeze(0),
);
}
Tensor::cat(&output_tensors, 0)
}
}
}
#[derive(Debug)]
/// # Position embeddings implementation for Reformer model
pub struct BasePositionEmbeddings {
embeddings: nn::Embedding,
dropout: Dropout,
}
impl BasePositionEmbeddings {
pub fn new<'p, P>(p: P, config: &ReformerConfig) -> BasePositionEmbeddings
where
P: Borrow<nn::Path<'p>>,
{
let p = p.borrow();
let dropout = Dropout::new(config.hidden_dropout_prob);
let embeddings = nn::embedding(
p / "embedding",
config.max_position_embeddings,
config.hidden_size,
Default::default(),
);
BasePositionEmbeddings {
embeddings,
dropout,
}
}
pub fn forward_t(&self, position_ids: &Tensor, train: bool) -> Tensor {
position_ids
.apply(&self.embeddings)
.apply_t(&self.dropout, train)
}
}
#[derive(Debug)]
pub enum PositionEmbedding {
AxialPositionEmbeddings(AxialPositionEmbeddings),
BasePositionEmbeddings(BasePositionEmbeddings),
}
impl PositionEmbedding {
pub fn forward_t(&self, position_ids: &Tensor, train: bool) -> Tensor {
match self {
PositionEmbedding::AxialPositionEmbeddings(ref embeddings) => {
embeddings.forward_t(position_ids, train)
}
PositionEmbedding::BasePositionEmbeddings(ref embeddings) => {
embeddings.forward_t(position_ids, train)
}
}
}
}
#[derive(Debug)]
/// # Embeddings implementation for Reformer model
pub struct ReformerEmbeddings {
position_embeddings: PositionEmbedding,
word_embeddings: nn::Embedding,
dropout: Dropout,
}
impl ReformerEmbeddings {
pub fn new<'p, P>(p: P, config: &ReformerConfig) -> Result<ReformerEmbeddings, RustBertError>
where
P: Borrow<nn::Path<'p>>,
{
let p = p.borrow();
let dropout = Dropout::new(config.hidden_dropout_prob);
let word_embeddings = nn::embedding(
p / "word_embeddings",
config.vocab_size,
config.hidden_size,
Default::default(),
);
let position_embeddings = if config.axial_pos_embds {
PositionEmbedding::AxialPositionEmbeddings(AxialPositionEmbeddings::new(
p / "position_embeddings",
config,
)?)
} else {
PositionEmbedding::BasePositionEmbeddings(BasePositionEmbeddings::new(
p / "position_embeddings",
config,
))
};
Ok(ReformerEmbeddings {
position_embeddings,
word_embeddings,
dropout,
})
}
pub fn forward_t(
&self,
input_ids: Option<&Tensor>,
position_ids: Option<&Tensor>,
input_embeds: Option<Tensor>,
start_ids_pos_encoding: i64,
train: bool,
) -> Result<Tensor, RustBertError> {
let (input_embeddings, input_shape, device) = match input_ids {
Some(input_value) => match input_embeds {
Some(_) => {
return Err(RustBertError::ValueError(
"Only one of input ids or input embeddings may be set".into(),
));
}
None => (
input_value.apply_t(&self.word_embeddings, train),
input_value.size(),
input_value.device(),
),
},
None => match input_embeds {
Some(embeds) => {
let size = vec![embeds.size()[0], embeds.size()[1]];
let device = embeds.device();
(embeds, size, device)
}
None => {
return Err(RustBertError::ValueError(
"At least one of input ids or input embeddings must be set".into(),
));
}
},
};
let calc_position_ids = if position_ids.is_none() {
Some(
Tensor::arange2(
start_ids_pos_encoding,
start_ids_pos_encoding + input_shape[1],
1,
(Kind::Int64, device),
)
.unsqueeze(0)
.expand(&input_shape, true),
)
} else {
None
};
let position_ids = position_ids.unwrap_or_else(|| calc_position_ids.as_ref().unwrap());
Ok(self.position_embeddings.forward_t(position_ids, train)
+ input_embeddings.apply_t(&self.dropout, train))
}
}