Skip to content

[None][perf] Pick the full-attention decode KV split per launch in FlashInfer trtllm-gen FMHA - #20934

Merged
Tabrizian merged 2 commits into
NVIDIA:feat/musespark-open-agentxfrom
Tabrizian:user/itabrizian/musespark-decode-kv-split
Oct 27, 2026
Merged

Tabrizian merged 2 commits into
NVIDIA:feat/musespark-open-agentxfrom
Tabrizian:user/itabrizian/musespark-decode-kv-split

Conversation

@Tabrizian

@Tabrizian Tabrizian commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

@coderabbitai summary

Description

At long context the sequence lengths in a decode batch differ widely. The trtllm-gen generation kernel splits each sequence's KV over smCount / (batch x kvHeads) CTAs, one CTA per SM. That split assumes equal lengths. A Muse Spark Open attention-DP batch of 6 or more sequences (16 KV heads per rank) therefore gets one CTA per (sequence, KV head). The launch then lasts as long as the one CTA streaming the longest sequence, at about 40 GB/s. At 1M context the full-attention decode FMHA ran at 1.5-2.2 TB/s on GB300 and took 12-27 ms of a 54 ms DEP8 decode step.

This PR reports k x the SM count to unwindowed decode launches, so the kernel runs about k x more KV splits in k waves through its MultiCtasKv path. Sliding-window layers keep the kernel's own split.

  • k = ceil(36 x batch x sqrt(kvHeads per rank) / smCount), capped at 8 and at what the FMHA workspace holds for the head dim. With the 32 MiB workspace that cap is 8 at head dim 64, 6 at 128 and 3 at 256.
  • TRTLLM_GEN_DECODE_SM_SCALE=k forces one k for every launch. TRTLLM_GEN_DECODE_SM_SCALE=1 restores today's behavior.
  • Grouped launches (TRTLLM_GEN_DECODE_SEQS_PER_LAUNCH, [None][feat] Muse Spark Open modeling_v2 TEP target: mid-high-concurrency optimizations for Vera Rubin #20844) pick k per group.
  • The multi-CTA counter buffer is sized for 8x the SM count.
  • FlashInfer's decode API takes no SM count. _trtllm_gen_decode() therefore swaps flashinfer.decode.get_device_sm_count for the one call, and makes no swap when k is 1.
  • When the modeling_v2 TEP target has replaced flashinfer for a call (its decode_kv_splits and split-KV stand-ins), that target's SM count stands. Its behavior is unchanged.

How k was chosen

The rule was fit on GB300 (152 SMs) with the FlashInfer trtllm-gen decode at Muse Spark Open shapes: FP8 Q/KV, head dim 64, 8 query heads per KV head, 8 query tokens per sequence (DFlash d7). The CUDA graph was captured at max_seq_len 1M, as serving captures it. The sweep timed k = 1-8 for 1-64 sequences at 1, 2, 4 or 16 KV heads per rank (TEP16 / TEP8 / TEP4 / DEP), 1950 launches in all.

Each shape got four batches drawn from 1M-context agentic first-turn lengths (lognormal, median 142k, mean about 190k) and one worst case (a 1M sequence among 142k ones). The best k was about batch at 16 KV heads, batch / 2 at 4, batch / 3 at 2 and batch / 4 at 1. More splits shorten the longest sequence's CTAs, but every CTA pays a fixed cost, and a rank with few KV heads streams little KV per launch.

Average slowdown against the best k for each batch:

rule sampled batches worst cases
this PR 2.4 % 6.0 %
fixed k = 8 (DEP) / 4 (TEP) 26.7 % 17.0 %
today (kernel's own split) 124 % 267 %

All outputs are within 7.6e-4 of the kernel's own split (BF16 reduction of the partial results).

Serving results

Muse Spark Open, GB300, 1M context, gen-only, MegaMoE. These runs used the same logic as an overlay of this file on the feat/musespark-open image.

run before, ms per step after, ms per step
DEP32 d7 bs8, k = 8 for every batch 58.5 35.7
DEP8 d7 bs6, k = 8 for every batch 54.5 34.9
DEP8 d7 bs6, forced k = 8 → per-batch k, back to back 34.57 34.31

In the per-batch run the ranks chose k = 1, 2, 4 and 6 for CUDA-graph batches 1, 2, 4 and 6. GSM8K through the DEP8 gen path with k = 8 scored 0.9409.

Impact and risk

  • This is on by default for full-attention decode on the FlashInfer trtllm-gen path, for every model that uses it. The fit used Muse Spark Open shapes at 1M context. Other head dims, GQA ratios and short contexts were not timed. TRTLLM_GEN_DECODE_SM_SCALE=1 restores today's behavior.
  • The code in this PR has not run on a GPU yet. The measurements above used the same rule in an overlay on the older feat/musespark-open image. This version differs in two ways: the cap now comes from the workspace size and head dim (still 8 at head dim 64), and grouped launches are handled.

Test Coverage

tests/unittest/_torch/attention/test_flashinfer_trtllm_gen_fmha.py:

  • test_decode_sm_scale covers the per-batch rule, sliding windows, the env override and the workspace cap by head dim.
  • test_decode_sm_scale_rejects_bad_env covers an invalid TRTLLM_GEN_DECODE_SM_SCALE value.
  • test_trtllm_gen_decode_reports_scaled_sm_count checks that the launch sees the scaled SM count and that the real count is restored, including when the launch raises. It also checks that a swapped-in flashinfer keeps its own count.
  • test_flashinfer_decode_reads_sm_count checks that the installed FlashInfer still reads get_device_sm_count from flashinfer.decode. Without that, the split would silently do nothing.

PR Checklist

Please review the following before submitting your PR:

  • PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.

  • PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.

  • Test cases are provided for new code paths (see test instructions)

  • If PR introduces API changes, an appropriate PR label is added - either api-compatible or api-breaking. For api-breaking, include BREAKING in the PR title.

  • Any new dependencies have been scanned for license and vulnerabilities

  • CODEOWNERS updated if ownership changes

  • Documentation updated as needed

  • Update tava architecture diagram if there is a significant design change in PR.

  • The reviewers assigned automatically/manually are appropriate for the PR.

  • Please check this after reviewing the above items as appropriate for this PR.

GitHub Bot Help

To see a list of available CI bot commands, please comment /bot help.

…ashInfer trtllm-gen FMHA

The trtllm-gen generation kernel splits each sequence's KV over
smCount / (batch x kvHeads) CTAs, one per SM, so at long context a decode
launch lasts as long as the CTA streaming the longest sequence. Report
k x the SM count to unwindowed decode launches, with
k = ceil(36 x batch x sqrt(kvHeads) / smCount), capped at 8 and at what the
FMHA workspace holds for the head dim. TRTLLM_GEN_DECODE_SM_SCALE=k forces
one k (1 keeps the kernel's own split). Grouped launches
(TRTLLM_GEN_DECODE_SEQS_PER_LAUNCH) pick k per group.

Signed-off-by: Iman Tabrizian <[email protected]>
@Tabrizian
Tabrizian requested a review from a team as a code owner October 24, 2026 06:09
@Tabrizian
Tabrizian requested review from kris1025 and yunruis October 24, 2026 06:09
…to swapped flashinfer

Replace the SM-count context manager with _trtllm_gen_decode(): no patch when
k is 1, and a flashinfer that another caller swapped for the call (the
modeling_v2 decode stand-ins) keeps that caller's SM count. Clamp k once.
Test that flashinfer's decode still reads get_device_sm_count.

Signed-off-by: Iman Tabrizian <[email protected]>
@Tabrizian

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #80397 [ run ] triggered by Bot. Commit: 0989032 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #80397 [ run ] completed with state SUCCESS. Commit: 0989032
/LLM/main/L0_MergeRequest_PR pipeline #66173 completed with status: 'SUCCESS'

CI Report

Link to invocation

@Tabrizian
Tabrizian merged commit dc753d7 into NVIDIA:feat/musespark-open-agentx Oct 27, 2026
9 checks passed
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.

2 participants