Skip to content

[muse_spark] Support DeepEP expert parallelism - #6037

Merged
fegin merged 1 commit into
mainfrom
fegin/stack/35
Nov 13, 2026
Merged

fegin merged 1 commit into
mainfrom
fegin/stack/35

Conversation

@fegin

@fegin fegin commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Stacked PRs:


[muse_spark] Support DeepEP expert parallelism

Summary (human-written)

As title. Enabling DeepEP for Muse Spark is trivial, performance is not
verified though.

Why

MuseSparkMoE.Config rejects DeepEPTokenDispatcher, but nothing in Muse Spark needs that. The shared MoE code already supports DeepEP with an output_postprocess:

  • In training, DeepEP uses its compact, host-synced path. The routed rows match the received token counts, so there are no rows past the last expert offset, and the HybridEP mask from [muse_spark] Support HybridEP expert parallelism #6032 is not needed.
  • RoutedExperts.forward already applies the DeepEP routing scores after the postprocess, because Muse Spark's per-expert RMSNorm is nonlinear and must run first.

DeepEP is the EP backend that search_r1 uses for both its trainer and its CUDA-graphed generator (expand layout), so this should also unblock Muse Spark RL on Hopper. I have not run the generator's expand layout with Muse Spark.

What changed

  • MuseSparkMoE.Config accepts DeepEPTokenDispatcher. TorchAOTokenDispatcher stays rejected.
  • RoutedExperts.forward: comment at the HybridEP mask gate on why DeepEP needs no mask. Training uses the compact layout, which has no rows past the last offset. The expand layout runs only without grad: dispatch_tokens forces the compact layout when grad is enabled, and the DeepEP kernel forbids backward in expand mode.
  • README: list DeepEP as supported, and add it to the CUDA graphs row: training needs graphs disabled. Note that DeepEP's combine kernel needs the MoE compressed dimension to be a multiple of 256. Add a TODO for the config-time check. The production flavors use 4096. debugmodel uses 128, so it cannot run with DeepEP.

Alternatives considered

  • Add a muse_spark_debugmodel_deepep recipe and an integration test: debugmodel cannot run DeepEP (compressed dimension 128), and changing the debug flavor would change every Muse Spark debug run and golden loss.
  • Validate the hidden size in DeepEPTokenDispatcher.Config: today a bad size fails at the first forward with DeepEP's static assert ("Invalid hidden"). A config-time check belongs to DeepEP in shared code, so it is left for a separate PR.

Numerics

Muse Spark debug model with compressed dimension 256 (a local config change, see above), FSDP2 + TP2 (SP) + EP2 on 4x H100, seed 42, deterministic, 20 steps:

Step AllToAll DeepEP
1 7.75888 7.75888
2 6.82995 6.83072
10 4.81295 4.81803
20 4.40471 4.39651

Average over 20 steps: 5.18424 vs 5.18254. Step 1 matches. Later steps differ by the combine kernels' summation order, with no consistent sign.

One-off unit check, the AllToAll comparison from the HybridEP tests run with DeepEP and an RMSNorm postprocess (2x H100): loss, input grad, and router, W13, W2, and norm weight grads match AllToAll within relative error 4.7e-3, below the 1.56e-2 bound. Loss and the expert and norm weight grads are bitwise equal.

Performance

Not reported. DeepEP's expected benefit is multi-node communication, and I only have single-node H100 runs, which would not show it.

Testing

  • pytest tests/unit_tests/gpu/distributed/test_deepep_activation_checkpoint.py -k DeepEP with DeepEP v2 at the CI pin 01dc3aa: 2 passed. These tests already run with an RMSNorm postprocess and check the score order against a reference.
  • Muse Spark debugmodel with compressed_dim 256, FSDP2 + Ulysses CP2 + EP4 (4x H100), seed 42, deterministic, 20 steps. Loss at steps 1 / 10 / 20: DeepEP 7.74766 / 4.80887 / 4.40117, AllToAll 7.74766 / 4.80533 / 4.39565.
  • No new test. The existing DeepEP tests cover the postprocess path, and the unit check above was a one-off run.
  • pytest tests/unit_tests/cpu/models/muse_spark tests/unit_tests/cpu/recipes/test_muse_spark.py: 40 passed, 3 xfailed.

@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 11, 2026
fegin added a commit that referenced this pull request Nov 11, 2026
## Summary (human-written)
<!-- Write this yourself, not with an AI tool. 2-5 sentences: the problem, why
     this approach, and what you checked yourself. -->

## Why
`MuseSparkMoE.Config` rejects `DeepEPTokenDispatcher`, but nothing in Muse Spark needs that. The shared MoE code already supports DeepEP with an `output_postprocess`:
- In training, DeepEP uses its compact, host-synced path. The routed rows match the received token counts, so there are no rows past the last expert offset, and the HybridEP mask from #6032 is not needed.
- `RoutedExperts.forward` already applies the DeepEP routing scores after the postprocess, because Muse Spark's per-expert RMSNorm is nonlinear and must run first.

