Skip to content

fix(hf): defer a legacy qzeros only on a real floor-width mismatch - #6409

Merged
Qubitium merged 2 commits into
mainfrom
fix/6394-deferred-qzeros-dispatch
Nov 29, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/6394-deferred-qzeros-dispatch

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #6396
Refs #6383 (mixed-map family; its GPTQ Marlin requires CUDA tensors half
landed in #6391)

A published pre-7.3.4 GPTQ checkpoint loaded with a GPU cap that forces
offload died inside accelerate.dispatch_model:

ValueError: qzeros is on the meta device, we need a `value` to put in on 0.

Exact repro (/tmp/legacyoff/mixed.py 3GiB, ModelCloud/DeepSeek-R1-Distill-Qwen-7B-gptqmodel-4bit-vortex-v1,
meta.quantizer: ["gptqmodel:1.7.4"], checkpoint_format: "gptq", bits=4,
group_size=32, sym=true, desc_act=true; all 196 qzeros tensors are already
32-aligned and ceil-width):

tree output
origin/main 6b25d2c69 3GiB FAIL ValueError qzeros is on the meta device, we need a 'value' to put in on 0.
this PR 3GiB OK {'MarlinLinear': 196}

_hf_legacy_qzeros_quant_linear deferred every symmetric legacy
checkpoint: PackedGroupedQuantLinear._save_to_state_dict popped qzeros from
the loader's expected-key set, so the checkpoint tensor was never loaded and
the module kept its constructor (meta) buffer. _materialize_hf_sym_qzeros
only runs from hf_gptqmodel_post_init, which Optimum calls from
post_init_model — after accelerate.dispatch_model has already tried to
place the meta buffer.

