Repository navigation
fix(hf): pick the CPU AUTO kernel from the model dtype (#6169) - #6210
Merged
Merged
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #6169.
hf_select_quant_linear/hf_select_quant_linear_v2are the entry points Optimum (GPTQ) and Transformers (AWQ) call. Neither caller passesdtype, so CPU AUTO selection could not honour the kernels'SUPPORTS_DTYPES. On an AVX512-BF16 host that always returnedTorchCpuAvx512Linear/TorchCpuAvx512AwqLinear, both of which declareSUPPORTS_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 nativeGPTQModel.loadpicks 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 toselect_quant_linear, so AUTO appliesSUPPORTS_DTYPES/auto_select_priorityexactly like native load.torch.get_default_dtype(), which Transformers sets to the model dtype inside itslocal_torch_dtypewindow while it callshf_quantizer.preprocess_model(transformers/modeling_utils.py,from_pretrained). The read takesmodel_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).backend=stays the caller's choice;pack=Truecan 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).TorchFusedLinearis 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 asfloat64also keeps the previous selection instead of failing everySUPPORTS_DTYPEScheck.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 Transformerslocal_torch_dtypewindow, a real FP16 forward through the bridge-selected kernel, the BF16 control, the explicit-dtypecontrol, 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
New nodes only:
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)The 5 passes are the intentional controls (BF16 keeps the AVX512 kernel, explicit
dtypewins, non-CPU/FP32/FP64 unchanged). Representative failure, at HEAD:i.e. the bridge handed an FP16 model to a kernel that declares itself BF16-only.
Green evidence (with the fix)
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_extrasfails 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 nativeGPTQModel.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.