DeepEP is the EP backend that `search_r1` uses for both its trainer and its CUDA-graphed generator (expand layout), so this also unblocks Muse Spark RL on Hopper.

## What changed
- `MuseSparkMoE.Config` accepts `DeepEPTokenDispatcher`. `TorchAOTokenDispatcher` stays rejected.
- README: list DeepEP as supported. Note that training needs CUDA graphs disabled, and that DeepEP's combine kernel needs the MoE compressed dimension to be a multiple of 256. The production flavors use 4096. `debugmodel` uses 128, so it cannot run with DeepEP.

## Alternatives considered
- Add a `muse_spark_debugmodel_deepep` recipe and an integration test: `debugmodel` cannot run DeepEP (compressed dimension 128), and changing the debug flavor would change every Muse Spark debug run and golden loss.
- Validate the hidden size in `DeepEPTokenDispatcher.Config`: today a bad size fails at the first forward with DeepEP's static assert ("Invalid hidden"). A config-time check belongs to DeepEP in shared code, so it is left for a separate PR.

## Numerics
Muse Spark debug model with compressed dimension 256 (a local config change, see above), FSDP2 + TP2 (SP) + EP2 on 4x H100, seed 42, deterministic, 20 steps:

| Step | AllToAll | DeepEP |
| --- | --- | --- |
| 1 | 7.75888 | 7.75888 |
| 2 | 6.82995 | 6.83072 |
| 10 | 4.81295 | 4.81803 |
| 20 | 4.40471 | 4.39651 |

Average over 20 steps: 5.18424 vs 5.18254. Step 1 matches. Later steps differ by the combine kernels' summation order, with no consistent sign.

Unit check, the RMSNorm postprocess setup from `test_matches_all_to_all_with_output_postprocess` with DeepEP instead of HybridEP (2x H100): loss, input grad, and router, W13, W2, and norm weight grads match AllToAll within relative error 4.7e-3, below the 1.56e-2 bound. Loss and the expert and norm weight grads are bitwise equal.

## Performance
Not reported. DeepEP's expected benefit is multi-node communication, and I only have single-node H100 runs, which would not show it.

## Testing
- `pytest tests/unit_tests/gpu/distributed/test_deepep_activation_checkpoint.py -k DeepEP` with DeepEP v2 at the CI pin 01dc3aa: 2 passed. These tests already run with an RMSNorm postprocess and check the score order against a reference.
- No new test. The existing DeepEP tests cover the postprocess path, and the unit check above was a one-off run.
- `pytest tests/unit_tests/cpu/models/muse_spark tests/unit_tests/cpu/recipes/test_muse_spark.py`: 40 passed, 3 xfailed.

stack-info: PR: #6037, branch: fegin/stack/35
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 11, 2026
@fegin

fegin commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

@claude review this PR

@claude

claude Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @fegin's task in 2m 49s —— View job


Approve, with small items to fix before merging: see the review.

@fegin
fegin marked this pull request as draft November 11, 2026 22:19
@fegin
fegin changed the base branch from fegin/stack/34 to main November 11, 2026 22:19
@pytorch-bot pytorch-bot Bot added ciflow/rl and removed ciflow/fake-pg Run 1-GPU Fake PG integration tests labels Nov 11, 2026
fegin added a commit that referenced this pull request Nov 11, 2026
## Summary (human-written)
As title. Enabling DeepEP for Muse Spark is trivial, performance is not
verified though.

## Why
`MuseSparkMoE.Config` rejects `DeepEPTokenDispatcher`, but nothing in Muse Spark needs that. The shared MoE code already supports DeepEP with an `output_postprocess`:
- In training, DeepEP uses its compact, host-synced path. The routed rows match the received token counts, so there are no rows past the last expert offset, and the HybridEP mask from #6032 is not needed.
- `RoutedExperts.forward` already applies the DeepEP routing scores after the postprocess, because Muse Spark's per-expert RMSNorm is nonlinear and must run first.

DeepEP is the EP backend that `search_r1` uses for both its trainer and its CUDA-graphed generator (expand layout), so this also unblocks Muse Spark RL on Hopper.

## What changed
- `MuseSparkMoE.Config` accepts `DeepEPTokenDispatcher`. `TorchAOTokenDispatcher` stays rejected.
- README: list DeepEP as supported. Note that training needs CUDA graphs disabled, and that DeepEP's combine kernel needs the MoE compressed dimension to be a multiple of 256. The production flavors use 4096. `debugmodel` uses 128, so it cannot run with DeepEP.

## Alternatives considered
- Add a `muse_spark_debugmodel_deepep` recipe and an integration test: `debugmodel` cannot run DeepEP (compressed dimension 128), and changing the debug flavor would change every Muse Spark debug run and golden loss.
- Validate the hidden size in `DeepEPTokenDispatcher.Config`: today a bad size fails at the first forward with DeepEP's static assert ("Invalid hidden"). A config-time check belongs to DeepEP in shared code, so it is left for a separate PR.

## Numerics
Muse Spark debug model with compressed dimension 256 (a local config change, see above), FSDP2 + TP2 (SP) + EP2 on 4x H100, seed 42, deterministic, 20 steps:

| Step | AllToAll | DeepEP |
| --- | --- | --- |
| 1 | 7.75888 | 7.75888 |
| 2 | 6.82995 | 6.83072 |
| 10 | 4.81295 | 4.81803 |
| 20 | 4.40471 | 4.39651 |

Average over 20 steps: 5.18424 vs 5.18254. Step 1 matches. Later steps differ by the combine kernels' summation order, with no consistent sign.

Unit check, the RMSNorm postprocess setup from `test_matches_all_to_all_with_output_postprocess` with DeepEP instead of HybridEP (2x H100): loss, input grad, and router, W13, W2, and norm weight grads match AllToAll within relative error 4.7e-3, below the 1.56e-2 bound. Loss and the expert and norm weight grads are bitwise equal.

## Performance
Not reported. DeepEP's expected benefit is multi-node communication, and I only have single-node H100 runs, which would not show it.

## Testing
- `pytest tests/unit_tests/gpu/distributed/test_deepep_activation_checkpoint.py -k DeepEP` with DeepEP v2 at the CI pin 01dc3aa: 2 passed. These tests already run with an RMSNorm postprocess and check the score order against a reference.
- No new test. The existing DeepEP tests cover the postprocess path, and the unit check above was a one-off run.
- `pytest tests/unit_tests/cpu/models/muse_spark tests/unit_tests/cpu/recipes/test_muse_spark.py`: 40 passed, 3 xfailed.

stack-info: PR: #6037, branch: fegin/stack/35
@fegin
fegin changed the base branch from main to fegin/stack/34 November 11, 2026 22:19
@fegin
fegin marked this pull request as ready for review November 11, 2026 22:20
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 11, 2026

@claude claude Bot 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.

Verdict: Approve -- please address the items below before merging. The shared RoutedExperts already handles DeepEP with an output postprocess, so Muse Spark only needs to stop rejecting DeepEP.

What this PR does. MuseSparkMoE.Config.__post_init__ now accepts DeepEPTokenDispatcher (TorchAOTokenDispatcher is still rejected). The README now lists DeepEP as supported and explains its limits: no training CUDA graphs, and the compressed dim must be a multiple of 256.

Example.

apply_transforms(muse_spark_config, [TokenDispatcherTransform(
    dispatcher=DeepEPTokenDispatcher, num_max_tokens_per_rank=N)])
# before: ValueError("Muse Spark MoE requires LocalTokenDispatcher, AllToAll..., or HybridEP...")
# after:  builds. hidden_dim = w13.in_features = compressed_dim (4096 in production).
#         RoutedExperts runs w2 without scores -> RMSNorm postprocess -> * scores_R -> combine
#         (models/common/moe/moe.py:172-209)

Must fix

  1. PR description: the "Summary (human-written)" section is empty. The one-line note above the template ("Enabling DeepEP for Muse Spark is trivial, performance is not verified though") reads like that summary. Please move it into the section.
  2. torchtitan/models/muse_spark/README.md:138-141: note that CP is not verified with DeepEP, and add a TODO for the config-time hidden-dim check (inline).

Should consider

  • torchtitan/models/muse_spark/README.md:109: add DeepEP to the CUDA graphs row of the table (inline, with a suggestion).
  • Description, "unblocks Muse Spark RL on Hopper": the generator uses the expand layout (cuda_graph_compatible=True), and nothing has run Muse Spark through it. The argument in "Why" that the HybridEP mask isn't needed covers only the training (compact) path. The expand layout has an unused tail past offsets_E[-1] (distributed/deepep/deepep.py:160-161), and the RMSNorm postprocess runs over it. That looks harmless: the generator runs under no_grad, and Qwen3's w2 tail already reaches combine the same way. Still, either run the generator once or reword the claim to "should unblock".
Details
  • Gate: the problem is real, since the old __post_init__ raised for DeepEP. The fix sits in the model folder and changes no shared code. #5708 (DeepEP training on the expand layout) is still open. It keeps do_cpu_sync=True for training, so the received rows stay exactly sized and the "no tail rows in training" argument still holds after it lands.
  • Training graphs: validate_model_training_compatibility already raises for DeepEP when accelerator graphs are on with EP > 1 (config/validation.py:109-125), so the README note matches what the code enforces.
  • Score order: with a postprocess, RoutedExperts.forward calls w2 without scores_R and multiplies by the scores after the postprocess (moe.py:172-209), which is correct for the nonlinear per-expert norm.
  • Buffer sizing: TokenDispatcherTransform sets hidden_dim from w13.in_features (config/transform/token_dispatcher.py:66-67), so DeepEP's buffer gets the compressed dim, not the model dim.
  • No new test: agreed. Debug flavor dim 128 can't run DeepEP, and test_deepep_activation_checkpoint.py already covers the postprocess path.

