Skip to content

[Fix] Decide the MoE padded-row bound from the layer scatter mode - #40672

Merged
ch-wan merged 5 commits into
mainfrom
mmangkad/moe-padded-row-mask-gathered-buffer
Sep 22, 2026
Merged

ch-wan merged 5 commits into
mainfrom
mmangkad/moe-padded-row-mask-gathered-buffer

Conversation

@mmangkad

@mmangkad mmangkad commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A more general fix for the truncation reported in #40643, which #39574 introduced: rather than special-casing DP attention, the bound is decided from the layer communicator's scatter mode, which also covers the non-DP and CP gathered cases and the other MoE models on the same path.

forward_batch.num_token_non_padded is LOCAL (forward_batch_info.py). It bounds a sparse MoE's input only while that input is this rank's own shard. On a gathered buffer each source rank contributes its own padded slot, so the real rows are not a prefix — masking with the local count drops every peer rank's tokens, and under --moe-a2a-backend none each rank sums a truncated partial output.

This moves that decision to ForwardBatch.moe_num_token_non_padded(), keyed on the scatter mode the layer communicator already computes for a sparse MLP.

Decision table

MoE input Condition Bound
SCATTERED a2a dispatch, FP4 all-gather, dwdp LOCAL
MOE_FULL CP extend — MoE-CP all-gather runs none
MOE_FULL any other forward — no gather, FULL layout as FULL below
FULL DSA / MLA CP prefill — CP all-gather, zigzag rows none
FULL attn_dp_size > 1 — dp_gather interleaves slots none
FULL attn_dp_size == 1, attn-TP sharded — all-reduce, not gather GLOBAL
FULL attn_dp_size == 1, replicated LOCAL
any num_token_non_padded is None (no EP) none

Changes

Area Change
forward_batch_info.py moe_num_token_non_padded() — the one definition
communicator.py _compute_mlp_mode's sparse branch extracted as sparse_mlp_scatter_mode()
17 model files + mega_moe.py 24 MoE routing sites read the accessor instead of the raw field
deepseek_v4.py inline num_token_non_padded = None around its MoE call dropped as redundant
test_num_token_non_padded_localization.py table above locked; cases fail against the pre-fix behavior

Verification

Check Result
test/registered/unit/model_executor + scatter-modes test 297 passed, 181 subtests
Pre-fix simulation (accessor returns raw local count) 9 subtests fail
Pre-MOE_FULL-gate simulation (unconditional None) 4 subtests fail
torch.compile(fullgraph=True) over the accessor's function-level import no graph break
Raw num_token_non_padded= MoE routing reads left in srt/ 0

CI States

Latest PR Test (Base): ❌ Run #35743730051
Latest PR Test (Extra): ✅ Run #35743729422
Latest PR Test (AMD ROCm 10): ⏳ Run #35743730189

@mmangkad

Copy link
Copy Markdown
Contributor Author

/rerun-test test/registered/unit/model_executor/test_num_token_non_padded_localization.py registered/ep/test_eplb_no_a2a.py registered/moe/test_moe_ep.py registered/dp_attn/test_dp_attention.py registered/dp_attn/test_dp_attention_bcg_kl.py registered/moe/test_topk_padded_region.py registered/moe/test_glm4_moe_models.py test/registered/ep/test_deepep_small.py test/registered/ep/test_routed_experts_dp_readback.py test/registered/cp/test_dsa_prefill_cp.py test/registered/cp/test_deepseek_v3_cp_single_node.py test/registered/moe/test_zero_experts.py

@github-actions

github-actions Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/unit/model_executor/test_num_token_non_padded_localization.py registered/ep/test_eplb_no_a2a.py registered/moe/test_moe_ep.py registered/dp_attn/test_dp_attention.py registered/dp_attn/test_dp_attention_bcg_kl.py registered/moe/test_topk_padded_region.py registered/moe/test_glm4_moe_models.py test/registered/ep/test_deepep_small.py test/registered/ep/test_routed_experts_dp_readback.py test/registered/cp/test_dsa_prefill_cp.py test/registered/cp/test_deepseek_v3_cp_single_node.py test/registered/moe/test_zero_experts.py:

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

cd test/ && python3 registered/unit/model_executor/test_num_token_non_padded_localization.py

🚀 2-gpu-h100 (5 tests): ✅ View workflow run

cd test/ && python3 registered/ep/test_eplb_no_a2a.py
cd test/ && python3 registered/moe/test_moe_ep.py
cd test/ && python3 registered/dp_attn/test_dp_attention.py
cd test/ && python3 registered/dp_attn/test_dp_attention_bcg_kl.py
cd test/ && python3 registered/moe/test_glm4_moe_models.py

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

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

🚀 4-gpu-h100 (2 tests): ✅ View workflow run

cd test/ && python3 registered/ep/test_deepep_small.py
cd test/ && python3 registered/ep/test_routed_experts_dp_readback.py

🚀 8-gpu-h200 (2 tests): ✅ View workflow run

cd test/ && python3 registered/cp/test_dsa_prefill_cp.py
cd test/ && python3 registered/cp/test_deepseek_v3_cp_single_node.py

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

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

@ch-wan ch-wan 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.

LGTM

@mmangkad mmangkad added run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci) bypass-fail-fast CI: a failing job no longer aborts its siblings (lint still gates) highest-priority CI: all three control labels, plus never batch-cancelled or stale-closed labels Sep 22, 2026
@ch-wan
ch-wan merged commit 7b977ce into main Sep 22, 2026
334 of 358 checks passed
@ch-wan
ch-wan deleted the mmangkad/moe-padded-row-mask-gathered-buffer branch September 22, 2026 23:02
Leoyzen pushed a commit to Leoyzen/sglang that referenced this pull request Sep 23, 2026
hdt98 added a commit to hdt98/sglang that referenced this pull request Sep 25, 2026
…ound on ROCm

sgl-project#40672 bounds padded rows from the layer scatter mode, so the HIP-only drop
absorbed from sgl-project#39804 is no longer needed and upstream has reverted it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bypass-fail-fast CI: a failing job no longer aborts its siblings (lint still gates) deepseek highest-priority CI: all three control labels, plus never batch-cancelled or stale-closed run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants