Build a sparse MoE feed-forward block
Route each token to its top-k SwiGLU experts and combine selected outputs.
What’s going wrong
Original LLM Architecture lab. Implement the missing behavior in solve(data) and keep the supplied JSON interface. Use Python’s standard library.
Input x contains token rows, router is a D×E matrix, experts contains one SwiGLU weight object per expert, and k is in [1,E]. Select by score descending then expert ID ascending. Compute softmax over selected scores and use it to mix only the chosen expert outputs. Output selected (IDs per token), loads (token count per expert) and output rows. Return empty token outputs with zero loads when x is empty.
Small, well-formed JSON inputs and standard-library Python arithmetic. Keep full precision during computation. The supplied output wrapper rounds results to six decimal places.
Expected behavior
Route each token to its top-k SwiGLU experts and combine selected outputs.
Reproduction
{"x":[[2,0],[0,3]],"router":[[1,0,0],[0,1,0]],"k":1,"experts":[{"gate":[[1,0],[0,1]],"up":[[1,0],[0,1]],"down":[[1,0],[0,1]]},{"gate":[[1,0],[0,1]],"up":[[1,0],[0,1]],"down":[[2,0],[0,2]]},{"gate":[[1,0],[0,1]],"up":[[1,0],[0,1]],"down":[[3,0],[0,3]]}]}{"selected":[[0],[1]],"loads":[1,1,0],"output":[[3.523188,0],[0,17.146334]]}{"x":[],"router":[[1,0,0],[0,1,0]],"k":2,"experts":[{"gate":[[1,0],[0,1]],"up":[[1,0],[0,1]],"down":[[1,0],[0,1]]},{"gate":[[1,0],[0,1]],"up":[[1,0],[0,1]],"down":[[2,0],[0,2]]},{"gate":[[1,0],[0,1]],"up":[[1,0],[0,1]],"down":[[3,0],[0,3]]}]}{"selected":[],"loads":[0,0,0],"output":[]}{"x":[[1,1]],"router":[[0,0,0],[0,0,0]],"k":2,"experts":[{"gate":[[1,0],[0,1]],"up":[[1,0],[0,1]],"down":[[1,0],[0,1]]},{"gate":[[1,0],[0,1]],"up":[[1,0],[0,1]],"down":[[2,0],[0,2]]},{"gate":[[1,0],[0,1]],"up":[[1,0],[0,1]],"down":[[3,0],[0,3]]}]}{"selected":[[0,1]],"loads":[1,1,0],"output":[[1.096588,1.096588]]}Before you begin
- Python functions, lists and dictionaries
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.