Skip to content

[dist_moe] Reset context after PP metadata inference - #5084

Merged
sanketpurandare merged 2 commits into
mainfrom
codex/dist-moe-post-metadata-cleanup
Oct 8, 2026
Merged

sanketpurandare merged 2 commits into
mainfrom
codex/dist-moe-post-metadata-cleanup

Conversation

@sanketpurandare

Copy link
Copy Markdown
Contributor

Summary

Dynamic pipeline metadata inference executes representative model forwards before the first training step. Dist-MoE owns activation state outside the stage module, so PipelineStage's module and FSDP cleanup cannot release that state.

Register DistMoeRuntime.context.reset through PyTorch's post-metadata-inference cleanup API. The runtime owns the returned handle and removes it during constructor rollback and normal teardown.

This replaces the validation-only private schedule monkeypatch. It does not change pipeline schedules, activation-slot coloring, Dist-MoE kernels, or static-metadata execution.

Dependency

Depends on pytorch/pytorch#199884. Keep this PR in draft until that API lands, then rebase on the corresponding PyTorch baseline and rerun the gates below.

Test plan

  • python -m pytest -q tests/unit_tests/cpu/test_dist_moe.py (38 passed)
  • Focused Pyrefly on torchtitan/models/common/dist_moe/runtime.py (0 errors)
  • All non-Pyrefly pre-commit hooks passed; project-wide Pyrefly reports an unrelated existing multimedia video.py error
  • A two-rank PP/CP MXFP8 Dist-MoE configuration completed 10 full CUDA-graph training steps with finite loss and gradients and no metadata monkeypatch

@pytorch-bot pytorch-bot Bot added the ciflow/rl label Oct 6, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 6, 2026
Comment on lines +371 to +375
self._metadata_inference_cleanup_handle = (
pp_schedule.register_post_metadata_inference_cleanup(
self.context.reset
)
)

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.

runtime.py:371 registers cleanup on pp_schedule. GraphPP passes its analysis-only liveness schedule, but metadata inference runs on a different execution schedule.

It works today only because copy.copy(schedule) shares the callback dictionary. Add an explicit registrar that forwards to GraphRuntime.schedule.

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.

Added GraphRuntime.register_post_metadata_inference_cleanup() as an explicit registrar that delegates to GraphRuntime.schedule, and passed it separately from pipeline_liveness_schedule into DistMoeRuntime. Eager PP still registers directly on its single schedule. The existing GraphTrainer test now uses distinct liveness and execution schedules and invokes the registrar to verify that registration targets the execution schedule and returns its handle. The focused eager and GraphTrainer Dist-MoE suites pass (46 passed), and focused Pyrefly reports zero errors for runtime.py.

@sanketpurandare
sanketpurandare requested a review from fegin October 6, 2026 23:29
@sanketpurandare
sanketpurandare force-pushed the codex/dist-moe-post-metadata-cleanup branch from c6c808a to 112f731 Compare October 8, 2026 10:46
@sanketpurandare
sanketpurandare marked this pull request as ready for review October 8, 2026 16:07
@pytorch-bot pytorch-bot Bot added ciflow/b200 Trigger B200 CI ciflow/fake-pg Run 1-GPU Fake PG integration tests labels Oct 8, 2026
Dynamic PP metadata inference executes real model forwards before the first
training step. Register the Dist-MoE context reset through PyTorch's
post-metadata-inference cleanup API so probe activation state cannot enter the
real schedule. Runtime teardown owns and removes the registration.

Test Plan:

```
python -m pytest -q tests/unit_tests/cpu/test_dist_moe.py
pyrefly check torchtitan/models/common/dist_moe/runtime.py
pre-commit run --files torchtitan/models/common/dist_moe/runtime.py tests/unit_tests/cpu/test_dist_moe.py
```
GraphTrainer retains a pre-rewrite pipeline schedule for activation-liveness analysis, but metadata inference executes on GraphRuntime.schedule. Registering cleanup on the liveness copy worked only because copy.copy shared its callback dictionary with the execution schedule.

Give GraphRuntime an explicit cleanup registrar and thread it independently from the liveness schedule into DistMoeRuntime. Eager PP continues to register directly on its single schedule.

This change was authored with assistance from Codex.

Test Plan:

```

python -m pytest -q tests/unit_tests/cpu/test_dist_moe.py torchtitan/experiments/graph_trainer/tests/test_dist_moe.py

pyrefly check torchtitan/models/common/dist_moe/runtime.py

SKIP=pyrefly-check pre-commit run --files torchtitan/models/common/dist_moe/runtime.py torchtitan/experiments/graph_trainer/graph_pp/runner.py torchtitan/experiments/graph_trainer/trainer.py torchtitan/experiments/graph_trainer/tests/test_dist_moe.py tests/unit_tests/cpu/test_dist_moe.py

```
@sanketpurandare
sanketpurandare force-pushed the codex/dist-moe-post-metadata-cleanup branch from 112f731 to 505e248 Compare October 8, 2026 18:06
@sanketpurandare
sanketpurandare merged commit e7143a9 into main Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/b200 Trigger B200 CI 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