-
Notifications
You must be signed in to change notification settings - Fork 7k
moe: add DeepEP V2 ElasticBuffer support to MoE token dispatcher #24443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dmvevents
wants to merge
4
commits into
sgl-project:main
Choose a base branch
from
dmvevents:deepep-v2-elasticbuffer-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+239
−0
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8c883d3
moe: add DeepEP V2 ElasticBuffer probe and opt-in buffer path
AntonAI f66ab63
moe: add CPU-only unit test for DeepEP V2 probe plumbing
AntonAI ecb4b0e
lint: apply black-jupyter auto-formatting
dmvevents f9f58ed
moe: address gemini-code-assist review on DeepEP V2 path
dmvevents File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| """CPU-only unit test for the DeepEP V2 probe plumbing in | ||
| `sglang.srt.layers.moe.token_dispatcher.deepep`. | ||
|
|
||
| Exercises the three reachable probe states: | ||
|
|
||
| 1. V1 `Buffer` available, V2 `ElasticBuffer` missing | ||
| 2. V2 `ElasticBuffer` available, V1 `Buffer` also available (normal V2 install) | ||
| 3. Neither available | ||
|
|
||
| The test does not execute any DeepEP kernels — it imports the module | ||
| under a monkey-patched `deep_ep` stub so it runs on any CPU host without | ||
| CUDA / NCCL / EFA. It is purely a guard against the probe pattern | ||
| silently regressing. | ||
|
|
||
| Run: | ||
| pytest -xvs test/srt/test_deepep_v2_probe.py | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import importlib | ||
| import sys | ||
| import types | ||
| import unittest | ||
| from contextlib import contextmanager | ||
|
|
||
|
|
||
| def _make_stub_deep_ep(*, with_buffer: bool, with_elastic_buffer: bool): | ||
| """Build a fake `deep_ep` module with the attributes we opt to expose.""" | ||
|
|
||
| mod = types.ModuleType("deep_ep") | ||
| if with_buffer: | ||
|
|
||
| class _StubBuffer: # noqa: D401 - placeholder class | ||
| num_sms = 20 | ||
|
|
||
| @staticmethod | ||
| def get_dispatch_config(size): # pragma: no cover - unused in test | ||
| return None | ||
|
|
||
| @staticmethod | ||
| def get_combine_config(size): # pragma: no cover - unused in test | ||
| return None | ||
|
|
||
| mod.Buffer = _StubBuffer | ||
| mod.Config = object | ||
| if with_elastic_buffer: | ||
|
|
||
| class _StubElasticBuffer: # noqa: D401 - placeholder class | ||
| pass | ||
|
|
||
| mod.ElasticBuffer = _StubElasticBuffer | ||
| return mod | ||
|
|
||
|
|
||
| @contextmanager | ||
| def _patched_deep_ep(*, with_buffer: bool, with_elastic_buffer: bool): | ||
| """Install a stub `deep_ep` in sys.modules and drop the SGLang | ||
| deepep module from the cache so it re-runs its imports against the | ||
| stub. Restores the original state on exit. | ||
| """ | ||
|
|
||
| saved = { | ||
| k: sys.modules[k] | ||
| for k in list(sys.modules) | ||
| if k == "deep_ep" or k.startswith("deep_ep.") | ||
| } | ||
| saved_deepep = sys.modules.pop( | ||
| "sglang.srt.layers.moe.token_dispatcher.deepep", None | ||
| ) | ||
| # Wipe the stub path | ||
| sys.modules.pop("deep_ep", None) | ||
| if with_buffer or with_elastic_buffer: | ||
| sys.modules["deep_ep"] = _make_stub_deep_ep( | ||
| with_buffer=with_buffer, with_elastic_buffer=with_elastic_buffer | ||
| ) | ||
| try: | ||
| yield | ||
| finally: | ||
| # Best-effort restore; we don't try to rewind every transitive | ||
| # import that the probe triggered. | ||
| sys.modules.pop("deep_ep", None) | ||
| for k, v in saved.items(): | ||
| sys.modules[k] = v | ||
| if saved_deepep is not None: | ||
| sys.modules["sglang.srt.layers.moe.token_dispatcher.deepep"] = saved_deepep | ||
|
|
||
|
|
||
| class TestDeepEPV2Probe(unittest.TestCase): | ||
| """Guard the V2 probe plumbing against silent regression.""" | ||
|
|
||
| def _import_probe_flags(self): | ||
| sys.modules.pop("sglang.srt.layers.moe.token_dispatcher.deepep", None) | ||
| mod = importlib.import_module("sglang.srt.layers.moe.token_dispatcher.deepep") | ||
| return ( | ||
| getattr(mod, "use_deepep"), | ||
| getattr(mod, "have_deepep_v2"), | ||
| ) | ||
|
|
||
| def test_v1_only_installed(self): | ||
| """Legacy install (V1 `Buffer` only). `use_deepep=True`, | ||
| `have_deepep_v2=False`. No regression on pre-V2 users.""" | ||
| with _patched_deep_ep(with_buffer=True, with_elastic_buffer=False): | ||
| try: | ||
| use_deepep, have_v2 = self._import_probe_flags() | ||
| except ImportError: | ||
| # Test env may not have full sglang deps; that's fine, | ||
| # the probe itself is what we need to prove compiles. | ||
| self.skipTest("sglang module stack unavailable on test host") | ||
| return | ||
| self.assertTrue(use_deepep) | ||
| self.assertFalse(have_v2) | ||
|
|
||
| def test_v2_installed(self): | ||
| """V2 install (both `Buffer` legacy + `ElasticBuffer` exported). | ||
| `use_deepep=True`, `have_deepep_v2=True`. The V2 path is now | ||
| reachable behind `SGLANG_DEEPEP_USE_V2=1`.""" | ||
| with _patched_deep_ep(with_buffer=True, with_elastic_buffer=True): | ||
| try: | ||
| use_deepep, have_v2 = self._import_probe_flags() | ||
| except ImportError: | ||
| self.skipTest("sglang module stack unavailable on test host") | ||
| return | ||
| self.assertTrue(use_deepep) | ||
| self.assertTrue(have_v2) | ||
|
|
||
| def test_neither_installed(self): | ||
| """deep_ep missing entirely. `use_deepep=False`, | ||
| `have_deepep_v2=False`. Module must still import cleanly so the | ||
| SGLang package can load on non-MoE targets.""" | ||
| with _patched_deep_ep(with_buffer=False, with_elastic_buffer=False): | ||
| try: | ||
| use_deepep, have_v2 = self._import_probe_flags() | ||
| except ImportError: | ||
| self.skipTest("sglang module stack unavailable on test host") | ||
| return | ||
| self.assertFalse(use_deepep) | ||
| self.assertFalse(have_v2) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To improve code robustness and assist static analysis tools, it is recommended to explicitly define
ElasticBufferasNonein theexceptblock. This ensures the symbol is always present in the module namespace, even if the import fails.