[Feature] Support Prefill-Decode disaggregation via vLLM KV transfer (#2220)
Signed-off-by: LiuBingyu <liubingyu62@gmail.com>
This commit is contained in:
1 parent
b88d3ce75e
commit
a5a4998ece
11 files changed
+1672
-48
No files matched your search
@@ -78,7 +78,6 @@ class AsyncOmni(EngineClient, OmniBase):
|
||||
self.final_output_task: asyncio.Task | None = None
|
||||
|
||||
self.config_path = self.engine.config_path
|
||||
self.stage_configs = self.engine.stage_configs
|
||||
self.tts_max_instructions_length = kwargs.get("tts_max_instructions_length", None)
|
||||
self.input_processor = self.engine.input_processor
|
||||
|
||||
@@ -209,6 +208,13 @@ class AsyncOmni(EngineClient, OmniBase):
|
||||
# Start final output dispatcher on the first call to generate()
|
||||
self._final_output_handler()
|
||||
|
||||
# Expand sampling params for PD disaggregation (user may provide N-1 params)
|
||||
if (
|
||||
sampling_params_list is not None
|
||||
and isinstance(sampling_params_list, Sequence)
|
||||
and not isinstance(sampling_params_list, (str, bytes))
|
||||
):
|
||||
sampling_params_list = self._maybe_expand_sampling_params(list(sampling_params_list))
|
||||
sampling_params_list = self.resolve_sampling_params_list(sampling_params_list)
|
||||
|
||||
# Track per-request metrics
|
||||
@@ -228,20 +234,27 @@ class AsyncOmni(EngineClient, OmniBase):
|
||||
req_state.metrics = metrics
|
||||
self.request_states[request_id] = req_state
|
||||
|
||||
# PD disaggregation: modify prefill-stage sampling params per request
|
||||
req_sp_list = list(sampling_params_list)
|
||||
pd_pair = self._get_pd_separation_pair()
|
||||
if pd_pair is not None:
|
||||
p_id = pd_pair[0]
|
||||
req_sp_list[p_id] = self._prepare_prefill_sampling_params(request_id, req_sp_list[p_id])
|
||||
|
||||
# Add request(s) to stage 0. For streaming inputs, submit
|
||||
# chunks incrementally through streaming_update.
|
||||
if isinstance(prompt, AsyncGenerator):
|
||||
input_stream_task = await self._add_streaming_input_request(
|
||||
request_id=request_id,
|
||||
input_stream=prompt,
|
||||
sampling_params_list=sampling_params_list,
|
||||
sampling_params_list=req_sp_list,
|
||||
final_stage_id=final_stage_id_for_e2e,
|
||||
)
|
||||
else:
|
||||
await self.engine.add_request_async(
|
||||
request_id=request_id,
|
||||
prompt=prompt,
|
||||
sampling_params_list=sampling_params_list,
|
||||
sampling_params_list=req_sp_list,
|
||||
final_stage_id=final_stage_id_for_e2e,
|
||||
)
|
||||
submit_ts = time.time()
|
||||
|
||||
@@ -66,6 +66,13 @@ class Omni(OmniBase):
|
||||
py_generator: bool = False,
|
||||
use_tqdm: bool | Callable[..., tqdm] = True,
|
||||
) -> Generator[OmniRequestOutput, None, None] | list[OmniRequestOutput]:
|
||||
# Expand sampling params for PD disaggregation (user may provide N-1 params)
|
||||
if (
|
||||
sampling_params_list is not None
|
||||
and isinstance(sampling_params_list, Sequence)
|
||||
and not isinstance(sampling_params_list, (str, bytes))
|
||||
):
|
||||
sampling_params_list = self._maybe_expand_sampling_params(list(sampling_params_list))
|
||||
sampling_params_list = self.resolve_sampling_params_list(sampling_params_list)
|
||||
try:
|
||||
if py_generator:
|
||||
@@ -125,10 +132,17 @@ class Omni(OmniBase):
|
||||
req_state.metrics = metrics
|
||||
self.request_states[req_id] = req_state
|
||||
|
||||
# PD disaggregation: modify stage-0 (prefill) sampling params per request
|
||||
req_sp_list = list(sampling_params_list)
|
||||
pd_pair = self._get_pd_separation_pair()
|
||||
if pd_pair is not None:
|
||||
p_id = pd_pair[0]
|
||||
req_sp_list[p_id] = self._prepare_prefill_sampling_params(req_id, req_sp_list[p_id])
|
||||
|
||||
self.engine.add_request(
|
||||
request_id=req_id,
|
||||
prompt=prompt,
|
||||
sampling_params_list=sampling_params_list,
|
||||
sampling_params_list=req_sp_list,
|
||||
final_stage_id=final_stage_id,
|
||||
)
|
||||
submit_ts = time.time()
|
||||
|
||||
@@ -14,6 +14,7 @@ from vllm.v1.engine.exceptions import EngineDeadError
|
||||
|
||||
from vllm_omni.engine.async_omni_engine import AsyncOmniEngine
|
||||
from vllm_omni.entrypoints.client_request_state import ClientRequestState
|
||||
from vllm_omni.entrypoints.pd_utils import PDDisaggregationMixin
|
||||
from vllm_omni.entrypoints.utils import get_final_stage_id_for_e2e
|
||||
from vllm_omni.metrics.stats import OrchestratorAggregator as OrchestratorMetrics
|
||||
from vllm_omni.model_executor.model_loader.weight_utils import download_weights_from_hf_specific
|
||||
@@ -65,7 +66,7 @@ def omni_snapshot_download(model_id: str) -> str:
|
||||
OutputMessageHandleResult = tuple[Literal[True], None, None, None] | tuple[Literal[False], str, int, ClientRequestState]
|
||||
|
||||
|
||||
class OmniBase:
|
||||
class OmniBase(PDDisaggregationMixin):
|
||||
"""Shared runtime foundation for AsyncOmni and Omni."""
|
||||
|
||||
def __init__(
|
||||
@@ -84,6 +85,7 @@ class OmniBase:
|
||||
if "log_requests" in kwargs:
|
||||
raise TypeError("`log_requests` has been removed in Omni/AsyncOmni. Use `log_stats`.")
|
||||
model = omni_snapshot_download(model)
|
||||
self._name = self.__class__.__name__
|
||||
self.model = model
|
||||
self.log_stats = log_stats
|
||||
self.async_chunk = async_chunk
|
||||
@@ -125,10 +127,18 @@ class OmniBase:
|
||||
model,
|
||||
)
|
||||
|
||||
# PD disaggregation state (detects if a prefill/decode stage pair is configured)
|
||||
self._init_pd_state()
|
||||
|
||||
@property
|
||||
def num_stages(self) -> int:
|
||||
return self.engine.num_stages
|
||||
|
||||
@property
|
||||
def stage_configs(self) -> list:
|
||||
"""Expose engine stage configs for PD disaggregation detection and validation."""
|
||||
return self.engine.stage_configs
|
||||
|
||||
@property
|
||||
def is_running(self) -> bool:
|
||||
return self.engine.is_alive()
|
||||
|
||||
@@ -23,9 +23,19 @@ logger = logging.getLogger(__name__)
|
||||
class PDDisaggregationMixin:
|
||||
"""Mixin supplying PD disaggregation helpers to OmniBase."""
|
||||
|
||||
def _get_pd_separation_pair(self) -> tuple[int, int] | None:
|
||||
"""PD prefill/decode indices when ``_init_pd_state`` ran; else ``None``.
|
||||
|
||||
Partial test doubles may skip ``OmniBase.__init__``; treat missing state as
|
||||
no PD disaggregation instead of raising ``AttributeError``.
|
||||
"""
|
||||
return getattr(self, "_pd_separation_pair", None)
|
||||
|
||||
def _init_pd_state(self) -> None:
|
||||
"""Initialise PD disaggregation state."""
|
||||
self._pd_separation_pair: tuple[int, int] | None = self._detect_pd_separation()
|
||||
self._pd_separation_pair: tuple[int, int] | None = self.detect_pd_separation_from_stage_configs(
|
||||
self.stage_configs
|
||||
)
|
||||
self._pd_connector_info: dict[str, Any] | None = None
|
||||
self._pd_kv_params_by_req: dict[str, dict[str, Any]] = {}
|
||||
self._pd_kv_params_lock = threading.Lock()
|
||||
@@ -40,11 +50,19 @@ class PDDisaggregationMixin:
|
||||
d_id,
|
||||
)
|
||||
|
||||
def _detect_pd_separation(self) -> tuple[int, int] | None:
|
||||
"""Scan stage_list for a prefill/decode pair. Returns (p_id, d_id) or None."""
|
||||
@staticmethod
|
||||
def detect_pd_separation_from_stage_configs(stage_configs: list[Any]) -> tuple[int, int] | None:
|
||||
"""Scan stage configs for a prefill/decode pair.
|
||||
|
||||
Returns:
|
||||
(prefill_idx, decode_idx) if one pair exists, None if not found.
|
||||
|
||||
Raises:
|
||||
ValueError: if multiple candidate PD pairs are found.
|
||||
"""
|
||||
prefill_by_id: dict[int, int] = {}
|
||||
decode_indices: list[int] = []
|
||||
for i, stage in enumerate(self.stage_list):
|
||||
for i, stage in enumerate(stage_configs):
|
||||
if getattr(stage, "is_prefill_only", False):
|
||||
prefill_by_id[i] = i
|
||||
sid = getattr(stage, "stage_id", i)
|
||||
@@ -55,7 +73,7 @@ class PDDisaggregationMixin:
|
||||
|
||||
pd_pairs: list[tuple[int, int]] = []
|
||||
for j in decode_indices:
|
||||
source_ids = getattr(self.stage_list[j], "engine_input_source", [])
|
||||
source_ids = getattr(stage_configs[j], "engine_input_source", [])
|
||||
for src in source_ids:
|
||||
if src in prefill_by_id:
|
||||
pd_pairs.append((prefill_by_id[src], j))
|
||||
@@ -107,10 +125,11 @@ class PDDisaggregationMixin:
|
||||
|
||||
def _validate_pd_separation_config(self) -> None:
|
||||
"""Validate PD stage configurations are consistent."""
|
||||
assert self._pd_separation_pair is not None
|
||||
p_id, d_id = self._pd_separation_pair
|
||||
p_stage = self.stage_list[p_id]
|
||||
d_stage = self.stage_list[d_id]
|
||||
pair = self._get_pd_separation_pair()
|
||||
assert pair is not None
|
||||
p_id, d_id = pair
|
||||
p_stage = self.stage_configs[p_id]
|
||||
d_stage = self.stage_configs[d_id]
|
||||
|
||||
def _get_kv_cfg(stage: "OmniStage") -> dict[str, Any]:
|
||||
ea = stage.engine_args
|
||||
@@ -158,11 +177,12 @@ class PDDisaggregationMixin:
|
||||
|
||||
def _get_pd_connector_info(self) -> dict[str, Any] | None:
|
||||
"""Extract prefill engine KV connector info."""
|
||||
if self._pd_separation_pair is None:
|
||||
pair = self._get_pd_separation_pair()
|
||||
if pair is None:
|
||||
return None
|
||||
|
||||
p_id, _ = self._pd_separation_pair
|
||||
p_stage = self.stage_list[p_id]
|
||||
p_id, _ = pair
|
||||
p_stage = self.stage_configs[p_id]
|
||||
|
||||
ea = p_stage.engine_args
|
||||
kv_cfg = getattr(ea, "kv_transfer_config", None)
|
||||
@@ -241,18 +261,17 @@ class PDDisaggregationMixin:
|
||||
|
||||
def _is_pd_routing(self, stage_id: int, next_stage_id: int) -> bool:
|
||||
"""True when edge stage_id → next_stage_id is the prefill→decode boundary."""
|
||||
return self._pd_separation_pair is not None and self._pd_separation_pair == (
|
||||
stage_id,
|
||||
next_stage_id,
|
||||
)
|
||||
pair = self._get_pd_separation_pair()
|
||||
return pair is not None and pair == (stage_id, next_stage_id)
|
||||
|
||||
def _maybe_expand_sampling_params(self, sampling_params_list: list) -> list:
|
||||
"""Auto-duplicate thinker SP for decode stage when user provides N-1 params."""
|
||||
if self._pd_separation_pair is None:
|
||||
pair = self._get_pd_separation_pair()
|
||||
if pair is None:
|
||||
return sampling_params_list
|
||||
if len(sampling_params_list) != len(self.stage_list) - 1:
|
||||
if len(sampling_params_list) != len(self.stage_configs) - 1:
|
||||
return sampling_params_list
|
||||
p_id, d_id = self._pd_separation_pair
|
||||
p_id, d_id = pair
|
||||
sp_list = list(sampling_params_list)
|
||||
sp_list.insert(d_id, sp_list[p_id])
|
||||
return sp_list
|
||||
|
||||
Reference in new issue
Block a user