Skip to content

MOE Sequential Block with Dispatch and Combine as Basic Ops - #3503

Merged
vthumbe1503 merged 4 commits into
NVIDIA:mainfrom
vthumbe1503:nccl_ep_ops
Oct 1, 2026
Merged

vthumbe1503 merged 4 commits into
NVIDIA:mainfrom
vthumbe1503:nccl_ep_ops

Conversation

@vthumbe1503

@vthumbe1503 vthumbe1503 commented Sep 10, 2026 •

Copy link
Copy Markdown
Collaborator

Description

  • Avoid Scale copies in our ScaledActivation TE Sequential Ops

    • Fused Scaled Activation Kernels were integrated to TE Sequential Ops in this PR. Scale(probability/recv_topk_weights in MOE terminology) donot require to be in the same dtype as that of the activation_input. Kernel supports a templated combination of input/scale dtypes.
    • We are modifying the Op to not necessarily change the dtype of scales. Scales are dequantized to bf16 similar to input only iftorch autocast desires it be dequantized. Otherwise we retain its dtype to avoid extra copies.
    • Why was this change needed
      • NCCL Ep requires the grad_topk_weights to be in fp32. We were already passing topk_weights as fp32. But the scaled activation op tried to match input dtype = bf16 and hence we needed back an forth from fp32 to bf16 both in forward and backward implementations of the Op. Avoiding the redundant scale conversion to bf16 is just an optimization since the fused scaled activation kernel anyway supports fp32.
  • Refactoring ep.py

    • Refactoring is done so that the external facing ep APIs for NCCL EP(ep_dispatch and ep_combine) and the MOE Sequential Ops(Moe_Dispatch and Moe_Combin) share the code.
    • Another class called EpConfig is added. The intent is to decouple the config from the mechanism of ep(nccl ep). For example MOE Dispatch passes implementation agnostic EPConfig to initialize the Op. And a fused op implementation in future doesnt need to depend on NCCL EP specific EPbuffer object.
    • CPU overhead optimization: Originally dispatch input was being quantized outside of the dispatch autograd function and being passed. This required quantization to produce MXFP8Tensor instead of MXFP8TensorStorage. Now the quantization is moved inside of autograd function allowing us to set quantizer.internal=False, and MXFP8TensorStorage as input to the NCCL EP Dispatch in case of MXFP8 recipe.
    • Two APIs also added namely get_ep_group and get_drop_on_overflow. These methods give the configurations being setup during bootstrap of NCCL EP. And hence during runtime in the Sequential Ops we need these APIs to make sure the EPConfig is setup in a way that bootstrap demands it to be.
  • ep_reference.py

    • Reference Implementation for MOE Block from CUDNN. CUDNN use the same reference implementation for verifying the cudnn Megamoe kernel workflow
  • MOE Dispatch/Combine Op Addition

    • For Dispatch Forward and Combine Backward, quantizer roles are defined which lets the Op use MXFP8 Dispatch when using MXFP8 recipe within a te autocast.
    • BF16 Combine is not supported in NCCL EP currently and since using it can be tricky in terms of convergence we dont define quantizer roles for Dispatch Backward and Combine Forward. NOTE: Fused implementation from cudnn is coming soon with MXFP8 combine. However that ll be turned on based on an environment variable.
    • Most of the code relies on common implementation done in ep.py
    • Epbuffer is passed as a kwarg to both Dispatch and Combine Op
    • We do additional validation to make sure that recipe configured for EpBuffer matches the quantizers configured in Op. Also additional validation done to make sure NCCL Ep buffer's configuration, user provided Dispatch_input and topk_weights/topk_idxs's shapes are in sync.
  • NOTE: zero copy with NCCL EP is disallowed in the ops currently to avoid the complexity of passing additional symmetric buffers to the Ops

Fixes # (issue)

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

Please list the changes introduced in this PR:

  • Change A
  • Change B

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@greptile-apps

greptile-apps Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[High risk] Adds expert-parallel dispatch and combine operations to the framework.

The PR appears safe to merge based on this re-review.

Summary

The PR adds NCCL expert-parallel dispatch and combine as basic operations, shares their implementation with the existing EP APIs, and adds a PyTorch MoE reference and distributed integration tests. The post-review merge introduces no accepted new finding.

Reviews (17) · Last reviewed commit: "Merge branch 'main' into nccl_ep_ops"

@vthumbe1503

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch

@vthumbe1503

Copy link
Copy Markdown
Collaborator Author

/te-ci L1 pytorch

1 similar comment
@vthumbe1503

Copy link
Copy Markdown
Collaborator Author

/te-ci L1 pytorch

@vthumbe1503
vthumbe1503 requested a review from ptrendx September 11, 2026 15:47
self.tokens_per_expert = torch.empty(
self.num_local_experts, dtype=torch.int64, device=device
)
self.num_local_tokens = self.max_tokens_per_rank

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reminder for myself to set + test self.num_local_tokens = input.shape[1].

Comment thread transformer_engine/pytorch/ep.py Resolved
Comment thread transformer_engine/pytorch/ep.py Resolved
Comment thread transformer_engine/pytorch/ep.py Resolved
Comment thread transformer_engine/pytorch/ep.py Resolved
@vthumbe1503

Copy link
Copy Markdown
Collaborator Author

/te-ci L1 pytorch

@timmoon10 timmoon10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we can decouple the EP comm dtype from the te.autocast recipe. The EP comm dtype is determined based on how EpBuffer is configured, and there's no reason that needs to match the GEMM dtype. Why not have MXFP8 GEMMs and BF16 EP comms? Or why not have NVFP4 GEMMs and MXFP8 EP comms (you need to requantize MXFP8 to NVFP4, but the plain MXFP8 path also requires requantizing the column-wise data).



@dataclass(frozen=True, slots=True)
class EpConfig:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is included in the APIs for the dispatch and combine ops, so we should make sure this only configures the math of EP communication. There should be no NCCL EP logic.

@timmoon10 timmoon10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We're seeing a DCO failure. It might be easier to flatten the branch. I've made my own branch at https://github.com/timmoon10/TransformerEngine/tree/tmoon/nccl_ep_ops with some other cleanups.

@timmoon10 timmoon10 removed the 2.20 label Sep 22, 2026
Co-authored-by: Tim Moon <4406448+timmoon10@users.noreply.github.com>
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
Comment thread tests/pytorch/distributed/run_ep.py Resolved
@greptile-apps

This comment has been minimized.

Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
@vthumbe1503

Copy link
Copy Markdown
Collaborator Author

/te-ci L1 pytorch

@vthumbe1503

vthumbe1503 commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator Author

Thanks @timmoon10 and @0z5a for getting the PR ahead last week.
I have pushed a single commit having both your changes in there.

@0z5a could you please create a new PR for EP=1 case like you mentioned? It can go in after this PR merges onto main

@vthumbe1503

Copy link
Copy Markdown
Collaborator Author

/te-ci L1 pytorch

@timmoon10 timmoon10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@vthumbe1503
vthumbe1503 merged commit 5e8a899 into NVIDIA:main Oct 1, 2026
11 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants