Skip to content

Conversation

windreamer
Copy link
Collaborator

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

  1. Build-system

    • Add xgrammar as a header-only dependency via CMake FetchContent (CUDA & Python bindings disabled).
    • Export xgrammar::tokenizer_info and xgrammar::grammar_compiler symbols under lmdeploy::xgrammar.
  2. Core C++ changes

    • DynamicDecodeLayer pipeline extended with two new layers:
      • GuidedDecodeMaskLayer: in setup() compiles / reuses grammar → builds per-request token bitmask; in forward() launches a light CUDA kernel to mask disallowed logits to -INF.
      • GuidedDecodeUpdateLayer: in forward() calls matcher->AcceptToken(output_id) to advance the FSM.
    • Grammar compiler cache (LRU, keyed by schema hash) shared across all sessions to avoid re-compilation.
  3. Python frontend

    • Re-use existing guided_decoding utilities from PyTorch backend; no new API surface.
    • turbo.TurboMindEngine now accepts the same response_format= / guided_json= / guided_choice= arguments.

Checklist

  • Pre-commit hooks (clang-format, flake8, mypy) passed.
  • Document updated

@windreamer windreamer changed the title Guided decoding with xgrammar [WIP] Guided decoding with xgrammar Sep 12, 2025
@windreamer windreamer force-pushed the guided_decoding_with_xgrammar branch 3 times, most recently from 8b3e766 to 8fd6d05 Compare September 12, 2025 09:44
@shell-nlp
Copy link
Contributor

good job!

@windreamer windreamer force-pushed the guided_decoding_with_xgrammar branch 25 times, most recently from 0362250 to 8bcbfff Compare September 22, 2025 12:41
Comment on lines 39 to 43
layers_.emplace_back(new LogitsProcessorLayer<float>{param});
layers_.emplace_back(new GuidedDecodeMaskLayer<float>{param});
layers_.emplace_back(new SamplingLayer<float>{param});
layers_.emplace_back(new GuidedDecodeUpdateLayer<float>{param});
layers_.emplace_back(new StopCriteriaLayer<float>{param});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sampling-related classes are declared as templates, but the template parameter T does not appear to be utilized in any of the following:

  • Member variable
  • Member function
  • Base class
    how about remove the templates? @lzhangzz @irexyc any comments?

@lvhan028
Copy link
Collaborator

cc @zhulinJulia24 may consider CI for guided decoding functions.
Here is the guide https://lmdeploy.readthedocs.io/en/latest/advance/structed_output.html

@windreamer windreamer force-pushed the guided_decoding_with_xgrammar branch from bf3a8ea to 39b4d13 Compare September 24, 2025 16:46
@windreamer
Copy link
Collaborator Author

What's the size of whl file if this PR is applied?

 ls -alh lmdeploy-0.10.0-cp310-cp310-linux_x86_64.whl
-rw-rw-r-- 1 tianzhongbo tianzhongbo 92M Sep 25 09:07 lmdeploy-0.10.0-cp310-cp310-linux_x86_64.whl

It seems we will increase the package size from 79M to 92M, which is 17%

@windreamer windreamer force-pushed the guided_decoding_with_xgrammar branch from b75e4c9 to 2355af6 Compare September 25, 2025 04:41
@windreamer windreamer force-pushed the guided_decoding_with_xgrammar branch from 8413618 to 3c4cbdb Compare September 26, 2025 01:18
@windreamer
Copy link
Collaborator Author

Also done replace outdated outlines with xgrammar in PyTorch Engine. And this enabled us to:

  • No longer need to restrict numpy<2
  • No buggy pyairports packages as dependencies

@windreamer windreamer requested a review from grimoire September 28, 2025 09:06
@windreamer windreamer linked an issue Sep 28, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] turbomind后端是否会支持guided_decoding
4 participants