Skip to content

Default TorchTitan RL to spmd_types - #4110

Closed
wwwjn wants to merge 5 commits into
mainfrom
fix/rl-moe-output-redistribution
Closed

wwwjn wants to merge 5 commits into
mainfrom
fix/rl-moe-output-redistribution

Conversation

@wwwjn

@wwwjn wwwjn commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Why

RL still used the DTensor backend, where the MoE routed-expert Shard(1) -> Partial output redistribution was rejected. The spmd_types backend supports that redistribution directly and is the backend RL is migrating to.

What changed

  • explicitly select spmd_types for trainer and generator parallelism in every RL example config
  • keep the generic InferenceParallelismConfig default unchanged as default
  • update the bitwise parity harness to run trainer forwards in the SPMD mesh context and synchronize local SPMD parameter shards
  • remove the temporary DTensor redistribution workaround from the earlier PR revisions

Local validation

  • all 19 public RL configs select spmd_types for trainer and generator
  • pytest -q torchtitan/experiments/rl/tests: 218 passed, 14 skipped
  • 4-GPU test_vllm_uneven_decode_tp_padding: passed on all ranks
  • 2-GPU TestBitwiseParityVarlen: 3 passed on all ranks, including exact trainer-vLLM prefill parity
  • 8-GPU rl_grpo_moe_debug_tp4_ep4: completed all 5 training steps
  • pre-commit run --all-files: all hooks pass except the pre-existing Pyrefly error in torchtitan/distributed/deepep/hybridep.py because this nightly does not export CustomClassBase

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 11, 2026
@wwwjn
wwwjn requested a review from acisseJZhong August 11, 2026 15:23
@wwwjn wwwjn changed the title Fix MoE output layout without sequence parallelism Restore MoE Shard-to-Partial output conversion Aug 11, 2026
@wwwjn
wwwjn marked this pull request as draft August 11, 2026 15:49
@wwwjn

wwwjn commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

@acisseJZhong This is trying to fix the RL/ inference path failure for EP=True and SP=False, basically we hit the error in RL integration test failed while redistribute S(1) -> P for the output of routed_experts. Could you take this over and fix it?

@acisseJZhong

acisseJZhong commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

@wwwjn I landed pytorch/pytorch#191828 we need to wait for the nightly to catch up. Sorry for breaking the RL integration test(I should wait for nightly to catch up and then land the titan changes). In the meantime let me change RL integration test to use spmd types instead, we need to migrate to it anyways.

@wwwjn

wwwjn commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

@wwwjn I landed pytorch/pytorch#191828 we need to wait for the nightly to catch up. Sorry for breaking the RL integration test(I should wait for nightly to catch up and then land the titan changes). In the meantime let me change RL integration test to use spmd types instead, we need to migrate to it anyways.

Yes I just figured out it's because RL is keep using DTensor backend and spmd_types works and mentioned in #3996 descriptions. Let me update RL to use spmd_types.

@acisseJZhong

Copy link
Copy Markdown
Contributor

I opened #4112 to migrate all the RL tests to spmd types.

@wwwjn wwwjn changed the title Restore MoE Shard-to-Partial output conversion Default TorchTitan RL to spmd_types Aug 11, 2026
@wwwjn

wwwjn commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

I opened #4112 to migrate all the RL tests to spmd types.

Thank you, I think we need to migrate not only tests, but all the configs to spmd_types. Would you mind me taking over it ? I want to check how inference + spmd_types works

@acisseJZhong

Copy link
Copy Markdown
Contributor

sure, I will leave #4112 as it is. I think spmd types is not working for some unit test like test_bitwise_parity.py you might want to fix.

wwwjn added a commit that referenced this pull request Aug 11, 2026
…#4089)

## Summary

Enable MinimalAsyncEP to compose with context parallelism, tensor
parallelism, and pipeline parallelism.

This PR does not add backend-specific CP, TP, or PP dispatch algorithms.
The required behavior comes from the common MoE and mesh contracts:

- #3996 makes `RoutedExperts` operate on a local sequence shard and
return that local layout.
- #4080 physically pads the sequence before routing, so every rank
dispatches the same number of materialized token rows.
- #3970 centralizes lifetime communication-buffer capacity inference for
persistent EP backends.
- The sparse mesh places PP outside EP, so every pipeline stage owns an
independent EP process group.

