splintr 0.19.1

Fast Rust tokenizer (BPE + SentencePiece + WordPiece) with Python bindings
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
use super::*;
use crate::core::encoder::{encoder_from_owned, Encoder};
use proptest::prelude::*;
use rustc_hash::FxHashMap;

use super::encode::{byte_pair_encode_ids_seeded_into, Seeding};
use super::ranks::{BytePairRanks, RankLookup};

/// `byte_pair_encode_ids_seeded_into` collected, so the property below can
/// compare it against the piece-reporting form as a value.
fn ids_seeded(
    piece: &[u8],
    merge_ranks: &Encoder,
    id_encoder: &Encoder,
    char_granular: bool,
) -> Vec<u32> {
    let mut out = Vec::new();
    byte_pair_encode_ids_seeded_into(
        piece,
        RankLookup::new(merge_ranks),
        id_encoder,
        match char_granular {
            true => Seeding::Chars,
            false => Seeding::Bytes,
        },
        &mut out,
    );
    out
}

/// A node in the reference implementation's linked list.
///
/// Identical to [`Node`] plus the `rank` field the linear scan needs.
#[derive(Debug, Clone, Copy)]
struct RefNode {
    prev: usize,
    next: usize,
    rank: u32,
    start: usize,
    len: usize,
}

/// The pre-heap implementation, kept verbatim as a correctness oracle.
///
/// It rescans the whole list for the minimum rank on every merge, which is
/// O(N × M) and unusable in production — but it is obviously correct, and
/// [`byte_pair_encode_with_ranks`] must not diverge from it on ANY input.
/// In particular it resolves equal ranks LEFTMOST, because the scan keeps
/// the first strict minimum in list order.
fn byte_pair_encode_reference(
    piece: &[u8],
    merge_ranks: &Encoder,
    id_encoder: &Encoder,
) -> Vec<u32> {
    byte_pair_encode_reference_seeded(piece, merge_ranks, id_encoder, false)
}

/// The oracle with the same seeding switch as
/// [`byte_pair_encode_pieces_seeded`], so character granularity is checked
/// against an independently-written implementation too. Byte granularity
/// stays exactly as pinned.
fn byte_pair_encode_reference_seeded(
    piece: &[u8],
    merge_ranks: &Encoder,
    id_encoder: &Encoder,
    char_granular: bool,
) -> Vec<u32> {
    if piece.is_empty() {
        return vec![];
    }

    // Fast path: single byte
    if piece.len() == 1 {
        return id_encoder.get(piece).map_or(vec![], |r| vec![r]);
    }

    // Fast path: entire piece is a single token
    if let Some(id) = id_encoder.get(piece) {
        return vec![id];
    }

    // Initialize linked list - one node per byte, or per whole UTF-8
    // character when `char_granular` (byte seeding on invalid UTF-8).
    let spans: Vec<(usize, usize)> = match char_granular
        .then(|| std::str::from_utf8(piece).ok())
        .flatten()
    {
        Some(text) => text
            .char_indices()
            .map(|(start, c)| (start, c.len_utf8()))
            .collect(),
        None => (0..piece.len()).map(|start| (start, 1)).collect(),
    };

    let mut nodes: Vec<RefNode> = Vec::with_capacity(spans.len());
    for (i, &(start, len)) in spans.iter().enumerate() {
        nodes.push(RefNode {
            prev: if i == 0 { usize::MAX } else { i - 1 },
            next: if i + 1 == spans.len() {
                usize::MAX
            } else {
                i + 1
            },
            rank: u32::MAX,
            start,
            len,
        });
    }

    // Helper closure to compute the merge rank of a pair
    let get_rank = |left_idx: usize, right_idx: usize, nodes: &[RefNode]| -> u32 {
        if left_idx == usize::MAX || right_idx == usize::MAX {
            return u32::MAX;
        }
        let left = &nodes[left_idx];
        let right = &nodes[right_idx];

        let start = left.start;
        let len = left.len + right.len;
        let slice = &piece[start..start + len];

        merge_ranks.get(slice).unwrap_or(u32::MAX)
    };

    // Initial rank calculation for all adjacent pairs
    for i in 0..nodes.len() - 1 {
        nodes[i].rank = get_rank(i, nodes[i].next, &nodes);
    }

    // Main merge loop
    loop {
        // Find the pair with minimum rank (highest priority merge)
        let mut min_rank = u32::MAX;
        let mut min_idx = usize::MAX;

        let mut curr = 0;
        // Find the head of the list (in case we started from a deleted node)
        while nodes[curr].prev != usize::MAX {
            curr = nodes[curr].prev;
        }

        // Linear scan through the linked list
        while curr != usize::MAX {
            let r = nodes[curr].rank;
            if r < min_rank {
                min_rank = r;
                min_idx = curr;
            }
            curr = nodes[curr].next;
        }

        // No more merges possible
        if min_rank == u32::MAX {
            break;
        }

        // Merge min_idx with its next node
        let next_idx = nodes[min_idx].next;

        // Update the merged node's length
        nodes[min_idx].len += nodes[next_idx].len;

        // Update linked list pointers (skip over next_idx)
        let new_next = nodes[next_idx].next;
        nodes[min_idx].next = new_next;
        if new_next != usize::MAX {
            nodes[new_next].prev = min_idx;
        }

        // Update ranks for affected pairs:
        // 1. The pair (prev, min_idx) if prev exists
        if nodes[min_idx].prev != usize::MAX {
            let prev = nodes[min_idx].prev;
            nodes[prev].rank = get_rank(prev, min_idx, &nodes);
        }

        // 2. The pair (min_idx, new_next)
        nodes[min_idx].rank = get_rank(min_idx, nodes[min_idx].next, &nodes);
    }

    // Collect final tokens by traversing the linked list
    let mut result = Vec::new();

    // Find head
    let mut curr = 0;
    while nodes[curr].prev != usize::MAX {
        curr = nodes[curr].prev;
    }

    while curr != usize::MAX {
        let node = &nodes[curr];
        let slice = &piece[node.start..node.start + node.len];

        if let Some(id) = id_encoder.get(slice) {
            result.push(id);
        } else {
            // Fallback: if somehow we have an unknown token, try to encode bytes individually
            // This shouldn't happen with a proper BPE vocabulary that covers all bytes
            for &byte in slice {
                if let Some(id) = id_encoder.get(&[byte][..]) {
                    result.push(id);
                }
            }
        }
        curr = nodes[curr].next;
    }

    result
}

fn make_encoder() -> Encoder {
    let mut encoder = FxHashMap::default();
    // Individual bytes
    encoder.insert(b"a".to_vec(), 0);
    encoder.insert(b"b".to_vec(), 1);
    encoder.insert(b"c".to_vec(), 2);
    // Merged pairs (lower rank = higher priority)
    encoder.insert(b"ab".to_vec(), 3);
    encoder.insert(b"bc".to_vec(), 4);
    encoder.insert(b"abc".to_vec(), 5);
    encoder_from_owned(encoder)
}

#[test]
fn test_single_byte() {
    let encoder = make_encoder();
    assert_eq!(byte_pair_encode(b"a", &encoder), vec![0]);
}

#[test]
fn test_simple_merge() {
    let encoder = make_encoder();
    // "ab" should merge to token 3
    assert_eq!(byte_pair_encode(b"ab", &encoder), vec![3]);
}

#[test]
fn test_chain_merge() {
    let encoder = make_encoder();
    // "abc" should merge to token 5
    // First "ab" (rank 3) or "bc" (rank 4)? "ab" has lower rank, so:
    // a b c -> ab c -> abc
    assert_eq!(byte_pair_encode(b"abc", &encoder), vec![5]);
}

#[test]
fn test_empty() {
    let encoder = make_encoder();
    let empty: Vec<u32> = vec![];
    assert_eq!(byte_pair_encode(b"", &encoder), empty);
}

#[test]
fn test_no_merge_possible() {
    let encoder = make_encoder();
    // "ac" has no merge entry, so stays as [a, c]
    assert_eq!(byte_pair_encode(b"ac", &encoder), vec![0, 2]);
}

/// A vocabulary in which every pair of a repeated character ties.
fn tie_encoder() -> Encoder {
    let mut encoder = FxHashMap::default();
    encoder.insert(b"a".to_vec(), 0);
    encoder.insert(b"aa".to_vec(), 1);
    encoder_from_owned(encoder)
}

/// Equal-rank pairs MUST resolve leftmost.
///
/// In `"aaa"` both `(0,1)` and `(1,2)` spell `"aa"` at rank 1. Taking the
/// left one yields `[aa][a]` = `[1, 0]`; taking the right one yields
/// `[a][aa]` = `[0, 1]`. tiktoken produces the former, so we must too —
/// and repeated-character runs are exactly the pathological input where
/// this is reachable, not some exotic corner.
///
/// This is the test that fails if someone "simplifies" `Merge`'s `Ord` to
/// compare rank alone: a rank-only heap picks an arbitrary one of the tied
/// pairs and silently changes token output.
#[test]
fn test_tiebreak_leftmost_wins() {
    let encoder = tie_encoder();
    assert_eq!(byte_pair_encode(b"aaa", &encoder), vec![1, 0]);
    assert_eq!(byte_pair_encode(b"aaaaa", &encoder), vec![1, 1, 0]);

    // And the oracle agrees, which is where the invariant comes from.
    assert_eq!(
        byte_pair_encode_reference(b"aaa", &encoder, &encoder),
        vec![1, 0]
    );
    assert_eq!(
        byte_pair_encode_reference(b"aaaaa", &encoder, &encoder),
        vec![1, 1, 0]
    );
}

/// The `Token` ids of a piece list, dropping the unresolved spans — the
/// same projection [`byte_pair_encode_with_ranks`] applies, so results can
/// be compared against the oracle's token vector.
fn tokens_only(pieces: Vec<Piece>) -> Vec<u32> {
    pieces
        .into_iter()
        .filter_map(|p| match p {
            Piece::Token(id) => Some(id),
            Piece::Unresolved { .. } => None,
        })
        .collect()
}

/// The same leftmost tiebreak as [`test_tiebreak_leftmost_wins`], over a
/// multi-byte alphabet under character seeding.
///
/// There a node index is a *character* index rather than a byte index, so
/// this pins that ordering too: `"▁▁▁"` must resolve `[▁▁][▁]`, not
/// `[▁][▁▁]`, exactly as `"aaa"` does.
#[test]
fn test_tiebreak_leftmost_wins_multibyte_chars() {
    let mut encoder = FxHashMap::default();
    encoder.insert("".as_bytes().to_vec(), 0);
    encoder.insert("▁▁".as_bytes().to_vec(), 1);
    let encoder = encoder_from_owned(encoder);

    let piece = "▁▁▁".as_bytes();
    assert_eq!(
        tokens_only(byte_pair_encode_pieces_seeded(
            piece,
            RankLookup::new(&encoder),
            &encoder,
            true
        )),
        vec![1, 0]
    );
    assert_eq!(
        byte_pair_encode_reference_seeded(piece, &encoder, &encoder, true),
        vec![1, 0]
    );
}

/// tiktoken-style vocabulary: the id doubles as the merge rank.
fn prop_encoder() -> Encoder {
    let mut encoder = FxHashMap::default();
    let tokens: [&[u8]; 18] = [
        b"a", b"b", b"c", b"d", b"aa", b"ab", b"ba", b"bb", b"cd", b"dc", b"cc", b"aaa", b"aab",
        b"abab", b"aaaa", b"bcd", b"abcd", b"abc",
    ];
    for (i, token) in tokens.iter().enumerate() {
        encoder.insert(token.to_vec(), i as u32);
    }
    encoder_from_owned(encoder)
}

/// HuggingFace-style vocabulary: merge priority is independent of the id,
/// and several merges deliberately share a rank so the leftmost tiebreak
/// is exercised rather than accidentally avoided by unique ranks.
fn prop_two_maps() -> (Encoder, Encoder) {
    let ranked: [(&[u8], u32); 13] = [
        (b"aa", 1),
        (b"ab", 1),
        (b"bb", 1),
        (b"ba", 2),
        (b"cc", 2),
        (b"cd", 3),
        (b"dc", 3),
        (b"aaa", 4),
        (b"aab", 4),
        (b"abb", 4),
        (b"abab", 5),
        (b"aaaa", 5),
        (b"abcd", 6),
    ];
    let mut merge_ranks = FxHashMap::default();
    for (token, rank) in ranked {
        merge_ranks.insert(token.to_vec(), rank);
    }

    // Ids in an order that has nothing to do with the merge ranks, so a
    // mix-up between the two maps cannot pass unnoticed.
    let ids: [&[u8]; 17] = [
        b"abcd", b"aaaa", b"abab", b"abb", b"aab", b"aaa", b"dc", b"cd", b"cc", b"ba", b"bb",
        b"ab", b"aa", b"d", b"c", b"b", b"a",
    ];
    let mut id_encoder = FxHashMap::default();
    for (i, token) in ids.iter().enumerate() {
        id_encoder.insert(token.to_vec(), i as u32);
    }
    (
        encoder_from_owned(merge_ranks),
        encoder_from_owned(id_encoder),
    )
}

/// HuggingFace-style vocabulary over a deliberately mixed-width alphabet:
/// ASCII (1 byte), `▁` (3 bytes), a CJK character (3 bytes) and an emoji
/// (4 bytes) — the widths that byte seeding cannot reassemble. Ranks tie so
/// the leftmost tiebreak is exercised, and `中😀` is deliberately given a
/// merge rank but NO id so the unresolved fallback path is covered too.
fn char_prop_maps() -> (Encoder, Encoder) {
    let ranked: [(&str, u32); 10] = [
        ("aa", 1),
        ("ab", 1),
        ("▁a", 1),
        ("b▁", 2),
        ("中😀", 2),
        ("😀😀", 2),
        ("aab", 3),
        ("ab▁", 3),
        ("中😀中", 4),
        ("aaaa", 5),
    ];
    let mut merge_ranks = FxHashMap::default();
    for (token, rank) in ranked {
        merge_ranks.insert(token.as_bytes().to_vec(), rank);
    }

    // Ids in an order unrelated to the merge ranks, and missing `中😀`.
    let ids: [&str; 14] = [
        "a",
        "b",
        "",
        "",
        "😀",
        "aaaa",
        "中😀中",
        "ab▁",
        "aab",
        "😀😀",
        "b▁",
        "▁a",
        "ab",
        "aa",
    ];
    let mut id_encoder = FxHashMap::default();
    for (i, token) in ids.iter().enumerate() {
        id_encoder.insert(token.as_bytes().to_vec(), i as u32);
    }
    (
        encoder_from_owned(merge_ranks),
        encoder_from_owned(id_encoder),
    )
}

#[test]
fn test_long_single_char_run() {
    let encoder = tie_encoder();
    let piece = vec![b'a'; 4096];
    let expected = vec![1u32; 2048];
    assert_eq!(byte_pair_encode(&piece, &encoder), expected);
    assert_eq!(
        byte_pair_encode_reference(&piece, &encoder, &encoder),
        expected
    );
}

#[test]
fn test_repeated_ab() {
    let encoder = prop_encoder();
    let piece = b"ab".repeat(512);
    assert_eq!(
        byte_pair_encode(&piece, &encoder),
        byte_pair_encode_reference(&piece, &encoder, &encoder)
    );
}

#[test]
fn test_whole_piece_is_one_token() {
    let encoder = prop_encoder();
    // "abcd" is in the vocabulary, so the fast path returns its id alone.
    assert_eq!(byte_pair_encode(b"abcd", &encoder), vec![16]);
}

#[test]
fn test_single_byte_and_empty_agree_with_reference() {
    let encoder = prop_encoder();
    let pieces: [&[u8]; 3] = [b"", b"a", b"z"];
    for piece in pieces {
        assert_eq!(
            byte_pair_encode(piece, &encoder),
            byte_pair_encode_reference(piece, &encoder, &encoder),
            "piece {piece:?}"
        );
    }
}

#[test]
fn test_bytes_absent_from_vocab() {
    let encoder = prop_encoder();
    // 'z' and 0xFF are not in the vocabulary at all; the fallback path
    // drops them, which the new implementation must reproduce exactly.
    let pieces: [&[u8]; 4] = [b"azb", b"\xff\xfe", b"abzcd", b"zzz"];
    for piece in pieces {
        assert_eq!(
            byte_pair_encode(piece, &encoder),
            byte_pair_encode_reference(piece, &encoder, &encoder),
            "piece {piece:?}"
        );
    }
}

/// A vocabulary covering `a` and `c` (and nothing else, no merges) — used
/// to exercise `byte_pair_encode_pieces_seeded`'s `Unresolved` reporting.
fn ac_only_encoder() -> Encoder {
    let mut encoder = FxHashMap::default();
    encoder.insert(b"a".to_vec(), 0);
    encoder.insert(b"c".to_vec(), 1);
    encoder_from_owned(encoder)
}

#[test]
fn test_pieces_reports_unresolved_span() {
    let encoder = ac_only_encoder();
    assert_eq!(
        byte_pair_encode_pieces_seeded(b"abc", RankLookup::new(&encoder), &encoder, false),
        vec![
            Piece::Token(0),
            Piece::Unresolved { start: 1, len: 1 },
            Piece::Token(1),
        ]
    );
    // The preserved primitive still just drops the gap.
    assert_eq!(byte_pair_encode(b"abc", &encoder), vec![0, 1]);
}

#[test]
fn test_pieces_coalesce_consecutive_unresolved() {
    // Vocabulary missing both `b` and `c`.
    let mut encoder = FxHashMap::default();
    encoder.insert(b"a".to_vec(), 0);
    encoder.insert(b"d".to_vec(), 1);
    let encoder = encoder_from_owned(encoder);
    assert_eq!(
        byte_pair_encode_pieces_seeded(b"abcd", RankLookup::new(&encoder), &encoder, false),
        vec![
            Piece::Token(0),
            Piece::Unresolved { start: 1, len: 2 },
            Piece::Token(1),
        ]
    );
}

#[test]
fn test_pieces_unresolved_at_start_and_end() {
    let encoder = ac_only_encoder();
    // "bab" style: missing byte at the very start and the very end.
    assert_eq!(
        byte_pair_encode_pieces_seeded(b"bab", RankLookup::new(&encoder), &encoder, false),
        vec![
            Piece::Unresolved { start: 0, len: 1 },
            Piece::Token(0),
            Piece::Unresolved { start: 2, len: 1 },
        ]
    );
}

#[test]
fn test_pieces_full_coverage_has_no_unresolved() {
    let encoder = prop_encoder();
    for piece in [&b""[..], b"a", b"abcd", b"aabbccdd", b"dcbadcba"] {
        let pieces =
            byte_pair_encode_pieces_seeded(piece, RankLookup::new(&encoder), &encoder, false);
        assert!(
            pieces.iter().all(|p| matches!(p, Piece::Token(_))),
            "piece {piece:?} produced {pieces:?}"
        );
    }
}

#[test]
fn test_pieces_single_byte_and_empty_fast_paths() {
    let encoder = ac_only_encoder();
    let empty: Vec<Piece> = vec![];
    assert_eq!(
        byte_pair_encode_pieces_seeded(b"", RankLookup::new(&encoder), &encoder, false),
        empty
    );
    assert_eq!(
        byte_pair_encode_pieces_seeded(b"a", RankLookup::new(&encoder), &encoder, false),
        vec![Piece::Token(0)]
    );
    assert_eq!(
        byte_pair_encode_pieces_seeded(b"b", RankLookup::new(&encoder), &encoder, false),
        vec![Piece::Unresolved { start: 0, len: 1 }]
    );
}

