Skip to content

fix(w4a): declare the source qzero layout before a native re-save (#4323) - #4348

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4323-w4afp8-resave
Nov 16, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4323-w4afp8-resave

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #4323

Root cause

The W4A re-save branch (ModelWriter -> _load_native_w4a_for_save) loads the
raw native tensors into a fresh export shell with assign=True, but it never
declared which qzero layout those words use. The shell therefore fell back to
its own (target) format when _load_from_state_dict asked, so a re-save wrote
v2 words under a v1 label (or v1 words under a v2 label) without the required
shift.

Point 4 of the issue is exactly this: the "declare source layout" block runs
only in the non-W4A branch.

Note on current main: the same-format gptq_v2 -> gptq_v2 case was already
repaired by #4304 (it moved the assign=True live-buffer check before the
copy). I verified that test passes on pristine origin/main. The missing
declaration was still present, though, and it still corrupts a format-changing
re-save. This PR restores the declaration at the source and adds the round-trip
regression test the issue asked for.

Fix

In the W4A branch, before _load_native_w4a_for_save:

  • resolve the on-disk format (qcfg._checkpoint_format_on_disk, falling back to
    qcfg.format),
  • set _checkpoint_format_on_disk and qzero_format(...) on the export-shell
    modules, exactly as the non-W4A branch already does.

After the load:

  • call resolve_resave_format_conversion(qcfg) to keep rejecting unsupported
    layout changes,
  • relabel the shell with the target format.

The v1/v2 shift is deliberately not applied here. _load_from_state_dict
already normalizes the live words to the kernel's runtime layout, and
_save_to_state_dict converts that layout to the target checkpoint format;
calling convert_loaded_qzeros_for_format_change as well would shift twice
(verified by a v1 -> v2 case).

Regression test

tests/models/test_w4a_resave_roundtrip.py quantizes a tiny W4AFP8 Llama,
writes a checkpoint, loads it, re-saves it, and checks:

  1. gptq_v2 -> gptq_v2: every qzeros tensor is bit-identical.
  2. gptq_v2 -> gptq and gptq -> gptq_v2: the words are rewritten but the
    logical zero points are unchanged.
  3. An independent FP64 dequant of the stored qweight/qzeros/scales
    (explicit low-nibble-first unpack, not the kernel) is identical before and
    after the re-save for every case.
  4. The re-saved file loads and produces the same logits as the first file.

Fail-before / pass-after

Pristine origin/main (3d9087362), same test file copied in:

FAILED tests/models/test_w4a_resave_roundtrip.py::test_w4afp8_resave_format_change_preserves_logical_zeros[gptq_v2-gptq]
FAILED tests/models/test_w4a_resave_roundtrip.py::test_w4afp8_resave_format_change_preserves_logical_zeros[gptq-gptq_v2]
PASSED tests/models/test_w4a_resave_roundtrip.py::test_w4afp8_gptq_v2_resave_is_bit_identical
=================== 2 failed, 1 passed ===================

The v2 -> v1 failure is the missing conversion: the re-saved words are
0x88888888 (-2004318072) unchanged instead of the v1 0x77777777, and the
reloaded checkpoint fails with W4AFP8 first release requires symmetric GPTQ zero point 8 in every group.

At 018959916^ (main before #4304) the same-format test itself fails with
Greatest absolute difference: 286331153 (0x11111111) -- exactly +1 on every
zero-point nibble, the corruption in the issue. Current main passes it, which
confirms #4304 repaired that mechanism and this PR closes the remaining hole.

With this branch:

=================== 3 passed ===================

Full 2x2 matrix on the patched tree (independent decode + reload + logits):

gptq_v2 -> gptq_v2: words_equal=True  logical_equal=True  reload=True maxdiff=0.0
gptq_v2 -> gptq:    words_equal=False logical_equal=True  reload=True maxdiff=0.0
gptq -> gptq_v2:    words_equal=False logical_equal=True  reload=True maxdiff=0.0
gptq -> gptq:       words_equal=True  logical_equal=True  reload=True maxdiff=0.0

Existing suites

  • tests/models/test_w4a_native_writer.py + tests/test_qzeros_assign_load_4304.py: 24 passed, 4 skipped.
  • tests/models/test_w4a_tiny_llama_lifecycle.py -k w4afp8: 2 passed.
  • tests/test_checkpoint_metadata_contracts.py + tests/test_cpu_int4pack_dispatch_contracts.py: 17 passed.

Lint

ruff check gptqmodel docs/eora tests setup.py passes (ruff 0.14.2, the version
pinned by format/check.sh). Current origin/main fails that gate on two
unrelated files from recent commits, so this PR also removes an unused json
import and rewrites one dict(...) literal. Those two hunks are mechanical and
otherwise untouched; flagging them because they sit outside the W4A path.

Residual risk

  • w4a_nvfp4 shares the same branch and format logic, but this host has no
    SM 12.1 part, so only the w4afp8 lane was executed.
  • Format changes other than GPTQ v1 <-> v2 still raise from
    resolve_resave_format_conversion; that behaviour is unchanged.
  • The two lint drive-bys touch files owned by recent unrelated commits; they
    contain no behavioural change and their tests pass.

)

The W4A re-save branch loads raw native tensors into an export shell with
assign=True but never declared which qzero layout those words use. The shell
therefore labelled them with its own (target) format, and a format-changing
re-save wrote v2 words as v1 (or vice versa) without the required shift.

Declare the on-disk layout on the shell before _load_native_w4a_for_save,
mirroring the non-W4A branch. After the load, validate the requested format
change and relabel the shell with the target format. The v1/v2 shift is not
applied here: _load_from_state_dict already normalized the live words to the
kernel runtime layout and _save_to_state_dict converts them to the target, so
applying convert_loaded_qzeros_for_format_change would shift twice.

The same-format gptq_v2 path was already repaired by #4304; this restores the
missing declaration that #4323 point 4 identified and fixes the still-broken
format-change re-save.

Adds tests/models/test_w4a_resave_roundtrip.py:
  - gptq_v2 -> gptq_v2: every qzero word bit-identical, reload matches.
  - gptq_v2 -> gptq and gptq -> gptq_v2: words converted, logical zeros and
    an independent FP64 dequant of the stored codes are unchanged, reload
    produces the same output.

Also fixes two pre-existing repo-wide ruff violations (unused import, dict()
literal) that fail format/check.sh on current main.

Fixes #4323
@Qubitium
Qubitium merged commit 63a5d3c into main Nov 16, 2026
8 of 9 checks passed
@Qubitium
Qubitium deleted the fix/issue-4323-w4afp8-resave branch November 16, 2026 09:19
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