Mixture of experts #
The facts behind the choices in NN.Spec.Layers.MoE, in four groups.
- Routing keeps exactly
min k Eexperts, and every kept expert beats every expert it displaces (card_kept,beats_of_kept), whenever the comparison of the scores is a strict weak order (StrictWeak). Real numbers satisfy this, and so do IEEE numbers other than NaN, on which+0and-0tie. - Which experts are kept depends only on how the scores compare (
kept_congr). Overℝthe softmax preserves every comparison, so routing on probabilities, as Mixtral does, keeps the experts with the largest logits (kept_softmax), and the gates are the softmax of the kept logits (getScalar_gatesVecSpec_softmax). Rounding breaks this with floats: in binary32 the logits0and2⁻³⁰have the same probability, so routing on probabilities keeps expert0and routing on logits keeps expert1. - Over
ℝthe dense layer, which runs every expert, equals Mixtral's sparse block, which runs only the kept ones (forwardSpec_eq_sparseForwardSpec). - A token's output depends only on that token, so a batch can be split across devices anywhere.
A capacity per device, as in GShard and Switch, breaks this: two tokens routed to the same
expert are both kept on two devices, but on one device the second is dropped
(
capacity_depends_on_devices). So does a balancing loss computed on each device's tokens (balance_depends_on_devices). Computed per work unit, a fixed set of tokens, both are functions of the unit again.
Routing #
The comparison of scores is a strict weak order: no score is above itself, > is
transitive, and whenever a > c every other score is below a or above c.
Instances For
Real scores are compared by a strict weak order.
An expert that beats another has the smaller rank.
Different experts have different ranks, so the ranks are 0, …, E - 1 in some order.
Routing keeps exactly min k E experts.
The list of kept experts has min k E entries.
Every kept expert beats every expert that is not kept.
Over ℝ: a kept expert's score is at least that of every expert it displaces, and larger
unless the displaced expert has the larger index.
Gates over the reals #
Over ℝ the softmax preserves every comparison, so routing on the probabilities keeps the
experts with the largest logits.
Over ℝ, the gate of expert e is its probability divided by the total probability of the
kept experts, or zero when e is not kept.
Over ℝ, the gates are the softmax of the logits of the kept experts, the experts with the
largest logits.
Over ℝ, with at least one expert kept, the gates are nonnegative and sum to one.
The dense layer is the sparse block #
One coordinate of the combined output, over ℝ.
Over ℝ, the dense layer computes exactly Mixtral's sparse block.
Capacity and balancing #
The capacity Switch gives each expert for a group of n tokens and E experts with capacity
factor one, ⌈n / E⌉.
Instances For
Two tokens routed to the first of two experts. On one device the capacity is one, so the second token is dropped; split over two devices, each device has capacity one and keeps its token.
Switch's balancing loss on a group of tokens, each given by its expert and the router's
probabilities: E times the sum over experts of the fraction of tokens sent to the expert times
its mean probability.
Instances For
Two tokens, each sent to the expert it prefers with probability 3/4. On one device the
balancing loss is 1; split over two devices, each device's loss is 3/2, and so is their
mean.