Skip to content

[Fix] deepseek-v4: Keep the draft MoE out of the target's expert placement - #45299

Merged
kpham-sgl merged 1 commit into
sgl-project:mainfrom
weireweire:dsv41-dspark-draft-expert-location
Oct 20, 2026
Merged

kpham-sgl merged 1 commit into
sgl-project:mainfrom
weireweire:dsv41-dspark-draft-expert-location

Conversation

@weireweire

@weireweire weireweire commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

DeepSeek-V4.1 with DSpark and MegaMoE cannot run EPLB. With --enable-eplb --ep-num-redundant-experts 16, the server dies while capturing the draft decode CUDA graphs: DeepGEMM's MegaMoE kernel times out in its grid barrier (Grid sync timeout, barrier.cuh), which later surfaces as unspecified launch failure.

DeepSeek-V4 draft MoE layers (DSpark stages and NextN) are DeepseekV2MoE layers built with is_nextn=True and zero-based layer ids. DSpark stages also own a smaller expert set: 128 routed experts, against the target's 384 over 40 layers.

EPLB only rebalances the target. The draft never gets an EPLB manager and exposes no routed_experts_weights_of_layer, yet the target's expert placement leaked into the draft in three ways:

  • the draft MoE was sized with the target's redundant expert slots;
  • FusedMoE.weight_loader indexed the draft's own expert tensors through the target's logical-to-physical map for layer 0..2;
  • the MegaMoE paths (CUDA mega_moe and AMD mega_moe_flydsl) built ExpertLocationDispatchInfo for layer_id 0..2 from the target's metadata.

With redundant experts, DSpark draft top-k ids were remapped to physical slots the draft MegaMoE does not own, and the kernel hung. For NextN MTP with the target's expert count, the same leak routed draft tokens by the live target map after a rebalance, while the draft weights stayed at the startup placement.

#34846 (closed) opted DSpark draft layers into dispatch instead. That cannot work for DeepSeek-V4.1, where the draft's expert count differs from the target's.

Modifications

  • FusedMoE takes uses_expert_location: bool = True. When it is False, weight_loader ignores the global expert-location metadata and uses the plain global-to-local mapping.
  • DeepseekV2MoE passes uses_expert_location=False for DeepSeek-V4 nextn layers. Those layers get no redundant expert slots.
  • mega_moe._run_mega_routed and mega_moe_flydsl._run_mega_routed pass no dispatch info for such layers. The other MoE paths in deepseek_v2.py already skip nextn layers.
  • moe_wna16 places qzeros with the same plain mapping for such layers, matching the weight loader.
  • The DSpark loader numbers the fused shared expert, and sizes its expert param mapping, after the stage's own routed experts, matching the plain mapping.

Target layers and non-DeepSeek-V4 models are unchanged. With redundant experts, a DeepSeek-V4 NextN layer on MegaMoE now gets its own MegaMoE buffer, since its expert count no longer matches the target's.

Out of scope, left for a follow-up:

  • non-V4 NextN layers on MegaMoE with EPLB, which keep today's behavior;
  • --enable-waterfill, which sizes the draft's balancer from the target's expert count plus redundant slots.

Accuracy Tests

GB300, DeepSeek-V4.1-Flash, EP4 with attention DP 4, MegaMoE, DSpark, --enable-eplb --ep-num-redundant-experts 16:

  • main: dies in draft decode CUDA graph capture.
  • This change: all graphs capture and the server serves.

GSM8K (sgl-eval, 400 questions, native speculative acceptance) shows no accuracy loss:

score accept length
no EPLB 97.50% ~4.26
EPLB, 16 redundant experts, two rebalances during the eval 98.00% ~4.16

Only DSpark was run on GPU; DeepSeek-V4 NextN MTP with EPLB was not. The isolated draft paths were also checked: on main, the MegaMoE draft path builds target dispatch info, and the draft weight loader indexes its experts through the target's map. Neither happens with this change.

Speed Tests and Profiling

Unchanged for target layers. For DeepSeek-V4 draft layers, behavior changes only when the target's placement is non-trivial (--enable-eplb, redundant experts, or a non-trivial --init-expert-location). This makes EPLB usable on DeepSeek-V4.1 DSpark + MegaMoE decode.

