[Refactor] Move Step-3.5, GLM5-Next, Dots3, MiniMax-M3 and Qwen3.5 onto ffn_exit (#41198)
This commit is contained in:
@@ -60,7 +60,6 @@ from sglang.srt.layers.attention.dsa.dsa_indexer import Indexer
|
||||
from sglang.srt.layers.communicator import (
|
||||
LayerCommunicator,
|
||||
LayerScatterModes,
|
||||
UnreducedOutput,
|
||||
enable_moe_dense_fully_dp,
|
||||
)
|
||||
from sglang.srt.layers.dp_attention import is_dp_attention_enabled
|
||||
@@ -1623,30 +1622,14 @@ class Dots3DecoderLayer(nn.Module):
|
||||
hidden_states, residual, forward_batch
|
||||
)
|
||||
|
||||
should_allreduce_fusion = (
|
||||
self.layer_communicator.should_fuse_mlp_allreduce_with_next_layer(
|
||||
forward_batch
|
||||
with self.layer_communicator.ffn_exit(forward_batch) as ffn_exit:
|
||||
hidden_states = self.mlp(
|
||||
hidden_states,
|
||||
forward_batch,
|
||||
ffn_exit.fuse_mlp_allreduce,
|
||||
ffn_exit.mlp_reduce_scatter,
|
||||
)
|
||||
)
|
||||
|
||||
# For DP with padding, reduce scatter can be used instead of all-reduce.
|
||||
use_reduce_scatter = self.layer_communicator.should_use_reduce_scatter(
|
||||
forward_batch
|
||||
)
|
||||
|
||||
hidden_states = self.mlp(
|
||||
hidden_states, forward_batch, should_allreduce_fusion, use_reduce_scatter
|
||||
)
|
||||
|
||||
if should_allreduce_fusion:
|
||||
hidden_states = UnreducedOutput(hidden_states)
|
||||
|
||||
if not should_allreduce_fusion:
|
||||
hidden_states, residual = self.layer_communicator.postprocess_layer(
|
||||
hidden_states, residual, forward_batch
|
||||
)
|
||||
|
||||
return hidden_states, residual
|
||||
return ffn_exit.finish(hidden_states, residual)
|
||||
|
||||
def op_comm_prepare_attn(
|
||||
self,
|
||||
|
||||
@@ -28,7 +28,6 @@ from sglang.srt.layers.attention.vision import VisionAttention
|
||||
from sglang.srt.layers.communicator import (
|
||||
LayerCommunicator,
|
||||
LayerScatterModes,
|
||||
UnreducedOutput,
|
||||
enable_moe_dense_fully_dp,
|
||||
get_attn_tp_context,
|
||||
layer_input_buffer,
|
||||
@@ -109,7 +108,6 @@ from sglang.srt.multimodal.mm_utils import (
|
||||
run_dp_sharded_mrope_vision_model,
|
||||
)
|
||||
from sglang.srt.runtime_context import (
|
||||
get_forward,
|
||||
get_lora,
|
||||
get_mm,
|
||||
get_parallel,
|
||||
@@ -953,16 +951,6 @@ class Glm5NextDecoderLayer(nn.Module):
|
||||
forward_batch,
|
||||
)
|
||||
|
||||
should_allreduce_fusion = (
|
||||
self.layer_communicator.should_fuse_mlp_allreduce_with_next_layer(
|
||||
forward_batch
|
||||
)
|
||||
)
|
||||
|
||||
use_reduce_scatter = self.layer_communicator.should_use_reduce_scatter(
|
||||
forward_batch
|
||||
)
|
||||
|
||||
if isinstance(self.mlp, Glm5NextMLP):
|
||||
gemm_output_zero_allocator = None
|
||||
|
||||
@@ -977,26 +965,13 @@ class Glm5NextDecoderLayer(nn.Module):
|
||||
else:
|
||||
_mlp_ctx = nullcontext()
|
||||
|
||||
with get_forward().scoped(
|
||||
fuse_mlp_allreduce=should_allreduce_fusion,
|
||||
mlp_reduce_scatter=use_reduce_scatter,
|
||||
):
|
||||
with _mlp_ctx:
|
||||
hidden_states = self.mlp(
|
||||
hidden_states,
|
||||
forward_batch,
|
||||
gemm_output_zero_allocator,
|
||||
)
|
||||
|
||||
if should_allreduce_fusion:
|
||||
hidden_states = UnreducedOutput(hidden_states)
|
||||
|
||||
if not should_allreduce_fusion:
|
||||
hidden_states, residual = self.layer_communicator.postprocess_layer(
|
||||
with self.layer_communicator.ffn_exit(forward_batch) as ffn_exit, _mlp_ctx:
|
||||
hidden_states = self.mlp(
|
||||
hidden_states,
|
||||
residual,
|
||||
forward_batch,
|
||||
gemm_output_zero_allocator,
|
||||
)
|
||||
hidden_states, residual = ffn_exit.finish(hidden_states, residual)
|
||||
|
||||
return hidden_states, residual, topk_indices
|
||||
|
||||
|
||||
@@ -39,7 +39,6 @@ from sglang.srt.layers.communicator import (
|
||||
LayerCommunicator,
|
||||
LayerScatterModes,
|
||||
ScatterMode,
|
||||
UnreducedOutput,
|
||||
enable_moe_dense_fully_dp,
|
||||
)
|
||||
from sglang.srt.layers.dp_attention import (
|
||||
@@ -1395,36 +1394,15 @@ class MiniMaxM3DecoderLayer(nn.Module):
|
||||
hidden_states, residual, forward_batch
|
||||
)
|
||||
|
||||
should_allreduce_fusion = (
|
||||
self.layer_communicator.should_fuse_mlp_allreduce_with_next_layer(
|
||||
forward_batch
|
||||
)
|
||||
)
|
||||
if self.is_layer_sparse and get_parallel().tp_size > 1:
|
||||
# Sparse MoE outputs are TP-partial; deferring their all-reduce into the next
|
||||
# layer's fusion re-triggers the M3 no-EOS runaway. Force immediate all-reduce.
|
||||
should_allreduce_fusion = False
|
||||
|
||||
use_reduce_scatter = self.layer_communicator.should_use_reduce_scatter(
|
||||
forward_batch
|
||||
)
|
||||
|
||||
if self.is_layer_sparse or hidden_states.shape[0] != 0:
|
||||
hidden_states = self.mlp(
|
||||
hidden_states,
|
||||
forward_batch=forward_batch,
|
||||
should_allreduce_fusion=should_allreduce_fusion,
|
||||
use_reduce_scatter=use_reduce_scatter,
|
||||
)
|
||||
|
||||
if should_allreduce_fusion:
|
||||
hidden_states = UnreducedOutput(hidden_states)
|
||||
else:
|
||||
hidden_states, residual = self.layer_communicator.postprocess_layer(
|
||||
hidden_states, residual, forward_batch
|
||||
)
|
||||
|
||||
return hidden_states, residual
|
||||
with self.layer_communicator.ffn_exit(forward_batch) as ffn_exit:
|
||||
if self.is_layer_sparse or hidden_states.shape[0] != 0:
|
||||
hidden_states = self.mlp(
|
||||
hidden_states,
|
||||
forward_batch=forward_batch,
|
||||
should_allreduce_fusion=ffn_exit.fuse_mlp_allreduce,
|
||||
use_reduce_scatter=ffn_exit.mlp_reduce_scatter,
|
||||
)
|
||||
return ffn_exit.finish(hidden_states, residual)
|
||||
|
||||
|
||||
class MiniMaxM3Model(nn.Module):
|
||||
|
||||
@@ -46,7 +46,6 @@ from sglang.srt.layers.attention.mamba.mamba import mamba_v2_sharded_weight_load
|
||||
from sglang.srt.layers.communicator import (
|
||||
LayerCommunicator,
|
||||
LayerScatterModes,
|
||||
UnreducedOutput,
|
||||
reduce_output,
|
||||
)
|
||||
from sglang.srt.layers.dp_attention import (
|
||||
@@ -108,7 +107,6 @@ from sglang.srt.models.utils import (
|
||||
)
|
||||
from sglang.srt.runtime_context import (
|
||||
get_exec,
|
||||
get_forward,
|
||||
get_lora,
|
||||
get_parallel,
|
||||
get_stream,
|
||||
@@ -307,19 +305,6 @@ def _select_fused_ar_input_for_linear(hidden_states, linear: nn.Module):
|
||||
)
|
||||
|
||||
|
||||
def _finish_mlp_output(hidden_states, *, expect_deferred: bool):
|
||||
from sglang.srt.layers.moe.cutedsl_ar_fusion import MoeFinalizeHandoff
|
||||
|
||||
if not expect_deferred:
|
||||
if isinstance(hidden_states, MoeFinalizeHandoff):
|
||||
raise RuntimeError("unexpected deferred-finalize handoff")
|
||||
return UnreducedOutput(hidden_states)
|
||||
|
||||
if not isinstance(hidden_states, MoeFinalizeHandoff):
|
||||
raise RuntimeError("Qwen3.5 expected a FlashInfer deferred-finalize handoff")
|
||||
return hidden_states
|
||||
|
||||
|
||||
if _is_npu:
|
||||
from sgl_kernel_npu.norm.split_qkv_rmsnorm_rope import (
|
||||
split_qkvgate_gemma_rmsnorm_rope,
|
||||
@@ -1181,53 +1166,16 @@ class Qwen3_5LinearDecoderLayer(nn.Module):
|
||||
hidden_states, residual, forward_batch
|
||||
)
|
||||
|
||||
mlp_reduce_scatter = self.layer_communicator.should_use_reduce_scatter(
|
||||
forward_batch
|
||||
)
|
||||
|
||||
fuse_mlp_allreduce = (
|
||||
self.layer_communicator.should_fuse_mlp_allreduce_with_next_layer(
|
||||
forward_batch
|
||||
)
|
||||
)
|
||||
defer_moe_finalize = (
|
||||
fuse_mlp_allreduce
|
||||
and isinstance(hidden_states, torch.Tensor)
|
||||
and isinstance(self.mlp, Qwen2MoeSparseMoeBlock)
|
||||
and self.layer_communicator.should_defer_moe_finalize(
|
||||
forward_batch, int(hidden_states.shape[0])
|
||||
)
|
||||
)
|
||||
if (
|
||||
fuse_mlp_allreduce
|
||||
and self.layer_communicator.is_last_layer
|
||||
and not defer_moe_finalize
|
||||
):
|
||||
# The last layer has no prepare_attn consumer for deferred AllReduce;
|
||||
# fall back before MLP so postprocess_layer performs the collective.
|
||||
fuse_mlp_allreduce = False
|
||||
with get_forward().scoped(
|
||||
fuse_mlp_allreduce=fuse_mlp_allreduce,
|
||||
mlp_reduce_scatter=mlp_reduce_scatter,
|
||||
):
|
||||
with self.layer_communicator.ffn_exit(forward_batch) as ffn_exit:
|
||||
if isinstance(self.mlp, Qwen2MoeSparseMoeBlock):
|
||||
hidden_states = self.mlp(
|
||||
hidden_states,
|
||||
forward_batch,
|
||||
defer_finalize=defer_moe_finalize,
|
||||
defer_finalize=ffn_exit.defer_moe_finalize,
|
||||
)
|
||||
else:
|
||||
hidden_states = self.mlp(hidden_states)
|
||||
if fuse_mlp_allreduce:
|
||||
hidden_states = _finish_mlp_output(
|
||||
hidden_states, expect_deferred=defer_moe_finalize
|
||||
)
|
||||
else:
|
||||
hidden_states, residual = self.layer_communicator.postprocess_layer(
|
||||
hidden_states, residual, forward_batch
|
||||
)
|
||||
|
||||
return hidden_states, residual
|
||||
return ffn_exit.finish(hidden_states, residual)
|
||||
|
||||
|
||||
class Qwen3_5AttentionDecoderLayer(nn.Module):
|
||||
@@ -1618,51 +1566,16 @@ class Qwen3_5AttentionDecoderLayer(nn.Module):
|
||||
hidden_states, residual = self.layer_communicator.prepare_mlp(
|
||||
hidden_states, residual, forward_batch
|
||||
)
|
||||
mlp_reduce_scatter = self.layer_communicator.should_use_reduce_scatter(
|
||||
forward_batch
|
||||
)
|
||||
|
||||
fuse_mlp_allreduce = (
|
||||
self.layer_communicator.should_fuse_mlp_allreduce_with_next_layer(
|
||||
forward_batch
|
||||
)
|
||||
)
|
||||
defer_moe_finalize = (
|
||||
fuse_mlp_allreduce
|
||||
and isinstance(hidden_states, torch.Tensor)
|
||||
and isinstance(self.mlp, Qwen2MoeSparseMoeBlock)
|
||||
and self.layer_communicator.should_defer_moe_finalize(
|
||||
forward_batch, int(hidden_states.shape[0])
|
||||
)
|
||||
)
|
||||
if (
|
||||
fuse_mlp_allreduce
|
||||
and self.layer_communicator.is_last_layer
|
||||
and not defer_moe_finalize
|
||||
):
|
||||
fuse_mlp_allreduce = False
|
||||
with get_forward().scoped(
|
||||
fuse_mlp_allreduce=fuse_mlp_allreduce,
|
||||
mlp_reduce_scatter=mlp_reduce_scatter,
|
||||
):
|
||||
with self.layer_communicator.ffn_exit(forward_batch) as ffn_exit:
|
||||
if isinstance(self.mlp, Qwen2MoeSparseMoeBlock):
|
||||
hidden_states = self.mlp(
|
||||
hidden_states,
|
||||
forward_batch,
|
||||
defer_finalize=defer_moe_finalize,
|
||||
defer_finalize=ffn_exit.defer_moe_finalize,
|
||||
)
|
||||
else:
|
||||
hidden_states = self.mlp(hidden_states)
|
||||
if fuse_mlp_allreduce:
|
||||
hidden_states = _finish_mlp_output(
|
||||
hidden_states, expect_deferred=defer_moe_finalize
|
||||
)
|
||||
else:
|
||||
hidden_states, residual = self.layer_communicator.postprocess_layer(
|
||||
hidden_states, residual, forward_batch
|
||||
)
|
||||
|
||||
return hidden_states, residual
|
||||
return ffn_exit.finish(hidden_states, residual)
|
||||
|
||||
|
||||
ALL_DECODER_LAYER_TYPES = {
|
||||
|
||||
@@ -11,11 +11,7 @@ from sglang.srt.eplb.expert_distribution import get_global_expert_distribution_r
|
||||
from sglang.srt.eplb.expert_location import ModelConfigForExpertLocation
|
||||
from sglang.srt.eplb.expert_location_dispatch import ExpertLocationDispatchInfo
|
||||
from sglang.srt.layers.activation import SiluAndMul
|
||||
from sglang.srt.layers.communicator import (
|
||||
LayerCommunicator,
|
||||
LayerScatterModes,
|
||||
UnreducedOutput,
|
||||
)
|
||||
from sglang.srt.layers.communicator import LayerCommunicator, LayerScatterModes
|
||||
from sglang.srt.layers.dp_attention import (
|
||||
is_dp_attention_enabled,
|
||||
)
|
||||
@@ -609,45 +605,32 @@ class Step3p5DecoderLayer(nn.Module):
|
||||
forward_batch,
|
||||
)
|
||||
|
||||
fuse_mlp_allreduce = (
|
||||
self.layer_communicator.should_fuse_mlp_allreduce_with_next_layer(
|
||||
forward_batch
|
||||
)
|
||||
)
|
||||
if self.use_moe:
|
||||
with self.layer_communicator.ffn_exit(forward_batch) as ffn_exit:
|
||||
# Both share_expert and MoE return unreduced (TP-partial) outputs.
|
||||
# Combine them first, then do a single all-reduce — saving one
|
||||
# full-TP all-reduce per layer.
|
||||
# Force fuse_mlp_allreduce=True so MoE skips its internal AR.
|
||||
share_output = self.share_expert(hidden_states)
|
||||
with get_forward().scoped(fuse_mlp_allreduce=True):
|
||||
moe_output = self.moe(hidden_states, forward_batch)
|
||||
hidden_states = moe_output + share_output
|
||||
if not ffn_exit.fuse_mlp_allreduce and not ffn_exit.mlp_reduce_scatter:
|
||||
hidden_states = tensor_model_parallel_all_reduce(hidden_states)
|
||||
return ffn_exit.finish(hidden_states, residual)
|
||||
|
||||
# The dense MLP all-reduces its own output unless postprocess
|
||||
# reduce-scatters it; it never leaves the sum to the next layer.
|
||||
mlp_reduce_scatter = self.layer_communicator.should_use_reduce_scatter(
|
||||
forward_batch
|
||||
)
|
||||
|
||||
if self.use_moe:
|
||||
# Both share_expert and MoE return unreduced (TP-partial) outputs.
|
||||
# Combine them first, then do a single all-reduce — saving one
|
||||
# full-TP all-reduce per layer.
|
||||
# Force fuse_mlp_allreduce=True so MoE skips its internal AR.
|
||||
share_output = self.share_expert(hidden_states)
|
||||
with get_forward().scoped(
|
||||
fuse_mlp_allreduce=True,
|
||||
mlp_reduce_scatter=mlp_reduce_scatter,
|
||||
):
|
||||
moe_output = self.moe(hidden_states, forward_batch)
|
||||
hidden_states = moe_output + share_output
|
||||
if not fuse_mlp_allreduce and not mlp_reduce_scatter:
|
||||
hidden_states = tensor_model_parallel_all_reduce(hidden_states)
|
||||
else:
|
||||
# The dense MLP all-reduces its own output unless postprocess
|
||||
# reduce-scatters it; it never leaves the sum to the next layer.
|
||||
with get_forward().scoped(
|
||||
fuse_mlp_allreduce=False, mlp_reduce_scatter=mlp_reduce_scatter
|
||||
):
|
||||
hidden_states = self.mlp(hidden_states)
|
||||
fuse_mlp_allreduce = False
|
||||
|
||||
if fuse_mlp_allreduce:
|
||||
hidden_states = UnreducedOutput(hidden_states)
|
||||
else:
|
||||
hidden_states, residual = self.layer_communicator.postprocess_layer(
|
||||
hidden_states, residual, forward_batch
|
||||
)
|
||||
return hidden_states, residual
|
||||
with get_forward().scoped(
|
||||
fuse_mlp_allreduce=False, mlp_reduce_scatter=mlp_reduce_scatter
|
||||
):
|
||||
hidden_states = self.mlp(hidden_states)
|
||||
return self.layer_communicator.postprocess_layer(
|
||||
hidden_states, residual, forward_batch
|
||||
)
|
||||
|
||||
|
||||
class Step3p5Model(nn.Module):
|
||||
|
||||
Reference in New Issue
Block a user