Skip to content

[Perf] runtime: Tune FlashInfer MoE tactics on distinct dummy tokens - #45543

Open
Venkat2811 wants to merge 1 commit into
sgl-project:mainfrom
Venkat2811:venkat/autotune-realistic-decode-batch
Open

Venkat2811 wants to merge 1 commit into
sgl-project:mainfrom
Venkat2811:venkat/autotune-realistic-decode-batch

Conversation

@Venkat2811

@Venkat2811 Venkat2811 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

  • FlashInfer's MoE autotune runs a decode dummy forward on zero-filled buffers
    (base_runner.py).
  • Every row is token 0, so every row is routed to the same experts.
  • FlashInfer tunes its MoE tactics on these repeated routes, a pattern real decode never produces.
  • On Kimi-K3 (896 experts, top-16, EP8) the resulting picks are 13-16 % slower per MoE layer at 24-32 tokens,
    and the pick changes from launch to launch.

Modifications

  • flashinfer_autotune.py: autotune_dummy_input_ids() fills the static input_ids with seeded, distinct token ids
    for the tuning forward and zeroes them afterwards.
  • base_runner.py: the decode tuning forward runs inside it.
  • Unchanged: the other dummy buffers, the extend tuning pass, and the cache key and format (an existing cache is reused
    until it is retuned).

Tested configurations

Kimi-K3 gpt-oss-120b
Hardware 8x B300 8x B300
Server --tp-size 8 --dcp-size 8 --ep-size 8 --tp 8
MoE runner flashinfer_mxfp4 flashinfer_mxfp4
Builds main @ 3c56008, with and without this change same
Speed bench_one_batch_server, input 8192, output 512 input 2048, output 512
Launches 2 per build, fresh autotune each same
Accuracy sgl-eval gsm8k, 1,319 questions -

The PR is rebased onto current main; the unit tests pass there.

Results

Kimi-K3, aggregated (decode output tok/s, mean of 2 launches)

batch 8 batch 16 batch 24 batch 32
main @ 3c56008 554.7 795.4 1077.1 1305.3
this PR 555.2 (+0.1 %) 856.7 (+7.7 %) 1132.1 (+5.1 %) 1372.4 (+5.1 %)
together with #45544 592.4 (+6.8 %) 898.9 (+13.0 %) 1168.0 (+8.4 %) 1397.4 (+7.1 %)

gpt-oss-120b, aggregated (decode output tok/s, mean of 2 launches)

batch 8 batch 16 batch 32 batch 64
main @ 3c56008 3045.1 5661.0 10260.2 17571.3
this PR 3017.8 (-0.9 %) 5636.7 (-0.4 %) 10253.7 (-0.1 %) 17612.9 (+0.2 %)
  • Neutral within the launch ranges: on this model (128 experts, top-4) main already picks the fast tiles.

Accuracy, Kimi-K3 (sgl-eval gsm8k, model preset, max_tokens 32768)

score
main @ 3c56008 97.12 %
this PR 97.73 %
  • Within the noise of one temperature-1.0 pass; the change only picks GEMM tactics.

Tests

Four CPU tests in test/registered/unit/model_executor/runner/test_flashinfer_autotune_sync.py:

  • the tuned forward sees distinct in-vocab ids and the buffer is zero afterwards (fails without the change: 1 != 56);
  • every rank tunes on the same ids;
  • the buffer is zeroed when the forward fails;
  • more rows than the vocabulary stay in range.

pre-commit passes on the changed files.

Reproduce
git clone https://github.com/sgl-project/sglang && cd sglang
git checkout 3c560089e1818a5217a73f2168273485a5350341      # main; apply this PR's diff for the other build
pip install -e python
pytest test/registered/unit/model_executor/runner/test_flashinfer_autotune_sync.py

export SGLANG_FLASHINFER_AUTOTUNE_CACHE=0 SGLANG_CACHE_DIR=$(mktemp -d)   # fresh autotune per launch

python -m sglang.launch_server --model-path moonshotai/Kimi-K3 --trust-remote-code \
  --tp-size 8 --dcp-size 8 --ep-size 8 --mem-fraction-static 0.85 \
  --linear-attn-prefill-backend triton --linear-attn-decode-backend triton \
  --reasoning-parser kimi_k3 --port 30000
python -m sglang.bench_one_batch_server --model-path moonshotai/Kimi-K3 \
  --base-url http://127.0.0.1:30000 --batch-size 8 16 24 32 --input-len 8192 --output-len 512

python -m sglang.launch_server --model-path openai/gpt-oss-120b --tp 8 \
  --reasoning-parser gpt-oss --port 30000
python -m sglang.bench_one_batch_server --model-path openai/gpt-oss-120b \
  --base-url http://127.0.0.1:30000 --batch-size 8 16 32 64 --input-len 2048 --output-len 512

uv venv .sgl-eval && uv pip install --python .sgl-eval/bin/python sgl-eval==0.1.3
.sgl-eval/bin/sgl-eval run gsm8k --base-url http://127.0.0.1:30000/v1 \
  --load-preset-from-model-id moonshotai/Kimi-K3 --max-tokens 32768 --num-examples 1319

Checklist

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ❌ Run #39562909397
Latest PR Test (Extra): ❌ Run #39562909380
Latest PR Test (AMD ROCm 10): ❌ Run #39562909370

The decode-shaped autotune forward ran on zero-filled static buffers, so
every row embedded token 0, the MoE router sent all rows to the same experts
and FlashInfer ranked its MoE tactics on that pattern. Give the decode dummy
forward seeded distinct token ids and zero the buffer after.

Kimi-K3 MXFP4 on 8x B300 (TP8 DCP8 EP8, FlashInfer 0.7.2): tactics tuned on
realistic routes are 13-16 % faster per MoE layer at 24-32 rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
detain pushed a commit to detain/sglang that referenced this pull request Oct 29, 2026
The decode-shaped autotune forward ran on zero-filled static buffers, so
every row embedded token 0, the MoE router sent all rows to the same experts
and FlashInfer ranked its MoE tactics on that pattern. Give the decode dummy
forward seeded distinct token ids and zero the buffer after.

Kimi-K3 MXFP4 on 8x B300 (TP8 DCP8 EP8, FlashInfer 0.7.2): tactics tuned on
realistic routes are 13-16 % faster per MoE layer at 24-32 rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
(cherry picked from commit 44b7dfd)
PR: sgl-project#45543 (open, ported; conflict in test_flashinfer_autotune_sync.py resolved as local
union: sgl-project#40420's disjoint-shape tests kept alongside the new dummy-token
tests; upstream-only context class TestModelPrefillAutotune (API absent from
this stack) not carried in).
(cherry picked from commit 677a076a2b318007764744894819b1abe9d91b6c)
detain added a commit to detain/sglang that referenced this pull request Oct 29, 2026
sgl-project#44514 from port-autotune (entries 257-260)

Batch 2 of the 2026-10-28 second wave. Four picks landed CLEAN in strict
order onto the comm docs tip -- code tip 2705ebb:
f46e547 sgl-project#40420 (open port, EP autotune-cache digest: per-rank caches
no longer dropped every startup; worker RED-proof 2F->2P), 894d4a7
sgl-project#45543 (open port, seeded distinct dummy token ids for the decode MoE
autotune; base_runner union preserves the entry-176 EXTEND call site;
upstream-only TestModelPrefillAutotune NOT carried in --
autotune_prefill_kernels absent, grep-verified), 6c866f3 sgl-project#43927
(merged pick 33618d4, default tactic joins tuning -- PARTIALLY
SATISFIES the C0a sweep-gap record; entry-243/246 'no tactic=-1'
PASS-needles STALE, CAVEATS bullet added), 2705ebb sgl-project#44514 (merged
pick 1897575, cache-key bump -> exactly ONE cold autotune boot,
entry-228 boot-cost practice).

