Instead of nudging a balancing bias by a fixed step, quantile balancing solves for it: each expert reads off the score threshold that only its target share of tokens beats. Implement the per-micro-batch solve and the global-batch update loop.
Routing subtracts a per-expert bias from the scores: token goes to the experts with the largest .
qb_route(scores, beta, k) for one micro-batch, scores (T, N), beta (N,):
idx: per token, the experts with the largest , in descending order (ties: lower index first).ValueError if is not divisible by ).(idx, beta_local) with shapes (T, k) and (N,).qb_train(steps, k, ema=0.0)
steps: a list of global batches; each global batch is a list of micro-batch score arrays (T, N). Start with .qb_route), accumulating each micro-batch's expert counts and its . After the whole global batch: = mean of the over its micro-batches; ; then subtract the mean over experts so sums to zero."global_maxvio" (MaxVio of the counts summed over the global batch's micro-batches), "micro_maxvio" (mean over its micro-batches of each micro-batch's MaxVio), both rounded to 4 decimals, and "beta" (the final rounded to 4). MaxVio is .