Skip to content

[FlashInfer V0.6.18] feat(dsv4): support --dsa-topk-backend flashinfer with fused top-k - #33237

Merged
Fridge003 merged 7 commits into
sgl-project:mainfrom
zianglih:agent/dsv4-flashinfer-fused-topk
Sep 1, 2026
Merged

Fridge003 merged 7 commits into
sgl-project:mainfrom
zianglih:agent/dsv4-flashinfer-fused-topk

Conversation

@zianglih

@zianglih zianglih commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Motivation

@HumansAnd

Scope: DeepSeek V4 FlashInfer top-k backend support. This PR makes --dsa-topk-backend flashinfer usable for the DeepSeek V4 indexer. It uses FlashInfer's fused compact-page transform when SGLANG_DSA_FUSE_TOPK=true and preserves the existing unfused FlashInfer path when the flag is disabled.

The packed PAGED-row prerequisite was merged in #33006. DeepSeek V4 adds a compact page table where one page-table entry represents 64 score positions. FlashInfer #4315 added the required page_size, caller-owned output, raw-index output, and padded-stride support.

Dependency: final FlashInfer v0.6.18 contains the compact-page fused top-k API from flashinfer#4315. SGLang main now pins v0.6.18 through merged #36954. This PR extends the existing runtime check to target and speculative-draft DSA top-k selection.

Modifications

  • Add a fused DeepSeek V4 FlashInfer helper that calls top_k_page_table_transform with:
    • padded-stride score rows without a contiguous materialization;
    • the compact page table with page_size=64;
    • graph-safe deterministic and tie-break settings;
    • caller-owned translated and optional raw-index output buffers.
  • Preserve the existing unfused flashinfer.top_k plus vectorized page translation path and resolve the fused/unfused transform once during backend initialization.
  • Pass a required, already-resolved should_use_topk_v2() decision into indexer metadata; FlashInfer and Torch do not create or copy an unused SGL plan.
  • Require flashinfer-python>=0.6.18 when FlashInfer is selected for target or speculative-draft DSA top-k, while preserving the existing resolved attention-backend check.
  • Update bench_topk.py to benchmark page_size={1,64} as a Cartesian axis with a preallocated output while keeping the focused CI value at K=512. The expanded performance grid below uses a source-unchanged runtime-axis wrapper. Page size 64 matches the compact CUDA DSA layout; page size 1 is the noncompact baseline.
  • Extend existing CPU and CUDA-graph tests across fused/unfused routing, K={512,1024}, optional raw output, padded score strides, replayed inputs, short rows, and raw/translated alignment. Tests exercise non-contiguous page tables and assert FlashInfer 0.6.18's -1 padding contract for every unused translated/raw suffix slot.

Accuracy Tests

Post-merge validation used SGLang abaeebc57eacb256a4e8013b59646c4d71cf0dc6 on one NVIDIA B300:

  • Merge parents: PR head d8750c398dccbea72bbc37bf37ddacae2defe801 and upstream main 881cbfe54c98356cfa1eaa134aa4d0be702fc90f.
  • Image: lmsysorg/sglang:nightly-dev-cu13-20260831-bb5e6198 (amd64 image sha256:3c75c6f7f8e6eaeebdcf92ef47caef818608134252ad5898f4c670b7911768a6).
  • Hardware/runtime: NVIDIA B300 (SM103), driver 590.48.01, Python 3.12.3, PyTorch 2.13.0+cu130, CUDA 13.0.
  • FlashInfer: flashinfer-python==0.6.18, flashinfer-cubin==0.6.18, and flashinfer-jit-cache==0.6.18+cu130, force-reinstalled directly from the final release assets because this nightly image predates [Deps] Bump FlashInfer to 0.6.18 #36954.

Install and focused test commands:

python3 -m pip install --force-reinstall --no-deps --no-cache-dir \
  https://github.com/flashinfer-ai/flashinfer/releases/download/v0.6.18/flashinfer_python-0.6.18-py3-none-any.whl \
  https://github.com/flashinfer-ai/flashinfer/releases/download/v0.6.18/flashinfer_cubin-0.6.18-py3-none-any.whl \
  'https://github.com/flashinfer-ai/flashinfer/releases/download/v0.6.18/flashinfer_jit_cache-0.6.18%2Bcu130-cp39-abi3-manylinux_2_28_x86_64.whl'

cd /sgl-workspace/pr33237-merge-validation/sglang
export PYTHONPATH="$PWD/python"
export SGLANG_CACHE_DIR=/sgl-workspace/pr33237-merge-validation/cache
export FLASHINFER_WORKSPACE_BASE="$SGLANG_CACHE_DIR/flashinfer"
unset FLASHINFER_TOPK_ALGO SGLANG_DSA_FUSE_TOPK FLASHINFER_DISABLE_VERSION_CHECK SGLANG_SKIP_SGL_KERNEL_VERSION_CHECK

CUDA_VISIBLE_DEVICES=0 python3 -m pytest -q \
  test/registered/unit/test_pre_publish_readers.py \
  test/registered/unit/test_platform_address_not_frozen.py \
  test/registered/unit/layers/test_dsv4_nonpaged_indexer.py \
  test/registered/kernels/ops/attention/test_dsa_indexer.py::TestDSAIndexer::test_dsv4_flashinfer_compact_topk_cuda_graph \
  test/registered/kernels/ops/attention/test_dsa_indexer.py::TestDSAIndexer::test_topk_v2_respects_topk_backend

Raw results:

engine FlashInfer gates and caller-derived XPU topk-v2 eligibility: PASS
17 passed, 15 warnings, 28 subtests passed in 35.17s

The suite covers upstream's pre-publish/platform guards, init-time fused/unfused routing, required resolved top-k-v2 metadata, a non-contiguous compact page table, K={512,1024}, optional raw output, padded score strides, -1 suffix padding, replayed inputs, short rows, raw/translated alignment, and CUDA-graph replay. A direct mock smoke independently checked both target/speculative FlashInfer version gates and the caller-resolved XPU exclusion. Changed-file pre-commit passed and left the checkout clean. No DeepSeek V4 model-serving accuracy run or performance rerun was performed; the speed data below remains historical pre-merge evidence. Test log SHA-256: 341cb33a65a9e7f97a6c9985a6977a1d111074d66960040146776aaf3714ff10.

Speed Tests and Profiling

Historical single-GPU operator microbenchmark at SGLang 798019b5dcf815371b4986b84117ad56eff092ae, using the pre-release FlashInfer 0.6.18 nightly recorded below:

  • Workload: FP32 random score rows, full uniform lengths, identity page tables, and caller-owned int32 translated-output buffers. Exact Cartesian grid: K={512,1024,2048} × seq_len={8192,65536,131072} × batch_size={4,8,16,32,64} × page_size={1,64}. Providers: SGL JIT-v1, SGL JIT-v2, FlashInfer, and PyTorch. JIT-v1 is unavailable at K=2048 by design.
  • Invocation: the committed benchmark keeps its focused CI value K=512. A source-unchanged inline wrapper accepts the four requested axes as command-line arguments and replaces the benchmark's runtime configuration before calling Benchmark.run(); the provider list is asserted unchanged.
  • Page-table layouts: page size 64 matches DeepSeek V4's compact C4 transform and the shared SGL JIT-v2 representation. Page size 1 matches the current generic DSA/GLM-5 FlashInfer path, which expands its underlying 64-token KV pages into one table entry per token.
  • Timing: 50 warmups; CUDA-graph replay measured with CUDA events; read inputs rotated and L2 flushed between timed replays; 1,000 requested operator iterations; median microseconds reported. Compilation, JIT-v2 planning, output initialization/allocation, Python/host dispatch, and serving are excluded.
  • Repetition: three independent complete sweeps on the same B200. All 330 timed cells per sweep had a cross-sweep max-minus-min span below 0.41% of their median.
  • Backend settings: FLASHINFER_TOPK_ALGO unset (automatic); SGLANG_DSA_FUSE_TOPK unset. The benchmark directly calls the fused FlashInfer API, so this is not a fused-versus-unfused comparison. The tested FlashInfer nightly predates the open CUB TopK PR, so these are not CUB-backend results. Each process emitted the already-documented AOT-to-JIT fallback warning; compilation remained outside timing.

Command:

cd /hai-workspace/sglang-pr33237-benchmark
export PYTHONPATH="$PWD/python"
export CUDA_VISIBLE_DEVICES=0
export SGLANG_KERNEL_DISABLE_LOG_BANDWIDTH=1
unset FLASHINFER_TOPK_ALGO SGLANG_DSA_FUSE_TOPK FLASHINFER_DISABLE_VERSION_CHECK

for benchmark_repeat in 1 2 3; do
  SGLANG_IS_IN_CI=true \
    /hai-workspace/sglang-pr33237-bench-venv/bin/python - \
    --k 512 1024 2048 \
    --seq-len 8192 65536 131072 \
    --batch-size 4 8 16 32 64 \
    --page-size 1 64 <<'PY' 2>&1 \
    | tee "/hai-workspace/sglang-pr33237-results-full-grid-798019b5d/run-${benchmark_repeat}.log"
import argparse
import runpy

parser = argparse.ArgumentParser()
parser.add_argument("--k", nargs="+", type=int, required=True)
parser.add_argument("--seq-len", nargs="+", type=int, required=True)
parser.add_argument("--batch-size", nargs="+", type=int, required=True)
parser.add_argument("--page-size", nargs="+", type=int, required=True)
args = parser.parse_args()

benchmark = runpy.run_path(
    "test/registered/kernels/benchmark/attention/bench_topk.py",
    run_name="bench_topk_module",
)["benchmark"]
overrides = {
    ("k",): args.k,
    ("seq_len",): args.seq_len,
    ("batch_size",): args.batch_size,
    ("page_size",): args.page_size,
}
if {names for names, _ in benchmark._configs} != set(overrides):
    raise RuntimeError(f"unexpected axes: {benchmark._configs}")
benchmark._configs[:] = [
    (names, [(value,) for value in overrides[names]])
    for names, _ in benchmark._configs
]
expected_providers = ["jit_v1", "jit_v2", "flashinfer", "torch"]
if benchmark._line_vals != expected_providers:
    raise RuntimeError(f"unexpected providers: {benchmark._line_vals}")
print("effective benchmark configs:", benchmark._configs, flush=True)
print("effective providers:", benchmark._line_vals, flush=True)
benchmark.run()
PY
done

Complete raw latency output:

Run 1
           k   seq_len  batch_size  page_size |      jit_v1(us)     jit_v2(us)  flashinfer(us)      torch(us)
0        512      8192           4          1 |          9.4618         5.0381         12.3904        76.1853
1        512      8192           4         64 |          9.5440         4.9760         12.1446        72.2534
2        512      8192           8          1 |          9.6666         5.0381         12.4518        78.7251
3        512      8192           8         64 |          9.5642         4.9360         12.1446        74.7315
4        512      8192          16          1 |          9.7894         5.1610         12.5542        82.9949
5        512      8192          16         64 |          9.5642         5.0176         12.1856        78.7866
6        512      8192          32          1 |          9.8304         5.2019         12.6358        83.2621
7        512      8192          32         64 |          9.6870         5.0995         12.3290        78.9914
8        512      8192          64          1 |          9.8509         5.3248         12.6576        95.4474
9        512      8192          64         64 |          9.7677         5.1814         12.4106        90.9830
10       512     65536           4          1 |         36.7411        11.3459         46.9206        96.4589
11       512     65536           4         64 |         36.5158        10.9773         46.6637        93.1530
12       512     65536           8          1 |         37.9290        11.4074         47.7075       101.1386
13       512     65536           8         64 |         37.6320        11.0182         47.4624        97.6771
14       512     65536          16          1 |         38.1788        16.7317         47.8246       110.9015
15       512     65536          16         64 |         37.7754        16.6707         47.5853       107.1680
16       512     65536          32          1 |         38.3209        16.8164         48.2440       119.2986
17       512     65536          32         64 |         37.9204        16.6821         47.8414       115.4702
18       512     65536          64          1 |         38.4341        17.0472         48.2353       141.2702
19       512     65536          64         64 |         38.1184        16.9557         47.9061       137.3357
20       512    131072           4          1 |         60.1395        12.6566         78.7661       108.2541
21       512    131072           4         64 |         59.5866        12.3898         78.2131       105.7357
22       512    131072           8          1 |         60.1821        12.6673         78.8543       117.1593
23       512    131072           8         64 |         59.5763        12.4333         78.1312       114.6867
24       512    131072          16          1 |         60.1413        15.9594         78.6648       122.6459
25       512    131072          16         64 |         59.4303        15.5561         77.8698       120.0786
26       512    131072          32          1 |         60.3477        24.0701         78.3604       139.7906
27       512    131072          32         64 |         59.7597        23.5691         77.7136       137.0603
28       512    131072          64          1 |         60.8815        30.2545         78.5036       179.9523
29       512    131072          64         64 |         60.3672        29.9008         78.1068       177.3477
30      1024      8192           4          1 |         10.4141         5.3453         12.7414        79.6160
31      1024      8192           4         64 |         10.3834         5.2835         12.5133        75.8166
32      1024      8192           8          1 |         10.5267         5.3654         12.8819        80.4042
33      1024      8192           8         64 |         10.3853         5.2822         12.5133        76.5357
34      1024      8192          16          1 |         10.5267         5.4682         12.9229        82.3706
35      1024      8192          16         64 |         10.4448         5.3043         12.5754        78.2746
36      1024      8192          32          1 |         10.5267         5.4896         13.0438        82.8723
37      1024      8192          32         64 |         10.5267         5.3658         12.6771        78.7658
38      1024      8192          64          1 |         11.3069         5.5910         13.7011        95.0691
39      1024      8192          64         64 |         11.3661         5.4480         13.4758        91.0541
40      1024     65536           4          1 |         38.0499        11.7146         48.4864        98.3126
41      1024     65536           4         64 |         37.6224        11.1411         47.5318        93.9318
42      1024     65536           8          1 |         38.4621        11.7555         48.6202       102.5942
43      1024     65536           8         64 |         37.8266        11.2234         47.5443        98.0880
44      1024     65536          16          1 |         38.6781        18.6343         48.6337       110.4782
45      1024     65536          16         64 |         38.1648        18.3910         47.7082       105.8166
46      1024     65536          32          1 |         38.8870        18.7400         48.8523       120.5453
47      1024     65536          32         64 |         38.4389        18.3802         47.9850       115.8471
48      1024     65536          64          1 |         39.0778        19.1518         48.9274       140.8244
49      1024     65536          64         64 |         38.8176        18.7563         48.1533       136.2323
50      1024    131072           4          1 |         67.7376        13.1072         86.3014       109.2163
51      1024    131072           4         64 |         67.2973        12.5952         85.4035       105.5014
52      1024    131072           8          1 |         68.0897        13.1279         86.6037       116.2364
53      1024    131072           8         64 |         67.3997        12.6982         85.4224       112.4518
54      1024    131072          16          1 |         68.1751        16.7256         86.4494       121.4790
55      1024    131072          16         64 |         67.3831        15.9565         85.2577       117.5192
56      1024    131072          32          1 |         68.3934        25.4443         86.2705       140.4532
57      1024    131072          32         64 |         67.7208        24.4771         85.2992       136.5675
58      1024    131072          64          1 |         68.7942        33.0287         86.4256       180.4564
59      1024    131072          64         64 |         68.5592        32.3194         85.9185       176.6251
60      2048      8192           4          1 |             N/A         5.5501         12.7181        79.9744
61      2048      8192           4         64 |             N/A         5.2019         12.4928        77.2506
62      2048      8192           8          1 |             N/A         5.5706         12.9229        80.1795
63      2048      8192           8         64 |             N/A         5.2432         12.5542        77.3530
64      2048      8192          16          1 |             N/A         5.7142         12.9030        80.4266
65      2048      8192          16         64 |             N/A         5.3658         12.6154        77.5373
66      2048      8192          32          1 |             N/A         5.9392         13.0867        80.4659
67      2048      8192          32         64 |             N/A         5.6128         12.7386        77.6806
68      2048      8192          64          1 |             N/A         5.9389         13.1277        94.5965
69      2048      8192          64         64 |             N/A         5.6525         12.7792        91.9414
70      2048     65536           4          1 |             N/A        12.7078         51.9882       100.1370
71      2048     65536           4         64 |             N/A        11.9603         50.1658        95.8125
72      2048     65536           8          1 |             N/A        12.7386         52.1523       103.5642
73      2048     65536           8         64 |             N/A        12.0218         50.1850        98.8979
74      2048     65536          16          1 |             N/A        20.9857         51.9585       110.3439
75      2048     65536          16         64 |             N/A        20.3158         50.0010       105.6650
76      2048     65536          32          1 |             N/A        21.0888         51.8910       120.9038
77      2048     65536          32         64 |             N/A        20.3174         50.1499       116.3791
78      2048     65536          64          1 |             N/A        21.5820         51.8650       142.8014
79      2048     65536          64         64 |             N/A        20.6933         50.2699       137.9304
80      2048    131072           4          1 |             N/A        14.2550         88.2586       110.8554
81      2048    131072           4         64 |             N/A        13.4963         86.4851       106.6781
82      2048    131072           8          1 |             N/A        14.2981         88.3548       119.2263
83      2048    131072           8         64 |             N/A        13.5578         86.5178       114.9709
84      2048    131072          16          1 |             N/A        18.4234         88.1805       121.2314
85      2048    131072          16         64 |             N/A        17.0580         86.2690       117.0260
86      2048    131072          32          1 |             N/A        27.5785         87.8409       141.5259
87      2048    131072          32         64 |             N/A        26.1035         86.1376       137.2856
88      2048    131072          64          1 |             N/A        37.0406         87.7847       182.4922
89      2048    131072          64         64 |             N/A        36.0936         86.4256       178.2245
Run 2
           k   seq_len  batch_size  page_size |      jit_v1(us)     jit_v2(us)  flashinfer(us)      torch(us)
0        512      8192           4          1 |          9.4634         5.0381         12.4029        76.1856
1        512      8192           4         64 |          9.5232         4.9562         12.1651        72.2534
2        512      8192           8          1 |          9.6483         5.0381         12.4518        78.6944
3        512      8192           8         64 |          9.5642         4.9562         12.1635        74.7315
4        512      8192          16          1 |          9.7894         5.1606         12.5338        82.9747
5        512      8192          16         64 |          9.5846         5.0381         12.1651        78.8067
6        512      8192          32          1 |          9.8406         5.2019         12.6365        83.2614
7        512      8192          32         64 |          9.6870         5.0995         12.3293        78.9914
8        512      8192          64          1 |          9.8509         5.3248         12.6566        95.3526
9        512      8192          64         64 |          9.7485         5.1837         12.3904        90.9424
10       512     65536           4          1 |         36.7414        11.3459         46.9094        96.4301
11       512     65536           4         64 |         36.5363        10.9773         46.6842        93.1613
12       512     65536           8          1 |         37.9290        11.4074         47.7181       101.0371
13       512     65536           8         64 |         37.6422        11.0186         47.4624        97.6794
14       512     65536          16          1 |         38.1661        16.7372         47.8248       110.9140
15       512     65536          16         64 |         37.7853        16.6707         47.6054       107.1706
16       512     65536          32          1 |         38.3290        16.8169         48.2118       119.3332
17       512     65536          32         64 |         37.8945        16.6821         47.8558       115.4333
18       512     65536          64          1 |         38.4244        17.0667         48.2258       141.2337
19       512     65536          64         64 |         38.1013        16.9472         47.9152       137.2843
20       512    131072           4          1 |         60.1398        12.6570         78.7661       108.2787
21       512    131072           4         64 |         59.5866        12.3699         78.1734       105.6973
22       512    131072           8          1 |         60.2199        12.6673         78.8668       117.1637
23       512    131072           8         64 |         59.5664        12.4502         78.1210       114.6867
24       512    131072          16          1 |         60.1413        15.9680         78.6812       122.6625
25       512    131072          16         64 |         59.4246        15.5415         77.8629       120.0737
26       512    131072          32          1 |         60.3590        24.0786         78.3701       139.7611
27       512    131072          32         64 |         59.7333        23.5861         77.7301       137.0603
28       512    131072          64          1 |         60.8535        30.2284         78.5222       180.0175
29       512    131072          64         64 |         60.3773        29.9011         78.0958       177.3656
30      1024      8192           4          1 |         10.4042         5.3453         12.7398        79.6160
31      1024      8192           4         64 |         10.3629         5.2637         12.5133        75.8067
32      1024      8192           8          1 |         10.5267         5.3658         12.8822        80.4045
33      1024      8192           8         64 |         10.3834         5.2838         12.5133        76.5232
34      1024      8192          16          1 |         10.5472         5.4682         12.9229        82.3603
35      1024      8192          16         64 |         10.4454         5.3229         12.5846        78.2742
36      1024      8192          32          1 |         10.5472         5.5085         13.0253        82.8826
37      1024      8192          32         64 |         10.5267         5.3658         12.6774        78.7658
38      1024      8192          64          1 |         11.3254         5.5923         13.7011        95.0854
39      1024      8192          64         64 |         11.3661         5.4477         13.4774        91.0131
40      1024     65536           4          1 |         38.0518        11.7146         48.4864        98.2730
41      1024     65536           4         64 |         37.6218        11.1414         47.5443        93.8906
42      1024     65536           8          1 |         38.4413        11.7555         48.5888       102.5718
43      1024     65536           8         64 |         37.8262        11.2230         47.5648        98.0688
44      1024     65536          16          1 |         38.6718        18.6343         48.6086       110.4575
45      1024     65536          16         64 |         38.1347        18.3706         47.7274       105.8598
46      1024     65536          32          1 |         38.9034        18.7231         48.8434       120.5484
47      1024     65536          32         64 |         38.4399        18.4061         47.9797       115.8157
48      1024     65536          64          1 |         39.0875        19.1534         48.9262       140.8625
49      1024     65536          64         64 |         38.7837        18.7563         48.1192       136.1917
50      1024    131072           4          1 |         67.7274        13.1056         86.2720       109.2186
51      1024    131072           4         64 |         67.2563        12.6157         85.3917       105.4310
52      1024    131072           8          1 |         68.0644        13.1287         86.6159       116.2809
53      1024    131072           8         64 |         67.4214        12.7165         85.4323       112.4138
54      1024    131072          16          1 |         68.1433        16.7500         86.4398       121.5066
55      1024    131072          16         64 |         67.3636        15.9625         85.2318       117.5591
56      1024    131072          32          1 |         68.4227        25.4537         86.2793       140.4148
57      1024    131072          32         64 |         67.7376        24.4736         85.3077       136.5669
58      1024    131072          64          1 |         68.7572        33.0287         86.4163       180.4753
59      1024    131072          64         64 |         68.5397        32.3194         85.8990       176.6248
60      2048      8192           4          1 |             N/A         5.5501         12.6979        79.9555
61      2048      8192           4         64 |             N/A         5.2019         12.4723        77.2499
62      2048      8192           8          1 |             N/A         5.5706         12.9229        80.1997
63      2048      8192           8         64 |             N/A         5.2618         12.5526        77.3731
64      2048      8192          16          1 |             N/A         5.7344         12.9024        80.4250
65      2048      8192          16         64 |             N/A         5.3453         12.6154        77.5373
66      2048      8192          32          1 |             N/A         5.9392         13.1072        80.4842
67      2048      8192          32         64 |             N/A         5.6115         12.7379        77.6810
68      2048      8192          64          1 |             N/A         5.9187         13.1277        94.5629
69      2048      8192          64         64 |             N/A         5.6528         12.7795        91.9139
70      2048     65536           4          1 |             N/A        12.6979         51.9882       100.0957
71      2048     65536           4         64 |             N/A        11.9411         50.1661        95.7744
72      2048     65536           8          1 |             N/A        12.7382         52.1421       103.4957
73      2048     65536           8         64 |             N/A        12.0214         50.2170        98.8464
74      2048     65536          16          1 |             N/A        20.9857         51.9648       110.3698
75      2048     65536          16         64 |             N/A        20.3162         50.0122       105.6563
76      2048     65536          32          1 |             N/A        21.0893         51.8827       120.9626
77      2048     65536          32         64 |             N/A        20.3243         50.1371       116.3793
78      2048     65536          64          1 |             N/A        21.5915         51.8827       142.8294
79      2048     65536          64         64 |             N/A        20.7016         50.2544       137.9461
80      2048    131072           4          1 |             N/A        14.2746         88.2589       110.8173
81      2048    131072           4         64 |             N/A        13.4758         86.5178       106.6368
82      2048    131072           8          1 |             N/A        14.2793         88.3611       119.1684
83      2048    131072           8         64 |             N/A        13.5581         86.4666       115.0077
84      2048    131072          16          1 |             N/A        18.3987         88.1722       121.2483
85      2048    131072          16         64 |             N/A        17.0513         86.2882       117.0272
86      2048    131072          32          1 |             N/A        27.5700         87.8397       141.5637
87      2048    131072          32         64 |             N/A        26.1035         86.1355       137.2331
88      2048    131072          64          1 |             N/A        37.0406         87.7847       182.4579
89      2048    131072          64         64 |             N/A        36.1131         86.4256       178.2345
Run 3
           k   seq_len  batch_size  page_size |      jit_v1(us)     jit_v2(us)  flashinfer(us)      torch(us)
0        512      8192           4          1 |          9.4618         5.0381         12.3907        76.1856
1        512      8192           4         64 |          9.5437         4.9766         12.1446        72.2534
2        512      8192           8          1 |          9.6666         5.0381         12.4515        78.7251
3        512      8192           8         64 |          9.5437         4.9562         12.1651        74.7315
4        512      8192          16          1 |          9.7894         5.1610         12.5341        82.9747
5        512      8192          16         64 |          9.5846         5.0381         12.1856        78.8074
6        512      8192          32          1 |          9.8509         5.2218         12.6362        83.2307
7        512      8192          32         64 |          9.6870         5.0995         12.3290        79.0125
8        512      8192          64          1 |          9.8509         5.3453         12.6566        95.3840
9        512      8192          64         64 |          9.7485         5.2019         12.4096        90.9805
10       512     65536           4          1 |         36.7616        11.3459         46.9296        96.4490
11       512     65536           4         64 |         36.4957        10.9773         46.6845        93.1702
12       512     65536           8          1 |         37.9101        11.4074         47.7293       101.1382
13       512     65536           8         64 |         37.6422        11.0198         47.4419        97.6291
14       512     65536          16          1 |         38.1659        16.7374         47.8370       110.8891
15       512     65536          16         64 |         37.7549        16.6810         47.5974       107.1504
16       512     65536          32          1 |         38.3043        16.8252         48.2279       119.2832
17       512     65536          32         64 |         37.9083        16.6767         47.8428       115.4461
18       512     65536          64          1 |         38.4143        17.0475         48.2447       141.2331
19       512     65536          64         64 |         38.1184        16.9709         47.9147       137.2752
20       512    131072           4          1 |         60.1376        12.6563         78.7453       108.2573
21       512    131072           4         64 |         59.5866        12.3699         78.1520       105.6755
22       512    131072           8          1 |         60.2327        12.6795         78.8794       117.1771
23       512    131072           8         64 |         59.5872        12.4518         78.1328       114.6666
24       512    131072          16          1 |         60.1241        15.9677         78.6648       122.7132
25       512    131072          16         64 |         59.4309        15.5548         77.8629       120.0654
26       512    131072          32          1 |         60.3087        24.0786         78.3799       139.7705
27       512    131072          32         64 |         59.7163        23.5691         77.7387       137.0624
28       512    131072          64          1 |         60.8911        30.2359         78.5239       179.9613
29       512    131072          64         64 |         60.3867        29.9008         78.0779       177.3178
30      1024      8192           4          1 |         10.4045         5.3453         12.7584        79.6058
31      1024      8192           4         64 |         10.3629         5.2838         12.5130        75.8170
32      1024      8192           8          1 |         10.5062         5.3654         12.8819        80.4250
33      1024      8192           8         64 |         10.3834         5.2838         12.5133        76.5338
34      1024      8192          16          1 |         10.5274         5.4685         12.9024        82.3603
35      1024      8192          16         64 |         10.4448         5.3046         12.5949        78.2541
36      1024      8192          32          1 |         10.5270         5.5088         13.0250        82.8928
37      1024      8192          32         64 |         10.5267         5.3462         12.6778        78.7866
38      1024      8192          64          1 |         11.3248         5.5910         13.7216        95.0563
39      1024      8192          64         64 |         11.3664         5.4477         13.4758        91.0634
40      1024     65536           4          1 |         38.0621        11.7146         48.4861        98.3549
41      1024     65536           4         64 |         37.6214        11.1613         47.5542        93.8890
42      1024     65536           8          1 |         38.4614        11.7555         48.5990       102.6150
43      1024     65536           8         64 |         37.8269        11.2230         47.5443        98.0490
44      1024     65536          16          1 |         38.6655        18.6469         48.6086       110.5207
45      1024     65536          16         64 |         38.1744        18.3706         47.6979       105.8502
46      1024     65536          32          1 |         38.9029        18.7330         48.8442       120.4993
47      1024     65536          32         64 |         38.4518        18.4061         47.9913       115.8860
48      1024     65536          64          1 |         39.0760        19.1342         48.9274       140.9219
49      1024     65536          64         64 |         38.8008        18.7563         48.1536       136.2261
50      1024    131072           4          1 |         67.7581        13.1078         86.2621       109.1789
51      1024    131072           4         64 |         67.3075        12.6144         85.3712       105.4288
52      1024    131072           8          1 |         68.0897        13.1287         86.5912       116.2179
53      1024    131072           8         64 |         67.3997        12.6976         85.4323       112.4861
54      1024    131072          16          1 |         68.1418        16.7503         86.4487       121.4468
55      1024    131072          16         64 |         67.3507        15.9692         85.2446       117.5447
56      1024    131072          32          1 |         68.4227        25.4632         86.2583       140.4538
57      1024    131072          32         64 |         67.7208        24.4805         85.2907       136.5661
58      1024    131072          64          1 |         68.7942        33.0272         86.4535       180.4905
59      1024    131072          64         64 |         68.5300        32.3191         85.9182       176.5845
60      2048      8192           4          1 |             N/A         5.5501         12.6982        79.9539
61      2048      8192           4         64 |             N/A         5.2006         12.4723        77.2602
62      2048      8192           8          1 |             N/A         5.5706         12.9229        80.1795
63      2048      8192           8         64 |             N/A         5.2627         12.5338        77.3939
64      2048      8192          16          1 |             N/A         5.7344         12.9226        80.4557
65      2048      8192          16         64 |             N/A         5.3658         12.6147        77.5373
66      2048      8192          32          1 |             N/A         5.9386         13.0867        80.4864
67      2048      8192          32         64 |             N/A         5.6115         12.7386        77.7011
68      2048      8192          64          1 |             N/A         5.9187         13.1277        94.5658
69      2048      8192          64         64 |             N/A         5.6525         12.7795        91.9446
70      2048     65536           4          1 |             N/A        12.7178         51.9885       100.1571
71      2048     65536           4         64 |             N/A        11.9603         50.1760        95.7501
72      2048     65536           8          1 |             N/A        12.7386         52.1507       103.5155
73      2048     65536           8         64 |             N/A        12.0218         50.1862        98.8851
74      2048     65536          16          1 |             N/A        20.9794         51.9775       110.3435
75      2048     65536          16         64 |             N/A        20.3142         50.0019       105.6531
76      2048     65536          32          1 |             N/A        21.0877         51.8827       120.9301
77      2048     65536          32         64 |             N/A        20.3245         50.1371       116.3773
78      2048     65536          64          1 |             N/A        21.5808         51.8729       142.7541
79      2048     65536          64         64 |             N/A        20.7019         50.2528       137.9325
80      2048    131072           4          1 |             N/A        14.2541         88.2483       110.8688
81      2048    131072           4         64 |             N/A        13.4758         86.4973       106.6384
82      2048    131072           8          1 |             N/A        14.2856         88.3611       119.1862
83      2048    131072           8         64 |             N/A        13.5578         86.4874       115.0061
84      2048    131072          16          1 |             N/A        18.3896         88.1805       121.2314
85      2048    131072          16         64 |             N/A        17.0515         86.3010       117.0392
86      2048    131072          32          1 |             N/A        27.5797         87.8205       141.5848
87      2048    131072          32         64 |             N/A        26.1120         86.1355       137.2995
88      2048    131072          64          1 |             N/A        37.0406         87.7754       182.4366
89      2048    131072          64         64 |             N/A        36.1033         86.4158       178.2717

Cross-sweep medians; FI / JIT-v2 is FlashInfer latency divided by JIT-v2 latency:

