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_DOMAINor"regular" - Purpose: The most granular mesh, with every parallelism in its own dimension. d9d uses it for logging and seeding.
- Dimensions:
pp: Pipeline Paralleldp_replicate: Data Parallel (DDP style)dp_shard: Data Parallel (FSDP style)cp_shard: Context Parallel (FSDP style)cp_replicate: Context Parallel (DDP style)tp: Tensor Parallel
Expert Domain (expert)
- Identifier:
EXPERT_DOMAINor"expert" - Purpose: The mesh for MoE layers. Shard sparse expert layers across the
ep_sharddimension and replicate them across theep_replicatedimension. - Dimensions:
pp: Pipeline Parallelep_replicate: Combined replication dimension ((DP * CP) // EP)ep_shard: Expert Parallel
Dense Domain (dense)
- Identifier:
DENSE_DOMAINor"dense" - Purpose: The mesh for dense layers.
- Dimensions:
pp: Pipeline Paralleldp_replicate: Data Parallel replication in HSDPdp_cp_shard: Data Parallel and Context Parallel merged, for sharding in HSDPcp_replicate: Context Parallel replicationtp: Tensor Parallel
Batch Domain (batch)
- Identifier:
BATCH_DOMAINor"batch" - Purpose: The mesh for distributing the input batch and sharding the data loader.
- Dimensions:
pp: Pipeline Paralleldp: Data Parallel (dp_replicate * dp_shard)cp: Context Parallel (cp_replicate * cp_shard)tp: Tensor Parallel
Flat Domain (flat)
- Identifier:
FLAT_DOMAINor"flat" - Purpose: A mesh with a single dimension that holds all processes.
- Dimensions:
world: World size
Usage
Initialization
Build the context from DeviceMeshParameters.
Accessing DeviceMesh Domains
Rank Utilities
Context Managers
These context managers control the order in which ranks run a block.
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 |
INFO
|
Returns:
| Type | Description |
|---|---|
DistributedContext
|
A new |
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 |
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 |