Repository navigation
FP8 block decode: weight-only GEMV where it beats the block GEMM, fused small-M called directly - #14813
Merged
Conversation
…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.
…ne compiled variant
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.
[skip ci]
Member
Author
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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
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]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
Mis not specialized. It only takes shapes where it is faster than main'sact_quant+ block GEMM in both timing modes (L2 flushed, and CUDA graph replay with the weight hot in L2):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=0disables it.Order between the two kernels, fixed at import:
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.
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: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
mlpof 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 fordown_proj):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.pyandtests/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):test_fp8_block_gemv.pyon #14792The 4 failures are in
test_modelopt_fp8_loading.pyand 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'smlpandk_projcompiled at decode batch 1 to 16 plus a prefill:On B200 (torch 2.14) a custom backend shows the compiled
mlpgraph holdingunsloth.fp8_block_gemvfordown_proj, so compiled regions on 100+ SM parts take the GEMV. The GEMV's variant count does not grow with the batch.Not proven