Skip to content

fix(checkpoint): stop HF save_pretrained from writing qzeros_format markers (#4152) - #4155

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4152-save-marker
Nov 15, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4152-save-marker

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #4152

Root cause

#4134 (6f9ad69e6, the fix for #4125) registers a persistent qzeros_format
buffer on every PackedGroupedQuantLinear
(gptqmodel/nn_modules/qlinear/__init__.py:1088) and overwrites it from
_write_state_dict_layout_marker (:1154). GPTQModel.save() skips the key in
get_state_dict_for_save (gptqmodel/utils/model.py:2490, :2611), but HF
PreTrainedModel.save_pretrained() collects state_dict() directly. Because
the buffer is registered persistent=True, nn.Module._save_to_state_dict
emits it before the packed hook runs, and gptqmodel/__init__.py:225 puts that
writer inside checkpoint_serialization(). The two save paths therefore wrote
different checkpoints: one extra int64 *.qzeros_format tensor per quantized
linear, a key neither config.json nor the GPTQ/AWQ format describes.

Fix

_write_state_dict_layout_marker now removes the buffer from the destination
while is_checkpoint_serialization() is true
(gptqmodel/nn_modules/qlinear/__init__.py:1175-1181). Popping is required
rather than returning early: nn.Module._save_to_state_dict has already
emitted the persistent buffer (with its placeholder value) before the hook runs.

The marker is kept for every read-only state_dict() transport — plain dict
copies and safetensors round trips within a live process — which is the #4125
item 3 contract. A persisted checkpoint takes its layout from config.json's
checkpoint_format, so the marker is redundant there.

flowchart TD
    A["PackedGroupedQuantLinear._save_to_state_dict"] --> B["nn.Module emits the persistent qzeros_format buffer"]
    B --> C{"is_checkpoint_serialization()?"}
    C -- yes --> D["pop qzeros_format<br/>checkpoint matches GPTQModel.save()"]
    C -- "no (read-only state_dict)" --> E["write the layout code tensor<br/>survives safetensors and dict copies"]
    style D fill:#d9f2d9,stroke:#2e7d32
    style E fill:#fff4cc,stroke:#b8860b
Loading

The #4125 item 3 tension

There is no conflict; the two requirements cover different transports.
checkpoint_serialization() is the checkpoint-writer scope. #4125 item 3 is
about state_dict() copies outside that scope. The marker is removed only
inside the writer scope and remains in every read-only collection. Re-ran the
item 3 check (torch.save, safetensors, plain dict): each transport still
carries qzeros_format=2, loads with no missing/unexpected keys, and gives
max logit diff 0.0.

Before / after (CPU, tiny Llama, RTNConfig(bits=4, group_size=128), TorchLinear)

Before (fix stashed):

save() keys: 33             qzeros_format keys: 0
save_pretrained() keys: 40  qzeros_format keys: 7
extra in save_pretrained: ['model.layers.0.mlp.down_proj.qzeros_format', 'model.layers.0.mlp.gate_proj.qzeros_format', 'model.layers.0.mlp.up_proj.qzeros_format', 'model.layers.0.self_attn.k_proj.qzeros_format', 'model.layers.0.self_attn.o_proj.qzeros_format', 'model.layers.0.self_attn.q_proj.qzeros_format', 'model.layers.0.self_attn.v_proj.qzeros_format']

After:

save() keys: 33             qzeros_format keys: 0
save_pretrained() keys: 33  qzeros_format keys: 0
extra in save_pretrained: []    missing: []

Save → reload gives 0.0 max logit diff on both paths (CUDA venv CPU runtime).
On the ROCm CPU runtime both reloads are bitwise identical and 4.4e-3 relative
to the in-memory module, because the freshly quantized module and its fp16
payload round differently there; the two written checkpoints are identical
(0 value-differing tensors).

Tests

Commands and results (CPU only; GPUs hidden so no accelerator was touched):

CUDA_VISIBLE_DEVICES="" PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest -q \
  tests/test_checkpoint_state_invariant.py tests/test_checkpoint_interop_4053.py
  -> 45 passed, 5 skipped

HIP_VISIBLE_DEVICES="" PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest -q \
  tests/test_checkpoint_state_invariant.py tests/test_checkpoint_interop_4053.py
  -> 45 passed, 5 skipped

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

The skipped cases are the CUDA/Marlin-only tests; they were not run.

Docs

docs/checkpoint-interop.md now documents that the marker is emitted only for
in-process state_dict() transports and removed from checkpoint writes, with a
flow diagram of the new branch.

Hardware

CPU only. No GPU was used for the reported results.

…arkers (#4152)

The persistent qzeros_format buffer is emitted by nn.Module._save_to_state_dict
before the packed hook runs, so HF save_pretrained() (which now runs inside
checkpoint_serialization()) published one extra int64 tensor per quantized
linear while GPTQModel.save() skipped it.

Remove the key from the destination inside checkpoint_serialization(). The
read-only state_dict() transports that #4125 item 3 depends on stay outside
that scope and keep the marker, so a plain dict copy and a safetensors round
trip still carry the v1/v2 layout code. A reloaded checkpoint takes its layout
from config.json's checkpoint_format instead.

Add a save-path key-set regression test and a marker scope test; both fail
before the fix.
@Qubitium
Qubitium merged commit 39aada6 into main Nov 15, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4152-save-marker branch November 15, 2026 22:46
Qubitium added a commit that referenced this pull request Nov 16, 2026
…ck (#4166, #4167, #4171) (#4235)

The live qzeros buffer always holds the kernel's runtime (logical/v2) zero
points, _qzeros_format records that live layout, and
_checkpoint_format_on_disk records only what a checkpoint write emits. The
v1/v2 conversion runs at the two serialization boundaries, is idempotent, and
follows the marker carried by the same incoming state dict.

#4166: accelerate with device_map=None loads a sharded checkpoint by calling
set_model_state_dict once per shard and refilling missing keys from the
module's own buffers, so the default marker made every refill convert the live
words again (+1 per shard). The load hook now converts only when the incoming
marker differs from the live layout, ignores a qzeros tensor that aliases the
live buffer when no marker travels with it, and the export shell declares its
source layout before loading with device_map={"": "cpu"}.

#4167: pack_module marked logical zeros only for FORMAT.GPTQ, so a GPTQ_V2
export kept the constructor's v1 label and the writer added one to every zero
point. Every GPTQ target format now records logical zeros, and the raw
pack()/pack_block()/pack_gpu() packers do the same.

#4171 item 1 (save_pretrained marker leak) was already fixed by #4155 and is
verified, not re-fixed. Item 2: _load_native_w4a_for_save no longer requires
the non-persisted marker in a native W4A checkpoint. Item 3: Machete, Swordfish
and BitBLAS now flag their runtime layout, and the base packed-module write
path refuses any runtime layout inside checkpoint_serialization().

Tests: load->save byte-compare for v1 single/sharded and v2, GPTQ_V2
quantize->save->reload NLL, direct pack() labelling, refilled-load
idempotency, W4A save round-trip, and the Machete/Swordfish/BitBLAS inventory
entries.
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] Follow-up on #4134: HF save_pretrained writes non-standard qzeros_format tensor for every GPTQ/AWQ layer (differs from GPTQModel.save)

1 participant