Not verified: the multi-GPU numerics, the generator expand path for Muse Spark, and DeepEP's multiple-of-256 hidden constraint. A maintainer still needs to review.

Comment thread torchtitan/models/muse_spark/README.md Outdated
@@ -108,7 +108,6 @@ Both production and debug flavors use MoE in every layer.
| Model compilation | Regional compilation |
| CUDA graphs | Text-only with Dist-MoE or HybridEP; multimodal support is a TorchTitan gap; standard all-to-all EP requires graphs to be disabled |

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.

This table row is what users read to check CUDA graph support, and it still doesn't mention DeepEP. DeepEP training also needs graphs disabled, since it takes the host-synced compact path. validate_model_training_compatibility (torchtitan/config/validation.py:109-125) already rejects it. Suggest adding it to the row:

Suggested change
| CUDA graphs | Text-only with Dist-MoE or HybridEP; multimodal support is a TorchTitan gap; standard all-to-all EP requires graphs to be disabled |
| CUDA graphs | Text-only with Dist-MoE or HybridEP; multimodal support is a TorchTitan gap; standard all-to-all EP and DeepEP training require graphs to be disabled |

Comment thread torchtitan/models/muse_spark/README.md Outdated
Comment on lines +138 to +141
DeepEP uses its compact, host-synced path in training, so CUDA graphs must be
disabled. Its combine kernel requires the MoE compressed dimension to be a
multiple of 256: the production flavors use 4096, but `debugmodel` uses 128 and
cannot run with DeepEP. DeepEP is verified with FSDP and with TP+EP.

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.

Two small doc gaps, compared with the HybridEP paragraph just above:

  1. CP isn't covered. The numerics run used FSDP2 + TP2 + EP2 with no CP, so add "CP is not verified with DeepEP." to match line 133-134.
  2. "debugmodel ... cannot run with DeepEP" is a known-unsupported configuration that is only documented. It fails at the first forward with DeepEP's "Invalid hidden" assert, not at config time. You already leave the config-time check for a follow-up. That's fine because the failure is loud, but please add a TODO here pointing at it so it doesn't get lost. The natural place for the check is DeepEPTokenDispatcher.Config.__post_init__ on hidden_dim, which TokenDispatcherTransform fills from w13.in_features, i.e. the compressed dim.

(Not verified: the multiple-of-256 requirement itself. I couldn't check it against DeepEP's kernel without a GPU build.)

fegin added a commit that referenced this pull request Nov 11, 2026
## Summary (human-written)
As title. Enabling DeepEP for Muse Spark is trivial, performance is not
verified though.

## Why
`MuseSparkMoE.Config` rejects `DeepEPTokenDispatcher`, but nothing in Muse Spark needs that. The shared MoE code already supports DeepEP with an `output_postprocess`:
- In training, DeepEP uses its compact, host-synced path. The routed rows match the received token counts, so there are no rows past the last expert offset, and the HybridEP mask from #6032 is not needed.
- `RoutedExperts.forward` already applies the DeepEP routing scores after the postprocess, because Muse Spark's per-expert RMSNorm is nonlinear and must run first.

DeepEP is the EP backend that `search_r1` uses for both its trainer and its CUDA-graphed generator (expand layout), so this also unblocks Muse Spark RL on Hopper.

## What changed
- `MuseSparkMoE.Config` accepts `DeepEPTokenDispatcher`. `TorchAOTokenDispatcher` stays rejected.
- README: list DeepEP as supported. Note that training needs CUDA graphs disabled, and that DeepEP's combine kernel needs the MoE compressed dimension to be a multiple of 256. The production flavors use 4096. `debugmodel` uses 128, so it cannot run with DeepEP.

## Alternatives considered
- Add a `muse_spark_debugmodel_deepep` recipe and an integration test: `debugmodel` cannot run DeepEP (compressed dimension 128), and changing the debug flavor would change every Muse Spark debug run and golden loss.
- Validate the hidden size in `DeepEPTokenDispatcher.Config`: today a bad size fails at the first forward with DeepEP's static assert ("Invalid hidden"). A config-time check belongs to DeepEP in shared code, so it is left for a separate PR.

## Numerics
Muse Spark debug model with compressed dimension 256 (a local config change, see above), FSDP2 + TP2 (SP) + EP2 on 4x H100, seed 42, deterministic, 20 steps:

| Step | AllToAll | DeepEP |
| --- | --- | --- |
| 1 | 7.75888 | 7.75888 |
| 2 | 6.82995 | 6.83072 |
| 10 | 4.81295 | 4.81803 |
| 20 | 4.40471 | 4.39651 |

