PyTorch adds autograd support to TokenSwitch for efficient MoE training
New dispatch/combine operations enable gradient flow in mixture-of-experts layers
PyTorch has merged a significant update to its TokenSwitch module, which handles token routing in mixture-of-experts (MoE) models. The commit, authored with assistance from Claude, adds full autograd support to the two core operations—dispatch and combine—that are fundamental to MoE layer execution. These operations are adjoint: the backward pass of dispatch calls combine, and the backward of combine calls dispatch. Previously, users had to implement custom backward functions or rely on non-differentiable routing, limiting the ability to train sparse MoE layers end-to-end with gradient descent.
The new API, designed to mirror conventions like `nn.Module` and `torch.matmul`, provides two usage modes via an optional `out` parameter. When `out=(out_tokens, out_weights, out_idx)` is supplied, the operations write directly to pre-allocated buffers and return them without tracking gradients—ideal for inference or memory-constrained scenarios. When `out` is omitted, internal buffers are allocated and autograd is automatically enabled through wrapper classes `_DispatchAutograd` and `_CombineAutograd`. Developers extending TokenSwitch can implement the raw buffer-writing methods `_dispatch` and `_combine` in subclasses, and inherit both modes from the base class. Notably, `topk_weights` receives no gradient, preserving the routing decision as metadata at a different byte width. This update paves the way for more efficient and flexible training of large-scale sparse models.
- Dispatch and combine are adjoint operations: backward of dispatch calls combine and vice versa.
- New API with optional `out` parameter: with `out` for efficient buffer reuse without autograd; without `out` for full autograd support.
- `topk_weights` receives no gradient, allowing mixed-precision routing metadata handling.
Why It Matters
Enables scalable gradient-based training of mixture-of-experts models with efficient memory reuse.