Skip to content

Add DeepEP v2 flex dispatcher backend - #5153

Open
Autumn1998 wants to merge 8 commits into
NVIDIA:mainfrom
Autumn1998:tongliu/deepepv2-flex-dispatcher-main
Open

Add DeepEP v2 flex dispatcher backend#5153
Autumn1998 wants to merge 8 commits into
NVIDIA:mainfrom
Autumn1998:tongliu/deepepv2-flex-dispatcher-main

Conversation

@Autumn1998

@Autumn1998 Autumn1998 commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

What does this PR do ?

The DeepEP V2 support as a backend of flex dispatcher.

PR on dev: #4793

⚠️ For major changes (either in lines of code or in its impact), please make sure to first share a design doc with the team. If you're unsure what's the best way to do so, contact @NVIDIA/mcore-oncall.

Issue tracking

For PRs from open-source community contributors:

  • New features: a linked issue is required. Please open a feature request and reference it here before submitting the PR.
  • Small updates (bug fixes, minor improvements): a linked issue is recommended and will accelerate the PR review process.

Linked issue:

Contribution process

Pre-checks

  • I have added relevant unit tests
  • I have added relevant functional tests
  • I have added proper typing to my code Typing guidelines
  • I have added relevant documentation
  • I have run the autoformatter.sh on my PR

Code review

Feel free to message or comment @NVIDIA/mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!

All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.

Step 1: Mark PR as "Ready for Review"

  1. When your PR is ready, click Ready for Review.
  2. An oncall reviewer is auto-assigned and expert reviewers are notified based on your changes.
    • Some PRs may jump straight to step 2. This is determined by .github/CODEOWNERS.

⚠️ Only mark as ready once merge-conflicts are resolved and the CI is passing.
Final Review might get declined if these requirements are not fulfilled.

Step 2: Final Review

For PRs that change megatron/core, once all expert reviewers have approved, the Final Review label is applied automatically and final reviewers are assigned.

For PRs outside megatron/core, this step is skipped.

Step 3: Approved

Once all required reviewers have approved, the Approved label is applied automatically.

Merge

Any member of mcore-engineers will be able to merge your PR.

@Autumn1998
Autumn1998 requested review from a team as code owners June 4, 2026 08:15
@copy-pr-bot

copy-pr-bot Bot commented Jun 4, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@svcnvidia-nemo-ci
svcnvidia-nemo-ci marked this pull request as draft June 4, 2026 08:15
@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

This PR has been automatically converted to draft because all PRs must start as drafts.

When you are ready for review, click Ready for Review to begin the review process. This will:

  1. Add the oncall reviewer (optional reviewer)
  2. Add required review teams based on your changes

See the contribution guide for more details.

@Autumn1998 Autumn1998 changed the title Tongliu/deepepv2 flex dispatcher main Add DeepEP v2 flex dispatcher backend Jun 4, 2026
@Autumn1998

Copy link
Copy Markdown
Contributor Author

/ok to test 646cceb

@Autumn1998

Copy link
Copy Markdown
Contributor Author

/ok to test c0a73a0

Signed-off-by: tongliu <tongliu@nvidia.com>
Signed-off-by: tongliu <tongliu@nvidia.com>
Signed-off-by: tongliu <tongliu@nvidia.com>
Signed-off-by: tongliu <tongliu@nvidia.com>
Signed-off-by: tongliu <tongliu@nvidia.com>
Signed-off-by: tongliu <tongliu@nvidia.com>
Signed-off-by: tongliu <tongliu@nvidia.com>
@Autumn1998
Autumn1998 force-pushed the tongliu/deepepv2-flex-dispatcher-main branch from 7b85b7d to 30e602f Compare June 12, 2026 04:33
@Autumn1998

Copy link
Copy Markdown
Contributor Author

/ok to test 30e602f

@YangFei1990 YangFei1990 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should merge DeepEPv2 and v2 instead of having complete separate APIs? I understand separation brought benefit for easy management, but currently from what I see almost 80-90% of code are duplicated.
Also, can you update Dockerfile.ci.dev's DEEPEP_COMMIT to point to newer commit in hybrid-ep so DeepEP v2 is also supported?

return _buffer


def get_elastic_buffer(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering why do we have this buffer reuse mechanism for DeepEPv2, seems it is still full eager execution (shape is reading during the runtime). Also is it safe to have different layer/mb to reuse the same buffer

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is the recommended way to use DeepEP v2:https://nvidia.slack.com/archives/C03V462SAMS/p1779421202250149

def _get_buffer(self, hidden_states: torch.Tensor):
self.buffer = get_elastic_buffer(
self.group,
num_max_tokens_per_rank=hidden_states.shape[0],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the num_max_tokens_per_rank be different across ranks?

@Autumn1998 Autumn1998 Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think different ranks should still pass in different values, so in the THD case we may need to pad.

@Autumn1998

Copy link
Copy Markdown
Contributor Author

Do you think we should merge DeepEPv2 and v2 instead of having complete separate APIs? I understand separation brought benefit for easy management, but currently from what I see almost 80-90% of code are duplicated. Also, can you update Dockerfile.ci.dev's DEEPEP_COMMIT to point to newer commit in hybrid-ep so DeepEP v2 is also supported?

I actually think it’s fine. The mirror PR in dev has already been merged. This kind of refactoring will cause conflicts when dev pulls from main later. If a fix is really needed, we can submit another PR afterward.

Currently, hep hasn’t merged deepep’s main yet, so v2 isn’t supported. At the moment, we can’t support both deepep and hep in a single commit.

@YangFei1990

YangFei1990 commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Do you think we should merge DeepEPv2 and v2 instead of having complete separate APIs? I understand separation brought benefit for easy management, but currently from what I see almost 80-90% of code are duplicated. Also, can you update Dockerfile.ci.dev's DEEPEP_COMMIT to point to newer commit in hybrid-ep so DeepEP v2 is also supported?

I actually think it’s fine. The mirror PR in dev has already been merged. This kind of refactoring will cause conflicts when dev pulls from main later. If a fix is really needed, we can submit another PR afterward.

Currently, hep hasn’t merged deepep’s main yet, so v2 isn’t supported. At the moment, we can’t support both deepep and hep in a single commit.

Is there a plan to update hep?

@Autumn1998

Copy link
Copy Markdown
Contributor Author

Do you think we should merge DeepEPv2 and v2 instead of having complete separate APIs? I understand separation brought benefit for easy management, but currently from what I see almost 80-90% of code are duplicated. Also, can you update Dockerfile.ci.dev's DEEPEP_COMMIT to point to newer commit in hybrid-ep so DeepEP v2 is also supported?

I actually think it’s fine. The mirror PR in dev has already been merged. This kind of refactoring will cause conflicts when dev pulls from main later. If a fix is really needed, we can submit another PR afterward.
Currently, hep hasn’t merged deepep’s main yet, so v2 isn’t supported. At the moment, we can’t support both deepep and hep in a single commit.

Is there a plan to update hep?

maybe not

…-dispatcher-main

Signed-off-by: tongliu <tongliu@nvidia.com>

# Conflicts:
#	megatron/core/models/gpt/fine_grained_callables.py
#	megatron/core/transformer/moe/token_dispatcher.py
#	megatron/core/transformer/transformer_config.py
#	tests/unit_tests/transformer/moe/test_token_dispatcher.py
@LitLeo

LitLeo commented Aug 5, 2026

Copy link
Copy Markdown

Unit test fix: SIGSEGV in DeepEP v2 tests due to uninitialized NCCL communicator

python -m torch.distributed.run --nproc_per_node=8 --nnodes=1 --master_addr=localhost --master_port=6000 -m pytest tests/unit_tests/transformer/moe/test_token_dispatcher.py

error:

Current thread 0x00007fbefac30000 [python] (most recent call first):
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/deep_ep/buffers/elastic.py", line 403 in get_buffer_size_hint
  File "/home/guangshu/1-codes/Megatron-LM/megatron/core/transformer/moe/fused_a2a.py", line 93 in get_elastic_buffer
  File "/home/guangshu/1-codes/Megatron-LM/megatron/core/transformer/moe/token_dispatcher.py", line 1454 in _get_buffer
  File "/home/guangshu/1-codes/Megatron-LM/megatron/core/transformer/moe/token_dispatcher.py", line 1475 in dispatch
  File "/home/guangshu/1-codes/Megatron-LM/megatron/core/transformer/moe/token_dispatcher.py", line 1889 in token_dispatch
  File "/home/guangshu/1-codes/Megatron-LM/tests/unit_tests/transformer/moe/test_token_dispatcher.py", line 22 in token_permutation
  File "/home/guangshu/1-codes/Megatron-LM/tests/unit_tests/transformer/moe/test_token_dispatcher.py", line 138 in dispatcher_dropless_test
  File "/home/guangshu/1-codes/Megatron-LM/tests/unit_tests/transformer/moe/test_token_dispatcher.py", line 508 in test_forward_backward
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/_pytest/python.py", line 167 in pytest_pyfunc_call
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/pluggy/_callers.py", line 121 in _multicall
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/pluggy/_manager.py", line 120 in _hookexec
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/pluggy/_hooks.py", line 512 in __call__
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/_pytest/python.py", line 1707 in runtest
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/_pytest/runner.py", line 184 in pytest_runtest_call
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/pluggy/_callers.py", line 121 in _multicall
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/pluggy/_manager.py", line 120 in _hookexec
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/pluggy/_hooks.py", line 512 in __call__
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/_pytest/runner.py", line 250 in <lambda>
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/_pytest/runner.py", line 361 in from_call
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/_pytest/runner.py", line 249 in call_and_report
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/_pytest/runner.py", line 139 in runtestprotocol
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/_pytest/runner.py", line 118 in pytest_runtest_protocol
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/pluggy/_callers.py", line 121 in _multicall
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/pluggy/_manager.py", line 120 in _hookexec
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/pluggy/_hooks.py", line 512 in __call__
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/_pytest/main.py", line 408 in pytest_runtestloop
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/pluggy/_callers.py", line 121 in _multicall
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/pluggy/_manager.py", line 120 in _hookexec
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/pluggy/_hooks.py", line 512 in __call__
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/_pytest/main.py", line 384 in _main
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/_pytest/main.py", line 330 in wrap_session
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/_pytest/main.py", line 377 in pytest_cmdline_main
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/pluggy/_callers.py", line 121 in _multicall
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/pluggy/_manager.py", line 120 in _hookexec
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/pluggy/_hooks.py", line 512 in __call__
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/_pytest/config/__init__.py", line 229 in _main
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/_pytest/config/__init__.py", line 253 in _console_main
  File "/opt/venv/python3.14.4/lib/python3.14/site-packages/pytest/__main__.py", line 9 in <module>
  File "<frozen runpy>", line 88 in _run_code
  File "<frozen runpy>", line 198 in _run_module_as_main

Root cause:

PyTorch lazily initializes NCCL communicators — ProcessGroupNCCL._comm_ptr() returns 0 (null) until the first collective operation is performed on that group.

DeepEP v2's get_nccl_comm_handle() calls backend._comm_ptr() and passes the result directly to the C++ function calculate_elastic_buffer_size(). When the communicator hasn't been initialized yet, this dereferences a null pointer → SIGSEGV.

In training this is never a problem because process groups are already warmed up by model initialization and gradient syncs before the first MoE forward pass. But in unit tests, initialize_model_parallel() creates fresh sub-groups and the first operation on tp_ep_group is ElasticBuffer.get_buffer_size_hint(), which crashes.

Fix:

Add a single dummy all_reduce on tp_ep_group in the test fixture (MoEModelTestContainer) when moe_flex_dispatcher_backend == "deepepv2". This forces NCCL communicator creation before DeepEP v2 reads the pointer.

Update(tests/unit_tests/transformer/moe/test_token_dispatcher.py)
  ⎿  Added 6 lines
       98          # init moe layer
       99          self.moe_layer = self.new_moe_layer()
      100
      101 +        # DeepEP v2 reads _comm_ptr() which is null until the first collective.
      102 +        # Warm up the tp_ep group so the NCCL communicator is initialized.
      103 +        if kwargs.get("moe_flex_dispatcher_backend") == "deepepv2":
      104 +            tp_ep_group = self.moe_layer.token_dispatcher.tp_ep_group
      105 +            torch.distributed.all_reduce(torch.zeros(1, device="cuda"), group=tp_ep_group)
      106 +
      107      def new_moe_layer(self, **kargs):
      108          submodules = get_submodules(
      109              get_gpt_layer_local_submodules(

Reference: DeepEP's own test (tests/elastic/test_ep.py) avoids this by passing device_id to init_process_group(), which triggers eager NCCL initialization. We use the more targeted warmup approach to avoid symmetric memory pressure from repeated group creation/destruction in the test suite.

previous_event = buffer.capture() if async_finish and allocate_on_comm_stream else None
# Process the dispatch and keep the handle for the subsequent combine call.
recv_x, recv_token_indices, recv_token_probs, handle, event = buffer.dispatch(
x,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could support do_expand=True ? @Autumn1998

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants