perf(prims-ts): faster D512 GQA decode for NVFP4 and FP8 KV #9771
All Vouch review confidence checks passed
Residual risk 0.23 = Change Risk (0.57) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.57 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #9771"
general
weight: 0.35
qsang-nv
<!-- .github/pull_request_template.md -->
📌 Description
Speeds up PrimTS decode at head_dim 512 with eight Q heads per KV head (Hq/Hkv 32/4, page 64, FP8 Q), the shape in #9602. Before this change it was slower than trtllm-gen FP8, most visibly when requests share prefix pages.
q_len 1 (dense Swaps Q8, NVFP4 K/V):
q_len 8 (grouped Keeps Q64/KV128):
- Scale TMAs are sliced per stage, which frees 20 KiB of SMEM for deeper FP8 K/V rings.
- Two loader warps.
- Lane-panel O for head-dim stages 2/3.
gmem_reduction_with_separate_kernel) for grouped Keeps D512. Auto now picks it before the in-kernel reducer.Both q_lens: dense FP4 multiply. The NVFP4 row converters (packed TMEM K, SMEM K/V rows) now use the dense
mul.rn.e4m3x4.e2m1x4.e4m3x4form, which the MLA decode converter already uses. It takes four consecutive E2M1 values from each 16-bit half and returns their E4M3 bytes in order, two instructions per eight values. Before, the converters used the paddede2m1p4x4form, which needs a shift, two multiplies and two byte permutes. K and V therefore stay in logical order, and neither Q nor O is permuted. The even-then-odd order is still available viause_permuted_packed_k_order/use_permuted_packed_v_order. Targets without the packed multiply (e.g. SM107) keep the previous sequence.Divergence fixes in the split reducer (
tmem_corr.py). Two lane-0 spin loops left ptxas unable to prove warp convergence for the whole kernel: one on the split ticket (after a lane-0 atomic), one on the cluster transaction barrier. ptxas then wrapped everytcgen05MMA of the MMA warp inBRA.DIV/ELECT/VOTEU.ANY/R2UR.BROADCAST. The fixes:Performance
The #9602 repro, unmodified: Hq/Hkv 32/4, D512, page 64, FP8 Q, BF16 output, KV 32768, 30720-token shared prefix. PrimTS uses NVFP4 K/V and trtllm-gen uses FP8 K/V. Times are kernel µs, median of 3 runs. The trtllm-gen column is the median over all 6 runs. The "this PR" and trtllm-gen columns were re-measured after the dense-multiply commit, interleaved with the previous head on the same node: q_len 1 shared got 2.2-3.0% faster and the other rows 0-0.6%. The "main" column is from the original run on another node, so main / PR carries about 2% of node variance (e.g. B300 q_len 1 unshared B32).
B300
| q_len | prefix pages | batch | main | this PR | trtllm-gen FP8 | main / PR | trtllm-gen / PR |
|---|---|---|---|---|---|---|---|
| 1 | shared | 8 | 104.9 | 61.7 | 73.8 | 1.70x | 1.20x |
| 1 | shared | 16 | 200.9 | 114.0 | 126.3 | 1.76x | 1.11x |
| 1 | shared | 32 | 364.5 | 216.1 | 236.5 | 1.69x | 1.09x |
| 1 | unshared | 8 | 121.6 | 111.1 | 173.7 | 1.09x | 1.56x |
| 1 | unshared | 16 | 218.5 | 198.0 | 320.6 | 1.10x | 1.62x |
| 1 | unshared | 32 | 401.7 | 369.7 | 615.0 | 1.09x | 1.66x |
| 8 | shared | 8 | 152.2 | 120.8 | 122.8 | 1.26x | 1.02x |
| 8 | shared | 16 | 278.2 | 225.2 | 226.5 | 1.24x | 1.01x |
| 8 | shared | 32 | 525.7 | 430.1 | 446.4 | 1.22x | 1.04x |
| 8 | unshared | 8 | 157.2 | 135.9 | 183.0 | 1.16x | 1.35x |
| 8 | unshared | 16 | 284.0 | 247.7 | 331.3 | 1.15x | 1.34x |
| 8 | unshared | 32 | 536.6 | 464.5 | 650.9 | 1.16x | 1.40x |
B200
| q_len | prefix pages | batch | main | this PR | trtllm-gen FP8 | main / PR | trtllm-gen / PR |
|---|---|---|---|---|---|---|---|
| 1 | shared | 8 | 109.7 | 63.6 | 74.8 | 1.72x | 1.18x |
| 1 | shared | 16 | 209.0 | 118.8 | 128.3 | 1.76x | 1.08x |
| 1 | shared | 32 | 379.2 | 224.5 | 240.8 | 1.69x | 1.07x |
| 1 | unshared | 8 | 121.6 | 111.4 | 172.1 | 1.09x | 1.54x |
| 1 | unshared | 16 | 220.6 | 194.0 | 315.9 | 1.14x | 1.63x |
| 1 | unshared | 32 | 400.7 | 359.6 | 601.8 | 1.11x | 1.67x |
| 8 | shared | 8 | 161.0 | 126.8 | 126.7 | 1.27x | 1.00x |
| 8 | shared | 16 | 291.9 | 234.3 | 237.9 | 1.25x | 1.02x |
| 8 | shared | 32 | 543.9 | 444.4 | 476.1 | 1.22x | 1.07x |
| 8 | unshared | 8 | 164.1 | 139.5 | 182.2 | 1.18x | 1.31x |
| 8 | unshared | 16 | 294.9 | 260.2 | 334.4 | 1.13x | 1.28x |
| 8 | unshared | 32 | 556.6 | 501.7 | 674.5 | 1.11x | 1.34x |
Shared prefixes at q_len 8: convert-once split (example, not part of this PR)
With a shared prefix at q_len 8, a single NVFP4 call converts the same prefix pages to FP8 once per request. trtllm-gen reads FP8 directly, so the single call stays near parity with it (table above).
The example
nvfp4_shared_prefix_convert_once.pyin PerkzZheng/prims-ts-examples (PR #4) uses only public APIs plus its own small CUDA kernel. Each step it:1. converts the shared pages once to
E4M3(e2m1 block_scale), the same FP8 values the NVFP4 kernel computes with;2. runs an FP8 K/V prefix pass, which relies on the FP8 D512 profile added here;
3. runs an NVFP4 suffix pass on a side stream;
4. combines the two with
flashinfer.merge_state_in_place.The example checks the conversion bit-exactly and checks both paths against an FP32 reference.
B300 results, 30720 shared plus 2048 private tokens per request, q_len 8. Times are per CUDA-graph replay with a cold L2 (CUDA events, so a few µs above the kernel times above):
| batch | single NVFP4 call | convert-once split | speedup |
|---|---|---|---|
| 32 | 436.2 µs | 323.6 µs | 1.35x |
| 16 | 229.4 µs | 210.9 µs | 1.09x |
| 8 | 124.9 µs | 149.5 µs | 0.84x |
The conversion is a fixed cost of about 35 µs per step. Use the split for roughly 16 or more requests sharing a long prefix, and the single call otherwise. The split is a caller-side choice; this PR does not change dispatch for it.
🔍 Related Issues
Addresses #9602 (PrimTS NVFP4 D512 GQA decode slower than trtllm-gen FP8 with shared prefix pages).
🚀 Pull Request Checklist
Thank you for contributing to FlashInfer! Before we review your pull request, please make sure the following items are complete.
✅ Pre-commit Checks
pre-commitby runningpip install pre-commit(or used your preferred method).pre-commit install.pre-commit run --all-filesand fixed any reported issues.🧪 Tests
unittest, etc.).Reason: <!-- e.g. cross-cutting change to shared infrastructure -->
Suggested CI test targets (optional; paths under
tests/, whitespace-separated — a startingpoint for
@flashinfer-bot run <targets>//bot run <targets>, reviewers may adjust):tests/attention/test_attention_ts_decode.pyNew tests in
tests/attention/test_attention_ts_decode.py:test_attention_ts_decode_d512_q8_auto_selects_grouped_keeps: B8/16/32 with NVFP4 and FP8 K/V select grouped Keeps Q64, no cluster reduction, and the separate reducer when split.test_attention_ts_decode_d512_keeps_policy_scope: Hq64 and q_len 1 stay off Keeps; FP8 K/V D512 outside eight Q heads per KV head is rejected.test_attention_ts_decode_d512_fp8_kv_grouped_keeps: FP8 K/V D512 grouped Keeps accuracy, causal and dense, BF16 and FP16, including base-2 LSE.test_attention_ts_decode_large_tile_size_qwith the separate reducer.test_attention_ts_decode_nvfp4_d512_keeps_ring_depths_fit(converted and raw ring depths fit SMEM),test_attention_ts_decode_whole_tile_nvfp4_stage_requires_full_slices(D192 rejected), and override cases added to the D512 Q8 transformed-slot and policy-scope tests.test_attention_ts_decode_nvfp4_d512_even_odd_order_opt_inruns the opt-in even-then-odd K order (q_len 1, Swaps Q8, with LSE) and K/V order (Keeps Q64, q_len 8) on the GPU against the reference. The transformed-slot and ring-depth tests now check that both orders are off by default and that the opt-in resolves. The large-tile test's LSE reference handles one-token decode Q.Results on B300 and B200 (CUDA 13, local runs):
tests/attention/test_attention_ts_decode.py: 1194 passed, 96 skipped. The 32 failures in that run were addressed:- 28
q_token_sparsetests failed to build. My local tree had no link to the CCCL now bundled with FlashInfer, so the build used the CTK's older CCCL, which lackscuda::fast_mod_div. This is unrelated to this PR.- 4 were in the new D512 tests: the FP8 reference partition and an error-message match. Both were fixed in the test.
-k "q_token_sparse or d512 or large_tile_size_q"): 159 passed, 96 skipped, 0 failed on each GPU.-k "large_tile_size_q or even_odd_order or d512"subset gave 133 passed, 96 skipped, 0 failed on each GPU.🔬 Experimental Track
<!-- Only for PRs submitted under the experimental policy (CONTRIBUTING.md → "Experimental APIs and Backends").
Leave this section untouched for normal PRs. -->
flashinfer/experimental/and/or an@flashinfer_experimental_api. Tracking issue: #- [ ] The tracking issue names an owner, the reason for the experimental path, and a graduation plan with a target release.
- [ ] Core changes are limited to a thin entry point (signature, shared validation, feature-gate check, backend selection, handoff).
- [ ] Tests live in
tests/experimental/and were validated on the intended hardware; a runnable example is included.- [ ] Nothing is registered in
flashinfer/aot.py, and no experimental backend is reachable frombackend="auto"withoutFLASHINFER_ALLOW_EXPERIMENTAL_AUTO_BACKENDS=1. (Calling an@flashinfer_experimental_apior naming a backend explicitly is itself the opt-in and needs no environment variable.)- [ ] Test scope declared below. The experimental CI lane runs exactly these targets, so keep them as narrow as the change allows.
<!-- Required for experimental PRs. Replace the commented lines below with your targets.
Do not delete the fence or change its
experimental-teststag — the experimental-trackwatcher reads it verbatim to decide which targets to ask CI for. -->
``
experimental-tests
`One target per line: a directory or a file. (A pytest ::selector is not
supported -- the sharding runner cannot consume one.) Must be under
tests/experimental/ and must exist. Delete these comment lines and add yours, e.g.
# tests/experimental/test_my_backend.py
tests/experimental/my_backend/
# Declaring the whole tree (tests/experimental/) is allowed but means every
experimental PR pays for every other feature's tests, in every matrix cell.
Reviewer Notes
andruff format --checkon the changed files instead, and both pass.: both reducer waits are now warp-uniform, and the change applies to every kernel that uses the split reducer. The win comes from ptxas no longer wrapping each MMA in a divergence guard; check withcuobjdump -sass(noBRA.DIVnext toUTCMMA`). On the non-D512 kernels I measured, timing moved by 0 to 0.8%.🤖 Generated with Claude Code
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
<!-- coderabbit-ai-generated:v1 -->
Summary by CodeRabbit
New Features
Added D512 paged attention support for FP8 and NVFP4 key/value formats with supported output types.
Expanded grouped-Keeps support for eligible D512 query-to-key/value head ratios, including split-reduction modes.
Added NVFP4 execution options for supported D512 profiles, including packed staging and even-then-odd packed K/V ordering.
Documentation
Updated D512 guidance with supported configurations, execution modes, restrictions, and fallback behavior.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->