From b563bbd59784a660f3de9a3cadd6f0e1bd24af35 Mon Sep 17 00:00:00 2001 From: Gawr Gura 0721 <121242164+LiRunGuo@users.noreply.github.com> Date: Tue, 6 Oct 2026 16:58:02 +0800 Subject: [PATCH] [Fix] Don't let a failed deep_ep import-time check kill servers that never use DeepEP (#40671) --- .../srt/layers/moe/token_dispatcher/deepep.py | 15 ++- .../layers/moe/token_dispatcher/deepep_v2.py | 2 +- .../layers/moe/test_deepep_import_guard.py | 92 +++++++++++++++++++ 3 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 test/registered/unit/layers/moe/test_deepep_import_guard.py diff --git a/python/sglang/srt/layers/moe/token_dispatcher/deepep.py b/python/sglang/srt/layers/moe/token_dispatcher/deepep.py index 6e7588d9eb..2c3670a839 100644 --- a/python/sglang/srt/layers/moe/token_dispatcher/deepep.py +++ b/python/sglang/srt/layers/moe/token_dispatcher/deepep.py @@ -45,6 +45,7 @@ _use_zbal = _is_npu and envs.SGLANG_ZBAL_LOCAL_MEM_SIZE.get() > 0 if TYPE_CHECKING: from sglang.srt.batch_overlap.single_batch_overlap import CombineOverlapArgs +_deepep_import_error: Optional[BaseException] = None try: if _use_zbal: from zbal.zbal.deepep_adaptor import Config @@ -58,8 +59,11 @@ try: ) use_deepep = True -except ImportError: +except Exception as exc: + # deep_ep's import-time host checks raise more than ImportError; defer any + # failure to the first DeepEP use so other models still start. use_deepep = False + _deepep_import_error = exc from enum import Enum, IntEnum, auto @@ -375,9 +379,14 @@ class _DeepEPDispatcherImplBase: ): if not use_deepep: raise ImportError( - "DeepEP is not installed. Please install DeepEP package from " + "DeepEP is not available. Please install DeepEP package from " "https://github.com/deepseek-ai/deepep." - ) + + ( + f" Original import error: {_deepep_import_error}" + if _deepep_import_error is not None + else "" + ) + ) from _deepep_import_error self.group = group self.router_topk = router_topk diff --git a/python/sglang/srt/layers/moe/token_dispatcher/deepep_v2.py b/python/sglang/srt/layers/moe/token_dispatcher/deepep_v2.py index b8050c5910..a69bb9f0c9 100644 --- a/python/sglang/srt/layers/moe/token_dispatcher/deepep_v2.py +++ b/python/sglang/srt/layers/moe/token_dispatcher/deepep_v2.py @@ -41,7 +41,7 @@ try: from deep_ep import ElasticBuffer use_deepep_v2 = True -except (ImportError, OSError) as exc: +except Exception as exc: # deep_ep's import-time host checks; see deepep.py use_deepep_v2 = False _deepep_v2_import_error = exc diff --git a/test/registered/unit/layers/moe/test_deepep_import_guard.py b/test/registered/unit/layers/moe/test_deepep_import_guard.py new file mode 100644 index 0000000000..d3706fa162 --- /dev/null +++ b/test/registered/unit/layers/moe/test_deepep_import_guard.py @@ -0,0 +1,92 @@ +"""CPU regression: a failing ``deep_ep`` import must not break other servers. + +``deep_ep`` runs host checks at import time that raise more than +``ImportError``, and every server process imports the MoE token dispatchers, so +while the guards only caught ``ImportError`` one failed check (for example a +second ``libnccl`` loaded through ``LD_PRELOAD``) killed the launcher even for +models that never use DeepEP. Each case runs in a fresh interpreter with a +stand-in ``deep_ep`` on ``PYTHONPATH``. +""" + +import os +import subprocess +import sys +import tempfile +import textwrap +import unittest + +from sglang.test.ci.ci_register import register_cpu_ci +from sglang.test.test_utils import CustomTestCase + +register_cpu_ci(est_time=30, suite="base-a-test-cpu") + +_FAILURE = "Duplicate NCCL runtime found in the current system" + +_CHILD = textwrap.dedent( + f""" + import torch + + from sglang.srt.layers.moe.token_dispatcher import deepep, deepep_v2 + + assert deepep.use_deepep is False + assert deepep_v2.use_deepep_v2 is False + + try: + deepep._DeepEPDispatcherImplBase( + group=None, + router_topk=8, + permute_fusion=False, + num_experts=64, + num_local_experts=8, + hidden_size=4096, + params_dtype=torch.bfloat16, + deepep_mode=None, + ) + except ImportError as exc: + assert {_FAILURE!r} in str(exc), str(exc) + assert isinstance(exc.__cause__, AssertionError), repr(exc.__cause__) + else: + raise SystemExit("DeepEP dispatcher was created without deep_ep") + + try: + deepep_v2._ensure_deepep_v2_available() + except ImportError as exc: + assert {_FAILURE!r} in str(exc), str(exc) + else: + raise SystemExit("DeepEP v2 reported available without deep_ep") + + print("OK") + """ +) + + +class TestDeepEPImportGuard(CustomTestCase): + def test_deep_ep_import_assertion_is_deferred(self): + with tempfile.TemporaryDirectory() as fake_root: + pkg = os.path.join(fake_root, "deep_ep") + os.makedirs(pkg) + with open(os.path.join(pkg, "__init__.py"), "w") as f: + f.write(f"raise AssertionError({_FAILURE!r})\n") + + env = dict(os.environ) + env["PYTHONPATH"] = os.pathsep.join( + p for p in (fake_root, env.get("PYTHONPATH")) if p + ) + result = subprocess.run( + [sys.executable, "-c", _CHILD], + env=env, + capture_output=True, + text=True, + timeout=300, + ) + + self.assertEqual( + result.returncode, + 0, + f"stdout:\n{result.stdout}\nstderr:\n{result.stderr[-4000:]}", + ) + self.assertIn("OK", result.stdout) + + +if __name__ == "__main__": + unittest.main()