Repository navigation
fix(loader): restore pre-7.0 GPTQ INT3 v1 qzeros (#6375) - #6388
Merged
Merged
Conversation
GPT-QModel < 7.0 wrote INT3 v1 qzeros with swapped per-word constants (word 0 = 0x24924924, word 2 = 0x49249249). #2763 switched the loader to the canonical logical-field shift, correct for AutoGPTQ and other third-party checkpoints, but left those GPT-QModel files decoding garbage with no error. Detect the producer from meta.quantizer and, for a gptqmodel version below 7.0.0, restore the canonical v1 words before the normal v1 -> v2 conversion. The same correction is applied through the plain-HF bridge, and the re-save export shell rewrites the raw legacy words so a loaded legacy checkpoint is not republished under a 7.x producer tag. The HF bridge also retags the in-memory config after conversion. Third-party checkpoints (no gptqmodel tag) and 7.0+ checkpoints keep the canonical path unchanged. The flag is a no-op for 2/4/5/6/7/8-bit. Adds tests/test_int3_legacy_qzeros_6375.py with a synthetic legacy encoding, canonical and third-party controls, 4/8-bit no-op coverage, a re-save round trip, and the plain-HF bridge path.
Qubitium
marked this pull request as ready for review
November 29, 2026 13:11
This was referenced Nov 29, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 29, 2026
* test(qzeros): pin the symmetric-qzeros state machine invariants Six changes landed on the symmetric ``qzeros`` path in one day (#6388, #6392, #6391, #6395, #6409 and the pending #6157 normalization). Each was verified on its own; nothing pinned the combination, and a later edit can satisfy one fix while breaking another. The state machine has three markers (``_HF_LEGACY_QZEROS``, ``_HF_CHECKPOINT_SYM``, ``_HF_DEFER_SYM_QZEROS``) and two word encodings, selected by ``_load_target_qzeros_format()``. The tests assert: - a 32-aligned legacy projection never defers, and a real floor-width mismatch always does (#6409 / #6396); - an asymmetric floor-width checkpoint is refused, never silently wrong; - a deferred module materializes to the standard shape with a resolved layout that agrees with the one the kernel decodes; - materialization is idempotent across 2/3/4/8-bit; - the deferral marker and a materialized buffer are mutually exclusive; - a float ``qzeros`` buffer (int4pack affine offsets) is never filled with a packed word -- skipped until the #6157 normalizer exists; - a clean checkpoint's word survives the load, and every pre- and post-#2983 producer lands on the same decoded zero point. ``qzero_format()`` is deliberately not read as a decode answer before ``_qzeros_format_established``: every kernel is ``REQUIRES_FORMAT_V2``, so the constructor placeholder ``1`` disagrees with the decoded ``2`` until a load resolves it (#4377). That distinction is the whole reason the first #6157 draft corrupted clean checkpoints. Test-only. Verified 49 passed / 13 skipped on main (a4471c5) and 50 / 12 with #6419 applied; 68 passed / 17 skipped together with the four existing qzeros test files, so there is no global-state pollution. Every skip is a kernel-geometry or environment gate with a precise reason. * test(qzeros): spell out the unreachable return after a pytest.skip ``_legacy_module_or_skip`` ended with a bare ``pytest.skip(...)`` call, which reads as an implicit ``return None`` to static analysis and made CodeQL flag the function for mixing explicit and implicit returns. ``pytest.skip`` raises, so the added return is unreachable; it exists only to make every non-exceptional path explicit, and the comment says so. Behaviour unchanged: 49 passed, 13 skipped, identical to before. --------- Co-authored-by: agent <agent@local>
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.
Summary
3-bit GPTQ v1 checkpoints written by GPT-QModel < 7.0 load at HEAD with no error and decode garbage because their on-disk
qzerosuse the pre-#2763 swapped per-word constants (word 0 =0x24924924, word 2 =0x49249249). #2763 made the loader use the canonical logical-field shift, which is correct for AutoGPTQ and other third-party checkpoints, but added no compatibility path for the files GPT-QModel itself wrote.This PR detects the producer from the checkpoint's
meta.quantizerand, for agptqmodel:< 7.0.0tag, restores the canonical v1 words before the normal v1 -> v2 conversion. Third-party checkpoints (no gptqmodel tag) and 7.0+ checkpoints keep the canonical path unchanged.Fixes #6375
Detection rule
FIRST_VERSION_WITH_CANONICAL_INT3_QZEROS = "7.0.0"(next toMIN_VERSION_WITH_V2), withQuantizeConfig.legacy_int3_qzeros_producer()reusing the existingmeta_get_versionable()parser. The oldestgptqmodeltag wins, because Optimum appends the loading version to the producer list. Missing/third-party tags returnNone.The correction runs only for
bits == 3,pack_dtype == torch.int32, and a legacy producer tag. 2/4/5/6/7/8-bit paths are untouched.What changed
gptqmodel/utils/model_dequant.py:_correct_legacy_int3_qzeros()adds the pre-7.0 per-word offsets back, then subtracts one per logical field to produce canonical v1 words.LEGACY_INT3_QZERO_WORD_OFFSETSrecords the swapped constants.gptqmodel/utils/model.py:convert_gptq_v1_to_v2_format_module(..., legacy_int3_producer=...)applies the correction before the normal v1 -> v2 shift and emits onelog.warn.oncenaming the producer.convert_gptq_v1_to_v2_format()derives the producer fromcfg.hf_convert_gptq_v1_to_v2_format()(the plain-HF bridge used by Optimum) passes the checkpointmetathrough and retagsmodel.config.quantization_configafter conversion so a latersave_pretrained()does not publish canonical words under the old< 7.0tag.gptqmodel/models/loader.py: records the detected producer onQuantizeConfigfor the save path.gptqmodel/models/writer.py: the re-save export shell is filled from the raw checkpoint, so it rewrites the legacy words to canonical v1 before serializing them. Without this,GPTQModel.save()of a legacy-loaded model republished legacy bytes under a 7.x producer tag and the next load was corrupt.tests/test_int3_legacy_qzeros_6375.py: CPU-only regression coverage.Verification
No published 5.6.12/6.0.3 3-bit checkpoint exists locally, so the legacy fixture is synthetic: a real 3-bit g128 sym TinyLlama checkpoint (written by HEAD) is re-encoded with the exact pre-7.0 word offsets. The issue's own re-encoding recipe is the fixture.
TinyLlama-1.1B-Chat-v1.0, 3-bit g128 sym, 40x512-token PPL windows (BOS per window),
TorchLinear, greedy generation:generate("The capital of France is", 24)0dd8e8abfParis, the capital of France...0dd8e8abfThe capital of France is(degenerate)meta.quantizerSynthetic tiny-Llama logits (max abs diff vs the v2 checkpoint oracle):
0dd8e8abfgptqmodel:7.3.4gptqmodel:7.0.0gptqmodel:6.0.3gptqmodel:5.6.124-bit / 8-bit no-regression on real local checkpoints (loading with the legacy flag forced to
6.0.3vs not forced):Llama-3.2-1B-Instruct-GPTQ-4bits-gp128(metagptqmodel:5.4.2)Qwen2-VL-2B-Instruct-GPTQ-Int8(no meta)Re-save checks: loading the legacy 6.0.3 fixture and calling
GPTQModel.save()writes canonical v1qzerosand reloads with max logit diff 0.0; re-saving withformat=gptq_v2also reloads with 0.0. The plain-HFsave_pretrained()path now writes canonical words and a 7.x producer tag and reloads with 0.0.Tests
tests/test_int3_legacy_qzeros_6375.py— 10 passed (CPU-only).test_checkpoint_interop_4053.py,test_checkpoint_compat.py,test_qzero_offsets.py,test_gptq_p_markerless_qzeros_5809.py,test_format_conversion_flow.py,test_qzeros_*,test_pack_3bit*,test_export_shell_gemv_resave_4305.py,test_hf_post_init_unpack_shift_5843.py,test_model_dequant_v2_metadata_alias_5413.py: 147 passed, 12 skipped (CUDA-only).test_qqq*.py: 210 passed, 105 skipped (CUDA-only).test_gptq.py,test_checkpoint_state_invariant.py,test_writer_state_overlay.py,test_resave_dense_parameter_dtype_4466.py,test_save_state_dict_buffer_collection.py,test_planar_format_gptq_p.py: 168 passed, 17 skipped (CUDA-only)./root/venv/bin/ruff checkon all changed files: clean.This PR is a draft.