Skip to content

Distributed Context

About

The d9d.core.dist_context package is the single source of truth for the distributed execution environment. Its DistributedContext class holds the topology, the rank mapping and the communication groups, so that every rank agrees on them. Use the context for all distributed questions, such as "Am I the main process?" or "Which rank is my pipeline peer?". Do not read raw os.environ variables or create ad-hoc process groups: this can lead to silent inconsistencies. DistributedContext requires CUDA GPUs. It sets the current CUDA device of each process to its local rank.

An Explicit Object

DistributedContext is a plain object. You build it once from DeviceMeshParameters, which also creates the process groups, and pass it to every component that needs it. Besides the device meshes, it holds a logger that prefixes each line with the mesh coordinates of the rank. It also sets the timeout of all process groups and synchronizes ranks. d9d and Other Frameworks compares this with the global state of other frameworks.

DeviceMesh Domains

Different parts of a model need different parallelism strategies, for example dense layers and Mixture-of-Experts (MoE) layers. d9d describes each strategy as a DeviceMesh domain.

The physical GPUs stay the same, but each domain arranges them into a different mesh. There are domains for MoE layers, for dense layers and for the input batch. Get the DeviceMesh of a domain with dist_ctx.mesh_for(domain_name).

Demonstration Video

To understand domains better, watch our short demonstration video on YouTube.

Regular Domain (regular)

  • Identifier: REGULAR_DOMAIN or "regular"
  • Purpose: The most granular mesh, with every parallelism in its own dimension. d9d uses it for logging and seeding.
  • Dimensions:
    1. pp: Pipeline Parallel
    2. dp_replicate: Data Parallel (DDP style)
    3. dp_shard: Data Parallel (FSDP style)
    4. cp_shard: Context Parallel (FSDP style)
    5. cp_replicate: Context Parallel (DDP style)
    6. tp: Tensor Parallel

