Skip to content

fix(load): honour a CPU-offload device map in the HF bridge (#5022) - #5046

Merged
Qubitium merged 2 commits into
mainfrom
root/hf-bridge-cpu-offload-5022
Nov 18, 2026
Merged

Qubitium merged 2 commits into
mainfrom
root/hf-bridge-cpu-offload-5022

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #5022. Plain transformers (and Optimum) loading of a GPT-QModel
GPTQ/RTN or AWQ-GEMM checkpoint with a device map that offloads layers to CPU
failed at load time on a CUDA host, although GPTQModel.load serves the same
maps since #4890:

RuntimeError: CPU-offloaded Marlin requires a writable in-memory weights map.   # gptq4 / gptq_v2 / rtn4
ValueError: AWQ Marlin requires CUDA tensors, got `meta`.                       # awq_gemm

Two independent causes:

  1. The bridge never told the selector about the offload.
    from_quantized derives cpu_offload from the map and passes it to
    select_quant_linear, so AUTO skips a kernel whose post_init needs CUDA
    (SUPPORTS_CPU_OFFLOAD, docs/kernel-dispatch-cpu.md §2). The three HF
    entry points did not, so AUTO selected MarlinLinear / AwqMarlinLinear
    for an offloading map.
  2. The CPU-offloaded Marlin repack could never run from a real
    from_pretrained.
    _post_init_cpu_offloaded_marlin accepted only a plain
    dict, or a PrefixedDataset whose .dataset is a dict. Transformers
    dispatches through Accelerate, which attaches
    AlignDevicesHook(weights_map=PrefixedDataset(OffloadedWeightsLoader(...))),
    so the fix(marlin): refresh CPU offload snapshot after post-init #3452 path was unreachable. AwqMarlinLinear had no offload-aware
    post-init at all.

What Changed

  • gptqmodel/utils/importer.py
    • device_map_uses_cpu(device_map) — one implementation of "does this map
      have a CPU-resident entry", used by both loaders. Ints are ordinals and
      "disk" is not a CPU placement, so neither counts.
    • auto_cpu_offload(backend, device_map) — narrows it to AUTO /
      AUTO_TRAINABLE (None included). An explicit backend="marlin" stays the
      caller's choice, because _post_init_cpu_offloaded_marlin can repack it.
    • hf_select_quant_linear and hf_select_quant_linear_v2 now pass
      cpu_offload=auto_cpu_offload(backend, device_map).
  • gptqmodel/models/loader.py: _device_map_uses_cpu delegates to the shared
    helper (same semantics; the existing
    test_device_map_uses_cpu_detects_only_real_cpu_entries still pins it).
  • gptqmodel/utils/hf_load.py: the native bridge's make_quant call passes the
    same cpu_offload.
  • gptqmodel/utils/model.py
    • new _cpu_offload_writable_storage(weights_map): unwraps
      PrefixedDataset, accepts a dict or an OffloadedWeightsLoader, and
      returns (storage, prefix, disk_index).
    • _post_init_cpu_offloaded_marlin writes through the loader's in-memory
      state_dict. A key that only exists in the loader's disk index still
      fails closed, with the original message plus the offending names.
    • the post-init loop treats AwqMarlinLinear like MarlinLinear, so AWQ's
      repack refreshes the offload snapshot instead of running on meta.
  • docs/kernel-dispatch-cpu.md §2: new "The Transformers/Optimum bridge
    derives the same contract" subsection (three selectors, the writable-map rule,
    the AWQ half).
  • tests/test_hf_bridge_cpu_offload_5022.py (new, 7 nodes) and the AWQ CPU
    unit lane now runs it.

Tests

tests/test_hf_bridge_cpu_offload_5022.py on unmodified origin/main
(b590f4332, test copied into a separate worktree): 6 failed, 1 passed.
The one pass is test_native_load_uses_the_same_offload_safe_kernel — the
#4890 reference behaviour the bridge had to match.

node on main
test_hf_from_pretrained_loads_an_offloaded_map gptq4/embeddings: AUTO selected MarlinLinear for a map with CPU entries; gptq4/late-layer: RuntimeError: CPU-offloaded Marlin requires a writable in-memory weights map.; awq_gemm/...: ValueError: AWQ Marlin requires CUDA tensors, got 'meta'.
test_auto_cpu_offload_only_applies_to_auto ImportError: cannot import name 'auto_cpu_offload'
test_prefixed_dataset_over_offloaded_weights_loader_is_writable ImportError: cannot import name '_cpu_offload_writable_storage'
test_offloaded_weights_loader_map_is_repacked_in_place[False] RuntimeError: CPU-offloaded Marlin requires a writable in-memory weights map.
test_offloaded_weights_loader_map_is_repacked_in_place[True] disk entry not reported as disk-backed
test_awq_marlin_uses_the_shared_offload_post_init AttributeError: 'AwqMarlinLinear' object has no attribute 'qweight'

On this branch: 7 passed on CUDA; 4 passed, 3 skipped with
CUDA_VISIBLE_DEVICES="" (the four CUDA nodes skip cleanly).

What the end-to-end node asserts, for all four checkpoint formats
(gptq4, gptq_v2, rtn4, awq_gemm) and both offloading map shapes
(late-layer: layers.1 + lm_head on CPU; embeddings:
model.embed_tokens on CPU):

  • the load succeeds and AUTO picks an offload-safe kernel
    (TritonV2Linear / AwqGEMMTritonLinear);
  • the logits match the single-GPU load within 8 fp16 ulps (measured 9.8e-4 on
    this fixture, one ulp = 8.4e-4 — the two loads use different kernels);
  • the HF bridge and the native loader produce the same logits for the same
    map and kernel (both TritonV2Linear).

