Skip to content

feat: support adaptive speculative decoding.#1876

Open
weizhehuang0827 wants to merge 2 commits into
xLLM-AI:mainfrom
weizhehuang0827:adaptive-speculatuve
Open

feat: support adaptive speculative decoding.#1876
weizhehuang0827 wants to merge 2 commits into
xLLM-AI:mainfrom
weizhehuang0827:adaptive-speculatuve

Conversation

@weizhehuang0827

@weizhehuang0827 weizhehuang0827 commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Description

实现自适应投机推理的 validate 前剪枝优化。在每个 decode step 的 draft 阶段结束后、validate 阶段开始前,根据 draft model 产出的 token 概率动态决定每个 sequence 保留多少 speculative tokens 参与 validate,裁剪掉置信度低、预期收益为负的 draft tokens。

核心算法:将 batch 内所有请求所有位置的候选 token 按 path probability(从第一个 token 到当前位置的累积概率)从大到小排列,从最高概率开始依次尝试加入验证集。每加入一个 token,基于 profile 拟合的时间预测模型更新估计吞吐(expected_emitted_tokens / estimated_time)。若加入后估计吞吐不增反降则停止加入该 token。最终每个 sequence 可能有不同的验证长度。

validate 阶段利用 ATB attention kernel 的 variable q_len 能力,只对每个 sequence 实际保留的 tokens 做计算,真正减少 validate 的 attention 和 FFN 计算量。

此方案参考 DSPark 自适应投机推理算法(区别主要在于计算接受概率时DSpark用的是模型本身的置信度头,我们这里先使用draft模型输出的logits),但还未专门针对graph和ZOS做适配。后续还可以进一步支持:(1)对draft的early stop;(2)根据近期各位置接受率动态调整num_speculative_tokens

Related Issues

N/A

Change Type

  • Bug fix
  • New feature
  • Performance improvement
  • Refactor
  • Documentation
  • Test
  • Build or CI

Pull Request Checklist

PR Title and Commit Messages

  • The PR title and each commit message follow the xLLM commit format: <type>: <subject>.

Pre-commit Checks

  • I have installed pre-commit by running pip install pre-commit or an equivalent command.
  • I have installed the hooks with pre-commit install.
  • I have run pre-commit run --all-files and fixed any reported issues.

Self Review

  • I have self-reviewed the code according to .agents/skills/code-review/references/custom-code-style.md, especially code written or assisted by AI.
  • I have rebased this PR onto the latest main branch.

Build and Test Coverage

  • Tests have been added or updated as needed.
  • CUDA: python setup.py build test has passed on a CUDA machine.
  • NPU: python setup.py build test has passed on an NPU machine.
  • MLU: python setup.py build test has passed on an MLU machine.

Reviewer Notes

测试数据(ShareGPT + DeepSeek-V3.2 MTP,TP=16 NPU, batch_size=32):

SL Metric Static Adaptive Δ
3 output_throughput 245.1 241.7 -1.4%
3 acceptance_rate 54.4% 59.6% +9.6%
5 output_throughput 238.0 309.5 +30%
5 acceptance_rate 34.1% 52.5% +54%
7 output_throughput 221.2 262.5 +18.7%
7 acceptance_rate 25.7% 44.5% +73%

SL=5/7 时 adaptive 显著优于 static(吞吐 +1830%,acceptance_rate +5073%)。SL=3 时裁剪空间小,两者持平。关闭 enable_adaptive_speculative_decode 后完全不影响 static 路径。

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request implements Adaptive Speculative Decoding for MTP, introducing an AdaptiveSpeculativeController to dynamically prune draft tokens before target validation based on path probabilities and a validate-time predictor. It also adds a SpeculativeProfileRegistry and integrates profiling within the ProfileManager. The review feedback is highly constructive, identifying an opportunity to avoid wasteful GPU kernel launches on the hot path when destination indices are empty, and recommending the cleanup of the unused target_validate_time_by_sl parameter across several controller and worker files.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread xllm/core/runtime/mtp_worker_impl.cpp Outdated
Comment thread xllm/core/runtime/mtp_worker_impl.cpp
Comment thread xllm/core/runtime/mtp_worker_impl.h Outdated
Comment thread xllm/core/runtime/adaptive_speculative_controller.h Outdated
Comment thread xllm/core/runtime/adaptive_speculative_controller.cpp Outdated
Comment thread xllm/core/runtime/mtp_worker_impl.cpp Outdated
Comment thread xllm/core/runtime/mtp_worker_impl.cpp Outdated
@weizhehuang0827 weizhehuang0827 force-pushed the adaptive-speculatuve branch 2 times, most recently from a50c467 to 7973a32 Compare July 8, 2026 12:40
…ning.

Implement adaptive speculative decode that prunes low-confidence draft
tokens before target validation, reducing validate compute via ATB
variable-length q_len support. All ranks compute pruning independently
(no cross-rank broadcast needed) since draft probs are deterministic.
Move AdaptiveSpeculativeController and SpeculativeProfileRegistry from
runtime/ to framework/speculative/ to keep runtime/ focused on
worker/executor concerns. Update all include paths and CMake targets.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

remove and add design docs to xllm-docs.

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.

2 participants