Skip to content

Fix StopStringCriteria byte fragment matching#46522

Closed
he-yufeng wants to merge 1 commit into
huggingface:mainfrom
he-yufeng:fix/stop-string-byte-fragments
Closed

Fix StopStringCriteria byte fragment matching#46522
he-yufeng wants to merge 1 commit into
huggingface:mainfrom
he-yufeng:fix/stop-string-byte-fragments

Conversation

@he-yufeng

Copy link
Copy Markdown

What does this PR do?

Fixes StopStringCriteria for byte-level BPE tokens that contain only part of a UTF-8 character.

ByteLevel vocab entries are now converted back to their underlying bytes during criterion preprocessing, and stop strings are matched in UTF-8 byte units for those tokenizers. The existing tensor-only runtime matcher is reused unchanged, so StopStringCriteria.__call__ remains compatible with compiled generation.

This covers CJK stop strings whose final character is split across multiple byte-fragment tokens while preserving the existing behavior for non-ByteLevel tokenizers and stops that do not overlap the final token.

Closes #46519.

Coordination and duplicate search

The implementation direction was coordinated in #46519:
#46519 (comment)

Before opening this PR I searched open PRs for 46519, StopStringCriteria byte fragment, and the affected symbols. I found no overlapping implementation.

Validation

  • pytest tests/generation/test_stopping_criteria.py -q -k stop_string (6 passed)
  • ruff check src/transformers/generation/stopping_criteria.py tests/generation/test_stopping_criteria.py
  • ruff format --check src/transformers/generation/stopping_criteria.py tests/generation/test_stopping_criteria.py
  • git diff --check
  • Reported Korean reproduction plus multilingual suffix checks with Qwen2 and GPT-2
  • torch.compile(criteria, backend="eager", fullgraph=True) on the fragmented CJK case

The full stopping-criteria test file also reached 13 passing tests before an unrelated local environment failure from missing optional torchvision.

AI assistance

AI assistance was used to inspect the tokenizer/runtime behavior, implement the patch, and run the validation above. I reviewed the changed code and the resulting behavior.

@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

View the CircleCI Test Summary for this PR:

https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=46522&sha=d3f6fb

@Rocketknight1

Copy link
Copy Markdown
Member

No drive-by code agent PRs when the original issue author is still working on it, please!

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.

StopStringCriteria misses CJK stop strings on byte-level tokenizers when a character splits into byte-fragment tokens

2 participants