Repository navigation
Conversation
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.
🔗 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 FailuresAs of commit 251ff46 with merge base 7f632bb ( 🟢 GREEN LIGHT - PR approved to be merged without human review:
reason: This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Collaborator
Author
|
@pytorchbot merge |
Collaborator
Merge startedYour 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 |
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.pybecomes the packagetorch/distributed/_token_switch/, with Routing, the autograd functions, the TokenSwitch base and TokenSwitchNCCL unchanged in__init__.py, and TokenSwitchMoRI, its routing state and_import_morimoved verbatim tomori.py.__init__.pyre-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_ranknow returns the receive capacity its checks enforce (effective_max_recv, notmax_recv).max_expert_major_slots_per_rankbecomes a TokenSwitch property defaulting tomax_recv_tokens_per_rankand 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), andtest_dispatch_short_out_raisesshortens 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:
TokenSwitchMoRITest 41/41 pass (two runs), TokenSwitchReferenceTest passes, NCCL classes skip on ROCm.