Fix spec v2 stop output boundary - #25980
Merged
Merged
Conversation
gq112
requested review from
Ying1123,
hnyls2002,
merrymercy and
xiezhq-hermann
as code owners
May 21, 2026 09:58
Contributor
There was a problem hiding this comment.
Code Review
This pull request enhances stop sequence detection and handling, specifically for speculative decoding scenarios where multiple tokens are accepted simultaneously. It introduces logic to accurately determine the finish length when a stop string or regex is matched and updates the detokenizer to support non-trimming of stop sequences. A performance optimization was suggested for the stop sequence location logic to minimize expensive decoding operations by narrowing the search window based on the number of newly accepted tokens.
…d-len # Conflicts: # python/sglang/srt/managers/schedule_batch.py
Collaborator
|
/tag-and-rerun-ci |
hnyls2002
approved these changes
Jun 9, 2026
minkail-m
pushed a commit
to modal-projects/sglang
that referenced
this pull request
Jul 17, 2026
Clean cherry-pick of upstream 2218622 (sgl-project/sglang PR sgl-project#25980), companion to sgl-project#23802: when a stop string/regex matches inside a multi-token accepted chunk, record Req.finished_len at the match position so tokens accepted after the stop are not emitted, and make the detokenizer's trim_matched_stop end the text exactly at the match (keeping the stop itself under no_stop_trim). No deviations from the upstream diff: our fork already has the finished_len machinery this hooks into (Req.output_ids_through_stop, output_streamer truncation at finished_len), so the fix composes without adaptation. Co-authored-by: gss <2783977641@qq.com> Co-authored-by: hnyls2002 <lsyincs@gmail.com> Co-authored-by: Liangsheng Yin <hnyls2002@gmail.com> (cherry picked from commit 2218622) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Chronostasys
pushed a commit
to MindLab-Research/sglang
that referenced
this pull request
Aug 24, 2026
Co-authored-by: gss <2783977641@qq.com> Co-authored-by: hnyls2002 <lsyincs@gmail.com> Co-authored-by: Liangsheng Yin <hnyls2002@gmail.com>
jakki-amd
pushed a commit
to jakki-amd/sglang
that referenced
this pull request
Sep 9, 2026
Co-authored-by: gss <2783977641@qq.com> Co-authored-by: hnyls2002 <lsyincs@gmail.com> Co-authored-by: Liangsheng Yin <hnyls2002@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
This PR fixes a stop boundary issue under spec v2. When spec v2 accepts multiple tokens in one step, a stop string may appear before the end of the accepted tokens. In that case, tokens after the matched stop should not be emitted.
This is an enhancement to #23802: after the stop match is detected, this PR ensures the final emitted output is cut at the actual stop boundary.
Modifications
finished_lenfor string/regex stop matches.output_ids_through_stop.no_stop_trim=true, keep the matched stop string but still trim text after it.Accuracy Tests
Server:
Request uses a MATH500 sample :
Before this PR:
{ "content": "We are given that", "finish_reason": "stop", "matched_stop": "given", "completion_tokens": 4 }With this PR:
{ "content": "We are given", "finish_reason": "stop", "matched_stop": "given", "completion_tokens": 3 }Checklist
Review and Merge Process
/tag-and-rerun-ci,/tag-run-ci-label,/rerun-failed-ciCI States
Latest PR Test (Base): ⏳ Run #27190443615
Latest PR Test (Extra): ❌ Run #27190443069