[EPLB] Refactor Async EPLB synchronization logic (#37601)

Signed-off-by: Sage Moore <sage@neuralmagic.com>
Co-authored-by: Tyler Michael Smith <tyler@neuralmagic.com>
This commit is contained in:
Sage Moore
2026-04-20 17:05:41 +00:00
committed by GitHub
co-authored by Tyler Michael Smith
parent 726efe177b
commit 3461c8b027
6 changed files with 312 additions and 266 deletions
+64 -81
View File
@@ -14,7 +14,8 @@ from torch.distributed import ProcessGroup
from vllm.distributed.parallel_state import get_eplb_group
from vllm.logger import init_logger
from .rebalance_execute import transfer_layer
from .eplb_utils import CpuGpuEvent
from .rebalance_execute import AsyncEplbLayerResult, transfer_layer
if TYPE_CHECKING:
from .eplb_state import EplbModelState, EplbState
@@ -60,18 +61,14 @@ def run_rebalance_experts(
model_state: "EplbModelState",
eplb_state: "EplbState",
physical_to_logical_map_cpu: torch.Tensor,
) -> None:
cuda_stream: torch.cuda.Stream,
) -> torch.Tensor:
assert model_state.eplb_stats is not None
eplb_stats = model_state.eplb_stats
# Wait for the main thread's all-reduce and clone to complete before
# accessing the global_expert_load_window tensor.
assert model_state.window_ready_event is not None
model_state.window_ready_event.wait()
model_state.window_ready_event = None
# Move the global expert load window to CPU for computation.
global_expert_load_window = eplb_stats.global_expert_load_window.cpu()
with torch.cuda.stream(cuda_stream):
global_expert_load_window = eplb_stats.global_expert_load_window.cpu()
# Compute new expert mappings for the model
new_physical_to_logical_map = eplb_state.policy.rebalance_experts(
global_expert_load_window,
@@ -83,7 +80,7 @@ def run_rebalance_experts(
)
assert new_physical_to_logical_map.device == torch.device("cpu")
model_state.new_physical_to_logical_map = new_physical_to_logical_map
return new_physical_to_logical_map
async def transfer_run_periodically(
@@ -93,85 +90,71 @@ async def transfer_run_periodically(
is_profile: bool = False,
) -> None:
while True:
await asyncio.to_thread(state.rearrange_event.wait)
state.rearrange_event.wait(stream=cuda_stream)
logger.info("async worker woke up for EPLB transfer")
assert state.is_async
for model_state in state.model_states.values():
layer_idx = 0
# Set the async worker's CUDA stream on the communicator
model_state.communicator.set_stream(cuda_stream)
rebalancing_algorithm_executed = False
physical_to_logical_map_cpu = None
current_num_layers = model_state.model.num_moe_layers
while (
model_state.rebalanced
and model_state.layer_to_transfer < current_num_layers
):
if not model_state.ep_buffer_ready and model_state.rebalanced:
# Polling the lock directly in the async thread avoids
# the thread switch overhead of asyncio.to_thread.
# This is typically faster than offloading to a worker thread.
while not model_state.buffer_lock.acquire(blocking=False):
await asyncio.sleep(0)
try:
if model_state.layer_to_transfer >= current_num_layers:
break
if (
not rebalancing_algorithm_executed
or model_state.new_physical_to_logical_map is None
):
# Move the physical_to_logical_map to CPU
# for rebalancing and transfer_layer.
physical_to_logical_map_cpu = (
model_state.physical_to_logical_map.cpu()
)
run_rebalance_experts(
model_state, state, physical_to_logical_map_cpu
)
rebalancing_algorithm_executed = True
logger.info(
"Async worker computed new indices for model %s",
model_state.model_name,
)
num_layers = model_state.model.num_moe_layers
assert model_state.new_physical_to_logical_map is not None
assert physical_to_logical_map_cpu is not None
# Snapshot the physical_to_logical_map (synchronized with
# rearrange_event) and copy it to CPU
with torch.cuda.stream(cuda_stream):
physical_to_logical_map_cpu = model_state.physical_to_logical_map.cpu()
layer_idx = model_state.layer_to_transfer
old_layer_indices = physical_to_logical_map_cpu[layer_idx]
new_layer_indices = model_state.new_physical_to_logical_map[
layer_idx
]
new_physical_to_logical_map = run_rebalance_experts(
model_state, state, physical_to_logical_map_cpu, cuda_stream
)
logger.info(
"Async worker computed new indices for model %s",
model_state.model_name,
)
# Wait for the main thread to finish consuming the buffer
# before initiating an EPLB transfer on another layer.
if model_state.buffer_consumed_event is not None:
cuda_stream.wait_event(model_state.buffer_consumed_event)
model_state.buffer_consumed_event = None
# Execute one EPLB layer transfer per model forward pass. Each iteration
# of this loop will copy the new set of expert weights into
# model_state.expert_buffer, which will be consumed by the main thread in
# move_to_workspace
while model_state.rebalanced and layer_idx < num_layers:
(
is_unchanged,
is_received_locally,
recv_metadata,
) = await transfer_layer(
old_layer_indices=physical_to_logical_map_cpu[layer_idx],
new_layer_indices=new_physical_to_logical_map[layer_idx],
expert_weights=model_state.model.expert_weights[layer_idx],
expert_weights_buffer=model_state.expert_buffer,
communicator=model_state.communicator,
ep_group=eplb_group,
is_profile=is_profile,
cuda_stream=cuda_stream,
)
(
model_state.is_unchanged,
model_state.is_received_locally,
model_state.recv_metadata,
) = await transfer_layer(
old_layer_indices=old_layer_indices,
new_layer_indices=new_layer_indices,
expert_weights=model_state.model.expert_weights[layer_idx],
expert_weights_buffer=model_state.expert_buffer,
ep_group=eplb_group,
communicator=model_state.communicator,
is_profile=is_profile,
cuda_stream=cuda_stream,
)
# block the async thread until the transfer to
# the intermediate buffer is complete.
cuda_stream.synchronize()
model_state.ep_buffer_ready = 1
finally:
model_state.buffer_lock.release()
else:
if not model_state.rebalanced:
break
await asyncio.sleep(0.001)
# Wait until all writes to expert_buffer have finished before making the
# AsyncEplbLayerResult visible to the main thread.
cuda_stream.synchronize()
state.rearrange_event.clear()
# This event guarantees that expert_buffer will not be overwritten by
# subsequent iterations of this loop until the main thread has consumed
# it. Record is called by the main thread after move_from_buffer().
consumed_event = CpuGpuEvent()
model_state.pending_result = AsyncEplbLayerResult(
layer_idx=layer_idx,
new_physical_to_logical_map=new_physical_to_logical_map[layer_idx],
is_unchanged=is_unchanged,
is_received_locally=is_received_locally,
recv_metadata=recv_metadata,
consumed_event=consumed_event,
)
# Block this thread until the main thread and main stream
# finish copying model_state.expert_buffer into
# model_state.model.expert_weights[layer_idx]
consumed_event.wait(stream=cuda_stream)
logger.debug("Layer %d transfer complete", layer_idx)
assert model_state.pending_result is None
layer_idx += 1