nemo_automodel.components.distributed.mesh_utils
nemo_automodel.components.distributed.mesh_utils
Device mesh construction and access utilities for distributed training.
Module Contents
Classes
Functions
Data
API
Named mesh shape plus derived flattened axes.
Return whether a derived mesh dimension spans the default process group.
Create raw device meshes based on distributed config type.
Create the FSDP2 root mesh and optional MoE mesh.
Create the Megatron FSDP mesh.
Create per-axis NCCL options for DeviceMesh subgroups.
init_process_group(timeout=...) configures the default process group, but
init_device_mesh creates additional per-axis process groups. Without a
backend override those groups keep PyTorch’s default NCCL timeout.
Register flattened axes without slicing a mesh on non-member ranks.
PyTorch cannot slice an explicit DeviceMesh on ranks outside that mesh.
Separate-model jobs still need every global rank to create process groups in
the same order, so construct each flattened subgroup explicitly and retain
the subgroup local to the current rank.
Unflatten a mesh with its NCCL timeout, including the PyTorch 2.9 fallback.
PyTorch reuses the default process group for a world-sized dimension only when no backend override is supplied. The default group already has the configured distributed timeout, so omitting that redundant override avoids allocating another NCCL communicator.
Derive a 2D ("ring", "ulysses") view of the mesh’s "cp" axis.
Diffusers’ context-parallel API (ContextParallelConfig.mesh) requires a
device mesh with dimensions named "ring" and "ulysses". This reshapes
the 1D "cp" axis of an existing root mesh into that layout so context
parallelism shares the process groups already created for FSDP2, instead of
initializing a second world mesh.
Parameters:
Root mesh containing a "cp" dimension (as created by
_create_fsdp2_device_mesh).
Size of the ring-attention dimension.
Size of the Ulysses (all-to-all) attention dimension.
Returns: DeviceMesh
A 2D DeviceMesh of shape (ring_degree, ulysses_degree) with dim
Raises:
ValueError: Ifring_degree * ulysses_degreedoes not equal the size of the mesh’s"cp"dimension.
Access a 1D submesh by parallelism name (e.g. "dp", "tp", "dp_cp").
PyTorch 2.11 deprecates root_mesh["name"] for dimensions created via
_flatten(). This reads the _flatten() result directly.
Parameters:
Any DeviceMesh (root or submesh).
Parallelism dimension name.
Return the DP mesh for FSDP2 without losing the original root mesh.
get_submesh() may rebuild a fresh DeviceMesh when asked to compose native
and flattened dims like ("dp_replicate", "dp_shard_cp"). That is fine
for many local operations, but FSDP2 expects its DP mesh to share the same
root mesh as TP/EP meshes. On multi-node TP runs this can break group
construction in non-obvious ways.
Prefer native dimensions whenever possible:
- cp=1, dp_replicate=1 ->
device_mesh["dp_shard"] - cp=1, dp_replicate>1 ->
device_mesh[("dp_replicate", "dp_shard")] - cp>1, dp_replicate=1 ->
device_mesh["dp_shard_cp"]
When both CP and replicated DP are active we fall back to get_submesh()
because the composed mesh is genuinely multi-level.
Access a submesh by parallelism dim names.
Handles all cases: single dims, multi-dim slices, and combinations that
include _flatten()-created dims (e.g. ("dp_replicate", "dp_shard_cp")).
For the latter, finds the parent _flatten() result and calls _unflatten()
to decompose it into the requested shape.
Parameters:
Any DeviceMesh (root or submesh).
Tuple of dimension names.