Skip to content

vllm.distributed.eplb.rebalance_execute

The actual execution of the rearrangement.

This involves the exchange of expert weights between GPUs.

Classes:

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 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 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, recv_rows)

Execute one batch of rank-pair-disjoint expert transfers.

_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.

_first_rows(local_expert_ids)

Map each expert to its first local row in local_expert_ids.

_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).

_recv_masks(old_local_expert_ids, new_local_expert_ids)

Rows keeping their expert, and rows filled without a remote receive.

_remote_recv_rows(old_local_expert_ids, new_local_expert_ids)

Experts a rank receives from peers (sorted), and the first row needing each.

copy_rows(dst, dst_rows, src, src_rows)

d[i].copy_(s[j]) for every tensor pair (d, s) and row pair (i, j), in one launch per tensor on CUDA (expert weights are contiguous).

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:

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.

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.

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.

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.