In expert-choice routing the roles flip: instead of each token picking its top- experts, each expert picks the tokens it scores highest, up to a fixed capacity. Every expert gets exactly the same load, but a token may get several experts or none, and whether a token is picked depends on the other tokens in the batch, including later ones. Implement the routing, the combine step and a measurement of that dependence.
expert_choice(logits, capacity_factor)
logits: (T, N). Scores are the softmax of each token's logits across the experts.capacity_factor is the average number of experts per token."capacity" (, int), "index" ((N, C) int array of token ids), "gate" ((N, C) float array, the score of each selected token), "experts_per_token" ((T,) int array) and "unrouted" (int, the number of tokens no expert picked).ec_combine(x, W, index, gate)
x: (T, d); W: (N, d, d_out), expert is the linear map . Return the (T, d_out) array whose row is over every expert that selected token (zeros for unrouted tokens).prefix_leakage(logits, capacity_factor, prefix_len)
(T, N) logits, and separately route only the first prefix_len rows with the same capacity_factor (so the capacity is recomputed from prefix_len). Return the share of the first prefix_len tokens whose set of experts differs between the two routings, as a float.