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] #5173 (fix of #5146) incomplete for block-scaled FP8 (FineGrainedFP8/DeepSeek/Qwen checkpoints, the load-only production format): TorchFP8Linear still rebuilds the weight in two full passes per forward - bs1 decode 3.6x dense fp16 on sm_120, 5.5x per linear #5318
Found on main @ c03416c, RTX 5090 (sm_120), torch 2.15 + CUDA 13.2, transformers 5.22.
What is wrong
Since #5242, FP8 is load-only. The checkpoints GPT-QModel now serves are production ones, and the common one is block-scaled: transformers FineGrainedFP8 / DeepSeek / Qwen, weight_block_size=[128,128], multiplier weight_scale_inv. #5173 (the fix for #5146) made row/tensor scales lean: exact codes, GEMM on the transposed view, scale applied in an FP32 epilogue. Block scales got only a partial treatment. TorchFP8Linear._forward_lean_scaled_matmul (gptqmodel/nn_modules/qlinear/fp8.py:~907-945) still rebuilds the full weight on every forward, in two separate full-weight passes before the GEMM:
codes = weight.to(dtype=x_flat.dtype): read 1 B, write 2 B per element;
blocks.mul_(scale[:, None, :, None]): read 2 B, write 2 B (a mixed-dtype in-place op with FP32 opmath);
matmul_fp32_reduce(x, codes.t()): read 2 B.
That is about 9 B of traffic per weight element for a 1 B format. Dense FP16 needs 2 B. At batch-1 decode it is all memory traffic.
Measured (Llama-3.1-8B truncated to 8 layers, FP8 checkpoint written by transformers FineGrainedFP8Config(); AUTO -> TorchFP8Linear, block 128x128)
Prefill 2048 tokens: 52.8 ms vs 39.7 dense. generate(128 tokens): 74.9 vs 250.6 tok/s. Under cache_implementation="static" (compiled), FP8 still runs at 61 vs 191 tok/s for dense, measured on a shared GPU.
Per linear, M=1 (us; same process, shared GPU):
layer (K x N)
TorchFP8Linear
dense fp16 GEMM
one fused dequant pass + cuBLAS GEMM (emulated)
q_proj 4096x4096
94.5
13.8
25.4
k_proj 4096x1024
31.3
7.1
23.8
gate_proj 4096x14336
388.5
70.0
149.3
down_proj 14336x4096
386.0
70.2
149.6
The emulation replaces the cast and the in-place scale with a single torch.compiled elementwise pass, (w8.view(R/128,128,C/128,128).float() * s[:,None,:,None]).half(), followed by the same matmul_fp32_reduce. Its output error vs an fp32 reference equals the current path's (2.07e-4 rel), and greedy tokens are identical. Monkeypatched into _forward_lean_scaled_matmul, it takes end-to-end decode from 21-27 ms to 8.1-8.6 ms per step on a shared GPU.
Minimum: decode block-scaled codes in one pass, not cast-then-scale. Use a small Triton/CUDA elementwise kernel, or a cached torch.compile of the decode, that reads FP8 and the block scale and writes the activation dtype once. That is about 2.6x per linear on these shapes.
Better: a fused W8A16 GEMV/GEMM for small M that applies the 128x128 block scale inside the K loop (the same structure as the Marlin/Machete FP8 paths), so decode reads 1 B per weight and beats dense. Or, where available, torch._scaled_mm with blockwise scales for the W8A8 activation_scheme="dynamic" contract that these checkpoints declare.
Test that should have caught it
#5173's benchmarks covered row/tensor payloads (the GPT-QModel-written layout). Add a decode-throughput regression for a FineGrainedFP8 block checkpoint, for example assert that bs1 decode stays within ~1.5x of dense FP16 on CUDA.
Severity
Medium (perf). Every production FP8 checkpoint served through GPT-QModel on CUDA decodes 3.6-3.8x slower than the same model in dense FP16, and 3.6x slower than 4-bit Marlin, even though the format halves the weight bytes.
Activity
ZX-ModelCloud commentedon Nov 19, 2026
Agent claim released:
agent-c33281b0cfcdfinished this issue.Pull request: #5340
User directed work on assigned issue; reproducing block-scaled FP8 decode cost and implementing single-pass decode