Skip to content

fix(qlinear): convert v1 qzeros copied into fresh v2 kernels (#4377) - #4414

Merged
Qubitium merged 1 commit into
mainfrom
fix/qzeros-format-load-4377
Nov 16, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/qzeros-format-load-4377

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4377.

Problem

fresh.load_state_dict(mod.state_dict()) from a v1-loaded module into a freshly built v2-runtime module skipped the v1→v2 qzeros conversion, so the kernel decoded v1 words as v2 (relative error 0.156 in the issue repro, fresh.qzero_format() 1 vs source 2).

Root cause: the load hook compares the incoming qzeros_format marker against _runtime_qzeros_format(), but GPTQQuantLinear.__init__ sets _qzeros_format = 1 as a placeholder before any words exist. A fresh REQUIRES_FORMAT_V2 module therefore reported runtime format 1, the incoming v1 marker looked like an exact match, and _restore_qzeros_format() was never called. The assign=True fix (0189599) covered only its own trigger.

Fix

gptqmodel/nn_modules/qlinear/__init__.py:

  • added _qzeros_format_established, set whenever a real layout is resolved (qzero_format(format=...) and _restore_qzeros_format()); the constructor placeholder stays "unestablished";
  • added _load_target_qzeros_format(): for a REQUIRES_FORMAT_V2 kernel whose layout is still the placeholder, the decode layout is v2. An established v1 layout is still respected, so convert_gptq_v2_to_v1_format keeps holding v1 words;
  • the load hook now converts when the marker differs from that target or the module still holds the placeholder, so a fresh module resolves the label even when the marker numerically matches.

Tests

New tests/test_qzeros_format_load_4377.py (CPU-only):

  • v1-loaded TorchLinear (FORMAT.GPTQ, sym=False) → fresh v2-runtime module: equal live qzeros, qzero_format() == 2, bit-identical forward. Fails on pre-fix source with assert 1 == 2;
  • v2→v2 copy stays v2.

Verification on the rebased branch:

  • tests/test_qzeros_format_load_4377.py tests/test_qzeros_assign_load_4304.py tests/test_qzero_offsets.py tests/test_qzeros_format_invariant.py tests/test_format_conversion_flow.py tests/test_hf_packing_api.py → 86 passed, 4 skipped.
  • assign=True, assign=False and plain copy into an already-loaded module still give relative error 0.0.
  • Issue repro: 0.1558 → 0.0, qzero_format() 1 → 2.
  • ruff check → clean.

Residual risk

  • _qzeros_format_established is a plain Python attribute, not in state_dict(). A module unpickled from a pre-fix pickle defaults to "unresolved", so a deliberately v1-converted v2 kernel restored that way could convert once more on a later v1-marker load. Module pickling is rare and state_dict transport is unaffected.
  • Real v1→v1 copies use non-v2 kernels (e.g. Marlin), which are CUDA-only and skipped here; CPU coverage exercises the decision logic only.

A v1 checkpoint module's state_dict() carries v1 words with a v1 marker,
while a freshly constructed REQUIRES_FORMAT_V2 kernel still reports the
constructor placeholder _qzeros_format=1 even though it decodes v2. The
load hook compared the marker against that placeholder, saw a match, and
skipped the v1->v2 conversion.

Track whether _qzeros_format was resolved (packing, load, or explicit
conversion) and treat the placeholder as v2 for a REQUIRES_FORMAT_V2
kernel. A matching marker on a fresh module also resolves the label, so
v2->v2 copies keep qzero_format()==2. Established v1 layouts, including
the deliberate convert_gptq_v2_to_v1_format path, stay v1.

Adds a CPU regression test that copies a v1-loaded TorchLinear state_dict
into a fresh v2-runtime module and asserts identical forward output plus
the expected qzero_format().
@Qubitium
Qubitium merged commit db31a55 into main Nov 16, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/qzeros-format-load-4377 branch November 16, 2026 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant