Experts on different devices
A mixture of experts (MoE) layer replaces the MLP with many MLPs, the experts, and a router that sends each token to a few of them. Most of the model's parameters sit in the experts, but each token uses only a few, so the experts are the natural thing to spread over devices: expert parallelism (EP) gives each rank a share of the experts, keeps everything else as it was, and moves the tokens to the experts instead of the weights to the tokens.
The page runs the first MoE layer of OLMoE-1B-7B-0125 (layer 1 of 16): 64 experts, each a SwiGLU MLP 2,048 → 1,024 → 2,048 of 6.3 million parameters, eight chosen per token. Its experts are 1.5 GiB in float32; four ranks hold 16 each, 384 MiB, shown in the 64 experts on 4 ranks panel. Only this layer was read from the checkpoint, by HTTP byte ranges. Each rank is a process on this Mac's CPU with its own sequence of 128 tokens (the first 512 OLMoE tokens of SQuAD's Apollo program article), as data-parallel ranks have their own batches; it runs the layer's attention on them itself. Times come from the CPU run; the pattern of all-to-alls is what carries over to GPUs.
The router
The router is one linear layer, 2,048 → 64, kept whole on every rank, so each rank routes its own tokens without asking anyone. For a token , OLMoE takes a softmax over the 64 scores and keeps the eight largest probabilities as they are (it does not renormalise them):
The The router panel is the Dense tab's MoE column: a row of 64 cells per token, the chosen ones shaded by their weight, grouped and tinted by the rank that holds them, with a band from each chosen expert to the token's weighted sum. A token's eight experts are usually spread over several ranks, so most of its copies must travel.
Dispatch and combine: two all-to-alls
Each rank makes one copy of a token per chosen expert, sorts the copies by expert (so the copies for each rank are consecutive) and sends them with an all-to-all: rank sends rank the copies whose expert lives on , and every rank receives its experts' copies from all four. A first, small all-to-all tells every rank how many rows of each of its experts are coming, so it can size its buffers. The rank runs each of its experts once on all of that expert's rows, from every sender, and a second all-to-all, the first one reversed, sends every output back to where its token lives. There the eight outputs are weighted by the router and added.
A row is one hidden vector, 2,048 floats or 8 KiB. The All-to-all matrix gives the recorded rows from each rank to each rank; the diagonal stays on its rank. The combine moves exactly the transposed amounts. This is Megatron-LM's all-to-all token dispatcher; GShard's dispatch and combine are the same exchange written as einsums.
Load imbalance
Nothing forces the router to spread tokens evenly. With 512 tokens and eight copies each, an even split would give every expert 64 copies, but the recorded counts run from 7 copies for the idlest expert to 325 for the busiest, expert 6. By rank they run from 838 rows on rank 3 to 1,182 on rank 0, 15 % above the even 1,024, and rank 0 reached the combine last while the others waited in it (the The layer on 4 ranks time line). On this CPU its experts took 49 ms against 44–47 ms on the other ranks, a smaller spread than the rows'. The Load imbalance table reads the busiest and idlest experts and ranks from the real routing.
GShard and Switch Transformer give each expert a fixed capacity, copies for tokens, experts per token and experts, with a capacity factor such as 1.25, and drop the copies past it; the table counts what each factor would drop from this batch. Dropless systems such as MegaBlocks, and Megatron by default, drop nothing and pay with uneven work instead, as recorded here. Training adds a load-balancing loss, Switch Transformer's , where is the fraction of copies sent to expert and its mean router probability; OLMoE trains with .
The same layer, split
The split layer is the same layer: against transformers' OLMoE block with all 64 experts in one process, every token's eight experts are the same, the MoE output and the layer's output agree to float rounding, and every row a rank sent is a row its peer received. The strip at the bottom shows one token's eight weighted expert outputs, each from the rank that holds the expert, and their sum.
Where it came from
Shazeer et al. (2017) introduced the sparsely-gated MoE layer with thousands of experts spread across devices. GShard (Lepikhin et al., 2020) made expert parallelism an all-to-all over TPU cores with capacity limits; Switch Transformer (Fedus et al., 2021) routed each token to a single expert. DeepSpeed-MoE (Rajbhandari et al., 2022) and MegaBlocks (Gale et al., 2022) made training and serving MoEs efficient, the latter without dropping tokens. OLMoE (Muennighoff et al., 2024) is a fully open MoE. The Transformer Explainer's MoE tab shows the layer itself, and the Efficiency Explainer's DP attention tab runs the experts of another MoE expert-parallel during serving.
Papers
- GShard: Scaling Giant Models with Conditional Computation and Automatic Sharding Expert parallelism as an all-to-all dispatch and combine across devices, with expert capacity.
- Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer The sparsely-gated mixture-of-experts layer, a router choosing a few of many experts per token.
- Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity One expert per token, the capacity factor and the load-balancing loss used in training.
- OLMoE: Open Mixture-of-Experts Language Models The model whose first MoE layer is split on this page: 64 experts, eight per token.
- DeepSpeed-MoE: Advancing Mixture-of-Experts Inference and Training to Power Next-Generation AI Scale Expert parallelism combined with data and tensor parallelism for training and serving MoEs.
- MegaBlocks: Efficient Sparse Training with Mixture-of-Experts Dropless MoE: every token's copies computed, however uneven the experts' loads.