Average over 20 steps: 5.18424 vs 5.18254. Step 1 matches. Later steps differ by the combine kernels' summation order, with no consistent sign.

Unit check, the RMSNorm postprocess setup from `test_matches_all_to_all_with_output_postprocess` with DeepEP instead of HybridEP (2x H100): loss, input grad, and router, W13, W2, and norm weight grads match AllToAll within relative error 4.7e-3, below the 1.56e-2 bound. Loss and the expert and norm weight grads are bitwise equal.

## Performance
Not reported. DeepEP's expected benefit is multi-node communication, and I only have single-node H100 runs, which would not show it.

## Testing
- `pytest tests/unit_tests/gpu/distributed/test_deepep_activation_checkpoint.py -k DeepEP` with DeepEP v2 at the CI pin 01dc3aa: 2 passed. These tests already run with an RMSNorm postprocess and check the score order against a reference.
- No new test. The existing DeepEP tests cover the postprocess path, and the unit check above was a one-off run.
- `pytest tests/unit_tests/cpu/models/muse_spark tests/unit_tests/cpu/recipes/test_muse_spark.py`: 40 passed, 3 xfailed.

stack-info: PR: #6037, branch: fegin/stack/35
@fegin
fegin marked this pull request as draft November 11, 2026 23:14
@fegin
fegin changed the base branch from fegin/stack/34 to main November 11, 2026 23:14
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 11, 2026
fegin added a commit that referenced this pull request Nov 12, 2026
## Summary (human-written)
As title. Enabling DeepEP for Muse Spark is trivial, performance is not
verified though.

## Why
`MuseSparkMoE.Config` rejects `DeepEPTokenDispatcher`, but nothing in Muse Spark needs that. The shared MoE code already supports DeepEP with an `output_postprocess`:
- In training, DeepEP uses its compact, host-synced path. The routed rows match the received token counts, so there are no rows past the last expert offset, and the HybridEP mask from #6032 is not needed.
- `RoutedExperts.forward` already applies the DeepEP routing scores after the postprocess, because Muse Spark's per-expert RMSNorm is nonlinear and must run first.

DeepEP is the EP backend that `search_r1` uses for both its trainer and its CUDA-graphed generator (expand layout), so this should also unblock Muse Spark RL on Hopper. I have not run the generator's expand layout with Muse Spark.

## What changed
- `MuseSparkMoE.Config` accepts `DeepEPTokenDispatcher`. `TorchAOTokenDispatcher` stays rejected.
- README: list DeepEP as supported, and add it to the CUDA graphs row: training needs graphs disabled. Note that CP is not verified with DeepEP, and that DeepEP's combine kernel needs the MoE compressed dimension to be a multiple of 256. Add a TODO for the config-time check. The production flavors use 4096. `debugmodel` uses 128, so it cannot run with DeepEP.

## Alternatives considered
- Add a `muse_spark_debugmodel_deepep` recipe and an integration test: `debugmodel` cannot run DeepEP (compressed dimension 128), and changing the debug flavor would change every Muse Spark debug run and golden loss.
- Validate the hidden size in `DeepEPTokenDispatcher.Config`: today a bad size fails at the first forward with DeepEP's static assert ("Invalid hidden"). A config-time check belongs to DeepEP in shared code, so it is left for a separate PR.

## Numerics
Muse Spark debug model with compressed dimension 256 (a local config change, see above), FSDP2 + TP2 (SP) + EP2 on 4x H100, seed 42, deterministic, 20 steps:

| Step | AllToAll | DeepEP |
| --- | --- | --- |
| 1 | 7.75888 | 7.75888 |
| 2 | 6.82995 | 6.83072 |
| 10 | 4.81295 | 4.81803 |
| 20 | 4.40471 | 4.39651 |

Average over 20 steps: 5.18424 vs 5.18254. Step 1 matches. Later steps differ by the combine kernels' summation order, with no consistent sign.

One-off unit check, the AllToAll comparison from the HybridEP tests run with DeepEP and an RMSNorm postprocess (2x H100): loss, input grad, and router, W13, W2, and norm weight grads match AllToAll within relative error 4.7e-3, below the 1.56e-2 bound. Loss and the expert and norm weight grads are bitwise equal.

## Performance
Not reported. DeepEP's expected benefit is multi-node communication, and I only have single-node H100 runs, which would not show it.

## Testing
- `pytest tests/unit_tests/gpu/distributed/test_deepep_activation_checkpoint.py -k DeepEP` with DeepEP v2 at the CI pin 01dc3aa: 2 passed. These tests already run with an RMSNorm postprocess and check the score order against a reference.
- No new test. The existing DeepEP tests cover the postprocess path, and the unit check above was a one-off run.
- `pytest tests/unit_tests/cpu/models/muse_spark tests/unit_tests/cpu/recipes/test_muse_spark.py`: 40 passed, 3 xfailed.

stack-info: PR: #6037, branch: fegin/stack/35
@fegin
fegin marked this pull request as draft November 12, 2026 00:54
@fegin
fegin changed the base branch from fegin/stack/34 to main November 12, 2026 00:54
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 12, 2026
fegin added a commit that referenced this pull request Nov 12, 2026
## Summary (human-written)
As title. Enabling DeepEP for Muse Spark is trivial, performance is not
verified though.

## Why
`MuseSparkMoE.Config` rejects `DeepEPTokenDispatcher`, but nothing in Muse Spark needs that. The shared MoE code already supports DeepEP with an `output_postprocess`:
- In training, DeepEP uses its compact, host-synced path. The routed rows match the received token counts, so there are no rows past the last expert offset, and the HybridEP mask from #6032 is not needed.
- `RoutedExperts.forward` already applies the DeepEP routing scores after the postprocess, because Muse Spark's per-expert RMSNorm is nonlinear and must run first.

DeepEP is the EP backend that `search_r1` uses for both its trainer and its CUDA-graphed generator (expand layout), so this should also unblock Muse Spark RL on Hopper. I have not run the generator's expand layout with Muse Spark.

## What changed
- `MuseSparkMoE.Config` accepts `DeepEPTokenDispatcher`. `TorchAOTokenDispatcher` stays rejected.
- README: list DeepEP as supported, and add it to the CUDA graphs row: training needs graphs disabled. Note that DeepEP's combine kernel needs the MoE compressed dimension to be a multiple of 256. Add a TODO for the config-time check. The production flavors use 4096. `debugmodel` uses 128, so it cannot run with DeepEP.

## Alternatives considered
- Add a `muse_spark_debugmodel_deepep` recipe and an integration test: `debugmodel` cannot run DeepEP (compressed dimension 128), and changing the debug flavor would change every Muse Spark debug run and golden loss.
- Validate the hidden size in `DeepEPTokenDispatcher.Config`: today a bad size fails at the first forward with DeepEP's static assert ("Invalid hidden"). A config-time check belongs to DeepEP in shared code, so it is left for a separate PR.

## Numerics
Muse Spark debug model with compressed dimension 256 (a local config change, see above), FSDP2 + TP2 (SP) + EP2 on 4x H100, seed 42, deterministic, 20 steps:

| Step | AllToAll | DeepEP |
| --- | --- | --- |
| 1 | 7.75888 | 7.75888 |
| 2 | 6.82995 | 6.83072 |
| 10 | 4.81295 | 4.81803 |
| 20 | 4.40471 | 4.39651 |

Average over 20 steps: 5.18424 vs 5.18254. Step 1 matches. Later steps differ by the combine kernels' summation order, with no consistent sign.

One-off unit check, the AllToAll comparison from the HybridEP tests run with DeepEP and an RMSNorm postprocess (2x H100): loss, input grad, and router, W13, W2, and norm weight grads match AllToAll within relative error 4.7e-3, below the 1.56e-2 bound. Loss and the expert and norm weight grads are bitwise equal.

## Performance
Not reported. DeepEP's expected benefit is multi-node communication, and I only have single-node H100 runs, which would not show it.

## Testing
- `pytest tests/unit_tests/gpu/distributed/test_deepep_activation_checkpoint.py -k DeepEP` with DeepEP v2 at the CI pin 01dc3aa: 2 passed. These tests already run with an RMSNorm postprocess and check the score order against a reference.
- Muse Spark debugmodel with compressed_dim 256, FSDP2 + Ulysses CP2 + EP4 (4x H100), seed 42, deterministic, 20 steps. Loss at steps 1 / 10 / 20: DeepEP 7.74766 / 4.80887 / 4.40117, AllToAll 7.74766 / 4.80533 / 4.39565.
- No new test. The existing DeepEP tests cover the postprocess path, and the unit check above was a one-off run.
- `pytest tests/unit_tests/cpu/models/muse_spark tests/unit_tests/cpu/recipes/test_muse_spark.py`: 40 passed, 3 xfailed.

stack-info: PR: #6037, branch: fegin/stack/35
@fegin
fegin changed the base branch from main to fegin/stack/34 November 12, 2026 00:54
@fegin
fegin marked this pull request as ready for review November 12, 2026 00:54
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 12, 2026

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.

oh we don't have to worry about https://github.com/pytorch/torchtitan/pull/6032/changes#r4544302973 for all DeepEP v2 modes? I vaguely remember that they have low-latency mode where a capacity factor needs to be given?

Now sure if it's https://github.com/pytorch/torchtitan/blob/main/torchtitan/distributed/deepep/deepep.py#L363

Please make sure it works with this setting, if we swap the model to be Muse Spark which has output_processor

