Repository navigation
Description
Summary
TorchLinear._can_use_rocm_fused requires self.scales.dtype == x.dtype before it dispatches the fused GPTQ GEMV (M=1) and the small-M GEMM (M=2..64) on Ada/Blackwell/ROCm. TritonV2Linear delegates to the same gate (#3982/#3999).
In a real load those two dtypes differ for every BF16 model:
- GPTQ checkpoints always store FP16 scales (
_scales_for_storagecasts to FP16,nn_modules/qlinear/__init__.py:1612). - After Accelerate casts the buffers to the BF16 model dtype, the loader restores every narrowed quantization buffer to its checkpoint dtype (
utils/model.py:4449-4470,_load_narrowed(fp16, bf16) == True).
So a BF16 model (the auto dtype for Qwen2.5/3, Llama-3 and most current configs) runs with FP16 scales and BF16 activations. The fused paths are never taken. Decode and small-batch then fall back to the full dequantize + GEMM. That fallback also makes an extra full-weight cast, because the Triton decode emits the scale dtype (FP16) and _forward_eager then calls weights.to(dtype=x.dtype).
The fused kernels do not need the scale dtype to match. Both load scales and immediately do .to(tl.float32) (gptq_gemv.py and gptq_gemm.py), and they only use x_ptr.dtype.element_ty for the activation-side rounding. The kernel tests never hit this case, because they build the module with .to(device="cuda", dtype=dtype), which makes the scales BF16 too (tests/kernels/test_gptq_ada_gemv.py:95).
Impact
- Post-quant speed: high for BF16 GPTQ models on
BACKEND.TORCH/BACKEND.GPTQ_TRITONon Ada, Blackwell and CDNA4.- The module docs measure the fused paths at 1.5x-2.9x (gfx950 GEMV), 1.11x-1.82x (Ada BF16 small-M GEMM) and 4-5x (TritonV2 decode, [PERF] GPTQ Triton decode/small-batch: TritonV2 bypasses the fused GEMV (4-5x at M=1); small-M GEMM gate excludes all 7B+ shapes (2.4-5.8x at M=2..64) #3982) over dequantize + matmul.
- BF16 models get none of that speedup, and pay one more full-weight FP16→BF16 cast pass per forward (2·K·N bytes written and read again) on the dense path.
- RDNA4 is unaffected because BF16 is deliberately excluded there via
gemm_fused_dtypes. That exclusion is separate from this gate. - Accuracy is unaffected.
Location (@ 85caf6c)
gptqmodel/nn_modules/qlinear/torch.py:714-743(_can_use_rocm_fused):
and not x.requires_grad
and self.scales.dtype == x.dtype # <- false for every BF16 model with FP16 checkpoint scales
...
and self._decoded_weight_fits(x.dtype)gptqmodel/nn_modules/qlinear/torch.py:614:weights = weights.to(device=x.device, dtype=x.dtype). This is the per-forward full-weight cast on the fallback. The Triton decode returnsself._decode_dtype(), i.e. FP16 (torch.py:1166).gptqmodel/nn_modules/qlinear/tritonv2.py:175-180: delegates to the same gate.
Mechanism
The gate is stricter than what the kernels require. Combined with the loader's (correct) restore of FP16 checkpoint scales, the fused paths become unreachable for BF16 models.
Reproduction / Validation
audit_kernels/repro_fused_gate_bf16.py, executed on CPU. The device and arch predicates (_arch_is_cuda_fused, _rocm_arch_is_fused, _can_use_triton_dequant) are monkeypatched to True so that only the dtype predicate differs. The module is a 2048x2048 4-bit GPTQ v2 layer.
fp16 checkpoint scale restored after a bf16 load: True
scales=torch.float16 x=torch.float16: fused GEMV eligible = True
scales=torch.float16 x=torch.bfloat16: fused GEMV eligible = False
scales=torch.bfloat16 x=torch.bfloat16: fused GEMV eligible = True
The end-to-end speed impact was not executed: requires an Ada/Blackwell/ROCm GPU.
Suggested fix
Relax the predicate to what the kernels support, and keep the range check:
and self.scales.dtype in (torch.float16, torch.bfloat16)
and self._decoded_weight_fits(x.dtype)Then add GEMV/GEMM tests that load FP16 scales with BF16 activations against the FP64 oracle. Today's tests cast the scales along with the module. For the dense fallback, ask the Triton decoder for x.dtype directly when _decoded_weight_fits(x.dtype) holds, instead of decoding to FP16 and casting the whole weight.
Notes
- Not verified on hardware. Parity of the kernels with FP16 scales and BF16 activations is inferred from the kernel source (scales are loaded and widened to FP32 before use) and should be confirmed by the suggested test.
Found by automated code audit (Claude Code).
Activity
Qubitium commentedon Nov 20, 2026
Agent claim released:
agent-4e3a0639ecaffinished this issue.Pull request: #5921
Unblocking the fused GPTQ GEMV/small-M GEMM gate for BF16 models
fix(gptq): fuse BF16 GPTQ decode with FP16 scales (#5921)
fix: close the #5978 follow-ups (AWQ scale search, dtype/eval round t…