Skip to content

fix(loader): reject tokenizer-less checkpoints and repair pad token (#6361) - #6386

Merged
Qubitium merged 1 commit into
mainfrom
fix/6361-empty-tokenizer
Nov 29, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/6361-empty-tokenizer

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #6361

Problem

Transformers 5 no longer raises for a checkpoint directory that has config.json but no tokenizer.json / tokenizer_config.json / vocab.json. It builds a one-token tokenizer ({'<|endoftext|>': 0}) that encodes every string to []. GPT-QModel accepted it, so:

  • quantize() blamed the calibration data (Calibration dataset is empty ... Lower min_length);
  • generate() failed with cannot reshape tensor of 0 elements ... [1, 0, -1, 64];
  • the empty tokenizer's pad_token_id=0 (token "!" in the real Qwen2.5 vocabulary) was written into config.pad_token_id and generation_config.pad_token_id;
  • the documented quantize(tokenizer=real) override re-derived the pad token from the already-mutated 0, saving pad_token: "!" / pad_token_id: 0.

Fix

Validation (gptqmodel/utils/hf.py). Every loaded tokenizer is checked against the model config:

  • len(tokenizer) <= 1 is the empty-tokenizer signature;
  • ordinary prose must encode to at least one non-whitespace token;
  • a vocabulary far below config.vocab_size is rejected only when it contains nothing but special tokens (tokenizer_size * 10 < vocab_size). The check is relative, never an absolute token floor, so a legitimate model with a vocabulary in the tens still loads; the ratio also tolerates padded embeddings (e.g. Llama-3's 128256 rows for 128000 tokens).

A failed tokenizer is treated exactly like a missing one: the config pad/bos/eos values Tokenicer touched are restored and the load raises an actionable ValueError naming the directory, the missing tokenizer.json / tokenizer_config.json / vocab.json, the loaded token count vs vocab_size, and the GPTQModel.load(..., tokenizer=...) / quantize(..., tokenizer=...) overrides.

Explicit tokenizer override. GPTQModel.load(), from_pretrained() and from_quantized() now accept tokenizer=, so the error's suggested fix is usable for both dense and quantized checkpoints.

Pad repair. load_explicit_tokenizer() takes the pad candidate from the supplied tokenizer's own pad/eos metadata (not from the possibly stale model_config.pad_token_id), aligns model.config.pad_token_id, and quantize() / requantize() / eora align generation_config.pad_token_id. A real Qwen2.5 tokenizer now yields <|fim_pad|> / 151662 in all three saved files, matching the tokenizer-present reference. BaseQModel.__init__ applies the same repair and aligns the generation config after autofix_hf_model_config.

Tests

New tests/test_missing_tokenizer_6361.py (CPU-only, no network; tiny Qwen2 checkpoint plus the real tokenizer from a local Qwen2.5 checkpoint):

  • tokenizer-less load raises the actionable error (config-only directory, so the failure is before model construction);
  • config pad ids are not mutated and no model/generation config is built on failure; an explicitly supplied empty tokenizer is rejected the same way;
  • quantize(tokenizer=real) on a checkpoint whose config still carries pad_token_id=0 saves pad_token / pad_token_id identical to the tokenizer-present reference;
  • a quantized checkpoint with its tokenizer files removed fails without an override and loads with tokenizer=;
  • a valid tokenizer still loads unchanged;
  • a legitimate small vocabulary (33 tokens vs vocab_size 33 and 50000) still loads.

Red on origin/main (the three bug-targeted tests fail; the two legitimate-tokenizer guards pass), green on the branch: 8 passed.

Also updated synthetic test fixtures that previously loaded tokenizer-less checkpoints (they now write a byte-level tokenizer that survives the loader's class selection). Suites run with CUDA_VISIBLE_DEVICES="":

  • test_missing_tokenizer_6361.py — 8 passed
  • test_loader*.py, test_calibration_config.py, test_quantize*.py, test_quantized_embeddings.py — passed
  • test_local_tokenizer_loading.py, test_local_model_loading.py, test_package_tokenizer_selection.py, test_remote_only_tokenizer.py, test_qwen2_family_compat.py, test_no_transformers_family_imports.py, test_checkpoint_subfolder_resolution.py — passed
  • test_calibration_auto_batch_entrypoints_5504.py, test_norm_requant.py, test_norm_lifecycle.py, test_auto_adapter_generate.py, test_tiny_moe_quant_smoke.py, test_layer_type_replay.py, test_internvl_*_4973.py/4799, test_vl_legacy_checkpoint_resave_4802.py, test_awq_native_shape_guard_5345.py, test_checkpoint_dynamic_exclusion_sources.py, test_missing_processor_metadata_6295.py, test_requantize_large_hidden_norm_6231.py — passed
  • test_checkpoint_quantization.py — 42 passed

ruff check on every changed file: clean.

…empty tokenizer

Transformers 5 builds a one-token tokenizer for a checkpoint directory that has
config.json but no tokenizer.json/tokenizer_config.json/vocab.json. GPT-QModel
accepted it: calibration tokenized every row to [], generate() crashed on a
zero-length tensor, and the empty tokenizer's pad id 0 (token "!" in Qwen2.5)
was written into config.pad_token_id and generation_config.pad_token_id.

- Validate the loaded tokenizer against the model config: reject a tokenizer
  that is degenerate (<=1 token), cannot encode ordinary prose, or has a
  special-token-only vocabulary far below the embedding (relative ratio, so
  legitimate tiny-vocab models still load).
- Raise an actionable error that names the directory, the missing tokenizer
  files, and the `tokenizer=` overrides; restore the config pad/bos/eos values
  Tokenicer touched before the failure.
- Accept `tokenizer=` on GPTQModel.load()/from_pretrained()/from_quantized()
  so a real tokenizer can replace a missing one.
- Repair the pad token from the supplied tokenizer's own metadata in
  quantize()/requantize()/eora and align generation_config.pad_token_id, so a
  previously mutated pad id 0 no longer wins.
- Add tests/test_missing_tokenizer_6361.py; update synthetic fixtures that
  relied on loading a tokenizer-less checkpoint.

Fixes #6361
@Qubitium
Qubitium marked this pull request as ready for review November 29, 2026 12:58
@Qubitium
Qubitium merged commit e8e7327 into main Nov 29, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/6361-empty-tokenizer branch November 29, 2026 12:58
Qubitium pushed a commit that referenced this pull request Nov 29, 2026
…6386)

Main now rejects a checkpoint whose tokenizer cannot encode ordinary text.
The legacy-qzeros fixture saved a 3-token WordLevel vocabulary, so every test
in the file failed at GPTQModel.load after #6386. Use the shared word-level
tokenizer instead; the fixture is test-only.
Qubitium added a commit that referenced this pull request Nov 29, 2026
…#6375) (#6392)

* test(loader): cover HF re-save retag for legacy INT3 qzeros (#6375)

* test(loader): make INT3 legacy fixture tokenizer usable (#6375)

* fix(hf): materialize 3-bit symmetric qzeros (#6375)

The HF bridge defers symmetric qzeros and regenerates them from the
constant zero point. qzeros_symmetric_word() only defines a single
constant word for 2/4/8-bit continuous packing, so every 3-bit symmetric
HF load raised after #6386. Pack the constant 3-bit zero point with the
10-1-10-1-10 layout instead.

---------

Co-authored-by: agent <agent@local>
Qubitium added a commit that referenced this pull request Nov 29, 2026
…follow-up) (#6391)

* fix(offload): reselect resident CPU Marlin with deferred qzeros (#6152 follow-up)

`_marlin_needs_offload_kernel` used `checkpoint_payload_is_meta()` as the
offload test. `_HF_DEFER_SYM_QZEROS` hides a legacy symmetric checkpoint's
`qzeros`, so an all-CPU Marlin layer has a meta `qzeros` while its packed
weight is resident. The bridge then kept Marlin and `post_init` raised
`ValueError: GPTQ Marlin requires CUDA tensors, got cpu` instead of
reselected the all-CPU load.

Use the primary packed weight (`runtime_device`) for the resident-CPU
decision, and rebuild the deferred `qzeros` on the replacement kernel when a
class swap left it as a meta parameter. The offloaded-layer refusal is
unchanged: a meta `qweight` still raises the documented RuntimeError.

Regression tests cover the synthetic bridge path, the all-CPU
`device_map="auto"` + `max_memory` load of a legacy checkpoint, and the
unchanged CPU-offload refusal.

* test(offload): give the legacy fixture a tokenizer that encodes text (#6386)

Main now rejects a checkpoint whose tokenizer cannot encode ordinary text.
The legacy-qzeros fixture saved a 3-token WordLevel vocabulary, so every test
in the file failed at GPTQModel.load after #6386. Use the shared word-level
tokenizer instead; the fixture is test-only.

* test(offload): drop the tokenizer imports the fixture no longer uses

---------

Co-authored-by: agent <agent@local>
Qubitium added a commit that referenced this pull request Nov 29, 2026
)

#6386 rejects a checkpoint whose tokenizer cannot encode ordinary text, and
transformers 5 no longer raises for a checkpoint without tokenizer files.
Several tiny fixtures wrote a 3-5 token `WordLevel` vocabulary (all special
tokens) or no tokenizer at all next to a model with `vocab_size` 32-1024, so
`GPTQModel.load` now fails at fixture setup.

Replace those vocabularies with a real byte-level tokenizer:

* `tests/tiny_tokenizer.py` where the embedding has room for its alphabet;
* an explicit 64-token byte-level BPE where the fixture pins token ids
  (`test_qwen2_vl_local_port.py`: pad/bos/eos/unk 0-3, vision tokens 60-63;
  `module_tree/test_inkling2_loop_plan.py`: image token id 4, EOS id 3).

No product code changes; the new validation is exercised as-is.

Fixes #6400

Co-authored-by: agent <agent@local>
Qubitium added a commit that referenced this pull request Nov 29, 2026
…6402) (#6411)

#6386 copied the tokenizer's `pad_token_id` onto the loaded model's
`generation_config` unconditionally. That discards a `generation_config.json`
which pins a pad id, and turns an explicit `null` ("no pad token") into the
tokenizer's id -- which changes `generate()` output.

The backfill only ever existed to repair a config that an *empty* tokenizer had
already rewritten (#6361). #6386 now rejects those tokenizers, and the repair
happens on `config.pad_token_id` inside `load_explicit_tokenizer`, before the
model is constructed, so the derived generation config already carries the
repaired id. Copying the tokenizer's id over `generation_config` afterwards
therefore undoes both the checkpoint policy and that repair.

Drop the overwrite and document why nothing replaces it.

Fixes #6402

Co-authored-by: agent <agent@local>
Qubitium added a commit that referenced this pull request Nov 29, 2026
…6403) (#6412)

#6386 added `tokenizer=` to `GPTQModel.load()` and forwarded it to the
resolved definition unconditionally, so the default `tokenizer=None`
became a load kwarg for every definition. `DeepSeekV41FlashQModel`
validates its load arguments and rejected the whole V4.1 path with
`TypeError: Unsupported DeepSeek V4.1 Flash load arguments: tokenizer`
before the checkpoint was read.

- `auto.py`: add `tokenizer` to the forwarded kwargs only when it is not
  `None`. `None` already means "let the definition load the checkpoint's
  own", so omitting it is the pre-#6386 behaviour.
- `deepseek_v41_flash.py`: pop `tokenizer` before the strict-argument
  check and honour it through `load_hf_tokenizer()`, so the documented
  `tokenizer=` override works for a V4.1 checkpoint without one.
- `loader.py`: the native-quantized-GGUF redirect had the same unguarded
  `tokenizer=tokenizer_override` (it can be `None`).

Fixes #6403

Co-authored-by: agent <agent@local>
Qubitium added a commit that referenced this pull request Nov 29, 2026
…coder reload fixture (#6433)

#6386 rejects a checkpoint without a usable tokenizer, and
`test_quantized_checkpoint_reloads_with_tied_decoder` saves its tiny
DiffusionGemma source with `save_pretrained` only, so `GPTQModel.load` fails
at fixture setup ("Tokenizer files are missing") before the #5514/#5716
reload path is reached.

Write `tests/tiny_tokenizer.py::write_tiny_tokenizer` next to the model, as
#6407 did. `vocab_size=60` keeps every tokenizer id below the config's
image/boi/eoi ids (60-62); the calibration rows and forward inputs are
pre-tokenized ids 1-56, so the test still feeds exactly the same data.

The remaining files from the same sweep are covered by the open draft #6420
(#6404); this is the one rejected fixture it does not touch.

No product code changes; the #6386 check is exercised as-is.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Qubitium added a commit that referenced this pull request Nov 29, 2026
#6386's `load_hf_tokenizer` rejects a checkpoint tokenizer that cannot encode
ordinary text. 47 nodes across 13 files still built one of three unusable
shapes; #6407 already migrated the other eight files.

Three root causes:

* `build_wordlevel_tokenizer` (`tests/tiny_remote_models.py`) produced a
  `WordLevel` model. Transformers 5 rebuilds a *saved* `WordLevel` as a `BPE`
  without an unknown token, so the tokenizer was usable in memory and encoded
  prose to nothing once written to disk. It is now a byte-level BPE with the
  same special tokens and ids (`<s>`=0, `</s>`=1, `<unk>`=2, `<pad>`=3) and
  every id below `vocab_size`. This alone fixes five files.
* All-special or unk-less `WordLevel` fixtures in the rest; they now use the
  shared `write_tiny_tokenizer`, except `test_local_processor_loading`, which
  keeps its four-token vocabulary (the test pins ids 1 and 3) and only gains
  the missing `unk_token`.
* Checkpoints written with no tokenizer files at all (`test_fp8`,
  `test_fail_closed_checkpoint_5218`, `test_construction_global_state_5421`).

`test_model_load_lock_4204._write_tiny_rtn_checkpoint` is fixed in place
rather than duplicated: `test_compile_load_race_5209` imports it, and its only
other user is the GIL-skipped test in that file.

No product code changes; the guard is untouched.

Fixes #6404

Co-authored-by: agent <agent@local>
Qubitium added a commit that referenced this pull request Dec 1, 2026
#6557)

#6386 rejects checkpoints without tokenizer files; the synthetic OLMoE
fixture saved none.

Fixes #6556

Co-authored-by: Claude Opus 5.5 <[email protected]>
Qubitium added a commit that referenced this pull request Dec 1, 2026
…6637)

`test_awq_batch_size_one_quantizes_a_mamba_hybrid` failed in a random
fraction of fresh processes, independent of test order, device or xdist.
The failures looked like "the tokenizer ... is unusable (129/130 token(s),
model vocab_size=128)" or "WordLevel error: Missing [UNK] token".

Cause: the fixture built `{tok0..tok127 -> 0..127}` and then added
`[PAD] -> 0` and `[UNK] -> 1`, so ids 0 and 1 each had two names. The
tokenizers WordLevel model keeps an id -> token map whose winner depends on
the Rust hash-map seed, which differs per process. On save only one name per
id survives. Across 20 fresh processes, 3 kept both specials (128 tokens),
11 lost `[PAD]` (re-added as id 128, 129 tokens), and 6 lost `[UNK]`, which
cannot be loaded at all. The loader correctly rejects those tokenizers
(#6386), so the test failed whenever the seed dropped a special.

Fix: build the vocabulary with one token per id (`[PAD]`=0, `[UNK]`=1,
`tok2..tok127`) and assert the ids are exactly `range(vocab_size)`, so a
colliding fixture now fails deterministically instead of by seed. The test's
calibration rows (ids 3..26) and its assertions are unchanged.

Co-authored-by: Claude Opus 5.5 <[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

1 participant