# The generator captures CUDA graphs, so it needs DeepEP's static,
# host-sync-free EXPAND dispatch.
config.generator = apply_transforms(
config.generator,
[
TokenDispatcherTransform(
dispatcher=DeepEPTokenDispatcher,
# vLLM shards each engine step's token budget across TP ranks.
num_max_tokens_per_rank=(
max_num_batched_tokens
// config.generator.parallelism.tensor_parallel_degree
),
kwargs={"cuda_graph_compatible": True},
)
],
)
return config

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I see. I need to check other cases. The tests locally didn't get any issue. Will check

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@tianyu-l Double check, the mask is used to avoid 1) stale memory leaking into the backward, 2) forward uses the stale memory. RL case doesn't have 1 and DeepEP doesn't have 2. So we don't need mask for DeepEP.

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.

when you say

RL case doesn't have 1

RL indeed has backward. Are you saying one of the following?

  • the low-latency mode of deepep doesn't support backward, so it cannot be used for trainer (my impression was this one)
  • the low-latency mode of deepep can be used for training, but somehow it doesn't leak stale memory into backward (if so, how could it be not leaking?)

Please add comments in code, somewhere

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the ambiguous wording, RL doesn't use the DeepEP expand (low-latency) layout because it doesn't support backward -- the kernel will assert. Agent added a comment in moe.py.

@fegin
fegin changed the base branch from fegin/stack/34 to main November 12, 2026 07:55
@fegin
fegin marked this pull request as draft November 12, 2026 07:55
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 12, 2026
fegin added a commit that referenced this pull request Nov 12, 2026
## Summary (human-written)
As title. Enabling DeepEP for Muse Spark is trivial, performance is not
verified though.

## Why
`MuseSparkMoE.Config` rejects `DeepEPTokenDispatcher`, but nothing in Muse Spark needs that. The shared MoE code already supports DeepEP with an `output_postprocess`:
- In training, DeepEP uses its compact, host-synced path. The routed rows match the received token counts, so there are no rows past the last expert offset, and the HybridEP mask from #6032 is not needed.
- `RoutedExperts.forward` already applies the DeepEP routing scores after the postprocess, because Muse Spark's per-expert RMSNorm is nonlinear and must run first.

DeepEP is the EP backend that `search_r1` uses for both its trainer and its CUDA-graphed generator (expand layout), so this should also unblock Muse Spark RL on Hopper. I have not run the generator's expand layout with Muse Spark.

## What changed
- `MuseSparkMoE.Config` accepts `DeepEPTokenDispatcher`. `TorchAOTokenDispatcher` stays rejected.
- README: list DeepEP as supported, and add it to the CUDA graphs row: training needs graphs disabled. Note that DeepEP's combine kernel needs the MoE compressed dimension to be a multiple of 256. Add a TODO for the config-time check. The production flavors use 4096. `debugmodel` uses 128, so it cannot run with DeepEP.

## Alternatives considered
- Add a `muse_spark_debugmodel_deepep` recipe and an integration test: `debugmodel` cannot run DeepEP (compressed dimension 128), and changing the debug flavor would change every Muse Spark debug run and golden loss.
- Validate the hidden size in `DeepEPTokenDispatcher.Config`: today a bad size fails at the first forward with DeepEP's static assert ("Invalid hidden"). A config-time check belongs to DeepEP in shared code, so it is left for a separate PR.

## Numerics
Muse Spark debug model with compressed dimension 256 (a local config change, see above), FSDP2 + TP2 (SP) + EP2 on 4x H100, seed 42, deterministic, 20 steps:

| Step | AllToAll | DeepEP |
| --- | --- | --- |
| 1 | 7.75888 | 7.75888 |
| 2 | 6.82995 | 6.83072 |
| 10 | 4.81295 | 4.81803 |
| 20 | 4.40471 | 4.39651 |

Average over 20 steps: 5.18424 vs 5.18254. Step 1 matches. Later steps differ by the combine kernels' summation order, with no consistent sign.

One-off unit check, the AllToAll comparison from the HybridEP tests run with DeepEP and an RMSNorm postprocess (2x H100): loss, input grad, and router, W13, W2, and norm weight grads match AllToAll within relative error 4.7e-3, below the 1.56e-2 bound. Loss and the expert and norm weight grads are bitwise equal.

## Performance
Not reported. DeepEP's expected benefit is multi-node communication, and I only have single-node H100 runs, which would not show it.

## Testing
- `pytest tests/unit_tests/gpu/distributed/test_deepep_activation_checkpoint.py -k DeepEP` with DeepEP v2 at the CI pin 01dc3aa: 2 passed. These tests already run with an RMSNorm postprocess and check the score order against a reference.
- Muse Spark debugmodel with compressed_dim 256, FSDP2 + Ulysses CP2 + EP4 (4x H100), seed 42, deterministic, 20 steps. Loss at steps 1 / 10 / 20: DeepEP 7.74766 / 4.80887 / 4.40117, AllToAll 7.74766 / 4.80533 / 4.39565.
- No new test. The existing DeepEP tests cover the postprocess path, and the unit check above was a one-off run.
- `pytest tests/unit_tests/cpu/models/muse_spark tests/unit_tests/cpu/recipes/test_muse_spark.py`: 40 passed, 3 xfailed.

