Skip to content

Fix SM120 DSV4 topk-256 decode dispatch - #3923

Closed
voipmonitor wants to merge 1 commit into
flashinfer-ai:mainfrom
voipmonitor:codex/sm120-dsv4-topk256-20260710
Closed

voipmonitor wants to merge 1 commit into
flashinfer-ai:mainfrom
voipmonitor:codex/sm120-dsv4-topk256-20260710

Conversation

@voipmonitor

@voipmonitor voipmonitor commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Add the missing SM120 DSV4 sparse-MLA decode instantiations for topk=256 and 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=256 decode 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, and 64). 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 --check
  • python3 -m py_compile flashinfer/mla/_sparse_mla_sm120.py tests/attention/test_sparse_mla_sm120.py
  • Added (num_heads=32, topk=256) to the existing SM120 DSV4 decode matrix.
  • Confirmed the dispatch is present in the image used for TP2 DSpark end-to-end validation.

Summary by CodeRabbit

  • New Features

    • Added SM120 sparse MLA decode support for TOPK=256 across 8, 16, 32, and 64 attention heads.
  • Bug Fixes

    • Unsupported decode configurations now return a clear error instead of silently using a fallback path.
  • Tests

    • Added coverage for the 32-head, TOPK=256 configuration.

@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b0f89b29-2906-4bad-a2a7-a0070e1cc988

📥 Commits

Reviewing files that changed from the base of the PR and between 422f6c9 and e939be5.

📒 Files selected for processing (3)
  • csrc/sparse_mla_sm120_decode_dsv4.cu
  • flashinfer/mla/_sparse_mla_sm120.py
  • tests/attention/test_sparse_mla_sm120.py

📝 Walkthrough

Walkthrough

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

Changes

SM120 DSV4 TOPK 256 support

Layer / File(s) Summary
CUDA dispatch expansion
csrc/sparse_mla_sm120_decode_dsv4.cu
Adds DSV4 dispatch branches for (8,256), (16,256), (32,256), and (64,256), and updates the adjacent TOPK documentation.
Python routing and validation
flashinfer/mla/_sparse_mla_sm120.py, tests/attention/test_sparse_mla_sm120.py
Adds matching Python dispatch entries, raises RuntimeError for unsupported decode shapes, and tests (32,256).

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related issues

  • flashinfer-ai/flashinfer issue 3828 — Covers the missing DSV4 TOPK=256 CUDA dispatch and Python routing addressed here.

Possibly related PRs

Suggested reviewers: saltyminty, sricketts, aleozlx, yzh119, cyx-6, kahyunnam, nv-yunzheq

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
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: adding SM120 DSV4 topk-256 decode dispatch support.
Description check ✅ Passed The description covers what changed, why, and how it was validated, though it omits the template's related issues and checklist sections.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@voipmonitor

Copy link
Copy Markdown
Contributor Author

Closing in favor of the earlier and broader #3817, which covers the same DSV4 topk=256 decode dispatch, includes NUM_HEADS=128, tests, and the unsupported-shape guard. The release image will use #3817 together with prefill fix #3896.

bkryu pushed a commit that referenced this pull request Aug 8, 2026
## 📌 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants