Repository navigation
Description
Found on main @ 12c1273, RTX 5090 (sm_120), torch 2.15 + CUDA 13.2.
What is wrong
#4594 (fix for #4545) brought back the native low-precision GEMM with FP32 reduction for bias-free FP8 layers. Layers that have a bias still always take the slow path: they dequantize the whole weight to FP32 and run an FP32 SGEMM.
gptqmodel/nn_modules/qlinear/fp8.py:740-757 TorchFP8Linear._forward_dequant_matmul:
if x_flat.dtype in (torch.float16, torch.bfloat16) and (
x_flat.device.type != "cuda"
or autocast_active(x_flat.device.type)
or (bias is not None and bias.dtype != x_flat.dtype) # <- always true for a biased FP8 layer
or not self._weights_fit_activation_dtype(x_flat.dtype)
):
weight = self.dequantize_weight(device=x_flat.device, dtype=torch.float32)
output = torch.matmul(x_flat.float(), weight)FP8 checkpoints store the bias in FP32, and since #4613 the in-session model does too, so bias.dtype != x_flat.dtype holds for every biased layer under FP16/BF16 activations. On CUDA the fallback isn't needed: the fast-path helper matmul_fp32_reduce (_numerics.py:120-160) already keeps a wide bias wide. It does torch.addmm(bias.float(), x, w, out_dtype=torch.float32) with FP16/BF16 tensor-core operands and rounds once. The range check _weights_fit_activation_dtype is a separate condition and stays in place.
Affected: every biased linear, for example q/k/v in Qwen2/Qwen2.5/Qwen1.5, and all linears in GPT-2/OPT/Bloom/Phi-2/StarCoder-style models.
Measurements
Module level, TorchFP8Linear with an FP32 bias (values x300, so they are not representable in FP16/BF16), M=2048. Current main vs. the same forward with only the bias.dtype != x.dtype clause removed on CUDA. Error is max |y - FP64 oracle| after rounding to the output dtype:
| K x N | dtype | main | native GEMM + FP32 bias epilogue | speedup | error main / patched |
|---|---|---|---|---|---|
| 3584x3584 | bf16 | 0.979 ms | 0.394 ms | 2.49x | 0.0312 / 0.0312 |
| 3584x3584 | fp16 | 0.995 ms | 0.403 ms | 2.47x | 0.00391 / 0.00391 |
| 3584x512 | fp16 | 0.336 ms | 0.070 ms | 4.78x | 0.00391 / 0.00391 |
| 4096x14336 | bf16 | 5.044 ms | 2.473 ms | 2.04x | 0.0312 / 0.0312 |
| 4096x14336 | fp16 | 5.112 ms | 2.541 ms | 2.01x | 0.00391 / 0.00391 |
(3584x512 bf16: 0.186 vs 0.069 ms, 2.68x; error 0.0156 / 0.0312, which is one output ulp either way.) At decode (M=1) the two paths are within noise.
Model level, FP8Config() quantize -> save -> reload (AUTO -> TorchFP8Linear, bias stored FP32), prefill 1x2048 in BF16, mean of 10:
| model | biased FP8 linears | main | patched | |
|---|---|---|---|---|
| Qwen1.5-7B (q/k/v biased, MHA) | 96 / 224 | 375.7 ms | 284.6 ms | +32% prefill on main |
| Qwen2.5-0.5B-Instruct (GQA, small k/v) | 72 / 168 | 22.0 ms | 21.3 ms | +3% |
Repro (module level)
import time, torch
from gptqmodel.nn_modules.qlinear import fp8 as F
from gptqmodel.nn_modules.qlinear._numerics import matmul_fp32_reduce
TorchFP8Linear, orig = F.TorchFP8Linear, F.TorchFP8Linear._forward_dequant_matmul
def patched(self, x, bias=None): # main minus the `bias.dtype != x.dtype` clause, CUDA only
if x.dtype in (torch.float16, torch.bfloat16) and x.is_cuda and not F.autocast_active("cuda") \
and self._weights_fit_activation_dtype(x.dtype):
return matmul_fp32_reduce(x, self.dequantize_weight(device=x.device, dtype=x.dtype), bias=bias)
return orig(self, x, bias)
def bench(f, n=50):
for _ in range(5): f()
torch.cuda.synchronize(); t = time.perf_counter()
for _ in range(n): f()
torch.cuda.synchronize(); return (time.perf_counter() - t) / n * 1e3
K, N = 3584, 3584
lin = torch.nn.Linear(K, N, bias=True); lin.bias.data *= 300
m = TorchFP8Linear(bits=8, group_size=-1, sym=True, desc_act=False, in_features=K, out_features=N, bias=True, register_buffers=False)
m.pack_original(lin, None, None); m = m.cuda().eval()
W = m.dequantize_weight(device="cuda", dtype=torch.float64)
x = torch.randn(2048, K, device="cuda", dtype=torch.bfloat16)
oracle = (x.double() @ W + m.bias.double()).bfloat16().double()
with torch.inference_mode():
for name, fn in (("main", orig), ("patched", patched)):
TorchFP8Linear._forward_dequant_matmul = fn
print(name, f"{bench(lambda: m(x)):.3f} ms", "err", (m(x).double() - oracle).abs().max().item())Suggested fix
On CUDA, drop the bias is not None and bias.dtype != x_flat.dtype clause. matmul_fp32_reduce already provides the FP32 epilogue for a wide bias. Keep the FP32 fallback for CPU/HIP, for autocast, and for weights outside the activation range, which is what #4594 checks. An alternative is the gate used by FP4/VPTQ (bias_narrows_losslessly), but that only helps biases that happen to be exactly representable.
Test that should have caught it
#4594 added a perf/selection check only for bias=False. Add the bias=True (FP32-stored) case, asserting that the CUDA forward does not materialise an FP32 weight (for example by spying on dequantize_weight(dtype=torch.float32)) and that it matches the FP64 oracle.
Severity
Medium [PERF]: 2-4.8x slower prefill GEMMs on every biased FP8 layer, +32% end-to-end prefill on Qwen1.5-7B, with no accuracy benefit.
🤖 Generated with Claude Code
Activity
Qubitium commentedon Nov 17, 2026
Taking this.
matmul_fp32_reducealready keeps a bias stored wider than the activations in its FP32 epilogue, sobias.dtype != x.dtypeshould not force the FP32 dequantize path; I'll make the guard bias-independent (keeping the weight-range guard) and cover the decision on CPU.perf(fp8): let a wide bias use the native FP8 GEMM path (#4681) (#4716)