Skip to content

[Feature]Support tag phase token enforce generation - #6031

Merged
yuanlehome merged 4 commits into
PaddlePaddle:release/online/20251131from
freeliuzc:support_enf_gen_1131
Jan 15, 2026
Merged

yuanlehome merged 4 commits into
PaddlePaddle:release/online/20251131from
freeliuzc:support_enf_gen_1131

Conversation

@freeliuzc

@freeliuzc freeliuzc commented Jan 14, 2026

Copy link
Copy Markdown
Collaborator

Motivation

  1. 确保模型生成 \n</think>\n\n 后,生成 <tool_call> / <response> 其中一个token

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.

Copilot AI review requested due to automatic review settings January 14, 2026 04:13
@paddle-bot

paddle-bot Bot commented Jan 14, 2026

Copy link
Copy Markdown

Thanks for your contribution!

@@ -0,0 +1,305 @@
// Copyright (c) 2024 PaddlePaddle Authors

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

2024 -> 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

本PR实现了一个推理阶段的token约束生成功能,用于在模型生成\n</think>\n\n模式后,强制下一个token必须是<tool_call><response>中的一个。

Changes:

  • 新增CUDA kernel reasoning_phase_token_constraint.cu实现状态机逻辑和logits约束
  • 在SpeculativeSampler中集成该功能,通过enf_gen_phase_tag配置项控制
  • 修改speculate_verify以支持reasoning_status参数,在状态1时限制draft token接受
  • 添加完整的单元测试覆盖状态转换和logits约束逻辑

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 12 comments.

Show a summary per file
File Description
custom_ops/gpu_ops/reasoning_phase_token_constraint.cu 新增CUDA kernel实现推理阶段状态机和token约束逻辑
fastdeploy/model_executor/layers/sample/sampler.py 在SpeculativeSampler中集成reasoning token约束功能
fastdeploy/model_executor/layers/sample/ops/apply_penalty_multi_scores.py 添加reasoning_phase_token_constraint包装函数
fastdeploy/model_executor/layers/sample/ops/init.py 导出新函数
fastdeploy/config.py 添加enf_gen_phase_tag配置项
custom_ops/gpu_ops/speculate_decoding/speculate_verify.cu 添加reasoning_status参数支持
tests/operators/test_reasoning_phase_token_constraint.py 新增完整的单元测试
tests/operators/test_speculate_verify.py 更新测试以支持新参数
custom_ops/setup_ops.py 添加新的CUDA文件到构建
custom_ops/gpu_ops/cpp_extensions.cc 添加C++绑定

Comment thread custom_ops/gpu_ops/reasoning_phase_token_constraint.cu
Comment thread custom_ops/gpu_ops/reasoning_phase_token_constraint.cu Outdated
Comment on lines +154 to +218
def test_status_0_to_1_only(self):
"""
status == 0
recent tokens contain <think_end>
=> status: 0 -> 1
logits should NOT be enforced
"""

# ------------------------
# setup: only think_end appears
# ------------------------
pre_ids = np.zeros((self.bs, self.max_seq_len), dtype=np.int64)

# batch 0: think_end at cur_step - 1
pre_ids[0, 3] = self.think_end_id

# batch 1: no think_end
pre_ids[1, :] = 0

self.pre_ids = paddle.to_tensor(pre_ids, dtype="int64")

self.reasoning_status = paddle.to_tensor([0, 0], dtype="int32")

logits_before = self.logits.numpy().copy()

# ------------------------
# call op
# ------------------------
reasoning_phase_token_constraint(
self.logits,
self.pre_ids,
self.stop_flags,
self.seq_lens_this_time,
self.seq_lens_encoder,
self.step_idx,
self.allowed_tokens,
self.reasoning_status,
self.output_padding_offset,
self.output_cum_offsets,
self.think_end_id,
self.line_break_id,
)

status_after = self.reasoning_status.numpy()
logits_after = self.logits.numpy()

# ============================================================
# 1. reasoning_status
# ============================================================
# batch 0: 0 -> 1
self.assertEqual(status_after[0], 1)

# batch 1: stays 0
self.assertEqual(status_after[1], 0)

# ============================================================
# 2. logits must be untouched
# ============================================================
np.testing.assert_allclose(
logits_after,
logits_before,
rtol=1e-5,
atol=1e-6,
)

Copilot AI Jan 14, 2026

Copy link

Choose a reason for hiding this comment

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

测试用例缺少对cur_step < 3场景的覆盖。所有测试用例都使用step_idx = 4,这无法触发状态机在cur_step < 3时的状态转换bug。

建议添加测试用例:

  • status == 0,cur_step = 2,检测到think_end_id
  • 预期:status应该转换为1
  • 实际:由于bug,status不会被更新

这个测试用例对于验证状态机的完整性很重要。

Copilot uses AI. Check for mistakes.
Comment thread tests/operators/test_reasoning_phase_token_constraint.py Outdated
Comment thread tests/operators/test_reasoning_phase_token_constraint.py Outdated
Comment thread fastdeploy/config.py Outdated
Comment thread tests/operators/test_reasoning_phase_token_constraint.py Outdated
Comment thread tests/operators/test_reasoning_phase_token_constraint.py Outdated
Comment thread fastdeploy/model_executor/layers/sample/sampler.py
@codecov-commenter

codecov-commenter commented Jan 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 53.84615% with 6 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (release/online/20251131@d4b9bc5). Learn more about missing BASE report.

Files with missing lines Patch % Lines
...or/layers/sample/ops/apply_penalty_multi_scores.py 20.00% 4 Missing ⚠️
fastdeploy/model_executor/layers/sample/sampler.py 60.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@                    Coverage Diff                     @@
##             release/online/20251131    #6031   +/-   ##
==========================================================
  Coverage                           ?   56.80%           
==========================================================
  Files                              ?      324           
  Lines                              ?    39388           
  Branches                           ?     5944           
==========================================================
  Hits                               ?    22374           
  Misses                             ?    15206           
  Partials                           ?     1808           
Flag Coverage Δ
GPU 56.80% <53.84%> (?)

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.

@yuanlehome
yuanlehome merged commit c8e882f into PaddlePaddle:release/online/20251131 Jan 15, 2026
11 of 16 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.

6 participants