kute-bpe 0.9.1

Byte Pair Encoding for KUTE: native byte-level BPE and BPE over symbol sequences
Documentation
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//! Algorithm core shared by byte BPE and symbol-sequence BPE.
//!
//! Works on sequences of `u32` ids over any base alphabet. Selection rule,
//! chosen to match rustbpe and tiktoken: merge the most frequent adjacent pair;
//! on a tie, the numerically smallest pair. The result depends only on the
//! multiset of input sequences, never on hash or thread order.

use kute_types::{KuteError, Result, TrainingConfig, Vocabulary};
use rustc_hash::{FxHashMap, FxHashSet};
use std::cmp::Reverse;
use std::collections::BinaryHeap;

pub type Pair = (u32, u32);

/// One learned merge and the number of times the pair occurred when chosen.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LearnedMerge {
    pub left: u32,
    pub right: u32,
    pub id: u32,
    pub count: u64,
}

/// Learn up to `max_merges` merges.
///
/// `words[i]` occurs `counts[i]` times. New ids start at `first_id`. A pair is
/// only merged while its count is at least `min_frequency` and `allow(left,
/// right)` returns true (used for the token length limit).
pub fn train_merges(
    mut words: Vec<Vec<u32>>,
    counts: &[u64],
    first_id: u32,
    max_merges: u32,
    min_frequency: u64,
    mut allow: impl FnMut(u32, u32) -> bool,
) -> Vec<LearnedMerge> {
    debug_assert_eq!(words.len(), counts.len());
    let mut pair_counts: FxHashMap<Pair, i64> = FxHashMap::default();
    // The words a pair has occurred in. A word may be listed more than once
    // and may no longer hold the pair; both are checked when it is visited.
    let mut where_used: FxHashMap<Pair, Vec<u32>> = FxHashMap::default();
    for (i, w) in words.iter().enumerate() {
        if counts[i] == 0 {
            continue;
        }
        for p in w.windows(2) {
            let pair = (p[0], p[1]);
            *pair_counts.entry(pair).or_insert(0) += counts[i] as i64;
            let list = where_used.entry(pair).or_default();
            if list.last() != Some(&(i as u32)) {
                list.push(i as u32);
            }
        }
    }

    let mut heap: BinaryHeap<(i64, Reverse<Pair>)> =
        pair_counts.iter().filter(|&(_, &c)| c > 0).map(|(&p, &c)| (c, Reverse(p))).collect();
    let mut banned: FxHashSet<Pair> = FxHashSet::default();
    let mut merges = Vec::new();
    let min_frequency = min_frequency.max(1) as i64;
    let mut touched: FxHashSet<Pair> = FxHashSet::default();
    let mut merged: Vec<u32> = Vec::new();
    // For each id of the word being rewritten: was it consumed by the merge?
    let mut consumed: Vec<bool> = Vec::new();

    while (merges.len() as u32) < max_merges {
        let Some((count, Reverse(pair))) = heap.pop() else { break };
        let current = pair_counts.get(&pair).copied().unwrap_or(0);
        if current <= 0 || banned.contains(&pair) {
            continue;
        }
        if current != count {
            // Stale entry: the count changed after it was queued.
            heap.push((current, Reverse(pair)));
            continue;
        }
        if current < min_frequency {
            break;
        }
        if !allow(pair.0, pair.1) {
            banned.insert(pair);
            continue;
        }

        let new_id = first_id + merges.len() as u32;
        merges.push(LearnedMerge { left: pair.0, right: pair.1, id: new_id, count: current as u64 });

        touched.clear();
        let users = where_used.remove(&pair).unwrap_or_default();
        for word_index in users {
            let i = word_index as usize;
            let word = &words[i];
            merged.clear();
            consumed.clear();
            let mut j = 0;
            while j < word.len() {
                if j + 1 < word.len() && word[j] == pair.0 && word[j + 1] == pair.1 {
                    merged.push(new_id);
                    consumed.extend([true, true]);
                    j += 2;
                } else {
                    merged.push(word[j]);
                    consumed.push(false);
                    j += 1;
                }
            }
            if merged.len() == word.len() {
                // Listed, but an earlier merge (or an earlier visit in this
                // one) already took the pair out of this word.
                continue;
            }
            // Only the pairs next to a merged position change; every other
            // adjacent pair of the word is in the new word too.
            let weight = counts[i] as i64;
            for (k, p) in word.windows(2).enumerate() {
                if consumed[k] || consumed[k + 1] {
                    let old = (p[0], p[1]);
                    *pair_counts.entry(old).or_insert(0) -= weight;
                    touched.insert(old);
                }
            }
            for p in merged.windows(2) {
                if p[0] == new_id || p[1] == new_id {
                    let new = (p[0], p[1]);
                    *pair_counts.entry(new).or_insert(0) += weight;
                    let list = where_used.entry(new).or_default();
                    if list.last() != Some(&word_index) {
                        list.push(word_index);
                    }
                    touched.insert(new);
                }
            }
            words[i].clone_from(&merged);
        }
        pair_counts.remove(&pair);
        for &p in &touched {
            if p == pair {
                continue;
            }
            match pair_counts.get(&p).copied() {
                Some(c) if c > 0 => heap.push((c, Reverse(p))),
                Some(_) => {
                    pair_counts.remove(&p);
                }
                None => {}
            }
        }
    }
    merges
}

/// The merges of `prior`, if a run of `algorithm` under `config` on the
/// corpus with this fingerprint may continue from them. `first_id` is the
/// size of the base alphabet, which the caller has checked is the same.
pub(crate) fn resumable_merges(
    prior: &Vocabulary,
    algorithm: &str,
    config: &TrainingConfig,
    corpus_fingerprint: &str,
    first_id: u32,
    max_merges: u32,
) -> Result<Vec<LearnedMerge>> {
    let refuse = |why: String| Err(KuteError::VocabularyMismatch(format!("cannot resume from {} {}: {why}", prior.id(), prior.version())));
    if prior.manifest.algorithm != algorithm {
        return refuse(format!("it was trained by {:?}, not {algorithm:?}", prior.manifest.algorithm));
    }
    if prior.provenance.corpus_fingerprints != [corpus_fingerprint.to_string()] {
        return refuse("it was trained on a different corpus".into());
    }
    let Some(recorded) = &prior.provenance.training else {
        return refuse("it does not record its training settings".into());
    };
    let now = serde_json::to_value(config)?;
    // The settings that decide which pair is merged next. A setting absent
    // from both records is the same in both.
    for key in ["min_frequency", "pretokenizer", "max_token_length", "train_window", "stream_bytes"] {
        if recorded.get(key) != now.get(key) {
            return refuse(format!("{key} differs"));
        }
    }
    if prior.merges.len() as u64 > max_merges as u64 {
        return refuse(format!("it already has {} merges and this run asks for {max_merges}", prior.merges.len()));
    }
    let mut out = Vec::with_capacity(prior.merges.len());
    for (i, m) in prior.merges.iter().enumerate() {
        let id = first_id + i as u32;
        if m.id != id || m.left >= id || m.right >= id {
            return refuse(format!("merge {i} is out of order"));
        }
        let count = prior.tokens.get(id as usize).and_then(|t| t.frequency).unwrap_or(0);
        out.push(LearnedMerge { left: m.left, right: m.right, id, count });
    }
    Ok(out)
}

