kempnerforge.distributed.expert_parallel¶
Expert Parallelism: partition MoE experts across an EP process group.
Each EP rank holds num_experts // ep_size experts. Tokens are shuffled
between ranks via all-to-all so every token reaches its assigned expert,
then results are returned to the originating rank. Every EP rank also holds
a copy of each other parameter, which average_non_expert_grads_ keeps
equal across the EP group.
When ep=1 (default), this module is a no-op and the model runs with
all experts replicated on every rank (the pre-EP behavior).
Functions
|
Partition MoE experts across the EP dimension of the DeviceMesh. |
|
All-to-all dispatch, local expert compute, all-to-all combine. |
|
The routed-expert parameters that EP splits across ranks, and the EP group. |
- kempnerforge.distributed.expert_parallel.ep_dispatch_and_compute(x, weights, indices, moe, ep_group, local_expert_start, num_local_experts, ep_world_size, gradient_scale=False)[source]¶
All-to-all dispatch, local expert compute, all-to-all combine.
When the EP ranks hold the same batch, as they do in training, the backward pass gives the local experts the mean of the ranks’ gradients, which is one process’s, and gives
xandweightsthe same gradients as without EP.- Parameters:
x (torch.Tensor) – (num_tokens, dim) flattened token representations.
weights (torch.Tensor) – (num_tokens, top_k) routing weights.
indices (torch.Tensor) – (num_tokens, top_k) global expert indices.
moe (MoEMLP) – The MoEMLP module. Holds either an nn.ModuleList of local experts (unpacked) or packed expert tensors already sliced to the local range.
ep_group (torch.distributed.ProcessGroup) – EP process group.
local_expert_start (int) – First global expert index on this rank.
num_local_experts (int) – Number of experts on this rank.
ep_world_size (int) – Number of ranks in the EP group.
gradient_scale (bool) – When
moeis in training mode, scale each local expert’s output by the average load over all experts divided by its own load, asMoEMLP._local_forwarddoes.
- Returns:
(num_tokens, dim) weighted combination of expert outputs.
- Return type:
output
- kempnerforge.distributed.expert_parallel.apply_expert_parallel(model, device_mesh)[source]¶
Partition MoE experts across the EP dimension of the DeviceMesh.
- For each MoEMLP in the model:
Prunes
expertsto the local subset for this EP rankStores EP metadata (group, mesh, world_size, local expert range)
The EP mesh lets a checkpoint place each rank’s experts among all of them (see
kempnerforge.checkpoint.expert_layout).Must be called AFTER tensor parallelism and BEFORE FSDP2.
No-op when
epis not in the mesh or has size 1.- Parameters:
model (torch.nn.Module)
device_mesh (DeviceMesh | None)
- Return type:
None
- kempnerforge.distributed.expert_parallel.expert_parallel_params(model)[source]¶
The routed-expert parameters that EP splits across ranks, and the EP group.
Each EP rank holds its own slice of these and a copy of every other parameter. Returns
([], None)when noMoEMLPhasep_world_size > 1.- Raises:
ValueError – Two
MoEMLPlayers record different EP groups, so no single group covers every routed expert.- Parameters:
model (torch.nn.Module)
- Return type:
- kempnerforge.distributed.expert_parallel.average_non_expert_grads_(model)¶
Give every EP rank the EP group’s mean gradient of each parameter outside the experts.
Each EP rank holds a copy of every parameter but its routed experts, and the EP ranks of a data-parallel group train on the same batches, so the copies’ gradients differ only in how the backward’s kernels round. Every EP rank leaves with the same mean, so copies that start equal take the same optimizer step and stay bitwise equal. Call it once per optimizer step, after the backward, when FSDP2 has reduced the gradients over the data-parallel ranks, and before clipping. A gradient that is None stays None.
Does nothing, and communicates nothing, when no
MoEMLPhasep_world_size > 1. Otherwise every EP rank must call it with gradients on the same parameters: it runs one all-reduce over the EP group per gradient dtype, of this rank’s shards of those gradients, and holds one flat copy of a dtype’s shards while that all-reduce runs.- Parameters:
model (torch.nn.Module) – The model, with
apply_expert_parallelapplied or not.- Return type:
None