vllm.distributed ¶
Modules:
Name | Description |
---|---|
communication_op | |
device_communicators | |
envs | |
eplb | Expert parallelism load balancer (EPLB). |
kv_events | |
kv_transfer | |
parallel_state | vLLM distributed state. |
tpu_distributed_utils | |
utils | |
TensorMetadata module-attribute
¶
TensorMetadata = namedtuple(
"TensorMetadata", ["device", "dtype", "size"]
)
USE_SCHED_YIELD module-attribute
¶
USE_SCHED_YIELD = (
version_info[:3] >= (3, 11, 1)
or version_info[:2] == (3, 10)
and version_info[2] >= 8
)
get_context_model_parallel_group module-attribute
¶
get_context_model_parallel_group = get_dcp_group
DeviceCommunicatorBase ¶
Base class for device-specific communicator. 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 vllm/distributed/device_communicators/base_device_communicator.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 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 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 |
|
__init__ ¶
__init__(
cpu_group: ProcessGroup,
device: Optional[device] = None,
device_group: Optional[ProcessGroup] = None,
unique_name: str = "",
)
Source code in vllm/distributed/device_communicators/base_device_communicator.py
all_gather ¶
Source code in vllm/distributed/device_communicators/base_device_communicator.py
all_gatherv ¶
all_gatherv(
input_: Union[Tensor, list[Tensor]],
dim: int = 0,
sizes: Optional[list[int]] = None,
) -> Union[Tensor, list[Tensor]]
Source code in vllm/distributed/device_communicators/base_device_communicator.py
all_reduce ¶
combine ¶
Combine the hidden states and router logits from the appropriate device. This is a no-op in the base class.
Source code in vllm/distributed/device_communicators/base_device_communicator.py
destroy ¶
dispatch ¶
dispatch(
hidden_states: Tensor,
router_logits: Tensor,
is_sequence_parallel: bool = False,
) -> tuple[Tensor, Tensor]
Dispatch the hidden states and router logits to the appropriate device. This is a no-op in the base class.
Source code in vllm/distributed/device_communicators/base_device_communicator.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/device_communicators/base_device_communicator.py
prepare_communication_buffer_for_model ¶
prepare_communication_buffer_for_model(
model: Module,
) -> None
Prepare the communication buffer for the model.
Source code in vllm/distributed/device_communicators/base_device_communicator.py
recv ¶
Receives a tensor from the source rank.
Source code in vllm/distributed/device_communicators/base_device_communicator.py
reduce_scatter ¶
Source code in vllm/distributed/device_communicators/base_device_communicator.py
reduce_scatterv ¶
send ¶
Sends a tensor to the destination rank in a blocking way
Source code in vllm/distributed/device_communicators/base_device_communicator.py
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
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 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 |
|
device_communicator instance-attribute
¶
device_communicator: Optional[DeviceCommunicatorBase] = None
use_cpu_custom_send_recv instance-attribute
¶
use_cpu_custom_send_recv = is_cpu() and hasattr(
_C, "init_shm_manager"
)
__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
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 |
|
_all_gather_out_place ¶
_all_reduce_out_place ¶
_reduce_scatter_out_place ¶
all_gather ¶
Source code in vllm/distributed/parallel_state.py
all_gatherv ¶
all_gatherv(
input_: Union[Tensor, list[Tensor]],
dim: int = 0,
sizes: Optional[list[int]] = None,
)
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
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 |
|
combine ¶
Source code in vllm/distributed/parallel_state.py
destroy ¶
Source code in vllm/distributed/parallel_state.py
dispatch ¶
dispatch(
hidden_states: Tensor,
router_logits: Tensor,
is_sequence_parallel: bool = False,
) -> tuple[Tensor, Tensor]
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,
all_gather_tensors: Optional[dict[str, bool]] = None,
) -> Optional[dict[str, Union[Tensor, Any]]]
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
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 |
|
reduce_scatter ¶
Source code in vllm/distributed/parallel_state.py
reduce_scatterv ¶
Source code in vllm/distributed/parallel_state.py
send ¶
Sends a tensor to the destination rank in a 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,
all_gather_tensors: Optional[dict[str, bool]] = None,
) -> Optional[dict[str, Union[Tensor, Any]]]
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
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 |
|
StatelessProcessGroup dataclass
¶
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.
Source code in vllm/distributed/utils.py
141 142 143 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 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 |
|
broadcast_recv_src_counter class-attribute
instance-attribute
¶
entries class-attribute
instance-attribute
¶
recv_src_counter class-attribute
instance-attribute
¶
send_dst_counter class-attribute
instance-attribute
¶
__init__ ¶
__init__(
rank: int,
world_size: int,
store: Store,
socket: Optional[socket],
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(),
) -> None
__post_init__ ¶
Source code in vllm/distributed/utils.py
all_gather_obj ¶
All gather an object from all ranks.
Source code in vllm/distributed/utils.py
barrier ¶
barrier(timeout: float = 30.0)
A robust barrier to synchronize all ranks.
Uses a multi-phase approach to ensure all processes reach the barrier before proceeding:
-
Each process signals it has reached the barrier
-
Each process signals that it has confirmed the arrival of all other ranks.
-
Rank 0 waits for all other ranks to signal their departure to ensure that all ranks have departed the barrier first.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout | float | Maximum time in seconds to wait for each phase (in seconds) | 30.0 |
Raises:
Type | Description |
---|---|
RuntimeError | If coordination fails or times out |
Source code in vllm/distributed/utils.py
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 |
|
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 vllm/distributed/utils.py
create staticmethod
¶
create(
host: str,
port: int,
rank: int,
world_size: int,
data_expiration_seconds: int = 3600,
store_timeout: int = 300,
) -> 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 vllm/distributed/utils.py
expire_data ¶
Expire data that is older than data_expiration_seconds
seconds.
Source code in vllm/distributed/utils.py
recv_obj ¶
Receive an object from a source rank.
send_obj ¶
Send an object to a destination rank.
Source code in vllm/distributed/utils.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 ¶
broadcast_tensor_dict ¶
broadcast_tensor_dict(
tensor_dict: Optional[
dict[Any, Union[Tensor, Any]]
] = None,
src: int = 0,
)
Source code in vllm/distributed/communication_op.py
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
direct_register_custom_op ¶
direct_register_custom_op(
op_name: str,
op_func: Callable,
mutates_args: list[str] | None = None,
fake_impl: Callable | None = None,
target_lib: Library | None = None,
dispatch_key: str | None = None,
tags: tuple[Tag, ...] = (),
)
torch.library.custom_op
can have significant overhead because it needs to consider complicated dispatching logic. This function directly registers a custom op and dispatches it to the CUDA backend. See https://gist.github.com/youkaichao/ecbea9ec9fc79a45d2adce1784d7a9a5 for more details.
By default, the custom op is registered to the vLLM library. If you want to register it to a different library, you can pass the library object to the target_lib
argument.
IMPORTANT: the lifetime of the operator is tied to the lifetime of the library object. If you want to bind the operator to a different library, make sure the library object is alive when the operator is used.
Source code in vllm/utils/__init__.py
divide ¶
Ensure that numerator is divisible by the denominator and return the division value.
ensure_divisibility ¶
Ensure that numerator is divisible by the denominator.
ensure_model_parallel_initialized ¶
ensure_model_parallel_initialized(
tensor_model_parallel_size: int,
pipeline_model_parallel_size: int,
decode_context_model_parallel_size: Optional[int] = 1,
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_dcp_group ¶
get_dcp_group() -> GroupCoordinator
get_decode_context_model_parallel_rank ¶
get_decode_context_model_parallel_world_size ¶
get_distributed_init_method ¶
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_pipeline_model_parallel_group ¶
Source code in vllm/distributed/parallel_state.py
get_pp_group ¶
get_pp_group() -> GroupCoordinator
get_pp_indices ¶
Try to evenly distribute layers across partitions.
If the number of layers is not divisible by the number of partitions, the remaining layers are evenly distributed across all but the last partition. The last partition is excluded because it often contains an additional norm layer and we are attempting to balance compute.
If pp_size > 2
and the number of remaining layers is 0 < x <= pp_size - 2
then the remaining layers are evenly distributed across the middle partitions. The first and last partitions are excluded because they contain the input and output embeddings respectively and we are attempting to reduce maximum memory consumption across partitions.
Source code in vllm/distributed/utils.py
get_tcp_uri ¶
get_tensor_model_parallel_group ¶
Source code in vllm/distributed/parallel_state.py
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 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
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 |
|
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",
timeout: Optional[timedelta] = None,
)
Source code in vllm/distributed/parallel_state.py
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 |
|
init_gloo_process_group ¶
init_gloo_process_group(
backend: Backend,
prefix_store: PrefixStore,
group_rank: int,
group_size: int,
timeout: timedelta,
) -> ProcessGroup
Stateless init ProcessGroup with gloo backend compatible with different torch versions.
Source code in vllm/distributed/utils.py
init_logger ¶
init_logger(name: str) -> _VllmLogger
The main purpose of this function is to ensure that loggers are retrieved in such a way that we can be sure the root vllm logger has already been configured.
Source code in vllm/logger.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,
decode_context_model_parallel_size: Optional[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 |
backend | Optional[str] | name of torch distributed communication backend. | None |
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
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 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 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 |
|
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
is_torch_equal_or_newer ¶
Check if the installed torch version is >= the target version.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target | str | a version string, like "2.6.0". | required |
Returns:
Type | Description |
---|---|
bool | Whether the condition meets. |
Source code in vllm/utils/__init__.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
patched_fused_scaled_matmul_reduce_scatter ¶
patched_fused_scaled_matmul_reduce_scatter(
A: Tensor,
B: Tensor,
A_scale: Tensor,
B_scale: Tensor,
reduce_op: str,
orig_scatter_dim: int,
scatter_dim_after_maybe_reshape: int,
group_name: str,
output_shape: list[int],
bias: Tensor | None = None,
result_scale: Tensor | None = None,
out_dtype: dtype | None = None,
use_fast_accum: bool = False,
) -> Tensor
Source code in vllm/distributed/parallel_state.py
patched_fused_scaled_matmul_reduce_scatter_fake ¶
patched_fused_scaled_matmul_reduce_scatter_fake(
A: Tensor,
B: Tensor,
A_scale: Tensor,
B_scale: Tensor,
reduce_op: str,
orig_scatter_dim: int,
scatter_dim_after_maybe_reshape: int,
group_name: str,
output_shape: list[int],
bias: Tensor | None = None,
result_scale: Tensor | None = None,
out_dtype: dtype | None = None,
use_fast_accum: bool = False,
) -> Tensor
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.
Source code in vllm/distributed/parallel_state.py
reduce_scatter ¶
Source code in vllm/distributed/parallel_state.py
reduce_scatter_fake ¶
Source code in vllm/distributed/parallel_state.py
resolve_obj_by_qualname ¶
Resolve an object by its fully-qualified class name.
Source code in vllm/utils/__init__.py
sched_yield ¶
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 vllm/distributed/utils.py
stateless_destroy_torch_distributed_process_group ¶
Destroy ProcessGroup returned by stateless_init_torch_distributed_process_group().
Source code in vllm/distributed/utils.py
stateless_init_torch_distributed_process_group ¶
stateless_init_torch_distributed_process_group(
host: str,
port: int,
rank: int,
world_size: int,
backend: str,
) -> ProcessGroup
A replacement for torch.distributed.init_process_group
that does not pollute the global state. The created ProcessGroup object can be used for some operations such as allreduce
, because it does not depend on the global rank. However, some operations such as broadcast
cannot be used because it depends on the global rank.
TODO: ask for help from PyTorch team if we need the broadcast
operation.¶
This function is useful when we are not sure about the total number of processes in the process group. For example, we may have process 1, 2, ..., 8 who want to communicate, and process 9 might be the same process as process 1, or it might be a different process; process 10 might be the same process as process 5, or it might be a different process. In this case, how can we reliably form a communication channel within process 9 and 10, without affecting the communication channel within process 1, 2, ..., 8?
One possible solution is to figure out if process 9 and 10 are the same as process 1 and 5 beforehand, and then form a communication channel based on the information, adjusting the ranks and world_size etc. However, figuring out the information is not always easy, and it will interfere with the main communication channel.
Our solution is to always form a communication channel with process 1, 2, ..., 8, and then use this function to form another communication channel with process 9 and 10. This way, regardless of whether process 9 and 10 are the same as process 1 and 5, the main communication channel is always formed with process 1, 2, ..., 8, and the additional communication channel is formed with process 9 and 10.
Source code in vllm/distributed/utils.py
tensor_model_parallel_all_gather ¶
All-gather the input tensor across model parallel group.
tensor_model_parallel_all_reduce ¶
tensor_model_parallel_gather ¶
Gather the input tensor across model parallel group.
tensor_model_parallel_reduce_scatter ¶
Reduce-Scatter the input tensor across model parallel group.