You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[PERF] #4594 (fix of #4545) incomplete: TorchFP8Linear (CUDA AUTO for FP8) still decodes every weight through FP32 plus a transpose copy per forward: bs1 decode 7.6x slower than dense fp16 on sm_120 (Llama-3.1-8B layers) #5146
Found on main @ 7b8f127, RTX 5090 (sm_120), torch 2.15 + CUDA 13.2, transformers 5.22.
What is wrong
With the default FP8Config() (weight-only, weight_scale_method="row", activation_quant_method="none"), CUDA AUTO loads TorchFP8Linear, and every forward runs _forward_dequant_matmul -> dequantize_weight -> _dequantize_native (gptqmodel/nn_modules/qlinear/fp8.py:627-669). That function makes several full-weight passes on every call, for every layer, including each decode step:
deq.div_(scale_inv.view(-1, 1)): reads and writes the fp32 weight.
.to(target_dtype): reads fp32, writes fp16.
.transpose(0, 1).contiguous(): a full transpose copy, only so that matmul_fp32_reduce gets an [in, out] operand.
That is about 24 B of memory traffic per weight element for a 1 B/element format, before the GEMM. #4594 (fix of #4545) moved the GEMM back to native fp16. It kept this FP32 decode and the transpose copy, so the FP8 kernel is now far slower than the dense fp16 model it replaces. torch.profiler for one mlp.gate_proj call (4096 -> 14336) at M=1 shows aten::copy_ 146 us x4, div_ 308 us, transpose copy 164 us, and the GEMM 48 us.
Numbers
Llama-3.1-8B-Instruct truncated to 8 decoder layers (same layer shapes as the 8B; vocab/lm_head dense in both). FP8Config() quantized with GPTQModel, fp16, batch 1, CUDA events, GPU otherwise idle:
dense HF fp16
FP8 AUTO (TorchFP8Linear)
decode step, bs1 (DynamicCache)
3.62 ms
27.6 ms (7.6x slower)
decode step, bs8
3.95 ms
27.4 ms (6.9x)
prefill 512 / 2048 tokens
11.1 / 39.6 ms
35.5 / 66.3 ms (3.2x / 1.7x)
generate() 128 tokens, bs1
254 tok/s
36 tok/s
q_proj / gate_proj / down_proj at M=1
23 / 79 / 83 us
194 / 959 / 966 us
For reference, GPTQ 4-bit (Marlin) on the same model decodes at 3.49 ms/step and AWQ Marlin at 3.41 ms.
Emulating a leaner path on the same modules: cast the FP8 codes straight to fp16, run the GEMM on the transposed view with an fp32 output, and apply the per-row inverse scale to the output (torch.mm(x, w.to(fp16).t(), out_dtype=torch.float32) * (1/scale_inv), then round once):
module, M
current
cast + GEMM + row-scale epilogue
rel diff vs current
gate_proj, 1
959 us
204 us
3.1e-4
down_proj, 1
967 us
204 us
2.8e-4
q_proj, 1
192 us
51 us
2.9e-4
gate_proj, 2048
2070 us
1429 us
3.0e-4
(The fp32 output keeps the unscaled product out of fp16 overflow. Done in fp16 output, down_proj at M=2048 overflowed.) The same applies to "tensor" scales. For "block" scales, the fp32 round trip and the transpose copy could still be dropped by scaling in the target dtype when _weights_fit_activation_dtype already says it is range-safe.
Range-safe weights (_native_gemm_eligible): decode straight to the activation dtype. Use the [out, in] weight as a transposed view (F.linear/mm on .t() needs no copy). For row/tensor scales, apply the scale in an fp32 output epilogue instead of on the full weight.
Longer term, a fused W8A16 FP8 kernel (for example the Marlin FP8 variant) would make the FP8 format faster than fp16 at decode instead of 7x slower.
Test that should have caught it
There is no throughput check comparing the FP8 kernel with a dense fp16 nn.Linear at M=1. A ratio bound on a 4096x14336 layer would catch both the fp32 round trip and the transpose copy.
Severity
Medium [PERF]: the default FP8 format is 7.6x slower than fp16 at batch-1 decode on the default CUDA kernel.
FP8 lean decode: cast straight to the activation dtype, mm on the transposed view with fp32 out, per-row scale in an fp32 epilogue; 4.3x faster bs1 decode than the old path on sm_89.
Resolved on main by merged PR #5173 (perf(fp8): decode W8A16 with exact codes and an FP32 scale epilogue, merge commit 7d4797c). Verified here that #5173 is an ancestor of origin/main; tests/kernels/test_fp8_kernel.py and tests/test_fp8_bias_range.py pass (97 passed, 6 skipped). No further change needed, so this issue can be closed.
Activity
Qubitium commentedon Nov 18, 2026
Agent claim released:
agent-4e3a0639ecaffinished this issue.Pull request: #5173
FP8 lean decode: cast straight to the activation dtype, mm on the transposed view with fp32 out, per-row scale in an fp32 epilogue; 4.3x faster bs1 decode than the old path on sm_89.
perf(fp8): decode W8A16 with exact codes and an FP32 scale epilogue (#…
Qubitium commentedon Nov 18, 2026
Resolved on
mainby merged PR #5173 (perf(fp8): decode W8A16 with exact codes and an FP32 scale epilogue, merge commit 7d4797c). Verified here that #5173 is an ancestor oforigin/main;tests/kernels/test_fp8_kernel.pyandtests/test_fp8_bias_range.pypass (97 passed, 6 skipped). No further change needed, so this issue can be closed.Qubitium commentedon Nov 18, 2026
Closing: resolved on main by merged PR #5173 (merge commit 7d4797c), verified as an ancestor of origin/main. No further change needed.