K Seq length Batch Page size JIT-v1 (us) JIT-v2 (us) FlashInfer (us) Torch (us) FI / JIT-v2
512 8,192 4 1 9.4618 5.0381 12.3907 76.1856 2.459x
512 8,192 4 64 9.5437 4.9760 12.1446 72.2534 2.441x
512 8,192 8 1 9.6666 5.0381 12.4518 78.7251 2.472x
512 8,192 8 64 9.5642 4.9562 12.1635 74.7315 2.454x
512 8,192 16 1 9.7894 5.1610 12.5341 82.9747 2.429x
512 8,192 16 64 9.5846 5.0381 12.1856 78.8067 2.419x
512 8,192 32 1 9.8406 5.2019 12.6362 83.2614 2.429x
512 8,192 32 64 9.6870 5.0995 12.3290 78.9914 2.418x
512 8,192 64 1 9.8509 5.3248 12.6566 95.3840 2.377x
512 8,192 64 64 9.7485 5.1837 12.4096 90.9805 2.394x
512 65,536 4 1 36.7414 11.3459 46.9206 96.4490 4.135x
512 65,536 4 64 36.5158 10.9773 46.6842 93.1613 4.253x
512 65,536 8 1 37.9290 11.4074 47.7181 101.1382 4.183x
512 65,536 8 64 37.6422 11.0186 47.4624 97.6771 4.307x
512 65,536 16 1 38.1661 16.7372 47.8248 110.9015 2.857x
512 65,536 16 64 37.7754 16.6707 47.5974 107.1680 2.855x
512 65,536 32 1 38.3209 16.8169 48.2279 119.2986 2.868x
512 65,536 32 64 37.9083 16.6821 47.8428 115.4461 2.868x
512 65,536 64 1 38.4244 17.0475 48.2353 141.2337 2.829x
512 65,536 64 64 38.1184 16.9557 47.9147 137.2843 2.826x
512 131,072 4 1 60.1395 12.6566 78.7661 108.2573 6.223x
512 131,072 4 64 59.5866 12.3699 78.1734 105.6973 6.320x
512 131,072 8 1 60.2199 12.6673 78.8668 117.1637 6.226x
512 131,072 8 64 59.5763 12.4502 78.1312 114.6867 6.275x
512 131,072 16 1 60.1413 15.9677 78.6648 122.6625 4.926x
512 131,072 16 64 59.4303 15.5548 77.8629 120.0737 5.006x
512 131,072 32 1 60.3477 24.0786 78.3701 139.7705 3.255x
512 131,072 32 64 59.7333 23.5691 77.7301 137.0603 3.298x
512 131,072 64 1 60.8815 30.2359 78.5222 179.9613 2.597x
512 131,072 64 64 60.3773 29.9008 78.0958 177.3477 2.612x
1,024 8,192 4 1 10.4045 5.3453 12.7414 79.6160 2.384x
1,024 8,192 4 64 10.3629 5.2835 12.5133 75.8166 2.368x
1,024 8,192 8 1 10.5267 5.3654 12.8819 80.4045 2.401x
1,024 8,192 8 64 10.3834 5.2838 12.5133 76.5338 2.368x
1,024 8,192 16 1 10.5274 5.4682 12.9229 82.3603 2.363x
1,024 8,192 16 64 10.4448 5.3046 12.5846 78.2742 2.372x
1,024 8,192 32 1 10.5270 5.5085 13.0253 82.8826 2.365x
1,024 8,192 32 64 10.5267 5.3658 12.6774 78.7658 2.363x
1,024 8,192 64 1 11.3248 5.5910 13.7011 95.0691 2.451x
1,024 8,192 64 64 11.3661 5.4477 13.4758 91.0541 2.474x
1,024 65,536 4 1 38.0518 11.7146 48.4864 98.3126 4.139x
1,024 65,536 4 64 37.6218 11.1414 47.5443 93.8906 4.267x
1,024 65,536 8 1 38.4614 11.7555 48.5990 102.5942 4.134x
1,024 65,536 8 64 37.8266 11.2230 47.5443 98.0688 4.236x
1,024 65,536 16 1 38.6718 18.6343 48.6086 110.4782 2.609x
1,024 65,536 16 64 38.1648 18.3706 47.7082 105.8502 2.597x
1,024 65,536 32 1 38.9029 18.7330 48.8442 120.5453 2.607x
1,024 65,536 32 64 38.4399 18.4061 47.9850 115.8471 2.607x
1,024 65,536 64 1 39.0778 19.1518 48.9274 140.8625 2.555x
1,024 65,536 64 64 38.8008 18.7563 48.1533 136.2261 2.567x
1,024 131,072 4 1 67.7376 13.1072 86.2720 109.2163 6.582x
1,024 131,072 4 64 67.2973 12.6144 85.3917 105.4310 6.769x
1,024 131,072 8 1 68.0897 13.1287 86.6037 116.2364 6.597x
1,024 131,072 8 64 67.3997 12.6982 85.4323 112.4518 6.728x
1,024 131,072 16 1 68.1433 16.7500 86.4487 121.4790 5.161x
1,024 131,072 16 64 67.3636 15.9625 85.2446 117.5447 5.340x
1,024 131,072 32 1 68.4227 25.4537 86.2705 140.4532 3.389x
1,024 131,072 32 64 67.7208 24.4771 85.2992 136.5669 3.485x
1,024 131,072 64 1 68.7942 33.0287 86.4256 180.4753 2.617x
1,024 131,072 64 64 68.5397 32.3194 85.9182 176.6248 2.658x
2,048 8,192 4 1 N/A 5.5501 12.6982 79.9555 2.288x
2,048 8,192 4 64 N/A 5.2019 12.4723 77.2506 2.398x
2,048 8,192 8 1 N/A 5.5706 12.9229 80.1795 2.320x
2,048 8,192 8 64 N/A 5.2618 12.5526 77.3731 2.386x
2,048 8,192 16 1 N/A 5.7344 12.9030 80.4266 2.250x
2,048 8,192 16 64 N/A 5.3658 12.6154 77.5373 2.351x
2,048 8,192 32 1 N/A 5.9392 13.0867 80.4842 2.203x
2,048 8,192 32 64 N/A 5.6115 12.7386 77.6810 2.270x
2,048 8,192 64 1 N/A 5.9187 13.1277 94.5658 2.218x
2,048 8,192 64 64 N/A 5.6525 12.7795 91.9414 2.261x
2,048 65,536 4 1 N/A 12.7078 51.9882 100.1370 4.091x
2,048 65,536 4 64 N/A 11.9603 50.1661 95.7744 4.194x
2,048 65,536 8 1 N/A 12.7386 52.1507 103.5155 4.094x
2,048 65,536 8 64 N/A 12.0218 50.1862 98.8851 4.175x
2,048 65,536 16 1 N/A 20.9857 51.9648 110.3439 2.476x
2,048 65,536 16 64 N/A 20.3158 50.0019 105.6563 2.461x
2,048 65,536 32 1 N/A 21.0888 51.8827 120.9301 2.460x
2,048 65,536 32 64 N/A 20.3243 50.1371 116.3791 2.467x
2,048 65,536 64 1 N/A 21.5820 51.8729 142.8014 2.404x
2,048 65,536 64 64 N/A 20.7016 50.2544 137.9325 2.428x
2,048 131,072 4 1 N/A 14.2550 88.2586 110.8554 6.191x
2,048 131,072 4 64 N/A 13.4758 86.4973 106.6384 6.419x
2,048 131,072 8 1 N/A 14.2856 88.3611 119.1862 6.185x
2,048 131,072 8 64 N/A 13.5578 86.4874 115.0061 6.379x
2,048 131,072 16 1 N/A 18.3987 88.1805 121.2314 4.793x
2,048 131,072 16 64 N/A 17.0515 86.2882 117.0272 5.060x
2,048 131,072 32 1 N/A 27.5785 87.8397 141.5637 3.185x
2,048 131,072 32 64 N/A 26.1035 86.1355 137.2856 3.300x
2,048 131,072 64 1 N/A 37.0406 87.7847 182.4579 2.370x
2,048 131,072 64 64 N/A 36.1033 86.4256 178.2345 2.394x

JIT-v2 won every same-page shape. FlashInfer/JIT-v2 geometric means for page sizes 1/64 were 3.28x/3.30x at K=512, 3.24x/3.28x at K=1024, and 3.07x/3.15x at K=2048; the overall same-page geometric mean was 3.22x. Page size 64 reduced latency relative to page size 1 by 3.30% for JIT-v2 and 1.88% for FlashInfer. FlashInfer remained 2.83x faster than the PyTorch fallback by geometric mean.

For the DeepSeek V4 routing proxy (K=512, page_size=64), FlashInfer used 3.30x JIT-v2 latency across the 15 shapes. Per-sequence geometric means were 2.42x at 8,192, 3.35x at 65,536, and 4.43x at 131,072. At batch 16, the exact ratios were 12.1856/5.0381 us = 2.42x, 47.5974/16.6707 us = 2.86x, and 77.8629/15.5548 us = 5.01x.

For the current GLM-5 routing proxy (K=2048, FlashInfer page size 1 versus JIT-v2 page size 64), FlashInfer used 3.23x JIT-v2 latency across the 15 paired shapes (range 2.32x-6.55x). Per-sequence geometric means were 2.39x at 8,192, 3.15x at 65,536, and 4.48x at 131,072. At batch 16, the exact ratios were 12.9030/5.3658 us = 2.40x, 51.9648/20.3158 us = 2.56x, and 88.1805/17.0515 us = 5.17x.

