skip to content

With expert parallelism in Megatron-Core or DeepSpeed, what all-to-all traffic does each MoE layer of a 64-expert model add, and where should the expert-parallel group sit?

level: seniorimportance: should knowfreq 6%

answer

  1. tokens travel to the experts
  2. dispatch, then combine
  3. mirrored again in backward
  4. Megatron's default dispatcher is allgather
  5. expert tensor size follows TP

basics

~20 s

Each MoE layer adds a dispatch all-to-all sending tokens to the ranks holding their experts and a combine all-to-all bringing results back, mirrored in backward, every micro-batch. Keep the expert-parallel group inside one NVLink node.

solid answer

~40 s

With an expert-parallel size of 8, each GPU holds 64 / 8 = 8 experts per MoE layer — Megatron-Core's `--num-experts 64 --expert-model-parallel-size 8`, or DeepSpeed's `MoE(..., num_experts=64, ep_size=8)`. Tokens routed to an expert on another rank must travel: a dispatch all-to-all before the experts, a combine all-to-all after, and the same pair in backward, in every MoE layer for every micro-batch. That is far more frequent than once-per-step gradient sync, so the EP group belongs inside the NVLink domain: EP 8 on eight-GPU nodes. In Megatron-Core set `--moe-token-dispatcher-type alltoall`, since the default is `allgather`, and check `--expert-tensor-parallel-size`: it defaults to the TP size, and with TP 8 attention an EP-8 expert group then spans 64 GPUs, across nodes. Setting it to 1 keeps EP inside the node.

go deeper

for a junior

Recall that expert parallelism places different experts on different GPUs, so tokens must be sent to their experts and back in every MoE layer.

for a middle

Explain the dispatch and combine all-to-alls, why backward repeats them, and how num_experts and the expert-parallel size set the experts per GPU.

for a senior

Place the expert-parallel group on the interconnect: keep EP times expert TP inside the NVLink node, choose Megatron's dispatcher deliberately, and catch the expert tensor size defaulting to attention TP.

for a principal

Judge whether an MoE program's expert count and cluster shape keep expert traffic on NVLink, or whether the interconnect, a different expert count or overlap work must change before training starts.

