Mixture of experts #
A mixture-of-experts layer scores every expert for each token, keeps the k best, and adds the
outputs of the kept experts weighted by gates. Implementations leave three choices open that change
the bits of the result, and this file makes each of them definite.
- Ties. Expert
jbeats expertiwhen its score is larger, or when neither score is larger andjhas the smaller index. Expertiis kept when fewer thankexperts beat it (kept), so ties go to the lower index, as inargmax. With totally ordered scores (no NaN) this keepsmin k Eexperts. With NaN among the scoresbeatscan cycle: for[0, NaN, 1]every expert is beaten once, sok = 1keeps none andk = 2keeps all three. The test needs only the comparison>and a count, so a device can decide it exactly for every entry at once.torch.topkdocuments that the indices of tied elements may vary across invocations. - Gates. As in Mixtral, the router's logits go through a softmax, the
klargest probabilities are kept, and the kept ones are divided by their sum (gatesVecSpec). Overℝthe experts kept are those with the largest logits and the gates are the softmax of their logits (NN.Proofs.Models.MoE). With floats, rounding can make two different logits equally probable, so there the probabilities decide. - Order. The gated outputs are added in increasing expert index, starting from zero
(
combineSpec), the order in which Mixtral's sparse block loops over its experts. Every expert runs on every token and an expert that is not kept has gate zero, so overℝthis dense form equals the sparse form that runs only the kept experts (sparseForwardSpec).
forwardSpec is the layer on one token. A layer over many tokens applies it to each, so a token's
output does not depend on the other tokens in its batch, and a batch can be split anywhere.
Capacity limits and batch-wide balancing losses break that independence;
NN.Proofs.Models.MoE shows what they cost.
Routing #
One for the entries of a vector of scores that kept k keeps and zero for the others.
Instances For
topKMaskVecSpec along the last axis of a tensor of any shape. A scalar is a row of one
entry, kept when 0 < k.
Instances For
The layer #
Mixtral's gates for one token: the softmax of the router's logits, kept for the experts
topKMaskVecSpec keeps (the k largest probabilities when none is NaN) and divided by the sum of
the kept probabilities, and zero for the other experts. With k = 0 nothing is kept and the
division is 0 / 0.
Instances For
The gated sum of the experts' outputs for one token, Σₑ gatesₑ * outputsₑ, added in
increasing expert index starting from zero.
Instances For
A mixture-of-experts layer on one token x: the router's logits give the gates
(gatesVecSpec), every expert runs on x, and their outputs are combined (combineSpec).
Instances For
Mixtral's sparse block on one token: only the kept experts run; each output is multiplied by
its probability divided by the sum of the kept probabilities, and the products are added into zero
in increasing expert index, as the block's loop of index_add_ over experts does. The sum of the
kept probabilities is taken in increasing index; Mixtral sums them in decreasing order, which gives
the same bits for k ≤ 2.