Module 24 — Mixture of Experts (MoE)
Mixture of Experts (MoE) is a sparse neural network architecture where only a small subset of expert networks is activated for each input token. Instead of processing every token through the same Feed Forward Network (FFN), an MoE layer uses a router (gating network) to dynamically choose the most suitable experts.
This significantly increases model capacity while keeping the computational cost relatively constant.
Modern MoE architectures are used in Mixtral, DeepSeek-MoE, Switch Transformer, GLaM, Qwen-MoE, and other large-scale language models.
Topics
- Router
- Experts
- Sparse Activation
- Gating
1. Input Representation
The input hidden representation is
Formula
2. Expert Networks
Each expert is an independent Feed Forward Network.
Formula
where
-
-
= Number of Experts
3. Router Network
The router computes a score for every expert.
Formula
where
- = Router Weight Matrix
4. Gating Network
The router scores are converted into probabilities using Softmax.
Formula
Expanded,
where
- = Probability assigned to Expert
5. Sparse Activation
Instead of activating all experts, only the Top-k experts are selected.
Formula
Typical values
- Top-1 (Switch Transformer)
- Top-2 (Mixtral)
- Top-4 (Large MoE Models)
This greatly reduces computational cost.
6. Expert Computation
Each selected expert computes its own output.
Formula
7. Weighted Expert Output
The final output is the weighted combination of selected experts.
Formula
This is the core equation of Mixture of Experts.
8. Sparse MoE Output
Using only Top-k experts,
Formula
Only active experts contribute to the final output.
9. Load Balancing Loss
To prevent a few experts from receiving all tokens, MoE models use a load-balancing objective.
Formula
where
- = Fraction of tokens assigned to expert
- = Average routing probability
This encourages balanced utilization across experts.
10. Total Training Loss
The complete optimization objective is
Formula
where
- = Main task loss
- = Auxiliary load-balancing loss
- = Balancing coefficient
11. Matrix Dimensions
Suppose
- Hidden Dimension =
- Number of Experts =
Input
Router Weights
Gating Scores
Expert Output
Final Output
12. Complete MoE Pipeline
The complete computation is
13. Advantages of MoE
- Massive model capacity
- Sparse computation
- Faster inference
- Lower FLOPs per token
- Efficient scaling to billions or trillions of parameters
14. Applications
Mixture of Experts is used in
- Switch Transformer
- GLaM
- Mixtral 8x7B
- Mixtral 8x22B
- DeepSeek-MoE
- DeepSeek V2
- Qwen-MoE
- DBRX
- Grok MoE
- Large Language Models (LLMs)
Summary
| Concept | Formula |
|---|---|
| Router | |
| Gating Network |