From 846181f1c40884cbdee81c8c6a5935f4defba047 Mon Sep 17 00:00:00 2001 From: Cheng Wan <54331508+ch-wan@users.noreply.github.com> Date: Sun, 27 Sep 2026 16:47:11 -0700 Subject: [PATCH] [Refactor] Give the fused prepare_mlp kernels an explicit contract (#41418) --- python/sglang/srt/layers/communicator.py | 106 ++++++++++++------ .../srt/layers/moe/cutedsl_ar_fusion.py | 8 +- .../unit/layers/moe/test_cutedsl_ar_fusion.py | 10 +- .../layers/test_declared_decoder_boundary.py | 69 +++++++++++- 4 files changed, 151 insertions(+), 42 deletions(-) diff --git a/python/sglang/srt/layers/communicator.py b/python/sglang/srt/layers/communicator.py index 5980c008c4..833124de6e 100644 --- a/python/sglang/srt/layers/communicator.py +++ b/python/sglang/srt/layers/communicator.py @@ -822,7 +822,7 @@ class LayerCommunicator: self._communicate_simple_fn = _select_attention_input_move( sides.input_rows, sides.attention ) - self._mlp_input = _select_ffn_input( + self._mlp_input, fused = _select_ffn_input( sides.attention_output, residual=sides.input_rows, residual_to=sides.ffn_residual_rows, @@ -830,9 +830,8 @@ class LayerCommunicator: force_layernorm_before_gather=self.force_layernorm_before_dp_gather, fusions=self._select_mlp_input_fusions(), ) - self._mlp_input_may_fuse = ( - self._mlp_input_reduce_output_and_update_and_read_residual - in getattr(self._mlp_input, "keywords", {}).get("fusions", ()) + self._mlp_input_may_return_new_residual = any( + f.may_return_new_residual for f in fused ) self._ffn_output = sides.ffn_output ( @@ -874,7 +873,9 @@ class LayerCommunicator: ScatterMode.MOE_FULL, ScatterMode.SCATTERED, ) - self._mlp_input, self._mlp_input_may_fuse = self._select_mlp_input() + self._mlp_input, self._mlp_input_may_return_new_residual = ( + self._select_mlp_input() + ) def _post_init_communicate(self): self._communicate_simple_fn = CommunicateSimpleFn.get_fn( @@ -927,14 +928,14 @@ class LayerCommunicator: """True if ``prepare_mlp``'s post-attention RMSNorm leaves ``residual`` untouched, so Eagle3 aux capture can keep its reference and skip the clone. - Only the flashinfer all-reduce-fusion kernel writes a fresh + Of the base fused entry's kernels only flashinfer's writes a fresh ``residual_out`` (see ``flashinfer_allreduce_residual_rmsnorm``); the aiter - fused kernel and every plain norm fold into ``residual`` in place. It runs - when ``prepare_mlp`` selected its fused entry and the batch is not - input-scattered. + kernel and every plain norm fold into ``residual`` in place. It runs when + ``prepare_mlp`` selected a fused entry that may return a new residual and + the batch is not input-scattered. """ return ( - self._mlp_input_may_fuse + self._mlp_input_may_return_new_residual and not get_attn_tp_context().input_scattered and apply_flashinfer_allreduce_fusion(residual.shape[0]) ) @@ -1127,23 +1128,26 @@ class LayerCommunicator: ) steps = partial( _mlp_input_gather, - order=_mlp_input_order(self._context, residual_input_mode, fusions), + order=_mlp_input_order( + self._context, residual_input_mode, tuple(f.run for f in fusions) + ), ) if kind is MlpInputKind.GATHER_MOE_CP: steps = partial(_mlp_input_gather_moe_cp, gather=steps) - return ( - steps, - self._mlp_input_reduce_output_and_update_and_read_residual in fusions, - ) + return steps, any(f.may_return_new_residual for f in fusions) - def _select_mlp_input_fusions(self) -> Tuple[Callable, ...]: - """The fused kernels that complete the attention output's all-reduce - together with the residual update and the post-attention norm, in the - order they are tried. Each takes (hidden_states, residual, forward_batch) - and returns None when it does not take the batch.""" + def _select_mlp_input_fusions(self) -> Tuple["FusedMlpInput", ...]: + """The fused kernels that can take the attention -> FFN steps, in the + order they are tried.""" if not hasattr(self.post_attention_layernorm, "forward_with_allreduce_fusion"): return () - return (self._mlp_input_reduce_output_and_update_and_read_residual,) + return ( + FusedMlpInput( + completes=SumGroup.ATTN_TP, + run=self._mlp_input_reduce_output_and_update_and_read_residual, + may_return_new_residual=True, + ), + ) def _mlp_input_reduce_output_and_update_and_read_residual( self, @@ -1723,6 +1727,21 @@ def _reduce_and_redistribute_output_to_dp( return global_hidden_states +class FusedMlpInput(msgspec.Struct, frozen=True): + """A kernel that completes the sum the attention output owes together with + the residual add and the post-attention norm, in that order. + + ``run(hidden_states, residual, forward_batch)`` returns the FFN's input and + the residual, or None when it does not take the batch; it returns None only + before touching its inputs or starting a collective.""" + + # The group whose sum it completes. + completes: SumGroup + run: Callable[..., Optional[Tuple[torch.Tensor, torch.Tensor]]] + # It may hand back a new residual and leave the one it took unchanged. + may_return_new_residual: bool + + def _mlp_input_without_dp( hidden_states: torch.Tensor, residual: torch.Tensor, @@ -1846,13 +1865,14 @@ def _select_ffn_input( residual_to: Layout, need: StageInput, force_layernorm_before_gather: bool, - fusions: Tuple[Callable, ...], -) -> Callable: - """The steps from the attention output to the FFN input: complete the - attention-TP sum, move the residual to the rows it has while the FFN runs, - add it and normalize, and bring the rows to what the FFN's group needs: a - gather over attention DP, or each rank's own slice. The fused kernels in - ``fusions`` are tried first when nothing is gathered or sliced.""" + fusions: Tuple[FusedMlpInput, ...], +) -> Tuple[Callable, Tuple[FusedMlpInput, ...]]: + """The steps from the attention output to the FFN input, and the fused + kernels they try first: complete the attention-TP sum, move the residual to + the rows it has while the FFN runs, add it and normalize, and bring the rows + to what the FFN's group needs: a gather over attention DP, or each rank's + own slice. A kernel in ``fusions`` is tried only when nothing is gathered or + sliced, and only if it completes the sum the attention output owes.""" # What the attention output owes decides the steps: the attention-TP sum, # always left by the output projection, or nothing. owes_attention_tp = produced.group is SumGroup.ATTN_TP @@ -1874,7 +1894,10 @@ def _select_ffn_input( or residual not in (produced.layout, need.layout) ): raise NotImplementedError(f"{produced=} {residual=} {need=}") - return partial(_mlp_input_scatter, scatters_residual=residual != residual_to) + return ( + partial(_mlp_input_scatter, scatters_residual=residual != residual_to), + (), + ) if ( residual_to != produced.layout or gathered not in (frozenset(), {TokenAxis.ATTN_DP}) @@ -1886,19 +1909,28 @@ def _select_ffn_input( gathers_residual = residual != residual_to if not gathered: if not owes_attention_tp: - return _mlp_input_norm - return partial( - _mlp_input_without_dp, gathers_residual=gathers_residual, fusions=fusions + return _mlp_input_norm, () + fused = tuple(f for f in fusions if f.completes is produced.group) + return ( + partial( + _mlp_input_without_dp, + gathers_residual=gathers_residual, + fusions=tuple(f.run for f in fused), + ), + fused, ) # The partial order adds the residual on attention-TP rank 0 before the DP # gather's collective completes that sum, which only a plain residual add # allows. if owes_attention_tp and not force_layernorm_before_gather: - return partial(_mlp_input_dp_partial, gathers_residual=gathers_residual) - return partial( - _mlp_input_dp_replicate, - gathers_residual=gathers_residual, - reduces_attention_tp=owes_attention_tp, + return partial(_mlp_input_dp_partial, gathers_residual=gathers_residual), () + return ( + partial( + _mlp_input_dp_replicate, + gathers_residual=gathers_residual, + reduces_attention_tp=owes_attention_tp, + ), + (), ) diff --git a/python/sglang/srt/layers/moe/cutedsl_ar_fusion.py b/python/sglang/srt/layers/moe/cutedsl_ar_fusion.py index 5eb3055004..886ea916fa 100644 --- a/python/sglang/srt/layers/moe/cutedsl_ar_fusion.py +++ b/python/sglang/srt/layers/moe/cutedsl_ar_fusion.py @@ -13,7 +13,9 @@ from typing import Callable, Optional, Sequence import msgspec import torch +from sglang.srt.layers.boundary_layout import SumGroup from sglang.srt.layers.communicator import ( + FusedMlpInput, LayerCommunicator, ScatterMode, UnreducedOutput, @@ -248,7 +250,11 @@ class CuteDSLFusionLayerCommunicator(LayerCommunicator): and _fused_norm_gamma(self.post_attention_layernorm) is not None ): return ( - self._mlp_input_reduce_output_and_update_and_read_residual_cutedsl, + FusedMlpInput( + completes=SumGroup.TP, + run=self._mlp_input_reduce_output_and_update_and_read_residual_cutedsl, + may_return_new_residual=True, + ), *fusions, ) return fusions diff --git a/test/registered/unit/layers/moe/test_cutedsl_ar_fusion.py b/test/registered/unit/layers/moe/test_cutedsl_ar_fusion.py index e6769dbc51..2f28e1d607 100644 --- a/test/registered/unit/layers/moe/test_cutedsl_ar_fusion.py +++ b/test/registered/unit/layers/moe/test_cutedsl_ar_fusion.py @@ -4,6 +4,7 @@ from unittest.mock import patch import pytest import torch +from sglang.srt.layers.boundary_layout import SumGroup from sglang.srt.layers.communicator import ( LayerCommunicator, ScatterMode, @@ -122,12 +123,17 @@ def test_cutedsl_entries_come_before_the_base_fused_kernel(): comm.layer_scatter_modes = SimpleNamespace( layer_input_mode=ScatterMode.TP_ATTN_FULL ) - assert comm._select_mlp_input_fusions() == (cutedsl, base) + fusions = comm._select_mlp_input_fusions() + assert [f.run for f in fusions] == [cutedsl, base] + # The workspace reduces over the TP group, which is the attention-TP + # group here; both kernels hand back a new residual. + assert [f.completes for f in fusions] == [SumGroup.TP, SumGroup.ATTN_TP] + assert all(f.may_return_new_residual for f in fusions) # A scattered residual is gathered first, which the workspace does not do. comm.layer_scatter_modes = SimpleNamespace( layer_input_mode=ScatterMode.SCATTERED ) - assert comm._select_mlp_input_fusions() == (base,) + assert [f.run for f in comm._select_mlp_input_fusions()] == [base] def test_a_replicated_output_producer_keeps_its_own_all_reduce(eligible): diff --git a/test/registered/unit/layers/test_declared_decoder_boundary.py b/test/registered/unit/layers/test_declared_decoder_boundary.py index 16eff6ad24..fe54a4597f 100644 --- a/test/registered/unit/layers/test_declared_decoder_boundary.py +++ b/test/registered/unit/layers/test_declared_decoder_boundary.py @@ -308,7 +308,7 @@ class TestWhichLayersUseDeclarations(CustomTestCase): communicator._mlp_input.keywords["fusions"], (entry,) if fuses else (), ) - self.assertIs(communicator._mlp_input_may_fuse, fuses) + self.assertIs(communicator._mlp_input_may_return_new_residual, fuses) self.assertFalse(communicator._postprocess_scatters_to_local_tokens) self.assertIs( communicator._communicate_summable_tensor_pair_fn, @@ -468,7 +468,7 @@ class TestTheAttentionOutputDecidesItsSum(CustomTestCase): TokenAxis.ATTN_TP_SCATTER: 2, } sides = sides_of(sizes) - steps = comm._select_ffn_input( + steps, _ = comm._select_ffn_input( produced, residual=sides.input_rows, residual_to=sides.ffn_residual_rows, @@ -555,6 +555,71 @@ class TestTheAttentionOutputDecidesItsSum(CustomTestCase): self.run_steps(produced) +class TestFusedKernelsTakeOnlyTheStepsTheyComplete(CustomTestCase): + """A fused kernel replaces the attention-TP sum, the residual add and the + norm only where those are the steps, and only if it completes the sum the + attention output owes.""" + + def fused(self, completes, may_return_new_residual): + return comm.FusedMlpInput( + completes=completes, + run=lambda h, r, fb: None, + may_return_new_residual=may_return_new_residual, + ) + + def select(self, *, attn_dp, fusions): + sides = sides_of( + { + TokenAxis.ATTN_DP: attn_dp, + TokenAxis.ATTN_CP: 1, + TokenAxis.ATTN_TP_SCATTER: 2, + } + ) + return comm._select_ffn_input( + sides.attention_output, + residual=sides.input_rows, + residual_to=sides.ffn_residual_rows, + need=sides.ffn, + force_layernorm_before_gather=False, + fusions=fusions, + ) + + def test_a_kernel_over_another_group_is_not_chosen(self): + over_tp = self.fused(SumGroup.TP, True) + over_attention_tp = self.fused(SumGroup.ATTN_TP, False) + steps, chosen = self.select(attn_dp=1, fusions=(over_tp, over_attention_tp)) + self.assertIs(steps.func, comm._mlp_input_without_dp) + self.assertEqual(steps.keywords["fusions"], (over_attention_tp.run,)) + self.assertEqual(chosen, (over_attention_tp,)) + + def test_no_kernel_is_chosen_where_rows_are_gathered(self): + steps, chosen = self.select( + attn_dp=2, fusions=(self.fused(SumGroup.ATTN_TP, True),) + ) + self.assertIs(steps.func, comm._mlp_input_dp_partial) + self.assertEqual(chosen, ()) + + def test_aux_capture_follows_the_chosen_kernels(self): + for kernels, expected in ( + ((self.fused(SumGroup.ATTN_TP, False),), False), + ((self.fused(SumGroup.ATTN_TP, True),), True), + ((self.fused(SumGroup.TP, True),), False), + ): + with self.subTest(kernels=kernels): + + class Declaring(LayerCommunicator): + def _select_mlp_input_fusions(self): + return kernels + + with planning(parallel_of(attn_dp=1, attn_tp=2)): + communicator = Declaring( + layer_scatter_modes=layer_facts(1, 3), + input_layernorm=Norm(), + post_attention_layernorm=Norm(), + ) + self.assertIs(communicator._mlp_input_may_return_new_residual, expected) + + # --------------------------------------------------------------------------- # Two consecutive layers, rank by rank.