Skip to content

[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

Closed

Description

@Qubitium

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:

  1. codes = weight.to(dtype=x_flat.dtype): read 1 B, write 2 B per element;
  2. blocks.mul_(scale[:, None, :, None]): read 2 B, write 2 B (a mixed-dtype in-place op with FP32 opmath);
  3. 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)

End-to-end, idle GPU, bs1 decode ms/step (DynamicCache loop):

fp16 bf16
dense HF 3.59 3.59
FP8 block (TorchFP8Linear) 12.93 (3.6x) 13.54 (3.8x)
GPTQ 4-bit Marlin, for reference 3.54 3.69

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.

Repro

# FP8 checkpoint: AutoModelForCausalLM.from_pretrained(src, quantization_config=FineGrainedFP8Config(), dtype=torch.bfloat16).save_pretrained(out)
import torch
from gptqmodel import GPTQModel
m = GPTQModel.load(out, dtype=torch.float16, device="cuda:0")
lin = m.model.model.layers[0].mlp.gate_proj                     # TorchFP8Linear, weight_scale_method='block'
x = torch.randn(1, 4096, device="cuda", dtype=torch.float16)
W = torch.randn(14336, 4096, device="cuda", dtype=torch.float16)
def t(fn, n=200):
    for _ in range(20): fn()
    s, e = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True); torch.cuda.synchronize(); s.record()
    for _ in range(n): fn()
    e.record(); torch.cuda.synchronize(); return s.elapsed_time(e) * 1000 / n
with torch.inference_mode():
    print(t(lambda: lin(x)), t(lambda: x @ W.t()))              # ~388 us vs ~70 us

Suggested fix

  • 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.

🤖 Generated with Claude Code

Activity

  1. ZX-ModelCloud commented on Nov 19, 2026

    @ZX-ModelCloud
    Collaborator

    Agent claim released: agent-c33281b0cfcd finished this issue.

    Pull request: #5340

    User directed work on assigned issue; reproducing block-scaled FP8 decode cost and implementing single-pass decode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions