Skip to content

FP8 block decode: weight-only GEMV where it beats the block GEMM, fused small-M called directly - #14813

Merged
danielhanchen merged 20 commits into
mainfrom
fp8int-block-gemv-smallm
Oct 22, 2026
Merged

danielhanchen merged 20 commits into
mainfrom
fp8int-block-gemv-smallm

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Follow-up to #14792 (block-FP8 small M: fused act_quant + narrow-N w8a8 kernel), which is merged on main. This adds a weight-only block-FP8 GEMV for decode and fixes the order in which the two decode kernels are tried.

GEMV (decode: one token per sequence, no grad, sm89+ CUDA): reads the fp8 weight once and applies the block scale to unquantized bf16 / fp16 activations with fp32 accumulation. M is not specialized. It only takes shapes where it is faster than main's act_quant + block GEMM in both timing modes (L2 flushed, and CUDA graph replay with the weight hot in L2):

  • narrow outputs only (2N <= K: k / v, o, down);
  • on parts with fewer than 100 SMs: weights up to 16M elements, at most 8 decode rows, and at most 2 rows on weights over 8M elements (L2 hot, its 16-row tile becomes compute bound: on L4, 2048x6144 at 4 / 8 / 16 rows took 1.06x / 1.46x / 1.98x main's time before this cap);
  • on parts with 100+ SMs: up to 16 rows.

A weight's eligibility (including its row cap) is decided once per scale tensor. Prefill, training, pre-sm89 and ROCm keep the block path. UNSLOTH_FP8_BLOCK_GEMV=0 disables it.

Order between the two kernels, fixed at import:

devices eager decode compiled regions
any CUDA device with fewer than 100 SMs (L4) GEMV where it applies, else the fused small-M kernel GEMV where it applies, else the block GEMM
all CUDA devices with 100+ SMs (RTX PRO 6000, B200), Triton block GEMM fused small-M kernel, called directly; the GEMV only where the fused kernel does not apply GEMV where it applies, else the block GEMM (the fused kernel is eager only)
FBGEMM / torchao block GEMM GEMV, else that GEMM same

With the GEMV tried first everywhere, the combined path was 3% to 4% faster than #14792 alone on L4 but 2% to 3% slower on RTX PRO 6000 (Qwen3-1.7B / 8B FP8, batch 1 / 4 / 8, A/A at most 3.2%), which is why big parts try the fused kernel first.

End to end decode

Greedy decode, 64 new tokens, torch 2.14.1. Main, #14792 and this PR run from separate checkouts in separate processes, interleaved main, PR, PR, main, 3 rounds (the two PRs alternate order); tok/s is the median, A/A is the largest main vs main gap within a round.

GPU model batch main tok/s #14792 / main this PR / main this PR / #14792 A/A
L4 Qwen3-1.7B-FP8 1 14.1 1.276 1.323 1.036 1.9%
L4 Qwen3-1.7B-FP8 4 56.8 1.273 1.314 1.033 1.9%
L4 Qwen3-1.7B-FP8 8 113.4 1.266 1.310 1.035 2.2%
L4 Qwen3-8B-FP8 1 10.8 1.231 1.302 1.058 1.8%
L4 Qwen3-8B-FP8 4 43.2 1.245 1.312 1.053 1.9%
L4 Qwen3-8B-FP8 8 86.3 1.238 1.309 1.057 2.1%
RTX PRO 6000 Qwen3-1.7B-FP8 1 55.5 1.150 1.206 1.049 2.0%
RTX PRO 6000 Qwen3-1.7B-FP8 4 219.8 1.153 1.201 1.042 0.6%
RTX PRO 6000 Qwen3-1.7B-FP8 8 438.5 1.151 1.198 1.041 2.0%
RTX PRO 6000 Qwen3-8B-FP8 1 42.7 1.145 1.190 1.039 1.0%
RTX PRO 6000 Qwen3-8B-FP8 4 170.3 1.154 1.192 1.033 1.3%
RTX PRO 6000 Qwen3-8B-FP8 8 339.2 1.153 1.191 1.032 1.4%

Engagement per 64-token generate: on L4, Qwen3-1.7B runs 5292 GEMV calls at batch 1 (k, v, down) and 3528 at batch 4 / 8 (k, v; down is past its 2-row cap), the rest on the fused kernel. On RTX PRO 6000 every decode projection runs the fused kernel and the GEMV runs 0 times in eager; the gain over #14792 there is host time, since the fused kernel is called directly instead of through FP8BlockQuantLinear.forward.

Kernel timing, both modes

Every shape and row count the GEMV takes, out of 20 swept projection shapes (Qwen3 0.6B to 8B, Llama 8B) at M = 1, 2, 4, 8, 16, bf16. GEMV time / main's act_quant + block GEMM time, 3 interleaved rounds, medians:

GPU N x K rows taken L2 flushed device time CUDA graph replay, L2 hot
L4 1024x2048 1 to 8 0.73 to 0.81 0.55 to 0.60
L4 1024x2560 1 to 8 0.69 to 0.74 0.54 to 0.73
L4 1024x3072 1 to 8 0.69 to 0.75 0.56 to 0.68
L4 1024x4096 1 to 8 0.72 to 0.73 0.56 to 0.64
L4 2048x6144 1 to 2 0.92 to 0.94 0.84 to 0.93
RTX PRO 6000 1024x2048 1 to 16 0.88 0.72 to 0.78
RTX PRO 6000 1024x2560 1 to 16 0.78 0.58 to 0.66
RTX PRO 6000 1024x3072 1 to 16 0.76 to 0.80 0.58 to 0.65
RTX PRO 6000 1024x4096 1 to 16 0.67 0.47 to 0.52
RTX PRO 6000 2048x6144 1 to 16 0.67 to 0.72 0.45 to 0.54
RTX PRO 6000 2560x9728 1 to 16 0.60 0.38 to 0.46
RTX PRO 6000 4096x12288 1 to 16 0.71 to 0.73 0.49 to 0.59
RTX PRO 6000 4096x14336 1 to 16 0.73 to 0.77 0.47 to 0.61

Worst cell: 0.94 (L2 flushed) and 0.93 (graph) on L4, 0.88 and 0.78 on RTX PRO 6000. The main vs main A/A spread is 1.2% (flushed) and 1.0% (graph) at the 90th percentile on L4, 2.2% and 5.3% on RTX PRO 6000 (its event timer ticks at about 2 us). Shapes and row counts it does not take are left to the fused kernel (eager) or the block GEMM (compiled); their numbers are unchanged from #14792.

Accuracy

Layer 0 mlp of Qwen3-0.6B-FP8 on real hidden states (16 decode rows from two prompts), against an fp64 evaluation of the same FP8 weights, B200 (100+ SMs, so eager takes the fused kernel and compiled takes the GEMV for down_proj):

rows eager (fused small-M, quantized activations) compiled (GEMV for down, unquantized) main
1 mean abs 0.0078, rel RMS 2.45% mean abs 0.0058, rel RMS 1.94% same as eager
4 0.0074, 2.81% 0.0057, 2.22% same as eager
8 0.0056, 2.81% 0.0044, 2.24% same as eager
16 0.0047, 2.87% 0.0037, 2.28% same as eager

The compiled path is closer to fp64 at every row count (about 20% lower error); eager and compiled differ by at most 0.039 on these inputs (largest reference value 3.8). From the GEMV's own branch (B200, Qwen3-0.6B-FP8, 32 prompts x 64 tokens): teacher-forced NLL 0.6201 with it vs 0.6219 without, paired difference 95% CI [-0.0050, +0.0013]. Because it skips activation quantization, greedy text differs from main and from #14792 where it runs.

Tests

28 existing FP8 test files plus tests/test_fp8_block_small_m.py and tests/test_fp8_block_gemv.py (GEMV against an fp64 oracle, split K, routing, kill switch and device gates, non-128 blocks, cached eligibility, the small-GPU row cap, a compiled single op, both router orders, the training path, compiled and pre-sm89 fallbacks):

run 28 existing files test_fp8_block_gemv.py on #14792 here
L4, torch 2.14.1 same 4 failing IDs as main 37 failed all pass
RTX PRO 6000, torch 2.14.1 same 4 failing IDs as main 37 failed all pass
RTX PRO 6000, torch 2.10.0 same 4 failing IDs as main 37 failed all pass
B200, torch 2.14, both new files 108 passed

The 4 failures are in test_modelopt_fp8_loading.py and are pre-existing on main. The small-M tests pin the GEMV off and the GEMV tests pin the fused kernel off, so each still tests its own kernel.

torch.compile

Qwen/Qwen3-0.6B-FP8, LoRA r 8: 3 training steps, HF generate at batch 1, 2, 4, 8, 16, then layer 0's mlp and k_proj compiled at decode batch 1 to 16 plus a prefill:

torch GPU training breaks / recompiles decode breaks / recompiles compiled modules breaks / recompiles Triton variants after batch 16
2.14.1 L4 0 / 0 0 / 0 0 / 0 GEMV 2, fused small-M 3, w8a8 GEMM 8
2.14.1 RTX PRO 6000 0 / 0 0 / 0 0 / 0 fused small-M 3, w8a8 GEMM 8
2.10.0 RTX PRO 6000 0 / 0 0 / 0 mlp 3 (same on main), k_proj 0 / 0 fused small-M 3, w8a8 GEMM 8

On B200 (torch 2.14) a custom backend shows the compiled mlp graph holding unsloth.fp8_block_gemv for down_proj, so compiled regions on 100+ SM parts take the GEMV. The GEMV's variant count does not grow with the batch.

Not proven

  • B200 timing: the exclusive B200 queue wait was over 30 minutes, so it was dropped; B200 ran the tests, the compiled graph check and the accuracy table.
  • The row caps are fitted at M = 1, 2, 4, 8, 16 on L4 and RTX PRO 6000; other row counts and other GPUs (for example 100+ SM Hopper, or parts between 58 and 188 SMs) are interpolated, not measured.
  • A machine mixing a sub-100-SM GPU with a 100+ SM one uses the GEMV-first order (with the small-GPU caps only on the small one).
  • Timing on torch 2.14.1 only; AMD / ROCm and XPU not run (the GEMV is gated off on ROCm).

…nt + block GEMM

Decode-sized block-FP8 calls (one token per sequence, no grad, sm89+ CUDA)
read the fp8 weight once and apply the 128x128 block scale to unquantized
bf16 / fp16 activations with fp32 accumulation. Prefill, training, pre-sm89
and ROCm keep the block GEMM. UNSLOTH_FP8_BLOCK_GEMV=0 restores it.
…de-sized M

Decode (M <= 16) on block-FP8 weights launched act_quant and a 16 x 128 tile
w8a8 GEMM with only N / 128 programs. One launch now quantizes the activation
tile in-kernel (same max / 448 scale and fp8 cast as act_quant) and runs a
narrow N tile with an optional deterministic split K, sized from the SM count.
Without a grad to compute, FP8BlockQuantLinear.forward is called directly to
skip the autograd.Function host cost. Larger M, torchao / fbgemm routes,
pre-sm89 devices and torch.compile regions keep the previous path.
UNSLOTH_FP8_BLOCK_SMALL_M=0 restores it.
Swept in both timing modes (L2 flushed and CUDA graph replay, L2-hot) on
L4 and RTX PRO 6000: the GEMV only runs for narrow outputs (2N <= K: down,
kv, o) and, on parts with fewer than 100 SMs, weights up to 16M elements.
Per-class tile configs from the same sweep. SM counts are read once at
import, so the gate stays trace-friendly.
The weight-only checks (layout, scale grid, routing cut, device gates) are
cached on the weight per scale tensor, so projections that keep the block
GEMM pay only the per-call input checks. Without it the gate cost a G4
Qwen3-0.6B-FP8 decode 4%.
Both branches added _sm_count; keep the import-time table (no device query
inside a compiled region) and drop the lru_cache copy.
Where both take a decode call, the fused act_quant + w8a8 kernel decodes
Qwen3-1.7B / 8B FP8 about 2.5% faster than the weight-only GEMV on an RTX
PRO 6000 (188 SMs); on L4 (58 SMs) the GEMV wins. Compiled regions keep the
GEMV, since the fused kernel is eager only. The small-M tests now pin the
GEMV off, the GEMV tests pin the fused kernel off, and two tests cover the
choice between them.
Weights the GEMV never takes, and (eager, 100+ SMs) weights left to the fused
small-M kernel, go straight to the block path after the first call, so the
GEMV check adds no per-call host work on top of the fused kernel.
The per-call SM count check and route cache still left the combined path
about 0.8% behind the fused kernel alone on an RTX PRO 6000. When every CUDA
device has 100+ SMs and the Triton block GEMM is in use, fp8_block_quant_linear
is now bound to a function that tries the fused small-M kernel first (called
directly), then the GEMV (compiled regions), then the block path; otherwise
the GEMV wrapper is bound as before. Tests cover both orders, the training
path, compiled regions and pre-sm89 gating.
L2-hot (CUDA graph replay) the GEMV's 16-row tile is compute bound: on L4
it took 1.06x / 1.46x / 1.98x the block GEMM's time on 2048 x 6144 at 4 / 8 /
16 rows, and 1.03x on 1024 x 3072 at 16 rows. Below 100 SMs it now takes up
to 8 rows, and up to 2 on weights over 8M elements; those calls go to the
fused small-M kernel (eager) or the block GEMM (compiled) instead.
main's rows and small-M code is kept as merged; the GEMV, its router and the
small-M test's GEMV-off fixture are layered on top.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-22T22:17:52.146506Z 4851c0f Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 4851c0f311

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 9bc4713 into main Oct 22, 2026
@danielhanchen
danielhanchen deleted the fp8int-block-gemv-smallm branch October 22, 2026 22:21
danielhanchen added a commit that referenced this pull request Oct 23, 2026
Conflicts:
- studio/backend/utils/security/file_security.py: main (#14527) moved the
  modules.json escape check ahead of the self-folder skip so "..", "../.."
  and "/" (only dots and slashes) are flagged. This branch replaced the
  lexical _escapes_folder(sub) with _escapes(folder, sub), which also
  resolves a ".." climbing through a symlinked directory. Kept main's
  ordering and comment and this branch's _escapes(folder, sub) call.
- tests/test_fp8_block_small_m.py: this branch only carried pre-commit
  formatting; main has the same formatting plus the autouse
  _no_block_gemv fixture (#14813). Took main's file.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant