vllm.distributed.eplb.rebalance_execute
¶
The actual execution of the rearrangement.
This involves the exchange of expert weights between GPUs.
Classes:
-
TransferMetadata–Metadata describing a completed EPLB buffer transfer.
Functions:
-
move_from_buffer–Copies expert weights from communication buffers back to the target weight
-
transfer_layer–Rearranges the expert weights in place according to the new expert indices.
AsyncEplbLayerResult
dataclass
¶
The result of one completed async EPLB layer transfer.
Attributes:
-
consumed_event(CpuGpuEvent) –Event used to synchronize access to the intermediate buffer. The async worker calls
-
layer_idx(int) –Index of the MoE layer that was transferred.
-
new_physical_to_logical_map(Tensor) –New physical→logical mapping for layers_idx, on CPU.
-
transfer_metadata(TransferMetadata) –Metadata describing what was received during transfer_layer.
Source code in vllm/distributed/eplb/rebalance_execute.py
consumed_event
instance-attribute
¶
Event used to synchronize access to the intermediate buffer. The async worker calls wait() after it finishes transferring weights to the intermediate buffer. The main thread calls record() after it finishes transferring weights out of the intermediate buffer in _move_to_workspace()
layer_idx
instance-attribute
¶
Index of the MoE layer that was transferred.
new_physical_to_logical_map
instance-attribute
¶
New physical→logical mapping for layers_idx, on CPU. Shape: (num_physical_experts)
transfer_metadata
instance-attribute
¶
Metadata describing what was received during transfer_layer.
TransferMetadata
dataclass
¶
Metadata describing a completed EPLB buffer transfer.
Attributes:
-
is_received_locally(ndarray) –Mask of (num_local_experts,) indicating experts received from local data.
-
is_unchanged(ndarray) –Mask of (num_local_experts,) indicating experts unchanged after rebalance.
-
recv_count(int) –Number of received experts for the layer.
-
recv_dst_rows(ndarray) –Target expert indices (num_local_experts,) in local tensors to send.
-
recv_expert_ids(ndarray) –Expert ids (num_local_experts,) of remote primary experts.
-
recv_primary_mask(ndarray) –Mask of (num_local_experts,) indicating primary experts received.
Source code in vllm/distributed/eplb/rebalance_execute.py
is_received_locally
instance-attribute
¶
Mask of (num_local_experts,) indicating experts received from local data.
is_unchanged
instance-attribute
¶
Mask of (num_local_experts,) indicating experts unchanged after rebalance.
recv_count
instance-attribute
¶
Number of received experts for the layer.
recv_dst_rows
instance-attribute
¶
Target expert indices (num_local_experts,) in local tensors to send.
recv_expert_ids
instance-attribute
¶
Expert ids (num_local_experts,) of remote primary experts.
recv_primary_mask
instance-attribute
¶
Mask of (num_local_experts,) indicating primary experts received.
_execute_migration_batch(batch, old_indices, old_rows, new_rows, ep_rank, expert_weights, expert_weights_buffers, communicator, layer_idx)
¶
Execute one batch of rank-pair-disjoint expert transfers.
Source code in vllm/distributed/eplb/rebalance_execute.py
_execute_migration_batches(num_local_experts, old_indices, new_indices, ep_rank, expert_weights, expert_weights_buffers, communicator, layer_idx, batches=None)
¶
Execute all contention-aware migration batches for one layer.
Source code in vllm/distributed/eplb/rebalance_execute.py
_map_old_expert_indices_with_rank_mapping(old_global_expert_indices, rank_mapping, new_ep_size)
¶
Map the old global expert indices to the new global expert indices.
Parameters:
-
(old_global_expert_indices¶Tensor) –Shape (num_layers, old_ep_size * num_local_physical_experts).
-
(rank_mapping¶dict[int, int]) –Mapping from old rank to new rank.
-
(new_ep_size¶int) –New expert parallelism size.
Returns:
-
Tensor–Mapped expert indices with shape
-
Tensor–(num_layers, new_ep_size * num_local_physical_experts).
Source code in vllm/distributed/eplb/rebalance_execute.py
get_ep_ranks_with_experts_batch(expert_ids, num_local_experts, old_indices, new_indices)
¶
Get the ranks of the experts that need to be exchanged.
Parameters:
-
(expert_ids¶ndarray) –1D array of expert indices to query.
-
(num_local_experts¶int) –The number of local experts.
-
(old_indices¶ndarray) –The old indices of the experts.
-
(new_indices¶ndarray) –The new indices of the experts.
Returns:
-
dict[int, list[int]]–A tuple of two dictionaries mapping expert_id to:
-
dict[int, list[int]]–- ranks_to_send: The ranks that have this expert and need to send.
-
tuple[dict[int, list[int]], dict[int, list[int]]]–- ranks_to_recv: The ranks that need to receive this expert.
Source code in vllm/distributed/eplb/rebalance_execute.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 | |
move_from_buffer(expert_weights, expert_weights_buffers, transfer_metadata, new_indices, ep_rank)
¶
Copies expert weights from communication buffers back to the target weight tensors after EPLB rebalancing.
Parameters:
-
(expert_weights¶Sequence[Tensor]) –List of the actual MoE layer weights used in the execution.
-
(expert_weights_buffers¶list[Tensor]) –Intermediate buffers containing the experts weights after the transfer is completed.
-
(transfer_metadata¶TransferMetadata) –TransferMetadata containing transfer metadata.
-
(new_indices¶ndarray) –(num_experts_total,) mapping from local rows to desired (possibly global) expert id, after rebalance.
-
(ep_rank¶int) –Rank of the process in the expert parallel group.
Source code in vllm/distributed/eplb/rebalance_execute.py
move_to_buffer(num_local_experts, old_indices, new_indices, expert_weights, expert_weights_buffers, stream, ep_rank, communicator, layer_idx=0, enable_migration_batching=False, migration_batches=None)
¶
Rearranges expert weights during EPLB rebalancing.
Parameters:
-
(num_local_experts¶int) –Number of local experts.
-
(old_indices¶ndarray) –(num_experts_total,) ndarray of current (old) global-to-locals expert assignments.
-
(new_indices¶ndarray) –(num_experts_total,) ndarray of desired (new) global-to-local assignments after rebalance.
-
(expert_weights¶Sequence[Tensor]) –Original expert weights for the layer.
-
(expert_weights_buffers¶Sequence[Tensor]) –Intermediate buffers (one per tensor).
-
(stream¶Stream | None) –CUDA/XPU stream for async copies (can be None for sync mode).
-
(ep_rank¶int) –Rank of this process in expert parallel group.
-
(communicator¶EplbCommunicator) –EplbCommunicator instance for P2P communication.
-
(layer_idx¶int, default:0) –Index of the MoE layer being transferred.
-
(enable_migration_batching¶bool, default:False) –Schedule remote transfers in batches where each rank communicates with at most one peer.
-
(migration_batches¶list[list[MigrationFlow]] | None, default:None) –Optional batches precomputed for this layer.
Returns:
-
TransferMetadata(TransferMetadata) –Metadata needed for completing remote weight transfers.
Source code in vllm/distributed/eplb/rebalance_execute.py
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 | |
rearrange_expert_weights_inplace(old_global_expert_indices, new_global_expert_indices, expert_weights, expert_buffer, ep_group, communicator, is_profile=False, rank_mapping=None)
¶
Rearranges the expert weights in place according to the new expert indices.
The value of the indices arguments are logical indices of the experts, while keys are physical.
Parameters:
-
(old_global_expert_indices¶Tensor) –Shape (num_moe_layers, num_physical_experts).
-
(new_global_expert_indices¶Tensor) –Shape (num_moe_layers, num_physical_experts).
-
(expert_weights¶Sequence[Sequence[Tensor]]) –A sequence of shape (num_moe_layers)(weight_count) of tensors of shape (num_local_physical_experts, hidden_size_i). For example, a linear layer may have up and down projection, so weight_count = 2. Each weight's hidden size can be different.
-
(expert_buffer¶Sequence[Tensor]) –Pre-allocated receive buffer tensors (one per weight tensor in a single layer).
-
(ep_group¶ProcessGroup) –The device process group for expert parallelism.
-
(communicator¶EplbCommunicator) –EplbCommunicator instance for P2P communication.
-
(is_profile¶bool, default:False) –If
True, do not perform any actual weight copy. This is used during profile run, where we only perform dummy communications to reserve enough memory for the buffers. -
(rank_mapping¶dict[int, int] | None, default:None) –A dictionary mapping old rank to new rank.
Source code in vllm/distributed/eplb/rebalance_execute.py
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 | |
transfer_layer(old_layer_indices, new_layer_indices, expert_weights, expert_weights_buffer, ep_group, communicator, is_profile=False, stream=None, rank_mapping=None, layer_idx=0, enable_migration_batching=False, migration_batches=None)
¶
Rearranges the expert weights in place according to the new expert indices.
The value of the indices arguments are logical indices of the experts, while keys are physical.
Parameters:
-
(old_layer_indices¶Tensor) –Shape (num_physical_experts,).
-
(new_layer_indices¶Tensor) –Shape (num_physical_experts,).
-
(expert_weights¶Sequence[Tensor]) –Iterable of weight tensors for this layer, each with shape (num_local_physical_experts, hidden_size_i). For example, a linear layer may have up and down projection.
-
(expert_weights_buffer¶Sequence[Tensor]) –Intermediate buffers (one per weight tensor).
-
(ep_group¶ProcessGroup) –The device process group for expert parallelism.
-
(communicator¶EplbCommunicator) –EplbCommunicator instance for P2P communication.
-
(is_profile¶bool, default:False) –If
True, do not perform any actual weight copy. This is used during profile run, where we only perform dummy communications to reserve enough memory for the buffers. -
(stream¶Stream | None, default:None) –CUDA stream for async copies (can be None for sync mode).
-
(rank_mapping¶dict[int, int] | None, default:None) –Optional rank mapping for elastic expert parallelism.
-
(layer_idx¶int, default:0) –Index of the MoE layer being transferred.
-
(enable_migration_batching¶bool, default:False) –Schedule remote transfers in batches where each rank communicates with at most one peer.
-
(migration_batches¶list[list[MigrationFlow]] | None, default:None) –Optional batches precomputed for this layer.
Returns:
-
TransferMetadata(TransferMetadata) –Metadata needed for completing remote weight transfers, including is_unchanged and is_received_locally masks.
Source code in vllm/distributed/eplb/rebalance_execute.py
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 | |