Skip to content

[Optimization] TopP=1.0 using _random_sample#7892

Merged
ckl117 merged 5 commits into
PaddlePaddle:developfrom
ckl117:dev_topp1_sampling
May 25, 2026
Merged

[Optimization] TopP=1.0 using _random_sample#7892
ckl117 merged 5 commits into
PaddlePaddle:developfrom
ckl117:dev_topp1_sampling

Conversation

@ckl117
Copy link
Copy Markdown
Collaborator

@ckl117 ckl117 commented May 22, 2026

Motivation

top_p=1.0 场景下无需执行 top_p 截断,直接使用 Gumbel-max trick 的 _random_sample 替代完整的 top_k_top_p_sampling,减少不必要的 GPU 计算,优化推理采样性能。

   bsz  time_top_p_sampling(us)   time_current(us)
     1         1.337             0.172
     4         1.424             0.178
     8         1.552             0.178
    16         1.742             0.184
    24         1.879             0.195
    32         1.937             0.221
    48         2.002             0.239
    56         2.089             0.247
    64         2.133             0.253
    96         2.290             0.278
   128         2.372             0.298
   256         3.514             0.413

Modifications

  • fastdeploy/model_executor/layers/sample/meta_data.pySamplingMetadata 新增 top_p_list 字段(Python list,用于快速判断是否全为 1.0)
  • fastdeploy/model_executor/layers/sample/ops/top_k_top_p_sampling.py:将 top_k renorm probs 逻辑抽离为独立函数 dispatch_top_k_renorm_probs;移除 topp_seed 多余的 CPU→GPU 拷贝
  • fastdeploy/model_executor/layers/sample/sampler.py:新增 _sample_from_probs 函数,当批次内所有请求 top_p=1.0 时走 _random_sample 快速路径,否则保持原 top_k_top_p_sampling 路径
  • fastdeploy/worker/input_batch.py:新增 top_p_list list 的初始化、swap、reset 逻辑
  • fastdeploy/worker/gpu_model_runner.py:在 insert_tasks_v1 中填充 top_p_list,在 _prepare_inputs 中传入 SamplingMetadata

Usage or Command

N/A(性能优化,接口无变化,用户无需修改使用方式)

Accuracy Tests

N/A(本次变更不改变 top_p < 1.0 场景的采样逻辑,仅 top_p=1.0 时改走等价的随机采样路径)

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

Thanks for your contribution!

PaddlePaddle-bot

This comment was marked as outdated.

@PaddlePaddle-bot
Copy link
Copy Markdown

PaddlePaddle-bot commented May 22, 2026

🤖 Paddle-CI-Agent | ci_status_monitor | 2026-05-22 19:42:20

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


1 任务总览

Required 任务已全部通过(10/10),当前不阻塞合并;仍有 1 个 Optional 任务失败、1 个 Optional Workflow 等待中,仅供参考。

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

2 任务状态汇总

2.1 Required任务 : 10/10 通过

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

状态 任务 耗时 根因 修复建议 日志 重跑
其余 10 个必选任务通过 - - - - -

2.2 可选任务 — 30/32 通过

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

状态 任务 耗时 日志 重跑
Trigger Jenkins for PR 7m49s Job -
⏸️ CI_HPU - Workflow -
其余 30 个可选任务通过 - - -

3 失败详情(仅 required)

无 required 失败任务。

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 22, 2026

Codecov Report

❌ Patch coverage is 81.39535% with 8 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@3ec5011). Learn more about missing BASE report.

Files with missing lines Patch % Lines
...executor/layers/sample/ops/top_k_top_p_sampling.py 60.00% 3 Missing and 1 partial ⚠️
fastdeploy/model_executor/layers/sample/sampler.py 88.88% 1 Missing and 2 partials ⚠️
fastdeploy/worker/input_batch.py 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop    #7892   +/-   ##
==========================================
  Coverage           ?   63.62%           
==========================================
  Files              ?      462           
  Lines              ?    64532           
  Branches           ?     9892           
==========================================
  Hits               ?    41056           
  Misses             ?    20699           
  Partials           ?     2777           
Flag Coverage Δ
GPU 72.74% <81.39%> (?)
XPU 7.11% <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.

@ckl117 ckl117 changed the title [Optimization] non-MTP support top_p=1 opt [Optimization] TopP=1.0 using _random_sample May 22, 2026
PaddlePaddle-bot

This comment was marked as outdated.

PaddlePaddle-bot

This comment was marked as outdated.

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 18:39:01

📋 Review 摘要

PR 概述:top_p=1.0 场景下跳过 top_p 截断,改用 Gumbel-max trick 的 _random_sample 快速路径,同时抽离 dispatch_top_k_renorm_probs 并移除多余的 topp_seed CPU→GPU 拷贝。
变更范围model_executor/layers/sample/worker/gpu_model_runner.pyworker/input_batch.pyscripts/run_pre_ce.sh
影响面 Tag[Optimization] [OP] [CI]

问题

级别 文件 概述
🟡 建议 fastdeploy/worker/gpu_model_runner.py:1046 其他硬件 ModelRunner 未同步 top_p_list,无法享受快速路径
❓ 疑问 fastdeploy/model_executor/layers/sample/sampler.py:1068 _normal_sample 中显式传入 top_p/top_k 是冗余代码
❓ 疑问 scripts/run_pre_ce.sh:10 torch==2.8.0 PyPI 可用性 + triton 移除后 CI 路径影响

📝 PR 规范检查

标题 [Optimization] 是官方 Tag,格式合规。描述结构(Motivation / Modifications / Usage or Command / Accuracy Tests / Checklist)完整,内容充实。Checklist 各项均未勾选,建议按实际情况更新勾选状态。

描述建议(可直接复制,仅需更新 Checklist 勾选状态):

## Motivation
top_p=1.0 场景下无需执行 top_p 截断,直接使用 Gumbel-max trick 的 `_random_sample` 替代完整的 `top_k_top_p_sampling`,减少不必要的 GPU 计算,优化推理采样性能。

## Modifications
- `fastdeploy/model_executor/layers/sample/meta_data.py``SamplingMetadata` 新增 `top_p_list` 字段(Python list,用于快速判断是否全为 1.0)
- `fastdeploy/model_executor/layers/sample/ops/top_k_top_p_sampling.py`:将 top_k renorm probs 逻辑抽离为独立函数 `dispatch_top_k_renorm_probs`;移除 `topp_seed` 多余的 CPU→GPU 拷贝
- `fastdeploy/model_executor/layers/sample/sampler.py`:新增 `_sample_from_probs` 函数,当批次内所有请求 top_p=1.0 时走 `_random_sample` 快速路径,否则保持原 `top_k_top_p_sampling` 路径
- `fastdeploy/worker/input_batch.py`:新增 `top_p_list` list 的初始化、swap、reset 逻辑
- `fastdeploy/worker/gpu_model_runner.py`:在 `insert_tasks_v1` 中填充 `top_p_list`,在 `_prepare_inputs` 中传入 `SamplingMetadata`

## Usage or Command
N/A(性能优化,接口无变化,用户无需修改使用方式)

## Accuracy Tests
N/A(本次变更不改变 top_p < 1.0 场景的采样逻辑,仅 top_p=1.0 时改走等价的随机采样路径)

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

总体评价

整体方案清晰合理:快速路径的条件判断(top_p_list 全为 1.0 + is_cuda())逻辑正确,topp_seed CPU→GPU 多余拷贝的移除也是正确优化。主要关注点为其他硬件 ModelRunner 的同步情况,以及 CI 脚本中 torch==2.8.0 版本可用性。

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

Choose a reason for hiding this comment

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

❓ 疑问 _normal_sample 中显式传入 top_p=sampling_metadata.top_p / top_k=sampling_metadata.top_k 是冗余代码。

_sample_from_probs 内部已有:

if top_p is None:
    top_p = sampling_metadata.top_p
if top_k is None:
    top_k = sampling_metadata.top_k

直接调用 _sample_from_probs(probs, sampling_metadata, topp_seed=sampling_metadata.seed) 即可,无需重复传入。

continue

assert len(request.eos_token_ids) == self.model_config.eos_tokens_lens
self.share_inputs["top_p_list"][idx] = request.get("top_p", 0.7)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 建议 其他硬件 ModelRunner(gcu_model_runner.pyxpu_model_runner.pymetax_model_runner.pyhpu_model_runner.py)的 insert_tasks_v1 / _prepare_inputs 中均未填充/传入 top_p_list,导致这些平台构建的 SamplingMetadata.top_p_list=None

_sample_from_probs 中,top_p_list is Noneneed_top_p_sampling 保持 True,这些平台无法获得 top_p=1.0 的快速路径优化,行为退化(非崩溃),但属于多硬件未同步。

建议在各硬件 runner 中同步添加 top_p_list 的初始化和传递逻辑,或在 PR 描述中说明其他平台暂不支持此优化的原因。

Comment thread scripts/run_pre_ce.sh
https://paddle-qa.bj.bcebos.com/FastDeploy/torch-2.6.0-cp310-cp310-manylinux1_x86_64.whl \
https://paddle-qa.bj.bcebos.com/FastDeploy/triton-3.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl \
https://paddle-qa.bj.bcebos.com/FastDeploy/xgrammar-0.1.19-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
# Use prebuilt wheel files to install xgrammar==0.1.19 and torch==2.8.0 specifically for the CI environment
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

❓ 疑问 torch==2.8.0 在 PyPI 上目前可用吗?请确认该版本号已发布,避免 CI 安装失败。

同时,原脚本安装了 triton-3.2.0 wheel,此次移除后,若 CI 中 FD_SAMPLING_CLASS=triton 路径有测试用例,import triton 会失败,请确认是否需要保留 triton 安装或在 CI 中跳过 triton 路径测试。

@ckl117 ckl117 merged commit e85f0e2 into PaddlePaddle:develop May 25, 2026
41 of 43 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.

4 participants