Skip to content

fix(loader): restore pre-7.0 GPTQ INT3 v1 qzeros (#6375) - #6388

Merged
Qubitium merged 1 commit into
mainfrom
fix/6375-int3-legacy
Nov 29, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/6375-int3-legacy

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

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 qzeros use 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.quantizer and, for a gptqmodel:< 7.0.0 tag, 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 to MIN_VERSION_WITH_V2), with QuantizeConfig.legacy_int3_qzeros_producer() reusing the existing meta_get_versionable() parser. The oldest gptqmodel tag wins, because Optimum appends the loading version to the producer list. Missing/third-party tags return None.

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_OFFSETS records 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 one log.warn.once naming the producer. convert_gptq_v1_to_v2_format() derives the producer from cfg. hf_convert_gptq_v1_to_v2_format() (the plain-HF bridge used by Optimum) passes the checkpoint meta through and retags model.config.quantization_config after conversion so a later save_pretrained() does not publish canonical words under the old < 7.0 tag.
  • gptqmodel/models/loader.py: records the detected producer on QuantizeConfig for 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:

checkpoint loader PPL generate("The capital of France is", 24)
canonical v1 (HEAD-written) base 0dd8e8abf 6.245 Paris, the capital of France...
legacy 6.0.3 encoding base 0dd8e8abf NaN The capital of France is (degenerate)
legacy 6.0.3 encoding this PR 6.245 same as canonical
legacy 5.6.12 encoding this PR 6.245 same as canonical
legacy bytes, no meta.quantizer this PR NaN degenerate (third-party path intentionally unchanged)

Synthetic tiny-Llama logits (max abs diff vs the v2 checkpoint oracle):

checkpoint base 0dd8e8abf this PR
canonical v1, no meta (AutoGPTQ-shaped) 0.0 0.0
canonical v1, gptqmodel:7.3.4 0.0 0.0
canonical v1, gptqmodel:7.0.0 0.0 0.0
legacy v1, gptqmodel:6.0.3 0.966797 0.0
legacy v1, gptqmodel:5.6.12 0.966797 0.0
legacy bytes, no meta 0.966797 0.966797

4-bit / 8-bit no-regression on real local checkpoints (loading with the legacy flag forced to 6.0.3 vs not forced):

checkpoint producer detected forced-flag max logit diff
Llama-3.2-1B-Instruct-GPTQ-4bits-gp128 (meta gptqmodel:5.4.2) 5.4.2 0.0
Qwen2-VL-2B-Instruct-GPTQ-Int8 (no meta) None 0.0

Re-save checks: loading the legacy 6.0.3 fixture and calling GPTQModel.save() writes canonical v1 qzeros and reloads with max logit diff 0.0; re-saving with format=gptq_v2 also reloads with 0.0. The plain-HF save_pretrained() path now writes canonical words and a 7.x producer tag and reloads with 0.0.

Tests

  • New: 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 check on all changed files: clean.

This PR is a draft.

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
Qubitium marked this pull request as ready for review November 29, 2026 13:11
@Qubitium
Qubitium merged commit 1615acc into main Nov 29, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/6375-int3-legacy branch November 29, 2026 13:11
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>
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