[Fix] Decide the MoE padded-row bound from the layer scatter mode (#40672)
Co-authored-by: Mohammad Angkad <mohammad.angkad@radixark.ai>
This commit is contained in:
co-authored by
Mohammad Angkad
parent
d34f7b2326
commit
7b977ce5dc
@@ -394,6 +394,24 @@ class _LayerModeComputationContext:
|
||||
)
|
||||
|
||||
|
||||
def sparse_mlp_scatter_mode() -> ScatterMode:
|
||||
"""SCATTERED hands a sparse MLP this rank's own token shard; FULL and
|
||||
MOE_FULL hand it a buffer gathered over the attn-TP or MoE-CP group."""
|
||||
if (
|
||||
# Token dispatch/combine will be handled outside of LayerCommunicator for these modes.
|
||||
not get_moe_a2a_backend().is_none()
|
||||
or should_use_flashinfer_cutlass_moe_fp4_allgather()
|
||||
or enable_dwdp()
|
||||
):
|
||||
return ScatterMode.SCATTERED
|
||||
# DSA CP and MLA CP both don't support MOE_FULL yet; fall back to FULL.
|
||||
if is_enable_moe_cp_allgather() and not (
|
||||
is_dsa_enable_prefill_cp() or is_mla_cp_enabled()
|
||||
):
|
||||
return ScatterMode.MOE_FULL
|
||||
return ScatterMode.FULL
|
||||
|
||||
|
||||
@dataclass
|
||||
class LayerScatterModes:
|
||||
layer_input_mode: ScatterMode
|
||||
@@ -423,19 +441,7 @@ class LayerScatterModes:
|
||||
@classmethod
|
||||
def _compute_mlp_mode(cls, context: _LayerModeComputationContext):
|
||||
if context.is_layer_sparse:
|
||||
if (
|
||||
# Token dispatch/combine will be handled outside of LayerCommunicator for these modes.
|
||||
not get_moe_a2a_backend().is_none()
|
||||
or should_use_flashinfer_cutlass_moe_fp4_allgather()
|
||||
or enable_dwdp()
|
||||
):
|
||||
return ScatterMode.SCATTERED
|
||||
# DSA CP and MLA CP both don't support MOE_FULL yet; fall back to FULL.
|
||||
if is_enable_moe_cp_allgather() and not (
|
||||
is_dsa_enable_prefill_cp() or is_mla_cp_enabled()
|
||||
):
|
||||
return ScatterMode.MOE_FULL
|
||||
return ScatterMode.FULL
|
||||
return sparse_mlp_scatter_mode()
|
||||
else:
|
||||
if enable_moe_dense_fully_dp():
|
||||
return ScatterMode.SCATTERED
|
||||
|
||||
@@ -221,7 +221,7 @@ def _run_mega_routed(
|
||||
hidden_states,
|
||||
router_logits,
|
||||
num_token_non_padded=(
|
||||
forward_batch.num_token_non_padded
|
||||
forward_batch.moe_num_token_non_padded()
|
||||
if forward_batch is not None
|
||||
else None
|
||||
),
|
||||
|
||||
@@ -1178,6 +1178,50 @@ class ForwardBatch(ForwardBatchDeepSeekMHAMixin):
|
||||
sharded=sharded,
|
||||
)
|
||||
|
||||
def moe_num_token_non_padded(self) -> Optional[torch.Tensor]:
|
||||
"""Bound for masking a sparse MoE's padded rows, or None when the MoE
|
||||
input is a gathered buffer whose real rows are not a prefix of it."""
|
||||
from sglang.srt.layers.communicator import ScatterMode, sparse_mlp_scatter_mode
|
||||
|
||||
if self.num_token_non_padded is None:
|
||||
return None
|
||||
|
||||
mode = sparse_mlp_scatter_mode()
|
||||
if mode == ScatterMode.SCATTERED:
|
||||
# a2a dispatch, FP4 all-gather and dwdp all route the local shard.
|
||||
return self.num_token_non_padded
|
||||
if mode == ScatterMode.MOE_FULL and self._moe_input_gathered_across_moe_cp():
|
||||
return None
|
||||
# DSA / MLA CP take the FULL mode but all-gather across CP on a prefill,
|
||||
# which leaves the real rows zigzag-permuted rather than in a prefix.
|
||||
if get_parallel().attn_cp_size > 1 and self._moe_input_gathered_across_cp():
|
||||
return None
|
||||
if get_parallel().attn_dp_size != 1:
|
||||
# dp_gather concatenates one padded slot per attention-DP rank.
|
||||
return None
|
||||
# A single attention-DP group all-reduces instead of gathering, so the
|
||||
# buffer holds the whole sequence and the GLOBAL count bounds it.
|
||||
if not self.attn_tp_sequence_sharded:
|
||||
return self.num_token_non_padded
|
||||
# None under cuda-graph replay: its static batch carries no GLOBAL count.
|
||||
return self.global_num_token_non_padded
|
||||
|
||||
def _moe_input_gathered_across_moe_cp(self) -> bool:
|
||||
from sglang.srt.layers.dp_attention import get_moe_cp_size
|
||||
|
||||
# Mirrors the communicator's MOE_FULL gather guard: decode stays FULL.
|
||||
return (
|
||||
self.forward_mode.is_context_parallel_extend()
|
||||
and self.attn_cp_metadata is not None
|
||||
and get_moe_cp_size() > 1
|
||||
)
|
||||
|
||||
def _moe_input_gathered_across_cp(self) -> bool:
|
||||
from sglang.srt.layers.attention.dsa.utils import dsa_use_prefill_cp
|
||||
from sglang.srt.layers.cp.utils import is_mla_cp_active
|
||||
|
||||
return dsa_use_prefill_cp(self) or is_mla_cp_active(self)
|
||||
|
||||
def mamba_track_aligned_lens(self) -> Optional[torch.Tensor]:
|
||||
"""Tokens of this extend chunk covered by the tracked mamba state,
|
||||
floored to the mamba_cache_chunk_size boundary the scheduler snapshots at;
|
||||
|
||||
@@ -476,7 +476,7 @@ class BailingMoESparseMoeBlock(nn.Module):
|
||||
hidden_states,
|
||||
router_logits,
|
||||
dynamic_expert_bias=dynamic_expert_bias,
|
||||
num_token_non_padded=forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
),
|
||||
|
||||
@@ -859,7 +859,7 @@ class BailingMoE(nn.Module):
|
||||
hidden_states,
|
||||
router_logits,
|
||||
dynamic_expert_bias=dynamic_expert_bias,
|
||||
num_token_non_padded=forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
),
|
||||
|
||||
@@ -925,7 +925,9 @@ class DeepseekV2MoE(nn.Module):
|
||||
)
|
||||
|
||||
num_token_non_padded = (
|
||||
forward_batch.num_token_non_padded if forward_batch is not None else None
|
||||
forward_batch.moe_num_token_non_padded()
|
||||
if forward_batch is not None
|
||||
else None
|
||||
)
|
||||
if not self._enable_a2a_moe:
|
||||
if self._can_dual_stream_graph(hidden_states):
|
||||
@@ -1457,7 +1459,7 @@ class DeepseekV2MoE(nn.Module):
|
||||
topk_output = self.topk(
|
||||
hidden_states,
|
||||
router_logits,
|
||||
num_token_non_padded=forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=(
|
||||
ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
@@ -1851,7 +1853,7 @@ class DeepseekV2MoE(nn.Module):
|
||||
state.topk_output = self.topk(
|
||||
hidden_states=hidden_states,
|
||||
router_logits=router_logits,
|
||||
num_token_non_padded=state.forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=state.forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=(
|
||||
ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
|
||||
@@ -3853,25 +3853,17 @@ class DeepseekV4DecoderLayer(nn.Module):
|
||||
if _use_cp and get_moe_a2a_backend().is_none()
|
||||
else nullcontext()
|
||||
)
|
||||
# The MoE sees DP-gathered rows, so this rank's local count cannot mask them.
|
||||
# The standard dispatcher masks padding in the gathered buffer.
|
||||
saved_num_token_non_padded = forward_batch.num_token_non_padded
|
||||
if _use_tp_moe_gather:
|
||||
forward_batch.num_token_non_padded = None
|
||||
try:
|
||||
with (
|
||||
get_forward().scoped(mlp_reduce_scatter=mlp_reduce_scatter),
|
||||
gathered_rows,
|
||||
):
|
||||
hidden_states = self.mlp(
|
||||
hidden_states,
|
||||
forward_batch,
|
||||
input_ids=input_ids,
|
||||
input_ids_global=input_ids_global,
|
||||
skip_shared_experts=_do_shared_local,
|
||||
)
|
||||
finally:
|
||||
forward_batch.num_token_non_padded = saved_num_token_non_padded
|
||||
with (
|
||||
get_forward().scoped(mlp_reduce_scatter=mlp_reduce_scatter),
|
||||
gathered_rows,
|
||||
):
|
||||
hidden_states = self.mlp(
|
||||
hidden_states,
|
||||
forward_batch,
|
||||
input_ids=input_ids,
|
||||
input_ids_global=input_ids_global,
|
||||
skip_shared_experts=_do_shared_local,
|
||||
)
|
||||
if _use_cp and get_moe_a2a_backend().is_none():
|
||||
hidden_states = dsa_cp_reduce_scatter_hidden_states(hidden_states)
|
||||
elif _use_tp_moe_gather:
|
||||
|
||||
@@ -515,7 +515,7 @@ class Dots3MoE(nn.Module):
|
||||
topk_output = self.topk(
|
||||
hidden_states,
|
||||
router_logits,
|
||||
num_token_non_padded=forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
),
|
||||
@@ -577,7 +577,7 @@ class Dots3MoE(nn.Module):
|
||||
state.topk_weights_local, state.topk_idx_local, _ = self.topk(
|
||||
hidden_states=hidden_states,
|
||||
router_logits=router_logits,
|
||||
num_token_non_padded=state.forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=state.forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
),
|
||||
|
||||
@@ -227,7 +227,7 @@ class ExaoneMoESparseMoEBlock(nn.Module):
|
||||
topk_output = self.topk(
|
||||
hidden_states,
|
||||
router_logits,
|
||||
num_token_non_padded=forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
),
|
||||
|
||||
@@ -680,7 +680,7 @@ class Glm4MoeSparseMoeBlock(nn.Module):
|
||||
topk_output = self.topk(
|
||||
hidden_states,
|
||||
router_logits,
|
||||
num_token_non_padded=forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
),
|
||||
@@ -734,7 +734,7 @@ class Glm4MoeSparseMoeBlock(nn.Module):
|
||||
state.topk_output = self.topk(
|
||||
hidden_states=hidden_states,
|
||||
router_logits=router_logits,
|
||||
num_token_non_padded=state.forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=state.forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
),
|
||||
|
||||
@@ -385,7 +385,7 @@ class Glm4MoeLiteSparseMoeBlock(nn.Module):
|
||||
topk_output = self.topk(
|
||||
hidden_states,
|
||||
router_logits,
|
||||
num_token_non_padded=forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
),
|
||||
@@ -446,7 +446,7 @@ class Glm4MoeLiteSparseMoeBlock(nn.Module):
|
||||
state.topk_output = self.topk(
|
||||
hidden_states=hidden_states,
|
||||
router_logits=router_logits,
|
||||
num_token_non_padded=state.forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=state.forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
),
|
||||
|
||||
@@ -403,7 +403,7 @@ class LLaDA2MoeSparseMoeBlock(nn.Module):
|
||||
topk_output = self.topk(
|
||||
hidden_states,
|
||||
router_logits,
|
||||
num_token_non_padded=forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
),
|
||||
|
||||
@@ -516,7 +516,7 @@ class MiMoV2MoE(nn.Module):
|
||||
topk_output = self.topk(
|
||||
hidden_states,
|
||||
router_logits,
|
||||
num_token_non_padded=forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
),
|
||||
@@ -549,7 +549,7 @@ class MiMoV2MoE(nn.Module):
|
||||
state.topk_output = self.topk(
|
||||
hidden_states=hidden_states,
|
||||
router_logits=router_logits,
|
||||
num_token_non_padded=state.forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=state.forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
),
|
||||
|
||||
@@ -618,7 +618,7 @@ class MiniMaxM2MoE(nn.Module):
|
||||
topk_output = self.topk(
|
||||
hidden_states,
|
||||
router_logits,
|
||||
num_token_non_padded=forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
),
|
||||
@@ -659,7 +659,7 @@ class MiniMaxM2MoE(nn.Module):
|
||||
state.topk_weights_local, state.topk_idx_local, _ = self.topk(
|
||||
hidden_states=hidden_states,
|
||||
router_logits=router_logits,
|
||||
num_token_non_padded=state.forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=state.forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
),
|
||||
|
||||
@@ -499,7 +499,7 @@ class MiniMaxM3MoE(nn.Module):
|
||||
topk_output = self.topk(
|
||||
hidden_states,
|
||||
router_logits,
|
||||
num_token_non_padded=forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
),
|
||||
|
||||
@@ -601,7 +601,7 @@ class Qwen2MoeSparseMoeBlock(nn.Module):
|
||||
topk_output = self.topk(
|
||||
hidden_states,
|
||||
router_logits,
|
||||
num_token_non_padded=forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=(
|
||||
ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
@@ -658,7 +658,7 @@ class Qwen2MoeSparseMoeBlock(nn.Module):
|
||||
hidden_states,
|
||||
router_logits,
|
||||
num_token_non_padded=(
|
||||
forward_batch.num_token_non_padded
|
||||
forward_batch.moe_num_token_non_padded()
|
||||
if forward_batch is not None
|
||||
else None
|
||||
),
|
||||
|
||||
@@ -350,7 +350,7 @@ class Qwen3MoeSparseMoeBlock(nn.Module):
|
||||
topk_output = self.topk(
|
||||
hidden_states,
|
||||
router_logits,
|
||||
num_token_non_padded=forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
),
|
||||
@@ -389,7 +389,7 @@ class Qwen3MoeSparseMoeBlock(nn.Module):
|
||||
hidden_states,
|
||||
router_logits,
|
||||
num_token_non_padded=(
|
||||
forward_batch.num_token_non_padded
|
||||
forward_batch.moe_num_token_non_padded()
|
||||
if forward_batch is not None
|
||||
else None
|
||||
),
|
||||
@@ -434,7 +434,7 @@ class Qwen3MoeSparseMoeBlock(nn.Module):
|
||||
state.topk_output = self.topk(
|
||||
hidden_states=hidden_states,
|
||||
router_logits=router_logits,
|
||||
num_token_non_padded=state.forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=state.forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
),
|
||||
|
||||
@@ -159,7 +159,7 @@ class SDARMoeSparseMoeBlock(nn.Module):
|
||||
topk_output = self.topk(
|
||||
hidden_states,
|
||||
router_logits,
|
||||
num_token_non_padded=forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id
|
||||
),
|
||||
|
||||
@@ -238,7 +238,7 @@ class Step3p5MoEMLP(nn.Module):
|
||||
topk_output = self.topk(
|
||||
hidden_states,
|
||||
router_logits,
|
||||
num_token_non_padded=forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
),
|
||||
@@ -270,7 +270,7 @@ class Step3p5MoEMLP(nn.Module):
|
||||
state.topk_output = self.topk(
|
||||
hidden_states=hidden_states,
|
||||
router_logits=router_logits,
|
||||
num_token_non_padded=state.forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=state.forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=ExpertLocationDispatchInfo.init_new(
|
||||
layer_id=self.layer_id,
|
||||
),
|
||||
|
||||
@@ -1000,7 +1000,7 @@ class XllmSparseMoeBlock(nn.Module):
|
||||
topk_output = self.topk(
|
||||
hidden_states,
|
||||
router_logits,
|
||||
num_token_non_padded=forward_batch.num_token_non_padded,
|
||||
num_token_non_padded=forward_batch.moe_num_token_non_padded(),
|
||||
expert_location_dispatch_info=(
|
||||
ExpertLocationDispatchInfo.init_new(layer_id=self.layer_id)
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user