Skip to content

fix(hf): pick the CPU AUTO kernel from the model dtype (#6169) - #6210

Merged
Qubitium merged 1 commit into
mainfrom
fix/6169-issue
Nov 29, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/6169-issue

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #6169.

hf_select_quant_linear / hf_select_quant_linear_v2 are the entry points Optimum (GPTQ) and Transformers (AWQ) call. Neither caller passes dtype, so CPU AUTO selection could not honour the kernels' SUPPORTS_DTYPES. On an AVX512-BF16 host that always returned TorchCpuAvx512Linear / TorchCpuAvx512AwqLinear, both of which declare SUPPORTS_DTYPES = [torch.bfloat16]. An FP16 GPTQ/AWQ checkpoint (the default for most published repos) therefore loaded on a BF16-only kernel and every forward took its per-call dense dequant fallback, ~7x slower than the ATen FP16 int4pack kernel that native GPTQModel.load picks for the same dtype.

What Changed

  • gptqmodel/utils/importer.py: both bridge entry points now resolve the model dtype when the caller passes none, and forward it to select_quant_linear, so AUTO applies SUPPORTS_DTYPES / auto_select_priority exactly like native load.
    • The dtype comes from torch.get_default_dtype(), which Transformers sets to the model dtype inside its local_torch_dtype window while it calls hf_quantizer.preprocess_model (transformers/modeling_utils.py, from_pretrained). The read takes model_global_state_lock() so a concurrent construction window cannot be observed ([BUG] GPTQModel.load fails when another thread runs a transformers construction (user from_pretrained or requantize(norm) teacher): class-wide no_tie_weights untie lm_head, unguarded default_dtype_before -> false 'corrupted state' #5421).
    • Scope: only CPU AUTO inference loads. Explicit backend= stays the caller's choice; pack=True can run outside the dtype window, where the settled default is not the model dtype; non-CPU placements keep their previous selection (unchanged per the issue's "keep other devices" requirement).
    • Only the two half dtypes are forwarded. FP32 is deliberately left on the previous ranking: TorchFusedLinear is the only CPU kernel that declares FP32, its availability is narrower than the half kernels', and the issue measured its FP32 path slower than the current fallback. A caller-set default such as float64 also keeps the previous selection instead of failing every SUPPORTS_DTYPES check.
  • tests/kernels/test_selection.py: 12 new nodes in the existing CPU AUTO selection family (plus a dtype fixture), covering v1 and v2, the real Transformers local_torch_dtype window, a real FP16 forward through the bridge-selected kernel, the BF16 control, the explicit-dtype control, the non-CPU control and the FP32/FP64 control.

Out of scope (unchanged): the FP32 CPU ranking noted in the issue, and AutoModelForCausalLM's own dtype resolution.

Tests

cd tests
PYTHONPATH=$PWD/..:$PWD /root/venv/bin/python -m pytest \
  kernels/test_selection.py test_hf_packing_api.py \
  test_hf_bridge_mixed_capability_5084.py test_hf_bridge_cpu_offload_5022.py \
  kernels/test_marlin_asymmetric_selection_5737.py test_cpu_kernel_constraints_4181.py \
  -q -rs
# 273 passed, 44 skipped

New nodes only:

cd tests
PYTHONPATH=$PWD/..:$PWD /root/venv/bin/python -m pytest kernels/test_selection.py -q \
  -k "6169 or keeps_bf16_kernel or explicit_dtype_wins or does_not_default_dtype_for_non_cpu or non_half_default_dtype"
# 12 passed

Lint: /root/venv/bin/ruff check gptqmodel/utils/importer.py tests/kernels/test_selection.py → All checks passed!

Red evidence (fix removed, tests kept; git checkout -- gptqmodel/utils/importer.py)

7 failed, 5 passed, 195 deselected
FAILED kernels/test_selection.py::test_hf_bridge_cpu_auto_resolves_default_dtype_6169[gptq-gptq-TorchCpuAvx512Linear]
FAILED kernels/test_selection.py::test_hf_bridge_cpu_auto_resolves_default_dtype_6169[awq-gemm-TorchCpuAvx512AwqLinear]
FAILED kernels/test_selection.py::test_hf_bridge_v1_cpu_auto_resolves_default_dtype_6169
FAILED kernels/test_selection.py::test_hf_bridge_selects_inside_transformers_dtype_window_6169[gptq-gptq]
FAILED kernels/test_selection.py::test_hf_bridge_selects_inside_transformers_dtype_window_6169[awq-gemm]
FAILED kernels/test_selection.py::test_hf_bridge_fp16_cpu_kernel_forwards_6169[gptq-gptq]
FAILED kernels/test_selection.py::test_hf_bridge_fp16_cpu_kernel_forwards_6169[awq-gemm]

The 5 passes are the intentional controls (BF16 keeps the AVX512 kernel, explicit dtype wins, non-CPU/FP32/FP64 unchanged). Representative failure, at HEAD:

>       assert selected.supports_dtype(torch.float16)
E       AssertionError: assert False
E        +  where False = supports_dtype(torch.float16)
E        +    where supports_dtype = <class 'gptqmodel.nn_modules.qlinear.torch_cpu_avx512.TorchCpuAvx512Linear'>.supports_dtype

i.e. the bridge handed an FP16 model to a kernel that declares itself BF16-only.

Green evidence (with the fix)

12 passed, 195 deselected      # new nodes
162 passed, 43 skipped         # kernels/test_selection.py
273 passed, 44 skipped         # affected families above

Skips

All skips are pre-existing and device/dependency related, none touch the new nodes:

  • kernels/test_selection.py (43): kernels whose device/runtime is absent on this host — MLX, TTNN, XLA, Machete, Swordfish, NVFP4, NF12 (no selector-compatible width), retired AWQ INT8.
  • test_hf_packing_api.py (1): CPU fallback path is only exercised without CUDA — this host has CUDA.
  • test_no_transformers_family_imports.py::test_config_model_and_tokenizer_import_without_vision_extras fails pre-existing in this environment (ModuleNotFoundError: No module named 'tiktoken'); confirmed by re-running it with this change reverted. It is unrelated to this diff and was excluded from the runs above.

Measured Impact

Not re-measured here: the issue's 34.7 s → 5.1 s forward comparison needs the published Qwen2.5-0.5B GPTQ/AWQ checkpoints, and this change was scoped to the CPU-only selector reproduction the issue describes. The PR proves the selection itself: an FP16 CPU AUTO load now resolves TorchAtenLinear / TorchAtenAwqLinear (both declare FP16), the same kernels native GPTQModel.load(dtype=torch.float16) picks, and an FP16 forward through the bridge-selected kernel is asserted. The remaining unverified item is the wall-clock speedup on a real checkpoint.

Hardware: x86-64 with AVX512 / AVX512-BF16 (avx512, avx512_bf16, avx512_vbmi, avx512_vnni), torch 2.15.0+cu132, transformers 5.23.0, Python 3.14, CPU-only run.

transformers/optimum never pass a dtype to the HF bridge, so CPU AUTO
selection could not honour the kernels' SUPPORTS_DTYPES. On an AVX512-BF16
host an FP16 GPTQ/AWQ checkpoint therefore loaded the BF16-only AVX512
kernel and every forward took its dense dequant fallback (~7x slower than
the ATen FP16 int4pack kernel AUTO picks when the dtype is known).

Both bridge entry points now resolve the model dtype from the process
default dtype that transformers sets inside its `local_torch_dtype`
window, and forward it for CPU AUTO inference loads. Explicit backends,
packing calls, non-CPU placements and non-half default dtypes keep the
previous selection.

Tests: tests/kernels/test_selection.py (12 new nodes, incl. a real FP16
forward through the bridge-selected kernel).
@Qubitium
Qubitium marked this pull request as ready for review November 29, 2026 01:31
@Qubitium
Qubitium merged commit e7e5d5d into main Nov 29, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/6169-issue branch November 29, 2026 01:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant