fix(prepare): use raw byte length for BPB token_bytes — avoids UTF-8 replacement inflation#385
Open
warren618 wants to merge 2 commits intokarpathy:masterfrom
Open
fix(prepare): use raw byte length for BPB token_bytes — avoids UTF-8 replacement inflation#385warren618 wants to merge 2 commits intokarpathy:masterfrom
warren618 wants to merge 2 commits intokarpathy:masterfrom
Conversation
The previous approach decoded each token via tiktoken and then re-encoded to UTF-8 to get byte length. For BPE tokens whose raw bytes are not valid standalone UTF-8 (e.g. a single continuation byte 0x80 = 1 byte), tiktoken decode produces the replacement character U+FFFD which encodes to 3 UTF-8 bytes. This inflates the byte-count denominator in BPB, producing artificially lower (better-looking) scores. Use mergeable_ranks directly to get the true raw byte length of each token, avoiding the decode/re-encode roundtrip entirely.
svlandeg
reviewed
Mar 22, 2026
Per reviewer feedback — this file was not part of the fix.
prathamesh-lang
approved these changes
Mar 23, 2026
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.
Summary
Fixes #384
The BPB token byte count is computed by decoding each token via tiktoken and re-encoding to UTF-8. For tokens whose raw bytes are not valid standalone UTF-8, tiktoken replaces them with U+FFFD (3 bytes), inflating the byte count and producing artificially lower BPB scores.
Fix
Use
mergeable_ranksdirectly to get the true raw byte length, bypassing the decode/re-encode roundtrip:Why this matters
BPB is the core metric driving the entire autoresearch loop. An inflated byte count denominator means experiments appear to perform better than they actually do.