Skip to content

Revert "Fix CodeLlama tokenizer dropping leading whitespace on decode (#47488)" - #47861

Merged
ArthurZucker merged 1 commit into
mainfrom
revert-47488-codellama-normalizer
Aug 10, 2026
Merged

ArthurZucker merged 1 commit into
mainfrom
revert-47488-codellama-normalizer

Conversation

@ArthurZucker

@ArthurZucker ArthurZucker commented Aug 10, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

Reverts #47488. Needed for the release.

Follow-up: #47862 fixes #47487 properly — decode-only, encoding byte-identical, no id changes.

#47488 swapped the Metaspace pre-tokenizer for a Prepend("▁") + Replace(" ", "▁") normalizer. Both look equivalent, but the normalizer form changes tokenization in two ways that break checkpoints already in the wild.

1. Added tokens stop matching. The normalizer is applied to the added tokens themselves when the added-vocabulary matcher (daachorse) is rebuilt, so <s> is stored as ▁<s> and no longer matches <s> in the text. Any checkpoint that ships its specials with normalized=True loses them:

tok.tokenize("Hey how are<s>you?<unk> <unk>")
['▁Hey', '▁how', '▁are', '<', 's', '>', 'you', '?', '<', 'unk', '>', '▁<unk>']

2. A spurious metaspace after every special token. The prepend runs on each chunk produced by the added-token split, not once on the sequence. So [INST] How are you doing?<s>[/INST] moves [ from 29961 to 518 — every multi-turn Llama-2 chat prompt tokenizes differently. #47488 wrote this into its own expected outputs rather than treating it as a regression:

-    tokens, ["▁[", "INST", "]", "▁How", "▁are", "▁you", "▁doing", "?", "<s>", "[", "/", "INST", "]"]
+    tokens, ["▁[", "INST", "]", "▁How", "▁are", "▁you", "▁doing", "?", "<s>", "▁[", "/", "INST", "]"]

meta-llama/Llama-2-7b-hf's tokenizer.json is not a reference here — we built it ourselves, before metaspace was fixed.

The round-trip issue reported in #47487 is real but it is decode-only and does not need a pipeline change. It is fixed in #47862, which keeps encoding byte-identical.

This is an exact git revert; tests/models/code_llama/test_tokenization_code_llama.py and tests/models/llama/test_tokenization_llama.py pass (116 passed, 14 skipped).

Who can review?

@itazap

…#47488)"

This reverts commit 1b20eb1.

#47488 replaced the `Metaspace` pre-tokenizer with a `Prepend("▁") + Replace(" ", "▁")`
normalizer. That changes tokenization in two ways that break existing checkpoints.

1. Added-token contents are themselves normalized when the added-vocabulary matcher
   (daachorse) is rebuilt, so `<s>` is stored as `▁<s>` and stops matching `<s>` in the
   text. Any checkpoint shipping its specials with `normalized=True` loses them:

       tok.tokenize("Hey how are<s>you?<unk> <unk>")
       ['▁Hey', '▁how', '▁are', '<', 's', '>', 'you', '?', '<', 'unk', '>', '▁<unk>']

2. The prepend runs on every chunk produced by the added-token split, so a metaspace is
   inserted after every special token. `[INST] How are you doing?<s>[/INST]` moves `[`
   from 29961 to 518, i.e. every multi-turn Llama-2 chat prompt tokenizes differently.
   #47488 wrote this into its own expected outputs rather than treating it as a bug.

The leading-whitespace round-trip reported in #47487 is real, but it is decode-only and
does not need a pipeline change.
@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: code_llama

@ArthurZucker
ArthurZucker merged commit b7e0c20 into main Aug 10, 2026
26 of 27 checks passed
@ArthurZucker
ArthurZucker deleted the revert-47488-codellama-normalizer branch August 10, 2026 07:54
Cyrilvallez pushed a commit that referenced this pull request Aug 10, 2026
…#47488)" (#47861)

This reverts commit 1b20eb1.

#47488 replaced the `Metaspace` pre-tokenizer with a `Prepend("▁") + Replace(" ", "▁")`
normalizer. That changes tokenization in two ways that break existing checkpoints.

1. Added-token contents are themselves normalized when the added-vocabulary matcher
   (daachorse) is rebuilt, so `<s>` is stored as `▁<s>` and stops matching `<s>` in the
   text. Any checkpoint shipping its specials with `normalized=True` loses them:

       tok.tokenize("Hey how are<s>you?<unk> <unk>")
       ['▁Hey', '▁how', '▁are', '<', 's', '>', 'you', '?', '<', 'unk', '>', '▁<unk>']

2. The prepend runs on every chunk produced by the added-token split, so a metaspace is
   inserted after every special token. `[INST] How are you doing?<s>[/INST]` moves `[`
   from 29961 to 518, i.e. every multi-turn Llama-2 chat prompt tokenizes differently.
   #47488 wrote this into its own expected outputs rather than treating it as a bug.

The leading-whitespace round-trip reported in #47487 is real, but it is decode-only and
does not need a pipeline change.
@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.

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.

CodeLlama tokenizer drops leading whitespace on decode round-trip

2 participants