Skip to content

[Bugfix] FlashInfer autotune cache dropped every startup under expert parallelism - #40420

Open
zhang-keliang wants to merge 1 commit into
sgl-project:mainfrom
zhang-keliang:fix/40320-autotune-cache-ep-divergence
Open

zhang-keliang wants to merge 1 commit into
sgl-project:mainfrom
zhang-keliang:fix/40320-autotune-cache-ep-divergence

Conversation

@zhang-keliang

@zhang-keliang zhang-keliang commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Fixes #40320. _autotune_cache_digest hashes each rank's whole cache file to decide whether the ranks agree. Under expert parallelism each rank tunes a disjoint subset of MoE shapes, so the per-rank tactics legitimately differ — len(set(digests)) == 1 cannot hold, and the cache is unlinked on every boot. Tactics are then re-drawn from timing noise on each start, so the same configuration gives different throughput across boots.

Modifications

load_configs in flashinfer is all-or-nothing: it accepts a per-rank cache file only when its _metadata matches the current environment, otherwise it skips the whole file. The cross-rank agreement the entry gate needs is that every rank makes the same load decision under the current environment — not that tactic entries agree. Digest the (loadable, stamp, env) triple instead of the file bytes. +7/-3 in flashinfer_autotune.py.

Two regression tests, both fail before / pass after:

  • TestAutotuneCacheDigest::test_disjoint_shape_sets_digest_alike_when_env_matches — unit-level: two rank caches with the same _metadata but disjoint per-shape entries must digest alike.
  • TestDropDivergedAutotuneCache::test_disjoint_shape_sets_with_matching_env_are_kept — integration over a real gloo group and real files: both ranks' caches must survive the gate.

Two tests were removed:

  • test_diverged_caches_are_dropped_on_every_rank enforced the byte-equality contract this fix disproves.
  • test_key_order_does_not_matter degrades to a mirror under the new digest and is subsumed by the new disjoint-content test above.

The two negative-branch tests remain: test_matching_caches_are_kept (positive) and test_caches_diverging_only_in_metadata_are_dropped (the real desync we still want to catch).

Accuracy Tests

Not applicable — this changes the cache-entry gate, not the tuning path itself. Full test_flashinfer_autotune_sync.py: 11 pass, 1 CUDA-only skip.

Speed Tests and Profiling

Not applicable directly. The intended effect is that under EP the tuned tactics are reused across boots instead of being re-drawn from timing noise; on the reporter's 4x GB10 DGX Spark setup the whole cache was being deleted on every start.

Checklist

  • Format your code according to the Format code with pre-commit.
  • Add unit tests according to the Run and add unit tests.
  • Update documentation (not applicable).
  • Provide accuracy and speed benchmark results (not applicable — see above).
  • Follow the SGLang code style guidance.

CI States

Latest PR Test (Base): ❌ Run #35494840938
Latest PR Test (Extra): ❌ Run #35494840832
Latest PR Test (AMD ROCm 10): ❌ Run #35494841106

… the load decision

_autotune_cache_digest hashed each rank's whole cache file to decide whether
the caches agree. Under expert parallelism each rank tunes a disjoint subset
of MoE shapes, so the digests never matched and the cache was unlinked every
boot; tactics were re-drawn from timing noise on every start.

load_configs is all-or-nothing on _metadata, so the consistency requirement
is that every rank would make the same load decision under the current
environment -- not that their tactic entries agree. Digest the (loadable,
stamp, env) triple instead.

Signed-off-by: summer <[email protected]>
detain added a commit to detain/sglang that referenced this pull request Oct 20, 2026
…/deferred/avoid map

- 3A [24-35]: NVFP4 MoE boot sgl-project#39807/sgl-project#40173, block-FP8 TP refine sgl-project#42566, k-pool indexer split sgl-project#43619,
  SM120 skinny GEMM sgl-project#42865/sgl-project#42867, DeepGEMM warmup guards sgl-project#32686/sgl-project#43712, autotune sgl-project#43363/sgl-project#40420,
  mHC overlap sgl-project#42985, FP8-einsum precompile sgl-project#42821.
- 3B [36-43]: PCIe CustomAllreduce VMM sgl-project#44448, EP local expert ids sgl-project#44480, shm TP broadcast sgl-project#44202,
  PCIe-IPC all-gather sgl-project#42863 + multicast guard sgl-project#41888, TP-sampling consistency sgl-project#41916/sgl-project#38565,
  draft embed/lm_head VRAM sgl-project#37155.
- sgl-project#39807 toolchain commit verified safe against our CUDA_HOME=12.9 pin (cu13 fallback needs CUDA_HOME unset AND glibc>=2.41; we are 2.39).
- stack_want.txt: execution-status banner. Branch tip 5dd6ec7.
detain pushed a commit to detain/sglang that referenced this pull request Oct 29, 2026
… the load decision

_autotune_cache_digest hashed each rank's whole cache file to decide whether
the caches agree. Under expert parallelism each rank tunes a disjoint subset
of MoE shapes, so the digests never matched and the cache was unlinked every
boot; tactics were re-drawn from timing noise on every start.