Expert Domain (expert)

  • Identifier: EXPERT_DOMAIN or "expert"
  • Purpose: The mesh for MoE layers. Shard sparse expert layers across the ep_shard dimension and replicate them across the ep_replicate dimension.
  • Dimensions:
    1. pp: Pipeline Parallel
    2. ep_replicate: Combined replication dimension ((DP * CP) // EP)
    3. ep_shard: Expert Parallel

Dense Domain (dense)

  • Identifier: DENSE_DOMAIN or "dense"
  • Purpose: The mesh for dense layers.
  • Dimensions:
    1. pp: Pipeline Parallel
    2. dp_replicate: Data Parallel replication in HSDP
    3. dp_cp_shard: Data Parallel and Context Parallel merged, for sharding in HSDP
    4. cp_replicate: Context Parallel replication
    5. tp: Tensor Parallel

Batch Domain (batch)

  • Identifier: BATCH_DOMAIN or "batch"
  • Purpose: The mesh for distributing the input batch and sharding the data loader.
  • Dimensions:
    1. pp: Pipeline Parallel
    2. dp: Data Parallel (dp_replicate * dp_shard)
    3. cp: Context Parallel (cp_replicate * cp_shard)
    4. tp: Tensor Parallel

Flat Domain (flat)

  • Identifier: FLAT_DOMAIN or "flat"
  • Purpose: A mesh with a single dimension that holds all processes.
  • Dimensions:
    1. world: World size

Usage

Initialization

Build the context from DeviceMeshParameters.

from d9d.core.dist_context import DeviceMeshParameters

# Define the topology.
params = DeviceMeshParameters(
    pipeline_parallel=2,
    data_parallel_replicate=8,
    data_parallel_shard=1,
    context_parallel_replicate=1,
    context_parallel_shard=1,
    expert_parallel=8,
    tensor_parallel=1
)

dist_ctx = params.build()

Accessing DeviceMesh Domains

1
2
3
4
5
6
from torch.distributed import DeviceMesh
from d9d.core.dist_context import DistributedContext, DENSE_DOMAIN

dist_ctx: DistributedContext = ...

mesh_dense: DeviceMesh = dist_ctx.mesh_for(DENSE_DOMAIN)

Rank Utilities

1
2
3
4
5
6
7
8
if dist_ctx.is_main_process:
    print("I am global rank 0")

if dist_ctx.is_local_main_process:
    print("I am rank 0 on this node")

# Synchronize.
dist_ctx.wait_world()

Context Managers

These context managers control the order in which ranks run a block.

1
2
3
4
5
6
# Only one process per node downloads the file.
with dist_ctx.local_main_process_first():
    if dist_ctx.is_local_main_process:
        download_dataset()
    # Other ranks enter the block after the download.
# All ranks continue together.

API Reference

d9d.core.dist_context

Distributed environment setup and its device meshes.

DeviceMeshParameters

Bases: BaseModel

Configuration for the parallelism degrees of the device meshes.

Attributes:

Name Type Description
pipeline_parallel int

Degree of pipeline parallelism (PP).

data_parallel_replicate int

Degree of data-parallel replication (DDP).

data_parallel_shard int

Degree of data-parallel sharding (FSDP).

context_parallel_replicate int

Degree of context-parallel (CP) replication.

context_parallel_shard int

Degree of context-parallel (CP) sharding.

tensor_parallel int

Degree of tensor parallelism (TP).

expert_parallel int

Degree of expert parallelism (EP) for MoE layers.

has_context_parallel_replicate property

Whether context parallel replication is enabled (degree > 1).

has_context_parallel_shard property

Whether context parallel sharding is enabled (degree > 1).

has_data_parallel_replicate property

Whether data parallel replication is enabled (degree > 1).

has_data_parallel_shard property

Whether data parallel sharding is enabled (degree > 1).

has_expert_parallel property

Whether expert parallelism is enabled (degree > 1).

has_pipeline_parallel property

Whether pipeline parallelism is enabled (degree > 1).

has_tensor_parallel property

Whether tensor parallelism is enabled (degree > 1).

is_distributed property

Whether any form of parallelism is enabled.

build(log_level=logging.INFO)

Builds a DistributedContext from these parameters.

Parameters:

Name Type Description Default
log_level int

The log level of the d9d logger.

INFO

Returns:

Type Description
DistributedContext

A new DistributedContext with the built device meshes.

DistributedContext

The single source of truth for the distributed execution environment.

It builds a PyTorch DeviceMesh for each domain (regular, expert, dense, ...). Rank placement, group membership and parallel topology must all come from this context, so that they stay consistent.

current_device property

The CUDA device of this rank.

is_local_main_process property

Whether the current process is local rank 0 on its node.

is_main_process property

Whether the current process is global rank 0.

local_rank property

The rank of the current process within its node.

logger property

The logger configured for distributed logging.

master_addr property

The IP address or host name of the master node.

mesh_params property

The parameters this context was built from.

node_rank property

The index of the node this process runs on.

num_nodes property

The total number of nodes in the cluster.

__init__(params, log_level)

Constructs the DistributedContext object and sets the current CUDA device to the local rank.

Parameters:

Name Type Description Default
params DeviceMeshParameters

The parallelism degrees to build the device meshes from.

required
log_level int

The log level of the d9d logger.

required
local_main_process_first()

Runs the block on the local main processes first.

Other ranks wait before entering the block. Local main processes wait after leaving it, so all ranks continue together.

main_process_first()

Runs the block on the global main process first.

Other ranks wait before entering the block. The global main process waits after leaving it, so all ranks continue together.

mesh_for(domain)

Returns the device mesh view associated with a specific logical domain.

The available domains:

  • "regular" (REGULAR_DOMAIN): the most granular mesh, with every parallelism in its own dimension. Dimensions: ("pp", "dp_replicate", "dp_shard", "cp_shard", "cp_replicate", "tp").
  • "expert" (EXPERT_DOMAIN): the mesh for Mixture-of-Experts (MoE) layers. Dimensions: ("pp", "ep_replicate", "ep_shard").
  • "dense" (DENSE_DOMAIN): the mesh for dense layers. Dimensions: ("pp", "dp_replicate", "dp_cp_shard", "cp_replicate", "tp").
  • "batch" (BATCH_DOMAIN): the mesh for distributing input data. Dimensions: ("pp", "dp", "cp", "tp").
  • "flat" (FLAT_DOMAIN): a single dimension with all the processes. Dimensions: ("world",).

Parameters:

Name Type Description Default
domain str

The name of the domain.

required

Returns:

Type Description
DeviceMesh

The device mesh of the domain.

Raises:

Type Description
ValueError

If the domain does not exist.

set_timeout(timeout_seconds)

Sets the timeout of the default process group and of all mesh process groups.

Does nothing in a non-distributed setup.

Parameters:

Name Type Description Default
timeout_seconds float

The new timeout in seconds.

required
wait_world()

Blocks until all ranks reach this point and the current CUDA device finishes its queued work.

build_dist_logger(qualifier, level)

Configures the d9d logger to write to stdout and returns it.

Each line includes the rank qualifier, so that ranks can be told apart in distributed logs. Existing handlers of the d9d logger are removed.

Parameters:

Name Type Description Default
qualifier str

The string that identifies the position of the current rank in the mesh.

required
level int

The log level.

required

Returns:

Type Description
Logger

The configured d9d logger.