Skip to content

[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

Closed

Description

@Qubitium

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.

  1. 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 plain dict, or a PrefixedDataset whose .dataset is a dict. Accelerate's dispatch_model, which transformers uses for every CPU-offloaded module, attaches AlignDevicesHook(weights_map=PrefixedDataset(OffloadedWeightsLoader(...))). The CPU tensors are in OffloadedWeightsLoader.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 real from_pretrained; the unit test tests/test_marlin_cpu_offload_post_init.py only drives a hand-built dict map.
  2. AWQ GEMM (AUTO → AwqMarlinLinear): ValueError: AWQ Marlin requires CUDA tensors, got 'meta'. AWQ has no offload-aware post-init path at all.
  3. 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 passes cpu_offload= to select_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 native GPTQModel.load path to skip kernels whose post_init needs CUDA (SUPPORTS_CPU_OFFLOAD, see docs/kernel-dispatch-cpu.md §2). With the same maps, GPTQModel.load picks TritonV2Linear / AwqGEMMTritonLinear and 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), compute cpu_offload from the device map the same way from_quantized does (_device_map_uses_cpu) and pass it to select_quant_linear. AUTO then picks TritonV2Linear / 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_marlin accept PrefixedDataset(OffloadedWeightsLoader) by writing through loader.state_dict when the key is held in memory (and refusing only true disk entries). Otherwise an explicit backend="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

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-5022 is working on this issue now.

  2. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-5022-hf-offload is 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

  3. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    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_offload in the HF entry points and make_quant, _cpu_offload_writable_storage unwrapping PrefixedDataset(OffloadedWeightsLoader(...)) for both Marlin kernels, docs, and a 501-line test. Verified on upstream/main @ af2ab180a that a GPT-QModel GPTQ checkpoint loads with device_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.

  4. added 2 commits that reference this issue on Nov 18, 2026
  5. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent release: agent-root-5022 — fixed and closed.

    PR Squash SHA What
    #5046 236f31b07 Product fix: HF bridge honours a CPU-offload device map (issue closed by this PR)
    #5051 139edadc0 Test hardening: the new regression nodes run without optimum and without CUDA
    #5052 7fea67058 CI: tests/test_hf_bridge_*.py added to the awq_cpu.yml paths: filter

    Fix (7 files, +645/−28):

    • gptqmodel/utils/importer.py: device_map_uses_cpu() (int keys are accelerator ordinals, "disk" is not CPU) and auto_cpu_offload(backend, device_map); hf_select_quant_linear / hf_select_quant_linear_v2 now pass cpu_offload= so AUTO never selects a kernel whose post_init needs CUDA.
    • gptqmodel/models/loader.py: _device_map_uses_cpu delegates to the shared helper.
    • gptqmodel/utils/hf_load.py: native bridge make_quant(..., cpu_offload=auto_cpu_offload(...)).
    • gptqmodel/utils/model.py: _cpu_offload_writable_storage() unwraps PrefixedDataset(OffloadedWeightsLoader(...)), writes through loader.state_dict when the key is resident and refuses only disk-index entries; AwqMarlinLinear now shares _post_init_cpu_offloaded_marlin.
    • docs/kernel-dispatch-cpu.md §2, tests/test_hf_bridge_cpu_offload_5022.py (7 nodes).

    Evidence:

    • New test on unmodified 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 with optimum shadowed 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.
    • End-to-end: 4 formats (gptq4, gptq_v2, rtn4, awq_gemm) x 2 offload map shapes (late-layer, embeddings) — from_pretrained loads, AUTO picks TritonV2Linear/AwqGEMMTritonLinear, logits within 8 fp16 ulps of the single-GPU load and equal to GPTQModel.load with the same map.
    • Explicit backend="marlin" + offload: main raises both reported errors; branch loads MarlinLinear/AwqMarlinLinear with finite logits.
    • Neighbour suites (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 on main and branch; test_hf_bridge_v2_save_qzeros_4952 and the Marlin-on-CPU test-order pollution are pre-existing on main.

    Worktrees and branches removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions