Reimplement guided decoding with xgrammar for PyTorch Engine#4028
Merged
lvhan028 merged 5 commits intoInternLM:mainfrom Oct 15, 2025
Merged
Reimplement guided decoding with xgrammar for PyTorch Engine#4028lvhan028 merged 5 commits intoInternLM:mainfrom
lvhan028 merged 5 commits intoInternLM:mainfrom
Conversation
893563c to
0b2c106
Compare
2 tasks
2a3bbdf to
18628b4
Compare
65afd9b to
443db85
Compare
grimoire
reviewed
Oct 14, 2025
…SamplingInputs and refater the code
d4b6ddb to
b7c5426
Compare
This was referenced Oct 16, 2025
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.
Motivation
The original outlines-based guided decoding in the PyTorch engine has three major problems:
Token-level mismatch & poor performance
Outlines works on characters, not on tokens.
Outdated & incompatible dependency
outlines<0.1.0which is more than one year old and hard-coupled to numpy 1.x.Life-cycle bug
xgrammar is a token-level GPU-native grammar engine:
Modification
guided_process.py
GuidedDecodingManagerthat wraps xgrammar.compile_json_schema()/compile_regex_grammar()allocate_token_bitmask()/apply_token_bitmask_inplace()session_id + seq_idprocessor cachelogits_process.py
_guided_sampling,guided_input_ids, …)FusedLogitsProcessorreceives aGuidedDecodingManagerinstanceforward():– batch-allocate one bitmask tensor
– fill it for every guided sequence
– apply in-place on GPU
accept_token()advances each matchermodel_agent.py / sampling.py
model_agentkeeps the singletonGuidedDecodingManagerARSamplingStrategybuildssession_ctx(session/seq IDs) andsession_to_cleanuplistSamplingInputscarries the two new fields instead ofguided_input_idsengine.py
end_session()now callssampling_strategy.on_session_end()→session_to_cleanup→ next forward deletes the processors, guaranteeing immediate release.requirements
outlines<0.1.0, addxgrammarfor all backends (cuda/rocm/ascend/camb/maca).tests