Skip to content

[Feature] Support sampling_threshold#7875

Open
ckl117 wants to merge 5 commits into
PaddlePaddle:developfrom
ckl117:dev_sampling_threshold
Open

[Feature] Support sampling_threshold#7875
ckl117 wants to merge 5 commits into
PaddlePaddle:developfrom
ckl117:dev_sampling_threshold

Conversation

@ckl117
Copy link
Copy Markdown
Collaborator

@ckl117 ckl117 commented May 21, 2026

Motivation

支持请求指定sampling_threshold,在采样前过滤probs小于sampling_threshold的所有tokens。

💡 If this PR is a Cherry Pick, the PR title needs to follow the format by adding the [Cherry-Pick] label at the very beginning and appending the original PR ID at the end. For example, [Cherry-Pick][CI] Add check trigger and logic(#5191)

💡 如若此PR是Cherry Pick,PR标题需遵循格式,在最开始加上[Cherry-Pick]标签,以及最后面加上原PR ID,例如[Cherry-Pick][CI] Add check trigger and logic(#5191)

Modifications

新增sampling_threshold字段

Usage or Command

支持sampling_threshold 的采样后端

默认(export FD_SAMPLING_CLASS=base)
or
export FD_SAMPLING_CLASS=base_non_truncated
or
export FD_SAMPLING_CLASS=air

测试脚本

curl -X POST "http://0.0.0.0:8112/v1/chat/completions" \
-H "Content-Type: application/json" \
-d '{
  "messages": [
    {"role": "user", "content": "牛顿的三大运动定律是什么?"}
  ],
  "top_p": 1.0,
  "temperature": 1.0,
  "sampling_threshold": 0.00002,
  "stream": false,
  "max_tokens": 3
}'

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.

@PaddlePaddle-bot
Copy link
Copy Markdown

PaddlePaddle-bot commented May 21, 2026

🤖 Paddle-CI-Agent | ci_status_monitor | 2026-05-22 00:26:50

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


1 任务总览

Required 任务 10 个中 9 个已通过,当前有 1 个 Required 失败任务阻塞合并:Run FastDeploy Unit Tests and Coverage / run_tests_with_coverage。失败原因是 PR 新增 sampling_threshold 相关代码的 diff 覆盖率未达阈值;可选任务有 2 个失败,仅供参考。

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

2 任务状态汇总

日志列说明:失败任务直接使用工具生成的日志链接;运行中任务显示 Job 链接。

2.1 Required任务 : 9/10 通过

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

状态 任务 耗时 根因 修复建议 日志 重跑
Run FastDeploy Unit Tests and Coverage / run_tests_with_coverage 1h21m PR问题:diff覆盖率77%未达80% 补充 sampling_threshold 覆盖测试 Job -
其余 9 个必选任务通过 - - - - -

2.2 可选任务 — 30/32 通过

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

状态 任务 耗时 日志 重跑
CI_HPU 1h6m Job -
Trigger Jenkins for PR 7m57s Job -
其余 30 个可选任务通过 - - -

3 失败详情(仅 required)

Run FastDeploy Unit Tests and Coverage / run_tests_with_coverage — 测试失败(覆盖率检查不通过,置信度: 高)

Run FastDeploy Unit Tests and Coverage / run_tests_with_coverage

  • 状态: ❌ 失败
  • 错误类型: 测试失败(覆盖率检查不通过)
  • 置信度: 高
  • 根因摘要: PR新增sampling_threshold,diff覆盖率77%未达80%阈值
  • 分析器: ci_analyze_unittest_fastdeploy

失败用例: 无(单元测试全部通过,覆盖率检查阶段失败)

根因详情:
PR 新增了 sampling_threshold 参数,单元测试均已通过(TEST_EXIT_CODE: 0),但 diff 覆盖率检查阶段失败(COVERAGE_EXIT_CODE: 9)。具体有 2 个文件的新增行未被覆盖:

  1. fastdeploy/engine/sampling_params.py L317、L319 — _verify_args() 中对 sampling_threshold 进行类型校验和范围校验的异常抛出路径(TypeErrorValueError),未编写对应的异常测试用例;
  2. fastdeploy/worker/input_batch.py L572、L583 — reset_share_inputs() 中对 sampling_threshold tensor 和 list 的重置逻辑,现有测试未触发该分支。

关键日志:

"fastdeploy/worker/input_batch.py": {"percent_covered": 66.67, "violation_lines": [572, 583]}
"fastdeploy/engine/sampling_params.py": {"percent_covered": 60.0, "violation_lines": [317, 319]}
total_num_violations: 4, total_percent_covered: 77 (threshold: 80)
##[error]Process completed with exit code 9.

修复建议:

  1. tests/engine/test_sampling_params.py 中新增测试:传入非 float 类型的 sampling_threshold 应触发 TypeError(覆盖 sampling_params.py L317),传入 ≥1.0 的值应触发 ValueError(覆盖 L319)。
  2. 在覆盖 input_batch.py 的测试中触发 reset_share_inputs 方法,验证 sampling_threshold tensor(L572)和 sampling_threshold_list(L583)被正确重置为默认值 0.0。

修复建议摘要: 在 test_sampling_params.py 补充 sampling_threshold 异常测试覆盖 L317/L319

关联变更: fastdeploy/engine/sampling_params.py L313-L319,fastdeploy/worker/input_batch.py L563-L583
链接: 查看日志

@ckl117 ckl117 changed the title [Feature]Support sampling_threshold [Feature] Support sampling_threshold May 21, 2026
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 55.00000% with 9 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@2a0edf8). Learn more about missing BASE report.

