diff --git a/python/sglang/srt/arg_groups/moe_hook.py b/python/sglang/srt/arg_groups/moe_hook.py index 599cef5f10..4306ac25eb 100644 --- a/python/sglang/srt/arg_groups/moe_hook.py +++ b/python/sglang/srt/arg_groups/moe_hook.py @@ -22,7 +22,10 @@ from sglang.srt.arg_groups.overrides import ( resolving_view, run_post_process_pass, ) -from sglang.srt.configs.moe_model_registry import model_supports_deepep_v2 +from sglang.srt.configs.moe_model_registry import ( + model_deepep_v2_prefill_dispatch_tokens, + model_supports_deepep_v2, +) from sglang.srt.connector import ConnectorType from sglang.srt.environ import envs from sglang.srt.model_executor.cuda_graph_config import Backend, Phase, with_phase @@ -541,6 +544,17 @@ def validate_deepep_v2_speculative_draft(server_args: Any) -> None: ) +def required_deepep_v2_prefill_tokens_per_rank(server_args: Any) -> int: + """Largest prefill dispatch on one rank, after model-specific sharding.""" + view = resolved_view(server_args) + tokens = max_prefill_buffer_tokens(server_args) or (view.max_prefill_tokens or 0) + return model_deepep_v2_prefill_dispatch_tokens( + hf_config=model_config_of(server_args).hf_config, + cfg=view, + default_tokens=tokens, + ) + + def validate_deepep_v2_dispatch_token_budget(server_args: Any) -> None: """Check the configured prefill and decode-graph buffer bounds.""" view = resolved_view(server_args) @@ -549,9 +563,7 @@ def validate_deepep_v2_dispatch_token_budget(server_args: Any) -> None: capacity = envs.SGLANG_DEEPEP_V2_NUM_MAX_DISPATCH_TOKENS_PER_RANK.get() if view.disaggregation_mode != "decode": - prefill_tokens = max_prefill_buffer_tokens(server_args) or ( - view.max_prefill_tokens or 0 - ) + prefill_tokens = required_deepep_v2_prefill_tokens_per_rank(server_args) if prefill_tokens > capacity: raise ValueError( "DeepEP v2 per-rank prefill budget exceeds " diff --git a/python/sglang/srt/configs/moe_model_registry.py b/python/sglang/srt/configs/moe_model_registry.py index 104db14eef..2bfb1f7815 100644 --- a/python/sglang/srt/configs/moe_model_registry.py +++ b/python/sglang/srt/configs/moe_model_registry.py @@ -1,47 +1,79 @@ -"""Model eligibility and MXFP8 precision policy for the DeepEP v2 adapter. +"""Model eligibility, precision and prefill sizing for the DeepEP v2 adapter. External model packages register architecture names before server-argument validation, without adding private architecture names to SGLang core. -Only the primary architecture controls eligibility and precision. +Only the primary architecture selects the model policy. """ from __future__ import annotations -from typing import Any +from typing import Any, Callable -# Membership enables DeepEP v2; the value requests FP32 SiLU intermediates. -_DEEPEP_V2_MODELS: dict[str, bool] = { - "DeepseekV3ForCausalLM": False, - "DeepseekV4ForCausalLM": False, - "Qwen3MoeForCausalLM": False, - "Glm5NextForConditionalGeneration": False, +import msgspec + + +class _DeepEPv2ModelPolicy(msgspec.Struct, frozen=True): + silu_mul_keep_fp32: bool = False + prefill_dispatch_tokens: Callable[[Any, int], int] | None = None + + +_DEEPEP_V2_MODELS: dict[str, _DeepEPv2ModelPolicy] = { + "DeepseekV3ForCausalLM": _DeepEPv2ModelPolicy(), + "DeepseekV4ForCausalLM": _DeepEPv2ModelPolicy(), + "Qwen3MoeForCausalLM": _DeepEPv2ModelPolicy(), + "Glm5NextForConditionalGeneration": _DeepEPv2ModelPolicy(), } def register_deepep_v2_model( - architecture: str, *, silu_mul_keep_fp32: bool = False + architecture: str, + *, + silu_mul_keep_fp32: bool = False, + prefill_dispatch_tokens: Callable[[Any, int], int] | None = None, ) -> None: - """Register a model architecture with validated DeepEP v2 semantics.""" + """Register a model architecture with validated DeepEP v2 semantics. - if ( - architecture in _DEEPEP_V2_MODELS - and _DEEPEP_V2_MODELS[architecture] != silu_mul_keep_fp32 - ): - raise ValueError(f"Conflicting DeepEP v2 precision policy for {architecture}") - _DEEPEP_V2_MODELS[architecture] = silu_mul_keep_fp32 + ``prefill_dispatch_tokens(cfg, tokens)`` receives a read-only resolved + configuration and the prefill-buffer ceiling. It returns the maximum + tokens one rank dispatches, without changing scheduler/buffer settings. + Omitting it preserves the default, unsharded ceiling. + """ + + policy = _DeepEPv2ModelPolicy( + silu_mul_keep_fp32=silu_mul_keep_fp32, + prefill_dispatch_tokens=prefill_dispatch_tokens, + ) + if architecture in _DEEPEP_V2_MODELS and _DEEPEP_V2_MODELS[architecture] != policy: + raise ValueError(f"Conflicting DeepEP v2 model policy for {architecture}") + _DEEPEP_V2_MODELS[architecture] = policy + + +def _policy_of(hf_config: Any) -> _DeepEPv2ModelPolicy | None: + architectures = getattr(hf_config, "architectures", None) or () + if not architectures: + return None + return _DEEPEP_V2_MODELS.get(architectures[0]) def model_supports_deepep_v2(hf_config: Any) -> bool: """Check model eligibility, independently of runtime layout and topology.""" - architectures = getattr(hf_config, "architectures", None) or () - architecture = architectures[0] if architectures else None - return architecture is not None and architecture in _DEEPEP_V2_MODELS + return _policy_of(hf_config) is not None def model_requires_fp32_silu_mul(hf_config: Any) -> bool: """Whether this model opts into FP32 intermediates for DeepEP v2 MXFP8.""" - architectures = getattr(hf_config, "architectures", None) or () - architecture = architectures[0] if architectures else None - return architecture is not None and _DEEPEP_V2_MODELS.get(architecture, False) + policy = _policy_of(hf_config) + return policy is not None and policy.silu_mul_keep_fp32 + + +def model_deepep_v2_prefill_dispatch_tokens( + hf_config: Any, cfg: Any, default_tokens: int +) -> int: + """Apply the primary model's dispatch sizing, or keep the buffer ceiling.""" + + policy = _policy_of(hf_config) + if policy is None or policy.prefill_dispatch_tokens is None: + return default_tokens + return policy.prefill_dispatch_tokens(cfg, default_tokens) diff --git a/test/registered/unit/layers/moe/test_hpc_ops_runner_guard.py b/test/registered/unit/layers/moe/test_hpc_ops_runner_guard.py index d3e771f87b..dfb0c1d334 100644 --- a/test/registered/unit/layers/moe/test_hpc_ops_runner_guard.py +++ b/test/registered/unit/layers/moe/test_hpc_ops_runner_guard.py @@ -156,20 +156,33 @@ def test_deepep_v2_runner_backstop(_moe_flags): def test_deepep_v2_registration_uses_primary_architecture_and_rejects_conflicts(): from sglang.srt.configs.moe_model_registry import ( + model_deepep_v2_prefill_dispatch_tokens, model_requires_fp32_silu_mul, model_supports_deepep_v2, register_deepep_v2_model, ) - register_deepep_v2_model("TestRoutewiseMoe", silu_mul_keep_fp32=True) + def prefill_dispatch_tokens(cfg, tokens): + return tokens // cfg.tp_size + + register_deepep_v2_model( + "TestRoutewiseMoe", + silu_mul_keep_fp32=True, + prefill_dispatch_tokens=prefill_dispatch_tokens, + ) config = SimpleNamespace(architectures=["TestRoutewiseMoe"]) + cfg = SimpleNamespace(tp_size=8) assert model_supports_deepep_v2(config) assert model_requires_fp32_silu_mul(config) + assert model_deepep_v2_prefill_dispatch_tokens(config, cfg, 32768) == 4096 config.architectures = ["UnsupportedMoe", "TestRoutewiseMoe"] assert not model_supports_deepep_v2(config) assert not model_requires_fp32_silu_mul(config) + assert model_deepep_v2_prefill_dispatch_tokens(config, cfg, 32768) == 32768 with pytest.raises(ValueError, match="Conflicting"): register_deepep_v2_model("TestRoutewiseMoe", silu_mul_keep_fp32=False) + with pytest.raises(ValueError, match="Conflicting"): + register_deepep_v2_model("TestRoutewiseMoe", silu_mul_keep_fp32=True) @pytest.mark.parametrize("block_size,width", [(32, 16), (128, 4)])