Skip to content

Refactor-tokenization-more - #42563

Merged
ArthurZucker merged 124 commits into
mainfrom
refactor-tokenization-more
Dec 9, 2025
Merged

ArthurZucker merged 124 commits into
mainfrom
refactor-tokenization-more

Conversation

@ArthurZucker

@ArthurZucker ArthurZucker commented Dec 2, 2025 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

  1. Update TokenizersBackend to simplify converting from sentencepiece.
  2. Add the model attribute to guide the conversion.
  3. Adds the convert_from_spm for special cases when the spm is not straight forward.
  4. Simplify init for TokenizersBackend to support back passing vocab file / merges file path.

@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.

@ArthurZucker
ArthurZucker force-pushed the refactor-tokenization-more branch from c339cc8 to 7fd3d4f Compare December 3, 2025 15:07
@ArthurZucker
ArthurZucker changed the base branch from fix-fallback to main December 3, 2025 15:08
@ArthurZucker
ArthurZucker merged commit 73a13f8 into main Dec 9, 2025
@ArthurZucker
ArthurZucker deleted the refactor-tokenization-more branch December 9, 2025 15:37
@github-actions

github-actions Bot commented Dec 9, 2025

Copy link
Copy Markdown
Contributor

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

run-slow: albert, auto, barthez, bert, big_bird, blenderbot, camembert, clip, clvp, code_llama, codegen, cohere, deberta, deberta_v2, distilbert, dpr

SangbumChoi pushed a commit to SangbumChoi/transformers that referenced this pull request Jan 23, 2026
* On commit to bind them all

* nits

* smnall update

* elif

* super small nit

* BPE!

* fix

* up up up

* fix?

* one typo

* per model updates

* more model specific updates

* more per model updates

* more model specific updates

* simplify default merges

* fiuxp

* update

* update

* style

* fix

* fix colpali

* nits

* simpler regex + big shitty bird

* fixup and fix

* fix codellama

* up

* fix pop on none

* fix parkeet

* fix llama

* big fixup

* fix markul lm

* update common

* fix mbart

* fix seamlessm4T

* fix comment

* torch tests

* nnits and revert UNK idx change

* oh only one deberta

* torch tests

* add convert from spm per model!

* fix last 2 for pegasus

* fix torch tests

* fixes

* fix tests

* check versioned files

* fix processor auto test

* fix custom tok clip

* try this fix

* modeling rag

* fix rag

* roformer the Tokenizers way

* up

* updatge

* fix unk

* update

* fix roberta

* if there is no mapped class and no tokenizer.json its fucked -> just have the mapped class ready!

* fix the rest

* fix copies

* fix doc and copies

* fix mbart50

* fix deberta_v2 test

* fix and simplify whisper :)

* fix big bird default was worng

* fix final

* fixup

* small nit

* a weird way to fix fuyu?

* default xlm roberta to fix kosmo behaviour!

* remove small errors

* last fix?

* fix pixtral

* style

* fix

* quality ta radce

* fix?

* remove something

* remov one code that shouldd not have been there!

* fix ?

* fixup

* update

* fix for custom code

* add a custom model path to make sure custom stuff is registe

* fix trust remote code

* exceeded

* don't

* ouppsy for cohere

* why is this one also affected

* fixup

* fixup

* nits

* fix idefics3 tests

* okay read the processor

* fix the layout.... models

* nits

* codellama needs the bos passed

* fix dpr

* fix?

* fixup

* distilbert defaults

* fix

* clvp update to PythonTokenizer

* bloom

* style

* layoutxlm

* style

* olmo

* only pop when we don't convert from tokenizer.json

* fixup

* hub issue

* id

* fix

---------

Co-authored-by: itazap <[email protected]>
Co-authored-by: [email protected] <[email protected]>
Comment on lines -2085 to -2093
# If tokenizer_file exists and tokenizer has a TokenizersBackend, replace the blank tokenizer with tokenizer.json
if tokenizer_file is not None and hasattr(tokenizer, "_tokenizer"):
from tokenizers import Tokenizer as TokenizerFast

tokenizer._tokenizer = TokenizerFast.from_file(tokenizer_file)
# Re-run post-initialization if the tokenizer has it
if hasattr(tokenizer, "_post_init"):
tokenizer._post_init()
# If only SPM exists, try to get vocab and merges and init to load a tokenizers-backend

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I believe this code should not have been removed. Opening a PR now.

@tomaarsen tomaarsen mentioned this pull request Jan 26, 2026
2 of 5 tasks
ydshieh added a commit that referenced this pull request Aug 15, 2026
…retrained

`_from_pretrained` strips `add_bos_token`/`add_eos_token` from init_kwargs
when a tokenizer.json is present, so `TokenizersBackend.__init__` sees no
explicit bos/eos kwargs and skips calling `update_post_processor()`. For
GPTNeoX (which rebuilds its Rust tokenizer from scratch rather than loading
it directly from tokenizer.json), the post_processor from the JSON file is
applied as-is and is never overridden — causing OLMo-7B to receive an
unexpected EOS token appended to every input, producing garbage generation.

Restore the explicit `update_post_processor()` call at the end of
`GPTNeoXTokenizer.__init__` so the Rust post_processor always reflects the
`add_bos_token`/`add_eos_token` settings.

Regression introduced by #42563 (commit 73a13f8).
Fixes OlmoIntegrationTest::test_model_7b_greedy_generation producing
`.1.1.1.1.1...` garbage instead of coherent text.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
ydshieh added a commit that referenced this pull request Aug 15, 2026
…ned (OLMo garbage generation) (#47988)

[GPTNeoX] Fix post_processor not being overridden when loading from pretrained

`_from_pretrained` strips `add_bos_token`/`add_eos_token` from init_kwargs
when a tokenizer.json is present, so `TokenizersBackend.__init__` sees no
explicit bos/eos kwargs and skips calling `update_post_processor()`. For
GPTNeoX (which rebuilds its Rust tokenizer from scratch rather than loading
it directly from tokenizer.json), the post_processor from the JSON file is
applied as-is and is never overridden — causing OLMo-7B to receive an
unexpected EOS token appended to every input, producing garbage generation.

Restore the explicit `update_post_processor()` call at the end of
`GPTNeoXTokenizer.__init__` so the Rust post_processor always reflects the
`add_bos_token`/`add_eos_token` settings.

Regression introduced by #42563 (commit 73a13f8).
Fixes OlmoIntegrationTest::test_model_7b_greedy_generation producing
`.1.1.1.1.1...` garbage instead of coherent text.

Co-authored-by: ydshieh <[email protected]>
PranshulSoni added a commit to PranshulSoni/transformers that referenced this pull request Sep 5, 2026
…t-processor disagree

huggingface#42563 stripped add_bos_token/add_eos_token from the init kwargs whenever a
tokenizer.json exists, and huggingface#47988 restored GPT-NeoX's unconditional
update_post_processor() call to fix allenai/OLMo-7B-hf. Together they break
checkpoints of the opposite polarity: blab-jhu/test-32m-dec declares
add_bos_token: true next to a [CLS]...[SEP] template, so the flag-driven rebuild
with stripped flags dropped the [CLS] prefix and shifted every recorded
ModernBertDecoder integration expectation (failing on main since huggingface#47988).

_from_pretrained now keeps the flags, and update_post_processor reconciles per
side: a side whose flag is explicitly declared follows the flag, an undeclared
side keeps the serialized behavior, and a backend without a serialized
TemplateProcessing rebuilds exactly as before. Verified against real checkpoints
(blab-jhu restored to the recorded contract, OLMo-7B-hf keeps the huggingface#47988 fix,
llama-2/mistral/pythia/gpt-neox-20b/OLMo-2 unchanged) and added offline
regression fixtures covering both polarities.
tarekziade added a commit that referenced this pull request Sep 7, 2026
…the rebuild

Skipping `update_post_processor()` whenever `tokenizer.json` ships a post-processor
makes the call added by #47988 dead code -- the base class already updates the
post-processor in exactly those cases (`_should_update_post_processor`,
`tokenization_utils_tokenizers.py`) -- so it reintroduces the OLMo bug. Verified on
`allenai/OLMo-7B-hf`: with that patch every input gets `<|endoftext|>` appended again.

The actual root cause is that `_from_pretrained` drops `add_bos_token`/`add_eos_token`
from the init kwargs when a `tokenizer.json` is present, so #47988 rebuilds the
post-processor from flags that have just been erased. The result is "no bos, no eos"
for every GPTNeoX checkpoint: correct for OLMo (which saves `false`/`false`), wrong for
any checkpoint whose post-processor adds special tokens -- `blab-jhu/test-32m-dec`
went from `[CLS] Paris is the capital of` to `Paris is the capital of`.

Keep the flags for the tokenizers whose class opts in, and let the base class rebuild
the post-processor from them. This restores the pre-#42563 behaviour on both
checkpoints (verified locally):

- `allenai/OLMo-7B-hf` (`add_bos_token=false`, `add_eos_token=false`): no EOS appended.
- `blab-jhu/test-32m-dec` (`add_bos_token=true`): `[CLS] Paris is the capital of`, i.e.
  6 tokens. The trailing `[SEP]` its `tokenizer.json` adds is the same pathology as
  OLMo's EOS on a causal LM, and 6 was the expected value from #38967 until #43764
  bumped it to 7 as collateral of the tokenizer refactor. The expected logit slices
  are unchanged, as only the trailing token disappears.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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.

4 participants