fix: handle invalid decode ids in native binding - #65
Merged
AlonKejzman merged 4 commits intoAug 10, 2026
Conversation
…r-only The native fallback only caught OverflowError and then re-extracted the ids as Vec<i64>. That still aborted decode on inputs it was meant to tolerate: - a float such as -inf/inf/nan (or any non-integer) fails Vec<u32> extraction with TypeError, which was re-raised; - an int beyond i64 (e.g. 2**100) survived the OverflowError branch only to overflow again during the Vec<i64> re-extraction. Broaden the fallback: on any rejection of the fast Vec<u32> extraction, iterate the sequence element-wise, keep the elements that extract as an i64 in [0, u32::MAX], and skip the rest. Floats, NaN, +/-inf, and >i64 ints are now dropped like out-of-range ints instead of crashing the decode. The all-valid fast path (one bulk Vec<u32> extraction) is unchanged, so there is no cost on the common path. Adds native tests (no transformers dependency) covering the float/inf/nan and >i64 cases the previous fallback could not handle. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Collaborator
|
Thank you for your contribution! |
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
AlonKejzman
approved these changes
Aug 10, 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.
Background
fastokens.patch_transformers()swaps the Hugging Face tokenizer backend for the native fastokens implementation.The crash happens only when invalid decode IDs reach detokenization, for example negative sentinels or values larger than
u32::MAX. In that case the native conversion path can raise:That should not take down the detokenizer process. The common case, where all token IDs are valid, should stay on the same fast path.
What changed
_fast.decode()/_fast.decode_batch().Vec<u32>extraction.OverflowError, retry by extracting signed integers and dropping IDs outside[0, 2^32 - 1].Why this shape
This is the narrowest place to fix the bug:
Performance
The common path still uses the normal
Vec<u32>extraction, so there is no extra pre-filtering pass for valid inputs.I re-ran local decode microbenchmarks against the baseline wheel. The numbers stayed in the same range, and the key property is that the fallback only runs on the exceptional overflow path, not on the hot path.
Testing
Validated locally:
Smoke test on the built wheel: