liveBook · Manning

Mixture of experts

As explained in Transformers in Action

A mixture-of-experts model routes inputs among specialized subnetworks called experts, typically activating or combining only a selected subset for each input.

A mixture-of-experts (MoE) model increases capacity without using every parameter for every token. Instead of sending each token through the same feed-forward network (FFN), it selects a small subset of specialized expert networks. This conditional computation keeps active computation lower than in a comparably large dense model. section 3.5 section 3.5.1

In a transformer block, an MoE layer usually replaces the FFN after self-attention. It contains multiple expert FFNs and a gating network, or router. The router scores experts for a token’s hidden representation and selects the top k, commonly one or two. Selected scores can then be normalized to weight the chosen experts. section 3.5.1

Listing 3.8 Top-k gating function for sparse MoE
class NativeGate(BaseGate):   #1
    def __init__(self, d_model, num_expert, world_size, top_k=2, gate_bias=True):
        super().__init__(num_expert, world_size)  #2
        self.gate = nn.Linear(d_model, self.tot_expert, bias=gate_bias)
        self.top_k = top_k

    def forward(self, inp, return_all_scores=False):
        gate = self.gate(inp)   #3

        gate_top_k_val, gate_top_k_idx = torch.topk(  #4
            gate, k=self.top_k, dim=-1, largest=True, sorted=False
        )

        gate_top_k_val = gate_top_k_val.view(-1, self.top_k)  #5
        gate_score = F.softmax(gate_top_k_val, dim=-1)

        self.set_loss(torch.zeros(1, requires_grad=True).to(inp.device))

        if return_all_scores:   #6
            return gate_top_k_idx, gate_score, gate
        return gate_top_k_idx, gate_score

The PyTorch gate projects input representations to expert scores, selects expert indices with torch.topk, then applies softmax to the selected scores. It implements routing, not the expert FFNs themselves. section 3.5.1

The trade-off is between total capacity and computation per token: many experts can exist, but only a few run for each token. A GShard-style top-2 gate adds load balancing and random routing to the simpler top-k gate. section 3.5.1

How Transformers in Action covers it

How deep it goes
Explains FFN replacement and top-k routing, then shows a PyTorch gate that returns selected experts and scores.
Examples
Shows an MoE architecture diagram, a PyTorch top-k gate, and a GShard-style top-2 gating listing.
What it assumes
Assumes familiarity with transformer blocks and feed-forward networks while explaining the router and dense-versus-sparse contrast.
What you can do after
Build it from scratch: implement a PyTorch top-k gate that selects expert indices and normalizes their scores.
How it connects
Connects mixture of experts to transformer blocks, self-attention, feed-forward networks, conditional computation, and dense models.

Also explained in

Compare how 4 books explain mixture of experts →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Transformers in Action. The section links point to the parts of the book it draws on.