Skip to content

[DSV4] Support raw-index output in TopK v2 - #33672

Merged
ch-wan merged 3 commits into
sgl-project:mainfrom
weireweire:perf/dsv4-topk-v2-raw-output
Sep 11, 2026
Merged

ch-wan merged 3 commits into
sgl-project:mainfrom
weireweire:perf/dsv4-topk-v2-raw-output

Conversation

@weireweire

@weireweire weireweire commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Allow TopK v2 to populate a raw-index output buffer. DSV4 sparse prefill internally allocates c4_sparse_raw_indices even when --enable-return-indexer-topk is disabled. The previous raw_indices is None gate therefore forced the InfX sparse-prefill path to fall back to TopK v1.

When --enable-return-indexer-topk is enabled, the capture-only temporary buffer could additionally take precedence over the functional sparse-prefill buffer. This change makes the sparse-prefill buffer canonical, passes it through TopK v2, and lets the capturer copy the same result afterward.

Changes

  • Pass the raw-output buffer to topk_transform_512_v2.
  • Prioritize core_metadata.c4_sparse_raw_indices; the capturer reads the same result afterward.
  • Add a regression test covering TopK v2 with sparse prefill and capture enabled.

No CUDA kernel, ABI, metadata format, default, or HiSparse behavior changes.

Related: #32771 contains the same buffer-ordering change; #33288 owns chunked-path metadata refresh.

Testing

  • DSV4 indexer unit tests: 8 passed, 12 subtests passed
  • TopK v2 GPU subset on GB300: 36 passed, 208 deselected
  • pre-commit run --all-files

A prior GB300 microbenchmark (8,192 queries, max C4 length 2,048, topk=1024, metadata planning excluded) measured 0.1023 ms for v1 and 0.0646 ms for v2 (36.9% lower kernel latency). Should have bigger influence on long prefix as the indexer will take more time.


CI States

Latest PR Test (Base): ⏳ Run #34427908088
Latest PR Test (Extra): ✅ Run #34427908071
Latest PR Test (AMD ROCm 10): ❌ Run #34427908194

@weireweire

Copy link
Copy Markdown
Contributor Author

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci label Aug 6, 2026
@weireweire
weireweire force-pushed the perf/dsv4-topk-v2-raw-output branch from 22bfab0 to 8787445 Compare August 6, 2026 09:33
@b8zhong

b8zhong commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/unit/layers/test_dsv4_nonpaged_indexer.py

@github-actions

github-actions Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/unit/layers/test_dsv4_nonpaged_indexer.py:

test/registered/unit/layers/test_dsv4_nonpaged_indexer.py: Dispatch failed: 422

@b8zhong

b8zhong commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

@weireweire Is this for InfX? I'm wondering why you using --enable-return-indexer-topk (unless its for RL or something)

@weireweire weireweire changed the title [DSV4] Route raw-output requests through TopK v2 [DSV4] Support raw-index output in TopK v2 Aug 13, 2026
@weireweire

Copy link
Copy Markdown
Contributor Author

@b8zhong it's for infX. Even without --enable-return-indexer-topk, sparse prefill for dsv4 will also internally require raw indices, I updated the pr description, sorry for the confusion.

@weireweire
weireweire force-pushed the perf/dsv4-topk-v2-raw-output branch from 8787445 to 948829f Compare August 13, 2026 02:57

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

Copy link
Copy Markdown
Contributor Author

/tag-and-rerun-ci extra

Leoyzen added a commit to Leoyzen/sglang that referenced this pull request Aug 19, 2026
Branch-local port of sgl-project#33672 (open upstream). The v2
kernel signature, c4_sparse_raw_indices internal allocation, and capturer
buffer reuse already existed on this branch; the missing piece was the
dispatch gate in _run_topk_transform plus buffer-priority ordering.

- Prioritize core_metadata.c4_sparse_raw_indices over the capture-only
  temporary buffer (sparse-prefill buffer is canonical; the capturer
  copies the same result afterward instead of stealing it).
- Drop the raw_indices-is-None gate on the TopK v2 path so sparse prefill
  no longer falls back to TopK v1; pass the raw-output buffer through.
- Chunked path keeps the existing topk_metadata regeneration guard.
- Test adaptions vs upstream: get_parallel patched (CP group
  uninitialized in unit tests); fake_v2 signatures take the 7th
  raw_output positional arg.

GB300 microbenchmark (8,192 queries, max C4 2048, topk=1024): v2 kernel
0.0646 ms vs v1 0.1023 ms (-36.9%), larger wins on long prefixes.
@weireweire
weireweire force-pushed the perf/dsv4-topk-v2-raw-output branch from 3c7ecbb to c9bd7c6 Compare August 27, 2026 07:20
@b8zhong

b8zhong commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

@weireweire Please fix conflicts

@weireweire

Copy link
Copy Markdown
Contributor Author

@b8zhong conflict fixed

Comment thread python/sglang/kernels/jit/csrc/deepseek_v4/topk_v2.cuh

@DarkSharpness DarkSharpness left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. I just wonder in which cases do we need both page & the topk indices?

@weireweire

Copy link
Copy Markdown
Contributor Author

@DarkSharpness thanks, both sparse prefill and --enable-return-indexer-topk need raw indices, while normal prefill need paged indices. So for your question it's when --enable-return-indexer-topk and normal prefill are both open.

Root cause: TopK v2 cannot emit raw and paged indices together.

Fix: Add dual-output kernel dispatch and preserve the sparse-prefill output buffer when capture is enabled. Rebase the complete PR patch onto current main.

Validation: Full pre-commit checks passed. Stable patch-id matches the original PR patch; git diff --check passed.
@weireweire
weireweire force-pushed the perf/dsv4-topk-v2-raw-output branch from 362ad8e to a103b0d Compare September 9, 2026 05:56
@weireweire

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@b8zhong

b8zhong commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test registered/rl/test_return_indexer_topk.py registered/kernels/ops/attention/test_topk_v2.py registered/unit/layers/test_dsv4_nonpaged_indexer.py

@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Results for /rerun-test registered/rl/test_return_indexer_topk.py registered/kernels/ops/attention/test_topk_v2.py registered/unit/layers/test_dsv4_nonpaged_indexer.py:

🚀 8-gpu-h200 (1 test): ✅ View workflow run

cd test/ && python3 registered/rl/test_return_indexer_topk.py

🚀 1-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/kernels/ops/attention/test_topk_v2.py

🚀 ubuntu-latest (1 test): ✅ View workflow run

cd test/ && python3 registered/unit/layers/test_dsv4_nonpaged_indexer.py

@weireweire

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@weireweire

Copy link
Copy Markdown
Contributor Author

could we merge this?

@ch-wan
ch-wan merged commit 335f6aa into sgl-project:main Sep 11, 2026
263 of 305 checks passed
DarkSharpness added a commit that referenced this pull request Sep 11, 2026
Co-authored-by: weireweire <20922698+weireweire@users.noreply.github.com>
trilog-inc pushed a commit to trilog-inc/sglang that referenced this pull request Sep 11, 2026
…) (sgl-project#39098)

Co-authored-by: weireweire <20922698+weireweire@users.noreply.github.com>
(cherry picked from commit da64c5c)
mqhc2020 pushed a commit to mqhc2020/sglang that referenced this pull request Sep 15, 2026
Co-authored-by: weireweire <20922698+weireweire@users.noreply.github.com>
Co-authored-by: Brayden Zhong <b8zhong@uwaterloo.ca>
Co-authored-by: Po-Han Huang (NVIDIA) <53919306+nvpohanh@users.noreply.github.com>
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.

6 participants