Fix SM120 DSV4 topk-256 decode dispatch - #3923
voipmonitor wants to merge 1 commit into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughSM120 DSV4 decode dispatch now supports TOPK=256 for 8, 16, 32, and 64 heads in CUDA and Python routing. Unsupported decode shapes raise an explicit error, and tests add coverage for the (32, 256) configuration. ChangesSM120 DSV4 TOPK 256 support
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related issues
Possibly related PRs
Suggested reviewers: Sequence Diagram(s)sequenceDiagram
participant _paged_attention
participant _DECODE_DSV4_DISPATCH
participant launch_sparse_mla_decode_dsv4
participant launch_decode_dsv4_impl
_paged_attention->>_DECODE_DSV4_DISPATCH: match num_heads and topk
_DECODE_DSV4_DISPATCH->>launch_sparse_mla_decode_dsv4: route supported TOPK=256 shape
launch_sparse_mla_decode_dsv4->>launch_decode_dsv4_impl: dispatch selected head count and TOPK=256
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request introduces support for a TOPK value of 256 in the SM120 sparse-MLA decode kernel for DeepSeek-V4 (DSV4), which accommodates DSpark's 128-token context and padded draft block. The changes update the CUDA dispatch macros, the Python dispatch set, and add a corresponding test case. Additionally, a runtime check is introduced to raise an error when num_tokens is less than or equal to _DECODE_MAX_TOKENS. There are no review comments, so no feedback is provided.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
## 📌 Description This PR consolidates the community work needed to run DeepSeek-V4-Flash-0731 with DSpark through the SM120/SM121 sparse MLA backend. A downstream DSpark configuration has 133 active sparse entries per query: 128 sliding-window entries plus 5 draft entries. The index tensor is padded to 192 because the kernel consumes 64-entry tiles. Other integrations use the 256 bucket. Neither shape was fully represented in the existing SM120 decode and prefill dispatch matrices, so decode-sized requests could miss the standalone kernel and fall through to the prefill-only orchestrator, which aborts on `num_tokens <= 64`. This change: - adds DSV4 decode instantiations for `topk=192` and `topk=256` with 8, 16, 32, 64, and 128 query heads; - adds DSV4 single-cache prefill dispatch for `topk=192` and `topk=256` using BF16 QK; - supports TP8 prefill by padding H8 into the existing 16-head MG tile while gating all global Q, sink, output, and LSE accesses; - enables the padded H8 MG path for both single-cache and dual-cache prefill, including both supported secondary-cache page sizes and runtime top-k lengths; - keeps the Python and CUDA dispatch matrices and user-facing documentation synchronized; - raises an actionable Python `ValueError` when an unsupported decode shape would otherwise enter the prefill-only kernel and abort the process; - fixes the sparse MLA benchmark KV cache view to match its default HND layout; and - expands correctness and benchmark coverage for the new dispatch shapes, active-length truncation, HND/NHD layouts, caller-owned workspaces, and output-buffer reuse. ## 🔍 Related Issues and Community PRs After coordination with the maintainers, this PR is intended as the single consolidated replacement for the overlapping open implementations: - #4309 — DSV4 `topk=192` decode/prefill and active-length coverage. - #4372 — broad DSV4 `topk=256` decode/prefill dispatch and public API coverage. - #3989 — combined DSpark `topk=256` fix and benchmark HND layout correction. - #3817 — DSV4 `topk=256` decode instantiations and decode-to-prefill fail-fast behavior. - #3834 — DSV4 `topk=256` BF16 prefill dispatch. It also covers the implementation scope of the earlier closed alternatives #3896 and #3923. Related reports include #3828, #3988, and #4336. The NVFP4 KV-cache extension in #3937 is a separate feature and is not superseded by this PR. ## 🚀 Pull Request Checklist ### ✅ Pre-commit Checks - [x] I used the repository-compatible `prek` hook runner. - [x] All hooks relevant to the changed files pass, including clang-format, mypy, ruff check, and ruff format. - [x] `git diff --check` passes. ## 🧪 Tests - [x] Tests have been added or updated as needed. - [x] `pytest -q tests/attention/test_sparse_mla_sm120.py`: 309 passed on SM120. - [x] `compute-sanitizer --tool memcheck` on representative H8 single-cache, dual-cache PBS64, dual-cache PBS2, and runtime-length cases: 4 passed, `ERROR SUMMARY: 0 errors`. - [x] The complete `benchmarks/bench_sparse_mla_sm120.py` sweep runs successfully, including the corrected HND path and H8/H16 single- and dual-cache cases. ## Reviewer Notes The main review focus is the padded H8 MG specialization. The kernel still computes a 16-head tile, but only the first 8 rows may touch caller-owned global tensors; the remaining rows are zero-filled in shared memory and excluded from sink, output, and LSE accesses. Tests cover sinks, active-length truncation, the public HND API, dual-cache full-tile and runtime-length paths, and both secondary-cache page sizes. AI tools were used to assist with implementation, testing, and PR preparation. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Expanded sparse MLA support for additional head counts and top-k values, including 192, 256, and 2048. * Added support for eight-head configurations and more prefill, token-count, page-size, and dual-cache scenarios. * Improved compatibility with HND and NHD layouts. * **Bug Fixes** * Unsupported decode shapes are now rejected before processing. * Improved truncation handling for top-k and extra top-k lengths. * **Tests** * Expanded coverage for layouts, buffer reuse, workspace, sequence lengths, decode, and prefill scenarios. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Co-authored-by: David Orman <ormandj@corenode.com> Co-authored-by: JimZhang-lab <60953754+JimZhang-lab@users.noreply.github.com> Co-authored-by: Albert Catalan Tatjer <ac.tatjer@gmail.com> Co-authored-by: carlosmolina0615 <carlosmolina0615@gmail.com> Co-authored-by: Wei-Cheng (Wayne) Chiu <waynehacking8@gmail.com> Co-authored-by: OsakaTX <OsakaTX@users.noreply.github.com> Co-authored-by: Martin Vit <martin@voipmonitor.org>
Summary
Add the missing SM120 DSV4 sparse-MLA decode instantiations for
topk=256and fail explicitly when a decode-sized DSV4 shape is unsupported instead of silently falling through to the generic paged-attention path.Why
DeepSeek-V4-Flash-DSpark can build non-causal sparse-attention metadata with a 128-token context plus a padded draft block. That produces a
topk=256decode bucket. The existing DSV4 dispatch table supports 128, 512, and 1024, so valid DSpark decode requests miss the specialized SM120 kernel.The added instantiations cover the DSV4 head counts used by TP1/TP2/TP4 (
8,16,32, and64). The Python dispatch table is kept in sync with the C++ switch.For decode-sized inputs, an unsupported shape now raises a diagnostic containing model type, token count, head count, top-k, QK width, page-block size, and extra top-k. Prefill-sized inputs retain the existing generic path.
Validation
git diff --checkpython3 -m py_compile flashinfer/mla/_sparse_mla_sm120.py tests/attention/test_sparse_mla_sm120.py(num_heads=32, topk=256)to the existing SM120 DSV4 decode matrix.Summary by CodeRabbit
New Features
Bug Fixes
Tests