MinimalAsyncEP therefore continues to dispatch the local `x_TD` rows
over its EP group and returns a combined tensor with the same local
token shape. The common MoE sharding path handles CP and TP, while the
pipeline runtime invokes the stage-local MinimalAsyncEP instances.

## Buffer capacity

MinimalAsyncEP now uses the shared lifetime capacity derivation:

```python
num_token_shards = context_parallel_degree * tensor_parallel_degree
if seq_len % num_token_shards != 0:
    raise ValueError(...)
num_max_tokens_per_rank = local_batch_size * seq_len // num_token_shards
```

CP and TP reduce the local token capacity because they shard the
sequence dimension before MoE. The shared configuration requires
`seq_len` to divide evenly across `CP * TP`; users must pad the
configured sequence length when it does not. PP does not enter this
calculation because it partitions model layers, not the token axis.

This replaces the backend-local `local_batch_size * seq_len`
calculation, which overallocated once CP or TP was enabled. The shared
setup also fills `hidden_dim`, preserves a user-specified capacity when
it is large enough, and rejects an undersized capacity. MinimalAsyncEP
keeps only its backend-specific dtype setup.

## Changes

- Remove the MinimalAsyncEP configuration restrictions for CP, TP, and
PP.
- Include MinimalAsyncEP in shared EP buffer-capacity configuration.
- Add an H100 integration configuration for `DP-shard=2, CP=2, TP=2,
EP=8`.
- Keep the test on `spmd_types` until the H100 suite migrates from the
default DTensor backend.

## Testing

### Tensor parallelism

Four H100s, 10 training steps:

```text
DP-shard=2, TP=2, EP=4
spmd_backend=spmd_types
compile disabled
full activation checkpointing
```

Loss decreased from `8.06603` to `3.86079`; final `grad_norm` was
`1.6618`.

### Context parallelism

Four H100s, 10 training steps:

```text
DP-shard=2, CP=2, EP=4
spmd_backend=spmd_types
compile disabled
full activation checkpointing
```

Loss decreased from `8.00828` to `3.87139`; final `grad_norm` was
`1.5916`.

### Pipeline parallelism

Four H100s with PyTorch `2.14.0.dev20260811+cu130`, two optimizer steps:

```text
DP-shard=2, PP=2, EP=2
1F1B with 8 microbatches
spmd_backend=spmd_types
compile disabled
full activation checkpointing
```

The run completed MinimalAsyncEP dispatch/combine, pipeline
forward/backward, and PP-wide gradient-norm reduction. Loss decreased
from `8.32359` to `6.38615`; `grad_norm` changed from `3.6094` to
`4.4162`.

### Numerical comparison against AllToAll

The standard AllToAll dispatcher and MinimalAsyncEP were run for 10
steps with the same `DP-shard=2, PP=2, EP=2` topology, fused expert
kernels, data, seed 42, and deterministic mode. Full-precision
TensorBoard metrics show a maximum relative loss difference of `4.55e-4`
and a maximum relative grad-norm difference of `2.60e-3`.

MinimalAsyncEP is not expected to be bitwise identical to AllToAll:
AllToAll rounds each score-weighted expert row to BF16 and then performs
deterministic scatter-add in expert order, while MinimalAsyncEP
accumulates the top-k rows in FP32 slot order and rounds once.

Two diagnostics verify that this difference is numerical rather than
token dropping:

- A temporary assertion reduced the active receive-row count across
every stage-local EP group and required it to equal `EP_size *
local_tokens * top_k`. It passed every forward and activation-recompute
dispatch across all 10 steps.
- Replacing only MinimalAsyncEP's top-k combine arithmetic with the
AllToAll arithmetic produced the exact same step-1 loss:
`8.1351776123046875` for both backends. Native MinimalAsyncEP produced
`8.1351785659790039`.

The exact eight-GPU `DP-shard=2, CP=2, TP=2, EP=8` CI configuration was
not run locally because the host has seven available H100s. CP and TP
were validated independently as listed above.

## Limitations

- MinimalAsyncEP requires the `spmd_types` backend. The default DTensor
path currently hits the same `Shard(1) -> Partial(sum)` limitation
tracked by #4110.
- MinimalAsyncEP requires full recomputation: full activation
checkpointing for eager training or full memory policy for GraphTrainer.
@wwwjn wwwjn closed this Aug 12, 2026
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.

2 participants