Skip to content

fix: convert v1 qzeros on assign=True state dict loads (#4304) - #4330

Merged
Qubitium merged 1 commit into
mainfrom
pr/4304
Nov 16, 2026
Merged

Qubitium merged 1 commit into
mainfrom
pr/4304

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

load_state_dict(..., assign=True) of a v1 qzeros tensor into a v2-runtime kernel
skipped the v1→v2 conversion, silently corrupting every zero point (regression from
08f1b2b / #4166).

The load hook decided whether the incoming words were the module's own live buffer by
calling _incoming_qzeros_is_live() after super()._load_from_state_dict(). With
assign=True, torch rebinds self.qzeros to the incoming checkpoint tensor, so that
check compared the tensor against itself, reported "refill", and skipped the conversion.
torch.distributed.checkpoint's set_model_state_dict uses assign=True for
meta-device models, so it hit the same path.

What Changed

gptqmodel/nn_modules/qlinear/__init__.py (only file changed):

  • _load_from_state_dict now evaluates _incoming_qzeros_is_live() before the copy
    and reuses that flag for the post-copy decision. Genuine checkpoint loads convert under
    both assign=False and assign=True; a refill of the module's own buffer still does not
    (the [BUG] GPTQModel.load(...).save(...) corrupts qzeros (every zero point shifted; sharded checkpoints become garbage) #4166 protection is unchanged, it is just measured at the correct time).
  • _incoming_qzeros_is_live also requires matching devices, so an incoming tensor on a
    different device can never be mistaken for an aliased live buffer by a numeric
    data_ptr() collision.
  • Docstring notes the "must be called before the copy" contract.

Tests

New tests/test_qzeros_assign_load_4304.py:

  • test_assign_true_converts_v1_qzeros_like_assign_false — loads raw v1 words
    (0x77777777) into a v2-runtime TorchLinear whose checkpoint format is v1, with
    assign=False and assign=True; both must yield the logical 0x88888888 words and
    qzero_format() == 2.
  • test_assign_true_does_not_double_convert_refilled_live_buffer — [BUG] GPTQModel.load(...).save(...) corrupts qzeros (every zero point shifted; sharded checkpoints become garbage) #4166 regression
    guard: a marker-less refill of the live buffer (identity/aliased tensor) is not shifted
    under either assign mode.
  • test_assign_true_is_idempotent_with_marker_carrying_refill — a marker-carrying
    accelerate/DCP refill stays a no-op under both assign modes.
$ python3 -m pytest tests/test_qzeros_assign_load_4304.py -q
3 passed

$ python3 -m pytest tests/test_qzeros_assign_load_4304.py tests/test_qzeros_format_invariant.py tests/test_checkpoint_interop_4053.py -q
35 passed, 2 skipped

$ ruff check gptqmodel/nn_modules/qlinear/__init__.py tests/test_qzeros_assign_load_4304.py
All checks passed!

End-to-end check against the issue repro (tiny local GPTQ v1 checkpoint, CPU):

checkpoint qzeros word: 0x77777777
assign=False -> fmt=2 word=0x88888888
assign=True  -> fmt=2 word=0x88888888   (before this fix: 0x77777777)

Pre-existing, unrelated failures on this CPU-only box (identical at the base commit):
tests/test_checkpoint_state_invariant.py AVX512-BF16 kernel cases (compiled extension
not built) and tests/kernels/test_ret_idx_cache_invalidation.py::test_fused_refresh_preserves_wider_source_metadata[load_assign-*]
("rshift_cpu" not implemented for 'Float').

Fixes #4304

@Qubitium
Qubitium merged commit 0189599 into main Nov 16, 2026
4 of 6 checks passed
@Qubitium
Qubitium deleted the pr/4304 branch November 16, 2026 08:47
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] load_state_dict(..., assign=True) of v1 qzeros into a v2-runtime kernel skips the v1→v2 conversion: silent corruption (regression from 08f1b2b)

1 participant