bugger.
All challenges/Pythonhardllm-sparse-experts-v1
THE INVESTIGATION

Build a sparse MoE feed-forward block

Route each token to its top-k SwiGLU experts and combine selected outputs.

LLM ArchitectureArchitecture builds

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

01 Different tokens choose different experts
{"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]]}
02 No tokens means zero expert loads
{"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":[]}
03 Ties choose the lower expert ID
{"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.

    The bug is intentional. Run the starter code to see it fail, then work toward a fix. Difficulty is provisional.
    Read-only preview
    Ready when you are
    Every fix starts with a failing test.

    Read the report, then sign in to start your investigation.