Skip to content

[BUG] HF interop: regression from #4620 - from_pretrained(device_map="auto", max_memory=..., offload_folder=...) crashes again for GPTQ and AWQ (the #5194/#5262/#5395/#5579 reselect was dropped with the Transformers bridge) #6152

Done

Description

@Qubitium

Summary

Plain Transformers loading of a GPTQ or AWQ-GEMM checkpoint with device_map="auto" and a max_memory budget fails at load time again in two cases:

  • the budget spills layers to disk (offload_folder), or
  • the budget resolves the whole model to CPU.

This is the standard Transformers/Accelerate way to run a model that does not fit in VRAM. #5194, #5262, #5395 and #5579 fixed it. Their fix lived in gptqmodel/utils/hf_load.py and in the validate_environment wrappers installed from gptqmodel/__init__.py. That code re-selected an offload-capable kernel once Transformers had resolved "auto" into a placement. #4620 ("never patch Transformers") deleted both. Nothing replaced them, so AUTO picks Marlin again from the unresolved "auto" keyword:

  • Disk-backed layers hit the _post_init_cpu_offloaded_marlin refusal.
  • All-CPU layers hit the Marlin CUDA-tensor check.

A GPU+CPU spill with no disk entries still works, because the Marlin CPU-snapshot repack from #5022 handles it. Explicit dict maps that contain "disk" also still work, because auto_cpu_offload sees the placement and picks TritonV2Linear / AwqGEMMTritonLinear.

Released third-party checkpoints (Qwen/Qwen2.5-0.5B-Instruct-GPTQ-Int4, Qwen/Qwen2.5-0.5B-Instruct-AWQ) and a GPTQ checkpoint freshly quantized and saved by current GPT-QModel (4-bit, g128, sym) all fail the same way.

Introduced by

d2dfb93 "Port every supported model onto the Transformers 5.23 ABI; never patch Transformers (#4620)". It removed gptqmodel/utils/hf_load.py (including hf_gptqmodel_reselect_offloaded_kernels) and the GptqHfQuantizer / AwqQuantizer validate_environment hooks added by e247431 (#5247), 6b3c079 (#5277), 640107f (#5543) and #5657. The end-to-end device_map="auto" + max_memory tests in tests/test_hf_bridge_cpu_offload_5022.py were removed in the same commit. The file now asserts not device_map_offloads_layers("auto").

Verified on d2dfb9385^: the same scripts load, with {0: 13, 'cpu': 3, 'disk': 12} placements and TritonV2Linear/AwqGEMMTritonLinear on the offloaded layers, and generate the expected text.

Location

HEAD a311c91.

gptqmodel/utils/importer.py:1301-1331: a keyword map is never treated as offloading, and no later hook re-checks the choice:

def device_map_offloads_layers(device_map) -> bool:
    if not isinstance(device_map, dict):
        return False
...
def auto_cpu_offload(backend, device_map) -> bool:
    if backend not in (None, BACKEND.AUTO, BACKEND.AUTO_TRAINABLE):
        return False
    return device_map_offloads_layers(device_map)

gptqmodel/utils/model.py:2316-2325 (_post_init_cpu_offloaded_marlin), reached from optimum's post_init_model -> hf_gptqmodel_post_init:

        if disk_backed:
            raise RuntimeError(
                "CPU-offloaded Marlin requires a writable in-memory weights map: "
                ...

Reproduction

Ran on RTX 5090, torch 2.15, transformers 5.23.0, optimum 2.3, accelerate 1.15:

import sys, json, torch
from transformers import AutoModelForCausalLM, AutoTokenizer
qp = "Qwen/Qwen2.5-0.5B-Instruct-GPTQ-Int4"     # or Qwen/Qwen2.5-0.5B-Instruct-AWQ
for mm in ({0: "350MiB", "cpu": "30MiB"},        # GPU + CPU + disk spill
           {0: "100MiB", "cpu": "20GiB"}):       # resolves to all-CPU
    try:
        m = AutoModelForCausalLM.from_pretrained(qp, dtype=torch.float16, device_map="auto",
                                                 max_memory=mm, offload_folder="offload")
        print("ok", set(m.hf_device_map.values()))
    except Exception as e:
        print(type(e).__name__, str(e)[:160])

HEAD a311c91:

GPTQ, {0:350MiB,cpu:30MiB}: RuntimeError CPU-offloaded Marlin requires a writable in-memory weights map: ['qweight', 'g_idx', 'scales', 'qzeros'] are disk-backed entries that `post_init` cannot rewrite. Load with `offload_state_dict=False`, or select a backend ...
AWQ,  {0:350MiB,cpu:30MiB}: RuntimeError CPU-offloaded Marlin requires a writable in-memory weights map: ['qweight', 'qzeros', 'scales'] are disk-backed entries ...
GPTQ, {0:100MiB,cpu:20GiB}: ValueError GPTQ Marlin requires CUDA tensors, got `cpu`.
AWQ,  {0:150MiB,cpu:20GiB}: ValueError AWQ Marlin requires CUDA tensors, got `cpu`.

d2dfb9385^ (same environment):

GPTQ: placements {0: 13, 'cpu': 3, 'disk': 12} {'TritonV2Linear', 'MarlinLinear'}  GEN ' Paris. It has a population of '
AWQ:  placements {0: 13, 'cpu': 3, 'disk': 12} {'AwqGEMMTritonLinear', 'AwqMarlinLinear'}  GEN ' Paris. The city has a population of'
GPTQ {0:150MiB,cpu:20GiB} (resolves all-CPU; HEAD raises the Marlin CUDA error here too): loads (TorchAtenLinear), GEN ' Paris. It has a population of '

Control at HEAD: the equivalent explicit dict map (layers.0-11: 0, layers.12-23: "disk", plus offload_folder) loads with TritonV2Linear. Its logits match the all-GPU load (max |dlogit| 0.04, argmax agree 100%).

The error text is misleading as well. offload_state_dict is not a Transformers 5 from_pretrained option, and the user never selected Marlin: AUTO did.

Expected vs actual

Suggested fix

Restore the behaviour without patching Transformers. The bridge entry points Transformers/Optimum already call are enough:

  • In hf_gptqmodel_post_init / gptqmodel_post_init, when a Marlin module's resolved placement (the Accelerate hook / model.hf_device_map, which exists by then) is cpu or disk, swap the module in place for an offload-capable kernel from select_quant_linear(..., cpu_offload=True) before post_init, rather than raising.
  • Alternatively, make auto_cpu_offload treat an accelerate keyword map as possibly offloading. This is simpler, but it costs Marlin speed for keyword loads that fit on the GPU.

Restore the removed end-to-end tests: device_map="auto" + max_memory with CPU, disk and all-CPU spills, for GPTQ and AWQ-GEMM.

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