Skip to content

feat(topk): index the page table per row so prefill can use the fused transform (2.86x) - #3

Merged
jiejingzhangamd merged 2 commits into
mainfrom
feat/dsa-topk-spec-rows
Aug 28, 2026
Merged

jiejingzhangamd merged 2 commits into
mainfrom
feat/dsa-topk-spec-rows

Conversation

@jiejingzhangamd

@jiejingzhangamd jiejingzhangamd commented Aug 27, 2026 •

Copy link
Copy Markdown
Collaborator

What

dsa_topk_transform (#2) indexes the page table by logits row, which assumes one row per sequence. Decode satisfies that. Prefill does not, and prefill is where this matters.

sglang's PAGED prefill hands topk_transform a chunk of token rows against a page table with one row per sequence — measured on a real GLM-5.2 + EAGLE run at 100k context, that is rows 4..8192 (median 2452) against a table of 1-3 rows. It also passes batch_idx_list = token_to_batch_idx, and the sgl-kernel path spends it on a materialised gather:

page_table_size_1 = attn_metadata.page_table_1[batch_idx_list]

At the median shape that gather expands a 400 KB table into 1.00 GB, per chunk, per indexer layer. GLM-5.2 runs the full indexer in 21 layers.

This adds an optional ptRowMap, [numRows] int32, giving each logits row its page-table row, so the kernel indexes the table instead of the caller materialising it. Passing null keeps the identity mapping, so decode is byte-for-byte unchanged and passes nothing.

Measured

MI355X (gfx950), ctx=102400, k=2048, page_table_1[batch_idx] + fast_topk_transform_fused vs dsa_topk_transform(..., ptRowMap=batch_idx):

rows seqs gathered table gather sgl-kernel sgl total aiter+map speedup
256 1 0.10 GB 22.3 us 100.9 us 122.2 us 47.6 us 2.57x
1024 1 0.42 GB 114.6 us 457.0 us 594.1 us 202.3 us 2.94x
2048 1 0.84 GB 223.4 us 899.6 us 1156.2 us 395.9 us 2.92x
2452 1 1.00 GB 253.1 us 1117.6 us 1399.1 us 489.9 us 2.86x
4096 1 1.68 GB 343.9 us 1773.3 us 2162.0 us 776.6 us 2.78x
8192 1 3.36 GB 668.9 us 3478.3 us 4215.7 us 1522.2 us 2.77x
2452 2 1.00 GB 247.8 us 1116.4 us 1394.0 us 494.7 us 2.82x
2452 3 1.00 GB 239.9 us 1114.6 us 1385.5 us 499.3 us 2.78x

2.77-2.94x across the whole observed range, all with zero index mismatch against the sgl-kernel result. The gather alone is 16-21% of the sgl-kernel path; the rest of the win is the fused transform that #2 already established for decode, which prefill could not reach before.

The transient allocation is worth noting separately from the time: at rows=8192 the gather asks the allocator for 3.36 GB, per layer, during prefill — the phase that is already the memory-tightest.

Why an indirection rather than an expanded table

That is the expanded table, and the numbers above are what it costs. Inside the kernel the alternative is one extra indexed load per row, in the tail pass, into a few KB of L2-hot table where the page lookup already lives.

Change

  • CoopTopKParams / CoopMbParams gain pt_row_map, and pt_row_of() resolves row -> page-table row.
  • The three sites that index the table (single-block kernel, the multi-block short-row path in coop_mb_hist0, and coop_mb_refine) go through it.
  • launch, the C ABI and the Python binding thread the pointer through; every layer defaults to null.

Validation

op_tests/test_dsa_topk_transform.py, 13 cases, all passing on MI355X: decode identity rows at 1 / 4 / 32 sequences; expanded rows at 1 / 4 / 32 sequences x 2 / 4 rows each, with per-row lengths differing within a sequence; page_size 1 and 64; short and empty rows padding with -1.

Every case runs against a deliberately shuffled page table whose sequences occupy disjoint physical ranges, so an identity or off-by-one mapping cannot pass by luck. test_spec_rows_reject_wrong_table asserts the mapped and unmapped results differ, which keeps a silently-ignored map from looking like a pass.

Note on the original framing

This PR was first opened claiming MTP was excluded from the fused path and that ptRowMap would let it in. That was wrong: sglang pre-expands the page table for target-verify (repeat_interleave on the eager path, [:bs * draft, :] under CUDA graph), so MTP shapes already pass the gate. Instrumenting the real gate confirmed it — GATEPROBE PASS rows=1024 lens=1024 pt=1024. The same instrumentation is what turned up prefill, where rows and page-table rows genuinely differ and nothing was expanding anything.

What actually blocked --dsa-topk-backend aiter under MTP was a missing case in sglang's _get_fused_topk_page_table, fixed separately; with that fix MTP + aiter measures 6.15 ms vs 6.44 ms median ITL at concurrency 8 (4.5%), from the decode path alone.

Follow-up

The matching sglang change widens the PAGED gate to accept prefill shapes and forwards batch_idx_list as ptRowMap instead of gathering with it. Without that, this parameter is inert.


Note on base. Retargeted from upstream-sync to main and rebased onto it. It now stacks on #6, which fixes the ctypes entry that #5 merged with — on this base that entry does not compile at all, so dsa_topk_transform is unbuildable on main until #6 lands. This PR therefore shows two commits until #6 merges, after which it collapses to its own.

Carried over from #6: ptRowMap is an std::optional<torch::Tensor> argument rather than a nullable aiter_tensor_t*. The kernel-side indirection is unchanged.

Testing. op_tests/test_dsa_topk_transform.py passes 13/13 on this base against a gfx950 MI355X.

The pre-rebase head is kept at backup/dsa-topk-spec-rows-20260827.

@jiejingzhangamd jiejingzhangamd changed the title feat(topk): let speculative-decode shapes use the fused page-table transform draft: ptRowMap for spec-decode rows (not needed for MTP enablement -- see body) Aug 27, 2026
@jiejingzhangamd
jiejingzhangamd marked this pull request as draft August 27, 2026 22:13
@jiejingzhangamd jiejingzhangamd changed the title draft: ptRowMap for spec-decode rows (not needed for MTP enablement -- see body) feat(topk): index the page table per row so prefill can use the fused transform (2.86x) Aug 27, 2026
@jiejingzhangamd
jiejingzhangamd marked this pull request as ready for review August 27, 2026 22:45
@jiejingzhangamd
jiejingzhangamd changed the base branch from upstream-sync to main August 28, 2026 05:13
@jiejingzhangamd
jiejingzhangamd force-pushed the feat/dsa-topk-spec-rows branch 2 times, most recently from 95fcd4b to d61bdae Compare August 28, 2026 05:42
jiejingzhangamd and others added 2 commits August 28, 2026 05:45
dsa_topk_transform (#5) landed with a ctypes entry, which does not compile on
this base: registering any ffi_type="ctypes" op forces torch_exclude on the
whole module (aiter/jit/core.py), and three of module_top_k_per_row's four
sources still include torch/all.h. Calling aiter.dsa_topk_transform on main
today dies in the JIT build with

    csrc/include/py_itfs_common.h:6:10: fatal error: 'torch/all.h' file not found

The module has been in this state since before #5 -- main's own
top_k_per_row_decode_fast and top_k_per_row_prefill_fast fail identically, they
are just never called, so nothing reached the broken path. #5 added the first
ctypes entry on this module that callers actually use.

This binds the op through pybind instead: a torch::Tensor signature matching
the two entries beside it, the stream from getCurrentHIPStream, a declaration
in topk_per_row.h and an m.def in TOP_K_PER_ROW_PYBIND. The kernel and
coop_topk.cuh are untouched.

The binding costs nothing measurable. Against the ctypes build on the same
MI355X, GLM-5.2 decode logits (live=100001, page_size=1, k=2048), three runs
each:

    rows    ctypes us                pybind us
       1    30.01 / 27.13 / 28.22    24.97 / 25.35 / 24.93
       8    26.85 / 26.13 / 26.48    26.21 / 26.14 / 26.17
      64    39.65 / 40.15 / 39.27    40.08 / 39.45 / 40.07

pybind is never slower and batch 1 is consistently ~3.4us faster with no
overlap between the two sets, so the per-call binding cost #5 cited as the
reason for ctypes is not visible here.

The proper fix is the detorch migration of this module (ROCm#4702, ROCm#4756), after
which the ctypes form builds and this can go back if it ever measures better.

op_tests/test_dsa_topk_transform.py passes 13/13 on gfx950, and output stays
bit-identical to sgl_kernel's fast_topk_transform_fused at every shape.
…ansform

dsa_topk_transform indexes the page table by logits row, which assumes one row
per sequence. That holds for decode but not for speculative decoding: verify and
draft-extend produce several rows per sequence -- one per draft token -- against
a page table that still has one row each. sglang's gate therefore excluded those
shapes and sent them back to the unfused top-k plus tensor-op chain, so MTP paid
the very cost this op exists to remove.

Add an optional ptRowMap, [numRows] int32, giving each logits row its page-table
row. Null keeps the identity mapping, so decode is unchanged and passes nothing.

An indirection inside the kernel rather than an expanded table on the caller
side: at 100k context one page_size=1 row is ~400 KB, so materialising bs*draft
copies of it would move more bytes than the selection itself. The lookup already
lives in the tail pass, where it is a dependent scattered load into a few KB of
L2-hot table, and one more indexed load per row does not change that.

Tests drive both shapes against a deliberately shuffled page table with disjoint
physical ranges per sequence, so an identity or off-by-one mapping cannot pass by
luck; one case asserts the mapped and unmapped results actually differ, which is
what keeps the map from being silently ignored.

Co-authored-by: Cursor <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant