[XPU] Enable XPU eplb (#44987)

Signed-off-by: mayuyuace <qiming1.zhang@intel.com>
Signed-off-by: Qiming Zhang <qiming1.zhang@intel.com>
This commit is contained in:
Qiming Zhang
2026-09-17 13:19:13 +08:00
committed by GitHub
parent 2743dc4f92
commit e6b1a5e5e3
16 changed files with 267 additions and 109 deletions
+14 -14
View File
@@ -10,7 +10,7 @@ import torch
from vllm.distributed.parallel_state import get_eplb_group
from vllm.logger import init_logger
from .eplb_utils import CpuGpuEvent
from .eplb_utils import CpuGpuEvent, device_stream
from .rebalance_execute import AsyncEplbLayerResult, transfer_layer
if TYPE_CHECKING:
@@ -24,17 +24,17 @@ def start_async_worker(
is_profile: bool = False,
) -> threading.Thread:
rank = get_eplb_group().device_group.rank()
device_index = state.cuda_device_index
device_index = state.device_index
assert state.is_async
def thread_target() -> None:
assert device_index is not None
torch.accelerator.set_device_index(device_index)
cuda_stream = torch.cuda.Stream(device=device_index)
stream = torch.Stream(device=torch.accelerator.current_accelerator())
try:
transfer_run_periodically(
state=state,
cuda_stream=cuda_stream,
stream=stream,
is_profile=is_profile,
)
except Exception as exc: # pragma: no cover - diagnostic path
@@ -49,13 +49,13 @@ def run_rebalance_experts(
model_state: "EplbModelState",
eplb_state: "EplbState",
physical_to_logical_map_cpu: torch.Tensor,
cuda_stream: torch.cuda.Stream,
stream: torch.Stream,
) -> torch.Tensor:
assert model_state.eplb_stats is not None
eplb_stats = model_state.eplb_stats
# Move the global expert load window to CPU for computation.
with torch.cuda.stream(cuda_stream):
with device_stream(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(
@@ -73,11 +73,11 @@ def run_rebalance_experts(
def transfer_run_periodically(
state: "EplbState",
cuda_stream: torch.cuda.Stream,
stream: torch.Stream,
is_profile: bool = False,
) -> None:
while True:
state.rearrange_event.wait(stream=cuda_stream)
state.rearrange_event.wait(stream=stream)
eplb_group = get_eplb_group().device_group
eplb_cpu_group = get_eplb_group().cpu_group
@@ -87,16 +87,16 @@ def transfer_run_periodically(
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)
model_state.communicator.set_stream(stream)
num_layers = model_state.model.num_moe_layers
# Snapshot the physical_to_logical_map (synchronized with
# rearrange_event) and copy it to CPU
with torch.cuda.stream(cuda_stream):
with device_stream(stream):
physical_to_logical_map_cpu = model_state.physical_to_logical_map.cpu()
new_physical_to_logical_map = run_rebalance_experts(
model_state, state, physical_to_logical_map_cpu, cuda_stream
model_state, state, physical_to_logical_map_cpu, stream
)
# Execute one EPLB layer transfer per model forward pass. Each iteration
@@ -132,13 +132,13 @@ def transfer_run_periodically(
communicator=model_state.communicator,
ep_group=eplb_group,
is_profile=is_profile,
cuda_stream=cuda_stream,
stream=stream,
layer_idx=layer_idx,
)
# Wait until all writes to expert_buffer have finished before making the
# AsyncEplbLayerResult visible to the main thread.
cuda_stream.synchronize()
stream.synchronize()
# This event guarantees that expert_buffer will not be overwritten by
# subsequent iterations of this loop until the main thread has consumed
@@ -155,6 +155,6 @@ def transfer_run_periodically(
# 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)
consumed_event.wait(stream=stream)
assert model_state.pending_result is None
layer_idx += 1