Repository navigation
fix(checkpoint): stop HF save_pretrained from writing qzeros_format markers (#4152) - #4155
Merged
Merged
Conversation
…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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4152
Root cause
#4134 (
6f9ad69e6, the fix for #4125) registers a persistentqzeros_formatbuffer 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 inget_state_dict_for_save(gptqmodel/utils/model.py:2490,:2611), but HFPreTrainedModel.save_pretrained()collectsstate_dict()directly. Becausethe buffer is registered
persistent=True,nn.Module._save_to_state_dictemits it before the packed hook runs, and
gptqmodel/__init__.py:225puts thatwriter inside
checkpoint_serialization(). The two save paths therefore wrotedifferent checkpoints: one extra int64
*.qzeros_formattensor per quantizedlinear, a key neither
config.jsonnor the GPTQ/AWQ format describes.Fix
_write_state_dict_layout_markernow removes the buffer from the destinationwhile
is_checkpoint_serialization()is true(
gptqmodel/nn_modules/qlinear/__init__.py:1175-1181). Popping is requiredrather than returning early:
nn.Module._save_to_state_dicthas alreadyemitted the persistent buffer (with its placeholder value) before the hook runs.
The marker is kept for every read-only
state_dict()transport — plain dictcopies and safetensors round trips within a live process — which is the #4125
item 3 contract. A persisted checkpoint takes its layout from
config.json'scheckpoint_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:#b8860bThe #4125 item 3 tension
There is no conflict; the two requirements cover different transports.
checkpoint_serialization()is the checkpoint-writer scope. #4125 item 3 isabout
state_dict()copies outside that scope. The marker is removed onlyinside the writer scope and remains in every read-only collection. Re-ran the
item 3 check (
torch.save, safetensors, plain dict): each transport stillcarries
qzeros_format=2, loads with no missing/unexpected keys, and givesmax logit diff 0.0.
Before / after (CPU, tiny Llama,
RTNConfig(bits=4, group_size=128), TorchLinear)Before (fix stashed):
After:
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
tests/test_checkpoint_interop_4053.py::test_save_pretrained_key_set_matches_saveRTN-quantizes the issue's tiny Llama, writes both save paths, asserts no
*.qzeros_formatkeys, equal key sets, and identical reloaded logits.Fails before the fix with
save_pretrained wrote layout markers: [...7 keys...].tests/test_checkpoint_state_invariant.py::test_layout_marker_is_omitted_only_for_checkpoint_writesasserts the marker is present in a read-only
state_dict()and absent insidecheckpoint_serialization(). Fails before the fix.(
test_v1_v2_copy_survives_every_state_dict_transport) is unchanged andstill passes, which keeps the marker mechanism from being deleted.
Commands and results (CPU only; GPUs hidden so no accelerator was touched):
The skipped cases are the CUDA/Marlin-only tests; they were not run.
Docs
docs/checkpoint-interop.mdnow documents that the marker is emitted only forin-process
state_dict()transports and removed from checkpoint writes, with aflow diagram of the new branch.
Hardware
CPU only. No GPU was used for the reported results.