Skip to content

fix(exl3): resolve head_bits against tied output heads (#6347) - #6354

Merged
Qubitium merged 1 commit into
mainfrom
fix/6347-exl3-tied
Nov 29, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/6347-exl3-tied

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #6347

Mechanism

#6224 (0558c6e) made head_bits imply lm_head=True inside
EXL3Config.__post_init__. The config cannot know whether the loaded model
ties its output head to the input embedding, so ModuleLooper hit its
lm_head-with-tied-weights refusal after the model was loaded and the
calibration dataset was prepared:

  File "gptqmodel/models/base.py", line 3681, in quantize
  File "gptqmodel/models/base.py", line 4492, in _quantize_with_calibration
  File "gptqmodel/looper/module_looper.py", line 2074, in loop
  File "gptqmodel/looper/module_looper.py", line 2196, in _loop_impl
NotImplementedError: quantization of `lm_head` layer with tied weights is not supported. Untie the model weights before quantization.

That turned a configuration that used to work (tied head stays with the
embedding) into a late hard failure on Llama-3.2-1B/3B, Qwen2.5-0.5B..3B,
Qwen3-0.6B..4B, Gemma, SmolLM, and every other small tied model.

Contract

head_bits remains an implied head request (so #6224 is preserved), but the
implication is resolved against the real model before calibration:

Request Tied head Untied head
head_bits=N (default lm_head=None) warn, keep the head tied, record lm_head=False schedule/pack the head at N
head_bits=N, lm_head=True hard NotImplementedError, now raised before calibration schedule/pack the head at N
head_bits=N, lm_head=False no head request; the explicit opt-out wins same
lm_head=False, head_bits=None unchanged unchanged

Why this contract:

  • It is the only one that keeps a tied checkpoint reloadable. The tied run
    writes lm_head=False; if __post_init__ still forced lm_head=True for
    any head_bits, QuantizeConfig.from_quant_config() would claim a packed
    head that the checkpoint never wrote. lm_head=None is therefore the
    "not requested" sentinel, and an explicit False survives the round-trip.
  • It does not silently ignore head_bits: the implied tied case logs a
    warning that names the shared weight, the consequence, and the opt-in
    (untie before quantize(), or load an untied-head checkpoint). The
    explicit-lm_head=False case logs that the recorded rate is not applied.
  • It does not crash after minutes of work: BaseQModel.quantize() resolves
    the request before _prepare_calibration_before_rotation() and before
    _quantize_with_calibration() prepares the calibration batches.
    ModuleLooper._loop_impl() keeps the same resolution for callers that
    drive the looper directly.
  • It does not auto-untie. Untying duplicates a vocabulary-sized matrix and
    changes the checkpoint layout; that is an explicit user choice, not a
    side effect of a rate option. The warning tells the user how to do it.

Red -> green

New tests/test_exl3_head_bits_tied_6347.py (CPU only, no CUDA packer):

Case origin/main (be14824) this branch
(a) tied + head_bits=6 FAIL: NotImplementedError: ... tied weights from _loop_impl PASS: warning + head stays tied + lm_head=False
explicit lm_head=True tied FAIL: CPU EXL3 device gate fires before the loop PASS: hard error raised by quantize() preflight
explicit lm_head=False + head_bits FAIL: lm_head is forced to True PASS: opt-out preserved
(b) untied + head_bits=6 PASS (guard) PASS: head is a planned step, wrapped in HookedLinear, packer K=6 vs projection K=4
(c) lm_head=False, head_bits=None PASS (guard) PASS: no head step, no dynamic rule, no warning

The file is red at origin/main (4 failed, 2 passed) and green here
(6 passed). Cases (b) and (c) are preserved-behavior guards, so they pass
on both revisions; the regression cases are (a) and the two explicit-request
variants. Because EXL3 packing itself is CUDA/HIP-only, the test drives the
real ModuleLooper head-resolution/planning path with a real tiny Llama on
CPU and asserts the rate that reaches EXL3Processor._build_quant_args; it
never calls the CUDA quantizer.

Verification

cd tests
PYTHONPATH=<repo>:<repo>/tests CUDA_VISIBLE_DEVICES="" \
  /root/venv/bin/python -m pytest test_exl3_head_bits_tied_6347.py \
    test_exl3_fractional_bits.py qcfg test_quant_bits_validation.py \
    test_quantize_config_offload_tempdir.py test_rotation_format_gate_4666.py \
    test_exl3_skip_reasons_4981.py test_exllamav3_cpu.py \
    test_exl3_generic_geometry.py test_exl3_in_session_fp16_4789.py \
    test_exl3_auto_nf12_recovery.py test_exl3_cancelled_run_5083.py \
    -q -rs -p no:cacheprovider
# 642 passed, 13 skipped (GPU-only)

All test_exl3_*.py test_exllama*.py: 576 passed, 650 skipped (GPU-only),
no failures. Full tests/qcfg: 268 passed.

ruff check on the changed files: clean.

module_tree/test_deepseek_v41_flash.py fails 7 tests / errors 9 on this
branch and identically on origin/main (_gptqmodel_checkpoint_selection
unsupported argument); it is unrelated and pre-existing.

Manual GPU smoke test (RTX 4090, not part of the CPU test)

Tiny 2-layer tied Llama, EXL3Config(bits=4, head_bits=6, device="cuda:0"):

  • quantize() warns, sets lm_head=False, and keeps lm_head a tied
    Linear(256, 128) that shares storage with embed_tokens;
  • q_proj becomes ExllamaV3Linear;
  • save + reload + forward succeed (logits (1, 4, 256), finite).

Tiny untied Llama, same config:

  • lm_head=True, and the head becomes ExllamaV3Linear with trellis
    (8, 16, 96) (6-bit);
  • q_proj is ExllamaV3Linear;
  • save + reload + forward succeed.

Existing test update

tests/test_exl3_fractional_bits.py::test_head_bits_overrides_an_explicit_lm_head_false
is renamed to test_head_bits_respects_an_explicit_lm_head_false and now
asserts lm_head is False. The old assertion encoded the reload bug: a tied
run saves lm_head=False with its head_bits, and forcing the implication
back to True on load makes the loader expect a packed head the checkpoint
never wrote.

#6224 made `head_bits` imply `lm_head=True` in `EXL3Config.__post_init__`.
The config cannot know whether the loaded model ties its output head to the
input embedding, so `ModuleLooper` refused the request on tied models after
the model was loaded and calibration was prepared.

Keep the implication for untied models, but resolve it against the real
model before any calibration work:

- implied `head_bits` on a tied head warns, keeps the head tied, and records
  `lm_head=False` so the saved config matches the tensors it wrote;
- explicit `lm_head=True` on a tied head keeps the hard error, now raised by
  `quantize()` instead of deep inside the loop;
- explicit `lm_head=False` is respected, so a tied checkpoint reloads with a
  dense head instead of claiming a packed one;
- an untied model still schedules and packs the head at `head_bits`.

Fixes #6347
@Qubitium
Qubitium marked this pull request as ready for review November 29, 2026 10:48
@Qubitium
Qubitium merged commit 0dd8e8a into main Nov 29, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/6347-exl3-tied branch November 29, 2026 10:48
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