KatInterview

Sparse Expert Layers: Routing and Cross-Device Sharding

Machine LearningModel answer
MediumModel answerMachine Learningmixture-of-expertsexpert-parallelismdistributed-training

Last updated

Describe how a sparse top-k expert feed-forward layer processes a token batch, and how placing experts on different devices changes communication, balance, and memory.

Your team wants to scale a transformer's parameter count without scaling per-token compute at the same rate, and is considering replacing the dense feed-forward block with a sparse expert layer.

Explain how such a layer works and how its experts are spread across accelerators. Follow one batch of tokens from the moment it enters the layer until the layer's output is produced.

Assumptions. Use a feed-forward expert layer with top-k routing as your running example. The number of experts, the value of k, the capacity policy, and the hardware layout are not given, so choose concrete values and state them.

Your answer should cover:

  • The architecture: what an expert is, what the router computes, and how the outputs are merged.
  • The device-level view: how experts are partitioned, and what data moves between devices.
  • What happens when some experts receive more tokens than they can handle.
  • Auxiliary losses or mechanisms that keep usage balanced.
  • Communication and memory costs, and how the answer differs for training throughput versus serving latency.
  • Common failure modes and trade-offs.

You may want to ask yourself: are all experts on one device or spread out? Can a token visit more than one expert? Is the target training throughput or serving latency?

Hints

Hint 1

Keep two ideas apart: the model definition (router, experts, weighted sum) and the system layout (which device holds which expert). The second is what creates the communication pattern.

Hint 2

Think of the layer as a pipeline: score tokens, pick top-k experts, send tokens to the devices holding those experts, compute, send results back, then combine with the router weights. Count how many all-to-all exchanges that implies.

Hint 3

Tokens are not spread evenly by default. Consider what a fixed capacity per expert does to overloaded experts, which auxiliary signal nudges the router toward balance, and how stragglers affect step time.