Repository navigation
fix: break rank ties from the left in the large-piece merge - #3
Open
waynehacking8 wants to merge 3 commits into
Open
waynehacking8 wants to merge 3 commits into
waynehacking8 wants to merge 3 commits into
Conversation
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]>
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2.
byte_pair_merge_large, which handles pieces of 500 bytes or more, kept(Reverse(rank), start)in aBinaryHeap. 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 nowReverse((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 7abytes with a vocab whose only merge isaa. 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_leftintests/integration.rsencodes runs of 499, 500, 501 and 1,001abytes throughencode_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 warningsandcargo fmt --checkpass.I also compared riptoken with this
src/lib.rschange 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:-or=of every length from 1 to 1,500, then one of five suffixesRandom 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: