Efficiency

Sparse Mixture of Experts: Routing, Load Balancing & Expert Parallelism

Conditional computation: a router picks a few experts per token. You can increase total expert parameters while keeping activated expert FFN compute small, but distributed systems may pay in communication and scheduling.

status: publishedimportance: importantdifficulty 4/5math: undergraduateread: 20mlive demo
Editorial efficiency illustration of tokens routed sparsely into expert blocks with load imbalance and routing paths.

Concept Structure

Sparse Mixture of Experts: Routing, Load Balancing & Expert Parallelism

01Intuition

Start with the picture, metaphor, or geometric mechanism.

02Math

Make the objects explicit and connect them with notation.

03Code

Mirror the equations with runnable implementation details.

04Interactive Demo

Manipulate the mechanism and watch the idea respond.

5prerequisites
1next concepts
2related links

Learning map

Sparse Mixture of Experts: Routing, Load Balancing & Expert Parallelism
BeforeScaled Dot-Product Attention & Transformer LayersNow4/4 sections readyTryManipulate one control and predict the visible change.NextMoE Serving & Scheduling: Token Dispatch, All-to-All, Disaggregated Parallelism

Object flow

4/4 sections readyAsk about thisResearch room
ConceptSparse Mixture of Experts: Routing, Load Balancing & Expert ParallelismEfficiency
2 sources attachedLocal snapshot ready
concept:efficiency/mixture-of-experts

Conceptual Bridge

What should feel connected as you move through this page.

Carry inScaled Dot-Product Attention & Transformer Layers

Bring the mental model from Scaled Dot-Product Attention & Transformer Layers; this page will reuse it instead of restarting from zero.

Work hereSparse Mixture of Experts: Routing, Load Balancing & Expert Parallelism

Conditional computation: a router picks a few experts per token. You can increase total expert parameters while keeping activated expert FFN compute small, but distributed systems may pay in communication and scheduling.

Carry outMoE Serving & Scheduling: Token Dispatch, All-to-All, Disaggregated Parallelism

The next edge should feel earned: use the demo prediction here before following MoE Serving & Scheduling: Token Dispatch, All-to-All, Disaggregated Parallelism.

Test the linkManipulate one control and predict the visible change.Then continue to MoE Serving & Scheduling: Token Dispatch, All-to-All, Disaggregated Parallelism
01

01

Intuition

Build the mental picture first so the rest of the page has something to attach to.

Section prompt

Sparse Mixture of Experts (MoE) is a conditional-computation layer pattern that routes tokens to a small subset of experts.

Instead of running the same dense MLP for every token, you keep a large set of expert MLPs and a small router that decides which experts each token should use. That buys you:

  • Capacity: lots of total parameters (many experts).
  • Efficiency: only run kk experts per token (small activated compute).

But it also sells you new problems: routing skew can overload experts, create stragglers, or drop tokens, and distributed expert-parallel implementations can introduce all-to-all communication. In MoE, "sparsity" is as much a systems story as a modeling story.

02

02

Math

Translate the story into symbols, assumptions, and a derivation you can inspect.

Section prompt

Router probabilities

Let a token hidden state be hRdh\in\mathbb R^d. A linear router produces logits and probabilities:

z=Wrh,p(eh)=softmax(z)e.z = W_r h,\qquad p(e\mid h)=\mathrm{softmax}(z)_e.

Top-k gating (sparse activation)

Let S=TopK(p(h),k)S=\mathrm{TopK}(p(\cdot\mid h), k). Only experts in SS run:

MoE(h)=eSp~efe(h),p~e=p(eh)jSp(jh).\mathrm{MoE}(h)=\sum_{e\in S}\tilde p_e\,f_e(h),\qquad \tilde p_e = \frac{p(e\mid h)}{\sum_{j\in S} p(j\mid h)}.

Load balancing (avoid expert collapse)

In the cited MoE setups, routing can become imbalanced, so auxiliary losses and routing noise encourage balanced expert usage. A classic form uses:

  • fif_i: fraction of tokens assigned to expert ii
  • PiP_i: average gating probability for expert ii

and:

LLB=NEi=1NEfiPi.\mathcal L_{\text{LB}} = N_E\sum_{i=1}^{N_E} f_i\,P_i.

Expert capacity (selected is not always served)

Top-k routing only proposes token-expert assignments. A serving or training system may also cap each expert at a finite number of token slots for the batch. If expert ee can accept CeC_e assignments, then an assignment (t,e)(t,e) is served only while:

load(e)<Ce.\mathrm{load}(e) < C_e.

Once the expert is full, later assignments to that same expert overflow unless the implementation has an explicit rerouting or fallback rule. The important distinction is:

TopK(p(ht),k)chooses candidates; capacity chooses which candidates run.\mathrm{TopK}(p(\cdot\mid h_t), k)\quad\text{chooses candidates; capacity chooses which candidates run.}

The interactive demo uses a deliberately simple dispatch rule: process tokens in batch order, fill each expert's slots, and mark later assignments to full experts as overflowed. Real implementations can add different overflow, padding, or rerouting policies, but they still have to account for finite expert capacity.

03

03

Code

Keep the implementation aligned with the notation so the algorithm is legible.

Section prompt
import numpy as np

rng = np.random.default_rng(0)
E, d, T = 8, 16, 2000  # experts, hidden dim, tokens

W = rng.standard_normal((E, d))  # router weights
h = rng.standard_normal((T, d))  # token hidden states

z = h @ W.T
p = np.exp(z - z.max(axis=1, keepdims=True))
p = p / p.sum(axis=1, keepdims=True)

top = p.argmax(axis=1)  # top-1 routing
freq = np.bincount(top, minlength=E) / T  # f_i
P = p.mean(axis=0)                         # P_i

L_lb = E * float(np.sum(freq * P))
print("freq f_i:", np.round(freq, 3))
print("mean gate P_i:", np.round(P, 3))
print("load-balancing loss:", round(L_lb, 3))
04

04

Interactive Demo

Use direct manipulation to connect the explanation to a moving system.

Section prompt

Use the demo to predict whether a small batch's top-k token-expert assignments fit inside finite expert slots. The top-k candidates are visible before reveal; final loads and overflowed assignments are hidden until you commit to the capacity outcome. It is a teaching model, not a trained router or production load-balancing simulator.

Live Concept Demo

Explore Sparse Mixture of Experts: Routing, Load Balancing & Expert Parallelism

The stage is code-native and interactive. Use it to test the explanation against the mechanism.

difficulty 4/5undergraduatecode-aligned
Demo Prediction Checkpoint

Manipulate one control and predict the visible change.

Commit to what Sparse Mixture of Experts: Routing, Load Balancing & Expert Parallelism should make visible before reading the result.

After The First Pass

Turn the concept into an inspected object.

Once the invariant is visible in the intuition, math, code, and demo, use these panels to inspect the mechanism visually, check source support, practice the idea, and attach a grounded research question.

Mechanism Storyboard

See the idea move before the page explains it

Conditional computation: a router picks a few experts per token. You can increase total expert parameters while keeping activated expert FFN compute small, but distributed systems may pay in communication and scheduling.

Prediction open01 / Intuition
Editorial efficiency illustration of tokens routed sparsely into expert blocks with load imbalance and routing paths.
Prediction lens

Start with the picture, metaphor, or geometric mechanism.

Commit first

Before reading further, choose the kind of change Sparse Mixture of Experts: Routing, Load Balancing & Expert Parallelism should make visible.

Visual Inquiry

Make the image answer a mathematical question

Conditional computation: a router picks a few experts per token. You can increase total expert parameters while keeping activated expert FFN compute small, but distributed systems may pay in communication and scheduling.

4/4 stages readyLive demo connected
Prediction

Which visible object should carry the first intuition?

Commit first

Pick the cue that should make Sparse Mixture of Experts: Routing, Load Balancing & Expert Parallelism easier to reason about before the page gives the answer.

Source Grounding

Canonical references for the mechanism on this page.

paper · 2017Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts LayerShazeer et al.

Introduces sparsely gated experts and the conditional-computation motivation behind MoE layers.

Open source
paper · 2021Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient SparsityFedus, Zoph, and Shazeer

Grounds top-1 expert routing, capacity tradeoffs, and the practical scaling recipe for sparse transformer experts.

Open source

Claim Review

Conditional computation: a router picks a few experts per token. You can increase total expert parameters while keeping activated expert FFN compute small, but distributed systems may pay in communication and scheduling.

Status1 substantive review recorded

Claims without a substantive review badge still need exact source-support review.

Sources2 references

shazeer-2017-sparsely-gated-moe, fedus-2021-switch-transformers

Witnesses4 local objects

Use equation, code, and demo objects to check whether the source support is operational.

