Skip to content

[moe] Overlap shared experts with the routed combine - #5937

Open
MissingPompeii wants to merge 1 commit into
pytorch:mainfrom
MissingPompeii:shared-expert-overlap
Open

MissingPompeii wants to merge 1 commit into
pytorch:mainfrom
MissingPompeii:shared-expert-overlap

Conversation

@MissingPompeii

Copy link
Copy Markdown

Summary (human-written)

Enable shared experts overlapped with routed experts' MoE combine.

Why

Shared experts execute after the routed combine returns. For EP dispatch the
combine is a communication phase; the shared-expert GEMMs have no dependency on
it, so the device sits idle for its duration.

Today (AllToAll/DeepEP, with shared experts):

routed experts -> routed combine (main stream) -> shared experts -> add

With combine_ready_event:

routed experts -> combine starts (event) -> routed combine (main stream) -> event wait   -> add
                            \                             -> shared experts (side stream)                             /

There is no way for the MoE to start shared experts at the routed-compute /
combine boundary. This adds that handoff as an optional dispatcher publisher.

What changed

  • torchtitan/models/common/moe/token_dispatcher.py: CombineReadyEvent
    (record/take of one torch.Event), optional
    BaseEPTokenDispatcher.Config.combine_ready_event: CombineReadyEvent.Config | None = None, and recording at the start of
    combine in AllToAllTokenDispatcher, TorchAOTokenDispatcher (EP=1 path),
    DeepEPTokenDispatcher, and HybridEPTokenDispatcher. LocalTokenDispatcher
    does not publish.
  • torchtitan/models/common/moe/moe.py: MoE keeps the publisher, runs shared
    experts on a device side stream after the routed experts, and waits before
    adding. The default path (no publisher) is unchanged. There is no
    _forward_split or input fork: DenseFeedForward uses one w13 projection,
    so there is no gate/up gradient join to move.
  • Config validation: MoE.Config rejects a publisher without shared_experts;
    DeepEPTokenDispatcher.Config rejects combine_ready_event together with
    cuda_graph_compatible=True.
  • torchtitan/models/common/moe/README.md: usage, timing diagram, eager/graph
    limitations, and the TokenDispatcherTransform caveat.
  • tests/unit_tests/cpu/distributed/test_expert_parallel.py: opt-in wiring and
    the DeepEP graph conflict.

Alternatives considered

  • MoE-side optional component plus dispatcher begin/take methods: semantically
    closer to the MoE, but splits the protocol in two and adds per-forward state.
  • An enable_shared_overlap boolean: upstream config rules require optional
    components as X.Config | None and no new enable_* booleans.

User-facing changes

Before After
Shared experts always run after the routed combine combine_ready_event=CombineReadyEvent.Config() runs them on a side stream from combine start
Dispatcher config New optional combine_ready_event field, default None; no behavior change

Testing

Run here: ast.parse on the three touched Python files, git diff --check,
added-line ASCII and 120-column checks.

Not run here: pytest, pre-commit, pyrefly (no torch/toolchain). New CPU
test, when run: pytest tests/unit_tests/cpu/distributed/test_expert_parallel.py
covers the opt-in wiring and the cuda_graph_compatible conflict.

GPU validation still needed: AllToAll/DeepEP/HybridEP paths, compile and graph
trainer, batch-invariant, activation remat, PP and multiple microbatches.

Known issues / follow-ups

  • Overlap is eager-only: CombineReadyEvent.record raises under torch.compile
    and non-strict tracing. Strict torch.export and accelerator graph capture are
    not covered; recipes must keep the option off.
  • The side stream is cached per device and shared by all MoE layers; correctness
    relies on wait_event/wait_stream, but layers and microbatches serialize
    their shared-expert work.
  • TokenDispatcherTransform rebuilds the dispatcher config from structural
    fields; pass combine_ready_event through its kwargs.
  • Batch-invariant mode and activation remat are unverified.

Add an optional CombineReadyEvent publisher to EP token dispatchers and run shared experts on a side stream from the combine start. Default off; no input fork is needed because DenseFeedForward uses one w13 projection.
@meta-cla

meta-cla Bot commented Nov 7, 2026

Copy link
Copy Markdown

Hi @MissingPompeii!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@pytorch-bot

pytorch-bot Bot commented Nov 7, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/rl
  • ciflow/fake-pg

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

1 similar comment
@pytorch-bot

pytorch-bot Bot commented Nov 7, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/rl
  • ciflow/fake-pg

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 7, 2026
@meta-cla

meta-cla Bot commented Nov 7, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

This branch has not been deployed

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

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants