The router that spends its experts unevenly
A balancing bias changes which experts a token selects, and a shared expert must run for every token.
What’s going wrong
Original LLM Architecture lab. This is a single-token-batch, standard-library teaching reduction of fine-grained mixture-of-experts routing, not a reproduction of a distributed MoE kernel.
A fine-grained MoE splits the feed-forward layer into many small experts and routes each token to a few of them. A shared expert always runs, so common structure does not have to be relearned by every routed expert. DeepSeek-style balancing adds a bias to the router logits when choosing experts, but uses the original logits when weighting them. The starter ignores the bias and omits the shared expert. Repair both.
Read one JSON object with tokens, router, bias, top_k, shared and experts. tokens has T arrays of d numbers; router has E arrays of d numbers; bias has E numbers; shared has d numbers; experts has E arrays of d numbers. T is 0 to 6, d is 1 to 4, E is 2 to 6, and top_k is an integer from 1 to E. Every number is finite with at most three decimal places. Inputs satisfy the stated bounds. Use only the Python standard library and print one JSON object. Round every returned number with round(value, 6) and print 0.0 for any value that rounds to zero. Inputs satisfy this contract.
For each token x, compute logits[e] = sum(router[e][j] * x[j] for j in range(d)) and biased[e] = logits[e] + bias[e]. Select top_k experts by biased descending, breaking ties by the smaller expert index. Compute a stable softmax over the original logits of the selected experts only; these are the gate weights, in the same order as the selected list. The output is shared[j] + sum(weights[i] * experts[selected[i]][j]). Print {"results":[{"selected":[...],"weights":[...],"output":[...]},...]}. An empty token batch returns {"results":[]}.
Expected behavior
Select experts with the bias-adjusted logits, weight the selected experts with a softmax over their original logits, and add the shared expert to every token output.
Reproduction
Execution evidence
Simulated system · deterministic replay
Fixed router logits, biases and expert outputs. No training, all-to-all communication or GPU is used.
- 01Logits
Router scores [1.0, 0.9, 0.0] with bias [0.0, 0.5, 0.0].
- 02Selection
The biased scores [1.0, 1.4, 0.0] select expert 1 for top_k=1; the original logits still weight it.
- 03Starter
The starter selects expert 0 and omits the shared expert.
- 04Reference
The reference returns expert 1 and adds the shared output to the routed output.
Explain your repair before submitting
Which observation contradicts the intended behavior? Where does the code break that contract? Explain why your change fixes the cause and which new regression case would detect it.
This evidence describes the starter. Use Run to observe your current code; Submit checks additional regressions. No live model calls are made.
{"tokens":[[1,0]],"router":[[1,0],[0.9,0],[0,1]],"bias":[0,0.5,0],"top_k":1,"shared":[1,1],"experts":[[2,0],[4,0],[0,8]]}{"results":[{"selected":[1],"weights":[1.0],"output":[5.0,1.0]}]}{"tokens":[[1,0]],"router":[[1,0],[0,1]],"bias":[0,0],"top_k":1,"shared":[3,4],"experts":[[2,0],[0,2]]}{"results":[{"selected":[0],"weights":[1.0],"output":[5.0,4.0]}]}{"tokens":[[1,0]],"router":[[2,0],[1,0],[0,1]],"bias":[0,0,0],"top_k":2,"shared":[0,0],"experts":[[2,0],[4,0],[0,8]]}{"results":[{"selected":[0,1],"weights":[0.731059,0.268941],"output":[2.537883,0.0]}]}Before you begin
- Dot products, top-k selection and stable softmax
- The difference between a dense FFN and a routed mixture of experts
- Why load balancing matters when experts are distributed across devices
Need a nudge?
Reveal one investigation hint at a time. Try each clue before opening the next.
Read the report, then sign in to start your investigation.