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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright 2018 The Open AI Team Authors
// Copyright 2020 Microsoft and the HuggingFace Inc. team.
// Copyright 2019-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::error::TokenizerError;
use crate::tokenizer::tokenization_utils::{
clean_text, decompose_nfkc, is_whitespace, split_on_special_tokens, strip_accents,
};
use crate::tokenizer::tokenization_utils::{lowercase, unknown_byte_fallback};
use crate::tokenizer::{MultiThreadedTokenizer, Tokenizer};
use crate::vocab::{DeBERTaV2Vocab, SentencePieceModel, Vocab};
use crate::{
Mask, Offset, OffsetSize, Token, TokenIdsWithOffsets, TokenIdsWithSpecialTokens, TokenRef,
};
use std::iter::Iterator;
use std::path::Path;
/// # DeBERTaV2 tokenizer
/// DeBERTa (v2) tokenizer (based on SentencePiece) performing:
/// - splitting on special characters
/// - text cleaning
/// - NFKC decomposition
/// - (optional) lower casing
/// - (optional) accent stripping
/// - SentencePiece BPE decomposition
pub struct DeBERTaV2Tokenizer {
model: SentencePieceModel,
vocab: DeBERTaV2Vocab,
lower_case: bool,
strip_accents: bool,
add_prefix_space: bool,
}
impl DeBERTaV2Tokenizer {
/// Create a new instance of a `DeBERTaV2Tokenizer`
/// Expects a SentencePiece BPE protobuf file as an input.
///
/// # Parameters
/// - path (`&str`): path to the SentencePiece model file
/// - lower_case (`bool`): flag indicating if the text should be lower-cased as part of the tokenization
/// - strip_accents (`bool`): flag indicating if accents should be stripped from the text
/// - add_prefix_space (`bool`): flag indicating if a space should be preprended to the input text before tokenization
///
/// # Example
///
/// ```no_run
/// use rust_tokenizers::tokenizer::{DeBERTaV2Tokenizer, Tokenizer};
/// let strip_accents = false;
/// let lower_case = false;
/// let add_prefix_space = false;
/// let tokenizer = DeBERTaV2Tokenizer::from_file(
/// "path/to/vocab/file",
/// lower_case,
/// strip_accents,
/// add_prefix_space,
/// )
/// .unwrap();
/// ```
pub fn from_file<P: AsRef<Path>>(
path: P,
lower_case: bool,
strip_accents: bool,
add_prefix_space: bool,
) -> Result<DeBERTaV2Tokenizer, TokenizerError> {
let model = SentencePieceModel::from_file(&path)?;
let vocab = DeBERTaV2Vocab::from_file(path)?;
Ok(DeBERTaV2Tokenizer {
model,
vocab,
lower_case,
strip_accents,
add_prefix_space,
})
}
/// Create a new instance of a `DeBERTaV2Tokenizer`
/// Expects a SentencePiece BPE protobuf file and special token mapping file as inputs.
///
/// # Parameters
/// - path (`&str`): path to the SentencePiece model file
/// - lower_case (`bool`): flag indicating if the text should be lower-cased as part of the tokenization
/// - strip_accents (`bool`): flag indicating if accents should be stripped from the text
/// - add_prefix_space (`bool`): flag indicating if a space should be preprended to the input text before tokenization
///
/// # Example
///
/// ```no_run
/// use rust_tokenizers::tokenizer::{DeBERTaV2Tokenizer, Tokenizer};
/// let strip_accents = false;
/// let lower_case = false;
/// let add_prefix_space = false;
/// let tokenizer = DeBERTaV2Tokenizer::from_file_with_special_token_mapping(
/// "path/to/vocab/file",
/// lower_case,
/// strip_accents,
/// add_prefix_space,
/// "path/to/special/token/mapping/file",
/// )
/// .unwrap();
/// ```
pub fn from_file_with_special_token_mapping<P: AsRef<Path>, S: AsRef<Path>>(
path: P,
lower_case: bool,
strip_accents: bool,
add_prefix_space: bool,
special_token_mapping_path: S,
) -> Result<DeBERTaV2Tokenizer, TokenizerError> {
let model = SentencePieceModel::from_file(&path)?;
let vocab =
DeBERTaV2Vocab::from_file_with_special_token_mapping(path, special_token_mapping_path)?;
Ok(DeBERTaV2Tokenizer {
model,
vocab,
lower_case,
strip_accents,
add_prefix_space,
})
}
/// Create a new instance of a `DeBERTaV2Tokenizer` from an existing vocabulary and model
///
/// # Parameters
/// - vocab (`DeBERTaV2Vocab`): vocabulary
/// - model (`SentencePieceBPEModel`): SentencePiece BPE model
/// - lower_case (`bool`): flag indicating if the text should be lower-cased as part of the tokenization
/// - strip_accents (`bool`): flag indicating if accents should be stripped from the text
/// - add_prefix_space (`bool`): flag indicating if a space should be preprended to the input text before tokenization
///
/// # Example
///
/// ```no_run
/// use rust_tokenizers::tokenizer::DeBERTaV2Tokenizer;
/// use rust_tokenizers::vocab::{DeBERTaV2Vocab, SentencePieceModel, Vocab};
/// let strip_accents = false;
/// let lower_case = false;
/// let add_prefix_space = false;
/// let vocab = DeBERTaV2Vocab::from_file("path/to/vocab/file").unwrap();
/// let model = SentencePieceModel::from_file("path/to/model/file").unwrap();
///
/// let tokenizer = DeBERTaV2Tokenizer::from_existing_vocab_and_model(
/// vocab,
/// model,
/// lower_case,
/// strip_accents,
/// add_prefix_space,
/// );
/// ```
pub fn from_existing_vocab_and_model(
vocab: DeBERTaV2Vocab,
model: SentencePieceModel,
lower_case: bool,
strip_accents: bool,
add_prefix_space: bool,
) -> DeBERTaV2Tokenizer {
DeBERTaV2Tokenizer {
model,
vocab,
lower_case,
strip_accents,
add_prefix_space,
}
}
fn post_process_pieces<'a>(&self, tokens: &'a mut Vec<Token>) -> &'a Vec<Token> {
let mut positions_to_update: Vec<(usize, Vec<Token>)> = vec![];
for (token_idx, token) in tokens.iter().enumerate() {
let mut token_chars = token.text.chars().rev();
if token.text.chars().count() > 1
&& (token_chars.next().unwrap() == ',')
& token_chars.next().unwrap().is_ascii_digit()
{
let mut new_token = token.clone();
let last_char = new_token.text.pop().unwrap();
let updated_tokens = self.model.decode_forward_token_ref(new_token.as_ref());
let updated_tokens = self.model.decode_backward(&updated_tokens);
let mut updated_tokens = self.model.parse_nodes_to_tokens(updated_tokens);
if !token.text.starts_with('\u{2581}')
& updated_tokens[0].text.starts_with('\u{2581}')
{
if updated_tokens[0].text.chars().count() == 1 {
updated_tokens.remove(0);
} else {
let first_char_length =
updated_tokens[0].text.chars().next().unwrap().len_utf8();
updated_tokens[0].text = (updated_tokens[0].text[first_char_length..])
.parse()
.unwrap();
}
}
updated_tokens.push(Token {
text: last_char.to_string(),
offset: Offset {
begin: token.offset.end,
end: token.offset.end,
},
reference_offsets: vec![*token.reference_offsets.last().unwrap()],
mask: token.mask,
});
positions_to_update.push((token_idx, updated_tokens));
}
if let Some(byte_tokens) = unknown_byte_fallback(token.as_ref(), Tokenizer::vocab(self))
{
positions_to_update.push((token_idx, byte_tokens));
}
}
for (pos, new_tokens) in positions_to_update.into_iter().rev() {
tokens.splice(pos..pos + 1, new_tokens);
}
tokens
}
}
impl Tokenizer<DeBERTaV2Vocab> for DeBERTaV2Tokenizer {
fn vocab(&self) -> &DeBERTaV2Vocab {
&self.vocab
}
fn vocab_mut(&mut self) -> &mut DeBERTaV2Vocab {
&mut self.vocab
}
fn tokenize_to_tokens(&self, initial_token: TokenRef) -> Vec<Token> {
let mut initial_token: Token = initial_token.to_owned();
if !is_whitespace(&initial_token.text.chars().next().unwrap()) & self.add_prefix_space {
initial_token.text.insert(0, ' ');
initial_token.reference_offsets.insert(0, 0);
};
let mut tokens = split_on_special_tokens(initial_token.as_ref(), &self.vocab)
.into_iter()
.map(|token| token.to_owned())
.collect::<Vec<Token>>();
let mut sub_tokens: Vec<Token> = Vec::new();
for token in tokens.iter_mut() {
if token.mask != Mask::Special && token.mask != Mask::Unknown {
clean_text(token, true);
decompose_nfkc(token);
if self.lower_case {
lowercase(token);
}
if self.strip_accents {
strip_accents(token);
}
token.text = token.text.replace(|c: char| is_whitespace(&c), "\u{2581}");
if !token.text.starts_with('\u{2581}') {
token.text.insert(0, '\u{2581}');
token.reference_offsets.insert(0, 0);
};
let output = self.model.decode_forward_token_ref(token.as_ref());
let decoded = self.model.decode_backward(&output);
let mut output: Vec<Token> = self.model.parse_nodes_to_tokens(decoded);
self.post_process_pieces(&mut output);
sub_tokens.extend(output)
} else {
sub_tokens.push(token.clone());
}
}
sub_tokens
}
fn convert_tokens_to_string(&self, tokens: Vec<String>) -> String {
tokens
.into_iter()
.map(|v| v.replace('\u{2581}', " "))
.collect::<Vec<String>>()
.join("")
}
fn build_input_with_special_tokens(
&self,
tokens_ids_with_offsets_1: TokenIdsWithOffsets,
tokens_ids_with_offsets_2: Option<TokenIdsWithOffsets>,
) -> TokenIdsWithSpecialTokens {
let mut output: Vec<i64> = vec![];
let mut token_segment_ids: Vec<i8> = vec![];
let mut special_tokens_mask: Vec<i8> = vec![];
let mut offsets: Vec<Option<Offset>> = vec![];
let mut original_offsets: Vec<Vec<OffsetSize>> = vec![];
let mut mask: Vec<Mask> = vec![];
special_tokens_mask.push(1);
special_tokens_mask.extend(vec![0; tokens_ids_with_offsets_1.ids.len()]);
special_tokens_mask.push(1);
token_segment_ids.extend(vec![0; tokens_ids_with_offsets_1.ids.len() + 2]);
output.push(self.vocab.token_to_id(self.vocab.get_cls_value()));
output.extend(tokens_ids_with_offsets_1.ids);
output.push(self.vocab.token_to_id(self.vocab.get_sep_value()));
offsets.push(None);
offsets.extend(tokens_ids_with_offsets_1.offsets);
offsets.push(None);
original_offsets.push(vec![]);
original_offsets.extend(tokens_ids_with_offsets_1.reference_offsets);
original_offsets.push(vec![]);
mask.push(Mask::Special);
mask.extend(tokens_ids_with_offsets_1.masks);
mask.push(Mask::Special);
if let Some(tokens_ids_with_offsets_2_value) = tokens_ids_with_offsets_2 {
let length = tokens_ids_with_offsets_2_value.ids.len();
special_tokens_mask.extend(vec![0; length]);
special_tokens_mask.push(1);
token_segment_ids.extend(vec![1; length + 1]);
output.extend(tokens_ids_with_offsets_2_value.ids);
output.push(self.vocab.token_to_id(self.vocab.get_sep_value()));
offsets.extend(tokens_ids_with_offsets_2_value.offsets);
original_offsets.extend(tokens_ids_with_offsets_2_value.reference_offsets);
offsets.push(None);
original_offsets.push(vec![]);
mask.extend(tokens_ids_with_offsets_2_value.masks);
mask.push(Mask::Special);
}
TokenIdsWithSpecialTokens {
token_ids: output,
segment_ids: token_segment_ids,
special_tokens_mask,
token_offsets: offsets,
reference_offsets: original_offsets,
mask,
}
}
}
impl MultiThreadedTokenizer<DeBERTaV2Vocab> for DeBERTaV2Tokenizer {}