These are CUDA-graph operator results, not serving results. Inputs use random FP32 scores, full lengths, identity page tables, contiguous tensors, and preallocated outputs; the grid does not cover ties, short rows, padded score strides, or raw-index output. PyTorch uses sorted top-k. The 131,072-token case is substantially closer to GLM-5 than the earlier grid but remains below its 202,752-token maximum. No DeepSeek V4 or GLM-5 end-to-end throughput result is claimed.

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

CI States

Latest PR Test (Base): ✅ Run #33354611262
Latest PR Test (Extra): ❌ Run #33354611056
Latest PR Test (AMD ROCm 7.2): ❌ Run #33354611147

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

bkryu pushed a commit to flashinfer-ai/flashinfer that referenced this pull request Aug 9, 2026
<!-- .github/pull_request_template.md -->

## 📌 Description

@HumansAnd

SGLang's DeepSeek V4 indexer uses a compact page table in which one
entry represents 64 score positions. Its score rows may also have
padding between rows, and CUDA graph capture owns the translated and
raw-index output buffers. The current `top_k_page_table_transform`
contract assumes one page-table entry per score and allocates only the
translated output, so SGLang has to split this into `top_k`, score
re-gathering, compact-page translation, and output copies.

This PR extends the existing fused page-table transform for that layout:

- Add `page_size`, defaulting to `1` for backward compatibility.
- Add optional caller-owned `out` and `out_raw_indices` buffers. Raw
indices remain positionally aligned with translated indices, including
deterministic post-sort and `-1` padding; the two buffers must be
disjoint.
- Honor the input row stride instead of requiring tightly packed rows,
while preserving last-dimension contiguity and alignment-safe
vectorization.
- Propagate the contract through the Python API, TVM FFI binding, Radix
and Filtered implementations, graph-safe dispatch, deterministic
post-sort, and the trivial `length <= k` path.
- Extend only the operation-local trace template for `page_size`. Its
core one-output definition excludes destination buffers; raw-output
calls are deliberately not emitted and use a distinct routing identity
so Trace Apply falls back to the API.

For each selected local score index `idx`, the compact transform is:

```text
physical_page = src_page_table[
    batch_idx, page_table_row_start + idx // page_size
]
output = physical_page * page_size + idx % page_size
```

`page_table_row_starts` is measured in page-table entries, while
`row_starts` is measured in score elements. When `page_size > 1` and
`row_starts` is supplied, callers must therefore provide
`page_table_row_starts` explicitly rather than relying on the existing
shared-start behavior.

The C++ path uses policy types rather than a second boolean mode. The
FFI boundary selects `DirectPageTableKernelPolicy` or
`ConfigurablePageTableKernelPolicy` once, and the kernel ABI is derived
structurally from the policy type: an empty policy contributes zero
arguments, while a stateful trivially-copyable policy contributes one
object. Host dispatch carries one typed policy value through the
selection stack; only the terminal launch forms the zero-or-one argument
pack. The direct policy therefore adds no kernel argument or device
branch, while the configurable policy owns score-row layout,
logical-to-physical translation, and the optional raw-index sink. Future
transforms that share the flat row-selection contract can extend the
configurable policy without multiplying kernel variants or changing the
selection kernels. Direct translation sites retain their original
expressions so supported-path SASS is preserved exactly.

