Skip to content

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

Merged
yuanlehome merged 4 commits into
PaddlePaddle:developfrom
freeliuzc:support_enf_gen_dev
Jan 15, 2026
Merged

yuanlehome merged 4 commits into
PaddlePaddle:developfrom
freeliuzc:support_enf_gen_dev

Conversation

@freeliuzc

@freeliuzc freeliuzc commented Jan 14, 2026

Copy link
Copy Markdown
Collaborator

Motivation

  1. 确保模型生成 \n\n\n 后,生成 <tool_call> / 其中一个token
    [Feature]Support tag phase token enforce generation #6031

💡 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

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 07:22
@paddle-bot

paddle-bot Bot commented Jan 14, 2026

Copy link
Copy Markdown

Thanks for your contribution!

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

line_break_id: int,
):
"""
reasoning_phase_token_constraint

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.

函数缺少docstring说明。作为新增的公共API,应该添加详细的文档字符串,包括:

  1. 函数的功能描述
  2. 各个参数的含义和预期类型
  3. 返回值说明
  4. 使用示例(如果适用)
    这对于API的可维护性和可用性非常重要。
Suggested change
reasoning_phase_token_constraint
Apply reasoning-phase token constraints to the logits for next-token prediction.
This function wraps a device-specific backend operator that enforces constraints
on the token distribution during the reasoning (or "thinking") phase. It updates
``logits`` in place according to the current decoding step, allowed tokens, and
reasoning status, then returns the same tensor for convenience.
Args:
logits (paddle.Tensor): Logits for candidate tokens at the current decoding
step. Shape is typically ``[batch_size, vocab_size]`` or a similar
2-D tensor. This tensor is modified in place.
pre_token_ids (paddle.Tensor): Previously generated token ids for each
sequence in the batch. Typically a 2-D integer tensor of shape
``[batch_size, seq_len]``.
stop_flags (paddle.Tensor): Boolean or integer tensor indicating which
sequences have already met their stop conditions and should no longer
produce new tokens. Shape: ``[batch_size]``.
seq_lens_this_time (paddle.Tensor): Effective sequence lengths for the
current decoding step (after padding is removed). Shape: ``[batch_size]``.
seq_lens_encoder (paddle.Tensor): Encoder-side sequence lengths associated
with each sequence in the batch. Shape: ``[batch_size]``.
step_idx (paddle.Tensor): Current decoding step index, usually a scalar or
a 1-D tensor with a single element.
reasoning_allowed_tokens (paddle.Tensor): Tensor containing the ids of
tokens that are allowed to be generated during the reasoning phase.
The exact shape depends on how allowed tokens are represented but is
typically integer-typed.
reasoning_status (paddle.Tensor): Tensor indicating the reasoning status
for each sequence (for example, whether the sequence is currently in
reasoning mode or in normal generation). Shape: ``[batch_size]``.
output_padding_offset (paddle.Tensor): Tensor used to map from padded
batch positions to compacted positions after removing padding.
output_cum_offsets (paddle.Tensor): Cumulative offsets associated with
``output_padding_offset`` for efficient indexing on the device.
think_end_id (int): Special token id that marks the end of the reasoning
phase. When this id is generated, constraints may change or be lifted.
line_break_id (int): Token id corresponding to a line break (such as
``"\\n"``). This can be used as a constraint boundary for reasoning
or output formatting.
Returns:
paddle.Tensor: The same ``logits`` tensor after in-place modification by
the backend operator. Returned for API convenience and potential chaining.
Example:
.. code-block:: python
logits = reasoning_phase_token_constraint(
logits=logits,
pre_token_ids=pre_token_ids,
stop_flags=stop_flags,
seq_lens_this_time=seq_lens_this_time,
seq_lens_encoder=seq_lens_encoder,
step_idx=step_idx,
reasoning_allowed_tokens=reasoning_allowed_tokens,
reasoning_status=reasoning_status,
output_padding_offset=output_padding_offset,
output_cum_offsets=output_cum_offsets,
think_end_id=think_end_id,
line_break_id=line_break_id,
)

Copilot uses AI. Check for mistakes.
@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 (develop@2c17acd). 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             @@
##             develop    #6034   +/-   ##
==========================================
  Coverage           ?   67.33%           
==========================================
  Files              ?      349           
  Lines              ?    45069           
  Branches           ?     6939           
==========================================
  Hits               ?    30345           
  Misses             ?    12486           
  Partials           ?     2238           
Flag Coverage Δ
GPU 67.33% <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 49617d9 into PaddlePaddle:develop Jan 15, 2026
15 of 20 checks passed
freeliuzc added a commit to freeliuzc/FastDeploy that referenced this pull request Jan 27, 2026
* support tag phase token enforce generation

* optimize note and some feature

* fix sampler unit test

---------

Co-authored-by: YuBaoku <49938469+EmmonsCurse@users.noreply.github.com>
LLSGYN pushed a commit to LLSGYN/FastDeploy that referenced this pull request Feb 2, 2026
* support tag phase token enforce generation

* optimize note and some feature

* fix sampler unit test

---------

Co-authored-by: YuBaoku <49938469+EmmonsCurse@users.noreply.github.com>
chang-wenbin pushed a commit to chang-wenbin/FastDeploy that referenced this pull request Mar 2, 2026
* support tag phase token enforce generation

* optimize note and some feature

* fix sampler unit test

---------

Co-authored-by: YuBaoku <49938469+EmmonsCurse@users.noreply.github.com>
xiaoguoguo626807 pushed a commit to xiaoguoguo626807/FastDeploy that referenced this pull request May 7, 2026
* support tag phase token enforce generation

* optimize note and some feature

* fix sampler unit test

---------

Co-authored-by: YuBaoku <49938469+EmmonsCurse@users.noreply.github.com>
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.

5 participants