Repository navigation
fix(w4a): declare the source qzero layout before a native re-save (#4323) - #4348
Merged
Merged
Conversation
) 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
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 #4323
Root cause
The W4A re-save branch (
ModelWriter->_load_native_w4a_for_save) loads theraw native tensors into a fresh export shell with
assign=True, but it neverdeclared which qzero layout those words use. The shell therefore fell back to
its own (target) format when
_load_from_state_dictasked, so a re-save wrotev2 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_v2case was alreadyrepaired by #4304 (it moved the
assign=Truelive-buffer check before thecopy). I verified that test passes on pristine
origin/main. The missingdeclaration 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:qcfg._checkpoint_format_on_disk, falling back toqcfg.format),_checkpoint_format_on_diskandqzero_format(...)on the export-shellmodules, exactly as the non-W4A branch already does.
After the load:
resolve_resave_format_conversion(qcfg)to keep rejecting unsupportedlayout changes,
The v1/v2 shift is deliberately not applied here.
_load_from_state_dictalready normalizes the live words to the kernel's runtime layout, and
_save_to_state_dictconverts that layout to the target checkpoint format;calling
convert_loaded_qzeros_for_format_changeas well would shift twice(verified by a v1 -> v2 case).
Regression test
tests/models/test_w4a_resave_roundtrip.pyquantizes a tiny W4AFP8 Llama,writes a checkpoint, loads it, re-saves it, and checks:
gptq_v2 -> gptq_v2: everyqzerostensor is bit-identical.gptq_v2 -> gptqandgptq -> gptq_v2: the words are rewritten but thelogical zero points are unchanged.
qweight/qzeros/scales(explicit low-nibble-first unpack, not the kernel) is identical before and
after the re-save for every case.
Fail-before / pass-after
Pristine
origin/main(3d9087362), same test file copied in:The v2 -> v1 failure is the missing conversion: the re-saved words are
0x88888888(-2004318072) unchanged instead of the v10x77777777, and thereloaded 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 withGreatest absolute difference: 286331153(0x11111111) -- exactly +1 on everyzero-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:
Full 2x2 matrix on the patched tree (independent decode + reload + logits):
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.pypasses (ruff 0.14.2, the versionpinned by
format/check.sh). Currentorigin/mainfails that gate on twounrelated files from recent commits, so this PR also removes an unused
jsonimport and rewrites one
dict(...)literal. Those two hunks are mechanical andotherwise untouched; flagging them because they sit outside the W4A path.
Residual risk
w4a_nvfp4shares the same branch and format logic, but this host has noSM 12.1 part, so only the
w4afp8lane was executed.resolve_resave_format_conversion; that behaviour is unchanged.contain no behavioural change and their tests pass.