Repository navigation
Fix expert-parallel token dispatch gradients on torch 2.7 to 2.10 - #49710
Conversation
Before torch 2.11 the functional all_to_all_single has no autograd kernel, so the dispatch and combine exchanges gave the experts and the hidden states no gradient. Both exchanges now use all_to_all_single_autograd, and the wait on the received rows passes the gradient through instead of hitting torch's missing-kernel fallback. A two-rank gloo test compares expert and hidden-state gradients with an unsharded reference.
|
Thank you for your contribution 🤗! CI Security Gate — waiting for maintainer approvalCI is approved automatically only for authors who already have a contribution merged in this repository. A maintainer will review this PR and approve CI to run on it. |
ArthurZucker
left a comment
There was a problem hiding this comment.
LGTM but @3outeille tell me!
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
| experts(hidden_states, torch.tensor([[0, 3], [1, 2], [2, 1], [3, 0]]), torch.ones(4, 2)) | ||
|
|
||
|
|
||
| def _dispatch_backward_matches_unsharded_experts(rank): |
There was a problem hiding this comment.
Instead, we should have a gradient check in test_tensor_parallel_mixin.py for test_ep_backward, the same way we do it in test_tp_backward
There was a problem hiding this comment.
Done. test_ep_backward in tests/test_tensor_parallel_mixin.py now compares every parameter gradient with the unsharded reference, as test_tp_backward does, with the expert weights sliced to the rank's experts on dim 0, and a gradient missing on either side counts as a failure. With the fix reverted it fails for qwen3_moe because the expert weights get no gradient, and the standalone test in tests/tensor_parallel/test_tensor_parallel.py is gone.
There was a problem hiding this comment.
nice. It can be made a bit easier to read/understand and that should be good to go
There was a problem hiding this comment.
I moved the slicing of the reference gradient to the local shard into one helper, _reference_grad_shard, which test_tp_backward now uses as well, so the gradient loop in test_ep_backward only skips, flags a missing gradient, and compares. The checks are the same as before.
test_ep_backward now checks every parameter gradient against the unsharded reference, as test_tp_backward does, slicing the expert weights to the rank's experts on dim 0 and the dense weights by the TP plan. A missing gradient on either side is a failure, which is how the dispatch bug showed. The standalone dispatch backward test is removed in favour of this check.
The slicing of the unsharded reference gradient to the rank's shard moves into one helper used by test_tp_backward and test_ep_backward, so the EP gradient loop reads as skip, fail on a missing gradient, compare. What the tests check is unchanged.
CI recapDashboard: View test results in Grafana |
What does this PR do?
With
ep_dispatch_experts, both all-to-all exchanges usedall_to_all_singlefromtorch.distributed._functional_collectives, which has no autograd kernel before torch 2.11. On torch 2.7 to 2.10 training ran without any error, but the expert weights got no gradient and the hidden states got none through the MoE block.This PR switches the dispatch and combine exchanges to
all_to_all_single_autograd, which exists on every torch the dispatch check accepts (2.7 and later). The explicit wait on the received rows (kept so the experts get a plain tensor, see #49421) now goes through a small identity-backwardautograd.Function, because callingwait_tensordirectly on a tensor that requires grad hits torch's missing-kernel fallback on those versions and logs the "autograd kernel was not registered" deprecation warning.The new test runs a
Qwen3MoeExperts(4 experts, top-2) split over two gloo CPU ranks through the dispatch forward, with a different batch per rank, and checks that the expert weight gradients and the hidden-state gradient match an unsharded reference run on both batches, and that no warning is raised. On torch 2.10 it fails before the change withgate_up_proj.gradbeingNone.Fixes #49702
Code Agent Policy
The Transformers repo is currently being overwhelmed by a large number of PRs and issue comments written by
code agents. These often are low-quality, or fix extremely minor issues that occur rarely or never in practice.
As a result, we're instituting a rule that first-time contributors should not use code agents to submit PRs or issues.
We'd also ask autonomous "OpenClaw"-like agents not to open any PRs or issues.
Issues/PRs from first-time contributors that violate this rule will probably just be closed without review, and we
might block you, especially if you open more than one or appear to be deliberately ignoring this. We especially do not
want new contributors to jump in on random issues to contribute an agent-written fix. This creates lots of noise
for reviewers and other users and will almost certainly get you blocked.
For more information, please read
CONTRIBUTING.md.Before submitting
Pull Request checks?
to it if that's the case.
Who can review?
@3outeille @ArthurZucker