Skip to content

fix(checkpoint): accept 5.4.x gptaq booleans and quantize untied lm_head (#4013) - #4034

Merged
Qubitium merged 4 commits into
mainfrom
fix/issue-4013
Nov 14, 2026
Merged

Qubitium merged 4 commits into
mainfrom
fix/issue-4013

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4013

TL;DR

Four checkpoint-compatibility defects in the load/quantize/save path, fixed
with +735/-8 lines: 229 lines of source, 273 lines of tests, and 233 lines of
documentation. The architectural consequence: a checkpoint's producer is now explicit
about what a non-GPT-QModel loader can and cannot consume, and the lm_head
pseudo-layer no longer holds a stale module reference after subset planning.

Item Root cause Fix
1 _normalize_gptaq rejected 5.4.x booleans (gptqmodel/quantization/config.py:3513 on the parent commit) map False to None, True to GPTAQConfig, folding the legacy gptaq_alpha / gptaq_memory_device siblings
2 stage kept a HookedLinear that subset planning had replaced with the AUTO_NF12 wrapper, so _get_qualified_name failed (utils/structure.py:542) re-read the head from the tree after planning; keep the head out of the calibration-source pack
3 AWQ -: exclusions never reached modules_to_not_convert (config.py:5200) mirror them into the payload; warn on quantized lm_head and 8-bit AWQ
4 8-bit AWQ natural lane order is not what vLLM awq_marlin undoes keep natural order, warn on export, document why

1. The problem

1a. Legacy meta.gptaq

flowchart TD
    A["quantize_config.json<br/>meta.gptaq = false or true"] --> B["_normalize_gptaq"]
    B -.->|"parent commit"| X["ValueError: gptaq must be a<br/>GPTAQConfig, dict, or None"]:::fail

    classDef fail fill:#ffdddd,stroke:#cc0000,color:#660000
Loading

Real 5.4.2-produced checkpoints hit this, e.g.
Llama-3.2-1B-Instruct-GPTQ-4bits-gp128.

1b. Untied lm_head

flowchart TD
    A["run_layer_stage<br/>step kind = lm_head"] --> B["module = get_module<br/>model, lm_head"]
    B --> C["build_layer_subset_plans"]
    C --> D["_maybe_auto_dtype_cast_forward_module<br/>replaces the live head"]
    D -.->|"stage keeps the old object"| X["_get_qualified_name<br/>utils/structure.py:542<br/>ValueError"]:::fail

    classDef fail fill:#ffdddd,stroke:#cc0000,color:#660000
Loading

With offload_to_disk=False the same detach surfaced as
RuntimeError: Lazy forward direct-state materialization left META tensors.

For a tied head the identity check missed lazily loaded shells, so the run
continued until Cannot reload quantization source: missing checkpoint tensors ['weight'] (models/base.py:3607) instead of the explicit
NotImplementedError at module_looper.py:1801.

2. The new flow

flowchart TD
    A["run_layer_stage<br/>step kind = lm_head"] --> B["module = get_module<br/>model, lm_head"]
    B --> C["build_layer_subset_plans"]
    C --> D{"live head replaced?"}
    D -->|"yes"| E["re-read module from the model tree<br/>stage_layer.py"]
    D -->|"no"| F["module unchanged"]
    E --> G["refresh full and cur_layer_device"]
    F --> H["_prepare_layer_direct_state_for_forward"]
    G --> H
    H --> I["calibration forward + quantize"]
    I --> J["submodule_finalize<br/>create_quant_module"]
    J --> K["post_quantize moves the head to CPU"]
Loading

The head is also excluded from AUTO_NF12 calibration packing
(models/base.py:3938): the head is its own quantization stage, its dense
source is required as the quant source, and packing it detached it from the tree
that post_quantize walks.

3. Tied-head detection

flowchart TD
    A["_lm_head_is_tied"] --> B{"find_tied_parameters<br/>matches lm_head.weight?"}
    B -->|"yes"| R["tied"]:::warn
    B -->|"no"| C{"config.tie_word_embeddings?"}
    C -->|"no"| N["untied"]
    C -->|"yes"| D{"head weight is META?"}
    D -->|"no"| N
    D -->|"yes"| E["LazyTurtle.has_checkpoint_tensor"]
    E --> F{"checkpoint owns lm_head.weight?"}
    F -->|"no"| R
    F -->|"yes"| N
    R --> Z["NotImplementedError:<br/>tied lm_head quantization is unsupported"]:::warn

    classDef warn fill:#fff3cd,stroke:#cc8800,color:#664400
Loading

has_checkpoint_tensor resolves the same runtime-to-checkpoint aliases as the
materializers but never loads a tensor, so the check stays cheap on large
checkpoints.

4. Per-subsystem detail

4a. Legacy config normalization

_normalize_gptaq and _normalize_foem now accept a boolean. from_quant_config
folds the legacy sibling keys into the object form before the target class is
resolved, so gptaq_alpha and gptaq_memory_device are no longer dropped as
unknown keys.

4b. AWQ exclusions and loadability metadata

flowchart LR
    A["quantize_config"] --> B{"method == AWQ?"}
    B -->|"yes"| C["excluded_module_patterns"]
    C --> D["-: dynamic rules<br/>+ declared list"]
    D --> E["modules_to_not_convert"]
    B -->|"no"| F["no key written"]
    A --> G["hf_compat_warnings"]
    G --> H{"lm_head quantized?"}
    H -->|"yes"| I["warn: HF random-initializes the head"]:::warn
    G --> J{"8-bit AWQ?"}
    J -->|"yes"| K["warn: vLLM awq_marlin decodes the wrong lane order"]:::warn

    classDef warn fill:#fff3cd,stroke:#cc8800,color:#664400
Loading

modules_to_not_convert is a mirror, not a second source of truth: the -:
rules in dynamic stay authoritative and the mirror is recomputed on every
save.

4c. 8-bit AWQ lane order — decision

Natural lane order stays canonical for GPT-QModel; exporting 8-bit AWQ logs a
warning.
I did not flip the packer to [0, 2, 1, 3] because (a) it would
silently change the meaning of every 8-bit AWQ checkpoint published since #3993,
with no version marker to distinguish the layouts, and (b) the awq_marlin side
of that decision lives in a compiled extension
(gptqmodel_ext/marlin/awq_marlin_repack.cu), so it cannot be validated here
without rebuilding and re-qualifying that kernel on every supported
architecture.

Measured with an independent FP64 oracle (out_features=64,
in_features=128, group_size=32, 8-bit codes, max absolute weight error):

Decode Error vs FP64 oracle
natural order (GPT-QModel) 4.12e-07
vLLM [0, 2, 1, 3] order 8.42

5. Failure and ambiguity contract

Situation Before After
5.4.x meta.gptaq: false ValueError at config parse GPTAQ disabled
5.4.x meta.gptaq: true ValueError GPTAQConfig with the recorded alpha/device
untied lm_head=True ValueError: Could not find the given module quantizes, saves, reloads
tied lm_head=True RuntimeError: missing checkpoint tensors ['weight'] NotImplementedError: ... tied weights is not supported
AWQ -: exclusions missing from modules_to_not_convert written, and reloaded
quantized lm_head saved silent save-time warning
8-bit AWQ saved silent save-time warning

6. Change inventory

Area File Lines Purpose
config gptqmodel/quantization/config.py +111/-4 legacy booleans, legacy sibling keys, AWQ exclusion mirror
looper gptqmodel/looper/stage_layer.py +18 re-read the live head after planning
looper gptqmodel/looper/module_looper.py +35/-4 _lm_head_is_tied
model gptqmodel/models/base.py +7 keep the head out of the AUTO_NF12 pack
writer gptqmodel/models/writer.py +35 hf_compat_warnings + save-time logging
structure gptqmodel/utils/structure.py +23 LazyTurtle.has_checkpoint_tensor
tests tests/test_checkpoint_compat.py +273 14 regressions
docs docs/checkpoint-compat.md, docs/awq-packed-layout.md, README.md +233 contracts and diagrams

7. Tests

Test Invariant
test_legacy_boolean_gptaq_false_disables_gptaq 5.4.x false maps to no GPTAQ
test_legacy_boolean_gptaq_true_keeps_alpha_and_device true keeps alpha and device
test_legacy_boolean_gptaq_true_without_alpha_uses_default missing alpha falls back to the default
test_legacy_boolean_foem_meta sibling normalizer follows the same contract
test_boolean_gptaq_and_foem_accepted_by_the_constructor direct constructor accepts booleans
test_awq_dynamic_exclusions_are_written_to_modules_to_not_convert -: rules reach the key and survive reload
test_awq_without_exclusions_writes_no_modules_to_not_convert no empty key
test_hf_compat_warning_for_quantized_lm_head head caveat reported
test_hf_compat_warning_for_8bit_awq 8-bit caveat reported
test_awq_8bit_lane_order_is_natural 8-bit packer matches an independent lane reference
test_awq_8bit_vllm_order_would_swap_lanes_one_and_two vLLM undo is not this layout
test_untied_lm_head_quantizes_and_reloads quantize, save, reload, finite logits
test_tied_lm_head_is_rejected_with_the_explicit_error NotImplementedError

Fail-before / pass-after on the same file (RTX 4090, CUDA 13.0 venv):

$ git stash && pytest tests/test_checkpoint_compat.py -q
11 failed, 3 passed
$ git stash pop && pytest tests/test_checkpoint_compat.py -q
14 passed

8. Validation

Hardware: RTX 4090 (GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae) and the R9700
via HIP_VISIBLE_DEVICES=0. The RTX 5090 was never used.

CUDA:  pytest tests/test_checkpoint_compat.py -q   -> 14 passed
ROCm:  pytest tests/test_checkpoint_compat.py -q   -> 14 passed
CUDA:  pytest tests/test_serialization.py tests/test_gptaq_foem_batch.py -q -> 11 passed
CUDA:  pytest tests/test_awq_affine_range.py tests/test_awq_pack_vectorized.py -q -> 9 passed
ruff:  ruff check gptqmodel docs/eora tests setup.py -> All checks passed!

Item 1 against a real 5.4.2 checkpoint
(/monster/data/model/Llama-3.2-1B-Instruct-GPTQ-4bits-gp128, meta.gptaq: false):

before: ValueError: QuantizeConfig: `gptaq` must be a GPTAQConfig, dict, or None.
after : parsed gptaq = None bits = 4 / LOAD OK: LlamaForCausalLM

Item 2 on the repo's own scenario (Qwen1.5-1.8B-Chat, 128 x 1024-token c4
samples, QuantizeConfig(bits=4, group_size=32, lm_head=True)):

