Skip to content

[Cherry-Pick][Optimization][Speculative Decoding]opt mtp logprob (#7883)#7884

Open
Sunny-bot1 wants to merge 2 commits into
PaddlePaddle:release/2.6from
Sunny-bot1:opt_mtp_logprob_26
Open

[Cherry-Pick][Optimization][Speculative Decoding]opt mtp logprob (#7883)#7884
Sunny-bot1 wants to merge 2 commits into
PaddlePaddle:release/2.6from
Sunny-bot1:opt_mtp_logprob_26

Conversation

@Sunny-bot1
Copy link
Copy Markdown
Collaborator

@Sunny-bot1 Sunny-bot1 commented May 21, 2026

Motivation

MTP + logprob(top_logprobs:0) 性能提升10%

Modifications

Usage or Command

Accuracy Tests

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 21, 2026

Thanks for your contribution!

PaddlePaddle-bot

This comment was marked as outdated.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 21, 2026

Codecov Report

❌ Patch coverage is 80.00000% with 4 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (release/2.6@b562b8d). Learn more about missing BASE report.

Files with missing lines Patch % Lines
fastdeploy/output/token_processor.py 78.94% 4 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff               @@
##             release/2.6    #7884   +/-   ##
==============================================
  Coverage               ?   72.23%           
==============================================
  Files                  ?      381           
  Lines                  ?    54233           
  Branches               ?     8473           
==============================================
  Hits                   ?    39175           
  Misses                 ?    12295           
  Partials               ?     2763           
Flag Coverage Δ
GPU 72.23% <80.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.

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-22 15:03:13

📋 Review 摘要

PR 概述:MTP + logprob(top_logprobs:0) 性能优化,通过按实际 top_logprobs 数打包传输元数据,避免填充无用 topk slot,Python 侧预批量 .tolist() 减少热路径开销,实测提升 10%
变更范围custom_ops/gpu_ops/speculate_decoding/(3 个 C++ 文件)、fastdeploy/worker/gpu_model_runner.pyfastdeploy/output/token_processor.py
影响面 Tag[Speculative Decoding] [OP] [DataProcessor]

问题

级别 文件 概述
❓ 疑问 mtp_save_first_token_with_topk.cc:125 message_flag 是否保证低 8 位,OR 操作可能 bit 污染
🟡 建议 gpu_model_runner.py A6:通用路径变更,是否需同步其他硬件 ModelRunner
🟡 建议 tests/ B2 + A3:C++ spec-decode 算子 bit-packing 协议无新增单测
📝 PR 规范 PR body Modifications/Usage or Command/Accuracy Tests 为空,Checklist 全未勾选

📝 PR 规范检查

标题包含两个官方 Tag([Optimization] + [Speculative Decoding]),按规范每个 PR 标题应仅含一个官方 Tag;## Modifications## Usage or Command## Accuracy Tests 三个段落缺少实际内容,Checklist 全未勾选。

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

  • [Cherry-Pick][Speculative Decoding] opt mtp logprob top_logprobs=0 perf +10% (#7883)

PR 描述建议(可直接复制,必须复刻 checklist §D2 模板的完整结构):

## Motivation
MTP + logprob(top_logprobs:0) 性能提升 10%:通过将 message_flag(低8位)和 max_num_logprobs(高16位)打包进 meta[1],C++ 侧循环仅写/读实际请求的 topk 列,避免填充无用 slot;Python 侧预批量 .tolist() 减少热路径逐次转换开销。

## Modifications
- `mtp_save_first_token_with_topk.cc`:将 `message_flag`(低8位)和 `max_num_logprobs`(高16位)打包写入 `meta[1]`;循环仅写 `max_num_logprobs` 列,索引步长从 `SPEC_LOGPROB_K+1` 改为 `max_num_logprobs`
- `speculate_save_output_with_topk.cc`:同上打包逻辑,去掉填充 `-1/0.0` 的 else 分支
- `speculate_get_output_with_topk.cc`:从 `meta[1]` 解包 `actual_topk`,内层循环改用 `actual_topk` 作上界
- `gpu_model_runner.py`:移除 speculative_decoding 下 `max_logprobs` 硬编码为 20 的限制,改用实际请求值
- `token_processor.py`:tokens/scores 张量切片到 `actual_topk` 列;热路径 `.tolist()` 改为循环前批量预转换

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

总体评价

优化思路清晰,bit-packing 协议在发送端(两个 .cc 文件)和接收端(C++ + Python)三处对齐一致,性能收益明确。主要关注点是 message_flag 的 bit 范围假设需显式保护,以及 gpu_model_runner.py 通用路径变更需确认其他硬件 Runner 不受影响。

// Pack message_flag (low 8 bits) and max_num_logprobs (high 16 bits) into
// meta[1]. Receiver unpacks both to avoid reading unused topk slots.
int max_num_logprobs = logprob_token_ids.shape()[1];
msg_sed.meta[1] = message_flag | (max_num_logprobs << 8);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

❓ 疑问 message_flag 是否保证只使用低 8 位(< 256)?

当前打包方式:msg_sed.meta[1] = message_flag | (max_num_logprobs << 8),低8位给 message_flag,高16位给 max_num_logprobs。若 message_flag 曾被赋值超过 255,两个字段的 bit 会互相污染,导致 Python 侧解包得到错误的 mtypeactual_topk

建议:显式断言或掩码保护:

assert((message_flag & 0xFF) == message_flag); //
msg_sed.meta[1] = (message_flag & 0xFF) | (max_num_logprobs << 8);

@PaddlePaddle-bot
Copy link
Copy Markdown

PaddlePaddle-bot commented May 22, 2026

🤖 Paddle-CI-Agent | ci_status_monitor | 2026-05-22 21:08:27

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


1 任务总览

required 任务有 2 个失败:Run FastDeploy Unit Tests and Coverage / run_tests_with_coverage 为 PR 相关测试失败,Approval 需要人工审批;当前不建议合入,需先处理 required 失败任务。

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

2 任务状态汇总

日志列说明:失败任务直接链接到对应 GitHub Actions Job。

2.1 Required任务 : 8/10 通过

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

状态 任务 耗时 根因 修复建议 日志 重跑
Run FastDeploy Unit Tests and Coverage / run_tests_with_coverage 1h14m PR问题:meta[1]协议变更导致topk为空 按新协议打包meta并校验logprobs基线 Job -
Approval 7s 需要 Approval 请通过人工审批 Job -
其余 8 个必选任务通过 - - - - -

2.2 可选任务 — 23/27 通过

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

状态 任务 耗时 日志 重跑
Run iluvatar Tests / run_iluvatar_cases 2m7s Job -
Check PR Template 13s Job -
CI_HPU 1h28m Job -
Trigger Jenkins for PR 1m4s Job -
其余 23 个可选任务通过 - - -

3 失败详情(仅 required)

Run FastDeploy Unit Tests and Coverage / run_tests_with_coverage — 测试失败(置信度: 高)

Run FastDeploy Unit Tests and Coverage / run_tests_with_coverage

  • 状态: ❌ 失败
  • 错误类型: 测试失败
  • 置信度: 高
  • 根因摘要: meta[1]协议变更导致topk为空
  • 分析器: ci_analyze_unittest_fastdeploy

失败用例:

测试 错误 根因
tests/output/test_token_processor.py::test_process_batch_output_speculative_logprob_handles_draft_batch IndexError: list index out of range 单测仍写旧协议 meta[1]=4,解析后 actual_topk=0
tests/output/test_process_batch_output.py::TestTokenProcessorProcessBatchOutput::test_process_batch_output_aborted_task_negative_token_speculative_decoding IndexError: list index out of range 单测仍写旧协议 meta[1]=3,topk 维被切成空列表
tests/e2e/test_ernie_21b_mtp_multistep.py::test_prefix_cache_text AssertionError: logprobs 与baseline不一致 MTP logprobs 输出列数/内容随 actual_topkmax_logprobs 变更而变化
tests/pooling/test_Ernie4_5_reward_serving.py ConnectionResetError: [Errno 104] Connection reset by peer 服务侧 engine worker queue 断连;可能是服务进程异常退出后的伴随失败

根因详情:
本 PR 将 speculative logprob 的 meta[1] 从纯 message_flag 改为 message_flag | (max_num_logprobs << 8),并在 fastdeploy/output/token_processor.py 中按高位解析 actual_topk 后切片。现有单测 fixture 仍直接写 processor.output_tokens[1, 0] = 3/4,导致 actual_topk=0tokens[:, :, :0] 为空,随后访问 row[0] 抛出 IndexError。同时,fastdeploy/worker/gpu_model_runner.py 不再在 speculative decoding 下固定 max_logprobs=20,e2e baseline 中的 logprobs 结果需要确认是预期变化还是输出兼容性回归。

关键日志:

processor.output_tokens[1, 0] = 4
actual_topk = (packed_meta1 >> 8) & 0xFFFF  # => 0
tokens = ...reshape([batch, MAX_DRAFT_TOKENS, K + 1])[:, :, :actual_topk]
IndexError: list index out of range at fastdeploy/output/token_processor.py:798
IndexError: list index out of range at fastdeploy/output/token_processor.py:923
AssertionError: logprobs 与baseline不一致 at tests/e2e/test_ernie_21b_mtp_multistep.py:226

修复建议:

  1. 更新单测 fixture:tests/output/test_token_processor.py:752/929tests/output/test_process_batch_output.py:214/251/285meta[1] 改为新协议,例如 message_flag | ((K + 1) << 8) 或按测试期望写入实际 topk。
  2. tests/e2e/test_ernie_21b_mtp_multistep.py:226 的 logprobs baseline 做行为确认:若本 PR 的 top_logprobs 输出变化是预期优化结果,请重新生成/更新 baseline;否则检查 fastdeploy/worker/gpu_model_runner.py:1229-1234 与 C++ 写端 max_num_logprobs 索引是否仍应保留兼容输出。
  3. 可在 fastdeploy/output/token_processor.py:833 附近补充断言或防御性日志,避免 actual_topk=0 时静默切出空维度后才抛 IndexError

修复建议摘要: 按新协议打包meta并校验logprobs基线

关联变更: fastdeploy/output/token_processor.py:830-838fastdeploy/worker/gpu_model_runner.py:1229-1234custom_ops/gpu_ops/speculate_decoding/speculate_save_output_with_topk.cc:124-128

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