Mixture of Experts Explained
{"anchor":"epistemic","axes":{"content":"Practitioner guide to Mixture of Experts covering architecture, training, fine-tuning, and serving tradeoffs","conte…{"filename_must_equal_uuid":true,"verbatim_body_from_source":false,"math_preserved_as_latex":false,"citation_graph_extracted":false,"hash_chain_intact":true}local graph
Mixture of Experts Explained
Section titled “Mixture of Experts Explained”Authors: Omar Sanseviero, Lewis Tunstall, Philipp Schmid, Sourab Mangrulkar, Younes B, Pedro Cuenca Publisher: Hugging Face Blog URL: huggingface.co/blog/moe Published: December 11, 2023 Source: HTML text extraction via agent-00-manual-ingest
Extracted Body
Section titled “Extracted Body”Mixture of Experts Explained Hugging Face Back to Articles Mixture of Experts Explained Published December 11, 2023 Update on GitHub Upvote 1128 +1122 Omar Sanseviero osanseviero Follow Lewis Tunstall lewtun Follow Philipp Schmid philschmid Follow Sourab Mangrulkar smangrul Follow Younes B ybelkada Follow Pedro Cuenca pcuenq Follow There is a second iteration (Feb 2026) of the blog post where we cover how the transformers library has built around MoEs to make them “first class citizens” of the library and the Hub. Here is the link to the post: Mixture of Experts (MoEs) in Transformers With the release of Mixtral 8x7B ( announcement , model card ), a class of transformer has become the hottest topic in the open AI community: Mixture of Experts, or MoEs for short. In this blog post, we take a look at the building blocks of MoEs, how they’re trained, and the tradeoffs to consider when serving them for inference. Let’s dive in! Table of Contents What is a Mixture of Experts? A Brief History of MoEs What is Sparsity? Load Balancing tokens for MoEs MoEs and Transformers Switch Transformers Stabilizing training with router Z-loss What does an expert learn? How does scaling the number of experts impact pretraining? Fine-tuning MoEs When to use sparse MoEs vs dense models? Making MoEs go brrr Expert Parallelism Capacity Factor and Communication costs Serving Techniques Efficient Training Open Source MoEs Exciting directions of work Some resources TL;DR MoEs: Are pretrained much faster vs. dense models Have faster inference compared to a model with the same number of parameters Require high VRAM as all experts are loaded in memory Face many challenges in fine-tuning , but recent work with MoE instruction-tuning is promising Let’s dive in! What is a Mixture of Experts (MoE)? The scale of a model is one of the most important axes for better model quality. Given a fixed computing budget, training a larger model for fewer steps is better than training a smaller model for more steps. Mixture of Experts enable models to be pretrained with far less compute, which means you can dramatically scale up the model or dataset size with the same compute budget as a dense model. In particular, a MoE model should achieve the same quality as its dense counterpart much faster during pretraining. So, what exactly is a MoE? In the context of transformer models, a MoE consists of two main elements: Sparse MoE layers are used instead of dense feed-forward network (FFN) layers. MoE layers have a certain number of “experts” (e.g. 8), where each expert is a neural network. In practice, the experts are FFNs, but they can also be more complex networks or even a MoE itself, leading to hierarchical MoEs! A gate network or router , that determines which tokens are sent to which expert. For example, in the image below, the token “More” is sent to the second expert, and the token “Parameters” is sent to the first network. As we’ll explore later, we can send a token to more than one expert. How to route a token to an expert is one of the big decisions when working with MoEs - the router is composed of learned parameters and is pretrained at the same time as the rest of the network. MoE layer from the Switch Transformers paper So, to recap, in MoEs we replace every FFN layer of the transformer model with an MoE layer, which is composed of a gate network and a certain number of experts. Although MoEs provide benefits like efficient pretraining and faster inference compared to dense models, they also come with challenges: Training: MoEs enable significantly more compute-efficient pretraining, but they’ve historically struggled to generalize during fine-tuning, leading to overfitting. Inference: Although a MoE might have many parameters, only some of them are used during inference. This leads to much faster inference compared to a dense model with the same number of parameters. However, all parameters need to be loaded in RAM, so memory requirements are high. For example, given a MoE like Mixtral 8x7B, we’ll need to have enough VRAM to hold a dense 47B parameter model. Why 47B parameters and not 8 x 7B = 56B? That’s because in MoE models, only the FFN layers are treated as individual experts, and the rest of the model parameters are shared. At the same time, assuming just two experts are being used per token, the inference speed (FLOPs) is like using a 12B model (as opposed to a 14B model), because it computes 2x7B matrix multiplications, but with some layers shared (more on this soon). Now that we have a rough idea of what a MoE is, let’s take a look at the research developments that led to their invention. A Brief History of MoEs The roots of MoEs come from the 1991 paper Adaptive Mixture of Local Experts . The idea, akin to ensemble methods, was to have a supervised procedure for a system composed of separate networks, each handling a different subset of the training cases. Each separate network, or expert, specializes in a different region of the input space. How is the expert chosen? A gating network determines the weights for each expert. During training, both the expert and the gating are trained. Between 2010-2015, two different research areas contributed to later MoE advancement: Experts as components : In the traditional MoE setup, the whole system comprises a gating network and multiple experts. MoEs as the whole model have been explored in SVMs, Gaussian Processes, and other methods. The work by Eigen, Ranzato, and Ilya explored MoEs as components of deeper networks. This allows having MoEs as layers in a multilayer network, making it possible for the model to be both large and efficient simultaneously. Conditional Computation : Traditional networks process all input data through every layer. In this period, Yoshua Bengio researched approaches to dynamically activate or deactivate components based on the input token. These works led to exploring a mixture of experts in the context of NLP. Concretely, Shazeer et al. (2017, with “et al.” including Geoffrey Hinton and Jeff Dean, Google’s Chuck Norris ) scaled this idea to a 137B LSTM (the de-facto NLP architecture back then, created by Schmidhuber) by introducing sparsity, allowing to keep very fast inference even at high scale. This work focused on translation but faced many challenges, such as high communication costs and training instabilities. MoE layer from the Outrageously Large Neural Network paper MoEs have allowed training multi-trillion parameter models, such as the open-sourced 1.6T parameters Switch Transformers, among others. MoEs have also been explored in Computer Vision, but this blog post will focus on the NLP domain. What is Sparsity? Sparsity uses the idea of conditional computation. While in dense models all the parameters are used for all the inputs, sparsity allows us to only run some parts of the whole system. Let’s dive deeper into Shazeer’s exploration of MoEs for translation. The idea of conditional computation (parts of the network are active on a per-example basis) allows one to scale the size of the model without increasing the computation, and hence, this led to thousands of experts being used in each MoE layer. This setup introduces some challenges. For example, although large batch sizes are usually better for performance, batch sizes in MOEs are effectively reduced as data flows through the active experts. For example, if our batched input consists of 10 tokens, five tokens might end in one expert, and the other five tokens might end in five different experts, leading to uneven batch sizes and underutilization . The Making MoEs go brrr section below will discuss other challenges and solutions. How can we solve this? A learned gating network (G) decides which experts (E) to send a part of the input: y = ∑ i = 1 n G ( x ) i E i ( x ) y = \sum_{i=1}^{n} G(x)_i E_i(x) y = i = 1 ∑ n G ( x ) i E i ( x ) In this setup, all experts are run for all inputs - it’s a weighte