Skip to content

FP8 block decode: keep the fused small-M kernel for fp16 when torchao is selected - #15361

Merged
danielhanchen merged 3 commits into
mainfrom
dh/s7-fp8-fp16-decode-fused
Oct 27, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
dh/s7-fp8-fp16-decode-fused

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

Since #15153 found torchao 0.18's blockwise_fp8_gemm, startup selects torchao_block_matmul on sm_90 / sm_100 / sm_120 whenever torchao is installed. can_use_fp8_block_small_m turns itself off whenever torchao is the selected backend, because torchao beats the fused small-M kernel for bf16.

torchao's kernel writes bf16 only, so torchao_block_matmul sends every fp16 call back to w8a8_block_fp8_matmul_triton. fp16 decode therefore lost the fused kernel and ran act_quant plus the Triton GEMM, two launches instead of one, and never used torchao anyway.

Fix

  • can_use_fp8_block_small_m only declines for a non-fp16 input when torchao is selected. bf16 still goes to torchao, and nothing changes when the Triton GEMM is selected (sm_89, or no torchao).
  • _fp8_block_small_m_first (100+ SM parts): with torchao selected, the GEMV is tried before the fused kernel, so the decode calls it already took (it skips act_quant) stay on it. With the Triton GEMM selected the order is unchanged.

Tests

tests/test_fp8_small_m_torchao_route.py needs no GPU. Kernels are replaced by recorders and the routing is checked through FP8BlockQuantLinear.forward and _fp8_block_small_m_first:

  • fp16, M = 1 / 4 / 16, torchao selected: one fused call. Fails on main, where act_quant plus the GEMM run.
  • bf16 with torchao selected still runs act_quant plus torchao. Triton selected runs the fused kernel for both dtypes. M above 16 and UNSLOTH_FP8_BLOCK_SMALL_M=0 keep two launches. The GEMV keeps its calls with torchao selected.

On main: 4 failed, 10 passed. With this change: 14 passed. The existing FP8 suites (tests/test_fp8_*.py, tests/test_compressed_tensors_fp8_kernel_route.py) also pass on CPU: 263 passed, 458 skipped (GPU-only).

Benchmark

RTX PRO 6000 Blackwell (sm_120), torch 2.13.0, torchao 0.18.0. Each arm runs in its own long-lived process. Each round times base, head, then the second base copy, and the next round reverses the order. That gives 12 rounds of 200 eager fp8_linear calls per arm, measured as host time per call with a sync at the end. A/A is the second base copy over the first.

fp16, GEMV on (default):

N x K, M main this PR head / main A/A
2048 x 2048, 1 42.1 us 30.6 us 0.73 0.96
6144 x 2048, 4 42.0 us 24.5 us 0.58 0.96
4096 x 4096, 1 41.7 us 30.7 us 0.74 0.98
14336 x 4096, 1 41.5 us 24.4 us 0.59 0.98
2048 x 6144, 16 (GEMV) 30.1 us 30.0 us 1.00 0.99
4096 x 14336, 4 (GEMV) 30.0 us 30.0 us 1.00 1.00

With the GEMV off, fp16 is 0.60 to 0.90 of main across all six shapes. For bf16, every shape takes the same route as on main, and head / main is between 0.99 and 1.01. That is inside the A/A spread of 0.97 to 1.04.

Outputs: bf16 is bit-identical to main for every shape. fp16 is bit-identical to the same code without torchao installed, which is what fp16 decode produced before #15153. Against main with torchao, the largest fp16 difference is 3.9e-3, and the error against an fp64 reference is the same on both sides. On L4 (sm_89, where the Triton GEMM is selected), every output is bit-identical to main and timings are unchanged.

Not covered

  • Hopper (sm_90) and B200 (sm_100) were not timed; the routing is the same as on sm_120.
  • The bf16 torchao route still autotunes 48 configurations for each new token-count bucket on the first call. This PR does not change that.

… is selected

torchao's blockwise GEMM writes bf16 only, so torchao_block_matmul sends fp16 calls to the
Triton GEMM after a separate act_quant. The small-M gate turned off whenever torchao was the
selected backend, so fp16 decode ran two launches instead of the one fused kernel.

Now fp16 keeps the fused kernel with torchao selected, and on 100+ SM parts the GEMV still
takes the calls it took before (it skips act_quant). bf16 routing is unchanged.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 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-26T11:54:46.951791Z 59b688b 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. You're on a roll.

Reviewed commit: 59b688b47c

ℹ️ 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 bc0603f into main Oct 27, 2026
@danielhanchen
danielhanchen deleted the dh/s7-fp8-fp16-decode-fused branch October 27, 2026 05:17
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