/// Apply learned merges to one sequence.
///
/// Equivalent to repeatedly merging, left to right, every occurrence of the
/// applicable pair with the lowest merged id, which is what tiktoken and
/// rustbpe do, but in `O(n log n)` so that megabyte inputs without a
/// pre-tokenizer stay fast.
pub fn apply_merges(ids: &mut Vec<u32>, ranks: &FxHashMap<Pair, u32>) {
    let n = ids.len();
    if n < 2 || ranks.is_empty() {
        return;
    }
    if n <= 16 {
        apply_merges_small(ids, ranks);
        return;
    }
    const NONE: u32 = u32::MAX;
    let mut next: Vec<u32> = (1..=n as u32).collect();
    next[n - 1] = NONE;
    let mut prev: Vec<u32> = (0..n as u32).map(|i| i.wrapping_sub(1)).collect();
    prev[0] = NONE;
    let mut alive = vec![true; n];
    let initial: Vec<Reverse<(u32, u32)>> = (0..n - 1)
        .filter_map(|i| ranks.get(&(ids[i], ids[i + 1])).map(|&rank| Reverse((rank, i as u32))))
        .collect();
    if initial.is_empty() {
        return;
    }
    let mut heap = BinaryHeap::from(initial);
    while let Some(Reverse((rank, pos))) = heap.pop() {
        let i = pos as usize;
        if !alive[i] {
            continue;
        }
        let j = next[i];
        if j == NONE {
            continue;
        }
        let j = j as usize;
        if ranks.get(&(ids[i], ids[j])) != Some(&rank) {
            continue;
        }
        ids[i] = rank;
        alive[j] = false;
        let after = next[j];
        next[i] = after;
        if after != NONE {
            prev[after as usize] = pos;
            if let Some(&r) = ranks.get(&(rank, ids[after as usize])) {
                heap.push(Reverse((r, pos)));
            }
        }
        let before = prev[i];
        if before != NONE {
            if let Some(&r) = ranks.get(&(ids[before as usize], rank)) {
                heap.push(Reverse((r, before)));
            }
        }
    }
    let mut write = 0;
    for read in 0..n {
        if alive[read] {
            ids[write] = ids[read];
            write += 1;
        }
    }
    ids.truncate(write);
}

