Skip to content

[XPU] Fix mtp cudagraph#7864

Open
cmcamdy wants to merge 5 commits into
PaddlePaddle:developfrom
cmcamdy:fix_mtp_cudagraph
Open

[XPU] Fix mtp cudagraph#7864
cmcamdy wants to merge 5 commits into
PaddlePaddle:developfrom
cmcamdy:fix_mtp_cudagraph

Conversation

@cmcamdy
Copy link
Copy Markdown
Collaborator

@cmcamdy cmcamdy commented May 20, 2026

Motivation

修复 XPU 平台 MTP(Multi-Token Prediction)投机解码在 CUDAGraph 模式下运行时出现的问题,主要包括:

  1. cu_seqlens_q_output / batch_id_per_token_output 在 cudagraph 模式下应使用 copy_ 原地更新而非重新赋值,否则破坏 graph 捕获的 tensor 地址稳定性。
  2. padding_sampling_params(Python 侧 CPU 实现)被替换为新增的 build_sampling_params XPU 自定义算子,同时在算子内部完成 infer_seed 的原地更新,以避免在 cudagraph 外额外操作。
  3. MTP draft model 当前阶段禁用 cudagraph(TODO),并修复相关 capture 流程。

Modifications

  • custom_ops/xpu_ops/src/ops/mtp/build_sampling_params.cc:新增 Paddle 自定义算子入口,注册 build_sampling_params op。
  • custom_ops/xpu_ops/src/plugin/include/xpu/plugin.h:声明 build_sampling_params C 接口。
  • custom_ops/xpu_ops/src/plugin/src/kernel/kunlun3cpp/mtp_kernel/build_sampling_params.xpu:新增 XPU3 kernel 实现。
  • custom_ops/xpu_ops/src/plugin/src/wrapper/mtp_wrapper/build_sampling_params.cpp:新增 CPU wrapper 和 XPU3 wrapper。
  • custom_ops/xpu_ops/test/test_build_sampling_params.py:新增单元测试,覆盖纯 decoder、纯 encoder、混合、单条、seed wrap-around 等场景。
  • fastdeploy/model_executor/layers/sample/sampler.py_verify_and_sample_xpu 改用 build_sampling_params XPU 算子;forward_xpu 新增 increment_value 参数。
  • fastdeploy/model_executor/xpu_pre_and_post_process.py:cudagraph 模式下改用 copy_ 原地更新 cu_seqlens_q_outputbatch_id_per_token_output
  • fastdeploy/spec_decode/mtp_xpu.py:draft model 当前强制禁用 cudagraph(TODO),_propose 新增 cudagraph padding 逻辑。
  • fastdeploy/worker/xpu_model_runner.pyincrement_value 改为与投机解码 token 数联动;投机解码模式下 infer_seed 更新移入 build_sampling_params 算子内部。

Usage or Command

N/A

Accuracy Tests

image
  • 没开cudagraph
image 9036be153df9a404145aa0141420ea52
  • 开了cudagraph
image 1fd558b0d56271f7c092358f4eecfed3

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.

@paddle-bot
Copy link
Copy Markdown

paddle-bot Bot commented May 20, 2026

Thanks for your contribution!

Copy link
Copy Markdown

@PaddlePaddle-bot PaddlePaddle-bot left a comment

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-05-20 16:29:30

📋 Review 摘要

PR 概述:修复 XPU 平台 MTP(Multi-Token Prediction)投机解码在 CUDAGraph 模式下的问题,并新增 build_sampling_params XPU 自定义算子。
变更范围custom_ops/xpu_ops/(新增算子)、fastdeploy/spec_decode/mtp_xpu.pyfastdeploy/worker/xpu_model_runner.pyfastdeploy/model_executor/layers/sample/sampler.py
影响面 Tag[XPU] [Speculative Decoding] [OP]

问题

级别 文件 概述
🔴 Bug fastdeploy/model_executor/layers/sample/sampler.py:1195 sampling_metadata.topp_seed 属性不存在,普通 XPU 采样路径运行时必抛 AttributeError
📝 PR 规范 PR 描述模板 Motivation / Modifications / Usage / Accuracy Tests 均为空

📝 PR 规范检查

PR 标题 [XPU] Fix mtp cudagraph 使用了合法 Tag,格式合规。

PR 描述中 ## Motivation## Modifications## Usage or Command## Accuracy Tests 四个必填段落均为空(仅保留了模板注释占位符),Checklist 全部未勾选,不符合描述模板要求。

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

  • [XPU][BugFix] Fix MTP CUDAGraph and add build_sampling_params XPU op

PR 描述建议(可直接复制):

## Motivation
修复 XPU 平台 MTP(Multi-Token Prediction)投机解码在 CUDAGraph 模式下运行时出现的问题,主要包括:
1. `cu_seqlens_q_output` / `batch_id_per_token_output` 在 cudagraph 模式下应使用 `copy_` 原地更新而非重新赋值,否则破坏 graph 捕获的 tensor 地址稳定性。
2.`padding_sampling_params`(Python 侧 CPU 实现)被替换为新增的 `build_sampling_params` XPU 自定义算子,同时在算子内部完成 `infer_seed` 的原地更新,以避免在 cudagraph 外额外操作。
3. MTP draft model 当前阶段禁用 cudagraph(TODO),并修复相关 capture 流程。

## Modifications
- `custom_ops/xpu_ops/src/ops/mtp/build_sampling_params.cc`:新增 Paddle 自定义算子入口,注册 `build_sampling_params` op。
- `custom_ops/xpu_ops/src/plugin/include/xpu/plugin.h`:声明 `build_sampling_params` C 接口。
- `custom_ops/xpu_ops/src/plugin/src/kernel/kunlun3cpp/mtp_kernel/build_sampling_params.xpu`:新增 XPU3 kernel 实现。
- `custom_ops/xpu_ops/src/plugin/src/wrapper/mtp_wrapper/build_sampling_params.cpp`:新增 CPU wrapper 和 XPU3 wrapper。
- `custom_ops/xpu_ops/test/test_build_sampling_params.py`:新增单元测试,覆盖纯 decoder、纯 encoder、混合、单条、seed wrap-around 等场景。
- `fastdeploy/model_executor/layers/sample/sampler.py``_verify_and_sample_xpu` 改用 `build_sampling_params` XPU 算子;`forward_xpu` 新增 `increment_value` 参数。
- `fastdeploy/model_executor/xpu_pre_and_post_process.py`:cudagraph 模式下改用 `copy_` 原地更新 `cu_seqlens_q_output``batch_id_per_token_output`- `fastdeploy/spec_decode/mtp_xpu.py`:draft model 当前强制禁用 cudagraph(TODO),`_propose` 新增 cudagraph padding 逻辑。
- `fastdeploy/worker/xpu_model_runner.py``increment_value` 改为与投机解码 token 数联动;投机解码模式下 `infer_seed` 更新移入 `build_sampling_params` 算子内部。

## Usage or Command
N/A

## Accuracy Tests
N/A

## 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.
- [ ] 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.

总体评价

本 PR 新增 build_sampling_params XPU 算子并修复 MTP cudagraph 相关问题,整体方向正确,但存在一个 P0 Bug:普通 XPU 采样路径(_normal_sample_xpu)引用了不存在的 sampling_metadata.topp_seed 属性,会在非投机解码的正常推理时直接崩溃,需在合入前修复。

top_k=sampling_metadata.top_k,
top_k_list=sampling_metadata.top_k_list,
topp_seed=topp_seed,
topp_seed=sampling_metadata.topp_seed,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 Bug sampling_metadata.topp_seed 属性不存在,运行时会抛出 AttributeError

SamplingMetadata 数据类中只有 seed 字段,没有 topp_seed 字段。此处直接传 sampling_metadata.topp_seed 会在普通 XPU 采样路径(非投机解码)下必然崩溃。

原代码通过 padding_sampling_params(...)seed 展开为 topp_seed(shape [token_num, 1])后再传入。本次 PR 对投机解码路径(_verify_and_sample_xpu)已正确改为调用 build_sampling_params 生成 topp_seed,但普通采样路径遗漏了同等处理。

建议修复方案(二选一):

  1. 在调用 top_k_top_p_sampling 之前,先调用 build_sampling_params 生成 padded seed,与投机解码路径保持一致;
  2. 或者在 SamplingMetadata 中新增 topp_seed 字段并在 _build_sampling_metadata 中填充。

示例(方案1):

_, topp_seed = build_sampling_params(
    sampling_metadata.top_p,
    sampling_metadata.top_k,
    sampling_metadata.seed,
    share_inputs["seq_lens_this_time"],
    share_inputs["seq_lens_encoder"],
    token_num_output_cpu=int(share_inputs["cu_seqlens_q_output"][-1]),
    increment_value=increment_value,
)[1:]  # 取 top_k_padding, topp_seed
_, next_tokens = top_k_top_p_sampling(
    probs,
    top_p=sampling_metadata.top_p,
    top_k=sampling_metadata.top_k,
    top_k_list=sampling_metadata.top_k_list,
    topp_seed=topp_seed,
)

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 20, 2026

