Gating Networks › Routing Score Computation

Michael BrenndoerferUpdated January 3, 20269 min read
Language AI HandbookAll chapters
Part 33 of 67Mixture of Experts
Chapter 241Gating Networks › Routing Score Computation
Reading section 3 of 11 · Routing Score Computation · 9 minSection 3 of 11 · 9 min

Book contents

Section 3 of 11 · 9 min read

Gating Networks › Routing Score ComputationOne section · Sections in this chapter3 / 11 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Raw routing logits from the linear projection must be converted into routing weights that determine how much each expert contributes to the output. The logits can be any real number, positive or negative, and they don't have any inherent probabilistic meaning. This transformation from arbitrary scores to real weights happens through a softmax function, creating a probability distribution over experts that we can interpret and use.

Softmax Gating

The softmax function is the standard tool for converting a vector of arbitrary real numbers into a probability distribution. In the context of routing, it turns our expert logits into normalized routing weights:

p(ei∣x)=exp⁡(G(x)i)∑j=1nexp⁡(G(x)j)p(e_i|x) = \frac{\exp(G(x)_i)}{\sum_{j=1}^{n} \exp(G(x)_j)}

where:

  • p(ei∣x)p(e_i|x): the probability of selecting expert ii for input xx
  • G(x)iG(x)_i: the raw routing score (logit) for expert ii
  • exp⁡(G(x)i)\exp(G(x)_i): the exponential function applied to the logit, so the value is positive
  • ∑j=1nexp⁡(G(x)j)\sum_{j=1}^{n} \exp(G(x)_j): the sum of exponentials across all nn experts, serving as the normalizing constant

Let's see what this formula does. The exponential function serves two purposes: first, it ensures all values are positive (since probabilities cannot be negative); second, it amplifies differences between logits. If one expert has a logit of 3 and another has 1, the exponential makes the first about e2≈7.4e^2 \approx 7.4 times larger, not just 2 points higher. This amplification makes the router's preferences more decisive.

The denominator is a normalizing constant. This keeps all probabilities sum to exactly 1. This normalization is needed because we want the routing weights to stand for a proper probability distribution.

Out[3]:
Visualization
Bar chart showing raw logit values for 6 experts.
Raw routing logits for six experts before softmax transformation.
Bar chart showing softmax probabilities for 6 experts.
Softmax probabilities showing how exponential amplification makes Expert 2's slight advantage decisive.

These routing probabilities p(ei∣x)p(e_i|x) stand for the router's "confidence" that expert ii is appropriate for input xx. Higher probabilities indicate stronger preference, and we can compare probabilities across experts to understand the router's relative confidence in each option.

In a soft MoE (which we introduced conceptually in the sparse models chapter), the final output would be a weighted combination of all expert outputs:

y=∑i=1np(ei∣x)⋅Ei(x)y = \sum_{i=1}^{n} p(e_i|x) \cdot E_i(x)

where:

  • yy: the combined output of the MoE layer
  • p(ei∣x)p(e_i|x): the routing probability for expert ii
  • Ei(x)E_i(x): the output computed by expert ii for input xx

However, this soft combination requires computing all expert outputs, which defeats the purpose of sparsity. If we must run every expert for every token, we gain no computational savings from having multiple specialized experts rather than one large one.

Hard Routing with Top-K Selection

Practical MoE systems use hard routing to obtain the computational benefits of sparsity: only the top-KK experts (by routing probability) are computed. The next chapter covers Top-K routing in detail, but the basic idea is needed to understand how the router functions in practice.

The process works in four steps:

  1. Compute all routing probabilities via softmax
  2. Select the KK highest-scoring experts
  3. Renormalize selected probabilities to sum to 1
  4. Compute only those KK experts

For example, with 8 experts and K=2K=2, only 2 experts run per token. This gives roughly 75% computational savings in the expert portion of the layer. The routing weights for selected experts become:

p~(ei∣x)=p(ei∣x)∑j∈TopKp(ej∣x)\tilde{p}(e_i|x) = \frac{p(e_i|x)}{\sum_{j \in \text{TopK}} p(e_j|x)}

where:

  • p~(ei∣x)\tilde{p}(e_i|x): the renormalized weight for selected expert ii
  • p(ei∣x)p(e_i|x): the original softmax probability for expert ii
  • TopK\text{TopK}: the set of indices corresponding to the KK experts with highest scores
  • ∑j∈TopKp(ej∣x)\sum_{j \in \text{TopK}} p(e_j|x): the sum of probabilities for the selected experts, used as a normalizing constant

This renormalization ensures the weighted combination of selected experts produces properly scaled outputs. Without renormalization, if the two selected experts had probabilities 0.3 and 0.2, they would sum to only 0.5, effectively scaling down the output. Renormalization adjusts them to approximately 0.6 and 0.4. This keeps the output magnitude remains appropriate.

Out[4]:
Visualization
Bar chart showing softmax probabilities for 8 experts.
Full softmax probabilities across all 8 experts.
Bar chart showing only top-2 experts selected.
Top-2 selected experts with probabilities for non-selected experts set to zero.
Bar chart showing renormalized weights for selected experts.
Renormalized weights so selected experts sum to 1.0.

Temperature Scaling

Some implementations add temperature scaling to the softmax to give additional control over routing behavior:

p(ei∣x)=exp⁡(G(x)i/τ)∑j=1nexp⁡(G(x)j/τ)p(e_i|x) = \frac{\exp(G(x)_i / \tau)}{\sum_{j=1}^{n} \exp(G(x)_j / \tau)}

where:

  • p(ei∣x)p(e_i|x): the temperature-scaled probability for expert ii
  • G(x)iG(x)_i: the raw routing logit
  • τ\tau: the temperature parameter that controls the sharpness of the distribution
  • exp⁡(G(x)i/τ)\exp(G(x)_i / \tau): the exponentiated scaled logit; using τ<1\tau < 1 amplifies differences, making the distribution sharper

This concept relates to the decoding temperature we covered in Part XXVIII, where temperature controls the randomness of text generation. The principle is the same: temperature modulates how peaked or flat the probability distribution is.

Lower temperatures sharpen the distribution, pushing probability mass toward the highest-scoring expert and approaching hard assignment to a single expert. Higher temperatures soften the distribution, spreading probability more evenly and approaching uniform routing where every expert has roughly equal chance of selection.

Out[5]:
Visualization
Bar chart showing sharp routing distribution at low temperature.
Temperature T=0.5: Sharp distribution approaching hard assignment.
Bar chart showing standard routing distribution.
Temperature T=1.0: Standard softmax distribution.
Bar chart showing softer routing distribution.
Temperature T=2.0: Softer distribution with more even spread.
Bar chart showing very soft routing distribution.
Temperature T=5.0: Very soft distribution approaching uniform routing.

Temperature controls the "confidence" of routing decisions across different training phases. Early in training, higher temperatures encourage exploration across experts, preventing the model from committing too strongly to particular routing patterns before it has learned enough. Later, lower temperatures can encourage more decisive routing, letting the model use the specialization it has developed.