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_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/pipeline parallelism, you can skip the model parallel initialization and destruction steps.
Classes:
-
GroupCoordinator–PyTorch ProcessGroup wrapper for a group of processes.
-
Handle–Minimal async work handle used by P2P send/recv methods.
Functions:
-
destroy_model_parallel–Set the groups to none and destroy them.
-
ensure_model_parallel_initialized–Helper to initialize model parallel groups if they are not initialized,
-
get_node_count–Return the total number of nodes in the distributed environment.
-
get_tensor_model_parallel_rank–Return my rank for the tensor model parallel group.
-
get_tensor_model_parallel_world_size–Return world size for the tensor model parallel group.
-
graph_capture–graph_captureis a context manager which should surround the code that -
in_the_same_node_as–This is a collective operation that returns if each rank is in the same node
-
initialize_model_parallel–Initialize model parallel groups.
-
is_global_first_rank–Check if the current process is the first rank globally across all
-
is_local_first_rank–Check if the current process is the first local rank (rank 0 on its node).
-
model_parallel_is_initialized–Check if tensor and pipeline parallel groups are initialized.
-
prepare_communication_buffer_for_model–Prepare the communication buffer for the model.
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.
Methods:
-
all_reduce–User-facing all-reduce function before we actually call the
-
barrier–Barrier synchronization among the group.
-
broadcast–Broadcast the input tensor.
-
broadcast_object–Broadcast the input object.
-
broadcast_object_list–Broadcast the input object list.
-
broadcast_tensor_dict–Broadcast the input tensor dictionary.
-
gather–NOTE: We assume that the input tensor is on the same device across
-
make_sibling_device_group–Create a new device-side ProcessGroup with the same per-rank membership
-
recv–Receives a tensor from the source rank.
-
recv_object–Receive the input object list from the source rank.
-
recv_tensor_dict–Recv the input tensor dictionary.
-
send–Sends a tensor to the destination rank in a blocking way
-
send_object–Send the input object list to the destination rank.
-
send_tensor_dict–Send the input tensor dictionary.
Attributes:
-
first_rank–Return the global rank of the first process in the group
-
is_first_rank–Return whether the caller is the first process in the group
-
is_last_rank–Return whether the caller is the last process in the group
-
last_rank–Return the global rank of the last process in the group
-
next_rank–Return the global rank of the process that follows the caller
-
prev_rank–Return the global rank of the process that precedes the caller
Source code in vllm/distributed/parallel_state.py
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 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 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 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 | |
first_rank property ¶
Return the global rank of the first process in the group
is_first_rank property ¶
Return whether the caller is the first process in the group
is_last_rank property ¶
Return whether the caller is the last process in the group
last_rank property ¶
Return the global rank of the last process in the group
next_rank property ¶
Return the global rank of the process that follows the caller
prev_rank property ¶
Return the global rank of the process that precedes the caller
all_reduce(input_) ¶
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(input_, src=0) ¶
Broadcast the input tensor. NOTE: src is the local rank of the source rank.
Source code in vllm/distributed/parallel_state.py
broadcast_object(obj=None, src=0) ¶
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(obj_list, src=0, group=None) ¶
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(tensor_dict=None, src=0, group=None, metadata_group=None) ¶
Broadcast the input tensor dictionary. NOTE: src is the local rank of the source rank.
Source code in vllm/distributed/parallel_state.py
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 | |
gather(input_, dst=0, dim=-1) ¶
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
make_sibling_device_group(group_desc=None) ¶
Create a new device-side ProcessGroup with the same per-rank membership as this coordinator's device_group, but backed by a distinct communicator. This is a collective call: every world rank must invoke it. Used where we want to issue ops that can run concurrently with ops on device_group.
Source code in vllm/distributed/parallel_state.py
recv(size, dtype, src=None) ¶
Receives a tensor from the source rank.
Source code in vllm/distributed/parallel_state.py
recv_object(src) ¶
Receive the input object list from the source rank.
Source code in vllm/distributed/parallel_state.py
recv_tensor_dict(src=None, all_gather_group=None, all_gather_tensors=None) ¶
Recv the input tensor dictionary. NOTE: src is the local rank of the source rank.
The group for the all-gather operation. If provided,
an optimization is enabled where each rank in the group sends a slice of a tensor and the receiver reconstructs it using an all-gather, which can improve performance. This is typically the tensor-parallel group.
all_gather_tensors: A dictionary to specify which tensors should use the all-gather optimization, which is only effective when all_gather_group is provided. By default, this optimization is on for any tensor whose size is divisible by the all_gather_group's world size. However, it should be disabled for tensors that are not fully replicated across the group (e.g., the residual tensor when sequence parallelism is enabled). This dictionary allows overriding the default behavior on a per-tensor basis.
Source code in vllm/distributed/parallel_state.py
send(tensor, dst=None) ¶
Sends a tensor to the destination rank in a blocking way
Source code in vllm/distributed/parallel_state.py
send_object(obj, dst) ¶
Send the input object list to the destination rank.
Source code in vllm/distributed/parallel_state.py
send_tensor_dict(tensor_dict, dst=None, all_gather_group=None, all_gather_tensors=None) ¶
Send the input tensor dictionary. NOTE: dst is the local rank of the source rank.
The group for the all-gather operation. If provided,
an optimization is enabled where each rank in the group sends a slice of a tensor and the receiver reconstructs it using an all-gather, which can improve performance. This is typically the tensor-parallel group.
all_gather_tensors: A dictionary to specify which tensors should use the all-gather optimization, which is only effective when all_gather_group is provided. By default, this optimization is on for any tensor whose size is divisible by the all_gather_group's world size. However, it should be disabled for tensors that are not fully replicated across the group (e.g., the residual tensor when sequence parallelism is enabled). This dictionary allows overriding the default behavior on a per-tensor basis.
Source code in vllm/distributed/parallel_state.py
Handle ¶
_create_subgroups_split_group(group_ranks, group_name, torch_distributed_backend) ¶
Create the device + CPU subgroups for GroupCoordinator via torch.distributed.split_group.
split_group is collective on the parent group, so every parent rank must enter with the same split_ranks definition. Each rank receives the subgroup it belongs to.
Source code in vllm/distributed/parallel_state.py
_device_backend_str(torch_distributed_backend) ¶
Normalize torch_distributed_backend to the "<device>:<backend>" format required by split_group's backend argument.
Accepts either a bare backend name (e.g. "nccl") or an already-prefixed string (e.g. "cuda:nccl").
Source code in vllm/distributed/parallel_state.py
_get_unique_name(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
_init_process_group_for_split_group(*, backend, distributed_init_method, world_size, rank, local_rank, timeout) ¶
Initialize the default PG with both CPU (gloo) and device (e.g. nccl) backends and an eager device_id binding so that subgroups can be created via split_group (which requires the parent communicator to be eagerly initialized). Falls back to gloo on CPU-only systems.
Source code in vllm/distributed/parallel_state.py
_init_stateless_group(group_ranks, group_name, host, backend, coord_store, use_device_communicator=True) ¶
Create a StatelessGroupCoordinator with the given parameters.
Source code in vllm/distributed/parallel_state.py
_node_count(pg) ¶
Returns the total number of nodes in the process group.
Parameters:
-
(pg¶ProcessGroup | StatelessProcessGroup) –The process group to analyze
Returns:
-
int(int) –The total number of nodes
Source code in vllm/distributed/parallel_state.py
_platform_device_type() ¶
Return the device-type string (e.g. "cuda", "xpu", "cpu") for the current platform, in the form expected by torch.distributed.init_process_group(backend=...).
Source code in vllm/distributed/parallel_state.py
_replace_active_groups(*, world, dp, ep, eplb, node_count) ¶
Destroy the current DP/EP/WORLD/EPLB groups and replace them.
Destruction is collective — all ranks in the old groups must call this function together. Pass all-None to tear down without replacement.
Source code in vllm/distributed/parallel_state.py
_split_tensor_dict(tensor_dict) ¶
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
_validate_default_pg_for_split_group() ¶
When an external launcher (e.g. torchrun) initialized the default PG, GroupCoordinator cannot patch in additional backends or change the eager-init behavior — split_group only selects subsets of an existing parent. Validate that the parent has both device_id and a CPU (gloo) backend, and emit a descriptive error pointing at the exact init call to update otherwise.
Source code in vllm/distributed/parallel_state.py
destroy_model_parallel() ¶
Set the groups to none and destroy them.
Source code in vllm/distributed/parallel_state.py
ensure_model_parallel_initialized(tensor_model_parallel_size, pipeline_model_parallel_size, prefill_context_model_parallel_size=1, decode_context_model_parallel_size=1, backend=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_node_count() ¶
Return the total number of nodes in the distributed environment.
get_tensor_model_parallel_rank() ¶
get_tensor_model_parallel_world_size() ¶
graph_capture(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 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(pg, source_rank=0) ¶
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
2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 | |
initialize_model_parallel(tensor_model_parallel_size=1, pipeline_model_parallel_size=1, prefill_context_model_parallel_size=1, decode_context_model_parallel_size=1, backend=None) ¶
Initialize model parallel groups.
Parameters:
-
(tensor_model_parallel_size¶int, default:1) –number of GPUs used for tensor model parallelism.
-
(pipeline_model_parallel_size¶int, default:1) –number of GPUs used for pipeline model parallelism.
-
(backend¶str | None, default:None) –name of torch distributed communication backend.
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
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 | |
is_global_first_rank() ¶
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:
-
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
is_local_first_rank() ¶
Check if the current process is the first local rank (rank 0 on its node).
Source code in vllm/distributed/parallel_state.py
model_parallel_is_initialized() ¶
prepare_communication_buffer_for_model(model) ¶
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.