Repository navigation
fix(dsls): keep CFG slot content from running past its end tag - #192
Merged
Merged
Conversation
The GIM tags are plain-text tokens, so a slot terminal such as /toxic .*/ or /[^,]+/ can match "<|/MASKED|>" itself. With the lazy suffix, whenever the slot regex is not yet satisfied at the end tag, llguidance keeps the end tag (and every following tag) inside the slot. Generation then ends on the vLLM stop string <|/GIM_RESPONSE|> while the grammar is still in that slot, and the non-strict parser accepts the unconstrained text. This produced regex violations and out-of-order tag ids under output_type="cfg". - Intersect every slot terminal with NO_MAGIC, which forbids the magic strings the parser treats as structure (all but "|>"). - Translate Python regex escapes llguidance (Rust regex) reads differently: escaped non-ASCII/punctuation literals such as a fullwidth parenthesis, \< and \>, and unescaped "/" that ended the lark literal early. 20/961 GIMBench-regex-match queries failed to compile before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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
Under
output_type="cfg", slot contents could violate their regex (e.g. regextoxic .*filled withprosthetics), and some responses failed to parse withTag ids should be in order. This PR makes the compiled CFG actually enforce each slot's regex, and translates Python regex escapes that llguidance reads differently.Root cause
<|/MASKED|>is<|/MASKED|>for GIM-4B), so a slot terminal such as/toxic .*/or/[^,]+/can match<|/MASKED|>itself. Withm_k[capture, suffix="<|/MASKED|>"], the slot ends at the end tag only if the regex is already satisfied. Otherwise llguidance keeps the end tag, and every tag after it, inside that slot. Generation then stops on the vLLM stop string<|/GIM_RESPONSE|>while the grammar is still in the slot, and the non-strict parser accepts the unconstrained text. Replaying such a response token by token throughLLMatcherwith the GIM-4B tokenizer shows that every token is allowed and the grammar does not end in an accepting state./closes the lark/.../literal early. Python accepts escaped non-ASCII characters such as\(or\。, but Rustregexrejects them, and it reads\</\>as word boundaries. 20 of the 961GIMBench-regex-matchqueries failed withInvalid CFG grammar.Changes
build_cfgnow intersects every slot terminal with aNO_MAGICterminal:T_k: /regex/ & NO_MAGIC.NO_MAGICforbids the magic strings thatMaskedTagalready rejects in content (all of them except|>).to_llguidance_regex()strips the backslash from escaped non-alphanumeric literals and always writes/as\/. Escaped ASCII letters and digits (\d,\w,一, ...) and regex metacharacters are kept as they are.LLMatcherwith a byte-level tokenizer.Verification
pytest: 64 passed; main passes 61, and the 3 extra are the new tests. The 9tests/models/test_vllm_offline.pytests fail the same way on main in an environment withoutvllm.ruff check,ruff format --checkandmypy srcpass.All 961
GIMBench-regex-matchqueries now compile, up from 941. On 898 real (regex, content) pairs from past result files, Pythonre.fullmatchand llguidance agree 100%.Mask computation cost is unchanged (about 0.4 ms/token with the GIM-4B vocabulary).
GIMBench rerun:
Sculpt-AI/GIM-4B,GIMBench-regex-matchfirst 100 items,vllm-offline, cfg, vLLM 0.19.1 with theguidancebackend. Both runs used gimkit 0.1.1; the "after" run only swaps in thisdsls.py, which is otherwise identical between 0.1.1 and main.max_tokens=8192After the fix, every remaining miss comes from a generation that hit the token budget. The model keeps trying to close a slot whose regex is not satisfied yet (the regex is not shown in the prompt), so it is no longer a structural violation.
Notes (not changed here)
finish_reason == "length") are still silently repaired by the non-strictinfill. Callers that need the guarantee should treat them as failures.🤖 Generated with Claude Code