kempnerforge.checkpoint.expert_layout

Expert-parallel experts under their global identity in a DCP state dict.

apply_expert_parallel leaves each EP rank num_experts / ep experts of every MoE layer: an unpacked layer keeps them in a ModuleList numbered from 0, and a packed layer keeps their rows of each packed weight. Every EP rank’s state dict then names and shapes its experts as if they were the layer’s only ones, and DCP, which writes one copy of each name and offset, would save one rank’s experts in place of all of them.

ExpertLayout translates a state dict so that each rank presents its experts as part of all num_experts: an unpacked expert under its global index, and a packed weight as a DTensor over ep and the FSDP mesh whose rows cover every expert. A checkpoint then holds every expert in the layout an ep = 1 run saves, so DCP loads it at any ep. The translated tensors share storage with the rank’s own, so nothing is copied or gathered, and a load into them loads the model and optimizer.

check_saved_experts refuses a checkpoint that holds fewer experts than the model, as one saved with ep > 1 before this layout existed does, and one that holds them in the other layout, packed or one tensor per expert (model.moe_packed_experts), whose names the model does not have.

Functions

check_saved_experts(model, saved, where)

Refuse a checkpoint whose MoE layers hold fewer experts than model's, or hold them in the other expert layout.

expert_layout(model)

The layout of model's expert-parallel layers; None when no layer splits its experts.

moe_layers(model)

Each MoEMLP in model with the name its state-dict entries start with.

state_dict_name(name)

A module's name without the activation-checkpoint and torch.compile wrappers.

Classes

ExpertLayout

Translates a DCP state dict between one EP rank's experts and all of the layer's.

kempnerforge.checkpoint.expert_layout.state_dict_name(name)[source]

A module’s name without the activation-checkpoint and torch.compile wrappers.

get_model_state_dict leaves them out of the names of its entries.

Parameters:

name (str)

Return type:

str

kempnerforge.checkpoint.expert_layout.moe_layers(model)[source]

Each MoEMLP in model with the name its state-dict entries start with.

The name is empty when model is itself the MoEMLP (see state_dict_name).

Parameters:

model (torch.nn.Module)

Return type:

Iterator[tuple[str, MoEMLP]]

kempnerforge.checkpoint.expert_layout.expert_layout(model)[source]

The layout of model’s expert-parallel layers; None when no layer splits its experts.

Parameters:

model (torch.nn.Module)

Return type:

ExpertLayout | None

class kempnerforge.checkpoint.expert_layout.ExpertLayout[source]

Bases: object

Translates a DCP state dict between one EP rank’s experts and all of the layer’s.

to_global translates the "model" and "optimizer" entries of a state dict, and the names in every other entry that maps parameter names to values, such as the never-stepped record. to_local undoes it once a load has filled the result, so one layout serves one save or one load.

Parameters:

layers – The split MoEMLP layers, each with the name its state-dict entries start with, as moe_layers gives them.

__init__(layers)[source]
Parameters:

layers (list[tuple[str, MoEMLP]])

Return type:

None

to_global(state)[source]

state with each rank’s experts placed among all of the layer’s experts.

Returns a new dict and leaves state as it is. Each tensor in the result is the one it replaces or a view of it, so a load into the result loads them.

Raises:

ValueError – The model state lacks the expert weights of a split layer, or the optimizer holds a tensor for a packed weight in a shape other than the weight’s, so the checkpoint could not hold every rank’s experts.

Parameters:

state (Mapping[str, Any])

Return type:

dict[str, Any]

global_names(names)[source]

names in order, each split expert’s local index replaced by its global one.

A name outside the split layers comes back as it is.

Raises:

ValueError – A name gives a local expert index past this rank’s experts.

Parameters:

names (Iterable[str])

Return type:

list[str]

group_names(names)[source]

A param group’s names as a checkpoint lists them, with every EP rank’s experts.

Each split expert’s name stands for that expert on every EP rank, under each global index in order, as to_global writes the param groups of an optimizer.

Parameters:

names (Iterable[str])

Return type:

list[str]

to_local(state)[source]

Undo to_global on the state dict that a load filled.

Names go back to this rank’s, and each view to_global made goes back to the tensor it views, which holds what the load wrote. Each optimizer param group lists this rank’s parameters again and keeps the loaded settings.

Parameters:

state (Mapping[str, Any])

Return type:

dict[str, Any]

kempnerforge.checkpoint.expert_layout.check_saved_experts(model, saved, where)[source]

Refuse a checkpoint whose MoE layers hold fewer experts than model’s, or hold them in the other expert layout.

saved is the checkpoint’s DCP index, its state_dict_metadata. A checkpoint saved with ep > 1 before the experts had a global identity holds num_experts / ep experts of each layer: every EP rank wrote its own under the same names, and the checkpoint kept one rank’s. Loading it at the same ep would give every rank that rank’s experts, and at another ep it fails on the experts it lacks, so it is refused here with what it holds. A layer saved with packed experts keeps them in up_w and down_w, and gate_w under SwiGLU, and one saved without in experts.{i}, so a checkpoint saved with the other model.moe_packed_experts holds none of the names the model loads, which DCP would report as missing keys; it is refused here, naming the setting it was saved with. A layer the checkpoint holds no expert of in either layout, as a dense checkpoint’s, is left to the load.

Raises:

ValueError – A layer of model has more experts than the checkpoint holds for it, or the checkpoint holds them in the other layout.

Parameters:
Return type:

None