Implement top-k mixture-of-experts routing from precomputed expert outputs. For token t and expert e, xt is the token vector, we is column e of w_router, and st,e is the router score. Superscript T denotes transpose.
st,e=xtTwe.Select k experts by descending score, breaking ties by lower expert index. Let et,j be the expert at selected position j and αt,j its weight; j and r range over the selected experts.
αt,j=∑r=1kexp(st,et,r)exp(st,et,j).For expert output ut,e and shared output utshared, the routed result yt is
yt=utshared+j=1∑kαt,jut,et,j.For T tokens, fe is the fraction selecting expert e and me is its mean probability under softmax over all E experts. The indicator is one when its condition holds and zero otherwise.
fe=T1t=1∑T1[e is selected for t]. me=T1t=1∑Tsoftmax(st)e.Return a dictionary of PyTorch tensors: output with shape (T, Dout), selected_experts and selected_weights with shape (T, k), and token_fraction and probability_mass with shape (E,). These contain y, the selected zero-based indices, their weights, f, and m, respectively. selected_experts is int64; all other results match x's dtype, and all results stay on x's device.
Input: x = [[1,0],[0,1]], w_router = [[1,0,1],[0,1,1]], expert_outputs = [[[10,0],[0,0],[0,10]],[[0,0],[5,5],[1,1]]], shared_output = [[1,1],[2,2]], k = 2
Output: {"output":[[6,6],[5,5]],"selected_experts":[[0,2],[1,2]],"selected_weights":[[0.5,0.5],[0.5,0.5]],"token_fraction":[0.5,0.5,1],"probability_mass":[0.288841,0.288841,0.422319]}
Explanation: Both tokens tie their own specialist with expert 2, so stable index order selects [0,2] and [1,2]. Equal selected weights average those outputs before the shared output is added.
Input: x = [[2]], w_router = [[1,0.5]], expert_outputs = [[[7],[3]]], shared_output = [[1]], k = 1
Output: {"output":[[8]],"selected_experts":[[0]],"selected_weights":[[1]],"token_fraction":[1,0],"probability_mass":[0.731059,0.268941]}
Input: x = [[0]], w_router = [[0,0]], expert_outputs = [[[2],[4]]], shared_output = [[1]], k = 2
Output: {"output":[[4]],"selected_experts":[[0,1]],"selected_weights":[[0.5,0.5]],"token_fraction":[1,1],"probability_mass":[0.5,0.5]}
Stable sorting preserves the original index order for tied scores.
torch.gather selects values using per-token indices.
torch.scatter can mark selected experts in a Boolean mask.
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: number
Implement top-k mixture-of-experts routing from precomputed expert outputs. For token t and expert e, xt is the token vector, we is column e of w_router, and st,e is the router score. Superscript T denotes transpose.
st,e=xtTwe.Select k experts by descending score, breaking ties by lower expert index. Let et,j be the expert at selected position j and αt,j its weight; j and r range over the selected experts.
αt,j=∑r=1kexp(st,et,r)exp(st,et,j).For expert output ut,e and shared output utshared, the routed result yt is
yt=utshared+j=1∑kαt,jut,et,j.For T tokens, fe is the fraction selecting expert e and me is its mean probability under softmax over all E experts. The indicator is one when its condition holds and zero otherwise.
fe=T1t=1∑T1[e is selected for t]. me=T1t=1∑Tsoftmax(st)e.Return a dictionary of PyTorch tensors: output with shape (T, Dout), selected_experts and selected_weights with shape (T, k), and token_fraction and probability_mass with shape (E,). These contain y, the selected zero-based indices, their weights, f, and m, respectively. selected_experts is int64; all other results match x's dtype, and all results stay on x's device.
Input: x = [[1,0],[0,1]], w_router = [[1,0,1],[0,1,1]], expert_outputs = [[[10,0],[0,0],[0,10]],[[0,0],[5,5],[1,1]]], shared_output = [[1,1],[2,2]], k = 2
Output: {"output":[[6,6],[5,5]],"selected_experts":[[0,2],[1,2]],"selected_weights":[[0.5,0.5],[0.5,0.5]],"token_fraction":[0.5,0.5,1],"probability_mass":[0.288841,0.288841,0.422319]}
Explanation: Both tokens tie their own specialist with expert 2, so stable index order selects [0,2] and [1,2]. Equal selected weights average those outputs before the shared output is added.
Input: x = [[2]], w_router = [[1,0.5]], expert_outputs = [[[7],[3]]], shared_output = [[1]], k = 1
Output: {"output":[[8]],"selected_experts":[[0]],"selected_weights":[[1]],"token_fraction":[1,0],"probability_mass":[0.731059,0.268941]}
Input: x = [[0]], w_router = [[0,0]], expert_outputs = [[[2],[4]]], shared_output = [[1]], k = 2
Output: {"output":[[4]],"selected_experts":[[0,1]],"selected_weights":[[0.5,0.5]],"token_fraction":[1,1],"probability_mass":[0.5,0.5]}
Stable sorting preserves the original index order for tied scores.
torch.gather selects values using per-token indices.
torch.scatter can mark selected experts in a Boolean mask.
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: number