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 |