From 3bffe69b74b1391c1735f57f9d2262a0630ef14e Mon Sep 17 00:00:00 2001 From: metamergebot Date: Fri, 9 Oct 2026 10:55:02 -0700 Subject: [PATCH] [moe] Fill num_token_non_padded for the customized A2A backend at EP 1 (#43245) Co-authored-by: Hanming Lu <69857889+hanming-lu@users.noreply.github.com> Co-authored-by: cctry --- .../srt/model_executor/forward_batch_info.py | 14 +++++++++---- .../test_num_token_non_padded_localization.py | 21 ++++++++++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/python/sglang/srt/model_executor/forward_batch_info.py b/python/sglang/srt/model_executor/forward_batch_info.py index 1efa971f40..0532121a0f 100644 --- a/python/sglang/srt/model_executor/forward_batch_info.py +++ b/python/sglang/srt/model_executor/forward_batch_info.py @@ -675,8 +675,7 @@ class ForwardBatch(ForwardBatchDeepSeekMHAMixin): # the eager forward and the cuda-graph # registry localize from on each forward / # replay. Present only when - # enable_num_token_non_padded() - # (moe_ep_size > 1). + # enable_num_token_non_padded(). # global_num_token_non_padded_cpu host int. Host-side attention/backend # slices read it directly; the prefill # graph registry derives its per-rank GPU @@ -2108,8 +2107,15 @@ class ForwardBatch(ForwardBatchDeepSeekMHAMixin): def enable_num_token_non_padded(): - # Elastic joiners also need graph padding masked after joining WORLD. - return get_parallel().moe_ep_size > 1 or world_dp_gather_enabled() + from sglang.srt.layers.moe.utils import get_moe_a2a_backend + + # Elastic joiners also need graph padding masked after joining WORLD; a + # customized A2A backend consumes the count whatever the EP size. + return ( + get_parallel().moe_ep_size > 1 + or world_dp_gather_enabled() + or get_moe_a2a_backend().is_customized() + ) def build_inner_fb_view( diff --git a/test/registered/unit/model_executor/test_num_token_non_padded_localization.py b/test/registered/unit/model_executor/test_num_token_non_padded_localization.py index af93d3cd17..ba21567196 100644 --- a/test/registered/unit/model_executor/test_num_token_non_padded_localization.py +++ b/test/registered/unit/model_executor/test_num_token_non_padded_localization.py @@ -29,13 +29,15 @@ from unittest.mock import patch import torch from sglang.srt.layers.moe import utils as moe_utils +from sglang.srt.layers.moe.utils import MoeA2ABackend from sglang.srt.model_executor.forward_batch_info import ( ForwardBatch, ForwardMode, compute_local_num_token_non_padded, compute_local_num_token_non_padded_cpu, + enable_num_token_non_padded, ) -from sglang.srt.runtime_context import get_parallel +from sglang.srt.runtime_context import get_flags, get_parallel from sglang.test.communicator_patch import patch_communicator from sglang.test.test_utils import CustomTestCase @@ -235,5 +237,22 @@ class TestMoeNumTokenNonPaddedTable(CustomTestCase): self.assertIsNone(_value(_forward_batch(sharded=False, local=None))) +class TestEnableNumTokenNonPadded(CustomTestCase): + def test_customized_a2a_backend_fills_the_count_at_ep1(self): + """At EP 1 without a WORLD-group DP gather, the customized backend + enables the count and `none` leaves it disabled.""" + for backend, expected in ( + (MoeA2ABackend.NONE, False), + (MoeA2ABackend.CUSTOMIZED, True), + ): + with ( + self.subTest(backend=backend.value), + get_parallel().override(moe_ep_size=1), + get_flags().moe.override(a2a_backend=backend), + get_flags().dp.override(use_world_group_for_gather=False), + ): + self.assertIs(enable_num_token_non_padded(), expected) + + if __name__ == "__main__": unittest.main()