This is a clean extension of the API introduced in #4169: `page_size=1`,
omitted output buffers, and tightly packed inputs retain the existing
behavior and cluster fast path. [SGLang
#33237](sgl-project/sglang#33237) uses this API
to replace its DeepSeek V4 unfused workaround with one graph-safe
FlashInfer call.

## ⚡ Performance

Fresh performance validation compares the exact rebase base
`29196cf437778906c72630dc5d9850de547501de` with head
`cf0319a3497e252219544c0a8b4168c6ba598f88` on the same NVIDIA B200
(driver 580.126.09), CUDA 13.2.78, and PyTorch 2.13.0+cu132. Base and
head used separate editable source trees and JIT workspaces.
`benchmarks/bench_topk.py` is byte-identical on both sides (SHA-256
`57cd1ca61b38120380cb9ea7cf81ae3ee972484724bb2bb785649ae05cc199d9`).

The script uses CUPTI (`cupti-python` 13.2.0 and `nvidia-cuda-cupti`
13.2.75), 10 dry runs, 100 measured iterations, cold L2, and the median.
After #4295 it already sets `use_cuda_graph=False`, so no temporary
benchmark-source edit was needed and the CUPTI plus CUDA graph
instability is excluded.

Exact PR-body commands:

```bash
python3 benchmarks/bench_topk.py \
  --op dsa_topk --dtype bf16 --dsa-input-pattern dsa_relu \
  --dsa-case all --dsa-topk 2048 --tie-break

python3 benchmarks/bench_topk.py \
  --op varlen --dtype bf16 --length-dist causal \
  --varlen-k 2048 --varlen-q-len 128 --tie-break
```

After #4295 these exact commands keep `deterministic=False`: they
measure the default nondeterministic path plus SMALL/LARGE tie selection
without the canonical output-order sort. For fair DSA pairing, the
confirmation runs used the same command after `torch.manual_seed(1234)`
and `torch.cuda.manual_seed_all(1234)`. Current-mode DSA ran
base/head/head/base; varlen used base/head/head/base and its built-in
per-case seeds. Canonical-output coverage repeated both commands with
`--deterministic` on base and head. All comparisons use matched per-case
medians; negative PR delta means the head is faster.

| workload / mode | default PR delta (worst) | deterministic PR delta
(worst) | tie-small PR delta (worst) | tie-large PR delta (worst) |
|---|---:|---:|---:|---:|
| DSA, current ABBA | `+0.05%` (`+0.13%`) | n/a | `-0.09%` (`+0.08%`) |
`-0.01%` (`+0.12%`) |
| page-table varlen, current ABBA | `+0.01%` (`+0.10%`) | n/a | `+0.06%`
(`+0.19%`) | `+0.05%` (`+0.24%`) |
| ragged varlen, current ABBA | `+0.02%` (`+0.08%`) | n/a | `+0.01%`
(`+0.13%`) | `-0.02%` (`+0.14%`) |
| DSA, explicit deterministic | `+0.03%` (`+0.15%`) | `-0.09%`
(`+0.10%`) | `+0.01%` (`+0.41%`) | `-0.09%` (no regressed point) |
| page-table varlen, explicit deterministic | `+0.01%` (`+0.10%`) |
`-0.18%` (`+0.03%`) | `-0.09%` (no regressed point) | `-0.05%`
(`+0.09%`) |
| ragged varlen, explicit deterministic | `-0.01%` (`+0.06%`) | `-0.10%`
(`+0.01%`) | `-0.01%` (`+0.03%`) | `-0.13%` (`+0.08%`) |

All 12 varlen rows had identical `len_min`, `len_mean`, `len_max`, and
`triv%` across paired runs. Current-mode suite geomeans are within
`+0.06%`, the largest individual delta is `+0.24%`, and deterministic
geomeans are flat or faster apart from a `+0.01%` DSA tie-small geomean.
This supports no measurable kernel-latency regression after the final
rebase.

These sweeps exercise the existing direct compatibility paths
(`page_size=1`, contiguous rows, no caller-owned outputs). The
configured V4 path has no pre-PR API equivalent. Its policy cleanup was
separately checked by an eager ABBA host audit using `page_size=64`,
padded row stride, raw output, and all three production shapes: its
configured 12-case batched end-to-end geomean was `+0.053%`, with a
worst point of `+0.380%`.

The policy design also has complementary binary evidence from the
exhaustive audit performed after #4295:

```text
direct: 354/354 affected kernels, 0 normalized SASS/resource/KPARAM mismatches
  radix=180, filtered=132, finalizer=42
configured: 116 kernels
  radix=60, filtered=44, finalizer=12
  exactly one trailing 24-byte policy object; PageTable mode only
```

The direct variants retained their upstream parameter counts and
constant-bank spans. The configured variants add one policy object
without a second policy family or boolean template axis.

## 🔍 Related Issues

- SGLang integration: sgl-project/sglang#33237
- Independent score/page-table starts:
#4169
- SGLang packed-PAGED workaround and backend-selection fix:
sgl-project/sglang#32490
- SGLang DeepSeek V4 Top-K backend integration:
sgl-project/sglang#31087

## 🚀 Pull Request Checklist

Thank you for contributing to FlashInfer! Before we review your pull
request, please make sure the following items are complete.

### ✅ Pre-commit Checks

- [x] I have installed `pre-commit` by running `pip install pre-commit`
(or used your preferred method).
- [x] I have installed the hooks with `pre-commit install`.
- [x] I have run the hooks manually with `pre-commit run --all-files`
and fixed any reported issues.

> If you are unsure about how to set up `pre-commit`, see [the
pre-commit documentation](https://pre-commit.com/).

## 🧪 Tests

- [x] Tests have been added or updated as needed.
- [x] All tests are passing (`unittest`, etc.).

Validation used an editable source build on one NVIDIA B200 with CUDA
13.2.78 and PyTorch 2.13.0+cu132. The rebased head was synced to an
isolated source tree and JIT workspace on `flashinfer-pr4366-cu132`:

```text
pre-commit run --all-files
Passed

python3 -m pytest -q \
  tests/utils/test_topk.py::test_top_k_page_table_transform_misaligned_scores_without_row_starts \
  tests/utils/test_topk.py::test_top_k_page_table_transform_compact_pages_cuda_graph_replay
18 passed, 3 warnings in 93.98s

python3 -m pytest -q \
  tests/topk_varlen/test_topk_varlen.py::test_radix_preallocated_outputs \
  tests/topk_varlen/test_topk_varlen.py::test_out_values_ignored_when_return_values_false
6 passed, 39 warnings in 4.41s

python3 -m pytest -q \
  tests/trace/test_fi_trace_template_consistency.py \
  tests/trace/test_template_init.py \
  -k top_k_page_table_transform
6 passed, 1 skipped, 972 deselected, 3 warnings in 0.27s

python3 -m pytest -q tests/utils/test_topk.py
1495 passed, 3 warnings in 7.56s
```

Final CUDA 13.2 validation was rerun on rebased head
`cf0319a3497e252219544c0a8b4168c6ba598f88`. It covers the page-table
changes plus the optional-output overlap from #3901 after conflict
resolution. `git range-diff`, `git diff --check`, and `pre-commit run
--all-files` also passed.

The Top-K matrix covers Radix multi-CTA and Filtered dispatch,
graph-safe mode, deterministic mode, optional raw output for default and
compact page sizes, independent score/page-table starts, compact and
default page sizes, padded row strides, misaligned input bases, trivial
and selected rows, and CUDA graph replay with mutated inputs. The trace
checks cover the operation-local schema and default-argument
initialization. A separate smoke check also verified that the Python
`page_size <= 2**30` validation matches the native contract.

The warnings are existing CUTLASS DSL deprecations from
`tests/conftest.py` and `flashinfer/cute_dsl/utils.py`.

## Reviewer Notes

Review focus is welcome on the positional pairing of raw and translated
outputs across deterministic post-sort, the structural zero-or-one
policy ABI, and the page-table transform in the Radix multi-CTA and
graph-safe Filtered epilogues.




<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
  * Added support for compact page tables with configurable page sizes.
* Added optional raw-index outputs alongside translated physical
indices.
  * Added reusable output buffers for flexible result storage.
* Improved support for empty rows, padding, non-contiguous inputs, and
physical-page remapping.
* **Bug Fixes**
  * Added validation for page metadata and output compatibility.
  * Improved CUDA graph and algorithm-path support.
* **Documentation**
* Updated API and tracing documentation for page sizes, physical
indices, and raw-index outputs.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
@ziang-and
ziang-and force-pushed the agent/dsv4-flashinfer-fused-topk branch from 0534ef4 to fd150ca Compare August 9, 2026 06:17
@zianglih zianglih changed the title feat(dsa): adopt fused FlashInfer page-table top-k feat(dsv4): use fused FlashInfer top-k for DeepSeek V4 Aug 9, 2026
@zianglih zianglih changed the title feat(dsv4): use fused FlashInfer top-k for DeepSeek V4 feat(dsv4): support --dsa-topk-backend flashinfer with fused top-k Aug 10, 2026
@zianglih
zianglih marked this pull request as ready for review August 10, 2026 07:11
@ziang-and
ziang-and force-pushed the agent/dsv4-flashinfer-fused-topk branch from 4b99b82 to 1e15e64 Compare August 16, 2026 06:11
@zianglih zianglih changed the title feat(dsv4): support --dsa-topk-backend flashinfer with fused top-k [FlashInfer V0.6.18] feat(dsv4): support --dsa-topk-backend flashinfer with fused top-k Aug 19, 2026
@zianglih

Copy link
Copy Markdown
Contributor Author

rebasing, now depending on #36954

@ziang-and
ziang-and force-pushed the agent/dsv4-flashinfer-fused-topk branch from 798019b to 08749f3 Compare August 29, 2026 06:57
@ziang-and
ziang-and force-pushed the agent/dsv4-flashinfer-fused-topk branch from 08749f3 to d05fef2 Compare August 29, 2026 07:00

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

Just lint to fix I think, plus a few small inline comments. Ran DSV4 on GB300 with 0.6.18, looks good.

Comment thread test/registered/kernels/ops/attention/test_dsa_indexer.py Outdated
Comment thread python/sglang/srt/layers/attention/dsv4/indexer.py Outdated
Comment thread python/sglang/srt/layers/attention/dsv4/metadata.py Outdated
Comment thread python/sglang/srt/layers/attention/dsv4/metadata.py Outdated
Comment thread test/registered/unit/layers/test_dsv4_nonpaged_indexer.py Outdated
@zianglih

Copy link
Copy Markdown
Contributor Author

/tag-and-rerun-ci

@zianglih

Copy link
Copy Markdown
Contributor Author

/tag-and-rerun-ci

@zianglih

zianglih commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

nv ci green

@Fridge003
Fridge003 merged commit 5edcd0a into sgl-project:main Sep 1, 2026
345 of 403 checks passed
1am9trash added a commit to RolaoDenthu/sglang that referenced this pull request Sep 1, 2026
StevenChenSE pushed a commit to StevenChenSE/sglang that referenced this pull request Sep 6, 2026
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.

3 participants