Skip to content

Update - #3385

Closed
acisseJZhong wants to merge 3 commits into
gh/acisseJZhong/14/basefrom
gh/acisseJZhong/14/head
Closed

Update#3385
acisseJZhong wants to merge 3 commits into
gh/acisseJZhong/14/basefrom
gh/acisseJZhong/14/head

Conversation

@acisseJZhong

@acisseJZhong acisseJZhong commented May 17, 2026 •

Copy link
Copy Markdown
Contributor

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
acisseJZhong added a commit that referenced this pull request May 20, 2026
… experts (#3386)

**Copying major part from @fegin's PR
#3160

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* #3389
* __->__ #3386
* #3385

<img width="463" height="665" alt="image"
src="https://github.com/user-attachments/assets/39a2e880-62da-4e20-ab7c-0f36fd0bccc5"
/>


https://excalidraw.com/#json=2abKr0m2s26fc6lyoF9Qq,MqMzUIoXWYJIfckHNOB7Sw

## Summary

Refactor MoE parallelization to establish clean DTensor boundaries:
- **Router**: gate weights Replicate, input all-gathered to Replicate
(via `NoParallel`), compute on DTensor, output converted to local via
`to_local(grad_placements=Partial)`
- **Shared experts**: weights TP-sharded (via
`ColwiseParallel`/`RowwiseParallel`), input all-gathered to Replicate
inside `ColwiseParallel`, compute on DTensor, output local via
`to_local` (Partial placement, `use_local_output=True`)
- **Routed experts**: input converted to local via `to_local` at
`GroupedExperts` boundary, EP-partitioned weights on local tensors,
dispatch/compute/combine all local

Key changes:
- Move `shared_experts` computation from inside
`TokenDispatcher.combine()` to `MoE.forward()`
- Move `to_local` from `MoE.forward()` into `GroupedExperts.forward()`,
with proper `grad_placements` based on placement type (Shard vs
Replicate)
- Replace `ColwiseParallelWithGradPlacement` with standard
`ColwiseParallel` using `input_layouts`
- Remove `shared_experts` parameter from all token dispatchers, replace
with `initial_output` buffer
- For EP+SP, keep `Shard(1)` as desired MoE input layout (avoid
redundant all-gather at MoE boundary); for TP-only, keep `Replicate()`
(required for TP-sharded expert weight reduction)

### Trade-offs
- Removes async overlap between `shared_experts` and the all-to-all
combine. Previously `shared_experts` ran while the combine A2A was in
flight on the NCCL stream. A follow-up can restore overlap using CUDA
streams.

## Test plan
single run
```
NCCL_NVLS_ENABLE=0 NGPU=4 MODULE='llama4' CONFIG='llama4_debugmodel' ./run_train.sh \
    --parallelism.pipeline_parallel_degree 1 \
    --parallelism.data_parallel_shard_degree 2 \
    --parallelism.tensor_parallel_degree 2 \
    --parallelism.expert_parallel_degree 2 \
    --debug.deterministic --debug.seed=42
```
Loss exactly match for dp4tp1ep1. 
```
NCCL_NVLS_ENABLE=0 python scripts/loss_compare.py . main --baseline-module='llama4' --baseline-config='llama4_debugmodel' --baseline-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 4 --parallelism.tensor_parallel_degree 1 --
parallelism.expert_parallel_degree 1 --parallelism.no-ena
ble_sequence_parallel" --baseline-ngpus=4 --test-module='
llama4' --test-config='llama4_debugmodel' --test-options=
"--parallelism.pipeline_parallel_degree 1 --parallelism.d
ata_parallel_shard_degree 4 --parallelism.tensor_parallel
_degree 1 --parallelism.expert_parallel_degree 1 --parall
elism.no-enable_sequence_parallel" --test-ngpus=4 --asser
t-equal --no-seed-checkpoint
```

For other settings, loss is expected to diverge compared to main due to
different reduction pattern, and shared expert computation changes
place.

DP2TP2EP2, dense part default SP2
<img width="1097" height="457" alt="image"
src="https://github.com/user-attachments/assets/942428c4-e08e-4618-8b65-7c14eed39c81"
/>

DP2TP2EP4, dense part SP off
<img width="757" height="357" alt="image"
src="https://github.com/user-attachments/assets/07f6d9da-0b09-460a-b38c-ff64bd498c2e"
/>

tested deepep with both SP on and off
<img width="784" height="314" alt="image"
src="https://github.com/user-attachments/assets/ab304814-54af-4e42-8f9e-f14338b92ed0"
/>
acisseJZhong added a commit that referenced this pull request May 21, 2026
…3389)

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* __->__ #3389
* #3385

