Skip to content

Fix expert-parallel token dispatch gradients on torch 2.7 to 2.10 - #49710

Merged
ArthurZucker merged 8 commits into
huggingface:mainfrom
digit50:fix/ep-dispatch-autograd-collective
Oct 20, 2026
Merged

ArthurZucker merged 8 commits into
huggingface:mainfrom
digit50:fix/ep-dispatch-autograd-collective

Conversation

@digit50

@digit50 digit50 commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

🤗 Transformers CI

What does this PR do?

With ep_dispatch_experts, both all-to-all exchanges used all_to_all_single from torch.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-backward autograd.Function, because calling wait_tensor directly 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 with gate_up_proj.grad being None.

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.

  • (First-time contributors only): I confirm that this PR description and code is not written by an LLM or code agent

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline and the
    Pull Request checks?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes according to the guidelines?
  • Did you write any new necessary tests?

Who can review?

@3outeille @ArthurZucker

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.
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution 🤗!

CI Security Gate — waiting for maintainer approval

CI 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 ArthurZucker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but @3outeille tell me!

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice. It can be made a bit easier to read/understand and that should be good to go

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.

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.

digit50 and others added 4 commits October 19, 2026 08:10
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.
@3outeille
3outeille enabled auto-merge October 19, 2026 16:41
@3outeille
3outeille disabled auto-merge October 19, 2026 16:45
@3outeille
3outeille enabled auto-merge October 19, 2026 17:50
@3outeille 3outeille added the for patch Tag issues / labels that should be included in the next patch label Oct 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 39325751487:2
Result: failure | Jobs: 15 | Tests: 136,370 | Failures: 0 | Duration: 9h 43m

@ArthurZucker
ArthurZucker disabled auto-merge October 20, 2026 07:13
@ArthurZucker
ArthurZucker merged commit 9f5750c into huggingface:main Oct 20, 2026
214 of 218 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

for patch Tag issues / labels that should be included in the next patch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expert-parallel token dispatch gives experts no gradient on torch 2.7 to 2.10

4 participants