distributed
¶
Classes¶
fastvideo.distributed.StatelessProcessGroup
dataclass
¶
StatelessProcessGroup(rank: int, world_size: int, store: Store, data_expiration_seconds: int = 3600, send_dst_counter: dict[int, int] = dict(), recv_src_counter: dict[int, int] = dict(), broadcast_send_counter: int = 0, broadcast_recv_src_counter: dict[int, int] = dict(), entries: deque[tuple[str, float]] = deque())
A dataclass to hold a metadata store, and the rank, world_size of the group. Only use it to communicate metadata between processes. For data-plane communication, create NCCL-related objects.
Functions¶
fastvideo.distributed.StatelessProcessGroup.all_gather_obj
¶
All gather an object from all ranks.
Source code in fastvideo/distributed/utils.py
fastvideo.distributed.StatelessProcessGroup.barrier
¶
fastvideo.distributed.StatelessProcessGroup.broadcast_obj
¶
Broadcast an object from a source rank to all other ranks. It does not clean up after all ranks have received the object. Use it for limited times, e.g., for initialization.
Source code in fastvideo/distributed/utils.py
fastvideo.distributed.StatelessProcessGroup.create
staticmethod
¶
create(host: str, port: int, rank: int, world_size: int, data_expiration_seconds: int = 3600) -> StatelessProcessGroup
A replacement for torch.distributed.init_process_group that does not
pollute the global state.
If we have process A and process B called torch.distributed.init_process_group
to form a group, and then we want to form another group with process A, B, C,
D, it is not possible in PyTorch, because process A and process B have already
formed a group, and process C and process D cannot join that group. This
function is a workaround for this issue.
torch.distributed.init_process_group is a global call, while this function
is a stateless call. It will return a StatelessProcessGroup object that can be
used for exchanging metadata. With this function, process A and process B
can call StatelessProcessGroup.create to form a group, and then process A, B,
C, and D can call StatelessProcessGroup.create to form another group.
Source code in fastvideo/distributed/utils.py
fastvideo.distributed.StatelessProcessGroup.expire_data
¶
Expire data that is older than data_expiration_seconds seconds.
Source code in fastvideo/distributed/utils.py
fastvideo.distributed.StatelessProcessGroup.recv_obj
¶
Receive an object from a source rank.
fastvideo.distributed.StatelessProcessGroup.send_obj
¶
Send an object to a destination rank.
Source code in fastvideo/distributed/utils.py
Functions¶
fastvideo.distributed.compute_padding_for_sp
¶
Compute padding needed for sequence parallel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seq_len
|
int
|
Original sequence length |
required |
sp_world_size
|
int
|
Sequence parallel world size |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[int, int]
|
(padded_seq_len, padding_amount) |
Source code in fastvideo/distributed/utils.py
fastvideo.distributed.divide
¶
Ensure that numerator is divisible by the denominator and return the division value.
fastvideo.distributed.ensure_divisibility
¶
Ensure that numerator is divisible by the denominator.
fastvideo.distributed.get_local_torch_device
¶
Return the torch device for the current rank.
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.init_logger
¶
init_logger(name: str) -> _FastvideoLogger
The main purpose of this function is to ensure that loggers are retrieved in such a way that we can be sure the root fastvideo logger has already been configured.
Source code in fastvideo/logger.py
fastvideo.distributed.initialize_model_parallel
¶
initialize_model_parallel(tensor_model_parallel_size: int = 1, sequence_model_parallel_size: int = 1, data_parallel_size: int = 1, backend: str | None = None) -> None
Initialize model parallel groups.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor_model_parallel_size
|
int
|
number of GPUs used for tensor model parallelism (used for language encoder). |
1
|
sequence_model_parallel_size
|
int
|
number of GPUs used for sequence model parallelism (used for DiT). |
1
|
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.pad_sequence_tensor
¶
pad_sequence_tensor(tensor: Tensor, target_seq_len: int, seq_dim: int = 1, pad_value: float = 0.0) -> Tensor
Pad a tensor along the sequence dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor
|
Tensor
|
Input tensor to pad |
required |
target_seq_len
|
int
|
Target sequence length after padding |
required |
seq_dim
|
int
|
Dimension to pad along (default: 1) |
1
|
pad_value
|
float
|
Value to use for padding (default: 0.0) |
0.0
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
Padded tensor |
Source code in fastvideo/distributed/utils.py
fastvideo.distributed.sequence_model_parallel_all_gather
¶
sequence_model_parallel_all_gather(input_: Tensor, dim: int = -1) -> Tensor
All-gather the input tensor across model parallel group.
fastvideo.distributed.sequence_model_parallel_all_gather_with_unpad
¶
sequence_model_parallel_all_gather_with_unpad(input_: Tensor, original_seq_len: int, dim: int = -1) -> Tensor
All-gather the input tensor and remove padding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_
|
Tensor
|
Sharded (and possibly padded) tensor to gather |
required |
original_seq_len
|
int
|
Original sequence length before padding |
required |
dim
|
int
|
Dimension to gather along (default: -1) |
-1
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
Gathered and unpadded tensor |
Source code in fastvideo/distributed/communication_op.py
fastvideo.distributed.sequence_model_parallel_all_to_all_4D
¶
sequence_model_parallel_all_to_all_4D(input_: Tensor, scatter_dim: int = 2, gather_dim: int = 1) -> Tensor
All-to-all communication of 4D tensors (e.g. QKV matrices) across sequence parallel group.
Source code in fastvideo/distributed/communication_op.py
fastvideo.distributed.sequence_model_parallel_shard
¶
Shard the input tensor across model parallel group with optional padding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_
|
Tensor
|
Input tensor to shard |
required |
dim
|
int
|
Dimension to shard along (default: 1) |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[Tensor, int]
|
(sharded_tensor, original_seq_len) - sharded_tensor: The sharded (and possibly padded) tensor - original_seq_len: Original sequence length before padding |
Source code in fastvideo/distributed/communication_op.py
fastvideo.distributed.split_tensor_along_last_dim
¶
split_tensor_along_last_dim(tensor: Tensor, num_partitions: int, contiguous_split_chunks: bool = False) -> Sequence[Tensor]
Split a tensor along its last dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor
|
Tensor
|
input tensor. |
required |
num_partitions
|
int
|
number of partitions to split the tensor |
required |
contiguous_split_chunks
|
bool
|
If True, make each chunk contiguous in memory. |
False
|
Returns:
| Type | Description |
|---|---|
Sequence[Tensor]
|
A list of Tensors |
Source code in fastvideo/distributed/utils.py
fastvideo.distributed.tensor_model_parallel_all_gather
¶
tensor_model_parallel_all_gather(input_: Tensor, dim: int = -1) -> Tensor
All-gather the input tensor across model parallel group.
fastvideo.distributed.tensor_model_parallel_all_reduce
¶
All-reduce the input tensor across model parallel group.
fastvideo.distributed.unpad_sequence_tensor
¶
Remove padding from a tensor along the sequence dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor
|
Tensor
|
Padded tensor |
required |
original_seq_len
|
int
|
Original sequence length (before padding) |
required |
seq_dim
|
int
|
Dimension to unpad along (default: 1) |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
Unpadded tensor |
Source code in fastvideo/distributed/utils.py
fastvideo.distributed.warmup_sequence_parallel_communication
¶
Warmup NCCL communicators for sequence parallel all-to-all operations.
The first NCCL collective operation is slow due to lazy communicator initialization. This function runs dummy all-to-all operations to trigger the initialization upfront, before the first real forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
device | None
|
Device to use for warmup tensors. If None, uses CUDA device 0. |
None
|
Source code in fastvideo/distributed/communication_op.py
Modules¶
fastvideo.distributed.communication_op
¶
Functions¶
fastvideo.distributed.communication_op.sequence_model_parallel_all_gather
¶
sequence_model_parallel_all_gather(input_: Tensor, dim: int = -1) -> Tensor
All-gather the input tensor across model parallel group.
fastvideo.distributed.communication_op.sequence_model_parallel_all_gather_with_unpad
¶
sequence_model_parallel_all_gather_with_unpad(input_: Tensor, original_seq_len: int, dim: int = -1) -> Tensor
All-gather the input tensor and remove padding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_
|
Tensor
|
Sharded (and possibly padded) tensor to gather |
required |
original_seq_len
|
int
|
Original sequence length before padding |
required |
dim
|
int
|
Dimension to gather along (default: -1) |
-1
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
Gathered and unpadded tensor |
Source code in fastvideo/distributed/communication_op.py
fastvideo.distributed.communication_op.sequence_model_parallel_all_to_all_4D
¶
sequence_model_parallel_all_to_all_4D(input_: Tensor, scatter_dim: int = 2, gather_dim: int = 1) -> Tensor
All-to-all communication of 4D tensors (e.g. QKV matrices) across sequence parallel group.
Source code in fastvideo/distributed/communication_op.py
fastvideo.distributed.communication_op.sequence_model_parallel_shard
¶
Shard the input tensor across model parallel group with optional padding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_
|
Tensor
|
Input tensor to shard |
required |
dim
|
int
|
Dimension to shard along (default: 1) |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[Tensor, int]
|
(sharded_tensor, original_seq_len) - sharded_tensor: The sharded (and possibly padded) tensor - original_seq_len: Original sequence length before padding |
Source code in fastvideo/distributed/communication_op.py
fastvideo.distributed.communication_op.tensor_model_parallel_all_gather
¶
tensor_model_parallel_all_gather(input_: Tensor, dim: int = -1) -> Tensor
All-gather the input tensor across model parallel group.
fastvideo.distributed.communication_op.tensor_model_parallel_all_reduce
¶
All-reduce the input tensor across model parallel group.
fastvideo.distributed.communication_op.warmup_sequence_parallel_communication
¶
Warmup NCCL communicators for sequence parallel all-to-all operations.
The first NCCL collective operation is slow due to lazy communicator initialization. This function runs dummy all-to-all operations to trigger the initialization upfront, before the first real forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
device | None
|
Device to use for warmup tensors. If None, uses CUDA device 0. |
None
|
Source code in fastvideo/distributed/communication_op.py
fastvideo.distributed.device_communicators
¶
Modules¶
fastvideo.distributed.device_communicators.base_device_communicator
¶
Classes¶
fastvideo.distributed.device_communicators.base_device_communicator.DeviceCommunicatorBase
¶DeviceCommunicatorBase(cpu_group: ProcessGroup, device: device | None = None, device_group: ProcessGroup | None = None, unique_name: str = '')
Base class for device-specific communicator with autograd support.
It can use the cpu_group to initialize the communicator.
If the device has PyTorch integration (PyTorch can recognize its
communication backend), the device_group will also be given.
Source code in fastvideo/distributed/device_communicators/base_device_communicator.py
fastvideo.distributed.device_communicators.base_device_communicator.DeviceCommunicatorBase.all_gather
¶all_gather(input_: Tensor, dim: int = -1) -> Tensor
Performs an all_gather operation with gradient support.
Source code in fastvideo/distributed/device_communicators/base_device_communicator.py
fastvideo.distributed.device_communicators.base_device_communicator.DeviceCommunicatorBase.all_reduce
¶Performs an all_reduce operation with gradient support.
Source code in fastvideo/distributed/device_communicators/base_device_communicator.py
fastvideo.distributed.device_communicators.base_device_communicator.DeviceCommunicatorBase.all_to_all_4D
¶Performs a 4D all-to-all operation with gradient support.
Source code in fastvideo/distributed/device_communicators/base_device_communicator.py
fastvideo.distributed.device_communicators.base_device_communicator.DeviceCommunicatorBase.gather
¶NOTE: We assume that the input tensor is on the same device across
all the ranks.
NOTE: dst is the local rank of the destination rank.
Source code in fastvideo/distributed/device_communicators/base_device_communicator.py
fastvideo.distributed.device_communicators.base_device_communicator.DeviceCommunicatorBase.recv
¶recv(size: Size, dtype: dtype, src: int | None = None) -> Tensor
Receives a tensor from the source rank.
Source code in fastvideo/distributed/device_communicators/base_device_communicator.py
fastvideo.distributed.device_communicators.base_device_communicator.DeviceCommunicatorBase.send
¶send(tensor: Tensor, dst: int | None = None) -> None
Sends a tensor to the destination rank in a non-blocking way
Source code in fastvideo/distributed/device_communicators/base_device_communicator.py
fastvideo.distributed.device_communicators.base_device_communicator.DeviceCommunicatorBase.slice
¶Performs rank-local slicing with all-gather backward.
Source code in fastvideo/distributed/device_communicators/base_device_communicator.py
fastvideo.distributed.device_communicators.base_device_communicator.DistributedAutograd
¶Collection of autograd functions for distributed operations.
This class provides custom autograd functions for distributed operations like all_reduce, all_gather, and all_to_all. Each operation is implemented as a static inner class with proper forward and backward implementations.
fastvideo.distributed.device_communicators.base_device_communicator.DistributedAutograd.AllGather
¶
Bases: Function
Differentiable all_gather operation.
The operation gathers tensors from all ranks and concatenates them along a specified dimension. The backward pass uses reduce_scatter to efficiently distribute gradients back to source ranks.
fastvideo.distributed.device_communicators.base_device_communicator.DistributedAutograd.AllReduce
¶
Bases: Function
Differentiable all_reduce operation.
The gradient of all_reduce is another all_reduce operation since the operation combines values from all ranks equally.
fastvideo.distributed.device_communicators.base_device_communicator.DistributedAutograd.AllToAll4D
¶
Bases: Function
Differentiable all_to_all operation specialized for 4D tensors.
This operation is particularly useful for attention operations where we need to redistribute data across ranks for efficient parallel processing.
The operation supports two modes: 1. scatter_dim=2, gather_dim=1: Used for redistributing attention heads 2. scatter_dim=1, gather_dim=2: Used for redistributing sequence dimensions
fastvideo.distributed.device_communicators.base_device_communicator.DistributedAutograd.Slice
¶
Bases: Function
Differentiable slice (shard) operation.
Forward selects this rank's chunk along dim.
Backward all-gathers local gradients to reconstruct full input grad.
fastvideo.distributed.device_communicators.cpu_communicator
¶
Classes¶
fastvideo.distributed.device_communicators.cpu_communicator.CpuCommunicator
¶CpuCommunicator(cpu_group: ProcessGroup, device: device | None = None, device_group: ProcessGroup | None = None, unique_name: str = '')
Bases: DeviceCommunicatorBase
Source code in fastvideo/distributed/device_communicators/cpu_communicator.py
fastvideo.distributed.device_communicators.cpu_communicator.CpuCommunicator.gather
¶NOTE: We assume that the input tensor is on the same device across
all the ranks.
NOTE: dst is the local rank of the destination rank.
Source code in fastvideo/distributed/device_communicators/cpu_communicator.py
fastvideo.distributed.device_communicators.cuda_communicator
¶
Classes¶
fastvideo.distributed.device_communicators.cuda_communicator.CudaCommunicator
¶CudaCommunicator(cpu_group: ProcessGroup, device: device | None = None, device_group: ProcessGroup | None = None, unique_name: str = '')
Bases: DeviceCommunicatorBase
Source code in fastvideo/distributed/device_communicators/cuda_communicator.py
fastvideo.distributed.device_communicators.cuda_communicator.CudaCommunicator.recv
¶recv(size: Size, dtype: dtype, src: int | None = None) -> Tensor
Receives a tensor from the source rank.
Source code in fastvideo/distributed/device_communicators/cuda_communicator.py
fastvideo.distributed.device_communicators.cuda_communicator.CudaCommunicator.send
¶send(tensor: Tensor, dst: int | None = None) -> None
Sends a tensor to the destination rank in a non-blocking way
Source code in fastvideo/distributed/device_communicators/cuda_communicator.py
fastvideo.distributed.device_communicators.npu_communicator
¶
Classes¶
fastvideo.distributed.device_communicators.npu_communicator.NpuCommunicator
¶NpuCommunicator(cpu_group: ProcessGroup, device: device | None = None, device_group: ProcessGroup | None = None, unique_name: str = '')
Bases: DeviceCommunicatorBase
Source code in fastvideo/distributed/device_communicators/npu_communicator.py
fastvideo.distributed.device_communicators.npu_communicator.NpuCommunicator.recv
¶recv(size: Size, dtype: dtype, src: int | None = None) -> Tensor
Receives a tensor from the source rank.
Source code in fastvideo/distributed/device_communicators/npu_communicator.py
fastvideo.distributed.device_communicators.npu_communicator.NpuCommunicator.send
¶send(tensor: Tensor, dst: int | None = None) -> None
Sends a tensor to the destination rank in a non-blocking way
Source code in fastvideo/distributed/device_communicators/npu_communicator.py
fastvideo.distributed.device_communicators.pyhccl
¶
Classes¶
fastvideo.distributed.device_communicators.pyhccl.PyHcclCommunicator
¶PyHcclCommunicator(group: ProcessGroup | StatelessProcessGroup, device: int | str | device, library_path: str | None = None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group
|
ProcessGroup | StatelessProcessGroup
|
the process group to work on. If None, it will use the default process group. |
required |
device
|
int | str | device
|
the device to bind the PyHcclCommunicator to. If None, it will be bind to f"npu:{local_rank}". |
required |
library_path
|
str | None
|
the path to the HCCL library. If None, it will use the default library path. |
None
|
It is the caller's responsibility to make sure each communicator is bind to a unique device.
Source code in fastvideo/distributed/device_communicators/pyhccl.py
Functions¶
fastvideo.distributed.device_communicators.pynccl
¶
Classes¶
fastvideo.distributed.device_communicators.pynccl.PyNcclCommunicator
¶PyNcclCommunicator(group: ProcessGroup | StatelessProcessGroup, device: int | str | device, library_path: str | None = None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group
|
ProcessGroup | StatelessProcessGroup
|
the process group to work on. If None, it will use the default process group. |
required |
device
|
int | str | device
|
the device to bind the PyNcclCommunicator to. If None, it will be bind to f"cuda:{local_rank}". |
required |
library_path
|
str | None
|
the path to the NCCL library. If None, it will use the default library path. |
None
|
It is the caller's responsibility to make sure each communicator is bind to a unique device.
Source code in fastvideo/distributed/device_communicators/pynccl.py
Functions¶
fastvideo.distributed.parallel_state
¶
FastVideo distributed state. It takes over the control of the distributed environment from PyTorch. The typical workflow is:
- call
init_distributed_environmentto initialize the distributed environment. - call
initialize_model_parallelorensure_model_parallel_initializedto initialize the model parallel groups.
- any code dealing with the distributed stuff
- call
destroy_model_parallelto destroy the model parallel groups. - call
destroy_distributed_environmentto destroy the distributed environment.
If you only need to use the distributed environment without model parallelism, you can skip the model parallel initialization and destruction steps.
Classes¶
fastvideo.distributed.parallel_state.GroupCoordinator
¶
GroupCoordinator(group_ranks: list[list[int]], local_rank: int, torch_distributed_backend: str | Backend, use_device_communicator: bool, use_message_queue_broadcaster: bool = False, group_name: str | None = None)
PyTorch ProcessGroup wrapper for a group of processes. PyTorch ProcessGroup is bound to one specific communication backend, e.g. NCCL, Gloo, MPI, etc. GroupCoordinator takes charge of all the communication operations among the processes in the group. It manages both CPU and device communication.
Source code in fastvideo/distributed/parallel_state.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
Attributes¶
fastvideo.distributed.parallel_state.GroupCoordinator.first_rank
property
¶Return the global rank of the first process in the group
fastvideo.distributed.parallel_state.GroupCoordinator.is_first_rank
property
¶Return whether the caller is the first process in the group
fastvideo.distributed.parallel_state.GroupCoordinator.is_last_rank
property
¶Return whether the caller is the last process in the group
fastvideo.distributed.parallel_state.GroupCoordinator.last_rank
property
¶Return the global rank of the last process in the group
fastvideo.distributed.parallel_state.GroupCoordinator.next_rank
property
¶Return the global rank of the process that follows the caller
fastvideo.distributed.parallel_state.GroupCoordinator.prev_rank
property
¶Return the global rank of the process that precedes the caller
Functions¶
fastvideo.distributed.parallel_state.GroupCoordinator.all_reduce
¶User-facing all-reduce function before we actually call the all-reduce operation.
We need this because Dynamo does not support passing an arbitrary
object (self in this case) to a custom op. We need to pass the
group name as a string, and then look up the group coordinator from
the group name, dispatch the all-reduce operation to the group
coordinator.
In addition, PyTorch custom ops do not support mutation or returning a new tensor in the same op. So we always make the all-reduce operation out-of-place.
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.parallel_state.GroupCoordinator.barrier
¶Barrier synchronization among the group.
NOTE: don't use device_group here! barrier in NCCL is
terrible because it is internally a broadcast operation with
secretly created GPU tensors. It is easy to mess up the current
device. Use the CPU group instead.
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.parallel_state.GroupCoordinator.broadcast
¶broadcast(input_: Tensor, src: int = 0)
Broadcast the input tensor.
NOTE: src is the local rank of the source rank.
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.parallel_state.GroupCoordinator.broadcast_object
¶Broadcast the input object.
NOTE: src is the local rank of the source rank.
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.parallel_state.GroupCoordinator.broadcast_object_list
¶Broadcast the input object list.
NOTE: src is the local rank of the source rank.
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.parallel_state.GroupCoordinator.broadcast_tensor_dict
¶broadcast_tensor_dict(tensor_dict: dict[str, Tensor | Any] | None = None, src: int = 0, group: ProcessGroup | None = None, metadata_group: ProcessGroup | None = None) -> dict[str, Tensor | Any] | None
Broadcast the input tensor dictionary.
NOTE: src is the local rank of the source rank.
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.parallel_state.GroupCoordinator.gather
¶NOTE: We assume that the input tensor is on the same device across
all the ranks.
NOTE: dst is the local rank of the destination rank.
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.parallel_state.GroupCoordinator.recv
¶recv(size: Size, dtype: dtype, src: int | None = None) -> Tensor
Receives a tensor from the source rank.
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.parallel_state.GroupCoordinator.recv_object
¶Receive the input object list from the source rank.
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.parallel_state.GroupCoordinator.recv_tensor_dict
¶recv_tensor_dict(src: int | None = None, all_gather_group: Optional[GroupCoordinator] = None) -> dict[str, Tensor | Any] | None
Recv the input tensor dictionary.
NOTE: src is the local rank of the source rank.
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.parallel_state.GroupCoordinator.send
¶send(tensor: Tensor, dst: int | None = None) -> None
Sends a tensor to the destination rank in a non-blocking way
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.parallel_state.GroupCoordinator.send_object
¶Send the input object list to the destination rank.
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.parallel_state.GroupCoordinator.send_tensor_dict
¶send_tensor_dict(tensor_dict: dict[str, Tensor | Any], dst: int | None = None, all_gather_group: Optional[GroupCoordinator] = None) -> dict[str, Tensor | Any] | None
Send the input tensor dictionary.
NOTE: dst is the local rank of the source rank.
Source code in fastvideo/distributed/parallel_state.py
Functions¶
fastvideo.distributed.parallel_state.destroy_model_parallel
¶
Set the groups to none and destroy them.
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.parallel_state.get_local_torch_device
¶
Return the torch device for the current rank.
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.parallel_state.initialize_model_parallel
¶
initialize_model_parallel(tensor_model_parallel_size: int = 1, sequence_model_parallel_size: int = 1, data_parallel_size: int = 1, backend: str | None = None) -> None
Initialize model parallel groups.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor_model_parallel_size
|
int
|
number of GPUs used for tensor model parallelism (used for language encoder). |
1
|
sequence_model_parallel_size
|
int
|
number of GPUs used for sequence model parallelism (used for DiT). |
1
|
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.parallel_state.initialize_sequence_parallel_group
¶
initialize_sequence_parallel_group(sequence_model_parallel_size: int = 1, backend: str | None = None, group_name_suffix: str = '') -> GroupCoordinator
Initialize a sequence parallel group for a specific model.
This function creates a sequence parallel group that can be used with the patch_sequence_parallel_group context manager. It allows different models to use different sequence parallelism configurations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sequence_model_parallel_size
|
int
|
number of GPUs used for sequence model parallelism. |
1
|
backend
|
str | None
|
communication backend to use. |
None
|
group_name_suffix
|
str
|
optional suffix to make the group name unique. |
''
|
Returns:
| Type | Description |
|---|---|
GroupCoordinator
|
A GroupCoordinator for sequence parallelism that can be used with |
GroupCoordinator
|
the patch_sequence_parallel_group context manager. |
Example usage
# Initialize sequence parallel group for model2
sp_group_model2 = initialize_sequence_parallel_group(
sequence_model_parallel_size=2,
group_name_suffix="model2"
)
# Use sequence parallelism for model2
with patch_sequence_parallel_group(sp_group_model2):
# Run model2 with sequence parallelism
output2 = model2(input2)
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.parallel_state.initialize_tensor_parallel_group
¶
initialize_tensor_parallel_group(tensor_model_parallel_size: int = 1, backend: str | None = None, group_name_suffix: str = '') -> GroupCoordinator
Initialize a tensor parallel group for a specific model.
This function creates a tensor parallel group that can be used with the patch_tensor_parallel_group context manager. It allows different models to use different tensor parallelism configurations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor_model_parallel_size
|
int
|
number of GPUs used for tensor model parallelism. |
1
|
backend
|
str | None
|
communication backend to use. |
None
|
group_name_suffix
|
str
|
optional suffix to make the group name unique. |
''
|
Returns:
| Type | Description |
|---|---|
GroupCoordinator
|
A GroupCoordinator for tensor parallelism that can be used with |
GroupCoordinator
|
the patch_tensor_parallel_group context manager. |
Example usage
# Initialize tensor parallel group for model1
tp_group_model1 = initialize_tensor_parallel_group(
tensor_model_parallel_size=4,
group_name_suffix="model1"
)
# Use tensor parallelism for model1
with patch_tensor_parallel_group(tp_group_model1):
# Run model1 with tensor parallelism
output1 = model1(input1)
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.parallel_state.is_the_same_node_as
¶
is_the_same_node_as(pg: ProcessGroup | StatelessProcessGroup, source_rank: int = 0) -> list[int]
This is a collective operation that returns if each rank is in the same node as the source rank. It tests if processes are attached to the same memory system (shared access to shared memory).
Source code in fastvideo/distributed/parallel_state.py
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 | |
fastvideo.distributed.parallel_state.model_parallel_is_initialized
¶
model_parallel_is_initialized() -> bool
fastvideo.distributed.parallel_state.patch_tensor_parallel_group
¶
patch_tensor_parallel_group(tp_group: GroupCoordinator)
Patch the tp group temporarily until this function ends.
This method is for draft workers of speculative decoding to run draft model with different tp degree from that of target model workers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tp_group
|
GroupCoordinator
|
the tp group coordinator |
required |
Source code in fastvideo/distributed/parallel_state.py
fastvideo.distributed.utils
¶
Classes¶
fastvideo.distributed.utils.StatelessProcessGroup
dataclass
¶
StatelessProcessGroup(rank: int, world_size: int, store: Store, data_expiration_seconds: int = 3600, send_dst_counter: dict[int, int] = dict(), recv_src_counter: dict[int, int] = dict(), broadcast_send_counter: int = 0, broadcast_recv_src_counter: dict[int, int] = dict(), entries: deque[tuple[str, float]] = deque())
A dataclass to hold a metadata store, and the rank, world_size of the group. Only use it to communicate metadata between processes. For data-plane communication, create NCCL-related objects.
Functions¶
fastvideo.distributed.utils.StatelessProcessGroup.all_gather_obj
¶All gather an object from all ranks.
Source code in fastvideo/distributed/utils.py
fastvideo.distributed.utils.StatelessProcessGroup.barrier
¶ fastvideo.distributed.utils.StatelessProcessGroup.broadcast_obj
¶Broadcast an object from a source rank to all other ranks. It does not clean up after all ranks have received the object. Use it for limited times, e.g., for initialization.
Source code in fastvideo/distributed/utils.py
fastvideo.distributed.utils.StatelessProcessGroup.create
staticmethod
¶create(host: str, port: int, rank: int, world_size: int, data_expiration_seconds: int = 3600) -> StatelessProcessGroup
A replacement for torch.distributed.init_process_group that does not
pollute the global state.
If we have process A and process B called torch.distributed.init_process_group
to form a group, and then we want to form another group with process A, B, C,
D, it is not possible in PyTorch, because process A and process B have already
formed a group, and process C and process D cannot join that group. This
function is a workaround for this issue.
torch.distributed.init_process_group is a global call, while this function
is a stateless call. It will return a StatelessProcessGroup object that can be
used for exchanging metadata. With this function, process A and process B
can call StatelessProcessGroup.create to form a group, and then process A, B,
C, and D can call StatelessProcessGroup.create to form another group.
Source code in fastvideo/distributed/utils.py
fastvideo.distributed.utils.StatelessProcessGroup.expire_data
¶Expire data that is older than data_expiration_seconds seconds.
Source code in fastvideo/distributed/utils.py
fastvideo.distributed.utils.StatelessProcessGroup.recv_obj
¶Receive an object from a source rank.
fastvideo.distributed.utils.StatelessProcessGroup.send_obj
¶Send an object to a destination rank.
Source code in fastvideo/distributed/utils.py
Functions¶
fastvideo.distributed.utils.compute_padding_for_sp
¶
Compute padding needed for sequence parallel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seq_len
|
int
|
Original sequence length |
required |
sp_world_size
|
int
|
Sequence parallel world size |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[int, int]
|
(padded_seq_len, padding_amount) |
Source code in fastvideo/distributed/utils.py
fastvideo.distributed.utils.divide
¶
Ensure that numerator is divisible by the denominator and return the division value.
fastvideo.distributed.utils.ensure_divisibility
¶
Ensure that numerator is divisible by the denominator.
fastvideo.distributed.utils.pad_sequence_tensor
¶
pad_sequence_tensor(tensor: Tensor, target_seq_len: int, seq_dim: int = 1, pad_value: float = 0.0) -> Tensor
Pad a tensor along the sequence dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor
|
Tensor
|
Input tensor to pad |
required |
target_seq_len
|
int
|
Target sequence length after padding |
required |
seq_dim
|
int
|
Dimension to pad along (default: 1) |
1
|
pad_value
|
float
|
Value to use for padding (default: 0.0) |
0.0
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
Padded tensor |
Source code in fastvideo/distributed/utils.py
fastvideo.distributed.utils.split_tensor_along_last_dim
¶
split_tensor_along_last_dim(tensor: Tensor, num_partitions: int, contiguous_split_chunks: bool = False) -> Sequence[Tensor]
Split a tensor along its last dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor
|
Tensor
|
input tensor. |
required |
num_partitions
|
int
|
number of partitions to split the tensor |
required |
contiguous_split_chunks
|
bool
|
If True, make each chunk contiguous in memory. |
False
|
Returns:
| Type | Description |
|---|---|
Sequence[Tensor]
|
A list of Tensors |
Source code in fastvideo/distributed/utils.py
fastvideo.distributed.utils.unpad_sequence_tensor
¶
Remove padding from a tensor along the sequence dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor
|
Tensor
|
Padded tensor |
required |
original_seq_len
|
int
|
Original sequence length (before padding) |
required |
seq_dim
|
int
|
Dimension to unpad along (default: 1) |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
Unpadded tensor |