Files with missing lines Patch % Lines
fastdeploy/engine/sampling_params.py 20.00% 2 Missing and 2 partials ⚠️
fastdeploy/worker/xpu_model_runner.py 0.00% 4 Missing ⚠️
fastdeploy/worker/input_batch.py 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop    #7875   +/-   ##
==========================================
  Coverage           ?   63.57%           
==========================================
  Files              ?      462           
  Lines              ?    64518           
  Branches           ?     9891           
==========================================
  Hits               ?    41016           
  Misses             ?    20721           
  Partials           ?     2781           
Flag Coverage Δ
GPU 72.70% <68.75%> (?)
XPU 7.11% <15.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

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-21 16:08:58

📋 Review 摘要

PR 概述:新增 sampling_threshold 参数支持,在采样前过滤绝对概率低于阈值的 token,改善生成质量。
变更范围engine/sampling_params.pyentrypoints/openai/protocol.pymodel_executor/layers/sample/worker/gpu_model_runner.pyworker/xpu_model_runner.pyworker/input_batch.py、文档
影响面 Tag[Feature] [APIServer] [Engine] [OP] [XPU]

问题

级别 文件 概述
🔴 Bug fastdeploy/worker/xpu_model_runner.py:762 逗号表达式而非赋值,XPU 上 sampling_threshold 永远无法写入共享内存
🟡 建议 fastdeploy/worker/ dcu_model_runner.py / iluvatar_model_runner.py / metax_model_runner.py / hpu_model_runner.py / gcu_model_runner.py 均未同步 sampling_threshold 支持

📝 PR 规范检查

标题 [Feature] Support sampling_threshold 格式合规,Tag 使用正确。

PR 描述结构基本符合模板,但 ## Accuracy Tests 章节内容为空(仅有注释占位),## Checklist 所有条目均未勾选。建议按实际情况补全。

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

## Motivation
支持请求指定 `sampling_threshold`,在采样前过滤概率小于 `sampling_threshold` 的所有 token,只从高绝对概率 token 中采样,提高生成质量。

## Modifications
- 新增 `SamplingParams.sampling_threshold` 字段(默认 0.0),并在 `_verify_args` 中校验范围 [0.0, 1.0)
- `CompletionRequest` / `ChatCompletionRequest` 新增 `sampling_threshold` 字段(OpenAI 协议扩展,ge=0.0, lt=1.0)
- `SamplingMetadata` 新增 `sampling_threshold` tensor 字段
- `sampler.py``_normal_sample``_verify_and_sample``forward_cuda``_normal_sample_xpu``_verify_and_sample_xpu` 中透传 `threshold` 参数
- `input_batch.py` 初始化/重置/交换逻辑同步新增 `sampling_threshold``sampling_threshold_list`
- `gpu_model_runner.py` / `xpu_model_runner.py``insert_tasks_v1``_prepare_inputs` 同步支持
- 文档(中英文)同步更新采样策略说明、接口参数说明、使用示例

## Usage or Command
支持 `sampling_threshold` 的采样后端:
```
默认(export FD_SAMPLING_CLASS=base)
or export FD_SAMPLING_CLASS=base_non_truncated
or export FD_SAMPLING_CLASS=air
```

测试脚本:
```shell
curl -X POST "http://0.0.0.0:8112/v1/chat/completions" \
-H "Content-Type: application/json" \
-d '{
  "messages": [{"role": "user", "content": "牛顿的三大运动定律是什么?"}],
  "top_p": 1.0,
  "temperature": 1.0,
  "sampling_threshold": 0.00002,
  "stream": false,
  "max_tokens": 3
}'
```

## Accuracy Tests
N/A(本 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.
- [ ] 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 整体思路清晰,sampling_threshold 功能链路完整(参数定义 → 协议解析 → 采样传参 → 文档)。XPU Runner 存在一处笔误(逗号替代赋值),必须修复后方可合入;此外建议补全其他硬件 Runner(DCU / Iluvatar / Metax / HPU / GCU)的同步支持,避免这些平台功能静默失效。

self.share_inputs["min_p"][idx : idx + 1] = request.get("min_p", 0.0)
self.share_inputs["min_p_list"][idx] = request.get("min_p", 0.0)
self.share_inputs["sampling_threshold_list"][idx] = request.get("sampling_threshold", 0.0)
self.share_inputs["sampling_threshold"][idx : idx + 1], request.get("sampling_threshold", 0.0)
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_threshold 值永远不会被写入共享内存。

当前代码:

self.share_inputs["sampling_threshold"][idx : idx + 1], request.get("sampling_threshold", 0.0)

这是一个元组表达式,不是赋值,因此 XPU 上 sampling_threshold 始终保持初始化值 0.0,功能完全失效。

建议修复为(与 gpu_model_runner.py 第1053-1055行保持一致):

self.share_inputs["sampling_threshold"][idx : idx + 1] = request.get("sampling_threshold", 0.0)

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