Skip to content

[Feat] dsa: Kernel prerequisites for a DCP-sharded indexer - #48501

Merged
hnyls2002 merged 7 commits into
mainfrom
lsyin/indexer-dcp-prep
Nov 5, 2026
Merged

hnyls2002 merged 7 commits into
mainfrom
lsyin/indexer-dcp-prep

Conversation

@hnyls2002

@hnyls2002 hnyls2002 commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Original authors: @yhyang201, @BBuf, @nvpohanh, @kpham-sgl

Port of #48414

The Kimi-K3.1 Day-0 branch (#48414) shards the DSA indexer's index-K cache across the DCP ranks (dsa_dcp_sharded.py); on main, Kimi-K3.1 still rejects --dcp-size > 1. This PR ports the kernels that path needs, with tests and no caller yet:

  1. Store only the rank's own index-K slots.
  2. Select a local top-k whose candidates merge exactly across ranks.
  3. Build FlashMLA's split-KV schedule cheaply for the short per-rank lengths.

Against the raw port, tie order and the cluster path reuse #47960 instead of the branch's own versions (whose cluster path missed ties in the top coarse bin), and schedule / dcp_size / dcp_rank replace the branch's flags and names. Left for the sharded indexer PR: the schedule tests that need its backend code.

Modifications

  • Top-k candidates (topk_v2.cuh, topk.py)
    • topk_transform_candidates_v2: a ragged top-k that writes (fp32 score bits, local * dcp_size + dcp_rank) per selected position, (0, -1) padded, so the ranks' candidates merge into the global top-k. It shares the ragged selection (ragged_window_topk); rows past cluster_floor run the shared cluster top-k (TopKCluster), each rank emitting the candidates it selects. The cluster size is picked from the row count on SM90 / SM100, where the cluster path is built.
    • deterministic=True on the ragged, paged, packed and candidates entry points builds that call in top-k mode 2 ([Perf] [NVIDIA] dsv4/topk_v2: Exactness modes (fast / exact / deterministic) and an exact cluster path #47960), whatever SGLANG_TOPK_V2_MODE is: equal scores at the k-th place resolve to the smallest indices, which the merge relies on. The module name carries the mode, so both builds load in one process.
  • FlashMLA schedule metadata (flashmla_sched_meta.cuh/.py)
    • schedule="parallel": the partition walk by pointer doubling across the block, giving the same schedule as the serial walk.
    • schedule="windowed" (sparse only): request costs cut into equal windows; a valid schedule with the same bound on the costliest partition, computed in a few microseconds.
    • The default stays "serial"; either alternative falls back to it when it does not fit in shared memory.
  • Index-K store (fused_store_index_cache.cuh, dsa_fused_store_index_cache.py)
    • dcp_size / dcp_rank: with widened slots, a rank stores only its own slots, at row slot // dcp_size. Defaults keep the current behavior.
    • Negative locations are skipped instead of written.

Tests

  • test_topk_v2.py: candidates against the ragged top-k, with and without row starts; cluster against one block, including rows whose threshold lands in the highest coarse bin; index-ordered ties on the paged, ragged and packed paths in a process whose default mode is exact.
  • test_flashmla_sched_meta.py: the parallel walk reproduces the serial one bit for bit, and a replay whose lengths outgrow its workspace falls back; the windowed schedule covers every request's blocks exactly once, and FlashMLA gives the same attention on it as on its own schedule.
  • test_fused_store_index_cache.py: under DCP, owned, foreign and negative slots mixed; the buffer equals a plain store of the owned tokens at slot // dcp_size.

On one B300 GPU at this head: test_topk_v2.py 400 passed, 14 skipped; test_flashmla_sched_meta.py 242 passed; test_fused_store_index_cache.py 16 passed.

Speed

B300, each call replayed 50 times in a CUDA graph, best of 5 (us per call).

flashmla_sched_meta, 148 partitions, top-k 2048, block 64, overhead 5. Per-request lengths uniform in [1, 512] (a DCP8 rank) or all 2048:

lengths b serial (us) parallel (us) windowed (us)
1..512 4 4.13 4.14 3.25
1..512 8 5.53 6.08 3.28
1..512 32 10.78 6.02 3.61
1..512 256 25.21 9.84 3.90
2048 8 16.07 6.93 3.51
2048 64 18.18 11.09 3.68
2048 256 27.57 27.57 3.93

Below kMinParallelBatch = 8 the parallel schedule is the serial one; at b = 8 it is slower on short lengths and faster on full ones, and from b = 16 it is faster on both. At b = 256 with full lengths its tables do not fit, so it falls back to serial.

topk_transform_candidates_v2, top-k 2048, deterministic, randn scores, cluster_floor=0:

rows seq_len one block (us) cluster 2 (us) cluster 4 (us)
1 8192 4.39 4.91 4.39
1 16384 6.79 6.49 5.25
1 131072 28.14 16.68 10.71
16 16384 7.20 6.67 5.39
16 131072 28.80 17.03 11.00
64 8192 4.42 5.71 6.12
64 131072 29.38 17.31 16.73

The cluster path loses at 8192 and wins from 16384 on, which _CANDIDATES_CLUSTER_FLOOR = 16384 follows (rows up to it take one block).


CI States

Latest PR Test (Base): 🚫 Run #41982168957
Latest PR Test (Extra): ❌ Run #41982168861
Latest PR Test (AMD ROCm 10): 🚫 Run #41982168914

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@hnyls2002
hnyls2002 changed the base branch from main to lsyin/base-47960 November 5, 2026 02:52

@BBuf BBuf 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.

[BBuf Review Agent] One thing to fix before this can go in.

uint32_t n_gt = 0, n_eq = 0;
for_each_input<N>(problem.in, problem.seq_len, [&](float val, uint32_t) {
n_gt += val >= v_hi ? 1u : 0u;
n_eq += (val < v_hi && val >= v_lo) ? 1u : 0u;

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.

[BBuf Review Agent] For a 32768-element row with the last 2048 scores set to 70000 and the rest zero, v_hi is NaN, so this counts no ties and the cluster path returns the first 2048 positions even with deterministic=True. Could we use !(val >= v_hi) here, matching the staging pass, and add this case?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, confirmed: with v_hi NaN the counting pass dropped every tie, and your row returned the first 2048 positions on the old head. Rather than patch the count, the cluster candidates now run the shared TopKCluster from #47960, so this code is gone (68d97b2). Your case is in test_topk_v2_candidates_cluster_matches_one_block as top_bin; it fails on the old head and passes now.

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.

[BBuf Review Agent] The NaN case is covered now, but transform_candidates still lacks the shared selector's 20-bit index guard: with length 2^20+64, k=32 and the first and last 64 scores tied at 1, deterministic cluster selection picks the tail. Could we fall back to one block for longer rows or add the same guard as the paged entry point?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Confirmed: on the old head your row picked the tail. In mode 2, rows past 2^20 now take the one-block path, which has no index limit (50c4d24). Your case is test_topk_v2_candidates_cluster_long_row_ties; it fails on the old head and passes now. Could you take another look?

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.

[BBuf Review Agent] Checked this head: mode 2 routes rows longer than 2^20 through candidates_one_block, and the regression checks the smallest 32 indices for both cluster sizes. This addresses the index-limit concern.

yhyang201 and others added 2 commits November 4, 2026 19:26
Co-authored-by: Xiaoyu Zhang <[email protected]>
Co-authored-by: Po-Han Huang <[email protected]>
Co-authored-by: Khoa Pham <[email protected]>
@github-actions

This comment has been minimized.

@hnyls2002 hnyls2002 added the run-ci CI: run the baseline test suite on this PR label Nov 5, 2026
@hnyls2002

Copy link
Copy Markdown
Collaborator Author

/rerun-test test_topk_v2.py test_flashmla_sched_meta.py test_fused_store_index_cache.py

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

🚀 1-gpu-h100 (2 tests): ❌ View workflow run

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

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

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

@hnyls2002
hnyls2002 merged commit b283319 into main Nov 5, 2026
127 of 165 checks passed
@hnyls2002
hnyls2002 deleted the lsyin/indexer-dcp-prep branch November 5, 2026 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jit-kernel run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants