vllm.distributed.parallel_state
vLLM distributed state. It takes over the control of the distributed environment from PyTorch. The typical workflow is:
- call
init_distributed_environment
to initialize the distributed environment. -
call
initialize_model_parallel
orensure_model_parallel_initialized
to initialize the model parallel groups. -
any code dealing with the distributed stuff
-
call
destroy_model_parallel
to destroy the model parallel groups. - call
destroy_distributed_environment
to destroy the distributed environment.
If you only need to use the distributed environment without model/pipeline parallelism, you can skip the model parallel initialization and destruction steps.
TensorMetadata
module-attribute
¶
TensorMetadata = namedtuple(
"TensorMetadata", ["device", "dtype", "size"]
)
get_pipeline_model_parallel_group
module-attribute
¶
get_pipeline_model_parallel_group = get_pp_group
GraphCaptureContext
dataclass
¶
GroupCoordinator
¶
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 vllm/distributed/parallel_state.py
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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 |
|
use_device_communicator
instance-attribute
¶
use_device_communicator: bool = use_device_communicator
__init__
¶
__init__(
group_ranks: list[list[int]],
local_rank: int,
torch_distributed_backend: Union[str, Backend],
use_device_communicator: bool,
use_message_queue_broadcaster: bool = False,
group_name: Optional[str] = None,
)
Source code in vllm/distributed/parallel_state.py
_all_gather_out_place
¶
_all_reduce_out_place
¶
_reduce_scatter_out_place
¶
all_gather
¶
Source code in vllm/distributed/parallel_state.py
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 vllm/distributed/parallel_state.py
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 vllm/distributed/parallel_state.py
broadcast
¶
Broadcast the input tensor.
NOTE: src
is the local rank of the source rank.
Source code in vllm/distributed/parallel_state.py
broadcast_object
¶
Broadcast the input object.
NOTE: src
is the local rank of the source rank.
Source code in vllm/distributed/parallel_state.py
broadcast_object_list
¶
Broadcast the input object list.
NOTE: src
is the local rank of the source rank.
Source code in vllm/distributed/parallel_state.py
broadcast_tensor_dict
¶
broadcast_tensor_dict(
tensor_dict: Optional[
dict[str, Union[Tensor, Any]]
] = None,
src: int = 0,
group: Optional[ProcessGroup] = None,
metadata_group: Optional[ProcessGroup] = None,
) -> Optional[dict[str, Union[Tensor, Any]]]
Broadcast the input tensor dictionary.
NOTE: src
is the local rank of the source rank.
Source code in vllm/distributed/parallel_state.py
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 |
|
destroy
¶
Source code in vllm/distributed/parallel_state.py
dispatch
¶
Source code in vllm/distributed/parallel_state.py
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 vllm/distributed/parallel_state.py
graph_capture
¶
graph_capture(
graph_capture_context: Optional[
GraphCaptureContext
] = None,
)
Source code in vllm/distributed/parallel_state.py
recv
¶
Receives a tensor from the source rank.
Source code in vllm/distributed/parallel_state.py
recv_object
¶
Receive the input object list from the source rank.
Source code in vllm/distributed/parallel_state.py
recv_tensor_dict
¶
recv_tensor_dict(
src: Optional[int] = None,
all_gather_group: Optional[GroupCoordinator] = None,
) -> Optional[dict[str, Union[Tensor, Any]]]
Recv the input tensor dictionary.
NOTE: src
is the local rank of the source rank.
Source code in vllm/distributed/parallel_state.py
reduce_scatter
¶
Source code in vllm/distributed/parallel_state.py
send
¶
Sends a tensor to the destination rank in a non-blocking way
Source code in vllm/distributed/parallel_state.py
send_object
¶
Send the input object list to the destination rank.
Source code in vllm/distributed/parallel_state.py
send_tensor_dict
¶
send_tensor_dict(
tensor_dict: dict[str, Union[Tensor, Any]],
dst: Optional[int] = None,
all_gather_group: Optional[GroupCoordinator] = None,
) -> Optional[dict[str, Union[Tensor, Any]]]
Send the input tensor dictionary.
NOTE: dst
is the local rank of the source rank.
Source code in vllm/distributed/parallel_state.py
_get_unique_name
¶
Get a unique name for the group. Example: _get_unique_name("tp") -> "tp:0" _get_unique_name("tp") -> "tp:1"
Source code in vllm/distributed/parallel_state.py
_node_count
¶
_node_count(
pg: Union[ProcessGroup, StatelessProcessGroup],
) -> int
Returns the total number of nodes in the process group.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pg
|
Union[ProcessGroup, StatelessProcessGroup]
|
The process group to analyze |
required |
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The total number of nodes |
Source code in vllm/distributed/parallel_state.py
_register_group
¶
_register_group(group: GroupCoordinator) -> None
_split_tensor_dict
¶
_split_tensor_dict(
tensor_dict: dict[str, Union[Tensor, Any]],
) -> tuple[list[tuple[str, Any]], list[Tensor]]
Split the tensor dictionary into two parts: 1. A list of (key, value) pairs. If the value is a tensor, it is replaced by its metadata. 2. A list of tensors.
Source code in vllm/distributed/parallel_state.py
all_gather
¶
Source code in vllm/distributed/parallel_state.py
all_gather_fake
¶
Source code in vllm/distributed/parallel_state.py
all_reduce
¶
Source code in vllm/distributed/parallel_state.py
all_reduce_fake
¶
cleanup_dist_env_and_memory
¶
cleanup_dist_env_and_memory(shutdown_ray: bool = False)
Source code in vllm/distributed/parallel_state.py
destroy_distributed_environment
¶
destroy_model_parallel
¶
Set the groups to none and destroy them.
Source code in vllm/distributed/parallel_state.py
ensure_model_parallel_initialized
¶
ensure_model_parallel_initialized(
tensor_model_parallel_size: int,
pipeline_model_parallel_size: int,
backend: Optional[str] = None,
) -> None
Helper to initialize model parallel groups if they are not initialized, or ensure tensor-parallel and pipeline-parallel sizes are equal to expected values if the model parallel groups are initialized.
Source code in vllm/distributed/parallel_state.py
get_dp_group
¶
get_dp_group() -> GroupCoordinator
get_ep_group
¶
get_ep_group() -> GroupCoordinator
get_node_count
¶
get_node_count() -> int
Return the total number of nodes in the distributed environment.
get_pp_group
¶
get_pp_group() -> GroupCoordinator
get_tensor_model_parallel_rank
¶
get_tensor_model_parallel_world_size
¶
get_tp_group
¶
get_tp_group() -> GroupCoordinator
get_world_group
¶
get_world_group() -> GroupCoordinator
graph_capture
¶
graph_capture(device: device)
graph_capture
is a context manager which should surround the code that
is capturing the CUDA graph. Its main purpose is to ensure that the
some operations will be run after the graph is captured, before the graph
is replayed. It returns a GraphCaptureContext
object which contains the
necessary data for the graph capture. Currently, it only contains the
stream that the graph capture is running on. This stream is set to the
current CUDA stream when the context manager is entered and reset to the
default stream when the context manager is exited. This is to ensure that
the graph capture is running on a separate stream from the default stream,
in order to explicitly distinguish the kernels to capture
from other kernels possibly launched on background in the default stream.
Source code in vllm/distributed/parallel_state.py
in_the_same_node_as
¶
in_the_same_node_as(
pg: Union[ProcessGroup, StatelessProcessGroup],
source_rank: int = 0,
) -> list[bool]
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 vllm/distributed/parallel_state.py
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 |
|
init_distributed_environment
¶
init_distributed_environment(
world_size: int = -1,
rank: int = -1,
distributed_init_method: str = "env://",
local_rank: int = -1,
backend: str = "nccl",
)
Source code in vllm/distributed/parallel_state.py
init_model_parallel_group
¶
init_model_parallel_group(
group_ranks: list[list[int]],
local_rank: int,
backend: str,
use_message_queue_broadcaster: bool = False,
group_name: Optional[str] = None,
) -> GroupCoordinator
Source code in vllm/distributed/parallel_state.py
init_world_group
¶
init_world_group(
ranks: list[int], local_rank: int, backend: str
) -> GroupCoordinator
Source code in vllm/distributed/parallel_state.py
initialize_model_parallel
¶
initialize_model_parallel(
tensor_model_parallel_size: int = 1,
pipeline_model_parallel_size: int = 1,
backend: Optional[str] = None,
) -> None
Initialize model parallel groups.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor_model_parallel_size
|
int
|
number of GPUs used for tensor model parallelism. |
1
|
pipeline_model_parallel_size
|
int
|
number of GPUs used for pipeline model parallelism. |
1
|
Let's say we have a total of 8 GPUs denoted by g0 ... g7 and we use 2 GPUs to parallelize the model tensor, and 4 GPUs to parallelize the model pipeline. The present function will create 4 tensor model-parallel groups and 2 pipeline model-parallel groups: 4 tensor model-parallel groups: [g0, g1], [g2, g3], [g4, g5], [g6, g7] 2 pipeline model-parallel groups: [g0, g2, g4, g6], [g1, g3, g5, g7] Note that for efficiency, the caller should make sure adjacent ranks are on the same DGX box. For example if we are using 2 DGX-1 boxes with a total of 16 GPUs, rank 0 to 7 belong to the first box and ranks 8 to 15 belong to the second box.
Source code in vllm/distributed/parallel_state.py
977 978 979 980 981 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 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 |
|
is_global_first_rank
¶
is_global_first_rank() -> bool
Check if the current process is the first rank globally across all parallelism strategies (PP, TP, DP, EP, etc.).
Unlike group-specific checks like get_tensor_model_parallel_rank() == 0
or get_pp_group().is_first_rank
, this function checks the global rank
across all parallelism dimensions.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if this is the global first rank (rank 0), False otherwise. Returns True if distributed is not initialized (single process). |
Source code in vllm/distributed/parallel_state.py
model_parallel_is_initialized
¶
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 vllm/distributed/parallel_state.py
prepare_communication_buffer_for_model
¶
prepare_communication_buffer_for_model(model: Module)
Prepare the communication buffer for the model. Traditional communication libraries like NCCL are almost model agnostic. However, emerging new communication libraries like MoE all2all (DeepEP) usually allocate the communication buffer based on the model shape for optimal performance.