Skip to content

fix(models): honor third-party checkpoint filters and layouts (#4053) - #4061

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4053-load
Nov 14, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4053-load

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Part of #4053 (items 1-3). Items 4-8 are in #4059.

TL;DR

Third-party checkpoints now declare which modules are dense, a format-changing re-save converts the qzeros exactly (or refuses), and save_pretrained() writes checkpoint-layout qzeros while Marlin/ExLlamaV2 fail closed. Net diff: 11 files, +~700 lines including the reference page and regressions.

1. The problem

Item 1: producer exclusions were ignored

The loader decided what to quantize from the module tree plus dynamic (gptqmodel/models/loader.py:2223-2266 on 39cd57a85). It never read AutoAWQ/HF modules_to_not_convert, llm-compressor ignore, or Optimum modules_in_block_to_quantize. validate_checkpoint_qweights returned early for non-GPTQ formats (gptqmodel/utils/model.py:2592), so a never-loaded AWQ placeholder stayed zero-filled and the model produced NaN without an error.

Item 2: a format change copied stale qzeros

get_model_with_quantize (gptqmodel/models/writer.py:2269-2351) reloaded the raw source tensors and wrote them unchanged. It never called convert_gptq_v1_to_v2_format / convert_gptq_v2_to_v1_format (gptqmodel/utils/model.py:925 / 1043), so model.quantize_config.format = X; model.save() wrote v1 words under a gptq_v2 label.

Item 3: save_pretrained() serialized the runtime layout

transformers' save_pretrained() calls state_dict() on the live modules. v2-requiring kernels keep v2 qzeros while checkpoint_format stays gptq; Marlin repacks qweight and empties qzeros/g_idx in post_init; ExLlamaV2 shuffles qweight in place and recorded that only in torch _metadata (gptqmodel/nn_modules/qlinear/exllamav2.py:189), which safetensors drops. The reload was NaN, garbage, or a shape crash.

2. The new flow

Producer filters

flowchart TD
    A["config.json quantization_config"] --> B["from_quant_config"]
    B --> C{"modules_to_not_convert or ignore"}
    C -- yes --> D["-: suffix regex rule"]
    C -- no --> E["keep declared dynamic"]
    D --> F["config.dynamic"]
    E --> F
    B --> G{"modules_in_block_to_quantize"}
    G -- yes --> H["external_module_inclusions()"]
    G -- no --> I["all declared modules eligible"]
    F --> J["loader module filter"]
    H --> J
    I --> J
    J --> K["create_quant_module"]
    K -- "dynamic match is False" --> L["module stays dense"]
    K -- "no exclusion" --> M["quantized module"]
    M --> N["validate_checkpoint_qweights"]
    L --> N
Loading

Format-changing re-save

flowchart TD
    A["from_quantized"] --> B["record _checkpoint_format_on_disk<br/>and layout snapshot"]
    B --> C["quantize_config.format = X"]
    C --> D["save_quantized"]
    D --> E{"resolve_resave_format_conversion"}
    E -- "same format" --> F["copy source tensors"]
    E -- "GPTQ v1 <-> v2" --> G["convert qzeros"]
    E -- "other layout" --> H["raise before writing"]
    G --> I["get_model_with_quantize"]
    I --> J["convert_loaded_qzeros_for_format_change"]
    J --> K["write target checkpoint"]
    style H fill:#f8d7da,stroke:#b02a37
Loading

save_pretrained()

flowchart TD
    A["model.save_pretrained"] --> B["nn.Module.state_dict"]
    B --> C["GPTQQuantLinear._save_to_state_dict"]
    C --> D{"runtime qzeros == checkpoint qzeros"}
    D -- yes --> E["write live buffers"]
    D -- no --> F["write converted copy"]
    B --> G["Marlin / ExLlamaV2 hook"]
    G --> H{"runtime layout applied"}
    H -- yes --> I["raise RuntimeError"]
    H -- no --> E
    style I fill:#f8d7da,stroke:#b02a37
Loading

The full reference page is docs/checkpoint-interop.md (linked from README.md). It also contains the items 4-8 flows landed in #4059; both sections are preserved.

3. What changed

Area File Purpose
config parse gptqmodel/quantization/config.py fold modules_to_not_convert / ignore into -: rules; record Optimum inclusions; preserve original spellings on write
loader gptqmodel/models/loader.py apply inclusion filter; record on-disk format and packing snapshot; mark modules for the save hook
validation gptqmodel/utils/model.py validate qweight for AWQ and shared Marlin/BitBLAS formats
re-save gptqmodel/models/writer.py, gptqmodel/utils/model.py exact v1<->v2 qzero conversion; refuse every other format change before writing
state dict gptqmodel/nn_modules/qlinear/__init__.py emit checkpoint-layout qzeros in _save_to_state_dict
fail closed gptqmodel/nn_modules/qlinear/{marlin,marlin_awq,exllamav2,exllamav2_awq}.py raise for runtime layouts with no inverse
HF bridge gptqmodel/utils/hf_load.py record the checkpoint format on the Optimum/native load path
docs/tests docs/checkpoint-interop.md, README.md, tests/test_checkpoint_interop_4053.py reference page + regressions

4. Before / after evidence

Item 1 (synthetic AutoAWQ Llama-3.2-1B-shaped checkpoint, modules_to_not_convert: ["down_proj"])

The fixture keeps down_proj dense and uses an independent dense oracle (code - zero) * scale; no GPT-QModel dequantizer is used as the reference.

Result
before (39cd57a85) RuntimeError: Cannot restore checkpoint buffers missing from checkpoint: model.layers.0.mlp.down_proj.scales (the issue's AUTO path was NaN)
after loads; down_proj is nn.Linear; logits finite; rel max error vs oracle 2.25e-4 (< 2e-3)

Real AWQ-Llama-3.2-1B-g128-gemm still loads after the new AWQ validation: finite, absmax=13.6640625.
Missing AWQ qweight after the fix raises ValueError: Missing required quantized weights in checkpoint ...; before it failed with the unrelated down_proj.scales restore error.

Item 2 (real checkpoints)

Checkpoint Change Before After
Llama-3.2-1B-Instruct-GPTQ-bit4-g128-symFasle-descFalse gptq -> gptq_v2 rel max logit 1.190463 rel max logit 0.000000, finite
Llama-3.2-1B-Instruct-HF-GPTQV2 gptq_v2 -> gptq rel NaN, finite=False rel 0.000000, finite

Item 3 (real Qwen2.5-0.5B-Instruct-gptq-4bit, Triton)

Before After
saved qzeros byte-equal to source False True
reload logits vs source rel NaN, finite=False rel 0.000000, finite

Marlin / AWQ Marlin / ExLlamaV2 / AWQ ExLlamaV2 now raise from _save_to_state_dict with a message naming the kernel; GPTQModel.save() is unaffected because it rebuilds the shell from the source checkpoint.

5. Tests

Hardware: CUDA 4090 GPU-50a20676-a7a4-8bb3-eb3d-f191fdc9fa42; ROCm R9700 via HIP_VISIBLE_DEVICES=0 (in this container HIP_VISIBLE_DEVICES=4 exposes zero devices; 0 is the single visible R9700).

Command Result
CUDA_VISIBLE_DEVICES=GPU-50a20676... pytest -q tests/test_checkpoint_interop_4053.py 7 passed
CUDA_VISIBLE_DEVICES=GPU-50a20676... pytest -q tests/test_serialization.py tests/test_required_qweight.py tests/test_checkpoint_compat.py tests/test_save_loaded_quantized_model_dtype.py 61 passed
CUDA_VISIBLE_DEVICES=GPU-50a20676... pytest -q tests/test_model_dequant_dynamic_bits.py tests/test_export.py tests/test_config_only_export.py 14 passed
HIP_VISIBLE_DEVICES=0 pytest -q tests/test_checkpoint_interop_4053.py (ROCm) 7 passed
HIP_VISIBLE_DEVICES=0 pytest -q tests/test_required_qweight.py tests/test_checkpoint_compat.py (ROCm) 50 passed, 2 skipped
HIP_VISIBLE_DEVICES=0 pytest -q tests/test_serialization.py::TestSerialization::test_gptq_v1_serialization (ROCm) 1 passed
ruff check gptqmodel docs/eora tests setup.py All checks passed

All 7 new tests fail on 39cd57a85 and pass on this branch. tests/test_q4_marlin.py cannot collect on the CUDA venv (ModuleNotFoundError: parameterized, pre-existing). tests/test_serialization.py's Marlin/ExLlamaV2 cases are CUDA-only and fail on ROCm by design.

6. Risks / limits

  • Only GPTQ v1 <-> v2 qzero changes are converted. Every other format change (GPTQ -> Marlin, GEMM -> GEMV, ...) raises; re-quantize from the dense source instead. This is deliberate: a wrong conversion is worse than an error.
  • Marlin/ExLlamaV2 save_pretrained() is now fail-closed. Users who relied on writing the runtime layout will get a clear RuntimeError; GPTQModel.save() remains the supported export path.
  • The producer exclusion patterns are translated to dot-boundary suffix regexes. A producer that relies on arbitrary substring matches (not dot boundaries) may need a regex entry instead.

…4053)

- Fold AutoAWQ modules_to_not_convert and llm-compressor ignore into -: dynamic exclusions; apply Optimum modules_in_block_to_quantize as an inclusion filter so dense in-layer modules stay dense.
- Validate qweight presence for AWQ and shared Marlin/BitBLAS formats instead of leaving zero placeholders that produce NaN.
- Record the on-disk qzero layout at load and convert v1<->v2 qzeros on a format-changing re-save; refuse every other format change before writing.
- Emit checkpoint-layout qzeros from save_pretrained for v2 runtime kernels, and fail closed for Marlin/ExLlamaV2 whose runtime weight permutation cannot be reversed.
- Add docs/checkpoint-interop.md and the issue regression tests.
# Conflicts:
#	README.md
#	docs/checkpoint-interop.md
@Qubitium
Qubitium merged commit 2c032e0 into main Nov 14, 2026
5 checks passed
@Qubitium
Qubitium deleted the fix/issue-4053-load branch November 14, 2026 20:03
Qubitium added a commit that referenced this pull request Nov 15, 2026
…ng (#4070) (#4085)

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

1 participant