nemo_automodel.components.moe.state_dict_utils
nemo_automodel.components.moe.state_dict_utils
Module Contents
Functions
API
Find the device-mesh dimension that partitions a grouped expert tensor.
Parameters:
DTensor with global shape [experts, …] and arbitrary trailing dimensions. A named ep mesh
dimension owns the expert partition when present; otherwise a Shard(0) placement identifies an
EP-free FSDP partition of the experts axis.
Returns: int | None
Index of the expert-partitioning mesh dimension, or None when every rank retains all experts.
Create a DTensor from a local tensor for expert parallelism.
Parameters:
Local portion of the tensor on this rank
Device mesh for DTensor creation
Current rank (for device placement)
Returns: torch.Tensor
DTensor if device_mesh is provided and DTensor is available, otherwise local_tensor
Get the range of experts that should be loaded for the current rank.
Parameters:
Device mesh for expert parallelism
Total number of experts
Returns: tuple[int, int]
Tuple of (start_expert_id, end_expert_id) for this rank
Get the slice of experts present on the current rank for a DTensor.
For non-DTensors, returns the full tensor with start_expert=0, end_expert=n_experts.
For DTensors sharded along the expert dimension (dim=0), returns only the local experts. The mesh dimension
may be the explicit ep dimension or an EP-free FSDP dimension such as dp_shard_cp.
Parameters:
Tensor with global shape [experts, …] and arbitrary trailing dimensions. For a DTensor,
the per-rank local shape is [local_experts, …] when a mesh dimension uses Shard(0); other
placements retain experts on the first local axis.
Total number of experts across all ranks.
Returns: torch.Tensor
Tuple containing the local tensor of shape [local_experts, …], the inclusive global ID of its first
Access a submesh by dim names from the given mesh.
Check if a tensor is a DTensor.
Check if a specific expert should be loaded on the current rank.
Parameters:
The expert ID to check
Device mesh for expert parallelism
Total number of experts
Returns: bool
True if this expert should be loaded on the current rank
Split expert weights, handling both regular tensors and DTensors.
For DTensors sharded on the expert axis, only splits the experts present on the current rank. Other DTensor placements retain all experts and are adjusted after removing the expert tensor dimension.
Parameters:
Tensor with global shape [experts, …] and arbitrary trailing dimensions. A Shard(0) DTensor
has local shape [local_experts, …]; inner-axis shards retain shape [experts, …] locally.
Total number of experts across all ranks.
Returns: list[torch.Tensor]
Tuple containing per-expert tensors of shape […] and their global expert IDs. Each output aliases the
Validate that a DTensor is properly sharded for expert parallelism.
Parameters:
Tensor to validate
Expected total number of experts
Name for error messages
Returns: bool
True if valid, raises ValueError if invalid