Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 24

Mixture-of-Experts: Scaling Models Efficiently

Recommended resources

Introduction

Welcome to Module 4, where we explore the cutting-edge architectural choices that define modern Large Language Models. In the previous module, you built a solid foundation by understanding the core Transformer, its architectural variants (encoder-only, decoder-only), and the training objectives (MLM vs. CLM) that give them their distinct capabilities.

As previewed, our first lesson in this module dives into the Mixture-of-Experts (MoE) architecture. This is a pivotal innovation that addresses a fundamental question in scaling AI: How can we dramatically increase a model's knowledge and capacity (by adding more parameters) without a proportional increase in the computational cost to run it? Models like Mixtral 8x7B, Llama 4, and reportedly GPT-4, all leverage this powerful idea.

Today, your learning outcome is to describe the Mixture-of-Experts (MoE) architecture and its benefits for scaling model size while maintaining computational efficiency. We will cover what MoE is, how it works at a technical level, and the challenges involved in training such models.


The Core Idea: Decoupling Parameters from Computation

At its heart, MoE is a clever architectural trick. In a standard Transformer block, every token is processed by the same Feed-Forward Network (FFN). This FFN layer accounts for a significant portion of the model's parameters and computation.

The MoE architecture replaces this single, dense FFN layer with two new components:

  1. A Router (or gating network).
  2. A collection of smaller FFNs, called "Experts".

For each token, the router dynamically selects a small subset of experts (e.g., 2 out of 8) to process it. The other experts remain inactive. This is called sparse activation.

Transformer vs. Mixture of Experts Architecture Comparison
A comparison between a standard Transformer decoder block and an MoE-based one. The key change is replacing the single Feed-Forward Network with a Router that selects from multiple Experts.

This design has a profound implication: you can add a vast number of experts, massively increasing the model's total parameter count, but the computational cost (FLOPs) for inference only depends on the few active experts. You get the benefit of a larger model's capacity with the inference speed closer to that of a much smaller model.

Let's watch a short video that reinforces this core concept and presents the empirical evidence for its effectiveness.

Stanford CS336 Language Modeling from Scratch | Spring 2025 | Lecture 4: Mixture of experts

This segment from a Stanford lecture on LLMs clearly explains the MoE concept and its primary benefit: achieving better performance for the same computational budget by increasing parameters without increasing FLOPs.

Watch from 01:37 to 05:54. Focus on the visual comparison between a dense model and a sparse MoE model, and absorb the key takeaway from the graphs: at the same FLOP count, MoE models consistently achieve lower training loss (i.e., better performance).


How MoE Works: The Routing Mechanism

Now that you understand the "what" and "why," let's dive into the "how." The router is the most critical component, as it's responsible for making the sparse selection happen. The most common and successful approach is called Top-K Routing.

Given your background in implementing ML models, we can walk through this at a code-level intuition. The process for a single token involves a few clear steps:

  1. Get Logits: The token's embedding (the output from the self-attention layer) is passed through a simple linear layer. The output of this layer is a vector of "logits," with one logit for each expert. These logits represent the router's confidence that an expert is a good choice for this token.
  2. Select Top-K: The router identifies the k highest logit values and their corresponding indices. These are the "winning" experts for this token.
  3. Create Sparse Weights: A new vector is created, filled with -infinity. The k logit values are copied into this vector at their original indices.
  4. Calculate Gating Weights: A softmax function is applied to this sparse vector. The -infinity values become zero, and the k selected logits are turned into a probability distribution that sums to 1. These are the final "gating weights."
  5. Compute Weighted Output: The token's embedding is passed only to the k selected experts. Their individual outputs are then multiplied by their respective gating weights and summed together. This weighted sum is the final output of the MoE layer.

The following resource provides a step-by-step guide with PyTorch code that perfectly illustrates this process.

makeMoE: Implement a Sparse Mixture of Experts Language Model from Scratch

This Hugging Face community blog, 'makeMoE', is excellent for understanding the implementation details. We'll focus on the sections that build the router and the full MoE layer.

Read the following three sections: 'Creating an Expert module': Notice that an expert is just a standard MLP. 'Top-k Gating Intuition through an Example': Follow the code snippets closely. This is the core logic of the router we just discussed. 'Creating a sparse Mixture of Experts module': Pay attention to the forward method in the SparseMoE class. See how it uses a mask to ensure that only the selected experts are computed for each token, which is the key to efficiency.

Test your understanding!

Imagine an MoE layer with 8 experts (num_experts=8) and Top-2 routing (top_k=2). If the router's linear layer produces the following logits for a token: [0.1, -0.5, 1.2, 0.8, -1.0, 0.3, 1.5, -0.2].

  1. Which two experts will be selected?
  2. After the softmax is applied to the sparse logits, what does this tell you about how the outputs of these two selected experts will be combined?
Show answer
  1. The two experts with the highest logits will be selected. The logits are 1.5 (index 6) and 1.2 (index 2). So, Expert 6 and Expert 2 are selected.
  2. The softmax converts the logits [1.5, 1.2] into gating weights. Since 1.5 is higher than 1.2, Expert 6 will receive a higher weight than Expert 2. The final output of the MoE layer will be a weighted sum: (weight_6 * output_6) + (weight_2 * output_2), where weight_6 > weight_2.

The Challenge of Training: Load Balancing

The Top-K routing mechanism introduces a significant training challenge: load imbalance. Without any intervention, the router might quickly learn to favor a few experts, sending most or all tokens to them. The other experts would receive no data, never get trained, and become "dead." This defeats the purpose of having a large number of experts. This is often called the "rich get richer" problem.

To combat this, MoE models are trained with an auxiliary loss function. This is an additional term added to the main loss (e.g., cross-entropy) that penalizes imbalanced expert utilization. It encourages the router to distribute tokens as evenly as possible across all available experts, ensuring they all learn effectively.

The Stanford lecture provides a clear explanation of this critical training detail.

Stanford CS336 Language Modeling from Scratch | Spring 2025 | Lecture 4: Mixture of experts

This clip explains the challenge of load balancing and introduces the auxiliary loss function used to solve it. This is arguably the most important trick for successfully training MoE models.

Watch the segment from 48:12 to 51:30. Focus on the intuition behind the balancing loss: it's a mathematical trick to push the router away from over-relying on a single 'superstar' expert and to utilize all of its parameters effectively.


Modern MoE Innovations: Shared and Fine-Grained Experts

As researchers have pushed MoE architectures further, they've introduced more refinements. Two notable ones, popularized by models from DeepSeek AI and now seen in models like Llama 4, are:

  • Shared Experts: Some knowledge might be useful for processing all tokens (e.g., basic syntactic rules). Instead of forcing every expert to learn this independently, some models include one or two "shared experts" that every token passes through, in addition to the sparsely selected ones.
  • Fine-Grained Experts: Rather than having a few large experts, why not have many more, much smaller experts? This allows for more specialized knowledge to be stored across the model without ballooning the parameter count. For instance, instead of 8 experts of size X, you might have 64 experts of size X/8.

These innovations are about finding the optimal trade-off between specialization, redundancy, and parameter efficiency.

Mixture of Experts: How LLMs get bigger without getting slower

To see how these concepts come together in a real-world model, this segment from another excellent explainer video discusses the architectural choices in recent MoE models like Mixtral and DeepSeek.

Watch from 20:15 to 23:05. Pay attention to how the video contrasts Mixtral's architecture (8 experts, k=2) with DeepSeek's refinements (more, smaller experts + a shared expert). This shows the practical application of the concepts we've discussed.


Conclusion

You have now unpacked one of the most important architectural trends in modern LLMs. The ability to scale model capacity while managing computational costs is a critical frontier, and MoE is the leading solution.

Key Takeaways:

  • Mixture-of-Experts (MoE) is an architecture that replaces the dense FFN layer in a Transformer with a router and multiple expert FFNs.
  • Its primary benefit is computational efficiency through sparse activation: it allows for a massive number of total parameters, but only a small fraction (the top K experts) are used for any given token. This provides the capacity of a very large model with inference costs closer to a smaller one.
  • The router uses a Top-K mechanism to select experts and calculate weights for combining their outputs.
  • A key training challenge is load imbalance, which is solved by adding an auxiliary loss to encourage the router to use all experts more evenly.
  • The main drawback is high memory (VRAM) requirement, as all experts must be loaded into memory even if they are not active.

What's Next?

In our next lesson, we will continue exploring modern LLM optimizations by turning our attention back to a fundamental component of the Transformer: positional information. You'll learn about Rotary Positional Embeddings (RoPE), a more elegant and powerful method for encoding sequence order than the absolute sinusoidal encodings we saw in the original Transformer. This technique is another cornerstone of nearly all current state-of-the-art models.