Skip to content

[Fix] detokenizer: Trim a matched stop string at its last occurrence - #33151

Open
havengit wants to merge 1 commit into
sgl-project:mainfrom
havengit:fix/detokenizer-stop-trim-rfind
Open

havengit wants to merge 1 commit into
sgl-project:mainfrom
havengit:fix/detokenizer-stop-trim-rfind

Conversation

@havengit

@havengit havengit commented Aug 1, 2026 •

Copy link
Copy Markdown

Motivation

When reasoning is enabled, the decoded text handed to trim_matched_stop concatenates the thinking block and the final answer, and the model may quote the user's stop string verbatim while planning ("...end with fei chang jingcai, ok"). trim_matched_stop anchored on output.find(matched) — the first occurrence, inside the thinking — so the whole answer was truncated away and only the start of the reasoning was returned.

Modifications

Use output.rfind(matched): the hit that ended generation is the last occurrence. no_stop_trim and the token-based path are unchanged.

Reproducer: Anthropic route, stop_sequences: ["jingcai"], system prompt asking the model to end with "fei chang jingcai!". Before: "<think>...mention jingcai..." (answer gone). After: "<think>plan</think>fei chang ".

Part of tracking issue #40529 (defect 5). Masked on stock builds by #33152 (the scheduler stops inside the reasoning first); observable once that lands.

Checklist

  • Format your code according to the Format code with pre-commit.
  • Add unit tests according to the Run and add unit tests (test/registered/unit/managers/test_trim_matched_stop.py, CPU).
  • Update documentation.
  • Provide accuracy and speed benchmark results.
  • Follow the SGLang code style guidance.

CI States

Latest PR Test (Base): ⊘ Skipped -- a maintainer comments /tag-and-rerun-ci to run CI tests.
Latest PR Test (Extra): ⊘ Skipped -- a maintainer comments /tag-and-rerun-ci to run CI tests.
Latest PR Test (AMD ROCm 10): ⚠️ Run #46642236465

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

Rebased onto main @ e0e9a7f39d (2026-12-01). After #54065 moved Req into python/sglang/srt/managers/req.py, the scheduler change now lives there; the test imports Req from its defining module (upstream's check-indirect-imports hook rejects importing it through schedule_batch). No behavioural change from the rebase.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

agiping pushed a commit to Infrawaves/sglang that referenced this pull request Sep 30, 2026
Limit this change to non-speculative decoding; speculative compatibility is intentionally out of scope. Document that boundary in the scheduler and regression tests.

Use the existing reasoning token count to restrict user stop strings, regexes, token IDs and streaming prefix holdback to content. Keep system EOS and length limits effective during thinking. Preserve the thinking prefix when trimming the first stop match in content, including repeated matches inside a single decoded token.

Reuse the existing trim interface and IPC fields. Do not include the reasoning-to-content fallback proposed in sgl-project#33154.

Adapted from sgl-project#33152 (eb800ddcd97fb7a27886580941b607017770da93), with the trimming defect described in sgl-project#33151 (efb505ebce378db6b55336f4917be83f68049628) addressed without a global find-to-rfind substitution.

Validation: 137 CPU tests and 53 subtests passed; 2 opt-in microbenchmarks skipped. Excluded one existing logprob fixture failure reproduced on base 32a0406. New non-speculative regressions fail on that base. Ruff/isort and diff checks pass. Full manager collection needs the missing mlx dependency; no real-model/GPU validation.
Zhangmj0621 pushed a commit to Infrawaves/sglang that referenced this pull request Oct 8, 2026
Limit this change to non-speculative decoding; speculative compatibility is intentionally out of scope. Document that boundary in the scheduler and regression tests.

Use the existing reasoning token count to restrict user stop strings, regexes, token IDs and streaming prefix holdback to content. Keep system EOS and length limits effective during thinking. Preserve the thinking prefix when trimming the first stop match in content, including repeated matches inside a single decoded token.

Reuse the existing trim interface and IPC fields. Do not include the reasoning-to-content fallback proposed in sgl-project#33154.

Adapted from sgl-project#33152 (eb800ddcd97fb7a27886580941b607017770da93), with the trimming defect described in sgl-project#33151 (efb505ebce378db6b55336f4917be83f68049628) addressed without a global find-to-rfind substitution.

Validation: 137 CPU tests and 53 subtests passed; 2 opt-in microbenchmarks skipped. Excluded one existing logprob fixture failure reproduced on base 32a0406. New non-speculative regressions fail on that base. Ruff/isort and diff checks pass. Full manager collection needs the missing mlx dependency; no real-model/GPU validation.
Nozom1ff pushed a commit to Infrawaves/sglang that referenced this pull request Oct 29, 2026
Limit this change to non-speculative decoding; speculative compatibility is intentionally out of scope. Document that boundary in the scheduler and regression tests.

Use the existing reasoning token count to restrict user stop strings, regexes, token IDs and streaming prefix holdback to content. Keep system EOS and length limits effective during thinking. Preserve the thinking prefix when trimming the first stop match in content, including repeated matches inside a single decoded token.

Reuse the existing trim interface and IPC fields. Do not include the reasoning-to-content fallback proposed in sgl-project#33154.

Adapted from sgl-project#33152 (eb800ddcd97fb7a27886580941b607017770da93), with the trimming defect described in sgl-project#33151 (efb505ebce378db6b55336f4917be83f68049628) addressed without a global find-to-rfind substitution.

Validation: 137 CPU tests and 53 subtests passed; 2 opt-in microbenchmarks skipped. Excluded one existing logprob fixture failure reproduced on base 32a0406. New non-speculative regressions fail on that base. Ruff/isort and diff checks pass. Full manager collection needs the missing mlx dependency; no real-model/GPU validation.
@havengit
havengit force-pushed the fix/detokenizer-stop-trim-rfind branch from efb505e to 7e9ae2a Compare November 30, 2026 14:20
@github-actions

This comment has been minimized.

@havengit havengit changed the title fix(detokenizer): use rfind to trim stop string at the actual hit point [Fix] detokenizer: Trim a matched stop string at its last occurrence Nov 30, 2026
@havengit

Copy link
Copy Markdown
Author

@merrymercy @hnyls2002 @cctry — rebased onto latest main (f69e723) with a CPU unit test added; GitHub reports the PR mergeable. Could a maintainer /tag-and-rerun-ci so the suite actually runs on this commit? The pr-test-finish failure above is only the "no run-ci label yet" skip.

Part of #40529 (tracking issue with a reproducer and before/after output for each defect). Happy to adjust the approach.

@havengit
havengit force-pushed the fix/detokenizer-stop-trim-rfind branch 3 times, most recently from 1938f1e to e568944 Compare December 1, 2026 06:08
When reasoning is enabled the decoded text concatenates the thinking block and the final answer, and the model may quote the user's stop string verbatim while planning. trim_matched_stop anchored on output.find(matched), i.e. the first occurrence inside the thinking, so the whole answer was truncated away and only the start of the reasoning was returned. The hit that ended generation is the last occurrence: use rfind. no_stop_trim and the token-based path are unchanged.
@havengit
havengit force-pushed the fix/detokenizer-stop-trim-rfind branch from e568944 to 340bcd7 Compare December 1, 2026 12:02

This branch has not been deployed

No deployments
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.

1 participant