Repository navigation
feat(topk): fuse the DSA page-table transform into a cooperative top-k - #2
Merged
Merged
Conversation
sglang's DSA indexer follows its per-row top-k with a chain of tensor ops to turn the winning positions into physical KV slots -- a score gather, two torch.where, the page split, a torch.gather, the recombine and a masked_fill (_topk_transform_512_vectorized). At k=2048 that chain is launch-bound and costs more than the selection it follows. This adds dsa_topk_transform, which does both halves in one launch. Selection is a cooperative radix-select (csrc/include/coop_topk.cuh): a 12-bit LDS histogram narrows the row, then a second pass over the fp16 key refines the threshold bin, with a fallback rescan for rows whose values collapse into one coarse key. The page-table mapping rides along as a tail pass after the block-wide sync rather than inside the scatter emit, which keeps the dependent scattered loads off the hot selection loop -- folding it into the emit measured 6-16us worse at batch >= 64. Against sgl_kernel's fast_topk_transform_fused on real GLM-5.2 decode logits (live=100001, page_size=1) this is 1.82-2.22x faster across batch 1-256 with bit-identical output. A null pageTable emits raw positions, making this a plain per-row top-k; a null rowStarts means every row starts at 0, which is what decode wants and saves it a per-call zeros tensor. Exposed as a ctypes entry because per-call binding cost is a visible fraction of a 20us op.
xiaobochen-amd
approved these changes
Aug 27, 2026
This was referenced Aug 27, 2026
xiaobochen-amd
pushed a commit
that referenced
this pull request
Aug 28, 2026
#5) sglang's DSA indexer follows its per-row top-k with a chain of tensor ops to turn the winning positions into physical KV slots -- a score gather, two torch.where, the page split, a torch.gather, the recombine and a masked_fill (_topk_transform_512_vectorized). At k=2048 that chain is launch-bound and costs more than the selection it follows. This adds dsa_topk_transform, which does both halves in one launch. Selection is a cooperative radix-select (csrc/include/coop_topk.cuh): a 12-bit LDS histogram narrows the row, then a second pass over the fp16 key refines the threshold bin, with a fallback rescan for rows whose values collapse into one coarse key. The page-table mapping rides along as a tail pass after the block-wide sync rather than inside the scatter emit, which keeps the dependent scattered loads off the hot selection loop -- folding it into the emit measured 6-16us worse at batch >= 64. Against sgl_kernel's fast_topk_transform_fused on real GLM-5.2 decode logits (live=100001, page_size=1) this is 1.82-2.22x faster across batch 1-256 with bit-identical output. A null pageTable emits raw positions, making this a plain per-row top-k; a null rowStarts means every row starts at 0, which is what decode wants and saves it a per-call zeros tensor. Exposed as a ctypes entry because per-call binding cost is a visible fraction of a 20us op. Ported onto main, which predates the aiter_tensor_t migration of this file, so it carries two deltas against the copy merged as #2: topk_per_row_kernels.cu also needs an explicit aiter_tensor.h include (upstream already had one on the newer base), and the coop namespace goes above the top_k_per_row_prefill doc comment rather than between that comment and the function it documents. Co-authored-by: jiejingzhangamd <[email protected]>
jiejingzhangamd
added a commit
that referenced
this pull request
Aug 28, 2026
sglang's DSA indexer follows its per-row top-k with a chain of tensor ops to turn the winning positions into physical KV slots -- a score gather, two torch.where, the page split, a torch.gather, the recombine and a masked_fill (_topk_transform_512_vectorized). At k=2048 that chain is launch-bound and costs more than the selection it follows. This adds dsa_topk_transform, which does both halves in one launch. Selection is a cooperative radix-select (csrc/include/coop_topk.cuh): a 12-bit LDS histogram narrows the row, then a second pass over the fp16 key refines the threshold bin, with a fallback rescan for rows whose values collapse into one coarse key. The page-table mapping rides along as a tail pass after the block-wide sync rather than inside the scatter emit, which keeps the dependent scattered loads off the hot selection loop -- folding it into the emit measured 6-16us worse at batch >= 64. Against sgl_kernel's fast_topk_transform_fused on real GLM-5.2 decode logits (live=100001, page_size=1) this is 1.71-1.92x faster across batch 1-64 with bit-identical output. A null pageTable emits raw positions, making this a plain per-row top-k; a null rowStarts means every row starts at 0, which is what decode wants and saves it a per-call zeros tensor. Ported onto main, which predates the detorch migration of module_top_k_per_row, with three deltas against the copy merged as #2: - The op is bound through pybind rather than ctypes. Registering any ctypes entry forces torch_exclude on the whole module (aiter/jit/core.py), and on this base three of the module's four sources still include torch/all.h, so no ctypes entry compiles here at all -- main's own top_k_per_row_decode_fast and top_k_per_row_prefill_fast fail the same way today, they are simply never called. ROCm#4702 and ROCm#4756 are what make the ctypes form buildable. The binding turns out to cost nothing measurable: against the ctypes build on the same machine, three runs give 26.2 vs 26.5 us at batch 8 and 39.9 vs 39.7 us at batch 64, with batch 1 faster on pybind (25.1 vs 28.5 us). The two trees sit on different bases so that last gap is not purely the binding, but there is no per-call penalty to recover here. - topk_per_row_kernels.cu keeps its torch::Tensor signature style, matching the two entries beside it, and takes the stream from getCurrentHIPStream. - The coop namespace goes above the top_k_per_row_prefill doc comment rather than between that comment and the function it documents. coop_topk.cuh is byte-identical to #2 and the selection kernel is unchanged.
JohnQin-llm
pushed a commit
that referenced
this pull request
Aug 28, 2026
#5) sglang's DSA indexer follows its per-row top-k with a chain of tensor ops to turn the winning positions into physical KV slots -- a score gather, two torch.where, the page split, a torch.gather, the recombine and a masked_fill (_topk_transform_512_vectorized). At k=2048 that chain is launch-bound and costs more than the selection it follows. This adds dsa_topk_transform, which does both halves in one launch. Selection is a cooperative radix-select (csrc/include/coop_topk.cuh): a 12-bit LDS histogram narrows the row, then a second pass over the fp16 key refines the threshold bin, with a fallback rescan for rows whose values collapse into one coarse key. The page-table mapping rides along as a tail pass after the block-wide sync rather than inside the scatter emit, which keeps the dependent scattered loads off the hot selection loop -- folding it into the emit measured 6-16us worse at batch >= 64. Against sgl_kernel's fast_topk_transform_fused on real GLM-5.2 decode logits (live=100001, page_size=1) this is 1.82-2.22x faster across batch 1-256 with bit-identical output. A null pageTable emits raw positions, making this a plain per-row top-k; a null rowStarts means every row starts at 0, which is what decode wants and saves it a per-call zeros tensor. Exposed as a ctypes entry because per-call binding cost is a visible fraction of a 20us op. Ported onto main, which predates the aiter_tensor_t migration of this file, so it carries two deltas against the copy merged as #2: topk_per_row_kernels.cu also needs an explicit aiter_tensor.h include (upstream already had one on the newer base), and the coop namespace goes above the top_k_per_row_prefill doc comment rather than between that comment and the function it documents. Co-authored-by: jiejingzhangamd <[email protected]>
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
Adds
dsa_topk_transform: the DSA indexer's per-row top-k and its page-table transform in a single kernel launch.sglang currently follows its top-k with a chain of tensor ops to turn the winning positions into physical KV slots — a score gather, two
torch.where, the page split, atorch.gather, the recombine and amasked_fill(_topk_transform_512_vectorized). At k=2048 that chain is launch-bound and costs more than the selection it follows (37.9–50.2 us measured).How
Selection is a cooperative radix-select (
csrc/include/coop_topk.cuh): a 12-bit LDS histogram narrows the row, a second pass over the fp16 key refines the threshold bin, and rows whose values collapse into a single coarse key fall back to a rescan.The page-table mapping runs as a tail pass after the block-wide sync, not inside the scatter emit. That ordering is the whole point: folding the lookup into the emit serialises dependent scattered loads against the row scan and measured 6–16 us worse at batch >= 64.
Long rows take a multi-block split path (
coop_mb_*); both paths share the same transform tail.Numbers
Against
sgl_kernel'sfast_topk_transform_fusedon real GLM-5.2 decode logits (live=100001, page_size=1), batch 1–256: 1.82–2.22x faster, bit-identical output.Fusing the transform in (vs. two launches) saves a further 1.4–2.2 us per call.
Interface
pageTable = Noneemits raw positions, making this a plain per-row top-k.rowStarts = Nonemeans every row starts at 0 — what decode always wants, and it saves a per-call zeros tensor.pageSizemust be a power of two; feeding the indexer's compact page_size=64 table yields the same physical slots as gathering a page_size=1 table without materialising the wide one.Exposed as a ctypes entry because per-call binding cost is a visible fraction of a 20 us op.
Validation
Built and run on MI355X (gfx950). Selected set matches
torch.topkexactly across one-block and row-split paths, short/long/ragged rows, and both page sizes, verified by inverting a deliberately shuffled page table.Selection is exact except on rows dense enough to collapse into one fp16 coarse key (synthetic clustered data loses 1–3 of 2048); real GLM-5.2 logits are exact. This is a property of the pre-existing coop selection, not of the transform.
Note on the base branch
This targets
upstream-sync(= ROCm/aiter9aa8a6b9) rather thanmain, so the diff is just the three files. This fork'smainis 304 commits behind upstream and 0 ahead, and predates the torch-freeaiter_tensor_t/ ctypes interface this op is built on, so it cannot host the change as-is. To take just the feature, cherry-pick39ba9cca.