proptest! {
    /// Single-map (tiktoken-style) path over a dense small alphabet, where
    /// merges actually chain.
    #[test]
    fn prop_matches_reference_single_map(
        piece in prop::collection::vec(prop::sample::select(vec![b'a', b'b', b'c', b'd']), 0..200)
    ) {
        let encoder = prop_encoder();
        prop_assert_eq!(
            byte_pair_encode(&piece, &encoder),
            byte_pair_encode_reference(&piece, &encoder, &encoder)
        );
    }

    /// Same path, but over arbitrary bytes so the unknown-byte fallback and
    /// the no-merge-possible cases are covered too.
    #[test]
    fn prop_matches_reference_arbitrary_bytes(
        piece in prop::collection::vec(any::<u8>(), 0..200)
    ) {
        let encoder = prop_encoder();
        prop_assert_eq!(
            byte_pair_encode(&piece, &encoder),
            byte_pair_encode_reference(&piece, &encoder, &encoder)
        );
    }

    /// Two-map (HuggingFace-style) path: merge ranks tie and disagree with
    /// the ids, so both the tiebreak and the map separation are exercised.
    #[test]
    fn prop_matches_reference_two_maps(
        piece in prop::collection::vec(prop::sample::select(vec![b'a', b'b', b'c', b'd', b'z']), 0..200)
    ) {
        let (merge_ranks, id_encoder) = prop_two_maps();
        prop_assert_eq!(
            byte_pair_encode_with_ranks(&piece, &merge_ranks, &id_encoder),
            byte_pair_encode_reference(&piece, &merge_ranks, &id_encoder)
        );
    }

    /// `byte_pair_encode_pieces_seeded` filtered down to its `Token` ids must
    /// agree with `byte_pair_encode_with_ranks` exactly — the latter is
    /// now defined in terms of the former, but this pins the equivalence
    /// as an independent property over arbitrary bytes (so the
    /// `Unresolved`-reporting fallback path is exercised too).
    #[test]
    fn prop_pieces_tokens_match_with_ranks(
        piece in prop::collection::vec(any::<u8>(), 0..200)
    ) {
        let encoder = prop_encoder();
        let tokens_only: Vec<u32> = byte_pair_encode_pieces_seeded(&piece, RankLookup::new(&encoder), &encoder, false)
            .into_iter()
            .filter_map(|p| match p {
                Piece::Token(id) => Some(id),
                Piece::Unresolved { .. } => None,
            })
            .collect();
        prop_assert_eq!(
            tokens_only,
            byte_pair_encode_with_ranks(&piece, &encoder, &encoder)
        );
    }

    /// Character-granular counterpart of the three properties above, over
    /// valid UTF-8 drawn from a mixed-width alphabet (1, 3 and 4 byte
    /// characters), against the character-seeded oracle.
    #[test]
    fn prop_char_seeded_matches_reference(
        chars in prop::collection::vec(
            prop::sample::select(vec!['a', 'b', '▁', '', '😀']), 0..200)
    ) {
        let (merge_ranks, id_encoder) = char_prop_maps();
        let text: String = chars.into_iter().collect();
        let piece = text.as_bytes();
        prop_assert_eq!(
            tokens_only(byte_pair_encode_pieces_seeded(piece, RankLookup::new(&merge_ranks), &id_encoder, true)),
            byte_pair_encode_reference_seeded(piece, &merge_ranks, &id_encoder, true)
        );
    }

    /// The two-byte index must answer exactly what the map answers.
    ///
    /// It fronts the map for a subset of keys, so it is only safe if it is
    /// indistinguishable from it — including the cases where the two could
    /// plausibly disagree: a two-byte key absent from the map, and a two-byte
    /// key the vocabulary maps to the `u32::MAX` sentinel, which the map path
    /// reports as unmergeable and the table stores as its own "absent" marker.
    #[test]
    fn prop_byte_pair_table_agrees_with_the_map(
        entries in prop::collection::vec(
            (prop::collection::vec(any::<u8>(), 1..5), any::<u32>()), 0..40),
        probes in prop::collection::vec(prop::collection::vec(any::<u8>(), 0..5), 1..40)
    ) {
        let map: Encoder = entries.into_iter().collect();
        let pairs = BytePairRanks::build(&map);
        let plain = RankLookup::new(&map);
        let fronted = RankLookup::with_pairs(&map, &pairs);
        for probe in &probes {
            prop_assert_eq!(fronted.get(probe), plain.get(probe), "diverged on {:?}", probe);
        }
    }

    /// The id-only entry point must equal the piece-reporting one filtered
    /// down to its tokens, on every input.
    ///
    /// `byte_pair_encode_ids_seeded` is not a wrapper — it has its own fast
    /// paths and its own collection loop, and it carries essentially all
    /// production traffic (every vocabulary without a byte fallback). The
    /// two implementations have to be checked against each other rather
    /// than assumed equal. Arbitrary bytes, so the unresolved-byte branch
    /// where they differ most is covered.
    #[test]
    fn prop_ids_seeded_matches_pieces_seeded(
        piece in prop::collection::vec(any::<u8>(), 0..200),
        char_granular in any::<bool>()
    ) {
        let (merge_ranks, id_encoder) = prop_two_maps();
        prop_assert_eq!(
            ids_seeded(&piece, &merge_ranks, &id_encoder, char_granular),
            tokens_only(byte_pair_encode_pieces_seeded(&piece, RankLookup::new(&merge_ranks), &id_encoder, char_granular
            ))
        );
    }

    /// Both merge-selection strategies must agree, on inputs that straddle
    /// the threshold that picks between them.
    ///
    /// The other properties compare against the oracle at whatever size
    /// proptest generates; this one forces the comparison to span
    /// `SCAN_SYMBOL_LIMIT` by construction — a piece is generated near the
    /// boundary and checked at lengths on both sides of it, so a divergence
    /// that only appears once the heap takes over cannot hide behind a
    /// generator that happens to favour short inputs.
    #[test]
    fn prop_selection_strategies_agree_across_the_threshold(
        piece in prop::collection::vec(
            prop::sample::select(vec![b'a', b'b', b'c', b'd']), 130..260)
    ) {
        let encoder = prop_encoder();
        for len in [32usize, 63, 64, 65, 129, piece.len()] {
            let slice = &piece[..len.min(piece.len())];
            prop_assert_eq!(
                byte_pair_encode(slice, &encoder),
                byte_pair_encode_reference(slice, &encoder, &encoder),
                "diverged at {} symbols", slice.len()
            );
        }
    }
}

/// Seeding a character whole must be the same computation as merging it up from
/// its bytes — which is a claim about the *vocabulary*, so it is checked against
/// a vocabulary built the way real ones are: by actually running BPE merges over
/// a corpus until the merge list contains tokens that straddle character
/// boundaries, which is exactly what makes the naive version of this unsound.
mod char_seeding {
    use super::*;
    use crate::core::bpe::ranks::PairRanks;
    use crate::core::byte_level::byte_level_encode;

    /// A corpus with three scripts and a lot of repetition, so the merges it
    /// trains include whole characters, multi-character words, and pieces that
    /// cut across character boundaries.
    const CORPUS: &[&str] = &[
        "中国人民",
        "中文字典",
        "人民日报",
        "日本語",
        "日本の中国",
        "the quick brown",
        "the中国",
        " 中国 the",
        "한국어",
        "한국 사람",
        "中国한국",
        "quick中",
        "языки",
        "я中国",
    ];

    /// One BPE training run: start from single bytes and merge the most frequent
    /// adjacent pair, assigning ids in merge order.
    ///
    /// Ids in merge order is what real vocabularies do and what lets rank and id
    /// be the same number, so the table under test takes its ordinary path.
    fn train(merges: usize) -> (Encoder, Encoder) {
        let mut words: Vec<Vec<Vec<u8>>> = CORPUS
            .iter()
            .map(|w| w.as_bytes().iter().map(|&b| vec![b]).collect())
            .collect();
        // The alphabet first: every byte value, so any input can be seeded.
        let mut raw: Vec<Vec<u8>> = (0..=u8::MAX).map(|b| vec![b]).collect();

        for _ in 0..merges {
            let mut counts: FxHashMap<(Vec<u8>, Vec<u8>), usize> = FxHashMap::default();
            for word in &words {
                for pair in word.windows(2) {
                    *counts
                        .entry((pair[0].clone(), pair[1].clone()))
                        .or_default() += 1;
                }
            }
            // Deterministic: most frequent, ties broken by the pair itself.
            let Some((best, _)) = counts
                .into_iter()
                .max_by(|a, b| a.1.cmp(&b.1).then_with(|| b.0.cmp(&a.0)))
            else {
                break;
            };
            let mut joined = best.0.clone();
            joined.extend_from_slice(&best.1);
            if raw.contains(&joined) {
                break;
            }
            raw.push(joined.clone());
            for word in &mut words {
                let mut at = 0;
                while at + 1 < word.len() {
                    if word[at] == best.0 && word[at + 1] == best.1 {
                        word[at] = joined.clone();
                        word.remove(at + 1);
                    } else {
                        at += 1;
                    }
                }
            }
        }

        let raw_encoder: Encoder = raw
            .iter()
            .enumerate()
            .map(|(id, bytes)| (bytes.as_slice(), id as u32))
            .collect();
        let mapped: Vec<(String, u32)> = raw
            .iter()
            .enumerate()
            .map(|(id, bytes)| (byte_level_encode(bytes), id as u32))
            .collect();
        let id_encoder: Encoder = mapped
            .iter()
            .map(|(text, id)| (text.as_bytes(), *id))
            .collect();
        (id_encoder, raw_encoder)
    }

    /// The vocabulary must actually contain the hazard, or the property below
    /// would pass by never meeting it.
    #[test]
    fn the_trained_vocabulary_contains_straddling_tokens() {
        let (_, raw_encoder) = train(400);
        let straddling = raw_encoder
            .keys()
            .filter(|key| {
                std::str::from_utf8(key).is_err()
                    && key[1..].iter().any(|&b| !(0x80..0xC0).contains(&b))
            })
            .count();
        assert!(
            straddling > 0,
            "the training corpus must produce tokens crossing character boundaries"
        );
    }

    #[test]
    fn seeding_characters_whole_matches_seeding_their_bytes() {
        let (id_encoder, raw_encoder) = train(400);
        // A separate rank map, as a model with its own `merges` list has: the
        // ranks happen to equal the ids here, which is what a vocabulary
        // numbered in merge order gives.
        let rank_map = id_encoder.clone();
        let table = PairRanks::build(&rank_map, &id_encoder, Some(&raw_encoder))
            .expect("the trained vocabulary is addressable by id");
        assert!(
            table.seeds_chars(),
            "some character of this vocabulary must be safe to seed whole"
        );

        // The filter has to be doing work in both directions, or the property
        // below would hold for reasons that say nothing about it: some character
        // must be vouched for and some must be refused.
        let multi_byte: Vec<&[u8]> = raw_encoder
            .keys()
            .filter(|key| {
                key.len() > 1
                    && std::str::from_utf8(key).is_ok_and(|text| text.chars().nth(1).is_none())
            })
            .collect();
        let vouched = multi_byte
            .iter()
            .filter(|key| table.char_seed(key) != u32::MAX)
            .count();
        assert!(vouched > 0, "no character was vouched for");
        assert!(
            vouched < multi_byte.len(),
            "every character was vouched for, so the safety test refused nothing"
        );

        let ranks = RankLookup::new(&rank_map).with_ids(Some(&table));
        let alphabet: Vec<&str> = vec![
            "", "", "", "", "", "", "", "", "", "", "я", "з", "ы", "the",
            "quick", " ", "中国", "日本", "한국",
        ];
        let mut runner = proptest::test_runner::TestRunner::deterministic();
        let strategy = proptest::collection::vec(0usize..alphabet.len(), 0..20);
        runner
            .run(&strategy, |picks| {
                let text: String = picks.iter().map(|&i| alphabet[i]).collect();
                let (mut chars, mut bytes) = (Vec::new(), Vec::new());
                byte_pair_encode_ids_seeded_into(
                    text.as_bytes(),
                    ranks,
                    &raw_encoder,
                    Seeding::RawChars,
                    &mut chars,
                );
                byte_pair_encode_ids_seeded_into(
                    text.as_bytes(),
                    ranks,
                    &raw_encoder,
                    Seeding::RawBytes,
                    &mut bytes,
                );
                prop_assert_eq!(chars, bytes, "diverged on {:?}", text);
                Ok(())
            })
            .expect("character seeding must equal byte seeding on every generated string");
    }
}

