Skip to content

[BUG] device selection: on a GPU-less host with the default (CUDA) torch build, device_map="auto" resolves to the CUDA family - GPTQModel.load raises "No devices detected for accelerator family cuda" and HF from_pretrained picks Triton kernels that crash on CPU #6165

Done

Description

@Qubitium

Summary

On a machine without a GPU that runs the default Linux pip install torch wheel (a CUDA build), device_map="auto" cannot load any GPTQ or AWQ checkpoint:

  • GPTQModel.load(repo, device_map="auto") selects TritonV2Linear, logs Loader: device = DEVICE.CUDA, then raises RuntimeError: No devices detected for accelerator family cuda``.
  • AutoModelForCausalLM.from_pretrained(repo, device_map="auto") loads the model on CPU but with TritonV2Linear / AwqGEMMTritonLinear. The first forward then crashes (ValueError: Expected a cuda device, but got: cpu for GPTQ, RuntimeError: invalid argument to exchangeDevice for AWQ).

The same loads work with no placement argument, with device="cpu", or with device_map="cpu". In those cases AUTO picks TorchCpuAvx512Linear / TorchCpuAvx512AwqLinear and the output is correct. device_map="auto" is the standard HF placement argument, so it shows up in most notebooks, CI scripts and model cards. Every CPU-only user with a CUDA torch wheel (laptops, CI runners, CPU servers) hits this.

Root cause: normalize_device_device_map uses torch.accelerator.current_accelerator() to resolve an accelerate keyword map ("auto", "balanced", "sequential") to a device family. Its default is check_available=False, so it returns the accelerator the wheel was built with (cuda) even when torch.cuda.is_available() is False and device_count() is 0. The keyword therefore resolves to DEVICE.CUDA. Kernel selection then picks a CUDA-only kernel, and the native loader tries to build a layer-wise map over zero CUDA devices. auto_select_device (the no-placement path) checks HAS_CUDA and gets this right.

Introduced by

Pre-existing since db0cc2f (#2334, "Device check", Jan 2026), which added torch.accelerator.current_accelerator() here. #3072 (ef96caf) carried it into _accelerate_keyword_device. Still present at HEAD 8ebe255.

Location

gptqmodel/utils/importer.py:1160 (HEAD 8ebe255), in normalize_device_device_map:

    normalized_device: Optional[SelectorDevice] = default
    accelerator = torch.accelerator.current_accelerator()
    if device is None:
        if device_map is not None:
            if isinstance(device_map, str):
                if _is_accelerate_device_map_keyword(device_map):
                    return _accelerate_keyword_device(
                        accelerator, defer_placement=defer_keyword_placement
                    )

_accelerate_keyword_device (importer.py:1080) returns DEVICE.CPU only when accelerator is None. With a CUDA wheel and no GPU it returns DEVICE.CUDA (or the family when defer_placement=True, which is the HF bridge path). The native loader then fails at gptqmodel/models/loader.py:4011:

raise RuntimeError(f"No devices detected for accelerator family `{family.value}`")

Reproduction (ran it)

Host with no usable GPU: CUDA_VISIBLE_DEVICES="", torch 2.15.0+cu132, transformers 5.23, HEAD 8ebe255. A machine with no NVIDIA GPU and the default torch wheel behaves the same way.

import torch
print(torch.cuda.is_available(), torch.accelerator.current_accelerator())   # False cuda
from gptqmodel import GPTQModel
from transformers import AutoModelForCausalLM, AutoTokenizer

repo = "Qwen/Qwen2.5-0.5B-Instruct-GPTQ-Int4"   # same with Qwen/Qwen2.5-0.5B-Instruct-AWQ
tok = AutoTokenizer.from_pretrained(repo)
ids = tok("The capital of France is", return_tensors="pt")

GPTQModel.load(repo, device_map="auto")
# INFO  Kernel: selected -> `TritonV2Linear`.
# INFO  Loader: device = DEVICE.CUDA
# RuntimeError: No devices detected for accelerator family `cuda`

m = AutoModelForCausalLM.from_pretrained(repo, device_map="auto")
print(m.device, {type(x).__name__ for x in m.modules() if type(x).__module__.startswith("gptqmodel.nn")})
# cpu {'TritonV2Linear'}            (AWQ: cpu {'AwqGEMMTritonLinear'})
m.generate(**ids, max_new_tokens=8)
# GPTQ: gptqmodel/nn_modules/triton_utils/dequant.py:332 dequant -> torch.cuda.device(...)
#       ValueError: Expected a cuda device, but got: cpu
# AWQ:  RuntimeError: invalid argument to exchangeDevice

Controls on the same host: GPTQModel.load(repo), GPTQModel.load(repo, device="cpu"), GPTQModel.load(repo, device_map="cpu") and from_pretrained(repo, device_map="cpu") all select TorchCpuAvx512Linear and generate "The capital of France is Paris.".

To check the fix, I patched only the current_accelerator() call in gptqmodel.utils.importer to return None when no accelerator is available. With that change, all four device_map="auto" loads (native and HF, GPTQ and AWQ) select the AVX512 CPU kernels and generate correctly:

native {'TorchCpuAvx512Linear'}    'The capital of France is Paris. It has a population of '
hf     {'TorchCpuAvx512Linear'}    'The capital of France is Paris. It has a population of '
native {'TorchCpuAvx512AwqLinear'} 'The capital of France is Paris. The city has a population of'
hf     {'TorchCpuAvx512AwqLinear'} 'The capital of France is Paris. The city has a population of'

Expected vs actual

  • Expected: with no usable accelerator, device_map="auto" (and "balanced" / "sequential") resolves to CPU and selects a CPU kernel, as transformers/accelerate do for dense models.
  • Actual: it resolves to the wheel's build accelerator. The native load crashes, and the HF load produces a model that crashes on its first forward.

Suggested fix

In normalize_device_device_map, use torch.accelerator.current_accelerator(check_available=True), or treat the accelerator as None when torch.accelerator.device_count() == 0, so a keyword map on a host with no devices falls back to DEVICE.CPU. Add a CPU-only test (CUDA_VISIBLE_DEVICES="" on a CUDA wheel) for GPTQModel.load(..., device_map="auto") and from_pretrained(..., device_map="auto").

Related, but a different trigger: #6152, where a GPU host's device_map="auto" + max_memory resolves to all-CPU and the dropped HF reselect leaves Triton/Marlin kernels in place. Here no GPU exists at all, and the native GPTQModel.load path fails too.

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