Codecov Report

❌ Patch coverage is 0% with 36 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@c1b7c08). 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% 12 Missing ⚠️
...tdeploy/model_executor/xpu_pre_and_post_process.py 0.00% 5 Missing ⚠️
fastdeploy/model_executor/layers/sample/sampler.py 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop    #7864   +/-   ##
==========================================
  Coverage           ?   63.32%           
==========================================
  Files              ?      462           
  Lines              ?    64412           
  Branches           ?     9878           
==========================================
  Hits               ?    40786           
  Misses             ?    20858           
  Partials           ?     2768           
Flag Coverage Δ
GPU 72.42% <0.00%> (?)
XPU 7.12% <0.00%> (?)

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

☔ View full report in Codecov by Sentry.
📢 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.

@PaddlePaddle-bot
Copy link
Copy Markdown

PaddlePaddle-bot commented May 20, 2026

🤖 Paddle-CI-Agent | ci_status_monitor | 2026-05-22 12:17:08

CI报告基于以下代码生成(30分钟更新一次):


1 任务总览

Required 任务当前 8/10 通过,仍有 2 个 required 失败任务 阻塞合并:主单测覆盖率失败和 Approval 未完成。请优先处理 required 失败项;optional 失败仅供参考。

总执行(rerun次数) 总任务 ✅ 通过 ❌ 失败 ⏳ 运行中 ⏸️ 等待中 跳过
38(0) 38 33 5 0 0 0

2 任务状态汇总

日志列说明:失败任务直接使用日志链接;运行中任务手动拼接 Job 链接。

2.1 Required任务 : 8/10 通过

必选任务阻塞合并,失败需优先处理。

状态 任务 耗时 根因 修复建议 日志 重跑
Run FastDeploy Unit Tests and Coverage / run_tests_with_coverage 1h22m PR问题:新增 Python 行 diff 覆盖率 0% 补测 mtp_xpu/sampler 新增分支 Job -
Approval 11s 需要 Approval 请通过人工审批 Job -
其余 8 个必选任务通过 - - - - -

2.2 可选任务 — 25/28 通过

可选任务不阻塞合并,失败仅供参考。

状态 任务 耗时 日志 重跑
Check PR Template 10s Job -
CI_HPU 1h25m Job -
Trigger Jenkins for PR 7m40s Job -
其余 25 个可选任务通过 - - -

3 失败详情(仅 required)

Run FastDeploy Unit Tests and Coverage / run_tests_with_coverage — 覆盖率阈值失败(置信度: 高)

Run FastDeploy Unit Tests and Coverage / run_tests_with_coverage

  • 状态: ❌ 失败
  • 错误类型: 覆盖率阈值失败
  • 置信度: 高
  • 根因摘要: 新增 Python 行 diff 覆盖率 0%
  • 分析器: 通用分析(fallback)

失败用例: 无,单测通过,失败发生在覆盖率校验步骤。

根因详情:
单测步骤已通过,但 Verify Code Coverage Threshold (80%) 步骤返回 COVERAGE_EXIT_CODE=9diff_coverage.json 显示 fastdeploy/spec_decode/mtp_xpu.pyfastdeploy/model_executor/layers/sample/sampler.py 的本次变更行未被覆盖,14 行 violation、总 diff 覆盖率为 0%,因此 Required 主测试失败。

关键日志:

All tests passed
Coverage generation failed (exit code 9)
fastdeploy/spec_decode/mtp_xpu.py: percent_covered=0.0, violation_lines=[124,181,185,209,210,216,217,358,366,367,368,369]
fastdeploy/model_executor/layers/sample/sampler.py: percent_covered=0.0, violation_lines=[64,1225]
total_num_violations=14, total_percent_covered=0

修复建议:

  1. tests/spec_decode/test_mtp_proposer.py 增补 fastdeploy/spec_decode/mtp_xpu.py 新增 cudagraph/MTP 分支测试,覆盖 _initialize_forward_meta_proposepadding_cudagraph_inputs 中新增行。
  2. 在 sampler 相关 XPU 测试中覆盖 fastdeploy/model_executor/layers/sample/sampler.pybuild_sampling_params 导入与 _verify_and_sample_xpu 调用路径;如硬件/场景限制导致无法覆盖,请按项目流程申请覆盖率豁免。

修复建议摘要: 补测 mtp_xpu/sampler 新增分支

关联变更: fastdeploy/spec_decode/mtp_xpu.py, fastdeploy/model_executor/layers/sample/sampler.py

链接: 查看日志

Approval — 等待人工审批(置信度: 高)

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

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.

3 participants