Repository navigation
feat(topk): index the page table per row so prefill can use the fused transform (2.86x) - #3
Merged
Merged
Conversation
jiejingzhangamd
marked this pull request as draft
August 27, 2026 22:13
jiejingzhangamd
marked this pull request as ready for review
August 27, 2026 22:45
jiejingzhangamd
force-pushed
the
feat/dsa-topk-spec-rows
branch
2 times, most recently
from
August 28, 2026 05:42
95fcd4b to
d61bdae
Compare
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]>
jiejingzhangamd
force-pushed
the
feat/dsa-topk-spec-rows
branch
from
August 28, 2026 05:47
d61bdae to
c20ef7f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_transforma 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 passesbatch_idx_list = token_to_batch_idx, and the sgl-kernel path spends it on a materialised gather: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_fusedvsdsa_topk_transform(..., ptRowMap=batch_idx):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/CoopMbParamsgainpt_row_map, andpt_row_of()resolves row -> page-table row.coop_mb_hist0, andcoop_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_tableasserts 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
ptRowMapwould let it in. That was wrong: sglang pre-expands the page table for target-verify (repeat_interleaveon 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 aiterunder 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_listasptRowMapinstead of gathering with it. Without that, this parameter is inert.Note on base. Retargeted from
upstream-synctomainand 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, sodsa_topk_transformis unbuildable onmainuntil #6 lands. This PR therefore shows two commits until #6 merges, after which it collapses to its own.Carried over from #6:
ptRowMapis anstd::optional<torch::Tensor>argument rather than a nullableaiter_tensor_t*. The kernel-side indirection is unchanged.Testing.
op_tests/test_dsa_topk_transform.pypasses 13/13 on this base against a gfx950 MI355X.The pre-rebase head is kept at
backup/dsa-topk-spec-rows-20260827.