Skip to content

[XPU][Speculative Decoding] Enable CudaGraph capture for MTP draft model#8061

Merged
Jiang-Jia-Jun merged 2 commits into
PaddlePaddle:developfrom
Clarity256:feature/xpu-mtp-cudagraph-capture
Jul 3, 2026
Merged

[XPU][Speculative Decoding] Enable CudaGraph capture for MTP draft model#8061
Jiang-Jia-Jun merged 2 commits into
PaddlePaddle:developfrom
Clarity256:feature/xpu-mtp-cudagraph-capture

Conversation

@Clarity256

@Clarity256 Clarity256 commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Motivation

  1. Draft model 前向推理启用 step_use_cudagraph 门控逻辑,并在 multi-step 执行中仅对首步进行 capture。
  2. Draft model 推理路径中传递 forward_metause_cudagraphxpu_pre_process,确保 cu_seqlens_q_output / batch_id_per_token_output 在 cudagraph 模式下使用 copy_ 原地更新,保证 tensor 地址稳定性。
  3. 新增 padding_cudagraph_inputs() 方法处理 draft model 的 buffer padding,并在 graph replay 时按 real_token_num 切片 model output。
  4. Target model 侧投机解码 warmup 流程适配(capture size 计算、accept_all_drafts 参数传递、TP>1 下 expected_decode_len 修正)。
  5. padding_sampling_params(Python 侧 CPU 实现)替换为 build_sampling_params XPU 自定义算子([XPU][OP] Add build_sampling_params kernel for MTP speculative decoding #8032),在算子内部完成 infer_seed 的原地更新,避免在 cudagraph 外额外操作。
  6. increment_value 改为与投机解码 token 数联动((num_speculative_tokens + 1) * 4)。
  7. Draft model 中 last_seq_lens_this_time 使用 copy_() 替代 clone(),避免 CUDAGraph replay 时产生新 tensor 导致内存持续增长。

Modifications

  • fastdeploy/spec_decode/mtp_xpu.py:draft model 启用 step_use_cudagraph 门控;_propose 新增 cudagraph padding 逻辑与 output slicing;_initialize_forward_meta 传递 cudagraph 参数;last_seq_lens_this_time 改为 copy_() 原地更新。
  • fastdeploy/worker/xpu_model_runner.pyincrement_value 与投机解码 token 数联动;warmup capture 流程适配 speculative decoding;infer_seed 更新移入 build_sampling_params 算子内部;draft model propose 传递 step_use_cudagraph;修正 TP>1 时 dummy_prefill_inputsexpected_decode_len
  • fastdeploy/model_executor/layers/sample/sampler.pyforward_xpu 改用 build_sampling_params XPU 算子替代 padding_sampling_params;新增 increment_value 参数。
  • fastdeploy/model_executor/xpu_pre_and_post_process.py:cudagraph 模式下改用 copy_ 原地更新 cu_seqlens_q_outputbatch_id_per_token_output,保证 graph 捕获的 tensor 地址稳定。
  • tests/xpu_ci/4cards_cases/run_mtp_cudagraph.py → test_mtp_cudagraph.py:重命名测试脚本以符合 CI 命名规范。

Usage or Command

Accuracy Tests

  • MTP with CUDAGraph:输出与参考结果一致(见 PR 截图)
mtp_with_cudagraph - MTP without CUDAGraph:输出与参考结果一致(见 PR 截图) mtp_without_cudagraph

Checklist

  • Add at least a tag in the PR title.
    • Tag list: [[FDConfig],[APIServer],[Engine], [Scheduler], [PD Disaggregation], [Executor], [Graph Optimization], [Speculative Decoding], [RL], [Models], [Quantization], [Loader], [OP], [KVCache], [DataProcessor], [BugFix], [Docs], [CI], [Optimization], [Feature], [Benchmark], [Others], [XPU], [HPU], [GCU], [DCU], [Iluvatar], [Metax]]
    • You can add new tags based on the PR content, but the semantics must be clear.
  • Format your code, run pre-commit before commit.
  • Add unit tests. Please write the reason in this PR if no unit tests.
  • Provide accuracy results.
  • If the current PR is submitting to the release branch, make sure the PR has been submitted to the develop branch, then cherry-pick it to the release branch with the [Cherry-Pick] PR tag.

@CLAassistant

CLAassistant commented Jun 17, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@Clarity256
Clarity256 force-pushed the feature/xpu-mtp-cudagraph-capture branch from 9e45d1d to 72c0f92 Compare June 17, 2026 05:28
PaddlePaddle-bot

This comment was marked as outdated.

PaddlePaddle-bot

This comment was marked as outdated.

@codecov-commenter

codecov-commenter commented Jun 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 38 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@7a60f79). Learn more about missing BASE report.

Files with missing lines Patch % Lines
fastdeploy/worker/xpu_model_runner.py 0.00% 17 Missing ⚠️
fastdeploy/spec_decode/mtp_xpu.py 0.00% 13 Missing ⚠️
...tdeploy/model_executor/xpu_pre_and_post_process.py 0.00% 5 Missing ⚠️
fastdeploy/model_executor/layers/sample/sampler.py 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop    #8061   +/-   ##
==========================================
  Coverage           ?   68.01%           
==========================================
  Files              ?      475           
  Lines              ?    66931           
  Branches           ?    10326           
==========================================
  Hits               ?    45525           
  Misses             ?    18502           
  Partials           ?     2904           
Flag Coverage Δ
GPU 78.12% <0.00%> (?)
XPU 6.95% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- Enable step_use_cudagraph for draft model with proper gating logic
- Pass forward_meta and use_cudagraph to xpu_pre_process in draft path
- Add padding_cudagraph_inputs() for draft model buffer management
- Slice model output by real_token_num when graph is active
- Adapt target model warmup and execute_model for MTP+CudaGraph
- Use build_sampling_params kernel in verify path (replaces padding_sampling_params)
- Fix memory issue by using copy_ instead of clone for seq_lens_this_time
- Fix expected_decode_len for TP>1 in dummy_prefill

Co-Authored-By: Clarity256 <1140021759@qq.com>
@Clarity256
Clarity256 force-pushed the feature/xpu-mtp-cudagraph-capture branch from 72c0f92 to 2355d0e Compare June 17, 2026 07:27
PaddlePaddle-bot

This comment was marked as outdated.

@PaddlePaddle-bot

PaddlePaddle-bot commented Jun 18, 2026

Copy link
Copy Markdown

🤖 Paddle-CI-Agent | ci_status_monitor | 2026-07-03 10:42:38 UTC+08:00

CI报告基于以下代码生成(30分钟更新一次):
PR commit: 7db4fc5 | Merge base: 7a60f79 (branch: develop)


1 Required任务 : 8/10 通过

总执行(rerun次数) 总任务 ✅ 通过 ❌ 失败 ⏳ 运行中 ⏸️ 等待中 跳过
42(0) 42 37 5 0 0 0
任务 错误类型 置信度 日志
Run FastDeploy Unit Tests and Coverage / run_tests_with_coverage PR问题:diff coverage 0%,新增XPU分支未覆盖 Job
Approval 需要 Approval Job

2 失败详情

🔴 Run FastDeploy Unit Tests and Coverage / run_tests_with_coverage — PR问题(置信度: 高)

分析器: ci_analyze_unittest_fastdeploy

失败用例: 无 pytest 失败,覆盖率阈值校验失败

用例 错误摘要
fastdeploy/model_executor/layers/sample/sampler.py diff coverage 0%,未覆盖新增/变更行 64、65、1276

关键日志:

Verify Code Coverage Threshold (80%) failed.
TEST_EXIT_CODE: 0
COVERAGE_EXIT_CODE: 9
diff-cover python_coverage_all.xml --diff-file=diff.txt --fail-under=80
"violation_lines": [64, 65, 1276]
"total_num_violations": 3, "total_percent_covered": 0
  • 根因摘要: sampler.py 新增 XPU 分支无覆盖
    日志显示所有测试通过,失败只发生在 Verify Code Coverage Threshold (80%)。diff coverage JSON 指向 sampler.py 的 3 行未覆盖变更:64、65 是 XPU ops 导入分支,1276 是 _verify_and_sample_xpu() 中的 build_sampling_params(...) 调用。本 PR 新增/调整 XPU sampling 参数构建路径,但 required 覆盖率任务未覆盖这些变更,导致 diff coverage 低于 80%。

修复建议:

  1. tests/layers/test_sampler.pytests/layers/test_speculative_sampler.py 增加 SpeculativeSampler._verify_and_sample_xpu() 的 TARGET_MATCH 单测,monkeypatch build_sampling_paramsverify_draft_tokenstop_k_top_p_sampling,断言 increment_valuetoken_num_output_cpu 被正确传入。
  2. sampler.py:64-65 的 XPU import 分支,优先用测试隔离/重载方式覆盖 current_platform.is_xpu() 路径;如果 GPU 覆盖环境无法加载 XPU ops,则按项目规范为硬件专属 import 行增加覆盖率排除。

关联变更: fastdeploy/model_executor/layers/sample/sampler.py:64, fastdeploy/model_executor/layers/sample/sampler.py:65, fastdeploy/model_executor/layers/sample/sampler.py:1276

🔴 Approval — 需要 Approval(置信度: 高)

该 Job 需要人工 Approval,完成审批后 CI 才会继续执行。

修复建议: 请通过人工审批。

@PaddlePaddle-bot PaddlePaddle-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🤖 Paddle-CI-Agent | pr_review | 2026-07-02 14:06:49

📋 Review 摘要

PR 概述:为 XPU MTP draft model 引入 CUDAGraph capture,并调整 speculative sampling 的参数构建与 seed 更新。
变更范围:XPU sampler、xpu_pre_process、MTP proposer、XPU model runner、XPU CI case
影响面 Tag[XPU] [Speculative Decoding] [Graph Optimization] [OP]

问题

级别 文件 概述
🔴 Bug fastdeploy/worker/xpu_model_runner.py:1452 SpecMethod.NAIVE speculative 路径不再推进 infer_seed,随机采样会复用同一组 seed

历史 Findings 修复情况

Finding 问题 状态
F1 cudagraph_only_prefill=True 的 prefill capture 被禁用 ⚠️ 仍存在
F2 MTP draft model 的 moe_phase 未同步 ⚠️ 仍存在

📝 PR 规范检查

标题包含两个官方 Tag,而当前 FastDeploy 规范要求标题必须且仅包含一个官方 Tag;另外 Usage or Command 章节为空。建议改为以下内容。

标题建议(可直接复制):

  • [XPU] Enable CUDAGraph capture for MTP draft model
PR 描述建议(点击展开,可直接复制)
## Motivation
1. Draft model 前向推理启用 `step_use_cudagraph` 门控逻辑,并在 multi-step 执行中仅对首步进行 capture。
2. Draft model 推理路径中传递 `forward_meta``use_cudagraph``xpu_pre_process`,确保 `cu_seqlens_q_output` / `batch_id_per_token_output` 在 cudagraph 模式下使用 `copy_` 原地更新,保证 tensor 地址稳定性。
3. 新增 `padding_cudagraph_inputs()` 方法处理 draft model 的 buffer padding,并在 graph replay 时按 `real_token_num` 切片 model output。
4. Target model 侧投机解码 warmup 流程适配 capture size 计算、`accept_all_drafts` 参数传递、TP>1 下 `expected_decode_len` 修正。
5.`padding_sampling_params` 替换为 `build_sampling_params` XPU 自定义算子,在算子内部完成 `infer_seed` 的原地更新,避免在 cudagraph 外额外操作。
6. `increment_value` 改为与投机解码 token 数联动:`(num_speculative_tokens + 1) * 4`7. Draft model 中 `last_seq_lens_this_time` 使用 `copy_()` 替代 `clone()`,避免 CUDAGraph replay 时产生新 tensor 导致内存持续增长。

## Modifications
- `fastdeploy/spec_decode/mtp_xpu.py`:draft model 启用 `step_use_cudagraph` 门控;`_propose` 新增 cudagraph padding 逻辑与 output slicing;`_initialize_forward_meta` 传递 cudagraph 参数;`last_seq_lens_this_time` 改为 `copy_()` 原地更新。
- `fastdeploy/worker/xpu_model_runner.py``increment_value` 与投机解码 token 数联动;warmup capture 流程适配 speculative decoding;`infer_seed` 更新移入 `build_sampling_params` 算子内部;draft model propose 传递 `step_use_cudagraph`;修正 TP>1 时 `dummy_prefill_inputs``expected_decode_len`- `fastdeploy/model_executor/layers/sample/sampler.py``forward_xpu` 改用 `build_sampling_params` XPU 算子替代 `padding_sampling_params`;新增 `increment_value` 参数。
- `fastdeploy/model_executor/xpu_pre_and_post_process.py`:cudagraph 模式下改用 `copy_` 原地更新 `cu_seqlens_q_output``batch_id_per_token_output`,保证 graph 捕获的 tensor 地址稳定。
- `tests/xpu_ci/4cards_cases/run_mtp_cudagraph.py``tests/xpu_ci/4cards_cases/test_mtp_cudagraph.py`:重命名测试脚本以符合 CI 命名规范。

## Usage or Command
N/A

## Accuracy Tests
- MTP with CUDAGraph:输出与参考结果一致(见 PR 截图)。
- MTP without CUDAGraph:输出与参考结果一致(见 PR 截图)。

## Checklist

- [x] Add at least a tag in the PR title.
  - Tag list: [`[FDConfig]`,`[APIServer]`,`[Engine]`, `[Scheduler]`, `[PD Disaggregation]`, `[Executor]`, `[Graph Optimization]`, `[Speculative Decoding]`, `[RL]`, `[Models]`, `[Quantization]`, `[Loader]`, `[OP]`, `[KVCache]`, `[DataProcessor]`, `[BugFix]`, `[Docs]`, `[CI]`, `[Optimization]`, `[Feature]`, `[Benchmark]`, `[Others]`, `[XPU]`, `[HPU]`, `[GCU]`, `[DCU]`, `[Iluvatar]`, `[Metax]`]
  - You can add new tags based on the PR content, but the semantics must be clear.
- [ ] Format your code, run `pre-commit` before commit.
- [x] Add unit tests. Please write the reason in this PR if no unit tests.
- [x] Provide accuracy results.
- [x] If the current PR is submitting to the `release` branch, make sure the PR has been submitted to the `develop` branch, then cherry-pick it to the `release` branch with the `[Cherry-Pick]` PR tag.

总体评价

MTP CUDAGraph 主路径的实现方向基本清楚,但这次把 speculative 场景的 infer_seed 更新整体移到 sampler/op 内后,遗漏了仍依赖 post-step 更新的 NAIVE speculative 路径。建议先修复该随机采样回归;本轮仅基于任务给出的 5 个 PR diff 文件审查,未拉取 CI 日志。

Comment thread fastdeploy/worker/xpu_model_runner.py
@Jiang-Jia-Jun
Jiang-Jia-Jun merged commit 96bf098 into PaddlePaddle:develop Jul 3, 2026
39 of 44 checks passed
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.

8 participants