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
//! # BPE Tokenizer
//!
//! Simple Byte-Pair Encoding tokenizer with training, encode, and decode.
use std::collections::HashMap;
// ── Type aliases ─────────────────────────────────────────────────────────────
/// Convenience alias for a pair of token strings.
pub type TokenPair = (String, String);
// ── BpeMerge ─────────────────────────────────────────────────────────────────
/// A single BPE merge rule: two adjacent tokens that were merged into one.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BpeMerge {
/// The pair of tokens that were merged.
pub pair: (String, String),
/// The resulting merged token.
pub merged: String,
/// Rank of this merge (lower = earlier / higher priority).
pub rank: usize,
}
// ── BpeTokenizer ─────────────────────────────────────────────────────────────
/// Byte-Pair Encoding tokenizer.
///
/// Train with [`BpeTokenizer::train`], then call [`encode`](Self::encode) /
/// [`decode`](Self::decode).
#[derive(Debug, Clone)]
pub struct BpeTokenizer {
/// Ordered list of merge rules (applied in rank order during encoding).
pub merges: Vec<BpeMerge>,
/// Vocabulary mapping token → id.
pub vocab: HashMap<String, usize>,
/// Whether to use byte-level initialisation (future extension; currently
/// always character-level).
pub byte_vocab: bool,
}
impl BpeTokenizer {
// ── Training ─────────────────────────────────────────────────────────────
/// Train a BPE tokenizer on `text` until the vocabulary reaches
/// `vocab_size` or no more pairs can be merged.
pub fn train(text: &str, vocab_size: usize) -> Self {
// Step 1: initialise vocabulary with individual characters.
let mut vocab: HashMap<String, usize> = HashMap::new();
// Split text into words separated by whitespace, add a space marker
// to every word except the first so decode can reconstruct spaces.
let words: Vec<Vec<String>> = text
.split_whitespace()
.enumerate()
.map(|(i, word)| {
let prefix = if i == 0 { "" } else { "Ġ" };
let mut chars: Vec<String> = word
.chars()
.map(|c| c.to_string())
.collect();
if !chars.is_empty() && !prefix.is_empty() {
chars[0] = format!("{}{}", prefix, chars[0]);
}
chars
})
.collect();
// Populate initial vocab from characters present in the corpus.
let mut next_id = 0usize;
for word in &words {
for ch in word {
vocab.entry(ch.clone()).or_insert_with(|| {
let id = next_id;
next_id += 1;
id
});
}
}
let mut corpus: Vec<Vec<String>> = words;
let mut merges: Vec<BpeMerge> = Vec::new();
let mut rank = 0usize;
// Step 2 & 3: greedily merge most-frequent adjacent pairs.
while vocab.len() < vocab_size {
let freqs = pair_frequencies(&corpus);
if freqs.is_empty() {
break;
}
// Find the most-frequent pair (tie-break: lexicographic order).
let best = freqs
.iter()
.max_by(|a, b| {
a.1.cmp(b.1)
.then_with(|| a.0.cmp(b.0).reverse())
})
.map(|(pair, _)| pair.clone());
let Some((left, right)) = best else { break };
let merged = format!("{}{}", left, right);
let merge = BpeMerge {
pair: (left.clone(), right.clone()),
merged: merged.clone(),
rank,
};
rank += 1;
// Add merged token to vocab.
let id = next_id;
next_id += 1;
vocab.insert(merged.clone(), id);
// Apply the merge throughout the corpus.
apply_merge(&mut corpus, &merge);
merges.push(merge);
}
Self {
merges,
vocab,
byte_vocab: false,
}
}
// ── Encode ───────────────────────────────────────────────────────────────
/// Encode `text` into a sequence of token strings using the learned merges.
pub fn encode(&self, text: &str) -> Vec<String> {
// Start from character split (with Ġ space markers).
let mut corpus: Vec<Vec<String>> = text
.split_whitespace()
.enumerate()
.map(|(i, word)| {
let prefix = if i == 0 { "" } else { "Ġ" };
let mut chars: Vec<String> = word
.chars()
.map(|c| c.to_string())
.collect();
if !chars.is_empty() && !prefix.is_empty() {
chars[0] = format!("{}{}", prefix, chars[0]);
}
chars
})
.collect();
// Apply each merge in rank order.
for merge in &self.merges {
apply_merge(&mut corpus, merge);
}
corpus.into_iter().flatten().collect()
}
// ── Decode ───────────────────────────────────────────────────────────────
/// Decode a token sequence back to a string.
///
/// The `Ġ` space marker (used internally to represent word boundaries) is
/// replaced with a space character.
pub fn decode(&self, tokens: &[String]) -> String {
tokens
.iter()
.map(|t| t.replace('Ġ', " "))
.collect::<String>()
.trim_start()
.to_string()
}
/// Number of tokens in the vocabulary.
pub fn vocab_size(&self) -> usize {
self.vocab.len()
}
}
// ── Pair frequency counting ───────────────────────────────────────────────────
/// Count adjacent token-pair frequencies across all sequences in `corpus`.
pub fn pair_frequencies(corpus: &[Vec<String>]) -> HashMap<(String, String), usize> {
let mut counts: HashMap<(String, String), usize> = HashMap::new();
for seq in corpus {
for window in seq.windows(2) {
let pair = (window[0].clone(), window[1].clone());
*counts.entry(pair).or_insert(0) += 1;
}
}
counts
}
// ── Merge application ─────────────────────────────────────────────────────────
/// Replace every occurrence of `merge.pair` with `merge.merged` in `corpus`.
pub fn apply_merge(corpus: &mut Vec<Vec<String>>, merge: &BpeMerge) {
for seq in corpus.iter_mut() {
let mut new_seq: Vec<String> = Vec::with_capacity(seq.len());
let mut i = 0;
while i < seq.len() {
if i + 1 < seq.len()
&& seq[i] == merge.pair.0
&& seq[i + 1] == merge.pair.1
{
new_seq.push(merge.merged.clone());
i += 2;
} else {
new_seq.push(seq[i].clone());
i += 1;
}
}
*seq = new_seq;
}
}
// ── Tests ─────────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn train_produces_merges() {
let text = "hello hello hello world world";
let tok = BpeTokenizer::train(text, 30);
// Should have merged some pairs — initial chars < final vocab.
assert!(
!tok.merges.is_empty(),
"expected at least one merge from repeated text"
);
}
#[test]
fn vocab_grows_with_training() {
let small = BpeTokenizer::train("ab ab", 10);
let large = BpeTokenizer::train("ab ab", 20);
// Larger target → at least as many merges tried.
assert!(large.vocab_size() >= small.vocab_size());
}
#[test]
fn encode_decode_roundtrip() {
let text = "hello world";
let tok = BpeTokenizer::train(text, 20);
let tokens = tok.encode(text);
let decoded = tok.decode(&tokens);
assert_eq!(decoded, text);
}
#[test]
fn encode_returns_nonempty() {
let tok = BpeTokenizer::train("the cat sat on the mat", 30);
let tokens = tok.encode("the cat");
assert!(!tokens.is_empty());
}
#[test]
fn pair_frequencies_counts_correctly() {
let corpus = vec![
vec!["a".to_string(), "b".to_string(), "a".to_string()],
];
let freqs = pair_frequencies(&corpus);
assert_eq!(freqs[&("a".to_string(), "b".to_string())], 1);
assert_eq!(freqs[&("b".to_string(), "a".to_string())], 1);
}
#[test]
fn apply_merge_replaces_all_occurrences() {
let merge = BpeMerge {
pair: ("a".to_string(), "b".to_string()),
merged: "ab".to_string(),
rank: 0,
};
let mut corpus = vec![vec![
"a".to_string(),
"b".to_string(),
"c".to_string(),
"a".to_string(),
"b".to_string(),
]];
apply_merge(&mut corpus, &merge);
assert_eq!(corpus[0], vec!["ab", "c", "ab"]);
}
#[test]
fn decode_handles_space_markers() {
// Manually construct tokens with space markers.
let tokens = vec!["hello".to_string(), "Ġworld".to_string()];
let tok = BpeTokenizer {
merges: vec![],
vocab: HashMap::new(),
byte_vocab: false,
};
assert_eq!(tok.decode(&tokens), "hello world");
}
}