Header bookkeeping for BOTH batches: STACK bumped to
qwennext-stack-260-2026-10-28 with the comm + autotune wave lines,
entries 1-260, tip 2705ebb, updated: 2026-10-28 (entries 255-260)
block. FAM-A1 LANDED + NEW TODO S-EXT1 (stack-owned: EXTEND autotune
still tunes on zero-filled dummies -- the bug sgl-project#45543 fixed for decode).

Verified at landed tips (prod venv, CVD=9), all EXACT vs predictions:
sync+gate 22P/1S/5sub, runner dir 89P/1S/47sub, MoE set 139P/22S/141sub,
qwen4_exp_ple_staging 37P; comm named set 114P/1S/30sub, dir
120P/1S/35sub 0F, new comm files collect 18. In-flight workers NOT
landed -- no drift attributable. NOTHING PUSHED.
detain added a commit to detain/sglang that referenced this pull request Oct 29, 2026
…kens (stack-owned, follows sgl-project#45543)

Rationale: our entry-176 EXTEND pass (maybe_flashinfer_autotune_extend)
allocates fresh ZERO-FILLED dummy buffers via _alloc_dummy_decode_buffers
and ran its prefill-sized tuning forward on them -- every row embedded
token 0, the MoE router sent all rows to the same experts, and FlashInfer
ranked the extend-bucket tactics on a routing pattern no real prefill
produces. That is the exact lie sgl-project#45543 fixed for the decode pass (claimed
13-16%/layer MoE gains, measured on different hardware: direction, not
measurement), and since sgl-project#44514 these picks persist under the v2_default
cache key, so the zero-tuned extend records were being reused boot after
boot.

Fix: wrap the single extend-pass dummy forward in the SAME seeded
autotune_dummy_input_ids helper sgl-project#45543 landed -- reuse, no forked second
implementation. One wrap point covers every extend-pass dummy forward:
both base_runner call sites (~:295 target, ~:299 draft) route through this
function. Buffer lifecycle verified at the site before choosing (S-EXT1's
open check): the helper fills ids BEFORE the tuning context and zeroes
AFTER it, exactly like the decode wrap; the extend buffers are function-
local and released in the finally, which the zeroed re-check preserves.
Default-on semantics and the entry-176 size/shape logic are UNCHANGED --
this is a dummy-INPUT fix, not a scope expansion.

Skipped (input-dependent, already covered): the MegaMoE prefill profile
inside flashinfer_autotune_context profiles topk_ids/topk_weights captured
from the DECODE-shaped dummy forward -- distinct since sgl-project#45543 -- and
builds its capacity profiles independently of buffers.input_ids; wrapping
it too would retune nothing. eager_runner/prefill_cuda_graph_runner EXTEND
sites are runtime/capture paths, not autotune, and take real batch tokens.

Cache-key finding (checked, not changed): neither the v2_default cache
path nor the sgl-project#40420 sync digest hashes anything about dummy tokens, so
this patch does NOT cold-boot the cache. Consequence: extend-bucket
records tuned on zeros under an existing v2_default key stay VALID and
keep hitting. Entry 260's scheduled cold boot has not happened yet (the
2026-10-28 batch is live-only-after-restart), so landing S-EXT1 before
the next restart folds the forced retune into that boot at zero extra
cost. If any prod boot with a warm cache happens FIRST, a forced-retune
needle is required: delete the per-rank rank_tpX_ppY_dpZ.json files --
note SGLANG_FLASHINFER_AUTOTUNE_CACHE=0 does NOT refresh the canonical
file (the cache-disabled path writes a timestamped runs/ copy only) --
or add a one-time dummy_tokens=seeded key part mirroring sgl-project#44514's
precedent, deliberately left out of this patch's scope.

Tests: test_flashinfer_autotune_sync.py gains TestExtendAutotuneDummy-
InputIds (4 CPU tests mirroring the sgl-project#45543 class): extend forward sees
seeded distinct in-vocab ids and releases buffers zeroed; ids equal the
helper's own output (guards against a divergent second implementation);
equal across runs (rank determinism); zeroed after a failing forward.
Teeth: production hunk reverted -> 3 failed / 1 passed ("1 != 16384");
restored -> 4 passed. Battery at tip vs after, CVD=9 CPU mode: sync+gate
22P/1S/5sub -> 26P/1S/5sub; runner dir 89P/1S/47sub -> 93P/1S/47sub;
qwen4_exp_ple_staging 37P -> 37P; MoE set 139P/22S/141sub -> identical.
ruff 0.15.1 (F401,F821,F823,UP037) + format clean on both files.

(no upstream PR; fork debt S-EXT1)

This branch has not been deployed

No deployments
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