-
Notifications
You must be signed in to change notification settings - Fork 4
/
basic.rs
178 lines (157 loc) · 5.22 KB
/
basic.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
use indexmap::IndexMap;
use crate::base::{
get_max_entry, get_stats, merge, Loadable, Saveable, Token, Tokenizer, Trainable,
};
/// Minimal (byte-level) Byte Pair Encoding tokenizer.
///
/// Algorithmically follows along the GPT tokenizer:
/// https://github.com/openai/gpt-2/blob/master/src/encoder.py
///
/// But:
/// - Does not handle the regular expression splitting pattern.
/// - Does not handle any special tokens.
///
/// # Examples
///
/// ```
/// use minbpe::BasicTokenizer;
/// use minbpe::Tokenizer;
/// use minbpe::Trainable;
///
/// let mut tokenizer = BasicTokenizer::new();
/// let text = "Hello, world!";
/// let vocab_size = 256;
/// let verbose = true;
///
/// tokenizer.train(text, vocab_size, verbose);
/// let encoded = tokenizer.encode(text);
/// let decoded = tokenizer.decode(&encoded);
///
/// assert_eq!(text, decoded);
/// ```
pub struct BasicTokenizer {
special_tokens: IndexMap<String, Token>,
merges: IndexMap<(Token, Token), Token>,
vocab: IndexMap<Token, Vec<u8>>,
}
impl BasicTokenizer {
pub fn new() -> Self {
BasicTokenizer {
special_tokens: IndexMap::new(),
merges: IndexMap::new(),
vocab: IndexMap::new(),
}
}
}
impl Default for BasicTokenizer {
fn default() -> Self {
Self::new()
}
}
impl Tokenizer for BasicTokenizer {
fn special_tokens(&self) -> &IndexMap<String, Token> {
&self.special_tokens
}
fn merges(&self) -> &IndexMap<(Token, Token), Token> {
&self.merges
}
fn vocab(&self) -> &IndexMap<Token, Vec<u8>> {
&self.vocab
}
fn decode(&self, ids: &[Token]) -> String {
// Given ids (list of integers), return Rust string
let text_bytes: Vec<u8> = ids
.iter()
.flat_map(|&idx| self.vocab[&idx].clone())
.collect();
String::from_utf8_lossy(&text_bytes).into_owned()
}
fn encode(&self, text: &str) -> Vec<Token> {
// Given a string text, return the token ids
let text_bytes = text.as_bytes();
let mut ids: Vec<Token> = text_bytes.iter().map(|&b| b as Token).collect();
while ids.len() >= 2 {
// Find the pair with the lowest merge index
let stats = get_stats(&ids);
let pair_opt = stats
.keys()
.filter_map(|&pair| self.merges.get(&pair).map(|_| pair))
.min_by_key(|&pair| self.merges[&pair]);
match pair_opt {
None => break, // If there are no more merges available, break
Some(pair) => {
// Otherwise, merge the best pair (lowest merge index)
let idx = self.merges[&pair];
ids = merge(&ids, pair, idx);
}
};
}
ids
}
}
impl Trainable for BasicTokenizer {
fn train(&mut self, text: &str, vocab_size: Token, verbose: bool) {
assert!(vocab_size >= 256, "Vocab size must be at least 256");
let num_merges = vocab_size - 256;
// Input text preprocessing
let text_bytes = text.as_bytes();
let mut ids: Vec<Token> = text_bytes.iter().map(|&b| b as Token).collect();
// Iteratively merge the most common pairs to create new tokens
let mut merges: IndexMap<(Token, Token), Token> = IndexMap::new();
let mut vocab: IndexMap<Token, Vec<u8>> =
(0..256).map(|idx| (idx, vec![idx as u8])).collect();
for i in 0..num_merges {
// Count up the number of times every consecutive pair appears
let stats = get_stats(&ids);
// Find the pair with the highest count
let pair = get_max_entry(&stats).unwrap().0;
// Mint a new token: assign it the next available id
let idx = 256 + i;
// Replace all occurrences of pair in ids with idx
ids = merge(&ids, *pair, idx);
// Save the merge
merges.insert(*pair, idx);
vocab.insert(
idx,
[vocab[&pair.0].clone(), vocab[&pair.1].clone()].concat(),
);
// Prints
if verbose {
println!(
"merge {}/{}: {:?} -> {} ({:?}) had {} occurrences",
i + 1,
num_merges,
pair,
idx,
vocab[&idx],
stats[pair]
);
}
}
// Save instance variables
self.merges = merges;
self.vocab = vocab; // FIXME: vs. build_vocab(&self.special_tokens, &self.merges);
}
}
impl Saveable for BasicTokenizer {
fn pattern(&self) -> &str {
""
}
}
impl Loadable for BasicTokenizer {
fn set_pattern(&mut self, pattern: &str) {
let temp = pattern.trim();
if !temp.is_empty() {
panic!("Cannot set a non-empty pattern!")
}
}
fn set_special_tokens(&mut self, special_tokens: IndexMap<String, Token>) {
self.special_tokens = special_tokens;
}
fn set_merges(&mut self, merges: IndexMap<(Token, Token), Token>) {
self.merges = merges;
}
fn set_vocab(&mut self, vocab: IndexMap<Token, Vec<u8>>) {
self.vocab = vocab;
}
}