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
|
Refuse a checkpoint whose MoE layers hold fewer experts than |
|
The layout of |
|
Each |
|
A module's |
Classes
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
namewithout the activation-checkpoint andtorch.compilewrappers.get_model_state_dictleaves them out of the names of its entries.
- kempnerforge.checkpoint.expert_layout.moe_layers(model)[source]¶
Each
MoEMLPinmodelwith the name its state-dict entries start with.The name is empty when
modelis itself theMoEMLP(seestate_dict_name).- Parameters:
model (torch.nn.Module)
- Return type:
- 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:
objectTranslates a DCP state dict between one EP rank’s experts and all of the layer’s.
to_globaltranslates 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_localundoes it once a load has filled the result, so one layout serves one save or one load.- Parameters:
layers – The split
MoEMLPlayers, each with the name its state-dict entries start with, asmoe_layersgives them.
- to_global(state)[source]¶
statewith each rank’s experts placed among all of the layer’s experts.Returns a new dict and leaves
stateas 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.
- global_names(names)[source]¶
namesin 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:
- Return type:
- group_names(names)[source]¶
A param group’s
namesas 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_globalwrites the param groups of an optimizer.
- 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.savedis the checkpoint’s DCP index, itsstate_dict_metadata. A checkpoint saved withep > 1before the experts had a global identity holdsnum_experts / epexperts of each layer: every EP rank wrote its own under the same names, and the checkpoint kept one rank’s. Loading it at the sameepwould give every rank that rank’s experts, and at anotherepit fails on the experts it lacks, so it is refused here with what it holds. A layer saved with packed experts keeps them inup_wanddown_w, andgate_wunder SwiGLU, and one saved without inexperts.{i}, so a checkpoint saved with the othermodel.moe_packed_expertsholds 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
modelhas more experts than the checkpoint holds for it, or the checkpoint holds them in the other layout.- Parameters:
model (torch.nn.Module)
where (object)
- Return type:
None