Skip to content

Restore the Unicode whitespace set in the GPT-SW3 tokenizer - #48912

Merged
itazap merged 2 commits into
huggingface:mainfrom
David-Wu1119:fix/gpt-sw3-whitespace-set
Sep 28, 2026
Merged

itazap merged 2 commits into
huggingface:mainfrom
David-Wu1119:fix/gpt-sw3-whitespace-set

Conversation

@David-Wu1119

@David-Wu1119 David-Wu1119 commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

CPU CI GPU run-slow

What does this PR do?

Restores the seven Unicode space characters that GPTSw3Tokenizer stopped normalizing after a source-level rewrite.

preprocess_text maps everything in self.whitespaces to 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:

before  U+0020 U+2009 U+200A U+202F U+2005 U+3000 U+2002 U+0020 U+2008 U+2003 U+FFFC U+0084
after   U+0020 U+0020 U+0020 U+0020 U+0020 U+3000 U+0020 U+0020 U+0020 U+0020 U+FFFC (empty)

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 (via hf-internal-testing/gpt-sw3-126m-instruct, the checkpoint already used by test_tokenizer_integration), running preprocess_text with the current set vs the pre-#40936 set and encoding the result:

input on main with this PR
"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_re already strips it one line earlier, so it has no effect either way.

The change

The set is restored with explicit \uXXXX escapes and one comment line, so the next rewrite of this file cannot silently collapse them again. The stale # fmt : off / # fmt : on markers (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_whitespace asserts preprocess_text(f"en{ws}text") == "en text" for all eight spaces, using the existing fixture. It fails on main and passes here; reverting only the source change makes it fail again.

pytest tests/models/gpt_sw3/test_tokenization_gpt_sw3.py
54 passed, 8 skipped

ruff check and ruff format --check clean at the pinned 0.14.10.

Before submitting

  • Did you read the contributor guideline and the Pull Request checks?
  • Was this discussed/approved via a Github issue or the forum? — no, filed directly as a regression with a reproducer.
  • Did you write any new necessary tests?

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

`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>
Copilot AI lite review requested due to automatic review settings September 17, 2026 21:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@itazap itazap left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! small nit and good for me

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 313b411, thanks for the review!

Applies the review suggestion.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: gpt_sw3

@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 35274569671:2
Result: success | Jobs: 3 | Tests: 88 | Failures: 0 | Duration: 3m 17s

@itazap
itazap enabled auto-merge September 28, 2026 12:19
@itazap
itazap added this pull request to the merge queue Sep 28, 2026
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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.

Merged via the queue into huggingface:main with commit 885320e Sep 28, 2026
34 checks passed
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.

5 participants