The deferral exists only to hide the pre-#2983 floor-width mismatch
(out_features // 32 * bits vs ceil(out_features * bits / 32)). Both widths
are identical for a 32-aligned projection, so there is nothing to defer there.

What Changed

Tests

New tests/test_deferred_qzeros_dispatch_6396.py:

  • test_matching_width_legacy_layer_keeps_its_qzeros_buffer — a 32-aligned
    legacy layer keeps the marker cleared and qzeros in state_dict(); a
    112-wide one keeps the deferral.
  • test_matching_width_legacy_checkpoint_has_no_meta_qzeros_before_post_init —
    a synthetic pre-7.3.4 checkpoint loads with no resident layer whose
    qzeros is meta, inspected at the post-init entry (the state
    dispatch_model sees).
  • test_floor_width_legacy_checkpoint_still_defers_and_loads — floor-width
    control (intermediate_size=112): the deferral still fires, the buffer is
    rebuilt to (2, 14) with one constant value, and the forward is finite.
  • test_real_legacy_checkpoint_loads_on_a_mixed_gpu_cpu_map (GPU-gated): the
    published checkpoint at max_memory={0: "3GiB", "cpu": "14GiB"} loads and
    forwards finite.

Fixture updates for the new behavior:

  • tests/test_hf_bridge_cpu_offload_5022.py: the two hand-built legacy
    MarlinLinear fixtures use a floor-width out_features (112) or re-apply
    the loader marker, because a 32-aligned layer no longer defers.
  • tests/test_hf_bridge_legacy_qzeros_marlin_6274.py:
    test_legacy_deferred_qzeros_on_cpu_offload_still_raises asserted the
    refusal for this aligned fixture; it is now
    test_matching_width_legacy_checkpoint_loads_on_cpu_offload, pinning the
    new outcome.

Red on origin/main 6b25d2c69 (new file only, fix not applied):

$ CUDA_VISIBLE_DEVICES= pytest test_deferred_qzeros_dispatch_6396.py -q
FAILED ...::test_matching_width_legacy_layer_keeps_its_qzeros_buffer
E   assert True is False            # aligned layer still defers
FAILED ...::test_matching_width_legacy_checkpoint_has_no_meta_qzeros_before_post_init
E   AssertionError: assert ['model.layers.0.self_attn.k_proj', ...] == []
2 failed, 1 passed

Green:

# GPU, cuda:0 (pinned 4090)
cd tests && CUDA_VISIBLE_DEVICES=0 PYTHONPATH=..:. /root/venv/bin/python -m pytest \
  test_deferred_qzeros_dispatch_6396.py test_hf_bridge_legacy_qzeros_marlin_6274.py -q -rs
# 10 passed, 0 skipped

# CPU, same six suites on main and on this branch
CUDA_VISIBLE_DEVICES= ... pytest test_checkpoint_interop_4053.py \
  test_hf_bridge_cpu_offload_5022.py test_hf_bridge_legacy_qzeros_marlin_6274.py \
  test_int3_legacy_qzeros_6375.py test_marlin_cpu_offload_post_init.py \
  kernels/test_qlinear_hierarchy.py -q -rs
tree result
6b25d2c69 (main) 15 failed, 59 passed, 14 skipped
this PR 15 failed, 59 passed, 14 skipped

The 15 failures are identical on both trees: the pre-existing #6386
tokenizer-fixture rejection in test_checkpoint_interop_4053.py
(ValueError: The tokenizer ... is unusable), plus the one AWQ node whose
expected "Missing required quantized weights" message that fixture now
preempts. test_cpu_offload_* skip on a CPU-only run (they need one CUDA
device plus the CPU); they pass on the pinned GPU run below.

Also verified: a current (7.3.4+) checkpoint loads with no legacy/deferral
marker, no meta qzeros, and finite logits.

Measured Impact

Published DeepSeek-R1-Distill-Qwen-7B-gptqmodel-4bit-vortex-v1, cuda:0
(RTX 4090), device_map="auto":

Case 6b25d2c69 (main) this PR
{0: "3GiB", "cpu": "14GiB"} ValueError: qzeros is on the meta device... loads, 196 MarlinLinear, finite logits
{0: "1GiB", "cpu": "14GiB"} loads (196 TorchLinear) loads (196 TorchLinear, post-init reselect)
{0: "350MiB", "cpu": "14GiB"}, offload_folder — see below
device_map="cpu" — see below

The 7B 1GiB/disk/CPU forwards reselect all 196 layers to the portable kernel.
On this host (load average ~1600 while measuring) one 7B forward at those caps
exceeds the measurement budget, so the offload caps are additionally verified
on the smaller published legacy checkpoint
ModelCloud/Llama-3.2-1B-Instruct-gptqmodel-4bit-vortex-v1
(meta.quantizer: gptqmodel:1.1.0, same layout, 112 layers), cuda:0:

1B case result
{0: "1GiB", "cpu": "8GiB"} loads in 9.4s, 112 MarlinLinear, finite logits
{0: "350MiB", "cpu": "8GiB"}, offload_folder loads, 112 TorchLinear, finite logits
device_map="cpu" loads in 75.8s, 112 TorchCpuAvx512Linear, finite logits

The portable-kernel inductor compile was disabled in the measurement harness
for these three rows (it is per-layer, orthogonal to this change, and dominated
the wall time on the loaded host); everything else is the default HF/Optimum
path.

No VRAM or speed change: the loaded buffer replaces a regenerated constant of
the same shape and dtype.

Logic Flow Documentation

  • This change does not alter a major logic flow. It narrows an existing
    construction-time compatibility marker; the post-init rebuild path is
    unchanged.

Review Requirements

  • Reviewed this PR.
  • Matches the owning module (nn_modules/qlinear) and the existing marker
    conventions (_HF_LEGACY_QZEROS, _HF_CHECKPOINT_SYM).
  • No monkeypatching.
  • Minimal surface: one constructor condition; tests updated where the old
    premise no longer holds.
  • Regressions considered: asymmetric checkpoints never deferred; a
    32-aligned width is always ceil-equal, so no checkpoint loses the
    translation path.

Kernel Accuracy Requirements

Not applicable: no kernel change. The loaded values are the checkpoint's own
symmetric constant (bit-identical to the regenerated word).

Notes

Measured on 6b25d2c69, which includes #6391. #6387 is not required for this
case: the failure happens during dispatch, before any post-init reselect.

agent added 2 commits November 29, 2026 14:11
`_hf_legacy_qzeros_quant_linear` wraps every symmetric checkpoint from a
pre-7.3.4 producer with `_HF_DEFER_SYM_QZEROS = True`. The deferral hides
`qzeros` from the loader's expected-key set, which is only needed when the
checkpoint stores the pre-#2983 floor width (`out_features // 32 * bits`).
The two widths are identical for a 32-aligned projection, but the buffer was
still hidden: the module kept its constructor (meta) `qzeros`, and
`_materialize_hf_sym_qzeros` only runs from `hf_gptqmodel_post_init`, which
Optimum calls *after* `accelerate.dispatch_model`. A GPU/disk placement then
failed with

    ValueError: qzeros is on the meta device, we need a `value` to put in on 0.

Measured on the published
`ModelCloud/DeepSeek-R1-Distill-Qwen-7B-gptqmodel-4bit-vortex-v1`
(`meta.quantizer: gptqmodel:1.7.4`; all 196 `qzeros` already ceil-width) with
`max_memory={0: "3GiB", "cpu": "14GiB"}`: the load fails before this change
and loads with finite logits after it.

The constructor now clears the deferral for a 32-aligned layer, so the
checkpoint tensor is loaded normally; a genuine floor-width layer keeps the
deferral and is still regenerated after loading.

`tests/test_deferred_qzeros_dispatch_6396.py` pins both branches, including a
GPU-gated mixed-map load+forward on the real checkpoint. Two
`test_hf_bridge_cpu_offload_5022.py` fixtures that hand-build a legacy Marlin
layer use a floor-width shape (or re-apply the loader marker), because a
32-aligned layer no longer defers.
A 32-aligned legacy checkpoint no longer defers `qzeros`, so the old
"legacy deferred qzeros on CPU offload still raises" node no longer describes
it: the refusal only guards a genuine floor-width buffer. Rename it to pin the
new outcome (the checkpoint loads), and keep the floor-width deferral covered
by the resident-CPU node in `test_deferred_qzeros_dispatch_6396.py`.

The GPU-gated real-checkpoint node keeps only the 3GiB map: a 1GiB cap
reselected all 196 layers to the portable kernel, whose optional inductor
compile makes the node a multi-minute run without adding coverage of the
dispatch failure (the 3GiB map still exercises it).
@Qubitium
Qubitium marked this pull request as ready for review November 29, 2026 14:25
@Qubitium
Qubitium merged commit 2030319 into main Nov 29, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/6394-deferred-qzeros-dispatch branch November 29, 2026 14:25
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