What is a Mixture of Experts?

Introduction

Project introduction

What is a Mixture of Experts?

Every transformer layer has a feed-forward network, a small two-layer MLP that each token passes through. A mixture-of-experts (MoE) layer replaces that one network with several copies, called experts, plus a router, which is a tiny linear layer that scores every expert for every token.

Each token is sent only to the k experts with the highest scores, often just 1 or 2 of 8 or more. The model therefore stores many experts' worth of parameters, but each token pays for only a few. Mixtral 8x7B, for example, stores about 47 billion parameters and uses about 13 billion per token.

The catch

Nothing forces the router to share the work. An expert that is picked a little more often early on gets more practice, becomes more useful, and gets picked even more. Shazeer et al. saw in 2017 that the router can collapse onto a few experts while the rest sit unused.

Real systems also give each expert a fixed capacity, so an overloaded expert has to turn tokens away. In this project you build the layer, watch this collapse happen in a tiny character language model, measure what it costs under a capacity limit, and fix it two ways. Collapse does not happen in every model, and this setup was chosen because it shows it reliably.

Each character visits one expert

8 experts, top-1
47+theACGx=3

The router scores all 8 experts for "4" and sends it to expert 4 only.

expert 0

0

expert 1

0

expert 2

0

expert 3

0

expert 4

1

expert 5

0

expert 6

0

expert 7

0

Routescore every expert
Dispatchtop-k experts only
Combineweight by the gates
Balancekeep every expert busy

What you will build

01

Routing

Top-k gates, then dispatch tokens to their experts and combine the results.

02

Limits

Count stored and used parameters, and give every expert a capacity.

03

Balance

The Switch balancing loss, the ST-MoE z-loss, and the bias method from DeepSeek-V3.

Start with the router

Pick the top-k experts for every token and turn their scores into gates.

Continue to Problem 1