## Summary
MoE cleanup from #2956:

- **Extract `local_reorder` helper** on `LocalTokenDispatcher`:
deduplicates the histc/argsort/score-weighting block between
`LocalTokenDispatcher.dispatch()` and
`AllToAllTokenDispatcher.dispatch()`
- **Split `DeepEPTokenDispatcher` into `DeepEPTokenDispatcher` +
`HybridEPTokenDispatcher`**: eliminates `comm_backend` string branching;
config fields (`non_blocking_capacity_factor`, `pad_multiple`) are now
class-specific. Callsites use `isinstance` instead of `getattr(...,
"comm_backend", ...)`
- **Fix DeepEP CUDA stream race** (#3413): added
`torch.cuda.synchronize()` before `get_dispatch_layout` in
`dispatch_tokens()` to ensure `masked_fill` results are visible on
DeepEP's comm stream. Without this, `get_dispatch_layout` reads stale
(pre-masked) expert indices, causing `num_tokens_per_expert` to
overcount and `_grouped_mm` to assert.

## Test plan
Test loss comparions 
```
NCCL_NVLS_ENABLE=0 python scripts/loss_compare.py . main --baseline-module='llama4' --baseline-config='llama4_debugmodel_ep' --baseline-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 2 --parallelism.no-enable_sequence_parallel" --baseline-ngpus=4 --test-module='llama4' --test-config='llama4_debugmodel_ep' --test-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 2 --parallelism.no-enable_sequence_parallel" --test-ngpus=4 --assert-equal --no-seed-checkpoint
```
felipemello1 pushed a commit to felipemello1/torchtitan that referenced this pull request May 22, 2026
…ytorch#3389)

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* __->__ pytorch#3389
* pytorch#3385

## Summary
MoE cleanup from pytorch#2956:

- **Extract `local_reorder` helper** on `LocalTokenDispatcher`:
deduplicates the histc/argsort/score-weighting block between
`LocalTokenDispatcher.dispatch()` and
`AllToAllTokenDispatcher.dispatch()`
- **Split `DeepEPTokenDispatcher` into `DeepEPTokenDispatcher` +
`HybridEPTokenDispatcher`**: eliminates `comm_backend` string branching;
config fields (`non_blocking_capacity_factor`, `pad_multiple`) are now
class-specific. Callsites use `isinstance` instead of `getattr(...,
"comm_backend", ...)`
- **Fix DeepEP CUDA stream race** (pytorch#3413): added
`torch.cuda.synchronize()` before `get_dispatch_layout` in
`dispatch_tokens()` to ensure `masked_fill` results are visible on
DeepEP's comm stream. Without this, `get_dispatch_layout` reads stale
(pre-masked) expert indices, causing `num_tokens_per_expert` to
overcount and `_grouped_mm` to assert.

## Test plan
Test loss comparions 
```
NCCL_NVLS_ENABLE=0 python scripts/loss_compare.py . main --baseline-module='llama4' --baseline-config='llama4_debugmodel_ep' --baseline-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 2 --parallelism.no-enable_sequence_parallel" --baseline-ngpus=4 --test-module='llama4' --test-config='llama4_debugmodel_ep' --test-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 2 --parallelism.no-enable_sequence_parallel" --test-ngpus=4 --assert-equal --no-seed-checkpoint
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/rl 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.

1 participant