stack-info: PR: #6037, branch: fegin/stack/35
@fegin
fegin marked this pull request as ready for review November 12, 2026 07:56
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 12, 2026
@fegin
fegin requested a review from tianyu-l November 12, 2026 07:56
## Summary (human-written)
As title. Enabling DeepEP for Muse Spark is trivial, performance is not
verified though.

## Why
`MuseSparkMoE.Config` rejects `DeepEPTokenDispatcher`, but nothing in Muse Spark needs that. The shared MoE code already supports DeepEP with an `output_postprocess`:
- In training, DeepEP uses its compact, host-synced path. The routed rows match the received token counts, so there are no rows past the last expert offset, and the HybridEP mask from #6032 is not needed.
- `RoutedExperts.forward` already applies the DeepEP routing scores after the postprocess, because Muse Spark's per-expert RMSNorm is nonlinear and must run first.

DeepEP is the EP backend that `search_r1` uses for both its trainer and its CUDA-graphed generator (expand layout), so this should also unblock Muse Spark RL on Hopper. I have not run the generator's expand layout with Muse Spark.

## What changed
- `MuseSparkMoE.Config` accepts `DeepEPTokenDispatcher`. `TorchAOTokenDispatcher` stays rejected.
- `RoutedExperts.forward`: comment at the HybridEP mask gate on why DeepEP needs no mask. Training uses the compact layout, which has no rows past the last offset. The expand layout runs only without grad: `dispatch_tokens` forces the compact layout when grad is enabled, and the DeepEP kernel forbids backward in expand mode.
- README: list DeepEP as supported, and add it to the CUDA graphs row: training needs graphs disabled. Note that DeepEP's combine kernel needs the MoE compressed dimension to be a multiple of 256. Add a TODO for the config-time check. The production flavors use 4096. `debugmodel` uses 128, so it cannot run with DeepEP.

## Alternatives considered
- Add a `muse_spark_debugmodel_deepep` recipe and an integration test: `debugmodel` cannot run DeepEP (compressed dimension 128), and changing the debug flavor would change every Muse Spark debug run and golden loss.
- Validate the hidden size in `DeepEPTokenDispatcher.Config`: today a bad size fails at the first forward with DeepEP's static assert ("Invalid hidden"). A config-time check belongs to DeepEP in shared code, so it is left for a separate PR.

## Numerics
Muse Spark debug model with compressed dimension 256 (a local config change, see above), FSDP2 + TP2 (SP) + EP2 on 4x H100, seed 42, deterministic, 20 steps:

| Step | AllToAll | DeepEP |
| --- | --- | --- |
| 1 | 7.75888 | 7.75888 |
| 2 | 6.82995 | 6.83072 |
| 10 | 4.81295 | 4.81803 |
| 20 | 4.40471 | 4.39651 |

Average over 20 steps: 5.18424 vs 5.18254. Step 1 matches. Later steps differ by the combine kernels' summation order, with no consistent sign.

One-off unit check, the AllToAll comparison from the HybridEP tests run with DeepEP and an RMSNorm postprocess (2x H100): loss, input grad, and router, W13, W2, and norm weight grads match AllToAll within relative error 4.7e-3, below the 1.56e-2 bound. Loss and the expert and norm weight grads are bitwise equal.

## Performance
Not reported. DeepEP's expected benefit is multi-node communication, and I only have single-node H100 runs, which would not show it.

## Testing
- `pytest tests/unit_tests/gpu/distributed/test_deepep_activation_checkpoint.py -k DeepEP` with DeepEP v2 at the CI pin 01dc3aa: 2 passed. These tests already run with an RMSNorm postprocess and check the score order against a reference.
- Muse Spark debugmodel with compressed_dim 256, FSDP2 + Ulysses CP2 + EP4 (4x H100), seed 42, deterministic, 20 steps. Loss at steps 1 / 10 / 20: DeepEP 7.74766 / 4.80887 / 4.40117, AllToAll 7.74766 / 4.80533 / 4.39565.
- No new test. The existing DeepEP tests cover the postprocess path, and the unit check above was a one-off run.
- `pytest tests/unit_tests/cpu/models/muse_spark tests/unit_tests/cpu/recipes/test_muse_spark.py`: 40 passed, 3 xfailed.

stack-info: PR: #6037, branch: fegin/stack/35
@fegin
fegin marked this pull request as draft November 12, 2026 17:50
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 12, 2026
@fegin
fegin marked this pull request as ready for review November 12, 2026 17:50
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 12, 2026
@fegin
fegin merged commit e62905d into main Nov 13, 2026
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.

3 participants