Skip to content

[BUG] HF save_pretrained of a GPTQ model on Marlin/ExLlamaV2 again silently writes the kernel runtime layout (unloadable checkpoint); #4125 guard removed by #4620 #6137

Done

Description

@Qubitium

Summary

When a GPTQ checkpoint is loaded through Transformers with gptqmodel installed, the bridge picks Marlin on sm80. model.save_pretrained(out) then succeeds but writes Marlin's private runtime tensors under an ordinary quant_method: gptq, checkpoint_format: gptq config:

  • qweight in Marlin tile layout, [56, 1792] instead of [112, 896]
  • empty qzeros and g_idx
  • an extra *.qzeros_format tensor per layer

That directory cannot be reloaded by GPTQModel.load or by AutoModelForCausalLM.from_pretrained (shape-mismatch errors). This is the #4125 regression again. #4134 fixed it by running PreTrainedModel.save_pretrained inside checkpoint_serialization(), so Marlin/ExLlamaV2 _save_to_state_dict refused and the qzeros_format marker was dropped (#4155). #4620 deleted that wrapper to keep the "never patch Transformers" rule and put nothing in its place. Nothing in the HF path now enters checkpoint_serialization(), so the refusal and the marker suppression never run for HF writes.

Introduced by

d2dfb9385 / #4620 (removed _patch_transformers_save_pretrained_runtime_layout_guard from gptqmodel/__init__.py). Previously fixed in 6f9ad69e6 / #4134 (issue #4125) and 39aada68f / #4155 (issue #4171).

Location

HEAD d81ad345e (origin/main 73e3d953f does not touch this).
gptqmodel/nn_modules/qlinear/marlin.py:576. The refusal only fires inside checkpoint_serialization(), which only GPT-QModel's own writer enters now:

    def _save_to_state_dict(self, destination, prefix, keep_vars):
        if getattr(self, "_marlin_repacked", False):
            from ...utils.checkpoint_state import is_checkpoint_serialization
            if is_checkpoint_serialization():
                raise RuntimeError("Marlin rewrites `qweight` into its runtime tile layout ...")
        super()._save_to_state_dict(destination, prefix, keep_vars)

gptqmodel/nn_modules/qlinear/exllamav2.py:280 has the same pattern. The qzeros_format suppression in gptqmodel/nn_modules/qlinear/__init__.py (~line 1663, if is_checkpoint_serialization():) has the same dependency. Removed code at 8c71625b5:gptqmodel/__init__.py:413:

        def save_pretrained_with_runtime_layout_guard(self, *args, **kwargs):
            with checkpoint_serialization():
                return original(self, *args, **kwargs)

Reproduction

Ran on A100 (sm80), transformers 5.23.0, optimum 2.3.0, gptqmodel on the path:

from transformers import AutoModelForCausalLM
m = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct-GPTQ-Int4", device_map="cuda:0")  # MarlinLinear x168
m.save_pretrained("out")          # HEAD: succeeds

At HEAD, out/model.safetensors has model.layers.0.self_attn.q_proj.qweight [56, 1792] (original [112, 896]), qzeros [0], g_idx [0], plus ...q_proj.qzeros_format. config.json still says checkpoint_format: gptq. Reloading out:

  • GPTQModel.load("out") -> ValueError: Trying to set a tensor of shape torch.Size([0]) in "g_idx" (which has shape torch.Size([4864]))
  • AutoModelForCausalLM.from_pretrained("out") -> RuntimeError: You set ignore_mismatched_sizes to False ... (size mismatch report)

The same script at 8c71625b5 (2026-11-19, before #4620) fails at save time, as intended:

RuntimeError: Marlin rewrites `qweight` into its runtime tile layout and replaces `qzeros`/`g_idx`; the original checkpoint tensors cannot be reconstructed. Use `GPTQModel.save()`/`save_quantized()` ...

Expected vs actual

Expected: a Transformers checkpoint write of a model whose kernels hold an irreversible runtime layout either refuses with the actionable error (behavior after #4134) or writes the real checkpoint tensors. It should not emit qzeros_format markers (#4155).
Actual: the save succeeds silently and produces a directory that cannot be loaded. A user who re-saves (for example after editing the generation config or tokenizer) and then removes the original ends up with no usable checkpoint. docs/checkpoint-interop.md (section 3, and the table row "GPTQModel.save() on a Marlin/ExLlamaV2 model") now describes HF save_pretrained() as a "read-only" caller, but it is a checkpoint write.

Suggested fix

Restore a refusal for HF writes without patching Transformers. Some options:

  • From the bridge entry point Transformers already calls (hf_gptqmodel_post_init), attach the guard to the model instance only, for example by marking modules or wrapping that instance's save_pretrained, so the Transformers class is never touched.
  • Keep (or lazily reconstruct) the checkpoint-layout tensors for Marlin/ExLlamaV2 so state_dict() emits a valid GPTQ payload.
  • At minimum, have the bridge select a kernel without an irreversible repack, or warn loudly on state_dict().

Also re-check that qzeros_format is not written by HF save_pretrained. Add back an end-to-end test covering HF load, then save_pretrained, then reload.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions