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.
Methods:¶
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 |