Skip to content

[BUG] Transformers load -> save_pretrained of a GPTQ v2 checkpoint writes every zero point +1 (nll 3.10 -> 15.23 on reload): HF bridge never marks v2 modules with qzero_format(2) #4952

Closed

Description

@Qubitium

Found on main @ 3867c82, CPU (Qwen2.5-0.5B-Instruct GPTQConfig(bits=4, group_size=128, format="gptq_v2"), made on this commit). Not a regression from #4771: the commits just before and just after #4771 behave the same.

What is wrong

Loading a GPTQ v2 checkpoint with plain Transformers and then calling save_pretrained() writes zero points shifted by one. The saved config still says checkpoint_format: gptq_v2, so every later load (Transformers or GPTQModel.load) decodes garbage, and nothing warns.

Root cause: the HF bridge's hf_gptqmodel_post_init_for_load -> hf_convert_gptq_v1_to_v2_format (gptqmodel/utils/model.py:957-986) records _checkpoint_format_on_disk = GPTQ_V2 but returns early for v2. It never calls module.qzero_format(format=2). The native loader does call it (gptqmodel/models/loader.py:3525-3532: "Mark the loaded modules so decoders and repack bookkeeping read the words as v2 instead of misreading every zero point one high"). On the HF path, every module therefore keeps the constructor placeholder _qzeros_format = 1 while its in-memory words are v2. state_dict() then "converts" the supposedly v1 runtime words to the v2 checkpoint layout (+1 per zero point) through checkpoint_layout_qzeros/_emitted_qzeros_format (nn_modules/qlinear/__init__.py:1840-1930).

The qzeros_format buffer is also not in the checkpoint, and on the HF path it is left as uninitialized memory (it read 7 in one run and 409534578 in the next). On the native path it is 2.

Inference in the HF-loaded session is correct (nll identical to GPTQModel.load). Only serialization is wrong, so a user who loads with Transformers, fine-tunes, merges or just re-saves ships a broken model.

Repro

import torch, glob, gptqmodel
from gptqmodel import GPTQModel, BACKEND
from transformers import AutoModelForCausalLM
from safetensors import safe_open

P = "Qwen2.5-0.5B-Instruct_gptq_v2"
disk = safe_open(glob.glob(P + "/*.safetensors")[0], "pt").get_tensor("model.layers.0.self_attn.q_proj.qzeros")
hf = AutoModelForCausalLM.from_pretrained(P, device_map="cpu", dtype=torch.bfloat16)
gm = GPTQModel.load(P, device="cpu", dtype=torch.bfloat16)
for name, net in (("hf", hf), ("gm", gm.model)):
    q = net.model.layers[0].self_attn.q_proj
    print(name, "runtime", q._runtime_qzeros_format(), "qzeros_format buf", int(q.qzeros_format),
          "mem==disk", torch.equal(q.qzeros.cpu(), disk),
          "state_dict==disk", torch.equal(net.state_dict()["model.layers.0.self_attn.q_proj.qzeros"].cpu(), disk))
hf.save_pretrained("resaved")      # then reload "resaved" with GPTQModel.load or AutoModelForCausalLM

Output:

hf runtime 1 qzeros_format buf 409534578 mem==disk True state_dict==disk False
gm runtime 2 qzeros_format buf 2 mem==disk True state_dict==disk True

wikitext nll (256 tokens), same checkpoint:

path before save after save -> reload (GM) after save -> reload (HF)
AutoModelForCausalLM (AUTO and gptq_torch backend) -> save_pretrained 3.097 15.23 15.23
GPTQModel.load -> model.save_pretrained / save() 3.097 3.097 —
third-party Llama-3.2-1B-Instruct-HF-GPTQV2 (optimum + gptqmodel 6.1) via HF 3.151 saved qzeros != source file (same defect)
v1 checkpoints (gptq, gptq_desc, gptq_dyn, act-order+dynamic), AWQ, AWQ asym via HF — identical identical

Suggested fix

In hf_convert_gptq_v1_to_v2_format (or hf_gptqmodel_post_init_for_load), mark v2 checkpoints the way the native loader does: m.qzero_format(format=2) for every GPTQQuantLinear when source_format == FORMAT.GPTQ_V2. Better, share one helper between the two loaders. Also initialize the qzeros_format buffer when the checkpoint does not carry it, rather than leaving uninitialized memory that later code may read.

Test that should have caught it

tests/test_checkpoint_interop_4053.py / the HF round-trip fixture covers v1 only. Add a format="gptq_v2" case: AutoModelForCausalLM.from_pretrained -> save_pretrained -> reload, and assert qzeros are bitwise equal to the source file and the logits match.

Severity

High. A plain load -> save through Transformers silently destroys every GPTQ v2 checkpoint (nll 3.1 -> 15.2), and the saved config gives no hint.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 finished this issue.

    Pull request: #4980

    Fix transformers-load -> save_pretrained of a GPTQ v2 checkpoint writing zero points +1: the HF bridge never converts the runtime qzeros layout back to the checkpoint format on save; add a round-trip test (nll 3.10 -> 15.23).

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