Skip to content

fix(hf): rebuild a deferred qzeros after a Marlin->Torch class swap - #6395

Merged
Qubitium merged 3 commits into
mainfrom
fix/6383-reselected-qzeros
Nov 29, 2026
Merged

Qubitium merged 3 commits into
mainfrom
fix/6383-reselected-qzeros

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

_materialize_hf_sym_qzeros decides "this kernel owns its zero point, skip the rebuild" from qzeros not being a registered buffer. That describes Marlin, but _reselect_offloaded_marlin_kernels (#6253) swaps a Marlin layer to a portable kernel by assigning __class__, so the replacement inherits Marlin's zero-point Parameter.

When the loader left that parameter on meta — a deferred symmetric qzeros (#6274) is absent from the offload map, so nothing materializes it — the rebuild replaced it with a zero-length parameter, and the first forward failed in TorchLinear._decode_qzeros:

File "gptqmodel/nn_modules/qlinear/torch.py", line 1109, in _decode_qzeros
    self.qzeros.unsqueeze(2).expand(-1, -1, self.pack_factor),
IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 2)

The rebuild now uses the final kernel: a real Marlin/AWQ-Marlin layer keeps its own representation, and a reselected portable kernel drops the inherited parameter and rebuilds the words with qzeros_standard_shape for its own packed geometry.

Repro (published checkpoint, no synthesis)

ModelCloud/Llama-3.2-1B-Instruct-gptqmodel-4bit-vortex-v1 (meta.quantizer: gptqmodel:1.1.0, checkpoint_format: gptq, group_size: 32, desc_act: true, sym: true):

AutoModelForCausalLM.from_pretrained(path, device_map="auto",
    max_memory={0: "350MiB", "cpu": "2GiB"}, offload_folder="/tmp/off")
tree result
0dd8e8ab (pre-wave) OK, 112 TorchLinear, logits sha1 203615fe…
3082b02d0 (main) ValueError: GPTQ Marlin requires CUDA tensors, got cpu (issue #6383)
main + #6387 loads, then IndexError above on the first forward
main + #6387 + this PR OK, 112 TorchLinear, logits sha1 203615fe… (identical to pre-wave)

Same shape on ModelCloud/DeepSeek-R1-Distill-Qwen-7B-gptqmodel-4bit-vortex-v1 (196 layers).

Tests

  • tests/test_hf_bridge_reselected_qzeros_6383.py (new) — fails on main with qzeros == (0,) and passes with the fix.
  • tests/test_hf_bridge_legacy_qzeros_marlin_6274.py::test_deferred_qzeros_are_skipped_for_a_kernel_that_owns_the_zero_point — the old stand-in was a Torch module with a Parameter qzeros, which the fix now (correctly) rebuilds; it now uses a Marlin-typed stand-in so the Marlin skip stays covered without CUDA.

ruff check clean. CPU: 6 passed in the two files. GPU (cuda:0): test_hf_bridge_cpu_offload_5022.py, test_marlin_cpu_offload_post_init.py, test_requantize_offload_head_6348.py, test_hf_bridge_grouped_load_cost_6336.py all pass.

Note

This is independent of #6387 (different lines, different lifecycle point). #6387 is still required for the load to get this far on main; without it the layer never reaches the Torch kernel. A separate, narrower case remains: a legacy checkpoint whose layers are dispatched with a GPU placement and a deferred qzeros not in the map still fails inside Accelerate's init_hook (qzeros is on the meta device); that needs the deferral materialized before dispatch_model, which is a larger change.

wave3-verify and others added 3 commits November 29, 2026 13:36
`_materialize_hf_sym_qzeros` keyed "this kernel owns its zero point" off
`qzeros` not being a registered buffer. That describes Marlin, but
`_reselect_offloaded_marlin_kernels` swaps a Marlin layer to a portable
kernel by assigning `__class__`, so the replacement inherits Marlin's
zero-point `Parameter`. When the loader left that parameter on meta (a
deferred symmetric `qzeros` is absent from the offload map, so nothing
materializes it) the rebuild replaced it with a zero-length parameter and
the first forward failed in `TorchLinear._decode_qzeros`:

    IndexError: Dimension out of range (expected to be in range of [-2, 1],
    but got 2)

Rebuild the words in the shape the final kernel reads instead: drop the
inherited parameter and use `qzeros_standard_shape` for the layer's packed
geometry. A real Marlin/AWQ-Marlin layer keeps its own representation.

Measured on `ModelCloud/Llama-3.2-1B-Instruct-gptqmodel-4bit-vortex-v1`
with `device_map="auto"` + `max_memory={0: "350MiB", "cpu": "2GiB"}`:
112 TorchLinear layers and logits identical to the pre-wave 0dd8e8a
offload path (sha1 203615fe...).
Conflicts in gptqmodel/utils/model.py::_materialize_hf_sym_qzeros:
take main's #6391 rebuild, but size the rebuilt buffer with
qzeros_standard_shape() and drop the inherited Parameter first.

_reselect_offloaded_marlin_kernels swaps __class__ in place, so the
replacement still owns Marlin's tile-layout qzeros Parameter; copying
that shape into the portable kernel is what produced the forward-time
IndexError in #6383.
@Qubitium
Qubitium merged commit 407298a into main Nov 29, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/6383-reselected-qzeros branch November 29, 2026 14:26
Qubitium pushed a commit that referenced this pull request Nov 29, 2026
Picks up main's commits since the previous merge base (6b25d2c):
#6395, #6399, #6407, #6408, #6409, #6410. Auto-merged cleanly; the
stable-ABI guards report no new offenders.
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

Development

Successfully merging this pull request may close these issues.

1 participant