Skip to content

cuda: thin F32 mat-muls take mmvf up to 8 columns on Ampere+ (+6% MTP decode on H200) - #380

Merged
bri-prism merged 1 commit into
prismfrom
cuda/f32-thin-mmvf
Oct 24, 2026
Merged

bri-prism merged 1 commit into
prismfrom
cuda/f32-thin-mmvf

Conversation

@bri-prism

Copy link
Copy Markdown
Collaborator

Overview

On Ampere and newer GPUs, F32 mat-muls with 4 or more columns go to the tiled mul_mat_f, which tiles over output rows. A thin src0 therefore gets only one or two blocks. On Bonsai 2 (Qwen3.5 GDN) the case that matters is ssm_alpha / ssm_beta: F32 4096×32, two per GDN layer, so 48 per verify step on the 9B. Under MTP depth 3, nsys puts them at 8.2% of GPU time, at 17.5 µs each for 512 KB of weights.

This PR routes F32 with src0 ≤ 512 rows and ≤ 8 columns (MMVF_MAX_BATCH_SIZE) to mmvf, which follows the existing small-src0 rule for F16 (+5 lines in mmvf.cu).

Validation

All runs are on an H200 with CUDA 12.8 and a Release sm_90 build, at prism 73bfe6cc plus this patch.

Kernel (test-backend-ops perf, F32 [4096 × m] × [4096 × n]):

m n=1–3 n=4 n=5 n=6 n=8 n=12–16
32 unchanged 14.3 → 3.1 µs −72% −67% −53% unchanged
128 unchanged 14.2 → 3.4 µs −67% −64% −51% unchanged*
512 unchanged 15.8 → 4.4 µs −64% −58% −46% unchanged

* One outlier run at m=128, n=16 (outside the rule, so the same kernel as before).

End to end: Ternary-Bonsai-2-9B PQ2_0-MTP-Q8_0 on llama-server, 4 prompts × 128 greedy tokens, 2 rounds, vs stock prism:

mode decode change
MTP, depth 3 +5.6% to +7.0%
DFlash2, depth 7 +0.4% to +5.8%
no speculation unchanged (1 column already took mmvf)

Correctness

  • test-backend-ops MUL_MAT 1651/1651, including new eval cases at m=32/512/513 and n=1–16.
  • Not bit-exact at 4–8 columns: mmvf reduces in a different order than mul_mat_f. Greedy output is deterministic and diverges late (around token 58–116) in 2 of 4 prompts. This is the same exactness class as today's switch from mmvf to mul_mat_f between 3 and 4 columns.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. Claude Code found this with nsys, wrote the change and tests, and ran the benchmarks; reviewed by me.

🤖 Generated with Claude Code

https://claude.ai/code/session_01PRQc3TQpn72qBxrUn5vKqo

@bri-prism

Copy link
Copy Markdown
Collaborator Author

Independent cross-check from a parallel branch that made the same change. That branch kept F32 matrices with ≤64 rows on mul_mat_vec_f up to 8 columns, mirroring the BF16 rule.

Results on H200, 9B PQ2_0 at depth 4096:

  • pp N=4: 6.129 → 5.535 ms (−9.7%). N=5-8 is 6-9% faster, and t(4)/t(1) drops from 1.67 to 1.51.
  • End-to-end MTP K=3: median ×1.057.
  • Path change: nsys shows mul_mat_f<float> replaced by mul_mat_vec_f<float>.
  • Exactness: columns 4-8 are bitwise equal to the 1-column result, so the batch is now invariant (exact FP32 instead of TF32). Tokens differ from tip, as noted in this PR.

That branch won't be opened as a separate PR. This one is the vehicle.

…columns on Ampere+

On Ampere and newer, F32 mat-muls with 4+ columns went to the tiled
mul_mat_f, which tiles over output rows: a 32-row src0 (the GDN
ssm_alpha/ssm_beta projections, 2 per GDN layer, i.e. 48 per verify step on
Bonsai 2 9B) gets one or two blocks. mmvf is 2-5x faster there up to its
batch limit. Same rule shape as the existing small-src0 F16 case.

H200, F32 [4096 x m] x [4096 x n]: m=32 n=4 14.3 -> 3.1 us, n=5..8 -53..-72%;
m=128/512 n=4..8 -46..-76%; n<=3 and n>=12 unchanged (other kernels).
Ternary-Bonsai-2-9B PQ2_0-MTP, llama-server: MTP depth 3 +5.6..+7.0%
decode, DFlash2 depth 7 +0.4..+5.8%. Not bit-exact for 4-8 columns
(different reduction than mul_mat_f; greedy output diverges late in 2/4
prompts, deterministically). test-backend-ops MUL_MAT 1651/1651.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01PRQc3TQpn72qBxrUn5vKqo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant