Skip to content

Pieces of 500 bytes or more can encode differently from tiktoken: the heap merge breaks rank ties from the right #2

Open

Description

@waynehacking8

For a regex piece of 500 bytes or more, CoreBPE can return different ids from tiktoken. A long run of one character is enough: with cl100k_base, "a" + "-" * 500 + "b" already differs.

import riptoken, tiktoken

r = riptoken.get_encoding("cl100k_base")
t = tiktoken.get_encoding("cl100k_base")
for n in (499, 500, 501, 1000):
    s = "a" + "-" * n + "b"
    print(n, t.encode_ordinary(s) == r.encode_ordinary(s))

This prints True for 499 and False for the other three, with riptoken 0.2.4 (the newest on PyPI) and tiktoken 0.14.0. The crate also differs, on 0.3.0 and on main (4cbddaf882); see the details below.

The cause is the tie-break in byte_pair_merge_large, which handles pieces of LARGE_PIECE_THRESHOLD (500) bytes or more. Its heap holds (Reverse(rank), start), and BinaryHeap pops the largest entry, so among pairs with the same rank it merges the rightmost one first.

The short-piece path merges the leftmost, and so does tiktoken, whose Merge ordering breaks rank ties with other.start.cmp(&self.start). In a run of one character every pair has the same rank, so the whole run comes out differently. Long words can hit it too, when the same pair occurs more than once.

Moving start inside the Reverse fixes it:

-    let mut heap: BinaryHeap<(Reverse<Rank>, usize)> = BinaryHeap::with_capacity(n);
+    let mut heap: BinaryHeap<Reverse<(Rank, usize)>> = BinaryHeap::with_capacity(n);
-            heap.push((Reverse(rank), i));
+            heap.push(Reverse((rank, i)));
-    while let Some((Reverse(rank), start)) = heap.pop() {
+    while let Some(Reverse((rank, start))) = heap.pop() {
-                heap.push((Reverse(new_rank), start));
+                heap.push(Reverse((new_rank, start)));
-                heap.push((Reverse(new_rank), prev));
+                heap.push(Reverse((new_rank, prev)));

With it, the 0.3.0 crate gave the same ids as tiktoken-rs on every input in the details below, the crate's own tests still pass, and in a quick timing the long-piece path got no slower.

This matters for vLLM, whose Rust frontend loads tiktoken.model files such as Kimi K3's with riptoken 0.3.0. Kimi's own tokenizer is tiktoken on the same file and pattern. Through vLLM's tokenizer at that commit, "Hello" + " " * 501 + "world" and the same with 500 dashes already get different ids from Kimi's tokenizer.

I can open a PR with the change and a regression test if that helps.

What I compared

These run in Rust, so the reference is tiktoken-rs 0.9.1. Its BPE core is copied from tiktoken. The patched build is riptoken 0.3.0 with the diff above; main is 4cbddaf882.

Runs of one character. Each row tries every run length from 1 to N, each followed by x, \n, \t, \r\n or nothing. A count is how many of the N lengths give different ids, and a range means it varies with the suffix. The first differing input always has a 500-byte piece; it shows up as 498 to 501 because the suffix joins the run's piece, or the run's last space joins x. A few longer runs happen to give the same ids either way.

Vocabulary Run N Differ (0.3.0 and main) First differing length Differ (patched)
cl100k_base spaces 1,500 992 to 995 498 to 501 0
cl100k_base - 1,500 985 to 987 498 to 500 0
cl100k_base = 1,500 985 to 987 498 to 500 0
Kimi K3 - 1,500 985 to 987 498 to 500 0
Kimi K3 = 1,500 985 to 987 498 to 500 0
Kimi K3 spaces 6,000 5,478 to 5,481 498 to 501 0

Kimi K3 is moonshotai/Kimi-K3's tiktoken.model with the split pattern from its tokenization_kimi.py.

Random words: 300 strings of 500 to 2,999 random lowercase letters (one piece each). 0.3.0 and main differ on 179 of them with cl100k_base and on 181 with o200k_base; the patch on none. 300 random CJK strings of the same lengths matched on every build.

Timing, on an 800 KB input made only of long pieces (runs of 2,000 characters and 2,000-letter words), 31 encodes alternating between the two builds on one x86_64 desktop: median 67.7 ms on 0.3.0 and 61.9 ms with the patch for cl100k_base, 71.4 ms and 65.5 ms for o200k_base. I didn't run scripts/bench.py; the patch only touches the long-piece path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions