Unbiased Gradient Estimation with Balanced Assignments for Mixtures of Experts

Wouter Kool, Chris J. Maddison, Andriy Mnih

TL;DR

Training Mixture of Experts (MoE) models under hardware capacity constraints typically relies on heuristic, biased gradient estimators that struggle when the underlying data is inherently imbalanced across experts. This paper proposes two mathematically unbiased estimators—one using importance-weighted random token skipping and another sampling balanced assignments via an extended Gumbel-Matching distribution. Surprisingly, the simple "skip-and-reweight" estimator outperforms the more complex balanced-matching approach, matching the performance of unconstrained routing by avoiding extreme importance-sampling variance.

What Problem Does This Paper Address?

Large-scale Mixture of Experts (MoE) architectures achieve high capacity without proportional compute costs by routing individual inputs in a minibatch to specific "expert" sub-networks. However, efficient execution on parallel accelerators (such as GPUs and TPUs) requires each expert to handle a fixed, uniform batch size (capacity $c = n/k$).

Existing solutions either drop tokens that exceed an expert's capacity or enforce balanced assignments using heuristic, non-probabilistic routines (e.g., linear programming assignment in BASE Layers or auxiliary balance losses in Switch Transformers). These methods introduce gradient bias and often degrade performance when the optimal data-to-expert allocation is naturally non-uniform. The authors aim to place MoE routing on firm probabilistic footing by developing estimators that respect expert capacity while delivering provably unbiased gradient estimates.

Methods

The paper frames routing within an importance-sampled REINFORCE framework and develops two stochastic capacity-aware sampling procedures:

  1. The "Skip" Estimator (Sample Skip IW):
  2. Datapoints independently choose experts according to the router proposal $q(z|x)$.
  3. If an expert receives more than capacity $c$ items, excess datapoints are randomly dropped (subsampled uniformly).
  4. Crucially, retained datapoints assigned to expert $j$ are upweighted by $\frac{n_j}{\min(n_j, c)}$, compensating for the probability of being discarded and keeping the minibatch gradient strictly unbiased.
  5. The $n \times k$ Gumbel-Matching Estimator (Gumbel-Matching IW):
  6. Extends the square Gumbel-Matching distribution to non-square $n \times k$ assignments via the Gumbel-Max trick over a constrained bipartite matching problem.
  7. Solved via a primal cycle-canceling algorithm using Floyd-Warshall ($O(k^3)$ per improvement step, faster than Hungarian $O(n^3)$ when $k \ll n$).
  8. Because exact marginals are intractable, the authors derive exact conditional probabilities $q_\theta(z_i | x, G_{-i})$ (conditioning on Gumbel perturbations for all other datapoints) to compute tractable, unbiased importance weights.
  9. Sinkhorn Balancing:
  10. As an alternative to hard sampling constraints, the authors use non-square Sinkhorn iterations on the routing probabilities $p_\theta(z|x)$ to balance assignments in expectation before sampling.

Datasets

  • Discontinuous 1D Synthetic Benchmark: Evaluated on a 1D regression task defined by: $$y = \mathbf{1}_{x < 0.5}(0.8x - 0.2) + \mathbf{1}_{x \ge 0.5}(-2.0x + 2.0)$$ with 100 points sampled uniformly $x \sim \text{Uniform}(-1, 1)$ with Gaussian label noise $\epsilon \sim \mathcal{N}(0, 0.1^2)$.

  • Role: Designed so that the optimal allocation across two linear experts is heavily imbalanced (75% to one expert, 25% to the other), directly testing how estimators handle capacity limits when the underlying task demands unequal routing. No existing standard benchmarks were used.

Key Insights and Ideas

  • Variance beats utilization: Intuitively, perfectly filling expert capacities (Gumbel-Matching) should be better than throwing data away (skipping). However, forcing balanced assignments forces datapoints to occasionally route to highly improbable experts. This leads to tiny proposal probabilities $q(z|x)$, causing massive importance weights $p(z|x) / q(z|x)$ that blow up gradient variance.
  • Skipping is surprisingly effective if reweighted: The simple skip estimator acts as zero-weighting dropped tokens while fairly upweighting the survivors. It completely matches the unconstrained ideal baseline (Sample), whereas naive skipping without importance weighting (Sample Skip) fails.
  • Pitfalls of differentiable gating: Standard differentiable gating (multiplying expert outputs by gate probabilities with an auxiliary load-balancing loss) fails when the true problem is imbalanced. Too much balance loss (e.g., 0.1) forces equal expert usage, destroying accuracy.

Key Results

  • Success Metric: Reaching Mean Squared Error (MSE) $< 0.02$ within 10,000 steps of Adam ($\text{lr} = 0.1$).
  • REINFORCE Skip Estimator: Sample Skip IW was the only constrained estimator to match the unconstrained Sample baseline across sampling temperatures $\tau \ge 1.0$, reaching MSE $< 0.01$. Unweighted Sample Skip failed to reach the threshold.
  • Gumbel-Matching: Struggled at lower temperatures due to extreme importance-weight variance, only converging when $\tau$ was large enough ($\tau \ge 10$) to make the proposal distribution near-uniform.
  • Differentiable Gating: Only succeeded in a narrow low-temperature regime when paired with a small auxiliary balance loss (weight 0.01–0.03) and importance weighting; omitting importance weights led to complete training failure.

Limitations and Future Work

  • Toy-Scale Evaluation: Experiments were restricted to a 1D synthetic regression problem with $k=2$ experts and $n=100$ tokens.
  • Scalability to Deep MoEs: While the authors outline how skipping can be combined with skip-connections or "no-token-left-behind" routing across multiple layers, neither estimator was tested on large transformer architectures (e.g., Switch Transformer, GShard) or real-world NLP/vision datasets.
  • Computational Overhead: Solving cycle-canceling Gumbel-Matching and calculating conditional distributions per layer in a deep network introduces non-negligible wall-clock overhead compared to heuristic Top-1/Top-2 routing.

Summary updated

BibTeX
@inproceedings{kool2021unbiased,
  author = {Wouter Kool and Chris J. Maddison and Andriy Mnih},
  title = {{Unbiased Gradient Estimation with Balanced Assignments for Mixtures of Experts}},
  booktitle = {ICBINB @ NeurIPS},
  year = {2021},
  url = {https://openreview.net/forum?id=Hvfva7l1tcj},
}

Loading related content...

Figure