Skip to content

FP8 block decode: fused act_quant and narrow-N GEMM for 1 to 16 tokens - #14792

Merged
danielhanchen merged 5 commits into
mainfrom
fp8int-block-smallm-config
Oct 22, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
fp8int-block-smallm-config

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Block-FP8 decode (M <= 16 rows) launched act_quant and then the 16 x 128 tile w8a8 GEMM, which runs only N / 128 programs (32 for a 4096 wide projection). This fuses the activation quantization into a narrow-N w8a8 kernel, one launch per linear:

  • Quantizes the activation tile in-kernel with the same max / 448 scale and e4m3 cast as act_quant.
  • N tile and an optional deterministic split K are sized from the SM count; M is not specialized, so batch 1 and batch 16 share one compiled variant per shape.
  • Without a grad to compute, FP8BlockQuantLinear.forward is called directly, skipping the autograd.Function host cost.
  • Larger M, the torchao / FBGEMM block routes, pre-sm89 devices and torch.compile regions keep the previous path. UNSLOTH_FP8_BLOCK_SMALL_M=0 restores it.

End to end decode

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

GPU model batch main tok/s speedup A/A
L4 Qwen3-1.7B-FP8 1 / 4 / 8 14.2 / 57.4 / 114.2 1.25x / 1.26x / 1.26x 0.8% / 1.8% / 1.4%
L4 Qwen3-8B-FP8 1 / 4 / 8 10.8 / 43.9 / 87.1 1.26x / 1.25x / 1.25x 2.2% / 1.6% / 3.2%
RTX PRO 6000 Qwen3-1.7B-FP8 1 / 4 / 8 55.3 / 220.2 / 438.6 1.15x / 1.16x / 1.15x 1.0% / 0.6% / 0.3%
RTX PRO 6000 Qwen3-8B-FP8 1 / 4 / 8 42.7 / 170.1 / 338.0 1.15x / 1.15x / 1.15x 0.3% / 0.7% / 0.7%

A second RTX PRO 6000 session reproduced 1.15x to 1.16x. The fused kernel ran 12348 times per 64-token Qwen3-1.7B generate (all 196 projections at each of the 63 decode steps); act_quant now runs only for the prefill (196 calls, was 12544).

Accuracy: at split K 1 the output is bit-identical to main. Split K > 1 changes the fp32 summation order only; teacher-forced on the same token stream (48 prompts x 64 steps, Qwen3-1.7B-FP8, B200), the mean abs logit error against a dequantized bf16 copy is 0.1987 here vs 0.1967 on main, paired difference 95% CI [-0.0044, +0.0080], with 22 rows better and 26 worse. Greedy text diverges from main after a few tokens for that reason.

Kernel timing

20 projection shapes (Qwen3 0.6B to 8B, Llama 8B), M = 1, 2, 4, 8, 16, bf16, this PR / main:

GPU metric median range worst shape
L4 device, L2 flushed 0.95 0.72 to 1.05 6144x2048, 1.03 to 1.05
L4 host, back to back calls 0.58 0.54 to 1.01
L4 CUDA graph replay, L2 hot 1.07 0.73 to 2.05 6144x2048 M=16
RTX PRO 6000 device, L2 flushed 0.76 0.58 to 1.06 9728x2560 M=8
RTX PRO 6000 host, back to back calls 0.80 0.65 to 0.92
RTX PRO 6000 CUDA graph replay, L2 hot 0.85 0.50 to 2.47 9728x2560 M=16

Eager decode is bound by host launch cost at these sizes, which is where the fused launch pays off. With the weight already in L2 and no launch cost (CUDA graph replay of the same weight), the narrow-N split-K tiles lose to the 128 x 128 GEMM on wide gate / up and long-K down projections at M 8 to 16. The fused kernel is eager only, so torch.compile graphs (including CUDA graphs from reduce-overhead) keep main's path; a model's weights do not stay in L2 across a decode step either.

Tests

Main and this PR from separate checkouts, -m "not server and not native_mxc":

run 28 existing FP8 test files new tests/test_fp8_block_small_m.py
L4, torch 2.14.1 same 4 failing IDs on main and here 69 failed on main, all pass here
RTX PRO 6000, torch 2.10.0 same 4 failing IDs on main and here 69 failed on main, all pass here

The 4 failures on both sides are in test_modelopt_fp8_loading.py and are pre-existing on main.

torch.compile

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

torch GPU training graph breaks / recompiles decode graph breaks / recompiles compiled modules, breaks main / PR Triton variants after batch 16 (main -> PR)
2.14.1 L4 0 / 0 on both 0 / 0 on both 0 / 0 w8a8 GEMM 14 -> 8, fused small-M 3
2.10.0 RTX PRO 6000 0 / 0 on both 0 / 0 on both mlp 3 / 3 (pre-existing), k_proj 0 / 0 w8a8 GEMM 14 -> 8, fused small-M 3

The fused kernel's 3 variants are one per (N, K) config and do not grow from batch 1 to batch 16; the compiled modules match eager to 0.0078 max abs (one bf16 ulp in the mlp output, torch 2.14) and exactly elsewhere.

Not proven

  • B200 timing: the exclusive B200 queue wait was over 30 minutes, so it was dropped (the accuracy run above is from B200).
  • CUDA graph replay of eager decode with weights resident in L2 is slower on some shapes (table above); not measured end to end since nothing in Unsloth captures this path.
  • Timing ran on torch 2.14.1 only; torch 2.10.0 has tests and the compile census.
  • AMD / ROCm and XPU were not run (the route is gated to the Triton block matmul).
  • Timing ran on the branch before it was merged with main 031a117757; main's only fp8.py change in between (FP8 dequant: multiply per-tensor and rowwise scales in fp32 for fp16 output #14677) is in the fp16 per-tensor / per-row weight_dequant, off the decode paths timed here. Tests and the compile census above were re-run on the merged branch.

…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.
[skip ci]
@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-22T20:03:04.448851Z fe9608e 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. Delightful!

Reviewed commit: fe9608e8b7

ℹ️ 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 f232b9a into main Oct 22, 2026
@danielhanchen
danielhanchen deleted the fp8int-block-smallm-config branch October 22, 2026 20:06
debojyoti-dev pushed a commit to debojyoti-dev/unsloth that referenced this pull request Oct 22, 2026
…ed small-M called directly (unslothai#14813)

## Summary

Follow-up to unslothai#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 unslothai#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, unslothai#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 | unslothai#14792 / main | this PR / main |
this PR / unslothai#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 unslothai#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 unslothai#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 unslothai#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 unslothai#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).
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