Repository navigation
fix(load): honour a CPU-offload device map in the HF bridge (#5022) - #5046
Merged
Merged
Conversation
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
This was referenced Nov 18, 2026
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/**`.
This was referenced Nov 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #5022. Plain
transformers(and Optimum) loading of a GPT-QModelGPTQ/RTN or AWQ-GEMM checkpoint with a device map that offloads layers to CPU
failed at load time on a CUDA host, although
GPTQModel.loadserves the samemaps since #4890:
Two independent causes:
from_quantizedderivescpu_offloadfrom the map and passes it toselect_quant_linear, so AUTO skips a kernel whosepost_initneeds CUDA(
SUPPORTS_CPU_OFFLOAD,docs/kernel-dispatch-cpu.md§2). The three HFentry points did not, so AUTO selected
MarlinLinear/AwqMarlinLinearfor an offloading map.
from_pretrained._post_init_cpu_offloaded_marlinaccepted only a plaindict, or aPrefixedDatasetwhose.datasetis adict. Transformersdispatches through Accelerate, which attaches
AlignDevicesHook(weights_map=PrefixedDataset(OffloadedWeightsLoader(...))),so the fix(marlin): refresh CPU offload snapshot after post-init #3452 path was unreachable.
AwqMarlinLinearhad no offload-awarepost-init at all.
What Changed
gptqmodel/utils/importer.pydevice_map_uses_cpu(device_map)— one implementation of "does this maphave 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 (
Noneincluded). An explicitbackend="marlin"stays thecaller's choice, because
_post_init_cpu_offloaded_marlincan repack it.hf_select_quant_linearandhf_select_quant_linear_v2now passcpu_offload=auto_cpu_offload(backend, device_map).gptqmodel/models/loader.py:_device_map_uses_cpudelegates to the sharedhelper (same semantics; the existing
test_device_map_uses_cpu_detects_only_real_cpu_entriesstill pins it).gptqmodel/utils/hf_load.py: the native bridge'smake_quantcall passes thesame
cpu_offload.gptqmodel/utils/model.py_cpu_offload_writable_storage(weights_map): unwrapsPrefixedDataset, accepts adictor anOffloadedWeightsLoader, andreturns
(storage, prefix, disk_index)._post_init_cpu_offloaded_marlinwrites through the loader's in-memorystate_dict. A key that only exists in the loader's diskindexstillfails closed, with the original message plus the offending names.
AwqMarlinLinearlikeMarlinLinear, so AWQ'srepack refreshes the offload snapshot instead of running on
meta.docs/kernel-dispatch-cpu.md§2: new "The Transformers/Optimum bridgederives 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 CPUunitlane now runs it.Tests
tests/test_hf_bridge_cpu_offload_5022.pyon unmodifiedorigin/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.
maintest_hf_from_pretrained_loads_an_offloaded_mapgptq4/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_autoImportError: cannot import name 'auto_cpu_offload'test_prefixed_dataset_over_offloaded_weights_loader_is_writableImportError: 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]test_awq_marlin_uses_the_shared_offload_post_initAttributeError: '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_headon CPU;embeddings:model.embed_tokenson CPU):(
TritonV2Linear/AwqGEMMTritonLinear);this fixture, one ulp = 8.4e-4 — the two loads use different kernels);
map and kernel (both
TritonV2Linear).Fix (b) is also verified end-to-end outside the test: with
quantization_config["backend"] = "marlin"and thelate-layermap,gptq4→MarlinLinearandawq_gemm→AwqMarlinLinearboth load andproduce finite logits here, while on
mainthe same script raises the twoerrors quoted above.
Neighbouring suites:
origin/maintest_marlin_cpu_offload_post_init.pytest_hf_gptqmodel_load_bridge.py+test_marlin_cpu_offload_post_init.py+test_layerwise_device_placement.py+ 4 HF/AWQ bridge filestests/test_docs_contract_refs.pytest_load_device_context_4863.py+test_checkpoint_devices.pyThe 9 shared failures are test-order pollution in that one combination
(
ImportError: Marlin kernel requires CUDAaftertests/kernels/...isimported in the same process) and the four
test_hf_bridge_v2_save_qzeros_4952nodes, which fail identically when run alone on unmodified
main. No delta.ruff checkclean 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-offloadcontract: the three selectors that must derive
cpu_offload, the AUTO-onlynarrowing, the writable-map rule (
PrefixedDataset(OffloadedWeightsLoader)),and the shared AWQ Marlin post-init. The existing AUTO flowchart and
SUPPORTS_CPU_OFFLOADtable are unchanged and still describe the decision.Notes
backend="marlin"with an offloading mapkeeps working (through the unwrapped map), which is why the selector takes
auto_cpu_offloadrather thandevice_map_uses_cpu.offload_state_dict=True) still cannot be repacked inplace; it now fails with a message that names the disk entries and points at
offload_state_dict=Falseor anSUPPORTS_CPU_OFFLOADkernel.gptqmodel-<version>.dist-infoon
sys.pathinstead of monkeypatching the availability probe, because bothquantizers gate on
importlib.metadata.version("gptqmodel")and this sourcecheckout has no installed distribution.