Skip to content

FP8 rowwise decode: one multi-row GEMV launch for 2 to 16 tokens - #14791

Merged
danielhanchen merged 5 commits into
mainfrom
fp8int-rowwise-gemv-rows
Oct 22, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
fp8int-rowwise-gemv-rows

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Per-channel (rowwise) FP8 weights with 2 to 16 decode rows (batched generate, GRPO HF rollouts) went to fbgemm_fp8_linear, whose scaled_mm backend dequantizes the whole weight to 16 bits on every call below 64 rows. This adds a multi-row variant of the existing rowwise GEMV that reads each fp8 weight tile once for all rows: exact upcast, fp32 dot accumulate, per-channel scale applied after the sum, split K only where K is long or N narrow (deterministic two-pass reduce, no atomics).

  • 1 row keeps the existing GEMV, 17+ rows keep the GEMM paths.
  • M is do_not_specialize, so one compiled kernel serves every batch size from 2 to 16.
  • Reached from the same callers as the 1-row GEMV: the compressed-tensors FP8 forward (UNSLOTH_COMPRESSED_TENSORS_FP8_KERNELS=1) and fast_linear_forward. UNSLOTH_FP8_GEMV_ROWS=0 restores the previous routing.

End to end decode

RedHatAI/Qwen3-1.7B-FP8-dynamic, UNSLOTH_COMPRESSED_TENSORS_FP8_KERNELS=1, greedy, 64 new tokens, torch 2.14.1. Main and this branch run from separate checkouts in separate processes, interleaved main, branch, main, 3 rounds; tok/s is the median, A/A is the largest main vs main gap within a round.

GPU batch main tok/s this PR tok/s speedup A/A
L4 1 23.0 23.3 1.01x 1.08%
L4 4 60.3 88.7 1.47x 1.54%
L4 8 120.4 177.0 1.47x 1.65%
RTX PRO 6000 1 91.2 91.3 1.00x 1.21%
RTX PRO 6000 4 236.9 342.7 1.45x 1.26%
RTX PRO 6000 8 473.3 674.1 1.42x 1.00%

The new kernel ran 12348 times per 64-token batch-4 / batch-8 generate and 0 times at batch 1. Greedy tokens differ from main at batch 4 / 8 (78 to 89% identical), since main dequantizes or quantizes the activation while this keeps the activation exact; against an fp64 oracle on B200 (84 cases, 60 of them routed to the new kernel), its mean abs error is at or below main's in all 60.

Kernel timing

Same per-channel quantized weights in both arms, bf16, decode rows M, called the way the compressed-tensors forward calls it. Device time with L2 flushed (median of 30), microseconds, main / this PR:

N x K L4 M=1 L4 M=4 L4 M=16 L4 M=17 PRO 6000 M=4 PRO 6000 M=16
2048x6144 (Qwen3-1.7B down) 28.7 / 27.6 221.7 / 43.0 226.3 / 62.5 228.4 / 228.4 36.9 / 16.4 45.0 / 18.4
6144x2048 (Qwen3-1.7B gate / up) 75.8 / 75.8 188.9 / 76.8 193.5 / 76.8 222.2 / 221.2 38.9 / 20.5 40.9 / 22.5
4096x4096 (Qwen3-8B q) 98.3 / 98.3 266.2 / 104.4 270.8 / 104.4 262.7 / 265.2 44.0 / 26.6 46.1 / 26.6
12288x4096 (Qwen3-8B gate / up) 281.6 / 281.6 1150.5 / 282.6 1153.0 / 284.7 1125.4 / 1126.4 151.6 / 61.4 153.6 / 61.4
4096x14336 (Llama 8B down) 279.6 / 279.6 1275.4 / 292.9 1286.6 / 303.1 1316.4 / 1319.9 186.4 / 47.1 190.5 / 49.2

Across 20 shapes (Qwen3 0.6B to 8B, Llama 8B) at M = 2, 4, 8, 16:

GPU metric this PR / main, median range
L4 device, L2 flushed 0.41 0.19 to 0.67
L4 host, back to back calls 0.43 0.06 to 0.67
L4 CUDA graph replay 0.34 0.06 to 0.82
RTX PRO 6000 device, L2 flushed 0.50 0.24 to 0.69
RTX PRO 6000 host, back to back calls 0.52 0.15 to 0.85
RTX PRO 6000 CUDA graph replay 0.41 0.10 to 0.80

