Skip to content

[WIP][rl] DeepEP Low Latency mode for cudagraphable MoE - #3799

Closed
wwwjn wants to merge 1 commit into
mainfrom
deepep-ll-cudagraph
Closed

wwwjn wants to merge 1 commit into
mainfrom
deepep-ll-cudagraph

Conversation

@wwwjn

@wwwjn wwwjn commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

NOTE: within the combine / dispatch function, LL and HT version have different code path which is hard to be merged.

  • Currently DeepEP Low Latency mode doesn't work with per-layer compile (AoT eager). Error describe below.

When compile ON, error message

The failure

TypeError: 'DeviceMesh' object cannot be interpreted as an integer
at DTensor.tensor_unflatten (torch/distributed/tensor/_api.py:447)
<- aot_autograd -> moe.py forward, at static cudagraph capture

Root cause

A DTensor flattens to inner tensors {_local_tensor: Tensor, device_mesh: DeviceMesh}. When a
torch-compiled (aot_eager) MoE forward outputs a DTensor with a symbolic (dynamic) dim, the AOT
subclass codegen (_codegen_wrap_subclass / _build_tuple in subclass_codegen.py) mis-assembles
the output's outer_size tuple and binds the DeviceMesh object into a size slot (probe showed
outer_size=['int','Tensor','int'] with the mesh leaking in). Reconstruction then tries to use
that DeviceMesh as an integer dimension → the error. It fires at static cudagraph capture.

Why DeepEP-LL specifically triggers it

DeepEP-LL's dispatch/combine comm is not dynamo-traceable (no register_fake), so the compiled
MoE forward graph-breaks at the comm. That break + a symbolic output dim + in-graph static
capture + per-layer compile is what exercises the buggy codegen path.

Contrast: HybridEP's comm does register fake impls → no graph break → it dodges this bug (that's
why HybridEP+compile captures clean). So this is a DeepEP-LL-specific interaction, not a
general compile problem.

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

@tianyu-l tianyu-l 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.

Thanks a lot. Looks reasonable. @elfiegg @syed-ahmed could you take a look?


Regarding

DeepEP-LL's dispatch/combine comm is not dynamo-traceable (no register_fake), so the compiled MoE forward graph-breaks at the comm.

Just ask agents to register_fake, using HybridEP as example.

# happen before apply_ac).
from torchtitan.distributed.deepep import deepep # noqa: F401

def wire_meshes(self, *, ep_mesh=None, tp_mesh=None) -> None:

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.

We will delete wire_meshes soon, as itself is a workaround.

I think the proper way is to follow MinimalAsyncEP and create a init_buffer function, to refactor DeepEP and HybridEP.
https://github.com/pytorch/torchtitan/blob/main/torchtitan/models/common/token_dispatcher.py#L951

The example itself could be improved, e.g. https://github.com/pytorch/torchtitan/blob/main/torchtitan/distributed/minimal_async_ep/api.py#L138-L145 should be done at config construction time, not in update_from_config. Another followup is to support TP for minimal_async_ep and completely remove https://github.com/pytorch/torchtitan/blob/main/torchtitan/distributed/minimal_async_ep/api.py#L70

@elfiegg

elfiegg commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

I'm wondering if we have considered refactoring to V2 DeepEP where high-throughput and low-latency are unified under a single ElasticBuffer interface, so are dispatch / combine APIs? By doing so, we don't need to call different APIs, create different States for two modes, also the new interface design looks very compatible with the legacy high-throughput mode: https://github.com/deepseek-ai/DeepEP#buffer-initialization

wwwjn added a commit that referenced this pull request Jun 29, 2026
Start iterating per comments here:
#3799 (comment)
(Leave the DeepEP-LL untouched)

 https://github.com/deepseek-ai/DeepEP#buffer-initialization

## Compact vs. expand mode

- DeepEP V1 High-Throughput mode = V2 Compact mode (do_expand=False,
cudagraph off, training): recv_x is deduplicated + dynamically sized —
one row per unique received token. Needs a CPU sync, is backward-able,
not cudagraph-able. Must be gathered (_permute) into expert order before
the grouped GEMM.
- DeepEP V1 Low-latency mode = V2 Expand (do_expand=True, cudagraph on,
inference): recv_x is a static slab — one slot per (token, expert),
already expert-grouped. No CPU sync, not backward-able, is
cudagraph-able.

## `num_max_tokens_per_rank` meaning

- Buffer allocation: sizes the single static ElasticBuffer used by both
modes. Each local expert gets capacity num_max_tokens_per_rank ×
num_ranks (every EP rank can send up to num_max to any one expert).
Required regardless of cudagraph.
- Dispatch: does not cap/shrink your actual send (your batch does that).
It's the upper bound the buffer is sized for; exceeding it = buffer
overflow (misconfiguration, not a feature).
- Combine: reduces the per-(token,expert) expert outputs back to tokens
via the handle. In expand mode the unused padding slots are discarded by
the handle (they never contribute), so combine doesn't add garbage and
doesn't drop legitimate tokens.

