-
Notifications
You must be signed in to change notification settings - Fork 610
Guided decoding with xgrammar #3965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Guided decoding with xgrammar #3965
Conversation
8b3e766
to
8fd6d05
Compare
good job! |
0362250
to
8bcbfff
Compare
8413618
to
3c4cbdb
Compare
Also done replace outdated
|
torchvision>=0.18.1,<0.23.0 | ||
transformers | ||
uvicorn | ||
xgrammar |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does xgrammar
support npu?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No,but currently XGrammar in PyTorch engine uses cpu kernels instead of gpu ones. And it seems also works in arm64 (untested)
1a55fc2
to
3a9b7ab
Compare
I don't know much about guided decoding. But I think there are bugs in the pytorch implementation (from the main branch) The matcher is maintained in instances of |
You are right! It is a bit tough... |
Motivation
LMDeploy’s TurboMind backend is the fastest inference stack in the ecosystem, yet it still lacks Guided Decoding – a feature that is already available in the PyTorch backend and heavily requested by the community.
This PR closes the gap by bringing token-level, C++ native Guided Decoding to TurboMind while keeping the API 100 % compatible with the existing PyTorch backend.
The implementation is built on xGrammar (Apache-2.0), a high-performance C++ library that compiles JSON / Choice / Regex grammars into token FSMs and applies them with negligible overhead.
Modification
Build-system
xgrammar
as a header-only dependency via CMakeFetchContent
(CUDA & Python bindings disabled).xgrammar::tokenizer_info
andxgrammar::grammar_compiler
symbols underlmdeploy::xgrammar
.Core C++ changes
DynamicDecodeLayer
pipeline extended with two new layers:GuidedDecodeMaskLayer
: insetup()
compiles / reuses grammar → builds per-request token bitmask; inforward()
launches a light CUDA kernel to mask disallowed logits to-INF
.GuidedDecodeUpdateLayer
: inforward()
callsmatcher->AcceptToken(output_id)
to advance the FSM.Python frontend
guided_decoding
utilities from PyTorch backend; no new API surface.turbo.TurboMindEngine
now accepts the sameresponse_format=
/guided_json=
/guided_choice=
arguments.Checklist