Mixture of experts (MoE) is a machine learning technique where multiple expert networks (learners) are used to divide a problem space into homogeneous regions.[1] MoE represents a form of ensemble learning.[2]
Basic theory
MoE always has the following components, but they are implemented and combined differently according to the problem being solved:
Experts , each taking the same input , and producing outputs .
A weighting function (also known as a gating function) , which takes input and produces a vector of outputs .
is the set of parameters. The parameter is for the weighting function.
Given an input , the mixture of experts produces a single output by combining according to the weights in some way.
Both the experts and the weighting function are trained by minimizing some loss function, generally via gradient descent. There is much freedom in choosing the precise form of experts, the weighting function, and the loss function.
Meta-pi network
The meta-pi network, reported by Hampshire and Waibel,[3] uses as the output. The model is trained by performing gradient descent on the mean-squared error loss . The experts may be arbitrary functions.
In their original publication, they were solving the problem of classifying phonemes in speech signal from 6 different Japanese speakers, 2 females and 4 males. They trained 6 experts, each being a "time-delayed neural network"[4] (essentially a multilayered convolution network over the mel spectrogram). They found that the resulting mixture of experts dedicated 5 experts for 5 of the speakers, but the 6th (male) speaker does not have a dedicated expert, instead his voice was classified by a linear combination of the experts for the other 3 male speakers.
Adaptive mixtures of local experts
The adaptive mixtures of local experts[5][6] uses a gaussian mixture model. Each expert simply predicts a gaussian distribution, and totally ignores the input. Specifically, the -th expert predicts that the output is , where is a learnable parameter. The weighting function is a linear-softmax function:The mixture of experts predict that the output is distributed according to the probability density function:It is trained by maximal likelihood estimation, that is, gradient ascent on . The gradient for the -th expert is
and the gradient for the weighting function is
For each input-output pair , the weighting function is changed to increase the weight on all experts that performed above average, and decrease the weight on all experts that performed below average. This encourages the weighting function to learn to select only the experts that make the right predictions for each input.
The -th expert is changed to make its prediction closer to , but the amount of change is proportional to . This has a Bayesian interpretation. Given input , the prior probability that expert is the right one is , and is the likelihood of evidence . So, is the posterior probability for expert , and so the rate of change for the -th expert is proportional to its posterior probability.
In words, the experts that, in hindsight, seemed like the good experts to consult, are asked to learn on the example. The experts that, in hindsight, were not, are left alone.
The combined effect is that the experts become specialized: Suppose two experts are both good at predicting a certain kind of input, but one is slightly better, then the weighting function would eventually learn to favor the better one. After that happens, the lesser expert is unable to obtain a high gradient signal, and becomes even worse at predicting such kind of input. Conversely, the lesser expert can become better at predicting other kinds of input, and increasingly pulled away into another region. This has a positive feedback effect, causing each expert to move apart from the rest and take care of a local region alone (thus the name "local experts").
Hierarchical MoE
Hierarchical mixtures of experts[7][8] uses multiple levels of gating in a tree. Each gating is a probability distribution over the next level of gatings, and the experts are on the leaf nodes of the tree. They are similar to decision trees.
For example, a 2-level hierarchical MoE would have a first order gating function , and second order gating functions and experts . The total prediction is then .
Variants
The mixture of experts, being similar to the gaussian mixture model, can also be trained by the expectation-maximization algorithm, just like gaussian mixture models. Specifically, during the expectation step, the "burden" for explaining each data point is assigned over the experts, and during the maximization step, the experts are trained to improve the explanations they got a high burden for, while the gate is trained to improve its burden assignment. This can converge faster than gradient ascent on the log-likelihood.[8][9]
Instead of performing a weighted sum of all the experts, in hard MoE,[11] only the highest ranked expert is chosen. That is, . This can accelerate training and inference time.[12]
The experts can use more general forms of multivariant gaussian distributions. For example,[7] proposed , where are learnable parameters. In words, each expert learns to do linear regression, with a learnable uncertainty estimate.
One paper proposed mixture of softmaxes for autoregressive language modelling.[16] Specifically, consider a language model that given a previous text , predicts the next word . The network encodes the text into a vector , and predicts the probability distribution of the next word as for an embedding matrix . In mixture of softmaxes, the model outputs multiple vectors , and predict the next word as , where is a probability distribution by a linear-softmax operation on the activations of the hidden neurons within the model. The original paper demonstrated its effectiveness for recurrent neural networks. This was later found to work for Transformers as well.[17]
Deep learning
The previous section described MoE as it was used before the era of deep learning. After deep learning, MoE found applications in running the largest models, as a simple way to perform conditional computation: only parts of the model are used, the parts chosen according to what the input is.[18]
The earliest paper that applies MoE to deep learning dates back to 2013,[19] which proposed to use a different gating network at each layer in a deep neural network. Specifically, each gating is a linear-ReLU-linear-softmax network, and each expert is a linear-ReLU network. Since the output from the gating is not sparse, all expert outputs are needed, and no conditional computation is performed.
The key design desideratum for MoE in deep learning is to reduce computing cost. Consequently, for each query, only a small subset of the experts should be queried. This makes MoE in deep learning different from classical MoE. In classical MoE, the output for each query is a weighted sum of all experts' outputs. In deep learning MoE, the output for each query can only involve a few experts' outputs. Consequently, the key design choice in MoE becomes routing: given a batch of queries, how to route the queries to the best experts.
Sparsely-gated MoE layer
The sparsely-gated MoE layer,[20] published by researchers from Google Brain, uses feedforward networks as experts, and linear-softmax gating. Similar to the previously proposed hard MoE, they achieve sparsity by a weighted sum of only the top-k experts, instead of the weighted sum of all of them. Specifically, in a MoE layer, there are feedforward networks, and a gating network . The gating network is defined by , where is a function that keeps the top-k entries of a vector the same, but sets all other entries to . The addition of noise helps with load balancing.
The choice of is a hyperparameter that is chosen according to application. Typical values are . The version is also called the Switch Transformer. The original Switch Transformer was applied to a T5 language model.[21]
As demonstration, they trained a series of models for machine translation with alternating layers of MoE and LSTM, and compared with deep LSTM models.[22] Table 3 shows that the MoE models used less inference time compute, despite having 30x more parameters.
Vanilla MoE tend to have issues of load balancing: some experts are consulted often, while other experts rarely or not at all. To encourage the gate to select each expert with equal frequency (proper load balancing) within each batch, each MoE layer has two auxiliary loss functions. This is improved by [21] into a single auxiliary loss function. Specifically, let be the number of experts, then for a given batch of queries , the auxiliary loss for the batch isHere, is the fraction of time where expert is ranked highest, and is the fraction of weight on expert . This loss is minimized at , precisely when every expert has equal weight in all situations.
Routing
In sparsely-gated MoE, only the top-k experts are queried, and their outputs are weighted-summed. There are other methods.[23]
In Hash MoE,[24] routing is performed deterministically by a hash function, fixed before learning begins. For example, if the model is a 4-layered Transformer, and input is a token for word "eat", and the hash of "eat" is , then the token would be routed to the 1st expert in layer 1, 4th expert in layer 2, etc. Despite its simplicity, it achieves competitive performance as sparsely gated MoE with .
In soft MoE, suppose in each batch, each expert can process queries, then there are queries that can be assigned per batch. Now for each batch of queries , the soft MoE layer computes an array , such that is a probability distribution over queries, and the -th expert's -th query is .[25] However, this does not work with autoregressive modelling, since the weights over one token depends on all other tokens'.[26]
Other approaches include solving it as a constrained linear programming problem,[27] making each expert choose the top-k queries it wants (instead of each query choosing the top-k experts for it),[28] using reinforcement learning to train the routing algorithm (since picking an expert is a discrete action, like in RL),[29] etc.
Capacity factor
Suppose there are experts in a layer. For a given batch of queries , each query is routed to one or more experts. For example, if each query is routed to one expert as in Switch Transformers, and if the experts are load-balanced, then each expert should expect on average queries in a batch. In practice, the experts cannot expect perfect load balancing: in some batches, one expert might be underworked, while in other batches, it would be overworked.
Since the inputs cannot move through the layer until every expert in the layer has finished the queries it is assigned, load balancing is important. As a hard constraint on load balancing, there is the capacity factor: each expert is only allowed to process up to queries in a batch.[23] found to work in practice.
Applications to transformer models
MoE layers are used in the largest transformer models, for which learning and inferring over the full model is too costly. They are typically sparsely-gated, with sparsity 1 or 2. In Transformer models, the MoE layers are often used to select the feedforward layers (typically a linear-ReLU-linear network), appearing in each Transformer block after the multiheaded attention. This is because the feedforward layers take up an increasing portion of the computing cost as models grow larger. For example, in the Palm-540B model, 90% of parameters are in its feedforward layers.[30]
A trained Transformer can be converted to a MoE by duplicating its feedforward layers, with randomly initialized gating, then trained further. This is a technique called "sparse upcycling".[31]
There are a large number of design choices involved in Transformer MoE that affect the training stability and final performance. The OLMoE report describes these in some detail.[32]
As of 2023[update], models large enough to use MoE tend to be large language models, where each expert has on the order of 10 billion parameters. Other than language models, Vision MoE[33] is a Transformer model with MoE layers. They demonstrated it by training a model with 15 billion parameters. MoE Transformer has also been applied for diffusion models.[34]
A series of large language models from Google used MoE. GShard[35] uses MoE with up to top-2 experts per layer. Specifically, the top-1 expert is always selected, and the top-2th expert is selected with probability proportional to that experts' weight according to the gating function. Later, GLaM[36] demonstrated a language model with 1.2 trillion parameters, each MoE layer using top-2 out of 64 experts. Switch Transformers[21] use top-1 in all MoE layers.
The NLLB-200 by Meta AI is a machine translation model for 200 languages.[37] Each MoE layer uses a hierarchical MoE with two levels. On the first level, the gating function chooses to use either a "shared" feedforward layer, or to use the experts. If using the experts, then another gating function computes the weights and chooses the top-2 experts.[38]
MoE large language models can be adapted for downstream tasks by instruction tuning.[39]
In December 2023, Mistral AI released Mixtral 8x7B under Apache 2.0 license. It is a MoE language model with 46.7B parameters, 8 experts, and sparsity 2. They also released a version finetuned for instruction following.[40][41]
In March 2024, Databricks released DBRX. It is a MoE language model with 132B parameters, 16 experts, and sparsity 4. They also released a version finetuned for instruction following.[42][43]
Further reading
Before deep learning era
McLachlan, Geoffrey J.; Peel, David (2000). Finite mixture models. Wiley series in probability and statistics applied probability and statistics section. New York Chichester Weinheim Brisbane Singapore Toronto: John Wiley & Sons, Inc. ISBN978-0-471-00626-8.
^Baldacchino, Tara; Cross, Elizabeth J.; Worden, Keith; Rowson, Jennifer (2016). "Variational Bayesian mixture of experts models and sensitivity analysis for nonlinear dynamical systems". Mechanical Systems and Signal Processing. 66–67: 178–200. Bibcode:2016MSSP...66..178B. doi:10.1016/j.ymssp.2015.05.009.
^Yang, Zhilin; Dai, Zihang; Salakhutdinov, Ruslan; Cohen, William W. (2017-11-10). "Breaking the Softmax Bottleneck: A High-Rank RNN Language Model". arXiv:1711.03953 [cs.CL].
^Narang, Sharan; Chung, Hyung Won; Tay, Yi; Fedus, William; Fevry, Thibault; Matena, Michael; Malkan, Karishma; Fiedel, Noah; Shazeer, Noam (2021-02-23). "Do Transformer Modifications Transfer Across Implementations and Applications?". arXiv:2102.11972 [cs.LG].
^Bengio, Yoshua; Léonard, Nicholas; Courville, Aaron (2013). "Estimating or Propagating Gradients Through Stochastic Neurons for Conditional Computation". arXiv:1308.3432 [cs.LG].
^Eigen, David; Ranzato, Marc'Aurelio; Sutskever, Ilya (2013). "Learning Factored Representations in a Deep Mixture of Experts". arXiv:1312.4314 [cs.LG].
^Shazeer, Noam; Mirhoseini, Azalia; Maziarz, Krzysztof; Davis, Andy; Le, Quoc; Hinton, Geoffrey; Dean, Jeff (2017). "Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer". arXiv:1701.06538 [cs.LG].
^Zhou, Yanqi; Lei, Tao; Liu, Hanxiao; Du, Nan; Huang, Yanping; Zhao, Vincent; Dai, Andrew M.; Chen, Zhifeng; Le, Quoc V.; Laudon, James (2022-12-06). "Mixture-of-Experts with Expert Choice Routing". Advances in Neural Information Processing Systems. 35: 7103–7114. arXiv:2202.09368.
^Bengio, Emmanuel; Bacon, Pierre-Luc; Pineau, Joelle; Precup, Doina (2015). "Conditional Computation in Neural Networks for faster models". arXiv:1511.06297 [cs.LG].