Add identity experts to an MoE pool: experts that return their input unchanged and cost nothing. A token that picks some of them runs fewer real FFN experts, so compute per token varies while the top-k stays fixed. Implement the layer, the per-token compute statistics, and a simple bias controller that holds the average number of FFN experts per token at a target.
The pool has n_ffn FFN experts (indices 0 .. n_ffn-1) followed by n_zero identity experts (indices n_ffn .. n_ffn+n_zero-1).
zc_route(x, W_router, n_ffn, top_k, bias=None)
x is (T, d), W_router is (d, n_ffn + n_zero). Scores are over the whole pool.bias, a vector over the pool, zeros if None): each token takes the top_k largest, ties to the lower index.(idx, w, n_ffn_per_token): (T, top_k) int array, (T, top_k) float array, and a length-T int array counting how many selected experts are FFN experts.zc_forward(x, W_router, W1, W2, n_zero, top_k, bias=None)
W1 is (n_ffn, d, h) and W2 is (n_ffn, h, d); FFN expert computes , identity experts return .(T, d) array.compute_stats(n_ffn_per_token, top_k, target)
Return a dict with "mean", "std" (population, ddof=0), "min", "max", "gap_to_target" (mean minus target), all floats rounded to 4 decimals, and "histogram": a list of top_k + 1 ints, entry being the number of tokens that ran exactly FFN experts.
calibrate_bias(x, W_router, n_ffn, top_k, target, steps, u)
Start with a zero bias. One identity-expert bias value is shared by all identity experts (FFN experts keep bias 0). At each of steps steps: route with the current bias, record the mean FFN count , then set if , if , unchanged if equal. Return {"means": [...], "beta": beta} with every value rounded to 4 decimals.