Fix (b) is also verified end-to-end outside the test: with
quantization_config["backend"] = "marlin" and the late-layer map,
gptq4 → MarlinLinear and awq_gemm → AwqMarlinLinear both load and
produce finite logits here, while on main the same script raises the two
errors quoted above.

Neighbouring suites:

run origin/main this branch
test_marlin_cpu_offload_post_init.py 1 failed (disk entry), 4 passed 5 passed
test_hf_gptqmodel_load_bridge.py + test_marlin_cpu_offload_post_init.py + test_layerwise_device_placement.py + 4 HF/AWQ bridge files 9 failed, 78 passed, 2 skipped identical node set (9/78/2)
tests/test_docs_contract_refs.py — 23 passed
test_load_device_context_4863.py + test_checkpoint_devices.py — 12 passed, 17 skipped

The 9 shared failures are test-order pollution in that one combination
(ImportError: Marlin kernel requires CUDA after tests/kernels/... is
imported in the same process) and the four test_hf_bridge_v2_save_qzeros_4952
nodes, which fail identically when run alone on unmodified main. No delta.
ruff check clean on all changed Python files.

Model Support / Module Plan

No model or module-plan change. The fix is in kernel selection and the
CPU-offload post-init, so it applies to every GPTQ/RTN/AWQ checkpoint the bridge
can load.

Logic Flow Documentation

docs/kernel-dispatch-cpu.md §2 gains the HF-bridge half of the CPU-offload
contract: the three selectors that must derive cpu_offload, the AUTO-only
narrowing, the writable-map rule (PrefixedDataset(OffloadedWeightsLoader)),
and the shared AWQ Marlin post-init. The existing AUTO flowchart and
SUPPORTS_CPU_OFFLOAD table are unchanged and still describe the decision.

Notes

  • The AUTO narrowing is deliberate: backend="marlin" with an offloading map
    keeps working (through the unwrapped map), which is why the selector takes
    auto_cpu_offload rather than device_map_uses_cpu.
  • A disk-backed offload (offload_state_dict=True) still cannot be repacked in
    place; it now fails with a message that names the disk entries and points at
    offload_state_dict=False or an SUPPORTS_CPU_OFFLOAD kernel.
  • The regression test publishes a metadata-only gptqmodel-<version>.dist-info
    on sys.path instead of monkeypatching the availability probe, because both
    quantizers gate on importlib.metadata.version("gptqmodel") and this source
    checkout has no installed distribution.

Plain `transformers`/Optimum loading of a GPT-QModel GPTQ/RTN or AWQ-GEMM
checkpoint with a map that offloads layers to CPU failed at load time, although
`GPTQModel.load` serves the same maps since #4890:

* GPTQ/RTN: `RuntimeError: CPU-offloaded Marlin requires a writable in-memory
  weights map.`
* AWQ-GEMM: `ValueError: AWQ Marlin requires CUDA tensors, got 'meta'.`

Two independent causes.

1. The bridge never told the selector about the offload. `from_quantized`
   derives `cpu_offload` from the map and passes it to `select_quant_linear`,
   so AUTO skips a kernel whose `post_init` needs CUDA. The HF entry points did
   not: `hf_select_quant_linear`, `hf_select_quant_linear_v2` (Transformers'
   GPTQ quantizer, Optimum's `GPTQQuantizer.convert_model`, and AWQ's
   `replace_with_awq_linear`) and the native bridge's `make_quant` all selected
   `MarlinLinear`/`AwqMarlinLinear` for an offloading map. The check now lives
   in `utils.importer.device_map_uses_cpu` (the native loader's
   `_device_map_uses_cpu` delegates to it) and `auto_cpu_offload`, which
   narrows it to AUTO so an explicit `backend="marlin"` stays the caller's
   choice.

2. `_post_init_cpu_offloaded_marlin` accepted only a plain `dict` or a
   `PrefixedDataset` over a `dict`. Transformers dispatches through Accelerate,
   which attaches
   `AlignDevicesHook(weights_map=PrefixedDataset(OffloadedWeightsLoader(...)))`,
   so the #3452 path could never run from a real `from_pretrained`.
   `_cpu_offload_writable_storage` unwraps the prefix and writes through the
   loader's in-memory `state_dict`, refusing only entries that live in its disk
   `index` (same message, now naming the disk-backed keys). `AwqMarlinLinear`
   shares the post-init: it repacks into the Marlin layout too, and before this
   it ran `post_init` on `meta`.
…fload-5022

# Conflicts:
#	.github/workflows/awq_cpu.yml
@Qubitium
Qubitium merged commit 236f31b into main Nov 18, 2026
8 of 9 checks passed
@Qubitium
Qubitium deleted the root/hf-bridge-cpu-offload-5022 branch November 18, 2026 00:14
Qubitium added a commit that referenced this pull request Nov 18, 2026
…5022) (#5052)

The AWQ CPU `unit` lane lists `tests/test_hf_bridge_cpu_offload_5022.py` (added
by #5046, hardened by #5051), but the `paths:` filter had no entry for it. A PR
that only touched that file therefore never ran the lane that owns it, so the
#5051 fix could not be observed there.

Add the `tests/test_hf_bridge_*.py` pattern beside the other test-file patterns.
`gptqmodel/utils/importer.py` and `gptqmodel/utils/model.py` stay out on
purpose: both are touched by most PRs, and the lane already runs for
`gptqmodel/models/**` and `docs/**`.
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