fix: sanitize invalid ids before fast decode - #64
Closed
froststeam wants to merge 1 commit into
Closed
Conversation
froststeam
force-pushed
the
fix/decode-sanitize-uint32
branch
3 times, most recently
from
August 10, 2026 10:44
de39114 to
6129f25
Compare
froststeam
force-pushed
the
fix/decode-sanitize-uint32
branch
from
August 10, 2026 10:59
6129f25 to
ba55287
Compare
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.
Background
fastokens.patch_transformers()replaces the Hugging Face tokenizer backend with the fastokens Python compatibility shim.The serving bug here is not on the normal decode path. It happens when invalid token IDs reach detokenization input, such as negative sentinels or values larger than
u32::MAX. In that case the native fast decode call can raise:The important part is that valid decode requests must stay on the fast path. We only need a fallback for malformed input.
Changes
decode()anddecode_batch()on the normal fast path._TokenizerShimthat only runs when the native decode path raisesOverflowError.[0, 2^32 - 1]and retry the decode.This keeps the fast path unchanged and makes malformed input fail open instead of crashing the detokenizer process.
Performance impact
I re-ran the valid-input benchmark against the current wheel and compared it with the baseline wheel built from the parent commit.
The current implementation stays on the same fast path for valid input, so there is no additional filtering or conversion work in the normal case. In local measurements, the valid-path numbers were effectively unchanged and the differences were within normal run-to-run variance.
Testing
Validated locally:
Smoke test on the built wheel: