Skip to content

[TokenSwitch] Support topk_weights gradient in dispatch so the MoE router can train #201470

Closed

Description

@svskvar

🚀 The feature, motivation and pitch

While using TokenSwitch for expert-parallel MoE training, the MoE router needs a gradient through TokenSwitch.dispatch to learn which experts to pick.

Today, dispatch in autograd mode returns no gradient for topk_weights:

The usual MoE pattern applies the routing weights to the expert output on the expert rank:

recv_tokens, recv_weights, _ = ts.dispatch(routing, x, topk_weights, max_recv)
out = ts.combine(routing, expert(recv_tokens) * recv_weights[:, :1])

Autograd computes the gradient of recv_weights, but dispatch backward drops it. So the router weights get no gradient from the main loss, and the router does not learn which experts to pick. The expert weights and the token gradients are correct. All backends inherit this, because the autograd is in the base class.

Proposed Solution

Return a gradient for topk_weights from dispatch backward. Send grad_out_topk_weights back to the source rank, one value for each (token, expert) pair. Backends that do not support this can keep the current behavior.

Would you accept a PR with above change?

cc @awgu @wanchaol @fegin @fduwjj @wz337 @wconstab @d4l3k @pragupta @msaroufim @dcci @aditvenk @weifengpy @kapilsh @kwen2501

Alternatives

No response

Additional context

No response

Activity

  1. added
    oncall: distributed
    bot-triaged
    oncall: distributed infra
    on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bot-triagedoncall: distributedoncall: distributed infra

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions