Overview

9 Dynamic routing with Mixture of Experts

Mixture of Experts is presented as a practical way to make language models more adaptive by routing different tokens to different specialized components at runtime. Rather than forcing every input through the same dense path, the model learns where to send each token so computation is used more selectively. The chapter situates this approach among other dynamic inference methods, contrasting it with early exiting and token dropping, and emphasizes that MoE is the most mature and widely adopted of the three, especially in modern frontier systems and increasingly in smaller-scale models.

The first project builds a two-expert MoE on SmolLM2-1.7B-Instruct by replacing selected MLP modules in the deeper layers with a custom block containing a frozen general expert, a trainable clinical expert, and a router. The model is trained with a mix of clinical extraction prompts and general conversation data so the router can learn to distinguish contexts without harming the base model’s overall behavior. Soft routing blends both experts during training, while hard routing switches to a binary choice at inference time, showing that the same architecture can either combine or sparsify expert usage depending on how the forward pass is implemented.

The second project reuses a previously fine-tuned clinical model as an expert, generalizes the MoE to support configurable top-k routing, and trains mostly the router before lightly adapting the expert tail to match the base model’s attention layers. This staged approach restores full performance on the clinical task while keeping general benchmark behavior stable or slightly improved, underscoring that expert specialization does not have to come at the cost of general capability. The chapter closes by connecting these experiments to sparse upcycling and Mixtral-style architectures, showing how the same underlying ideas scale from small instructional setups to large production MoEs.

The MoE block replaces the MLP module in selected transformer layers. A router receives the hidden states and assigns routing weights to two experts that process in parallel — expert_general, which holds the original MLP weights (frozen), and expert_clinical, the trainable copy. Attribute names in parentheses correspond directly to the model's code structure.
Two routing behaviors applied to the same token. Soft routing (left, top-k = 2) runs both experts in parallel and combines their outputs in a weighted sum — × 0.38 for expert_general, × 0.62 for expert_clinical. Hard routing (right, top-k = 1) selects expert_clinical via argmax and passes its output through directly.
Benchmark comparison across five general-knowledge tasks for the base model, MoE Soft, and MoE Hard. Delta values expressed in percentage points (pp) relative to the baseline.
Two views of the same top-k routing decision. Left: the router produces one logit score per expert for each token; the two highest scores per row (highlighted) are selected. Right: the same assignments reorganized by expert, showing which tokens each expert will process and their renormalized weights — for each token, those weights sum to 1 across its assigned experts. The example uses four experts rather than two so that top_k = 2 leaves half the experts idle per token. The loop in TrainableTopKMoE.forward performs exactly this regrouping.
Two-phase training strategy for the upcycled MoE. Phase 1 trains only the router across all 24 layers (lr = 1e-3) for three epochs — 98,304 parameters, 0.0034% of the model — reaching 90% schema compliance. Phase 2 reduces the router learning rate to lr = 1e-6 and unfreezes the expert_clinical tail layers 18–23 at lr = 1e-5 for one epoch, bringing trainable parameters to 10.35% and compliance to 100%.
Benchmark comparison across five general-knowledge tasks for the upcycled MoE, baseline vs top_k=2 vs top_k=1.

Summary

  • Dynamic inference techniques, like early exiting, token dropping, and Mixture of Experts, allow the model to make runtime decisions about what to compute. Of the three, MoE is the most mature and the only one with growing support in standard inference libraries.
  • An MoE architecture is built around three elements. Experts process information independently, the router decides which expert acts on each token, and the combined forward pass merges their outputs into a single response.
  • Building an MoE starting from a copy of the base model weights allows initializing the experts with already-acquired knowledge, instead of starting from scratch. This strategy, known as sparse upcycling, speeds up convergence.
  • Soft routing and hard routing are two inference behaviors that can be obtained on the same trained weights, by modifying only the forward method. The model's behavior depends both on its weights and on how the experts' outputs are combined.
  • The router does not work on raw tokens, but on their hidden states, which accumulate the context of everything that has occurred before in the sequence. That is why the same token can be sent to different experts depending on the context in which it appears.
  • The TrainableTopKMoE class generalizes the architecture to accept a configurable number of experts and a top-k parameter that controls how many of them are activated per token.
  • An MLP Module from a pre-trained model can be transplanted directly as an expert into an MoE architecture, without the need to retrain it from scratch. This opens the door to reusing existing specialized models as components of a larger system.
  • If the transplanted expert comes from a model with a different Attention Module than the base model, it might need a light adjustment to adapt to the tensors it receives. A light training of the layers closest to the output is usually enough to recover full performance.
  • Specializing an expert in a specific domain does not necessarily penalize the general capabilities of the model. General benchmarks of the MoE model built in this chapter are maintained or improved compared to the base model.

FAQ

What is dynamic inference, and how is it different from a standard LLM forward pass?Dynamic inference makes runtime decisions about what to compute, what to skip, or where to route each token. Unlike a standard LLM forward pass, which uses the full model for every token, dynamic inference adapts computation to the input.
What are the three main dynamic inference techniques discussed in this chapter?The chapter covers early exiting, token dropping, and Mixture of Experts (MoE). Early exiting stops computation when confidence is high, token dropping removes less useful tokens from later layers, and MoE routes tokens to specialized experts.
What is a Mixture of Experts architecture?An MoE replaces dense MLP modules with multiple specialized experts and a router. The router decides, token by token, which expert or experts should process the hidden states, and their outputs are combined into a single result.
What are the core components of an MoE block?The three core components are the experts, the router, and the combined forward pass. The experts process inputs, the router assigns weights or selections, and the forward pass merges expert outputs into one output tensor.
What is sparse upcycling in the context of MoE?Sparse upcycling is a way of converting a dense model into an MoE by duplicating already-trained dense modules instead of initializing experts randomly. This gives the new experts a strong starting point for specialization.
How is the two-expert MoE built in this chapter?The model keeps the original MLP as Expert 0, creates a deep copy as Expert 1, and adds a linear router that outputs two logits per token. The MLPs in selected layers are replaced with this MoE block.
Why does the chapter add noise to the router during training?Noise helps prevent the router from collapsing too early to one expert when both experts start with identical weights. It forces exploration so the clinical expert gets enough training signal to specialize.
What is the difference between soft routing and hard routing?Soft routing runs both experts and blends their outputs using router-generated probabilities. Hard routing makes a binary choice per token and sends each token to only one expert at inference time.
Why are only certain layers replaced with MoE blocks?The chapter modifies the deeper layers because they are more abstract and better suited for specialization. It also reduces memory and compute demands compared with replacing every layer.
What happens when a fine-tuned clinical expert is transplanted into the MoE?The router is trained to distribute tokens between the general base expert and the already-trained clinical expert. To make this work well, the expert tail is lightly fine-tuned so it adapts to the base model’s attention layers, which leads to stronger clinical performance without hurting general behavior.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Rearchitecting LLMs ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Rearchitecting LLMs ebook for free