[None][perf] Fuse MiniMax-M3 MSA per-layer KV-cache writes into one kernel - #18614
[None][perf] Fuse MiniMax-M3 MSA per-layer KV-cache writes into one kernel#18614zheyuf wants to merge 1 commit into
Conversation
…ernel Port of NVIDIA#16755 from feat/m3_with_msa to main. Each MSA layer wrote its new-token main K, main V and (sparse layers) index-K through three separate aten advanced-indexing writes, each with its own division / remainder / cast preprocessing: ~12 tiny launches per sparse layer, ~720 per decode step at 60 layers, all captured into the decode CUDA graphs. Replace them with one Triton launch per layer that derives (page, within-page) from out_cache_loc in-register and writes K, V and index-K together before the indexer's proxy pass. Layouts the kernel cannot take fall back to the legacy per-cache writes. Conflict resolution against main only: run_indexer keeps main's strict indexer_kv_dtype validation and gates the bf16 index-K write on idx_k_prewritten; the model layer keeps SparseBackendForwardArgs. Signed-off-by: Zheyu Fu <zheyuf@NVIDIA.com> (cherry picked from commit 3c85c6b) Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
|
/bot help |
GitHub Bot Help
Provide a user friendly way for developers to interact with a Jenkins server. Run See details below for each supported subcommand. Details
Launch build/test pipelines. All previously running jobs will be killed.
kill
Kill all running builds associated with pull request. skip
Skip testing for latest commit on pull request. reuse-pipeline
Reuse a previous pipeline to validate current commit. This action will also kill all currently running builds associated with the pull request. IMPORTANT NOTE: This is dangerous since lack of user care and validation can cause top of tree to break. |
|
/bot run --disable-fail-fast |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: 📒 Files selected for processing (5)
Included review availability: Your plan provides up to 12 included reviews per hour; 11 remain after this review. WalkthroughChangesThe MiniMax-M3 MSA path now performs fused paged K/V and optional index-K cache writes. Metadata tracks prewritten layers so indexer and attention paths skip duplicate writes. CUDA-gated tests compare fused and reference writes across cache dtypes, head counts, and strided layouts. MiniMax-M3 fused cache write flow
Estimated code review effort: 3 (Moderate) | ~20 minutes Merge Risk: 🔵 Low · up to The PR consolidates per-layer K/V/index-K cache writes into a fused operation, improving decode performance while preserving the established fallback for unsupported layouts. If the fused write fails after partially updating cache state, recovery and slot reuse are not fully established, so merge is appropriate with explicit owner follow-up on failure handling. Sequence Diagram(s)sequenceDiagram
participant MiniMaxM3Model
participant MiniMaxM3MsaSparseAttention
participant fused_write_layer_caches
participant PagedCaches
participant run_indexer
MiniMaxM3Model->>MiniMaxM3MsaSparseAttention: provide K/V and optional index-K
MiniMaxM3MsaSparseAttention->>fused_write_layer_caches: write layer caches
fused_write_layer_caches->>PagedCaches: store K/V and index-K
MiniMaxM3MsaSparseAttention->>run_indexer: run with idx_k_prewritten
run_indexer->>PagedCaches: skip duplicate BF16 index-K write
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Full details: Description checkExplanation The description clearly explains the problem, solution, performance impact, implementation flow, and checklist status. It omits the dedicated Test Coverage heading, but the overall description is sufficiently complete and references the relevant validation context. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
|
PR_Github #71089 [ run ] triggered by Bot. Commit: |
|
PR_Github #71089 [ run ] completed with state |
Dev Engineer Review
run_indexerAPI now acceptsidx_k_prewritten.QA Engineer Review
test_fused_scatter_matches_reference.tests/integration/test_lists/coverage was identified.Description
Port of #16755 (merged on Minimax-m3 side branch for AgentX submission) to
main. Same change, re-applied on top of main's MSA backend.Where this PR targets at:
MiniMax-M3 MSA per-layer KV-cache writes fusion (K, V, index-K):
Take a sparse layer as example:
The problem
Each MSA layer writes its new-token main K, main V, and (sparse layers) index-K. Writing of K, V and index-K takes four tiny kernels: division, remainder, index cast, index_put scatter. So that's 3*4=12 kernel launches per sparse layer. At 60 layers that is ~720 tiny kernels per decode step, all captured into decode CUDA graphs and re-executed on every step. It accumulates to large launch overhead.
The fix
This PR replaces them with one Triton launch per layer (~720 → 60) launches on the write path each iter).
Measured impact (4x B300, InferenceMAX-style serving benchmark)
trtllm-serve+benchmark_serving, NVFP4 + fp8 KV cache, MSA, decode CUDA graphs + overlap scheduler, no spec decode, random 8k/1k, identical seeded prompt sets, JIT/autotuner warmed.Nsys trace for a layer (look at green parts)
Before fix: 4 tiny kernels-to-be-fused for index-K, then 3 kernels for indexer, then 8 tiny kernels-to-be-fused for K and V.

After fix: 1 fused Triton launch (contains previous 12 kernels for K, V and K-index), then 3 kernels for indexer. So it's fuse + reorder: compute the page/offset once, write K + V + index-K together, before the indexer runs.

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-compatibleorapi-breaking. Forapi-breaking, includeBREAKINGin 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.