/// The entry point byte-fallback vocabularies take: ids when the piece needs no
/// fallback, pieces when it does.
///
/// Its whole reason to exist is that the two answers must be the same answer —
/// it takes the id-keyed merge on the strength of an argument (every symbol
/// resolved, so nothing downstream can fail to resolve) rather than by running
/// the piece-reporting merge and looking. These check the argument against the
/// implementation it replaces.
mod ids_or_pieces {
    use super::*;
    use crate::core::bpe::byte_pair_encode_ids_or_pieces;

    /// Run both paths over one piece and return them for comparison.
    fn both(piece: &[u8], seeding: Seeding) -> (Option<Vec<Piece>>, Vec<u32>, Vec<Piece>) {
        let (merge_ranks, id_encoder) = prop_two_maps();
        let table = PairRanks::build(&merge_ranks, &id_encoder, None)
            .expect("this vocabulary is addressable by id");
        let ranks = RankLookup::new(&merge_ranks).with_ids(Some(&table));

        let mut ids = Vec::new();
        let pieces = byte_pair_encode_ids_or_pieces(piece, ranks, &id_encoder, seeding, &mut ids);
        let reference = byte_pair_encode_pieces_seeded(
            piece,
            RankLookup::new(&merge_ranks),
            &id_encoder,
            seeding == Seeding::Chars,
        );
        (pieces, ids, reference)
    }

    /// A piece of characters the vocabulary has takes the id-keyed merge, and
    /// gets the piece-reporting merge's answer.
    #[test]
    fn a_representable_piece_is_answered_in_ids() {
        for piece in [&b"abab"[..], b"aaaa", b"abcd", b"a", b"", b"bbba"] {
            let (pieces, ids, reference) = both(piece, Seeding::Bytes);
            assert!(
                pieces.is_none(),
                "{piece:?} is representable and must not reach the fallback path"
            );
            assert_eq!(ids, tokens_only(reference), "diverged on {piece:?}");
        }
    }

    /// A piece with a character the vocabulary lacks reports pieces, writes no
    /// ids, and reports exactly what the piece-reporting merge would have.
    #[test]
    fn an_unrepresentable_piece_is_answered_in_pieces() {
        // `z` is in neither map, alone and in company.
        for piece in [&b"z"[..], b"az", b"zab", b"abzab", b"zz"] {
            let (pieces, ids, reference) = both(piece, Seeding::Bytes);
            assert!(
                ids.is_empty(),
                "{piece:?} needs the fallback, so nothing may be written to the id buffer"
            );
            assert_eq!(pieces, Some(reference), "diverged on {piece:?}");
        }
    }
}

proptest! {
    #![proptest_config(ProptestConfig::with_cases(400))]

    /// Over arbitrary bytes, the two answers agree — ids where the piece
    /// resolves, and the piece-reporting merge's own output where it does not.
    ///
    /// Arbitrary bytes rather than an alphabet, so most generated pieces take
    /// the fallback path and the boundary between the two is crossed
    /// constantly.
    #[test]
    fn prop_ids_or_pieces_matches_pieces_seeded(
        piece in prop::collection::vec(any::<u8>(), 0..200),
        char_granular in any::<bool>()
    ) {
        let (merge_ranks, id_encoder) = prop_two_maps();
        let table = PairRanks::build(&merge_ranks, &id_encoder, None)
            .expect("this vocabulary is addressable by id");
        let ranks = RankLookup::new(&merge_ranks).with_ids(Some(&table));
        let seeding = match char_granular {
            true => Seeding::Chars,
            false => Seeding::Bytes,
        };

        let mut ids = Vec::new();
        let pieces = byte_pair_encode_ids_or_pieces(&piece, ranks, &id_encoder, seeding, &mut ids);
        let reference = byte_pair_encode_pieces_seeded(
            &piece, RankLookup::new(&merge_ranks), &id_encoder, char_granular);

        match pieces {
            None => {
                prop_assert!(
                    !reference.iter().any(|p| matches!(p, Piece::Unresolved { .. })),
                    "answered in ids, but the piece merge found something unresolved"
                );
                prop_assert_eq!(ids, tokens_only(reference));
            }
            Some(pieces) => {
                prop_assert!(ids.is_empty(), "the fallback path must write no ids");
                prop_assert_eq!(pieces, reference);
            }
        }
    }
}