vllm.distributed.eplb.rebalance_execute ¶
The actual execution of the rearrangement.
This involves the exchange of expert weights between GPUs.
AsyncEplbLayerResult dataclass ¶
The result of one completed async EPLB layer transfer.
Source code in vllm/distributed/eplb/rebalance_execute.py
consumed_event instance-attribute ¶
consumed_event: CpuGpuEvent
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()
new_physical_to_logical_map instance-attribute ¶
new_physical_to_logical_map: Tensor
New physical→logical mapping for layers_idx, on CPU. Shape: (num_physical_experts)
transfer_metadata instance-attribute ¶
transfer_metadata: TransferMetadata
Metadata describing what was received during transfer_layer.
TransferMetadata dataclass ¶
Metadata describing a completed EPLB buffer transfer.
Source code in vllm/distributed/eplb/rebalance_execute.py
is_received_locally instance-attribute ¶
is_received_locally: ndarray
Mask of (num_local_experts,) indicating experts received from local data.
is_unchanged instance-attribute ¶
is_unchanged: ndarray
Mask of (num_local_experts,) indicating experts unchanged after rebalance.
recv_dst_rows instance-attribute ¶
recv_dst_rows: ndarray
Target expert indices (num_local_experts,) in local tensors to send.
_map_old_expert_indices_with_rank_mapping ¶
_map_old_expert_indices_with_rank_mapping(
old_global_expert_indices: Tensor,
rank_mapping: dict[int, int],
new_ep_size: int,
) -> Tensor
Map the old global expert indices to the new global expert indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
old_global_expert_indices | Tensor | Shape (num_layers, old_ep_size * num_local_physical_experts). | required |
rank_mapping | dict[int, int] | Mapping from old rank to new rank. | required |
new_ep_size | int | New expert parallelism size. | required |
Returns:
| Type | Description |
|---|---|
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 ¶
get_ep_ranks_with_experts_batch(
expert_ids: ndarray,
num_local_experts: int,
old_indices: ndarray,
new_indices: ndarray,
) -> tuple[dict[int, list[int]], dict[int, list[int]]]
Get the ranks of the experts that need to be exchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expert_ids | ndarray | 1D array of expert indices to query. | required |
num_local_experts | int | The number of local experts. | required |
old_indices | ndarray | The old indices of the experts. | required |
new_indices | ndarray | The new indices of the experts. | required |
Returns:
| Type | Description |
|---|---|
dict[int, list[int]] | A tuple of two dictionaries mapping expert_id to: |
dict[int, list[int]] |
|
tuple[dict[int, list[int]], dict[int, list[int]]] |
|
Source code in vllm/distributed/eplb/rebalance_execute.py
65 66 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 | |
move_from_buffer ¶
move_from_buffer(
expert_weights: Sequence[Tensor],
expert_weights_buffers: list[Tensor],
transfer_metadata: TransferMetadata,
new_indices: ndarray,
ep_rank: int,
) -> None
Copies expert weights from communication buffers back to the target weight tensors after EPLB rebalancing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expert_weights | Sequence[Tensor] | List of the actual MoE layer weights used in the execution. | required |
expert_weights_buffers | list[Tensor] | Intermediate buffers containing the experts weights after the transfer is completed. | required |
transfer_metadata | TransferMetadata | TransferMetadata containing transfer metadata. | required |
new_indices | ndarray | (num_experts_total,) mapping from local rows to desired (possibly global) expert id, after rebalance. | required |
ep_rank | int | Rank of the process in the expert parallel group. | required |
Source code in vllm/distributed/eplb/rebalance_execute.py
move_to_buffer ¶
move_to_buffer(
num_local_experts: int,
old_indices: ndarray,
new_indices: ndarray,
expert_weights: Sequence[Tensor],
expert_weights_buffers: Sequence[Tensor],
cuda_stream: Stream | None,
ep_rank: int,
communicator: EplbCommunicator,
) -> TransferMetadata
Rearranges expert weights during EPLB rebalancing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_local_experts | int | Number of local experts. | required |
old_indices | ndarray | (num_experts_total,) ndarray of current (old) global-to-local expert assignments. | required |
new_indices | ndarray | (num_experts_total,) ndarray of desired (new) global-to-local assignments after rebalance. | required |
expert_weights | Sequence[Tensor] | Original expert weights for the layer. | required |
expert_weights_buffers | Sequence[Tensor] | Intermediate buffers (one per tensor). | required |
cuda_stream | Stream | None | CUDA stream for async copies (can be None for sync mode). | required |
ep_rank | int | Rank of this process in expert parallel group. | required |
communicator | EplbCommunicator | EplbCommunicator instance for P2P communication. | required |
Returns:
| Name | Type | Description |
|---|---|---|
TransferMetadata | TransferMetadata | Metadata needed for completing remote weight transfers. |
Source code in vllm/distributed/eplb/rebalance_execute.py
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 | |
rearrange_expert_weights_inplace ¶
rearrange_expert_weights_inplace(
old_global_expert_indices: Tensor,
new_global_expert_indices: Tensor,
expert_weights: Sequence[Sequence[Tensor]],
ep_group: ProcessGroup,
communicator: EplbCommunicator,
is_profile: bool = False,
rank_mapping: dict[int, int] | None = None,
) -> 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:
| Name | Type | Description | Default |
|---|---|---|---|
old_global_expert_indices | Tensor | Shape (num_moe_layers, num_physical_experts). | required |
new_global_expert_indices | Tensor | Shape (num_moe_layers, num_physical_experts). | required |
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. | required |
ep_group | ProcessGroup | The device process group for expert parallelism. | required |
communicator | EplbCommunicator | EplbCommunicator instance for P2P communication. | required |
is_profile | bool | If | False |
rank_mapping | dict[int, int] | None | A dictionary mapping old rank to new rank. | None |
Source code in vllm/distributed/eplb/rebalance_execute.py
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 | |
transfer_layer ¶
transfer_layer(
old_layer_indices: Tensor,
new_layer_indices: Tensor,
expert_weights: Sequence[Tensor],
expert_weights_buffer: Sequence[Tensor],
ep_group: ProcessGroup,
communicator: EplbCommunicator,
is_profile: bool = False,
cuda_stream: Stream | None = None,
rank_mapping: dict[int, int] | None = None,
) -> TransferMetadata
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:
| Name | Type | Description | Default |
|---|---|---|---|
old_layer_indices | Tensor | Shape (num_physical_experts,). | required |
new_layer_indices | Tensor | Shape (num_physical_experts,). | required |
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. | required |
expert_weights_buffer | Sequence[Tensor] | Intermediate buffers (one per weight tensor). | required |
ep_group | ProcessGroup | The device process group for expert parallelism. | required |
communicator | EplbCommunicator | EplbCommunicator instance for P2P communication. | required |
is_profile | bool | If | False |
cuda_stream | Stream | None | CUDA stream for async copies (can be None for sync mode). | None |
rank_mapping | dict[int, int] | None | Optional rank mapping for elastic expert parallelism. | None |
Returns:
| Name | Type | Description |
|---|---|---|
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
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 | |