Top-K Routing › Visualizing Routing Patterns

Michael BrenndoerferNovember 15, 20255 min read
Language AI HandbookAll chapters
Part 33 of 67Mixture of Experts
Chapter 242Top-K Routing › Visualizing Routing Patterns
Reading section 9 of 12 · Visualizing Routing Patterns · 5 minSection 9 of 12 · 5 min

Book contents

Section 9 of 12 · 5 min read

Top-K Routing › Visualizing Routing PatternsOne section · Sections in this chapter9 / 12 ↓
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

Abstract descriptions of routing behavior become much more intuitive when visualized. Let's examine what the routing patterns look like in practice for a sequence of tokens, and what these patterns tell us about how the MoE layer is using its expert capacity.

Out[24]:
Visualization
Heatmap showing sparse routing patterns where each token activates exactly 2 of 8 experts.
Heatmap of top-2 routing weights for a 16-token sequence across 8 experts. Each row is a token and each column is an expert. Colored cells indicate the two selected experts for that token, with color intensity proportional to the normalized gating weight. White cells indicate unselected experts receiving zero weight, showing the sparsity that makes MoE efficient.

The heatmap reveals the sparsity pattern of top-K routing directly. Each token (row) activates exactly 2 experts (colored cells), with the color intensity showing the routing weight. White cells represent experts that receive zero weight for that token, and in a model with 8 experts, 75% of the cells are white for any given token. This sparse activation is what makes MoE computationally efficient: instead of all 8 experts processing every token, only 2 do.

Looking at the pattern across tokens, you can also observe how different tokens prefer different expert combinations. Some experts may appear more frequently (showing they specialize in common token types), while others appear less frequently (showing they specialize in rarer but important token types). In a well-trained MoE model, this heatmap would reveal a structured semantic organization: similar tokens would tend to activate similar pairs of experts.

Let's also examine how tokens are distributed across experts, which is a key diagnostic for detecting load imbalance:

Out[25]:
Visualization
Bar chart showing uneven token distribution across 8 experts.
Token assignment counts per expert for the 16-token sample sequence. The red dashed line shows the perfectly uniform distribution (4 tokens per expert for K=2 across 16 tokens and 8 experts). Deviations from this line indicate load imbalance, which the next chapter addresses with auxiliary balancing losses.

This distribution reveals a common challenge with naive top-K routing: without explicit encouragement, tokens do not distribute evenly across experts. Some experts receive many more tokens than others, while some may receive very few. This load imbalance is a necessary issue that the next chapter addresses directly. The red dashed line shows what perfectly uniform distribution would look like: with 16 tokens, K=2, and 8 experts, the ideal distribution would give exactly 4 tokens to each expert. Deviations from this represent wasted capacity: overloaded experts may need to drop tokens, and underloaded experts sit idle.

Out[26]:
Visualization
Histogram of top-2 routing weights across all tokens, showing peaks near 0.5 and 1.0 with a red dashed line marking the equal-weighting threshold.
Histogram of top-2 normalized routing weights across 500 randomly generated tokens. The distribution shows two characteristic peaks: one near 0.5 showing equal splits between two similarly-scored experts, and one near 1.0 showing near-winner-take-all decisions. This bimodal pattern reflects the diversity of routing confidence across different token types.

The weight distribution histogram provides another view of routing behavior. In this example with random router logits, the distribution is roughly symmetric around 0.5: when logits are random, the gap between the top two is equally likely to favor either direction. In a trained model, the distribution would reflect learned routing patterns: tokens where one expert clearly dominates would cluster near 1.0, while ambiguous tokens would cluster near 0.5.