fix(formatron): handle duplicate BPE token IDs and kbnf mask/accept inconsistency#170
Open
lesj0610 wants to merge 1 commit intoturboderp-org:masterfrom
Open
fix(formatron): handle duplicate BPE token IDs and kbnf mask/accept inconsistency#170lesj0610 wants to merge 1 commit intoturboderp-org:masterfrom
lesj0610 wants to merge 1 commit intoturboderp-org:masterfrom
Conversation
…nconsistency
Two related fixes for GPT-2 byte-level BPE tokenizers (e.g. EXAONE 4.x):
1. create_engine_vocabulary(): back-fill duplicate token IDs
get_vocab_dict() builds a {str: int} dict where the last ID wins for
duplicate token strings, silently dropping earlier IDs from new_vocab.
When the model samples one of the dropped IDs, kbnf raises
"The input token id is rejected", crashing generation.
Fix: rebuild a str->bytes lookup from new_vocab and back-fill any token
ID absent from new_vocab by reusing the bytes of its duplicate string.
2. accept_token(): guard against kbnf mask/accept inconsistency
kbnf <=0.4.2 can report a token as allowed via compute_allowed_tokens()
but then reject the same token in try_accept_new_token(), raising
ValueError and crashing the process. Wrapping the call in try/except
and re-raising turns the crash into a clean per-request abort.
This defensive pattern remains safe after a kbnf-side fix.
Note: kbnf >=0.5.7 resolves the underlying inconsistency.
A pre-built wheel for Linux x86_64 is available at:
https://github.com/lesj0610/kbnf/releases/tag/v0.5.7
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Problem
Two related bugs affect constrained generation (
json_schema,regex_pattern,grammar_string) with GPT-2 byte-level BPE tokenizers (e.g. EXAONE 4.x, GPT-J, GPT-NeoX, BLOOM):Bug 1 — Duplicate token IDs dropped from vocabulary
get_vocab_dict()builds a{str: int}dict comprehension; when multiple token IDs share the same piece string, only the last ID survives. Earlier IDs are silently absent fromnew_vocab. When the model samples one of these dropped IDs, kbnf raises:This crashes the generation loop.
Bug 2 — kbnf mask/accept inconsistency (kbnf ≤ 0.4.2)
compute_allowed_tokens()can report a token as valid whiletry_accept_new_token()subsequently rejects the same token withValueError. In kbnf ≤ 0.4.2 this is triggered by GPT-2 special tokens (e.g.[PAD], id=0, bytes=b'[PAD]') whose literal ASCII bytes incidentally match valid grammar positions (e.g. inside a JSON string value).The rejection propagates uncaught, crashing the calling process.
Fixes
1.
create_engine_vocabulary()— back-fill duplicate token IDsBuild a
str → byteslookup fromnew_vocab, then iterate the full vocabulary and assign bytes for any ID that was dropped due to string deduplication.2.
accept_token()— defensivetry/exceptaround kbnf callWrap
self._formatter.accept_token(token)intry/except ValueErrorand re-raise. This turns a process crash into a clean per-request abort that callers can handle gracefully.This guard remains safe and beneficial even after a kbnf-side fix: it converts any future unforeseen mask/accept inconsistency into a recoverable error rather than a crash.
kbnf version note
kbnf ≥ 0.5.7 resolves the underlying mask/accept inconsistency in the Rust engine. The PyPI package is currently at 0.4.2; a pre-built wheel for Linux x86_64 is available at:
https://github.com/lesj0610/kbnf/releases/tag/v0.5.7
The
setup.pycomment points to this wheel until the upstream author publishes 0.5.7 to PyPI.Affected models
Any model using a GPT-2 byte-level BPE tokenizer where special tokens are stored as literal ASCII strings — approximately 10–20% of open-source LLMs (EXAONE, GPT-J, GPT-NeoX, BLOOM, some Falcon variants).
Testing
Verified with EXAONE-4.0.1-32B +
json_schemaconstrained generation: 10/10 requests succeed with kbnf 0.5.7; previously crashed on ~1 in 5 requests with kbnf 0.4.2.