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.
Functions
|
Refuse a checkpoint whose MoE layers hold fewer experts than |
|
The layout of |
|
Each |
Classes
Translates a DCP state dict between one EP rank's experts and all of the layer's. |
- 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. It leaves out the activation-checkpoint andtorch.compilewrappers, asget_model_state_dictdoes.- 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.
- kempnerforge.checkpoint.expert_layout.check_saved_experts(model, saved, where)[source]¶
Refuse a checkpoint whose MoE layers hold fewer experts than
model’s.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 the checkpoint holds no expert of is left to the load.- Raises:
ValueError – A layer of
modelhas more experts than the checkpoint holds for it.- Parameters:
model (torch.nn.Module)
where (object)
- Return type:
None