From 5ea0f393cef3c154eecfcd93fcada04383fbafa4 Mon Sep 17 00:00:00 2001 From: David-Wu1119 <133224895+David-Wu1119@users.noreply.github.com> Date: Thu, 17 Sep 2026 17:03:49 -0400 Subject: [PATCH 1/2] Restore the Unicode whitespace set in the GPT-SW3 tokenizer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `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 05c0e1d390 ("rm slow tokenizers", #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 --- .../models/gpt_sw3/tokenization_gpt_sw3.py | 18 +++++++++++++++--- .../gpt_sw3/test_tokenization_gpt_sw3.py | 6 ++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/transformers/models/gpt_sw3/tokenization_gpt_sw3.py b/src/transformers/models/gpt_sw3/tokenization_gpt_sw3.py index 81ec20053d20..6c576d6308f3 100644 --- a/src/transformers/models/gpt_sw3/tokenization_gpt_sw3.py +++ b/src/transformers/models/gpt_sw3/tokenization_gpt_sw3.py @@ -120,9 +120,21 @@ def __init__( self.keep_accents = keep_accents # Used for whitespace normalization in input texts - # fmt : off - self.whitespaces = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "", ""} - # fmt : on + # Escapes are used on purpose: these code points are easy to lose to editors and + # automated rewrites that normalize unusual whitespace in source files. + self.whitespaces = { + "\u0020", # space + "\u2002", # en space + "\u2003", # em space + "\u2005", # four-per-em space + "\u2008", # punctuation space + "\u2009", # thin space + "\u200a", # hair space + "\u202f", # narrow no-break space + "\u3000", # ideographic space + "\ufffc", # object replacement character + "\u0084", # control character, also removed by non_printing_characters_re + } # Regular expression to remove non-printing characters (e.g. some unicode control chars) in preprocessing self.non_printing_characters_re = re.compile( diff --git a/tests/models/gpt_sw3/test_tokenization_gpt_sw3.py b/tests/models/gpt_sw3/test_tokenization_gpt_sw3.py index 06d03769e84f..5bbe01559153 100644 --- a/tests/models/gpt_sw3/test_tokenization_gpt_sw3.py +++ b/tests/models/gpt_sw3/test_tokenization_gpt_sw3.py @@ -97,6 +97,12 @@ def test_full_tokenizer(self): ) # fmt: on + def test_preprocess_text_normalizes_unicode_whitespace(self): + tokenizer = GPTSw3Tokenizer(SAMPLE_VOCAB, name_or_path="test") + + for whitespace in ["\u2002", "\u2003", "\u2005", "\u2008", "\u2009", "\u200a", "\u202f", "\u3000"]: + self.assertEqual(tokenizer.preprocess_text(f"en{whitespace}text"), "en text") + def test_fast_encode_decode(self): tokenizer = GPTSw3Tokenizer(SAMPLE_VOCAB, name_or_path="test") texts = ["This is a test", "I was born in 92000, and this is falsé."] From 313b4119816eaa3e2377cd27ff0e04630d9d72a4 Mon Sep 17 00:00:00 2001 From: David-Wu1119 <133224895+David-Wu1119@users.noreply.github.com> Date: Sat, 19 Sep 2026 01:59:06 -0400 Subject: [PATCH 2/2] Drop the comment explaining the escapes Applies the review suggestion. Co-Authored-By: Claude Opus 5 --- src/transformers/models/gpt_sw3/tokenization_gpt_sw3.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/transformers/models/gpt_sw3/tokenization_gpt_sw3.py b/src/transformers/models/gpt_sw3/tokenization_gpt_sw3.py index 6c576d6308f3..de93ee53f0bf 100644 --- a/src/transformers/models/gpt_sw3/tokenization_gpt_sw3.py +++ b/src/transformers/models/gpt_sw3/tokenization_gpt_sw3.py @@ -120,8 +120,6 @@ def __init__( self.keep_accents = keep_accents # Used for whitespace normalization in input texts - # Escapes are used on purpose: these code points are easy to lose to editors and - # automated rewrites that normalize unusual whitespace in source files. self.whitespaces = { "\u0020", # space "\u2002", # en space