Substantively reviewedSparse MoE layers use a learned router to choose top-k experts per token, so total expert parameters can grow while active expert compute stays bounded by k; in the cited MoE/Switch designs, load-balancing losses and expert-capacity mechanisms manage skew, overload, dropped tokens, and dispatch/communication costs.Claim metadata: source checked

Shazeer defines trainable sparse/noisy top-k gating, many expert FFNs with only selected experts evaluated, and importance/load losses for imbalance. Fedus supports Switch top-1 routing, expert capacity/capacity-factor overflow and dropped tokens, f_i/P_i auxiliary load loss, and reduced-but-present dispatch/communication costs. Local witnesses instantiate router/top-k, load signals, and toy capacity overflow.

Sources: Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer, Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient SparsityReviewed routing/load/capacity mechanics only. Math/code/demo are toy local witnesses; no exact throughput, quality or specialization, all-to-all/expert-parallel implementation, scheduler optimality, serving latency/cost, or guarantee that MoE improves wall-clock performance.A bounded review summary is present; still check caveats and exact source scope.

Shazeer supports trainable sparse/top-k MoE gating: only selected experts run, enabling large total expert parameters with limited active compute, plus importance/load balancing for skew. Fedus supports Switch top-1 routing, expert capacity/capacity-factor overflow tradeoffs, dropped tokens, the f_i/P_i auxiliary load loss, and reduced-but-present dispatch/all-to-all communication costs.

Reviewer: codex+oracle+codex-5.3; reviewed 2026-05-08

Practice Loop

Try the idea before it explains itself

Conditional computation: a router picks a few experts per token. You can increase total expert parameters while keeping activated expert FFN compute small, but distributed systems may pay in communication and scheduling.

Readiness0/3 checks ready
Predict

Before touching the demo, predict one visible change that should happen in Sparse Mixture of Experts: Routing, Load Balancing & Expert Parallelism.

Hint 1

Reveal when your model needs a nudge.

Hint 2

Reveal when your model needs a nudge.

Hint 3

Reveal when your model needs a nudge.

Object research drawerClose
ConceptSparse Mixture of Experts: Routing, Load Balancing & Expert ParallelismEfficiency

Research Room

Attach the question to an exact object

Pick the concept, equation, source, code witness, claim, misconception, or demo state before asking for help. The handoff stays grounded to that object.
Next local actionNo local draft saved yet

Open the draft below to save one note and next action in this browser.

conceptEfficiency

Sparse Mixture of Experts: Routing, Load Balancing & Expert Parallelism

Anchored question

What is the smallest example that makes Sparse Mixture of Experts: Routing, Load Balancing & Expert Parallelism click without losing the math?

Local action draftNo local draft saved yetExpand only when ready to capture one local next action
Local action draft

This draft stays locally in this browser for concept:efficiency/mixture-of-experts.

No local draft saved.
Evidence to inspect
  • Source ids to inspect: shazeer-2017-sparsely-gated-moe, fedus-2021-switch-transformers
  • Definition, prerequisite, and contrast concept links
  • The equation or code witness that makes the concept operational
  • One demo state that shows the invariant instead of a slogan
What would resolve this
  • The learner can state the mechanism in their own words
  • The learner can name the prerequisite that would repair confusion
  • The learner can predict how the mechanism changes under one perturbation
Grounded AI handoff

I am working in Continuous Function's research reading room. Object: concept - Sparse Mixture of Experts: Routing, Load Balancing & Expert Parallelism Object key: concept:efficiency/mixture-of-experts Context: Efficiency Anchor id: concept/concept-notebook/efficiency/mixture-of-experts Open question: What is the smallest example that makes Sparse Mixture of Experts: Routing, Load Balancing & Expert Parallelism click without losing the math? Evidence to inspect: - Source ids to inspect: shazeer-2017-sparsely-gated-moe, fedus-2021-switch-transformers - Definition, prerequisite, and contrast concept links - The equation or code witness that makes the concept operational - One demo state that shows the invariant instead of a slogan What would resolve this: - The learner can state the mechanism in their own words - The learner can name the prerequisite that would repair confusion - The learner can predict how the mechanism changes under one perturbation Answer as a careful research tutor: stay source-grounded, separate verified evidence from assumptions, name the relevant math objects, and end with one next action.

Open source object
concept/concept-notebook/efficiency/mixture-of-experts concept:efficiency/mixture-of-experts