Repository navigation
Description
Found on main @ 4b65923, RTX 4090 (sm89), torch 2.15 + CUDA 13.2.
What is wrong
PR #4533 (fix for #4471) changed TorchFP8Linear._forward_dequant_matmul (gptqmodel/nn_modules/qlinear/fp8.py:694-717) so that the full-FP32 path is taken whenever bias is None:
if x_flat.dtype in (torch.float16, torch.bfloat16) and (
bias is None # <- new
or x_flat.device.type != "cuda"
...
):
weight = self.dequantize_weight(device=x_flat.device, dtype=torch.float32)
output = torch.matmul(x_flat.float(), weight)The default FP8 config is weight-only (activation_quant_method is not per_token), so _scaled_mm is never used and every forward goes through _forward_dequant_matmul. Almost every linear in Llama/Qwen3/Mistral/... has no bias, so on CUDA every FP16/BF16 forward now runs an FP32 SGEMM on an FP32 copy of the weight, instead of the previous FP16/BF16 tensor-core GEMM with FP32 accumulate (matmul_fp32_reduce). Output accuracy is the same: matmul_fp32_reduce already accumulates and rounds in FP32. The PR fixes a corner case (decoded weights above 65504 under FP16), but in doing so it makes the common case 2x slower.
The widening is also unnecessary for BF16. BF16 has the FP32 exponent range, so decoded FP8 weights cannot overflow when narrowed to BF16. BF16 still pays the full cost.
Measurements (same box, same saved checkpoint, b25a6123e = parent of #4533 vs 4b6592381)
Llama-3.2-1B-Instruct, FP8Config() (default), saved and reloaded with BACKEND.AUTO -> TorchFP8Linear:
| before #4533 | after #4533 | |
|---|---|---|
| prefill 1x2048, fp16 | 52.6 ms | 114.3 ms (2.2x) |
| prefill 1x2048, bf16 | 54.8 ms | 110.1 ms (2.0x) |
| greedy decode, fp16 | 49.2 tok/s | 41.5 tok/s (-16%) |
| greedy decode, bf16 | 49.2 tok/s | 42.0 tok/s (-15%) |
Module-level, a single bias-free TorchFP8Linear (row scale), forward time:
| K x N, M | before | after |
|---|---|---|
| 4096x4096, M=512, fp16 | 0.390 ms | 0.656 ms |
| 4096x4096, M=4096, fp16 | 1.061 ms | 2.913 ms |
| 4096x14336, M=4096, fp16 | 4.108 ms | 10.353 ms |
| 4096x14336, M=4096, bf16 | 4.395 ms | 10.182 ms |
Wikitext PPL is the same before and after (19.066 for the fp16 checkpoint), so the regression buys no accuracy on real checkpoints.
Repro
import time, torch
from gptqmodel.nn_modules.qlinear.fp8 import TorchFP8Linear
K, N, M = 4096, 14336, 4096
lin = torch.nn.Linear(K, N, bias=False)
m = TorchFP8Linear(bits=8, group_size=-1, sym=True, desc_act=False, in_features=K, out_features=N,
bias=False, register_buffers=False)
m.pack_original(lin, None, None)
m = m.cuda().eval()
x = torch.randn(M, K, device="cuda", dtype=torch.bfloat16)
with torch.inference_mode():
for _ in range(3): m(x)
torch.cuda.synchronize(); t = time.perf_counter()
for _ in range(20): m(x)
torch.cuda.synchronize(); print((time.perf_counter() - t) / 20 * 1e3, "ms")
# b25a6123e: 4.40 ms 4b6592381: 10.18 msSuggested fix
- Do not widen when
x_flat.dtype == torch.bfloat16: the narrowed BF16 weight cannot overflow, andmatmul_fp32_reducealready keeps the reduction and bias in FP32. - For FP16, decide once per load instead of on every forward. Compute
max|decoded weight|atpost_init/load (or after_load_from_state_dict) and cache a flag. Take the FP32 path only when that max exceeds the FP16 finite range. The flag must be invalidated on reload the same way_invalidate_compiledis.
Test that should have caught it
tests/test_portable_wide_epilogues_4471.py only checks numerics. A dispatch assertion would have caught this: a bias-free FP16/BF16 module whose weights are in range must not call dequantize_weight(dtype=torch.float32) on CUDA. Mock dequantize_weight and check the dtype it is called with.
Severity
[PERF] regression, 2x prefill and about 15% decode on every default FP8 checkpoint on CUDA, with no accuracy benefit for in-range weights.
🤖 Generated with Claude Code
Activity
Qubitium commentedon Nov 17, 2026
Claimed. This issue is now owned by a dedicated worker; a fix PR referencing it will follow.
Qubitium commentedon Nov 17, 2026
Claimed by an AI agent. This is a regression from #4533; will restore the bias-free CUDA fast path while keeping the wide accumulator for the bias epilogue, with measured prefill/decode evidence.
perf(fp8): keep the native GEMM for range-safe bias-free weights (#4545…