fn apply_merges_small(ids: &mut Vec<u32>, ranks: &FxHashMap<Pair, u32>) {
    while ids.len() >= 2 {
        let mut best: Option<(usize, u32)> = None;
        for i in 0..ids.len() - 1 {
            if let Some(&rank) = ranks.get(&(ids[i], ids[i + 1])) {
                if best.is_none_or(|(_, b)| rank < b) {
                    best = Some((i, rank));
                }
            }
        }
        let Some((_, rank)) = best else { break };
        // Merge every occurrence of the winning pair, left to right.
        let mut write = 0;
        let mut read = 0;
        while read < ids.len() {
            if read + 1 < ids.len() && ranks.get(&(ids[read], ids[read + 1])) == Some(&rank) {
                ids[write] = rank;
                read += 2;
            } else {
                ids[write] = ids[read];
                read += 1;
            }
            write += 1;
        }
        ids.truncate(write);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn ranks_of(merges: &[LearnedMerge]) -> FxHashMap<Pair, u32> {
        merges.iter().map(|m| ((m.left, m.right), m.id)).collect()
    }

    /// Textbook quadratic reference used to check both implementations.
    fn reference(mut ids: Vec<u32>, ranks: &FxHashMap<Pair, u32>) -> Vec<u32> {
        loop {
            let best = ids.windows(2).filter_map(|p| ranks.get(&(p[0], p[1])).copied()).min();
            let Some(rank) = best else { return ids };
            let mut out = Vec::new();
            let mut i = 0;
            while i < ids.len() {
                if i + 1 < ids.len() && ranks.get(&(ids[i], ids[i + 1])) == Some(&rank) {
                    out.push(rank);
                    i += 2;
                } else {
                    out.push(ids[i]);
                    i += 1;
                }
            }
            ids = out;
        }
    }

    #[test]
    fn picks_most_frequent_then_smallest_pair() {
        // "ab" x3 and "cd" x3 tie; (97,98) is the smaller pair.
        let words = vec![vec![97, 98], vec![99, 100]];
        let merges = train_merges(words, &[3, 3], 256, 2, 1, |_, _| true);
        assert_eq!((merges[0].left, merges[0].right, merges[0].id, merges[0].count), (97, 98, 256, 3));
        assert_eq!((merges[1].left, merges[1].right, merges[1].id), (99, 100, 257));
    }

    #[test]
    fn overlapping_pairs_count_correctly() {
        // "aaaa": pair (a,a) occurs 3 times, merges to X X, then (X,X).
        let merges = train_merges(vec![vec![1, 1, 1, 1]], &[1], 10, 5, 1, |_, _| true);
        assert_eq!(merges.len(), 2);
        assert_eq!((merges[0].left, merges[0].right, merges[0].count), (1, 1, 3));
        assert_eq!((merges[1].left, merges[1].right, merges[1].count), (10, 10, 1));
    }

    #[test]
    fn respects_min_frequency_and_allow() {
        let words = vec![vec![1, 2, 3], vec![1, 2]];
        let merges = train_merges(words.clone(), &[5, 1], 10, 9, 2, |_, _| true);
        // (1,2) x6 then (10,3) x5; nothing else reaches 2.
        assert_eq!(merges.len(), 2);
        let none = train_merges(words, &[5, 1], 10, 9, 1, |_, _| false);
        assert!(none.is_empty());
    }

    /// Textbook trainer: recount every pair from scratch before each merge.
    fn reference_training(mut words: Vec<Vec<u32>>, counts: &[u64], first_id: u32, max_merges: u32, min_frequency: i64) -> Vec<LearnedMerge> {
        let mut merges = Vec::new();
        while (merges.len() as u32) < max_merges {
            let mut pair_counts: std::collections::BTreeMap<Pair, i64> = Default::default();
            for (w, &c) in words.iter().zip(counts) {
                for p in w.windows(2) {
                    *pair_counts.entry((p[0], p[1])).or_insert(0) += c as i64;
                }
            }
            // most frequent, then the smallest pair
            let Some((&pair, &count)) = pair_counts.iter().filter(|&(_, &c)| c > 0).max_by_key(|&(&p, &c)| (c, Reverse(p))) else { break };
            if count < min_frequency {
                break;
            }
            let id = first_id + merges.len() as u32;
            merges.push(LearnedMerge { left: pair.0, right: pair.1, id, count: count as u64 });
            for w in words.iter_mut() {
                let mut out = Vec::with_capacity(w.len());
                let mut i = 0;
                while i < w.len() {
                    if i + 1 < w.len() && (w[i], w[i + 1]) == pair {
                        out.push(id);
                        i += 2;
                    } else {
                        out.push(w[i]);
                        i += 1;
                    }
                }
                *w = out;
            }
        }
        merges
    }

    #[test]
    fn training_matches_reference() {
        let mut state = 0x2545f4914f6cdd1du64;
        let mut rand = move |m: u32| {
            state ^= state << 13;
            state ^= state >> 7;
            state ^= state << 17;
            (state % m as u64) as u32
        };
        for round in 0..300 {
            // Small alphabets force repeats such as "aaaa" and "abab".
            let alphabet = 1 + rand(6);
            let n = 1 + rand(24) as usize;
            let words: Vec<Vec<u32>> = (0..n).map(|_| (0..rand(30)).map(|_| rand(alphabet)).collect()).collect();
            let counts: Vec<u64> = (0..n).map(|_| rand(5) as u64).collect();
            let min_frequency = 1 + rand(3) as u64;
            let max = 1 + rand(60);
            let fast = train_merges(words.clone(), &counts, alphabet, max, min_frequency, |_, _| true);
            let slow = reference_training(words, &counts, alphabet, max, min_frequency as i64);
            assert_eq!(fast, slow, "round {round}");
        }
    }

    /// The same comparison with a token length limit, the way the trainers
    /// call it: `allow` refuses a pair whose token would be too long, and a
    /// refused pair stays refused while its neighbours go on merging.
    #[test]
    fn training_with_a_length_limit_matches_reference() {
        let mut state = 0x853c49e6748fea9bu64;
        let mut rand = move |m: u32| {
            state ^= state << 13;
            state ^= state >> 7;
            state ^= state << 17;
            (state % m as u64) as u32
        };
        let mut refused_somewhere = 0;
        for round in 0..2000 {
            let alphabet = 1 + rand(4);
            let n = 1 + rand(16) as usize;
            let mut words: Vec<Vec<u32>> = (0..n).map(|_| (0..rand(40)).map(|_| rand(alphabet)).collect()).collect();
            // The same word more than once, as separate entries.
            if n > 1 && rand(3) == 0 {
                words[n - 1] = words[0].clone();
            }
            let counts: Vec<u64> = (0..n).map(|_| rand(4) as u64).collect();
            let min_frequency = 1 + rand(3) as i64;
            let max = 1 + rand(80);
            let limit = 2 + rand(6) as usize;

            let mut lengths = vec![1usize; alphabet as usize];
            let fast = train_merges(words.clone(), &counts, alphabet, max, min_frequency as u64, |l, r| {
                let len = lengths[l as usize] + lengths[r as usize];
                if len > limit {
                    return false;
                }
                lengths.push(len);
                true
            });

            // Textbook: recount everything, take the best pair not refused.
            let mut lengths = vec![1usize; alphabet as usize];
            let mut banned: std::collections::BTreeSet<Pair> = Default::default();
            let mut slow: Vec<LearnedMerge> = Vec::new();
            'steps: while (slow.len() as u32) < max {
                let mut pair_counts: std::collections::BTreeMap<Pair, i64> = Default::default();
                for (w, &c) in words.iter().zip(&counts) {
                    for p in w.windows(2) {
                        *pair_counts.entry((p[0], p[1])).or_insert(0) += c as i64;
                    }
                }
                let mut order: Vec<(i64, Reverse<Pair>)> = pair_counts.iter().filter(|&(_, &c)| c > 0).map(|(&p, &c)| (c, Reverse(p))).collect();
                order.sort_unstable_by(|a, b| b.cmp(a));
                let mut chosen = None;
                for (count, Reverse(pair)) in order {
                    if banned.contains(&pair) {
                        continue;
                    }
                    if count < min_frequency {
                        break 'steps;
                    }
                    let len = lengths[pair.0 as usize] + lengths[pair.1 as usize];
                    if len > limit {
                        banned.insert(pair);
                        refused_somewhere += 1;
                        continue;
                    }
                    lengths.push(len);
                    chosen = Some((pair, count));
                    break;
                }
                let Some((pair, count)) = chosen else { break };
                let id = alphabet + slow.len() as u32;
                slow.push(LearnedMerge { left: pair.0, right: pair.1, id, count: count as u64 });
                for w in words.iter_mut() {
                    let mut out = Vec::with_capacity(w.len());
                    let mut i = 0;
                    while i < w.len() {
                        if i + 1 < w.len() && (w[i], w[i + 1]) == pair {
                            out.push(id);
                            i += 2;
                        } else {
                            out.push(w[i]);
                            i += 1;
                        }
                    }
                    *w = out;
                }
            }
            assert_eq!(fast, slow, "round {round}");
        }
        assert!(refused_somewhere > 1000, "the limit should refuse pairs often: {refused_somewhere}");
    }

    #[test]
    fn fast_apply_matches_reference() {
        let mut state = 0x9e3779b97f4a7c15u64;
        let mut rand = move |m: u32| {
            state ^= state << 13;
            state ^= state >> 7;
            state ^= state << 17;
            (state % m as u64) as u32
        };
        for round in 0..60 {
            let alphabet = 2 + rand(5);
            let words: Vec<Vec<u32>> = (0..20).map(|_| (0..2 + rand(40)).map(|_| rand(alphabet)).collect()).collect();
            let counts: Vec<u64> = (0..20).map(|_| 1 + rand(4) as u64).collect();
            let merges = train_merges(words, &counts, alphabet, 30, 1, |_, _| true);
            let ranks = ranks_of(&merges);
            for len in [0usize, 1, 2, 5, 16, 17, 64, 300] {
                let input: Vec<u32> = (0..len).map(|_| rand(alphabet)).collect();
                let expected = reference(input.clone(), &ranks);
                let mut fast = input.clone();
                apply_merges(&mut fast, &ranks);
                assert_eq!(fast, expected, "round {round} len {len}");
                let mut small = input;
                apply_merges_small(&mut small, &ranks);
                assert_eq!(small, expected, "small path, round {round} len {len}");
            }
        }
    }
}