## What expert parallelism places where In the MoE sibling model every MoE layer has 64 experts. **Expert parallelism** (EP) gives each rank of an expert-parallel group a subset of them: with an EP size of 8, each GPU holds 64 / 8 = 8 experts per MoE layer. Both engines require the expert count to divide evenly: - **Megatron-Core 0.19:** `--num-experts 64 --expert-model-parallel-size 8`; argument validation asserts that the number of experts is a multiple of the expert-parallel size. - **DeepSpeed 0.19:** `deepspeed.moe.layer.MoE(hidden_size, expert, num_experts=64, ep_size=8, k=...)` asserts the same and builds 8 local experts per rank. The router (how it picks experts and balances them is the MoE foundation's subject) sends each token to experts that mostly live on other ranks, so the layer has to move tokens. ## The all-to-all pair, forward and backward For every MoE layer and every micro-batch: 1. **Dispatch all-to-all** — each rank sends every routed token's hidden vector to the rank holding the chosen expert; with top-k routing a token can travel to k ranks. 2. **Expert compute** on the local experts. 3. **Combine all-to-all** — the outputs travel back to each token's home rank. 4. **Backward** — the gradient of an all-to-all is another all-to-all in the reverse direction; DeepSpeed's `_AllToAll` autograd function implements its backward that way, so the pair repeats in backward. That is two token exchanges in forward and two in backward per MoE layer per micro-batch, against one gradient synchronisation per optimizer step for data parallelism. Megatron-Core's MoE guide reports that EP all-to-all can take 30-40% of training time without optimization. Engine details that change the traffic: - **DeepSpeed** packs tokens into a fixed per-expert capacity buffer (`capacity_factor` default 1.0, `min_capacity` 4, `drop_tokens=True`), so each all-to-all moves a fixed-size tensor and overflow tokens are dropped; `drop_tokens=False` means unlimited capacity. - **Megatron-Core** selects the exchange with `--moe-token-dispatcher-type`: `allgather` (the default) gathers all tokens across the TP × EP domain, which suits TP-only or small-EP setups; `alltoall` sends each token only where it is routed and is the documented choice for EP > 1; `flex` uses the DeepEP or HybridEP backends for cross-node EP. The `alltoall` dispatcher also exchanges the routing probabilities alongside the tokens. ## Where the expert-parallel group should sit | EP size | Experts per GPU | Where each all-to-all runs on 8-GPU nodes | |---|---|---| | 8 | 8 | inside one node, over NVLink | | 16 | 4 | across two nodes | | 64 | 1 | across eight nodes, over the inter-node network | Megatron-Core's guidance is to keep EP × expert TP inside one node and to grow beyond a node with pipeline parallelism instead. The trap is `--expert-tensor-parallel-size`: it defaults to the tensor-parallel size. If attention uses TP 8 and the experts use EP 8 with the default, the expert grid needs 8 × 8 = 64 GPUs per pipeline stage, so the all-to-all leaves the node. Setting `--expert-tensor-parallel-size 1` keeps EP 8 on one node. Megatron-Core's expert grid is ETP × EP × EDP × PP, separate from the dense grid: with ETP 1, EP 8 and PP 4 on 256 GPUs, the expert-data-parallel size is 256 / (1 × 8 × 4) = 8, and each expert's gradients are reduced across those 8 copies once per step. Combining TP with EP also requires `--sequence-parallel`. DeepSpeed likewise builds an expert-parallel group and an expert-data-parallel group for each `ep_size`, so an expert's gradients are reduced only among the ranks that hold copies of that expert, not across the whole data-parallel group. ## Hiding what remains - `--overlap-moe-expert-parallel-comm` with `--delay-wgrad-compute` overlaps one micro-batch's EP all-to-all with another's computation. - `--moe-shared-expert-overlap` runs shared-expert computation while tokens are in flight. - In DeepSpeed, a lower `capacity_factor` shrinks the dispatch buffer at the risk of dropping more tokens. ## Mistakes interviewers listen for - Leaving Megatron's dispatcher at its `allgather` default for EP 8. - Leaving `--expert-tensor-parallel-size` at its default under TP 8 attention. - Spreading one expert per GPU across eight nodes "for balance". - Treating the all-to-all as a once-per-step cost.

  • Why can't Megatron-Core's expert-parallel size simply be any divisor of the GPU count?
    Argument validation requires the expert count to be a multiple of `--expert-model-parallel-size`, so 64 experts allow EP 1, 2, 4, 8, 16, 32 or 64. The expert grid ETP × EP × PP must also divide the world size; on 256 GPUs with ETP 1 and PP 4, EP 8 gives an expert-data-parallel size of 8.
  • What does DeepSpeed's MoE capacity do to the all-to-all?
    DeepSpeed's gate packs tokens into a per-expert buffer whose size follows `capacity_factor` (default 1.0) with a floor of `min_capacity` (default 4), so the dispatch all-to-all moves a fixed-size tensor every step. With `drop_tokens=True`, the default, tokens beyond capacity are dropped; `drop_tokens=False` behaves like unlimited capacity, so the exchange grows with routing imbalance.

Expert parallelism works like a hospital whose specialists are spread across floors: every ward sends each patient to the floor with the right specialist and takes them back after the visit. Trips by internal lift (NVLink inside a node) are cheap; trips by ambulance between buildings (the inter-node network) are slow, so one ward's specialists belong in one building.

  • Expert parallelism communicates once per step, like gradient sync
  • Megatron-Core's MoE dispatcher is all-to-all by default
  • Spreading one expert per GPU across nodes always speeds up an MoE layer
  • expert_tensor_parallel_size defaults to 1, independent of attention TP
  • The combine step needs no communication; outputs stay on the expert's rank

Assumes Megatron-Core 0.19.2 and DeepSpeed 0.19.7: the allgather default dispatcher, expert_tensor_parallel_size defaulting to the TP size and MoE(ep_size=...) are checked against those releases.