load_configs is all-or-nothing on _metadata, so the consistency requirement
is that every rank would make the same load decision under the current
environment -- not that their tactic entries agree. Digest the (loadable,
stamp, env) triple instead.

Signed-off-by: summer <[email protected]>
(cherry picked from commit 10c6feb)
PR: sgl-project#40420 (open, ported)
(cherry picked from commit 763e886d45cb4b605597479e4d9d554c26d673ce)
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)
@adi-suresh01

Copy link
Copy Markdown
Contributor

I tested this on 2x RTX PRO 6000 Blackwell (sm_120), main at 7e31007a with FlashInfer 0.7.4, --moe-runner-backend flashinfer_cutlass, Qwen3-30B-A3B-NVFP4, one cold and one warm boot per arm. The flashinfer_autotune.py hunk applies to main with a 47-line offset; the test-file hunk no longer applies, since test_flashinfer_autotune_sync.py has grown on main.

  • Stock FlashInfer 0.7.4: the two rank files are byte-identical (fix(moe): keep CUTLASS MoE autotune cache keys rank-invariant flashinfer-ai/flashinfer#6006 took the ranks out of the CUTLASS MoERunner key), so main already keeps the cache there, with or without this PR: 0 gate drops, 0 entries re-measured.
  • With tp_rank, ep_rank and cluster_rank put back into that key (the pre-[RFC] sm75 EOL #6006 format, my patch): main drops and re-measures 51 of 54 entries per rank at TP2/EP2 and 54 of 54 at TP2. With this PR: no drop, 0 re-measured.
  • Rank 1's file deleted: still drops, both ranks tune, the boot finishes.
  • One entry removed from rank 1's file, both files still loadable: no drop, both ranks profiled that one entry together, no hang. This leans on FlashInfer's per-profile hit agreement (_all_ranks_hit_cache), which is in v0.7.3 and v0.7.4 and not in v0.7.2 or earlier. On those versions nothing would keep the ranks together in this case; I did not test it there.

One note on the docstring: at TP2/EP2 the ranks tuned the same shapes, and the files differed only by the rank fields in the key (with those taken out, the two files were identical). So "each rank tunes a disjoint subset of MoE shapes" does not match what I saw on this runner; the rank fields in the key are why the files differ.

Full table and the open GB10 question: #40320 (comment)

@leejnau

leejnau commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the fix and the clear root-cause write-up. I measured this on current main (FlashInfer
0.7.4), and I'd suggest going one step further: delete _drop_diverged_autotune_cache and
_autotune_cache_digest entirely.

Why that is safe. Since FlashInfer 0.7.3, SGLang sets the TP CPU group via
set_autotune_process_group(...), and FlashInfer agrees on cache hits per profile across that
group. The set_autotune_process_group docstring says: "Cached selections must agree; any miss or
disagreement makes every rank re-profile." SGLang also always runs the warmup forward; there is no
skip on a cache hit, so every rank makes the same sequence of tuning calls. Together these make
a whole-file gate redundant, and the metadata-only digest in this PR too. A rank whose file is
missing, stale, or holds different entries simply misses those profiles, and FlashInfer re-profiles
them on every rank. @adi-suresh01's removed-entry run above shows the same on SM120.

Experiment. B100, Qwen/Qwen3-30B-A3B-FP8 --tp 2, FlashInfer 0.7.4:

  1. Cold boot.
  2. Warm boot.
  3. Change one tactic in rank 1's JSON, then boot again.
  4. Warm boot.
main (whole-file gate) main with the gate call removed
Boot after the perturbation Both ranks' caches discarded; full retune of 1326 measurements Only the disputed profile re-profiled: 46 measurements (23 per rank)
Served tactics vs. the cold boot (208 lookups) 88 redrawn All identical
Ranks agree on every lookup / any hang yes / no yes / no
Next warm boot 0 measurements 0 measurements

Without the gate, rank-local entries, as in the GB10 TP4/EP2 report that opened #40320, should also
survive restarts: a disagreement costs one re-profile of that profile, never the whole cache. I
tested the perturbed-entry case above, not a rank-local-shape model.

Would you like to update this PR to remove the gate and the digest? The digest and drop-gate tests
would go with them; TestAutotuneTacticSyncGroup stays. I'm happy to share the harness, or to help.

@leejnau

leejnau commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Hi @zhang-keliang, following up on my comment above: are you still planning to work on this PR?

If you're busy, I'm happy to take it over. I'd open a follow-up PR that deletes _drop_diverged_autotune_cache and _autotune_cache_digest (with their tests), as described above, links #40320, and credits you as co-author. If you'd rather keep it here, enabling "Allow edits by maintainers" would let me push the change to this branch instead.

Either way works. If I don't hear back in a week or so, I'll go ahead with the follow-up PR so the cache stops being discarded on EP startups.

@leejnau

leejnau commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

@zhang-keliang I've opened #54439, which deletes the gate and the digest as described above, and credits you as co-author. Thanks again for finding the root cause. If you'd rather land it here instead, let me know and I'll close mine.

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

3 participants