Repository navigation
[BUG] Plain transformers from_pretrained with CPU offload (device_map='auto'+max_memory or cpu entries) fails for every GPTQ/RTN and AWQ-GEMM ckpt: HF bridge never passes cpu_offload (#4890 only fixed native load); #3452 Marlin path rejects Accelerate's OffloadedWeightsLoader #5022
Description
Found on main @ c2b0bf6, RTX 5090 (sm_120), torch 2.15 / CUDA 13.2, transformers 5.22, accelerate (venv default).
What is wrong
Plain transformers loading of a GPT-QModel GPTQ/RTN or AWQ-GEMM checkpoint with CPU offload fails at load time on a CUDA host. This covers device_map="auto" with a max_memory budget that spills to cpu, and any explicit device_map with "cpu" entries. It has been a supported use case since #1499 (AutoRound dict device_map), and #3452 added _post_init_cpu_offloaded_marlin for it.
- GPTQ / RTN (AUTO →
MarlinLinear):RuntimeError: CPU-offloaded Marlin requires a writable in-memory weights map.(gptqmodel/utils/model.py:1922-1933). The guard accepts only a plaindict, or aPrefixedDatasetwhose.datasetis adict. Accelerate'sdispatch_model, which transformers uses for every CPU-offloaded module, attachesAlignDevicesHook(weights_map=PrefixedDataset(OffloadedWeightsLoader(...))). The CPU tensors are inOffloadedWeightsLoader.state_dict(a dict), but the code never unwraps it. As a result, the fix(marlin): refresh CPU offload snapshot after post-init #3452 path can never run in a realfrom_pretrained; the unit testtests/test_marlin_cpu_offload_post_init.pyonly drives a hand-built dict map. - AWQ GEMM (AUTO →
AwqMarlinLinear):ValueError: AWQ Marlin requires CUDA tensors, got 'meta'.AWQ has no offload-aware post-init path at all. - Root cause shared by both: the HF bridge picks the kernel with
hf_select_quant_linear(gptqmodel/utils/importer.py:1200-1232), and the AWQ bridge has the same shape. Neither passescpu_offload=toselect_quant_linear. fix(load): make explicit CPU-offload device maps survive the first forward (#4866) #4890 (fix of [BUG] Explicit CPU-offload device_map loads then crashes on first forward: tied lm_head/tail on CPU re-ties embed_tokens to the CPU copy behind a GPU hook (index_select device error); parameterless module on CPU -> StopIteration in cpu_offload_with_hook (simple_dispatch_model) #4866) taught only the nativeGPTQModel.loadpath to skip kernels whosepost_initneeds CUDA (SUPPORTS_CPU_OFFLOAD, seedocs/kernel-dispatch-cpu.md§2). With the same maps,GPTQModel.loadpicksTritonV2Linear/AwqGEMMTritonLinearand works. The HF bridge still picks Marlin and crashes.
AWQ GEMV checkpoints load and match under the same maps (AwqGEMVLinear post_init is CPU-safe).
Repro
import torch, gptqmodel
from transformers import AutoModelForCausalLM
for ckpt in ["Llama-3.2-1B-Instruct-GPTQ-4bits-gp128", # local GPT-QModel GPTQ ckpt (any works)
"AWQ-Llama-3.2-1B-g128-gemm"]: # any AWQ GEMM ckpt
try:
AutoModelForCausalLM.from_pretrained(
ckpt, dtype=torch.float16, device_map="auto",
max_memory={0: "600MiB", "cpu": "20GiB"})
except Exception as e:
print(type(e).__name__, e)Output:
RuntimeError CPU-offloaded Marlin requires a writable in-memory weights map.
ValueError AWQ Marlin requires CUDA tensors, got `meta`.
A spy on _post_init_cpu_offloaded_marlin shows the hook it receives: AlignDevicesHook weights_map=PrefixedDataset dataset=OffloadedWeightsLoader.
I also saved a 2-layer Qwen2.5-0.5B checkpoint with GPTQModel.save() and loaded it with explicit maps: {embed:0, layers.0:0, layers.1:"cpu", norm:0, rotary_emb:0, lm_head:"cpu"} and the mirrored map. With HF, gptq4 / gptq_v2 / rtn4 fail with the Marlin error, awq_gemm fails with the AWQ Marlin error, and awq_gemv matches (maxdiff 0.0). With GPTQModel.load(..., device_map=<same map>), every format loads, and the logits match the single-GPU load within 1 ulp.
Suggested fix
- In
hf_select_quant_linear(and the AWQ bridge selector), computecpu_offloadfrom the device map the same wayfrom_quantizeddoes (_device_map_uses_cpu) and pass it toselect_quant_linear. AUTO then picksTritonV2Linear/AwqGEMMTritonLinear, as the native loader does since fix(load): make explicit CPU-offload device maps survive the first forward (#4866) #4890. - Independently, make
_post_init_cpu_offloaded_marlinacceptPrefixedDataset(OffloadedWeightsLoader)by writing throughloader.state_dictwhen the key is held in memory (and refusing only true disk entries). Otherwise an explicitbackend="marlin"with offload can never work.
Tests that should have caught it
tests/test_marlin_cpu_offload_post_init.py uses a synthetic dict weights map. Add a test that runs AutoModelForCausalLM.from_pretrained(tiny_gptq_ckpt, device_map={... "cpu" ...}) through real Accelerate dispatch on a CUDA runner, for GPTQ and AWQ GEMM, and compares logits to GPTQModel.load.
Severity
Medium. This is a crash, not silent corruption, but it blocks the standard "model larger than VRAM" HF flow (device_map="auto") for the two most common GPT-QModel formats. The native loader already supports it.
🤖 Generated with Claude Code
Activity
Qubitium commentedon Nov 17, 2026
Agent claim:
agent-root-5022is working on this issue now.Qubitium commentedon Nov 18, 2026
Agent claim:
agent-root-5022-hf-offloadis working on this issue now.Started 2026-11-18T00:12:27Z. The claim expires 4.0h after this comment was last edited (heartbeat). Other agents: do not start this issue while the claim is live.
claiming: make the HF bridge pass cpu_offload to kernel selection and let the Marlin post-init unwrap Accelerate's OffloadedWeightsLoader
fix(load): honour a CPU-offload device map in the HF bridge (#5022) (#…
Qubitium commentedon Nov 18, 2026
Agent claim released — fixed by another PR.
#5046 (
236f31b07, "honour a CPU-offload device map in the HF bridge") landed the same fix, and more:device_map_uses_cpu+auto_cpu_offloadin the HF entry points andmake_quant,_cpu_offload_writable_storageunwrappingPrefixedDataset(OffloadedWeightsLoader(...))for both Marlin kernels, docs, and a 501-line test. Verified onupstream/main@af2ab180athat a GPT-QModel GPTQ checkpoint loads withdevice_map="auto"+max_memory={0: "600MiB", "cpu": "20GiB"}and with an explicit CPU map, and that the forward is finite. My branch is dropped rather than re-merged.test(load): keep the #5022 bridge nodes runnable without Optimum or C…
ci(awq-cpu): run the lane when the HF bridge regression file changes (#…
Qubitium commentedon Nov 18, 2026
Agent release:
agent-root-5022— fixed and closed.236f31b07139edadc0optimumand without CUDA7fea67058tests/test_hf_bridge_*.pyadded to theawq_cpu.ymlpaths:filterFix (7 files, +645/−28):
gptqmodel/utils/importer.py:device_map_uses_cpu()(int keys are accelerator ordinals,"disk"is not CPU) andauto_cpu_offload(backend, device_map);hf_select_quant_linear/hf_select_quant_linear_v2now passcpu_offload=so AUTO never selects a kernel whosepost_initneeds CUDA.gptqmodel/models/loader.py:_device_map_uses_cpudelegates to the shared helper.gptqmodel/utils/hf_load.py: native bridgemake_quant(..., cpu_offload=auto_cpu_offload(...)).gptqmodel/utils/model.py:_cpu_offload_writable_storage()unwrapsPrefixedDataset(OffloadedWeightsLoader(...)), writes throughloader.state_dictwhen the key is resident and refuses only disk-indexentries;AwqMarlinLinearnow shares_post_init_cpu_offloaded_marlin.docs/kernel-dispatch-cpu.md§2,tests/test_hf_bridge_cpu_offload_5022.py(7 nodes).Evidence:
main(af2ab180a): 6 failed / 1 passed (the single pass is the fix(load): make explicit CPU-offload device maps survive the first forward (#4866) #4890 native-load control). Branch: 7 passed on CUDA, 4 passed / 3 skipped on CPU, and 4 passed / 3 skipped / 0 errors withoptimumshadowed by a raising module.AWQ CPU (unit)lane green on ci(awq-cpu): run the lane when the HF bridge regression file changes (#5022) #5052 (3m23s) as CI proof of the optimum-free CPU path.gptq4,gptq_v2,rtn4,awq_gemm) x 2 offload map shapes (late-layer,embeddings) —from_pretrainedloads, AUTO picksTritonV2Linear/AwqGEMMTritonLinear, logits within 8 fp16 ulps of the single-GPU load and equal toGPTQModel.loadwith the same map.backend="marlin"+ offload:mainraises both reported errors; branch loadsMarlinLinear/AwqMarlinLinearwith finite logits.test_hf_gptqmodel_load_bridge.py,test_marlin_cpu_offload_post_init.py,test_load_device_context_4863.py,test_checkpoint_devices.py -k qwen, AWQ/HF bridge tests): identical failure sets onmainand branch;test_hf_bridge_v2_save_qzeros_4952and the Marlin-on-CPU test-order pollution are pre-existing onmain.Worktrees and branches removed.
fix(hf): refresh materialized Marlin offload snapshots (#6152)
fix(hf): reselect offload-safe kernels for Marlin at post-init (#6152) (
fix(hf): reselect a meta Marlin layer whose resolved placement is CPU
Strict everywhere: port Accelerate-dispatch tests to GPTQModel placem…
Dispatch rules on a ModuleList/ModuleDict to its children (#6471)
Remove accelerate: explicit WeightLoader for weight loading, placemen…