Skip to content

fix(checkpoint): repair v1 state_dict loads, HF quant methods, pickling (#4070) - #4085

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4070-statedict
Nov 15, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4070-statedict

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #4070

Items 1-2 are regressions from #4061.

TL;DR

Five checkpoint-interop fixes, 13 files, +484/-16. The worst one was silent:
load_state_dict(model.state_dict()) copied v1 qzeros into a v2 kernel and
corrupted every GPTQ v1 model while strict=True reported success. The new
flow converts a self round-trip in place and leaves the runtime layout alone
when a partial load carries no qzeros.

1. The problem

Item Old mechanism Evidence
1 _save_to_state_dict emits checkpoint v1 words, but _load_from_state_dict copied them into the v2 buffer and set _qzeros_format = 1 without converting gptqmodel/nn_modules/qlinear/__init__.py:1285 (save) and :1300 (load) before this PR
2 _load_from_state_dict reset _qzeros_format from self.format even when qzeros was not loaded gptqmodel/nn_modules/qlinear/__init__.py:1300
3 to_dict() wrote quant_method: self.method instead of export_quant_method() gptqmodel/quantization/config.py:5192
4 Packed GGUF wrote quant_method: "gguf", which transformers routes to its own GgufHfQuantizer gptqmodel/quantization/config.py:5192 + gptqmodel/models/writer.py:215
5 optimize() stored torch_compile(bound method) on the instance; pickle cannot serialize it gptqmodel/nn_modules/qlinear/torch.py:393

2. The new flow

GPTQQuantLinear._load_from_state_dict now keys off the layout the loader
actually recorded (_checkpoint_format_on_disk), converts a v1 checkpoint into
the v2 runtime layout for REQUIRES_FORMAT_V2 kernels, and skips the format
update entirely when prefix + "qzeros" is absent.

flowchart TD
    A["model.load_state_dict(state_dict)"] --> B["GPTQQuantLinear._load_from_state_dict"]
    B --> C["super()._load_from_state_dict<br/>copies the words"]
    C --> D{"prefix + qzeros<br/>in state_dict?"}
    D -- no --> E["keep _qzeros_format<br/>partial load, no relabel"]
    D -- yes --> F{"_checkpoint_format_on_disk"}
    F -- "GPTQ / gemm" --> G{"REQUIRES_FORMAT_V2?"}
    G -- yes --> H["convert_gptq_v1_to_v2_format_module<br/>_qzeros_format = 2"]
    G -- no --> I["_qzeros_format = 1"]
    F -- "GPTQ_V2" --> J["_qzeros_format = 2"]
    F -- "unset (export shell)" --> K["restore from self.format<br/>caller owns conversion"]
    E --> L["_clear_runtime_weight_cache"]
    H --> L
    I --> L
    J --> L
    K --> L
    style E fill:#d9f2d9,stroke:#2e7d32
    style H fill:#d9f2d9,stroke:#2e7d32
Loading

The unset branch is deliberate: get_model_with_quantize() builds an export
shell from the target qcfg, and accelerate fills it through
_load_from_state_dict before the writer applies
convert_loaded_qzeros_for_format_change. Keeping the historical restore there
avoids double-converting the shell.

to_dict() now publishes export_quant_method(), and quant_method (a
deprecated alias) can no longer override the canonical method key. Packed GGUF
publishes gptqmodel_gguf and registers a refusing HF quantizer.

flowchart TD
    A["BaseQuantizeConfig.to_dict"] --> B["export_quant_method()"]
    B --> C["_serialize_export_quant_method"]
    C -- "METHOD.GGUF" --> D["quant_method: gptqmodel_gguf"]
    C -- "METHOD.AWQ" --> E["quant_method: awq"]
    C -- "METHOD.GPTQ" --> F["quant_method: gptq"]
    D --> G["transformers get_hf_quantizer"]
    G --> H["GPTQModelGGUFHfQuantizer.__init__<br/>raise ValueError"]
    E --> I["AwqHfQuantizer"]
    F --> J["GptqHfQuantizer"]
    style H fill:#f8d7da,stroke:#b02a37
    style I fill:#d9f2d9,stroke:#2e7d32
    style J fill:#d9f2d9,stroke:#2e7d32
Loading

TorchLinear.__getstate__ drops the compiled dequantize_weight instance
attribute; the class method is the eager fallback.

flowchart TD
    A["TorchLinear.optimize()"] --> B["self.dequantize_weight =<br/>torch_compile(bound method)"]
    B --> C["torch.save(model)"]
    C --> D["TorchLinear.__getstate__"]
    D --> E["pop dequantize_weight"]
    E --> F["pickle succeeds"]
    F --> G["torch.load restores the class method"]
    G --> H["eager dequantize_weight fallback"]
    style H fill:#d9f2d9,stroke:#2e7d32
Loading

The committed reference page is
docs/checkpoint-interop.md sections 9-11,
linked from README.md.

3. Before / after measurements

Hardware: RTX 4090 GPU-50a20676-a7a4-8bb3-eb3d-f191fdc9fa42 (CUDA) and
R9700 HIP_VISIBLE_DEVICES=2 (ROCm). Real model:
/monster/data/model/Llama-3.2-1B-Instruct-GPTQ-4bits-gp128 (v1, g128,
gptq_triton and gptq_torch).

Item 1: load_state_dict(model.state_dict()) identity

Before (main):

before  ppl=[19.8636, 6.074, 8.5159] next=' Berlin' p=0.8265
after   ppl=[931382.9221, 1317228.6913, 1035423.227] next='imid' p=0.0104
runtime_after=1 keys_missing=0 unexpected=0

After this PR:

before  ppl=[19.8636, 6.074, 8.5159] next=' Berlin' p=0.8265
after   ppl=[19.8636, 6.074, 8.5159] next=' Berlin' p=0.8265
runtime_after=2 keys_missing=0 unexpected=0

The absolute PPL depends on the eval text, so it differs from the issue's
9.82 / 1856980.9; the failure mode and next token are identical
(' Berlin' -> 'imid'). The synthetic fixture's logits moved by relative
0.406 before the fix and 0.0 after it. gptq_torch and gptq_triton
produced the same numbers on CUDA. On ROCm gptq_torch the same identity
holds: before/after [20.1685, 6.1908, 8.4766], ' Berlin' both times, and
the partial-load round trip stayed at 6.1908 ' Berlin'.

Item 2: partial load

Before (main):

after partial format 1 state_dict word -2004318072 ppl 6.2272 next ' Berlin'
saved word -2004318072
reload ppl 868348.8651 next ' clock'

After this PR:

after partial format 2 state_dict word 2004318071 ppl 6.074 next ' Berlin'
saved word 2004318071
reload ppl 6.074 next ' Berlin'

Item 3: RTN AWQ export method

RTNConfig(bits=4, group_size=128, format=FORMAT.GEMM).to_dict():

Before After
method: gptq, quant_method: gptq, format: gemm method: gptq, quant_method: awq, format: gemm

transformers.quantizers.AutoQuantizationConfig.from_dict(payload) now selects
AwqConfig for GEMM/GEMV/GEMV_FAST/LLM_AWQ and GPTQConfig for GPTQ/GPTQ_V2.
The generic QuantizeConfig.from_quant_config(payload) round-trips to
RTNConfig with export_quant_method() == METHOD.AWQ; the deprecated
quant_method alias no longer switches the quantizer back to GPTQ.

Item 4: packed GGUF quant_method

Before After
quant_method: "gguf" -> transformers GgufHfQuantizer -> random weights quant_method: "gptqmodel_gguf" -> registered GPTQModelGGUFHfQuantizer raises
gguf LOADED finite True sum -0.2945389747619629
gptqmodel_gguf RAISED ValueError GPT-QModel packed GGUF checkpoints cannot be loaded by `transformers`; load them with `GPTQModel.load` or vLLM. (quant_method: 'gptqmodel_gguf')

Already-published checkpoints with quant_method: "gguf" (plus
method: "gguf") still load through GPTQModel.load; gguf remains accepted
by _normalize_quant_method.

Item 5: pickling

Before: PicklingError: Can't pickle <function TorchLinear.dequantize_weight ...>.
After: torch.save wrote 41,861 bytes and torch.load restored a working
LlamaForCausalLM; logits after reload are identical to the live model.

4. Failure and ambiguity contract

Situation Before After
load_state_dict(state_dict()) on a v1 model v1 words copied into a v2 kernel, silent corruption exact v1 to v2 conversion, bit-identical
Partial load without qzeros _qzeros_format flipped to 1, next save garbage runtime layout kept
Export shell loaded by accelerate (_checkpoint_format_on_disk unset) old restore from self.format unchanged; writer owns conversion
RTN format="gemm" quant_method: gptq + format: gemm quant_method: awq
New packed GGUF in HF gguf collision, random weights gptqmodel_gguf refuses loudly
Legacy packed GGUF in HF gguf, random weights unchanged (published files cannot be rewritten)
torch.save(model) after optimize() PicklingError round-trips, runs eager

5. Tests

CUDA_VISIBLE_DEVICES=GPU-50a20676-a7a4-8bb3-eb3d-f191fdc9fa42 \
  PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest \
  tests/test_checkpoint_interop_4053.py tests/test_checkpoint_compat.py \
  tests/qcfg/test_config_serialization.py tests/test_weight_only_config.py -q
-> 81 passed

HIP_VISIBLE_DEVICES=2 PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest \
  tests/test_checkpoint_interop_4053.py tests/test_checkpoint_compat.py \
  tests/qcfg/test_config_serialization.py tests/test_weight_only_config.py -q
-> 80 passed, 1 skipped (gptq_triton is CUDA-only on ROCm)

PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest \
  tests/qcfg tests/test_inmemory_qzero_format.py \
  tests/test_format_conversion_flow.py tests/test_weight_only_config.py \
  tests/test_serialization.py -q
-> 154 passed

ruff check gptqmodel docs/eora tests setup.py
-> All checks passed!

New regression tests: test_load_state_dict_round_trip_preserves_v1_model
(Torch + Triton), test_partial_load_state_dict_keeps_runtime_qzeros_layout,
test_optimized_torch_linear_model_pickles,
test_hf_from_pretrained_refuses_packed_gguf,
test_rtn_config_serializes_export_quant_method,
test_gguf_config_publishes_non_colliding_quant_method,
test_legacy_gguf_quant_method_still_loads, and
test_hf_compat_warning_for_packed_gguf.

6. Reviewer notes

…ng (#4070)

Items 1-2 are regressions from #4061. state_dict() emits checkpoint-layout v1 qzeros while Torch/Triton decode v2, so load_state_dict() must convert a self round-trip and must not relabel the runtime layout on a partial load. Serialize export_quant_method() so RTN AWQ exports publish quant_method: awq, publish packed GGUF under the non-colliding gptqmodel_gguf name with a refusing HF quantizer, and drop the compiled dequantize_weight from TorchLinear's pickle state.
# Conflicts:
#	README.md
#	gptqmodel/__init__.py
#	gptqmodel/models/writer.py
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