Checklist

  • Format your code according to pre-commit.

CI States

Latest PR Test (Base): ❌ Run #39365705959
Latest PR Test (Extra): ❌ Run #39365706063
Latest PR Test (AMD ROCm 10): ❌ Run #39365707455

@weireweire
weireweire force-pushed the dsv41-dspark-draft-expert-location branch 2 times, most recently from 3e03efa to ba04baf Compare October 20, 2026 03:24
@weireweire weireweire changed the title [Fix] deepseek-v4.1: Keep the DSpark draft MoE out of the target's expert placement [Fix] deepseek-v4: Keep the draft MoE out of the target's expert placement Oct 20, 2026
Root cause: DeepSeek-V4 draft MoE layers (DSpark stages and NextN) are
DeepseekV2MoE layers with is_nextn=True and zero-based layer ids; DSpark
stages also own a smaller expert set (128 vs the target's 384). EPLB only
ever rebalances the target, yet the draft was sized with the target's
redundant slots, loaded its experts through the target's logical-to-physical
map, and the MegaMoE paths (CUDA and AMD flydsl) built dispatch info for
layer_id 0..2 from the target's metadata. With redundant experts, DSpark
top-k ids were remapped to physical slots the draft MegaMoE does not own
and the kernel hung in its grid barrier ("Grid sync timeout") during draft
CUDA graph capture.

Fix: FusedMoE takes uses_expert_location (default True); DeepseekV2MoE
passes False for DeepSeek-V4 nextn layers, which then get no redundant
slots, load with the plain global-to-local mapping, and get no MegaMoE
dispatch info; moe_wna16 places their qzeros the same way. The DSpark
loader numbers the fused shared expert after the stage's own routed experts,
matching the plain mapping.

Validation: GB300, DeepSeek-V4.1-Flash, EP4/attention-DP4, MegaMoE, DSpark,
--enable-eplb --ep-num-redundant-experts 16: main dies in draft decode-graph
capture; with the fix all graphs capture and the server serves. GSM8K
(sgl-eval, 400 questions, native acceptance): no EPLB 97.50%, EPLB16 with
two rebalances during the eval 98.00%.
@kpham-sgl

Copy link
Copy Markdown
Collaborator

The per-layer opt-out is the right size for this bug. The underlying issue is that the expert-location metadata is a process singleton keyed by layer_id, and the draft reuses target ids 0..2, so every reader has to know to skip draft layers on its own. We will follow up with a more principled fix: a single gate in ExpertLocationDispatchInfo.init_new that takes the MoE layer, so the mega and non-mega paths share one predicate instead of uses_expert_location in one place and enable_eplb and not is_nextn in another.

(from Claude)

@kpham-sgl

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/e2e/models/test_deepseek_v4_flash_fp4_megamoe_b200.py test/registered/ep/test_eplb_no_a2a.py test/registered/moe/test_fused_append_remap_per_rank_shared_slots.py test/registered/unit/models/test_deepseek_v4_shared_expert_fusion.py

@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

🚀 4-gpu-b200 (1 test): ✅ View workflow run

cd test/ && python3 registered/e2e/models/test_deepseek_v4_flash_fp4_megamoe_b200.py

🚀 2-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/ep/test_eplb_no_a2a.py

🚀 1-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/moe/test_fused_append_remap_per_rank_shared_slots.py

🚀 ubuntu-latest (1 test): ✅ View workflow run

cd test/ && python3 registered/unit/models/test_deepseek_v4_shared_expert_fusion.py

@kpham-sgl
kpham-sgl merged commit c7687a0 into sgl-project:main Oct 20, 2026
106 of 123 checks passed
JustinTong0323 added a commit to JustinTong0323/sglang that referenced this pull request Oct 20, 2026
Same sgl-project#45299 fallout, one file over: _layer() mocks moe.experts for the
flydsl path, which now reads uses_expert_location at
mega_moe_flydsl.py:317.

Signed-off-by: Xinyuan Tong <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants