← Back to Pull requests

perf(prims-ts): faster D512 GQA decode for NVFP4 and FP8 KV #9771

Closed
unknown closed this pull request without merging on Nov 24, 2026 01:31 UTC
View on GitHub
unknown unknown commented on Nov 20, 2026 11:31 UTC
Author

<!-- .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):

  • Stage each K or V tile once as a packed NVFP4 payload (all head-dim slices, scale rows once) instead of one padded raw stage per D128 slice.

  • Use one transformed TMEM ring slot per K or V tile (four head-dim stages) and one converter warp group.

  • Issue a slot's MMAs from one elected block.
  • q_len 8 (grouped Keeps Q64/KV128):

  • Auto-select Keeps for D512 NVFP4 K/V at eight Q heads per KV head; before, only Swaps was considered. The same kernel also runs FP8-E4M3 K/V, which is now a supported D512 profile for that head ratio. Other head ratios keep their current D512 behavior.

  • Kernel changes:

  • - 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.
  • Allow the standalone split reducer (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.e4m3x4 form, 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 padded e2m1p4x4 form, 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 via use_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 every tcgen05 MMA of the MMA warp in BRA.DIV/ELECT/VOTEU.ANY/R2UR.BROADCAST. The fixes:

  • The ticket is taken by a whole warp: a lane-0 atomic, a shuffle, and a uniform warp-wide spin.

  • Every correction thread waits on the cluster mbarrier itself.
  • 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.py in 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

  • I have installed pre-commit by running pip install pre-commit (or used your preferred method).

  • I have installed the hooks with pre-commit install.

  • I have run the hooks manually with pre-commit run --all-files and fixed any reported issues.
  • If you are unsure about how to set up pre-commit, see the pre-commit documentation.

    🧪 Tests

  • Tests have been added or updated as needed.

  • All tests are passing (unittest, etc.).

  • This PR requires full-scope CI, not targeted tests (expensive; justify below).

  • Reason: <!-- e.g. cross-cutting change to shared infrastructure -->

    Suggested CI test targets (optional; paths under tests/, whitespace-separated — a starting
    point for @flashinfer-bot run <targets> / /bot run <targets>, reviewers may adjust): tests/attention/test_attention_ts_decode.py

    New 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.

  • D512 Keeps rows in test_attention_ts_decode_large_tile_size_q with the separate reducer.

  • Review follow-up (host-only config tests): 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.

  • Dense FP4 multiply follow-up: test_attention_ts_decode_nvfp4_d512_even_odd_order_opt_in runs 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):

  • Full tests/attention/test_attention_ts_decode.py: 1194 passed, 96 skipped. The 32 failures in that run were addressed:

  • - 28 q_token_sparse tests 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 lacks cuda::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.
  • Rerun of the failed groups after the fixes (-k "q_token_sparse or d512 or large_tile_size_q"): 159 passed, 96 skipped, 0 failed on each GPU.

  • After the review fixes (9f58f96): the full file gave 1227 passed and 96 skipped on each GPU. The one failure was a new host test, fixed in the same commit. After that fix, the D512, whole-tile and large-tile subset gave 132 passed and 96 skipped on B300.

  • Dense FP4 multiply (479a5d531): the full file gave 1229 passed, 96 skipped and 1 failed on each GPU. The failure was the new opt-in test's q_len 1 case, where the large-tile helper's LSE reference expected 3-D Q per request; the kernel itself ran. After the helper fix, the -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. -->

  • This PR is experimental: it adds or changes code under 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 from backend="auto" without FLASHINFER_ALLOW_EXPERIMENTAL_AUTO_BACKENDS=1. (Calling an @flashinfer_experimental_api or 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-tests tag — the experimental-track
    watcher 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

  • Pre-commit could not run on my machine. I ran ruff check and ruff format --check on the changed files instead, and both pass.

  • The D512 Keeps auto policy and the FP8 K/V D512 profile are limited to eight Q heads per KV head, the only ratio measured. Other D512 shapes keep the current selection.

  • tmem_corr.py: 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 with cuobjdump -sass (no BRA.DIV next to UTCMMA`). 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 -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: qsang-nv Review duration: 5160m
    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
    0.57
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 57% (MEDIUM) · Higher = larger blast radius
    57%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.5% · Safe to merge
    22.5%