Skip to content

fix(models): stop rewriting a checkpoint's generation pad id on load (#6402) - #6411

Merged
Qubitium merged 2 commits into
mainfrom
fix/6402-pad-token-id
Nov 29, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/6402-pad-token-id

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #6402

What was wrong

#6386 added this to BaseQModel.__init__:

if self.tokenizer is not None:
    generation_config = getattr(self.model, "generation_config", None)
    pad_token_id = getattr(self.tokenizer, "pad_token_id", None)
    if generation_config is not None and pad_token_id is not None:
        generation_config.pad_token_id = pad_token_id

The loader always resolves a tokenizer (from the checkpoint when the caller passes none) and hands it to BaseQModel, so this ran on every load. It overwrote the checkpoint's own generation_config.json:

  • a pinned pad id was replaced by the tokenizer's (151643 → 0 in the ovis clip-qwen2 policy);
  • an explicit null ("no pad token") became the tokenizer's id, which changes generate() output (qwen3 marin: greedy/beam diverged at token index 10).

pad_token_id is part of the generation policy, not a tokenizer detail, so a load must not rewrite it.

Why the block can simply go

The backfill only ever existed to repair a config that an empty tokenizer had already rewritten to 0 (#6361). Since #6386:

  • such tokenizers are rejected outright, and
  • the repair happens on config.pad_token_id inside load_explicit_tokenizer, which runs in load_hf_tokenizer before the model is constructed.

The model therefore derives its generation config from the already-repaired model config, and autofix_hf_model_config carries that repaired id into the on-disk config. Copying the tokenizer's id over generation_config afterwards undoes both. Verified by #6361's own suite: its expected_pad_id is 151662 (<|fim_pad|>), not 0, and those assertions stay green without the block.

Regression test

tests/test_generation_pad_token_id_6402.py (CPU-only, tiny Qwen3 checkpoint + the shared tests/tiny_tokenizer.py byte-level BPE, whose pad id is 0):

  • checkpoint-pinned — generation_config.json pins pad_token_id: 1;
  • checkpoint-unset — generation_config.json pins pad_token_id: null.

Both assert the tokenizer really disagrees (pad_token_id == 0), then that the checkpoint value survives the load.

Red → green

Unpatched (block restored on the same tree):

$ cd tests && CUDA_VISIBLE_DEVICES="" PYTHONPATH=<clone>:<clone>/tests /root/venv/bin/python -m pytest \
    test_generation_pad_token_id_6402.py -q -rs -p no:cacheprovider
PASSED
FAILED test_generation_pad_token_id_6402.py::test_load_preserves_the_checkpoint_generation_pad[checkpoint-unset]
FAILED test_generation_pad_token_id_6402.py::test_load_preserves_the_checkpoint_generation_pad[checkpoint-pinned]
E       assert 0 == None
E       assert 0 == 1
2 failed, 1 passed

The three issue nodes on unpatched main:

$ cd tests && CUDA_VISIBLE_DEVICES="" PYTHONPATH=<clone>:<clone>/tests /root/venv/bin/python -m pytest \
    "test_ovis_local_port.py::test_released_generation_policy_public_load_and_two_safetensors_cycles[clip-qwen2-policy0]" \
    "test_qwen3_local_port.py::test_public_native_selection_tokenizer_generation_and_reload[False-None-marin]" \
    "test_qwen3_local_port.py::test_public_native_selection_tokenizer_generation_and_reload[True-1-marin]" \
    -q --tb=line -p no:cacheprovider
3 failed

With the fix (rebased on 407298ad2, main merged in):

18 passed in 68.25s

(test_generation_pad_token_id_6402.py + test_missing_tokenizer_6361.py + all parametrizations of the two issue nodes.)

Minimal pad-id probe after the fix:

source gen pad : None      loaded gen pad : None
source cfg pad : 0         loaded cfg pad : 0
                           loaded tok pad : 0

Wider run

$ cd tests && CUDA_VISIBLE_DEVICES="" PYTHONPATH=<clone>:<clone>/tests /root/venv/bin/python -m pytest \
    test_generation_pad_token_id_6402.py test_missing_tokenizer_6361.py \
    test_missing_processor_metadata_6295.py test_ovis_local_port.py \
    test_qwen3_local_port.py test_qwen_local_port.py \
    test_layer_type_replay.py test_tiny_moe_quant_smoke.py \
    -q -rs -p no:cacheprovider
314 passed, 11 warnings in 706.87s
$ /root/venv/bin/ruff check gptqmodel/models/base.py tests/test_generation_pad_token_id_6402.py
All checks passed!

test_generation_config_do_sample.py, test_generation_compile_policy_4669.py, test_module_replacement_generation_config_5387.py: 28 passed.

Scope

gptqmodel/models/base.py (deletion + the comment that explains why nothing replaces it) and one new test file. quantize()'s pad alignment is untouched: that path is only reached when the caller supplies a tokenizer, which is the documented #6361 repair.

Draft — not merged, no labels applied.

agent added 2 commits November 29, 2026 14:42
…6402)

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

[BUG] #6386 overwrites a checkpoint generation_config.pad_token_id with the tokenizer pad id (ovis + qwen3-marin red)

1 participant