distributed

Utility functions for using torch.distributed.

Classes

DistributedProcessGroup

A convenient wrapper around torch.distributed.ProcessGroup objects.

ParallelState

A class to manage various parallel groups such as data parallel, tensor parallel etc.

Functions

backend

Returns the distributed backend.

barrier

Synchronizes all processes.

fsdp2_wrap

Auto-detect a HF causal-LM's decoder layers and FSDP2 fully_shard each one.

is_available

Returns whether the distributed package is available.

is_fsdp2_model

Return True if any submodule of model has been wrapped with FSDP2 fully_shard.

is_initialized

Returns whether the distributed package is initialized.

is_master

Returns whether the current process is the master process.

rank

Returns the rank of the current process.

size

Returns the number of processes.

class DistributedProcessGroup

Bases: object

A convenient wrapper around torch.distributed.ProcessGroup objects.

__init__(group=None)

Initialize the distributed process group.

Parameters:

group (ProcessGroup | int | None)

static get_dist_syncd_obj(obj, groups, op)

Get the distributed synchronized object across the specified distributed groups.

Parameters:
is_initialized()

Check if the distributed process group is initialized.

Return type:

bool

rank()

Get the rank of the current process group.

Return type:

int

world_size()

Get the world size of the current process group.

Return type:

int

class ParallelState

Bases: object

A class to manage various parallel groups such as data parallel, tensor parallel etc.

Specify the parallel groups of type torch.distributed.ProcessGroup for the current module. If the parallel group is not used, it should be set to -1. if a parallel group is None, it will use the default PyTorch distributed process group which is the whole world.

__init__(data_parallel_group=None, tensor_parallel_group=-1, expert_model_parallel_group=-1)

Initialize the parallel state.

Parameters:
  • data_parallel_group (ProcessGroup | int | None)

  • tensor_parallel_group (ProcessGroup | int | None)

  • expert_model_parallel_group (ProcessGroup | int | None)

backend()

Returns the distributed backend.

Return type:

str | None

barrier(group=None)

Synchronizes all processes.

Return type:

None

fsdp2_wrap(model, shard_root=True, mp_policy=None, cpu_offload=False)

Auto-detect a HF causal-LM’s decoder layers and FSDP2 fully_shard each one.

By default (shard_root=True) the root module is wrapped too, so embed/lm_head/norm are sharded instead of replicated per rank; pass shard_root=False to leave the root replicated (only decoder layers sharded). Returns the detected decoder layers so callers can reuse the detection result.

Parameters:

cpu_offload (bool)

is_available()

Returns whether the distributed package is available.

Return type:

bool

is_fsdp2_model(model)

Return True if any submodule of model has been wrapped with FSDP2 fully_shard.

Return type:

bool

is_initialized()

Returns whether the distributed package is initialized.

Return type:

bool

is_master(group=None)

Returns whether the current process is the master process.

Return type:

bool

rank(group=None)

Returns the rank of the current process.

Return type:

int

size(group=None)

Returns the number of processes.

Return type:

int