DeepSeekMoE with Auxiliary-Loss-Free Load Balancing
DeepSeek-V3's feed-forward layers are DeepSeekMoE layers: a few shared experts that every token uses, many fine-grained routed experts of which each token uses Kr, and a sigmoid affinity per token and expert. To keep the routed experts evenly loaded without an auxiliary loss, each expert has a bias that is added to its affinity when choosing experts, and nowhere else. After each training step the bias of an overloaded expert goes down by γ and the bias of an underloaded one goes up by γ. Implement the router, the layer output and the bias update.
For a token with FFN input ut and routed-expert centroids ei:
si,t=Sigmoid(ut⊤ei),gi,t′={si,t,0,si,t+bi∈Topk({sj,t+bj},Kr)otherwise,gi,t=∑jgj,t′gi,t′
ht′=ut+∑i=1NsFFNi(s)(ut)+∑i=1Nrgi,tFFNi(r)(ut).
In this problem every expert is a plain linear map, FFN(x)=xW (V3's experts are SwiGLU networks; the routing is the same).
route(u, centroids, bias, k)
u: (T,d); centroids: (N,d), one row per routed expert; bias: (N,).
- Return
(idx, gates), both (T,k): idx lists the chosen experts in descending order of s+b (equal values: the lower index first), gates the matching gi,t.
moe_layer(u, centroids, bias, k, shared, routed)
shared: a list of Ns arrays (d,d) (possibly empty); routed: a list of N arrays (d,d).
- Return h′ of shape (T,d).
update_bias(bias, idx, n_experts, gamma)
idx: the selected experts of one training batch, any shape. Expert i's load ci is how many times it appears. The balanced load is the mean cˉ=(number of entries)/N.
- Return a new bias array: bi−γ if ci>cˉ, bi+γ if ci<cˉ, and bi unchanged if ci=cˉ.
Notes
- Do not modify the inputs.