MaxVio (maximal violation) is the standard read-out of MoE load balance: how far the busiest expert's load sits above the mean load, as a fraction of the mean. The same routing can look balanced at one scope and badly skewed at another, so it has to be computed at the scope you care about. Implement it at four scopes from a global batch's routing decisions.
maxvio(counts)
counts: loads along the last axis (any leading shape). with the mean over all entries of the last axis, zeros included. Return a float for 1-D input, otherwise an array of the leading shape.expert_counts(idx, num_experts)
idx: int array (..., L, k), the experts chosen by each of tokens. Return the int array (..., num_experts) of assignment counts (every one of a token's choices counts once), summed over the last two axes.maxvio_scopes(idx, num_experts, ep_size)
idx: (M, B, L, k): a global batch of micro-batches, each holding sequences of tokens.ep_size expert-parallel ranks in contiguous blocks: expert lives on rank ( divisible by ). A rank's load is the sum of its experts' loads."sequence": [mean, max] of MaxVio over the sequences, each from its own expert counts;"micro_batch": [mean, max] of MaxVio over the micro-batches;"global": MaxVio of the expert counts summed over the whole global batch;"rank": [mean, max] over the micro-batches of the MaxVio of the rank loads.