lm_head loss: 0.0005524732 diff 99.80%      (EXPECT_LM_HEAD_LOSS = 0.0005535857)
CHECK_LM_HEAD_LOSS OK
RELOAD lm_head: MarlinLinear
Quantize summary: 1 layer(s), 169 module(s), 1m02s, peak VRAM 12.81 GiB

The same flow also passes on a tiny untied Qwen2 with offload_to_disk=False,
saving and reloading to MarlinLinear with finite logits.

9. Reviewer notes

  • tests/test_lm_head.py::TestLmHeadQuant::test_quant_lm_head cannot be
    collected in these venvs: neither CUDA nor ROCm has datasets, and the test's
    eval half needs lm_eval. I ran its quantization half unchanged (via a local
    JSON-lines stand-in for datasets.load_dataset) on the real model, shown
    above. The tiny-model regression in tests/test_checkpoint_compat.py covers
    the same path without either dependency.
  • tests/test_awq_dynamic_group_size.py::test_dynamic_group_size_selects_compatible_kernel_and_packs
    fails on main too (AttributeError: 'Stub' object has no attribute '_module_format'); it is pre-existing and unrelated.
  • Item 3's HF-side error magnitudes (1.59 / 1.27) are from the issue; this PR
    fixes the metadata that caused them but does not re-measure HF logits.
  • 8-bit AWQ: choosing natural order means an 8-bit AWQ checkpoint is not
    loadable by vLLM awq_marlin. That limitation is now loud at save time and
    documented, not silent.

…ead (#4013)

Four checkpoint-compatibility defects, all in the load/quantize/save path:

1. `_normalize_gptaq` accepted only None/dict/GPTAQConfig, so every 5.4.x
   checkpoint (`meta.gptaq: false|true`) raised ValueError. Map False to None
   and True to GPTAQConfig, folding the legacy `gptaq_alpha` /
   `gptaq_memory_device` siblings into the object. `_normalize_foem` takes the
   same shapes.

2. `lm_head=True` crashed on untied models: subset planning replaces the live
   head with the AUTO_NF12 forward wrapper, leaving the stage's `module`
   reference detached from the tree, so direct-state materialization raised
   "Could not find the given module within the target model"
   (utils/structure.py:542). Re-read the head from the tree after planning and
   keep the head out of the calibration-source pack. Tied heads now reach the
   explicit NotImplementedError via `_lm_head_is_tied`, which also consults the
   checkpoint for a missing `lm_head.weight`.

3. AWQ `-:` exclusions are written to `modules_to_not_convert` so HF and vLLM
   AutoAWQ skip those modules instead of building uninitialized layers. A
   quantized lm_head and 8-bit AWQ now log their loadability caveats at save
   time.

4. 8-bit AWQ keeps natural lane order as GPT-QModel's canonical layout and
   warns on export: vLLM `awq_marlin` undoes a `[0,2,1,3]` interleave that this
   layout never applied.

Tests: tests/test_checkpoint_compat.py (14 cases, CUDA + ROCm).
Docs: docs/checkpoint-compat.md, docs/awq-packed-layout.md, README.
Comment thread tests/test_checkpoint_compat.py Resolved
…mplicit (fall through) returns'

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
@Qubitium
Qubitium merged commit 85f18c0 into main Nov 14, 2026
5 checks passed
@Qubitium
Qubitium deleted the fix/issue-4013 branch November 14, 2026 17:04
Qubitium added a commit that referenced this pull request Nov 18, 2026
…5027) (#5041)

`WeightOnlyLooper` refused `lm_head=True` on a tied model with
`find_tied_parameters(model)`, which only compares materialized parameters.
With the default `offload_to_disk=True` the model is a lazy shell whose
`lm_head.weight` and `embed_tokens.weight` are independent META parameters, so
the guard never fired: the run quantized every layer and then died on the
head's meta tensor (`Cannot copy out of meta tensor; no data!`), an error that
points away from the real cause. The module looper had already been fixed in
#4034.

Move `ModuleLooper._lm_head_is_tied` into
`utils.tied_weights.lm_head_quantization_is_tied()` and call it from both
loopers, so one check decides the answer: real storage identity, then
accelerate's tied report, then the lazy-shell checkpoint evidence, then the
resolved tie snapshot, then the root config declaration.

Fixes #5027
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