[Elastic EP] Support Model Runner V2 (#53934)
Signed-off-by: almogtavor <almogtavor@gmail.com> Signed-off-by: Almog Tavor <70065337+almogtavor@users.noreply.github.com>
This commit is contained in:
@@ -15,6 +15,9 @@ from vllm.v1.worker.gpu.buffer_utils import (
|
||||
|
||||
|
||||
class BlockTables:
|
||||
# Set by the elastic EP warmup so KV writes land in the null block.
|
||||
redirect_writes_to_null_block = False
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
block_sizes: list[int],
|
||||
@@ -126,6 +129,8 @@ class BlockTables:
|
||||
bpk = self.blocks_per_kv_block[i]
|
||||
if bpk > 1:
|
||||
block_ids = [b * bpk + k for b in block_ids for k in range(bpk)]
|
||||
if self.redirect_writes_to_null_block:
|
||||
block_ids = [0] * len(block_ids)
|
||||
end = start + len(block_ids)
|
||||
row_capacity = self.block_tables[i].gpu.shape[1]
|
||||
if end > row_capacity:
|
||||
|
||||
@@ -161,7 +161,6 @@ class CudaGraphManager:
|
||||
# DBO supports FULL CUDA graphs only.
|
||||
self.ubatch_runner = ubatch_runner
|
||||
|
||||
self.dp_size = vllm_config.parallel_config.data_parallel_size
|
||||
self.tp_size = vllm_config.parallel_config.tensor_parallel_size
|
||||
self.is_first_pp_rank = get_pp_group().is_first_rank
|
||||
self.is_last_pp_rank = get_pp_group().is_last_rank
|
||||
@@ -385,6 +384,23 @@ class CudaGraphManager:
|
||||
self._candidates.setdefault(key, []).extend(matching)
|
||||
current_range_start = num_tokens + 1
|
||||
|
||||
@property
|
||||
def dp_size(self) -> int:
|
||||
# Not cached: elastic EP rewrites parallel_config in place on scale.
|
||||
return self.vllm_config.parallel_config.data_parallel_size
|
||||
|
||||
def release_graphs(self) -> None:
|
||||
"""Drop the captured graphs so a later capture() can refill them.
|
||||
|
||||
Elastic EP reallocates the MoE workspace when it grows, which leaves
|
||||
every captured graph holding a stale data pointer. `_capture_descs` is
|
||||
kept, so `needs_capture()` still reports the work to redo.
|
||||
"""
|
||||
self.graphs.clear()
|
||||
self._graphs_captured = False
|
||||
if self.breakable_cg_runner is not None:
|
||||
BreakableCUDAGraphWrapper.clear_all_graphs()
|
||||
|
||||
def needs_capture(self) -> bool:
|
||||
return len(self._capture_descs) > 0
|
||||
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
from collections.abc import Callable
|
||||
from collections.abc import Callable, Iterator
|
||||
from contextlib import contextmanager
|
||||
from functools import wraps
|
||||
from typing import Any
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
@@ -15,6 +16,9 @@ from vllm.model_executor.models.interfaces import (
|
||||
get_mixture_of_experts_model,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm.v1.worker.gpu.model_runner import GPUModelRunner
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
@@ -37,6 +41,27 @@ def step_eplb_after(*, is_dummy: bool = False) -> Callable:
|
||||
return decorator
|
||||
|
||||
|
||||
@contextmanager
|
||||
def preserve_serving_state(model_runner: "GPUModelRunner") -> Iterator[None]:
|
||||
"""Keep the elastic EP warmup out of the request pool and the KV cache."""
|
||||
# After the drain only parked streaming sessions still hold a slot, and
|
||||
# those are re-added from NewRequestData on their next chunk.
|
||||
_remove_all_requests(model_runner)
|
||||
model_runner.block_tables.redirect_writes_to_null_block = True
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
_remove_all_requests(model_runner)
|
||||
model_runner.block_tables.redirect_writes_to_null_block = False
|
||||
if model_runner.kv_block_zeroer is not None:
|
||||
model_runner.kv_block_zeroer.zero_block_ids([0])
|
||||
|
||||
|
||||
def _remove_all_requests(model_runner: "GPUModelRunner") -> None:
|
||||
for req_id in list(model_runner.req_states.req_id_to_index):
|
||||
model_runner._remove_request(req_id)
|
||||
|
||||
|
||||
class EPLBController:
|
||||
def __init__(self, parallel_config: Any, device: torch.device):
|
||||
self.parallel_config = parallel_config
|
||||
@@ -82,10 +107,8 @@ class EPLBController:
|
||||
self._has_registered_models = True
|
||||
return True
|
||||
|
||||
def maybe_register_model(
|
||||
self, model: nn.Module, model_config: Any, load_dummy_weights: bool
|
||||
) -> bool:
|
||||
if not self.parallel_config.enable_eplb or load_dummy_weights:
|
||||
def maybe_register_model(self, model: nn.Module, model_config: ModelConfig) -> bool:
|
||||
if not self.parallel_config.enable_eplb:
|
||||
return False
|
||||
|
||||
moe_model = get_mixture_of_experts_model(model)
|
||||
@@ -100,7 +123,13 @@ class EPLBController:
|
||||
self._has_registered_models = True
|
||||
return True
|
||||
|
||||
def maybe_start_async_loop(self, eplb_models_added: bool) -> None:
|
||||
def maybe_start_async_loop(
|
||||
self, eplb_models_added: bool, load_dummy_weights: bool = False
|
||||
) -> None:
|
||||
# A scaling-up worker registers to join the communicator handshake, but
|
||||
# its groups are not live yet, so _perform_eplb_reshuffle() starts this.
|
||||
if load_dummy_weights:
|
||||
return
|
||||
if eplb_models_added and self.state is not None and self.state.is_async:
|
||||
self.state.start_async_loop()
|
||||
|
||||
@@ -128,18 +157,10 @@ class EPLBController:
|
||||
|
||||
def setup_from_mapping(
|
||||
self,
|
||||
model: nn.Module,
|
||||
model_config: Any,
|
||||
model_config: ModelConfig,
|
||||
expanded_physical_to_logical: torch.Tensor,
|
||||
) -> None:
|
||||
moe_model = get_mixture_of_experts_model(model)
|
||||
assert moe_model is not None
|
||||
|
||||
self.state = EplbState.from_mapping(
|
||||
model=moe_model,
|
||||
model_config=model_config,
|
||||
device=self.device,
|
||||
parallel_config=self.parallel_config,
|
||||
expanded_physical_to_logical=expanded_physical_to_logical,
|
||||
)
|
||||
self._has_registered_models = True
|
||||
# Update in place: a fresh EplbState would build a second communicator
|
||||
# over a group the other ranks have already left.
|
||||
assert self.state is not None
|
||||
self.state.update_mapping(model_config, expanded_physical_to_logical)
|
||||
|
||||
@@ -234,10 +234,6 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
# Persistent buffer for intermediate tensors (non-first PP ranks).
|
||||
self.intermediate_tensors: IntermediateTensors | None = None
|
||||
|
||||
# Data parallelism.
|
||||
self.dp_size = self.parallel_config.data_parallel_size
|
||||
self.dp_rank = self.parallel_config.data_parallel_rank
|
||||
|
||||
# Dual batch overlap. Created in initialize_kv_cache(), once everything
|
||||
# it runs the microbatched forward with exists.
|
||||
self.ubatch_runner: UBatchRunner | None = None
|
||||
@@ -509,9 +505,9 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
if self.is_pooling_model and self.is_last_pp_rank:
|
||||
self.pooling_runner = PoolingRunner(self.model, self.vllm_config)
|
||||
eplb_models_added |= self.eplb.maybe_register_model(
|
||||
self.model, self.model_config, load_dummy_weights
|
||||
self.model, self.model_config
|
||||
)
|
||||
self.eplb.maybe_start_async_loop(eplb_models_added)
|
||||
self.eplb.maybe_start_async_loop(eplb_models_added, load_dummy_weights)
|
||||
|
||||
if not self.is_first_pp_rank:
|
||||
# For non-first PP ranks, create intermediate tensors sized
|
||||
@@ -526,6 +522,16 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
|
||||
get_offloader().post_init()
|
||||
|
||||
@property
|
||||
def dp_size(self) -> int:
|
||||
# Not cached: elastic EP rewrites parallel_config in place on reconfigure.
|
||||
return self.parallel_config.data_parallel_size
|
||||
|
||||
@property
|
||||
def dp_rank(self) -> int:
|
||||
# Not cached: elastic EP rewrites parallel_config in place on reconfigure.
|
||||
return self.parallel_config.data_parallel_rank
|
||||
|
||||
def get_model(self) -> nn.Module:
|
||||
return self.model
|
||||
|
||||
@@ -2337,9 +2343,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
def setup_eplb_from_mapping(
|
||||
self, expanded_physical_to_logical: torch.Tensor
|
||||
) -> None:
|
||||
self.eplb.setup_from_mapping(
|
||||
self.model, self.model_config, expanded_physical_to_logical
|
||||
)
|
||||
self.eplb.setup_from_mapping(self.model_config, expanded_physical_to_logical)
|
||||
|
||||
########### EPLB methods end ###########
|
||||
|
||||
|
||||
@@ -294,7 +294,11 @@ def _warmup_kernels(
|
||||
model_runner.scheduler_config.max_num_batched_tokens
|
||||
// max(prompt_len, decode_query_len),
|
||||
)
|
||||
if max_blocks_per_req > 0:
|
||||
block_tables = getattr(model_runner, "block_tables", None)
|
||||
null_blocks = block_tables is not None and (
|
||||
block_tables.redirect_writes_to_null_block
|
||||
)
|
||||
if max_blocks_per_req > 0 and not null_blocks:
|
||||
# Reserve block 0 (null block) and ensure we have enough blocks.
|
||||
# Encoder-only models allocate no KV blocks, so this cap doesn't apply.
|
||||
num_reqs = min(
|
||||
@@ -450,4 +454,6 @@ def _warmup_kernels(
|
||||
cleanup_output.finished_req_ids = set(req_ids)
|
||||
worker_execute_model(cleanup_output)
|
||||
model_runner.kv_connector.set_disabled(False)
|
||||
if model_runner.kv_block_zeroer is not None:
|
||||
model_runner.kv_block_zeroer.zero_block_ids([0])
|
||||
torch.accelerator.synchronize()
|
||||
|
||||
Reference in New Issue
Block a user