Skip to content

fix(offload): reselect resident CPU Marlin with deferred qzeros (#6152 follow-up) - #6391

Merged
Qubitium merged 3 commits into
mainfrom
fix/6152-legacy-cpu-reselect
Nov 29, 2026
Merged

Qubitium merged 3 commits into
mainfrom
fix/6152-legacy-cpu-reselect

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

device_map="auto" + max_memory that resolves the whole model to CPU still crashes for a checkpoint marked pre-7.3.4, even after #6253. It raises the original ValueError: GPTQ Marlin requires CUDA tensors, got cpu instead of reselecting the layer:

AutoModelForCausalLM.from_pretrained(
    "/monster/data/model/Qwen2.5-0.5B-Instruct-gptq-4bit",   # meta.quantizer = gptqmodel:1.9.0
    dtype=torch.float16, device_map="auto",
    max_memory={0: "100MiB", "cpu": "20GiB"},
)
# before: ValueError: GPTQ Marlin requires CUDA tensors, got `cpu`.

This is the all-CPU half of #6152. The disk/CPU-offload half is intentionally unchanged (see below).

Root cause

Two merged changes interact:

  1. _HF_DEFER_SYM_QZEROS (fix(hf): packable selection, per-layer shape dispatch and legacy qzeros in the HF bridge (#6191, #6178, #6336) #6274) hides a legacy symmetric checkpoint's qzeros from the core loader, so the parameter stays a meta placeholder on an otherwise CPU-resident layer.
  2. _marlin_needs_offload_kernel (fix(hf): reselect offload-safe kernels for Marlin at post-init (#6152) #6253) used checkpoint_payload_is_meta() as the "offloaded" test. That is true here, so it entered the offload branch, found no layer-level Accelerate hook, returned False, and kept MarlinLinear. post_init then reached Marlin's CUDA check on CPU.

Even with the reselect decision fixed, _materialize_hf_sym_qzeros() would still take the "this kernel owns its zero point" branch, because a class swap leaves Marlin's qzeros as a parameter rather than a buffer. It would replace the placeholder with an empty parameter, and the portable Torch decode would fail.

Fix

  • _marlin_needs_offload_kernel: after the real offload check, decide resident-CPU from the primary packed weight (runtime_device()), which is meta only for a layer whose payload actually lives in an offload map. A meta qzeros placeholder no longer hides the all-CPU reselect.
  • _materialize_hf_sym_qzeros: when a class swap left qzeros as a parameter on a non-Marlin kernel, rebuild it with the symmetric constant instead of discarding it.
  • The offloaded-layer refusal is unchanged: a meta qweight on a replacement kernel still raises the documented RuntimeError telling the user to load with offload_state_dict=False and a resident device map.

Evidence

Real Qwen2.5-0.5B-Instruct-gptq-4bit (gptqmodel 1.9.0 sidecar, checkpoint_format: gptq, current-width qzeros), all-CPU device_map="auto" + max_memory={0: "100MiB", "cpu": "20GiB"}:

tree kernels forward
main 3082b02d0 — ValueError: GPTQ Marlin requires CUDA tensors, got cpu
this PR 168 × TorchLinear finite; max|dlogit|=0.0586, argmax agree 100% vs the all-GPU Marlin reference

qzeros after load is the rebuilt constant 0x88888888 with the constructor's current width, _HF_DEFER_SYM_QZEROS=False.

The documented offload refusal still fires for the same checkpoint with CPU offload (max_memory={0: "32KiB", "cpu": "4GiB"}):

RuntimeError: MarlinLinear: a legacy floor-width symmetric GPTQ checkpoint cannot be
materialized on a CPU/disk-offloaded layer. Load it with `offload_state_dict=False`
and a resident device map.

(Note for reviewers: offload_state_dict is popped and ignored by Transformers 5.23's from_pretrained; only the "resident device map" part of that message is actionable there. Not changed here.)

Tests

New nodes:

  • test_hf_post_init_all_cpu_materializes_deferred_qzeros (CPU-only, synthetic): meta deferred qzeros + resident CPU weight -> reselect, rebuild, finite forward.
  • test_legacy_symmetric_checkpoint_loads_when_auto_resolves_to_cpu (CUDA): the real device_map="auto" + max_memory all-CPU load of the legacy fixture.
  • test_legacy_deferred_qzeros_on_cpu_offload_still_raises (CUDA): the offload refusal is preserved.

Suites (same set as #6253, plus test_hf_bridge_legacy_qzeros_marlin_6274.py):

  • CPU: 320 passed, 125 skipped
  • cuda:4 (RTX 4090): 402 passed, 43 skipped
  • ruff check on all changed files: clean

No merge; this is a follow-up against 3082b02d0.

Update: current main and the #6386 fixture fallout

Main advanced to 1615accf9 while this PR was open. A cherry-pick onto that head applies cleanly, and the focused suites pass there (CPU: 320 passed, 125 skipped; the three new nodes pass on cuda:4).

One test-only follow-up was needed: #6386 now rejects a checkpoint whose tokenizer cannot encode ordinary text, and the pre-existing test_hf_bridge_legacy_qzeros_marlin_6274.py fixture saved a 3-token WordLevel vocabulary. Every test in that file (including the two new nodes) failed at GPTQModel.load on current main. The fixture now uses the shared build_wordlevel_tokenizer, and the file passes on 1615accf9 (CPU 3 passed, 3 skipped; cuda:4 6 passed).

Note: current main also has 16 pre-existing GPU fixture errors from the same #6386 validation in test_hf_bridge_cpu_offload_5022.py and test_cpu_offload_tied_both_copies_4943.py (reproduced on clean 1615accf9 without this PR). They are unrelated to this change and are not addressed here.

agent added 3 commits November 29, 2026 13:16
…follow-up)

`_marlin_needs_offload_kernel` used `checkpoint_payload_is_meta()` as the
offload test. `_HF_DEFER_SYM_QZEROS` hides a legacy symmetric checkpoint's
`qzeros`, so an all-CPU Marlin layer has a meta `qzeros` while its packed
weight is resident. The bridge then kept Marlin and `post_init` raised
`ValueError: GPTQ Marlin requires CUDA tensors, got cpu` instead of
reselected the all-CPU load.

Use the primary packed weight (`runtime_device`) for the resident-CPU
decision, and rebuild the deferred `qzeros` on the replacement kernel when a
class swap left it as a meta parameter. The offloaded-layer refusal is
unchanged: a meta `qweight` still raises the documented RuntimeError.

Regression tests cover the synthetic bridge path, the all-CPU
`device_map="auto"` + `max_memory` load of a legacy checkpoint, and the
unchanged CPU-offload refusal.
…6386)

Main now rejects a checkpoint whose tokenizer cannot encode ordinary text.
The legacy-qzeros fixture saved a 3-token WordLevel vocabulary, so every test
in the file failed at GPTQModel.load after #6386. Use the shared word-level
tokenizer instead; the fixture is test-only.
@Qubitium
Qubitium merged commit c32f2b3 into main Nov 29, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/6152-legacy-cpu-reselect branch November 29, 2026 13:32
Qubitium pushed a commit that referenced this pull request Nov 29, 2026
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 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