Skip to content

fix: break rank ties from the left in the large-piece merge - #3

Open
waynehacking8 wants to merge 3 commits into
daechoi:mainfrom
waynehacking8:heap-merge-tie-break
Open

waynehacking8 wants to merge 3 commits into
daechoi:mainfrom
waynehacking8:heap-merge-tie-break

Conversation

@waynehacking8

@waynehacking8 waynehacking8 commented Nov 7, 2026 •

Copy link
Copy Markdown

Fixes #2.

byte_pair_merge_large, which handles pieces of 500 bytes or more, kept (Reverse(rank), start) in a BinaryHeap. The heap pops the largest entry, so among pairs of equal rank it merged the rightmost first, while the short-piece path and tiktoken merge the leftmost. The heap entry is now Reverse((rank, start)), which pops the lowest rank and, among equal ranks, the lowest start.

The new unit test, large_piece_breaks_rank_ties_from_the_left, runs both paths on runs of 3, 5 and 7 a bytes with a vocab whose only merge is aa. It fails before this change: for 3 bytes the heap path returns [97, 300] and the short-piece path [300, 97]. It passes after. long_runs_merge_equal_rank_pairs_from_the_left in tests/integration.rs encodes runs of 499, 500, 501 and 1,001 a bytes through encode_ordinary, so the switch to the heap path at 500 bytes is covered through the public encoder; before this change it fails on the 501-byte run. cargo test, cargo clippy --all-targets -- -D warnings and cargo fmt --check pass.

I also compared riptoken with this src/lib.rs change against tiktoken-rs 0.9.1, with a small probe outside the repo (not part of this PR). Each cell counts inputs whose token ids differ from tiktoken-rs:

input before after
cl100k_base: a run of space, - or = of every length from 1 to 1,500, then one of five suffixes 985 to 995 of the 1,500 lengths, for each character and suffix 0
cl100k_base: 300 random Latin strings of 500 to 2,999 characters 179 of 300 0
o200k_base: the same 300 strings 181 of 300 0

Random Han strings of the same lengths matched before and after. A difference needs equal-rank pairs that overlap, as in aaa, so long Latin words were affected too, not only runs of one character. The heap path has been there since 0.1.0, so for affected input the ids change from every earlier release and now match tiktoken; ids stored before this fix will differ. The fix is recorded under Unreleased in the CHANGELOG.

The long-piece path did not get slower. Median of 31 encodes of 800,600 bytes:

encoding before after
cl100k_base 67.7 ms 61.9 ms
o200k_base 71.4 ms 65.5 ms

byte_pair_merge_large keeps (Reverse(rank), start) in a BinaryHeap,
which pops the largest entry, so among pairs of equal rank it merged
the rightmost first. The Vec path and tiktoken merge the leftmost. In
a run of one character every pair has the same rank, so pieces of 500
bytes or more could encode differently from tiktoken, e.g. cl100k_base
on "a" + "-" * 500 + "b" (daechoi#2).

Keying the heap on Reverse((rank, start)) pops the lowest rank and,
among equal ranks, the lowest start.

The new test checks the heap path against the Vec path on runs of 3,
5 and 7 bytes with one merge; it fails before this change ([97, 300]
against [300, 97] for 3 bytes).

Fixes daechoi#2

Signed-off-by: waynehacking8 <[email protected]>
The earlier test calls byte_pair_merge_large directly. This one encodes
runs of 499, 500, 501 and 1001 bytes through the public encoder, so the
500-byte switch to the heap path is exercised as users hit it (daechoi#2). It
fails before the fix on the 501-byte run ([0, 1, ...] against
[1, ..., 1, 0]).

The changelog entry now notes that token ids for such inputs change
from every earlier release.

Signed-off-by: waynehacking8 <[email protected]>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant