Repository navigation
Restore the Unicode whitespace set in the GPT-SW3 tokenizer - #48912
Merged
itazap merged 2 commits intoSep 28, 2026
Merged
Conversation
`GPTSw3Tokenizer.whitespaces` is used by `preprocess_text` to map unusual Unicode spaces to a plain space, a step the docstring describes as identical to what was used when training the tokenizer. In 05c0e1d ("rm slow tokenizers", huggingface#40936) nine of the eleven characters in that set were rewritten to U+0020 and U+0084 became an empty string, so the set now holds only {U+0020, U+3000, U+FFFC} and seven Unicode spaces are no longer normalized: U+2002, U+2003, U+2005, U+2008, U+2009, U+200A, U+202F. With AI-Sweden-Models' released spiece.model, "en text" now tokenizes as ['en', '<0xE2>', '<0x80>', '<0x89>', 'text'] instead of ['en', 'text']. The characters are restored as explicit escapes so a future source rewrite cannot silently lose them again. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
pedrobarrosomarketing-coder
approved these changes
Sep 18, 2026
itazap
approved these changes
Sep 18, 2026
itazap
left a comment
Collaborator
There was a problem hiding this comment.
thanks! small nit and good for me
itazap
reviewed
Sep 18, 2026
Comment on lines
+123
to
+124
| # Escapes are used on purpose: these code points are easy to lose to editors and | ||
| # automated rewrites that normalize unusual whitespace in source files. |
Collaborator
There was a problem hiding this comment.
Suggested change
| # Escapes are used on purpose: these code points are easy to lose to editors and | |
| # automated rewrites that normalize unusual whitespace in source files. |
Applies the review suggestion. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: gpt_sw3 |
Contributor
CI recapDashboard: View test results in Grafana |
itazap
enabled auto-merge
September 28, 2026 12:19
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
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.
What does this PR do?
Restores the seven Unicode space characters that
GPTSw3Tokenizerstopped normalizing after a source-level rewrite.preprocess_textmaps everything inself.whitespacesto a plain space, and its docstring states this "is identical to what was used when training the tokenizer".Diagnosis
The characters were lost in 05c0e1d3907cd5a2dcd0c36735a31f7b5c11ba07 ("rm slow tokenizers", #40936). That commit touched the file for unrelated reasons, but the literals inside the set were rewritten:
So the set went from 11 distinct entries to 3 (
{U+0020, U+3000, U+FFFC}), and U+2002, U+2003, U+2005, U+2008, U+2009, U+200A and U+202F now pass through unnormalized. The""in the current set is what is left of U+0084.It was a source-text rewrite, not a logic change, so nothing failed: the existing tests never exercised those characters. I checked the rest of that commit for the same damage — this is the only file affected.
Reproducer
Against AI-Sweden-Models' released
spiece.model(viahf-internal-testing/gpt-sw3-126m-instruct, the checkpoint already used bytest_tokenizer_integration), runningpreprocess_textwith the current set vs the pre-#40936 set and encoding the result:main"en text"['▁en', '<0xE2>', '<0x80>', '<0x89>', 'text']['▁en', '▁text']"en text"['▁en', '<0xE2>', '<0x80>', '<0x83>', 'text']['▁en', '▁text']"en text"['▁en', '<0xE2>', '<0x80>', '<0xAF>', 'text']['▁en', '▁text']Same for U+2002, U+2005, U+2008 and U+200A. Text containing these spaces — common in Swedish news and PDF-extracted text, which is what this model is trained on — falls back to byte tokens instead of the word tokens the model saw in training.
U+3000 and U+FFFC survived the rewrite and are unchanged here. U+0084 is restored for fidelity, though
non_printing_characters_realready strips it one line earlier, so it has no effect either way.The change
The set is restored with explicit
\uXXXXescapes and one comment line, so the next rewrite of this file cannot silently collapse them again. The stale# fmt : off/# fmt : onmarkers (note the space — they were never recognized by the formatter) are dropped with the one-liner they guarded.Tests
New
test_preprocess_text_normalizes_unicode_whitespaceassertspreprocess_text(f"en{ws}text") == "en text"for all eight spaces, using the existing fixture. It fails onmainand passes here; reverting only the source change makes it fail again.ruff checkandruff format --checkclean at the pinned 0.14.10.Before submitting
Disclosure: I am not a first-time contributor (#47238), and this fix was written with AI assistance. I found the regression by scanning for duplicate members in set literals, confirmed it by diffing the code points across the file's history, and verified the tokenizer output myself against the released checkpoint before opening this.
Who can review?
@ArthurZucker @itazap
🤖 Generated with Claude Code