M = 1 and M = 17 are within the A/A band of main on every shape (same code path).

Tests

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

run 28 existing FP8 test files new test_fp8_rowwise_gemv_rows.py
L4, torch 2.14.1 same 4 failing IDs on main and here 106 failed on main, all pass here
RTX PRO 6000, torch 2.10.0 same 4 failing IDs on main and here 106 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

HF generate at batch 1, 2, 4, 8, 16 (each batch 8 new tokens), counted after the last batch:

torch GPU graph breaks main / PR recompiles main / PR Dynamo graphs main / PR Triton variants (PR)
2.14.1 L4 0 / 0 0 / 0 16 / 12 1-row GEMV 2, rows GEMV 2, reduce 0
2.10.0 RTX PRO 6000 0 / 0 0 / 0 12 / 8 1-row GEMV 2, rows GEMV 2, reduce 0

The rows kernel's variant count is the same after batch 2 as after batch 16. Fewer Dynamo graphs because the 2 to 16 row calls no longer go through the compiled fbgemm_fp8_linear path.

Not proven

  • B200 timing: the exclusive B200 queue wait was over 30 minutes, so it was dropped. The kernel correctness sweep from the branch work ran on B200.
  • Timing ran on torch 2.14.1 only; torch 2.10.0 has tests and the compile census, not timing.
  • AMD / ROCm, XPU and Blackwell with Triton 3.3 (rows calls keep the GEMM there, gate unchanged) were not run.
  • The split-K reduce kernel compiled 0 variants in the Qwen3-1.7B decode census; its end to end use is covered by the unit tests only.
  • 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.

…ing the weight

Per-channel FP8 weights with 2..16 decode rows (batched generate, GRPO HF
rollouts) went to fbgemm_fp8_linear, whose scaled_mm backend dequantizes the
whole weight to 16 bits for every call below 64 rows. A multi-row variant of
the GEMV reads each fp8 weight tile once for all rows (exact upcast, fp32 dot
accumulate, per-channel scale after the sum), with split K only where K is
long or N narrow (deterministic two-pass reduce, no atomics). One row keeps
the existing kernel; 17+ rows keep the GEMM paths; UNSLOTH_FP8_GEMV_ROWS=0
restores the previous routing.
…usion

M (2..16 decode rows) is do_not_specialize, so one compiled kernel serves
every batch size; tl.dot input_precision / out_dtype and enable_fp_fusion are
passed explicitly.
[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:11.086758Z f26e907 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. Breezy!

Reviewed commit: f26e9071dd

ℹ️ 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 444dcbf into main Oct 22, 2026
@danielhanchen
danielhanchen deleted the fp8int-rowwise-gemv-rows branch October 22, 2026 20:06
danielhanchen added a commit that referenced this pull request Oct 22, 2026
pre-commit.ci fails `ruff-format-with-kwargs` on every open PR (#14806,
#14805, #14803, #14790, #14785, #14781, #14780, #14777, #14776, #14775,
#14774, #14773, #14772, #14770, #14769, #14768, and others): "files were
modified by this hook". pre-commit.ci runs the hooks on all files, and
20 files on main (26 by the time this lands) are not in the formatter's
output, so every PR is reformatted outside its own diff. Where
pre-commit.ci can push, its autofix commit drags those unrelated files
into the PR (for example #14790's touches `tests/test_block_compile.py`,
`tests/test_bnb_integration_compile.py` and `unsloth/models/_utils.py`).

The drift came in with PRs merged today, among them #14702, #14751,
#14759, #14784, #14786, #14787, #14788, #14791 and the FP8 block decode,
GRPO routing replay, 4-bit decode, compiled blocks and MoE LoRA target
changes.

### Change

`python scripts/run_ruff_format.py` (the hook's own entry point,
`ruff==0.6.9` as pinned in `.pre-commit-config.yaml`) applied to those
20 files, plus 6 more from #14739, #14779 and #14806, which merged while
this was open. Formatting only.

### Verification

- Every changed file parses to an identical AST before and after.
- A second run of the hook over the same files changes nothing (fixed
point), and over all tracked Python files on this branch it changes
nothing.
- `ruff check` passes on the changed files;
`tests/test_formatter_fixed_point.py` passes (16).

Open PRs pick this up on their next pre-commit.ci run after merging
main.
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