## Token dropping

- Compact: unconditionally dropless in its output (allocated to the
exact received count). The only failure mode is dispatcher needs to
sending more tokens exceeding the buffer's num_max_tokens_per_rank
reservation → overflow.
- Expand: dropless iff num_max_tokens_per_rank ≥ actual max tokens/rank
— the static slab's num_max × num_ranks per-expert capacity covers the
worst case (a rank's whole batch routed to one expert). If a rank
dispatches more than num_max, the excess overflows the slab → dropped.

## Test  (TBA)
- Pre-training numerics parity w/ DeePEP <img width="861" height="247"
alt="Screenshot 2026-06-27 at 4 47 41 PM"
src="https://github.com/user-attachments/assets/cca8f3e6-4885-4eb6-8b2a-5e448eeaa3f9"
/>
    - baseline: AllToAllTokenDispatcher
- Loss comparison w/ current main
- Inference cudagraph-able
   - MoE expand mode is cudagraph captured.
- RL end to end run: (trainer DeepEP no cudagraphable, and generator
cudagraph-able)
- 30B multi-node (16GPU) run: verified. DeepEP v2 (EP16, 2 nodes) +
FULL_AND_PIECEWISE cudagraph + generation is conherent correct
end-to-end
- Composability with Activation checkpointing:
    - tested with FullAC, recomputing DeepEP to save memory, run succeed
@wwwjn

wwwjn commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Go with DeepEP-v2 #3808

@wwwjn wwwjn closed this Jul 6, 2026
jemitche1 pushed a commit to jemitche1/torchtitan that referenced this pull request Jul 15, 2026
…h#3808)

Start iterating per comments here:
pytorch#3799 (comment)
(Leave the DeepEP-LL untouched)

 https://github.com/deepseek-ai/DeepEP#buffer-initialization

## Compact vs. expand mode

- DeepEP V1 High-Throughput mode = V2 Compact mode (do_expand=False,
cudagraph off, training): recv_x is deduplicated + dynamically sized —
one row per unique received token. Needs a CPU sync, is backward-able,
not cudagraph-able. Must be gathered (_permute) into expert order before
the grouped GEMM.
- DeepEP V1 Low-latency mode = V2 Expand (do_expand=True, cudagraph on,
inference): recv_x is a static slab — one slot per (token, expert),
already expert-grouped. No CPU sync, not backward-able, is
cudagraph-able.

## `num_max_tokens_per_rank` meaning

- Buffer allocation: sizes the single static ElasticBuffer used by both
modes. Each local expert gets capacity num_max_tokens_per_rank ×
num_ranks (every EP rank can send up to num_max to any one expert).
Required regardless of cudagraph.
- Dispatch: does not cap/shrink your actual send (your batch does that).
It's the upper bound the buffer is sized for; exceeding it = buffer
overflow (misconfiguration, not a feature).
- Combine: reduces the per-(token,expert) expert outputs back to tokens
via the handle. In expand mode the unused padding slots are discarded by
the handle (they never contribute), so combine doesn't add garbage and
doesn't drop legitimate tokens.

## Token dropping

- Compact: unconditionally dropless in its output (allocated to the
exact received count). The only failure mode is dispatcher needs to
sending more tokens exceeding the buffer's num_max_tokens_per_rank
reservation → overflow.
- Expand: dropless iff num_max_tokens_per_rank ≥ actual max tokens/rank
— the static slab's num_max × num_ranks per-expert capacity covers the
worst case (a rank's whole batch routed to one expert). If a rank
dispatches more than num_max, the excess overflows the slab → dropped.

## Test  (TBA)
- Pre-training numerics parity w/ DeePEP <img width="861" height="247"
alt="Screenshot 2026-06-27 at 4 47 41 PM"
src="https://github.com/user-attachments/assets/cca8f3e6-4885-4eb6-8b2a-5e448eeaa3f9"
/>
    - baseline: AllToAllTokenDispatcher
- Loss comparison w/ current main
- Inference cudagraph-able
   - MoE expand mode is cudagraph captured.
- RL end to end run: (trainer DeepEP no cudagraphable, and generator
cudagraph-able)
- 30B multi-node (16GPU) run: verified. DeepEP v2 (EP16, 2 nodes) +
FULL_AND_PIECEWISE cudagraph + generation is conherent correct
end-to-end
- Composability with Activation checkpointing:
    - tested with FullAC, recomputing DeepEP to save memory, run succeed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu 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.

3 participants