Skip to content

Fix overlapping stop sequence matching - #2303

Open
tandede wants to merge 1 commit into
Lightning-AI:mainfrom
tandede:fix-stop-sequence-overlap
Open

Fix overlapping stop sequence matching#2303
tandede wants to merge 1 commit into
Lightning-AI:mainfrom
tandede:fix-stop-sequence-overlap

Conversation

@tandede

@tandede tandede commented Aug 19, 2026

Copy link
Copy Markdown

What changed

  • retain the longest generated suffix that is also a prefix of each stop sequence instead of resetting all progress after a mismatch
  • flush buffered tokens that become safe when another stop sequence completes
  • record the start of a completed multi-token stop sequence in batched generation so none of its prefix tokens leak into the output
  • add single-prompt and batched regression coverage for overlapping prefixes, competing stop sequences, ordering independence, and multi-token stop boundaries

Why

The previous matcher tracked only one prefix length and reset it to zero on every mismatch. A token that ended the current partial match but also began a new one was therefore discarded as a possible prefix. The immediate return path could also drop safe buffered tokens, while the batched path recorded the final stop token rather than the start of the full sequence.

This made streaming output depend on token repetition and stop-sequence ordering, and could either miss a stop sequence, omit valid output, or emit part of a multi-token stop sequence.

Fixes #2302

Validation

  • pytest -q tests/: 1490 passed, 634 skipped, 3 xfailed, 2 xpassed, 1 rerun
  • pre-commit run --all-files
  • exhaustive prefix-state comparison across 201,060 generated-token transitions

@tandede
tandede marked this pull request as ready for review August 19, 2026 11:29
@tandede

tandede commented Aug 23, 2026

Copy link
Copy Markdown
Author

Hi maintainers, just a gentle follow-up on this PR when you have time. It fixes overlapping stop-sequence matching and includes regression coverage for single-prompt and batched generation. All reported checks are currently passing, and I’m happy to address any feedback or adjust the implementation if needed. Thanks for maintaining LitGPT.

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.

Stop sequence matching mishandles overlapping prefixes and buffered tokens

1 participant