Repository navigation
[moe][perf] AllToAll dispatcher: permute and unpermute with gathers (1/4) - #5162
Conversation
9985821 to
30f0b98
Compare
30f0b98 to
27e472c
Compare
27e472c to
9966477
Compare
9966477 to
f6e3ab5
Compare
f6e3ab5 to
698dd37
Compare
698dd37 to
f5a3ed1
Compare
|
Reply from claude: @claude review |
|
Claude finished @felipemello1's task in 2m 3s —— View job Verdict: Approve -- please fill in the human-written summary before merging; a maintainer still needs to review. Review · |
There was a problem hiding this comment.
Verdict: Approve -- please address the item below before merging -- the change is a correct, bitwise-identical substitution of scatters by gathers in a path that runs eagerly on every EP>1 step; the only open item is the empty human-written summary. The PR touches models/common/, so a maintainer still needs to review.
What this PR does. AllToAllTokenDispatcher._permute still gathers x[perm], but through _PermuteRows, whose backward gathers by inv_perm instead of autograd's default accumulating index_put (sort + zero fill). _unpermute drops out[perm] = y and gathers y[inv_perm]. inv_perm comes from one R-length int scatter (_inverse_permutation). TorchAOTokenDispatcher keeps its own _permute/_unpermute, since its indices are not a permutation: they include a sentinel row for padding.
Example. 2 EP ranks, 1 local expert each... take perm = [2, 0, 1], inv = [1, 2, 0]:
permute fwd main: x[perm] PR: x[perm]
permute bwd main: zeros.index_put_(perm, g, accumulate=True)
PR: g[inv]
unpermute fwd main: out = empty; out[perm] = y
PR: y[inv] (out[perm[i]] = y[i] <=> out[j] = y[inv[j]])
unpermute bwd main: g[perm] PR: g[inv(inv)] = g[perm]
All row moves, so outputs and grads are identical as long as perm is a full permutation of the R received rows, which _permute guarantees (seg_ids has output_size=total).
Must fix
- PR description -- the "Summary (human-written)" section is empty. Per
CONTRIBUTING.md("AI-assisted contributions"), the author fills it in before merge.
Details
- Necessity: the problem is real on
main. Both ops run outside any compile region, and the description measures them (9.25 ms -> 4.08 ms per layer at 16k tokens, DSv3 EP2). This PR is not superseded by the rest of the stack: #5382 and #5518 change the local sort/combine (_local_reorder/_score_and_scatter_add), not the EP rank-major -> expert-major_permute. - Subclasses:
TorchAOTokenDispatcheris the only subclass, and it overrides both_permuteand_unpermute, so_PermuteRowsnever sees its padded, non-permutation indices. - Determinism:
scatter_with unique indices and gathers are deterministic, and underuse_deterministic_algorithms(True)scatterwith asrctensor has a deterministic kernel, so it won't raise. - Memory: the saved index has the same
Rint64 size as before (perm, orinvfor unpermute). The extra cost is oneR-length int scatter in each of permute bwd, unpermute fwd and unpermute bwd. That is negligible next to theR x Dbf16 row moves, and the benchmark already includes it. - Test: the new CPU test checks forward and both backwards against the old indexing semantics with exact tolerances. The custom backward is otherwise only guarded by GPU EP goldens, so the test is cheap and earns its place.
Not verified: GPU timings, the spmd_types type-checking run with EP, and GraphTrainer + EP (the description already lists the last one as untested). I couldn't run the unit test here because pytest needs approval.
…athers _permute's x[perm] backpropagates with an accumulating index_put (sort plus zero fill) and _unpermute's out[perm] = y is a non-vectorized scatter. For a permutation both are gathers: gather by the permutation and backpropagate by gathering with its inverse.
Review fix: the base _unpermute gathers by the inverse permutation, which keeps every row, so input_shape is unused there; TorchAO's padded override needs it.
f5a3ed1 to
89629f1
Compare
|
Claude finished @felipemello1's task in 50s —— View job Verdict: Approve. Before merging, the empty human-written summary still needs filling in, and a maintainer still needs to review. Review |
There was a problem hiding this comment.
Verdict: Approve -- please address the item below before merging. The new commits 89629f1 and f03292a change only comments and docstrings, and the examples are correct. The one open item from the last review is still open. This PR touches models/common/, so a maintainer still needs to review.
What changed since the last review. _PermuteRows, _inverse_permutation, and AllToAllTokenDispatcher._unpermute now explain themselves with a 3-row example instead of >>> doctests. _unpermute also says why input_shape is unused. No code changed.
Example check (token_dispatcher.py:27-32, :58-60):
perm = [2, 0, 1], x = [a, b, c]
x[perm] = [x2, x0, x1] = [c, a, b] matches
inv[perm[i]] = i -> inv[2]=0, inv[0]=1, inv[1]=2 = [1, 2, 0] matches
grad_out[inv] = [go1, go2, go0] = [ga, gb, gc] matches
x[perm][inv][j] = x[perm[inv[j]]] = x[j] matches
The added lines are ASCII only. The two non-ASCII dashes in the file, at :413 and :1020, were already there.
Must fix
- PR description: the "Summary (human-written)" section is still empty (still open from the last review). Per
CONTRIBUTING.md("AI-assisted contributions"), the author fills it in before merge.
Should consider
token_dispatcher.py:38-39: item 3 of the "Why not plainx[perm]" list is a GB300 benchmark number, not a reason. Hardware-specific timings in a docstring go stale when kernels or hardware change, and the PR description already has the numbers. Items 1-2 make the case on their own, so item 3 could be dropped.
Not verified: GPU timings (unchanged from the last review).
…ce, any EP Rebased on main after #5162 and #5436 merged. One commit with the PR's changes from: - 8ad2584 [moe][perf] Gather-sum MoE dispatch and combine, training and inference, any EP - 2e3549b [moe] Address review: names, docstrings, fewer tests, TorchAO EP=1 TODO - 742288d [moe] Fold the region-name check into the trainer == generator test - aaa0090 [moe] Rename _sort_and_gather to _reorder_by_expert
Summary (human-written)
Agent details
TLDR: The AllToAll dispatcher's eager permute and unpermute become gathers: -5.2 ms per MoE layer at 16k tokens on every EP>1 step (DeepSeek-V3 EP2 rank, 1 GB300), bitwise identical. Today's Qwen3.5-35B-A3B and GPT-OSS RL recipes run this dispatcher in both roles; Dist-MoE and DeepEP have their own.
Stack (4 PRs, merge bottom-up; this PR is in bold)
Problem
AllToAllTokenDispatcher._permutereorders the received rows withx[perm].index_put: a sort plus a zero fill._unpermutewritesout[perm] = y, a non-vectorized scatter.Solution
_PermuteRowsgathers by the permutation and backpropagates by gathering with its inverse._unpermutegathers by the inverse._unpermute.Results
You may ask: does this change the reference dispatcher? Its output stays bitwise identical to main's, so the all-to-all dispatcher is still the correctness reference (
torchtitan/models/common/moe/README.md:12).NOTE:
deepseek_v3_671buses EP=2).Test plan