Skip to content

[BUG] HF bridge: CPU kernel selection ignores the model dtype - from_pretrained(..., device_map="cpu") of FP16 GPTQ/AWQ checkpoints picks the BF16-only AVX512 kernel and runs its per-call dequant fallback (~7x slower than the ATen FP16 kernel AUTO is meant to pick) #6169

Done

Description

@Qubitium

Summary

hf_select_quant_linear_v2 is the entry point Optimum (GPTQ) and Transformers (AWQ) call. It accepts dtype=None, and neither caller passes a dtype. AUTO selection on CPU therefore never sees the activation dtype. On an AVX512-BF16 host it always returns TorchCpuAvx512Linear / TorchCpuAvx512AwqLinear, both of which declare SUPPORTS_DTYPES = [torch.bfloat16].

Most published GPTQ/AWQ checkpoints store torch_dtype: float16, and transformers 5 uses that dtype by default. So a plain AutoModelForCausalLM.from_pretrained(repo, device_map="cpu") gives an FP16 model on the BF16-only kernel. Every forward then takes the kernel's dense dequant fallback. The output is still correct, but a 128-token forward on Qwen2.5-0.5B takes about 34 s. The kernel AUTO picks for FP16 in native GPTQModel.load (TorchAtenLinear / TorchAtenAwqLinear) takes about 5 s. The kernel's own comment says FP16 is not meant to land on it:

    # The ported HF kernel is BF16-only: its public entrypoint and SIMD core
    # both consume/produce BF16. FP16 checkpoints use the native ATen FP16
    # int4pack backend instead.
    SUPPORTS_DTYPES = [torch.bfloat16]

Native GPTQModel.load(repo, device="cpu", dtype=torch.float16) passes the dtype and correctly picks TorchAtenLinear. Only the HF interop path is affected, but that is the default path for CPU users of plain Transformers/PEFT.

Introduced by

Pre-existing since d3c03fa (#3408), which made the BF16-only AVX512 kernels the top CPU AUTO choice. Before that, AUTO's CPU pick was TorchAtenLinear, which supports FP16. The HF selector has never had a dtype for this decision, and I see the same behavior at d2dfb93^ (before #4620). Still present at HEAD 8ebe255.

Location

gptqmodel/utils/importer.py:1371 (HEAD 8ebe255), hf_select_quant_linear_v2(..., dtype: Optional[...] = None, ...), then normalized_dtype = _normalize_dtype(dtype, "dtype") at :1412. With None, select_quant_linear cannot filter on SUPPORTS_DTYPES. The callers are optimum/gptq/quantizer.py select_quant_linear() and transformers/integrations/awq.py replace_with_awq_linear(), and neither passes dtype. TorchCpuAvx512Linear.SUPPORTS_DTYPES = [torch.bfloat16] (gptqmodel/nn_modules/qlinear/torch_cpu_avx512.py:183), and the same holds for torch_cpu_avx512_awq.py:67.

Reproduction (ran it; AVX512-BF16 CPU, CUDA_VISIBLE_DEVICES="", torch 2.15, transformers 5.23, optimum, HEAD 8ebe255)

import time, torch, gptqmodel
from transformers import AutoModelForCausalLM
x = torch.randint(0, 1000, (1, 128))
for repo in ["Qwen/Qwen2.5-0.5B-Instruct-GPTQ-Int4", "Qwen/Qwen2.5-0.5B-Instruct-AWQ"]:
    m = AutoModelForCausalLM.from_pretrained(repo, device_map="cpu")      # default dtype -> float16
    print(repo, m.dtype, {type(q).__name__ for q in m.modules() if type(q).__module__.startswith("gptqmodel.nn")})
    with torch.inference_mode():
        m(x); t = time.time(); m(x); print("fwd128 s", round(time.time() - t, 2))

Output:

Qwen/Qwen2.5-0.5B-Instruct-GPTQ-Int4 torch.float16 {'TorchCpuAvx512Linear'}      fwd128 s 34.7
Qwen/Qwen2.5-0.5B-Instruct-AWQ       torch.float16 {'TorchCpuAvx512AwqLinear'}   fwd128 s 35.3

For reference on the same host:

  • Same checkpoint with dtype=torch.bfloat16: TorchCpuAvx512Linear, fwd128 0.15 s.
  • Native GPTQModel.load(repo, device="cpu", dtype=torch.float16) picks TorchAtenLinear: 21 s for a 512-token forward, about 5 s for 128 tokens.
  • Dense Qwen/Qwen2.5-0.5B-Instruct in FP16: 2.4 s for a 512-token forward.

When I wrapped hf_select_quant_linear_v2, I found that torch.get_default_dtype() already equals the requested model dtype when Transformers calls it (float16 / bfloat16 / float32 for dtype=None / bf16 / fp32). I then forwarded that value as dtype, and the FP16 loads picked the intended kernels with identical text:

GPTQ fp16 {'TorchAtenLinear'}     fwd128 s 5.07  'The capital of France is Paris. It has a population of '
AWQ  fp16 {'TorchAtenAwqLinear'}  fwd128 s 5.39  'The capital of France is Paris. The city has a population of'

Expected vs actual

  • Expected: the HF bridge respects SUPPORTS_DTYPES the same way native load does. An FP16 model on CPU gets the FP16 int4pack kernel (TorchAtenLinear / TorchAtenAwqLinear).
  • Actual: it gets a kernel that declares itself BF16-only, and every linear call goes through the per-call dense dequant fallback. The default from_pretrained(..., device_map="cpu") of an FP16 GPTQ/AWQ checkpoint is about 7x slower than the intended kernel and about 230x slower than the same model in BF16.

Suggested fix

In hf_select_quant_linear_v2 (and hf_select_quant_linear), when dtype is None, fall back to torch.get_default_dtype(). Transformers sets that to the model dtype while it builds the model and calls the selector. Alternatively, re-check SUPPORTS_DTYPES after load and swap in a kernel that supports the dtype, as the native loader effectively does. Note: with FP32, the dtype-aware choice is TorchFusedLinear (fwd128 5.0 s), which on this host is slower than the AVX512 FP32 fallback (2.7 s). So the FP32 ranking may need its own look, but FP16 is the default case and has a clear win.

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