Skip to content

Move TokenSwitchMoRI into torch/distributed/_token_switch/mori.py - #206366

Closed
jeffdaily wants to merge 2 commits into
mainfrom
jeffdaily/tokenswitch-mori-pkg
Closed

jeffdaily wants to merge 2 commits into
mainfrom
jeffdaily/tokenswitch-mori-pkg

Conversation

@jeffdaily

Copy link
Copy Markdown
Collaborator

Linked issue or supporting maintainer

Author has write access. Requested by @d4l3k in #201225.

BC-breaking?

No

Summary (human written only)

Follow-up to #201225. Addresses @d4l3k's review comment there by moving the MoRI backend out of the shared module into torch/distributed/_token_switch/mori.py, and addresses the four non-blocking findings of the automated review on that PR.

Agent details (if applicable)

My agent came up with this analysis, but I've read and approved it.

The first commit is a pure move: torch/distributed/_token_switch.py becomes the package torch/distributed/_token_switch/, with Routing, the autograd functions, the TokenSwitch base and TokenSwitchNCCL unchanged in __init__.py, and TokenSwitchMoRI, its routing state and _import_mori moved verbatim to mori.py. __init__.py re-imports them at the bottom so existing imports work. The NCCL backend stays in __init__.py; @d4l3k is asking its owners to move it the same way.

The second commit fixes the review findings. TokenSwitchMoRI.max_recv_tokens_per_rank now returns the receive capacity its checks enforce (effective_max_recv, not max_recv). max_expert_major_slots_per_rank becomes a TokenSwitch property defaulting to max_recv_tokens_per_rank and overridden by MoRI, so shared code no longer names a MoRI attribute and MoRI's combine backward drops its second buffer. The MoRI-only tests get @skip_if_lt_x_gpu(2), and test_dispatch_short_out_raises shortens each dispatch output in turn.

Test plan, on 4x MI355X (gfx950) with a ROCm nightly overlaid with this package and MoRI EP v2 + flydsl built from source:

python test/distributed/test_token_switch.py -v
lintrunner torch/distributed/_token_switch/__init__.py torch/distributed/_token_switch/mori.py test/distributed/test_token_switch.py

TokenSwitchMoRITest 41/41 pass (two runs), TokenSwitchReferenceTest passes, NCCL classes skip on ROCm.

Reviewer request on #201225: keep backend-specific code out of the shared TokenSwitch module. torch/distributed/_token_switch.py becomes the package torch/distributed/_token_switch/, whose __init__.py keeps Routing, the autograd functions, the TokenSwitch base and TokenSwitchNCCL unchanged. TokenSwitchMoRI, _MoRIRoutingState and _import_mori move verbatim to mori.py.

__init__.py imports TokenSwitchMoRI and _import_mori back at the bottom, after the base classes are defined, so every existing `from torch.distributed._token_switch import ...` keeps working (including the private names test_token_switch.py uses) and mori.py can subclass TokenSwitch without a circular-import failure. The NCCL backend stays in __init__.py; moving it is left to its owners. Two comments that named the old file path now name the package.

Test Plan:

On a gfx950 host (4 GPUs), with a ROCm nightly torch overlaid with this package and MoRI EP v2 + flydsl installed:

```
python -c "import torch.distributed._token_switch as m; from torch.distributed._token_switch import _import_mori, _import_nccl_ep, Routing, TokenSwitch, TokenSwitchMoRI, TokenSwitchNCCL"
python test/distributed/test_token_switch.py -v
lintrunner torch/distributed/_token_switch/__init__.py torch/distributed/_token_switch/mori.py
```

Authored with Claude.
Addresses the non-blocking findings of the automated review on #201225.

TokenSwitchMoRI.max_recv_tokens_per_rank returned MoRI's cfg.max_recv while every size check and the backward passes used cfg.effective_max_recv. They are equal in today's configuration, but a caller sizing flat outputs from the documented property would get an error naming a bound the API never exposed if they ever differ. The property now returns the receive capacity the backend actually enforces, and the dispatch size error names the property to size by.

The expert_major bound was reachable only as TokenSwitchMoRI.max_expert_major_slots_per_rank, which the base docstring and the shared _CombineAutograd.backward pointed at by name. max_expert_major_slots_per_rank is now a TokenSwitch property defaulting to max_recv_tokens_per_rank (NCCL enforces that bound for both layouts) and MoRI overrides it. _CombineAutograd.backward sizes the gradient buffer by the layout's bound, so MoRI's _combine_backward no longer allocates a second buffer and copies back. A per-layout method was the other option; one property keeps the existing MoRI name and adds no new call shape.

The MoRI-only tests get @skip_if_lt_x_gpu(2) like the shared mixins, since they assume at least two ranks, and test_dispatch_short_out_raises now shortens each dispatch output in turn, as NCCL's test_dispatch_requires_full_capacity does.

Test Plan:

On a gfx950 host (4 GPUs), with a ROCm nightly torch overlaid with this package and MoRI EP v2 + flydsl installed:

```
python test/distributed/test_token_switch.py -v
lintrunner torch/distributed/_token_switch/__init__.py torch/distributed/_token_switch/mori.py test/distributed/test_token_switch.py
```

Authored with Claude.
@pytorch-bot pytorch-bot Bot added ciflow/h100-symm-mem ciflow/torchtitan Run TorchTitan integration tests release notes: distributed (c10d) release notes category labels Nov 24, 2026
@pytorch-bot

pytorch-bot Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/206366

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 251ff46 with merge base 7f632bb (image):
💚 Looks good so far! There are no failures yet. 💚

🟢 GREEN LIGHT - PR approved to be merged without human review:

  • Triviality
    • Mechanical refactor: TokenSwitchMoRI, _MoRIRoutingState, _import_mori move verbatim to _token_switch/mori.py, re-exported at the bottom of __init__.py
    • Single-cause bugfix: MoRI max_recv_tokens_per_rank returned cfg.max_recv while _dispatch enforced effective_max_recv; now returns _recv_cap
    • Test changes: skip_if_lt_x_gpu(2) on MoRI-only tests and test_dispatch_short_out_raises shortening each output in turn
  • Behavior of the shared backward
    • _CombineAutograd.backward is the only caller of _combine_backward; it now sizes the buffer to the layout bound, so MoRI's fallback buffer is unneeded
    • New base max_expert_major_slots_per_rank defaults to max_recv_tokens_per_rank, so the NCCL and reference bounds are unchanged
  • User-facing surface
    • Everything touched sits under the private torch.distributed._token_switch; existing imports still resolve via the re-export
    • CMake and nccl_ep_pybind.cpp edits are comment path updates only
  • Evidence
    • Description reports TokenSwitchMoRITest 41/41 on 4x MI355X; the require at least {need} rows regex still matches the reworded error

reason: clean
Reviewed commit: 251ff46

Inference job

🐛 Report a wrong verdict

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@jeffdaily

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Nov 24, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

This branch was previously deployed

1 inactive deployment
bedrock — 251ff463 Deployed Nov 24, 2026 by pytorchbot via analyze #39915
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/h100-symm-mem ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request GL/approved Approved by GreenLight Merged open source release notes: distributed (c10d) release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants