Repository navigation
fix(qlinear): convert v1 qzeros copied into fresh v2 kernels (#4377) - #4414
Merged
Merged
Conversation
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().
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 #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_formatmarker against_runtime_qzeros_format(), butGPTQQuantLinear.__init__sets_qzeros_format = 1as a placeholder before any words exist. A freshREQUIRES_FORMAT_V2module therefore reported runtime format 1, the incoming v1 marker looked like an exact match, and_restore_qzeros_format()was never called. Theassign=Truefix (0189599) covered only its own trigger.Fix
gptqmodel/nn_modules/qlinear/__init__.py:_qzeros_format_established, set whenever a real layout is resolved (qzero_format(format=...)and_restore_qzeros_format()); the constructor placeholder stays "unestablished";_load_target_qzeros_format(): for aREQUIRES_FORMAT_V2kernel whose layout is still the placeholder, the decode layout is v2. An established v1 layout is still respected, soconvert_gptq_v2_to_v1_formatkeeps holding v1 words;Tests
New
tests/test_qzeros_format_load_4377.py(CPU-only):TorchLinear(FORMAT.GPTQ,sym=False) → fresh v2-runtime module: equal live qzeros,qzero_format() == 2, bit-identical forward. Fails on pre-fix source withassert 1 == 2;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=Falseand plain copy into an already-loaded module still give relative error 0.0.qzero_format()1 → 2.ruff check→ clean.Residual risk
_qzeros_format_establishedis a plain Python attribute, not instate_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 andstate_dicttransport is unaffected.