[Feature] Add first-class KV hints request envelope for programmatic KV management (#53423)
Signed-off-by: Karen Chung <karenc@nvidia.com>
This commit is contained in:
@@ -553,6 +553,7 @@ def _create_req_context(req: Request) -> ReqContext:
|
||||
return ReqContext(
|
||||
req_id=req.request_id,
|
||||
kv_transfer_params=params,
|
||||
kv_hints=req.kv_hints,
|
||||
load_tier_filter=load_filter,
|
||||
)
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ from vllm.tasks import SupportedTask
|
||||
from vllm.v1.engine import EngineCoreRequest
|
||||
from vllm.v1.engine.input_processor import InputProcessor
|
||||
from vllm.v1.fault_tolerance.utils import FaultToleranceRequest, FaultToleranceResult
|
||||
from vllm.v1.kv_hints import KvHintsEnvelope
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm.v1.engine import PauseMode
|
||||
@@ -102,6 +103,7 @@ class EngineClient(ABC):
|
||||
priority: int = 0,
|
||||
data_parallel_rank: int | None = None,
|
||||
session_id: str | None = None,
|
||||
kv_hints: KvHintsEnvelope | None = None,
|
||||
reasoning_ended: bool | None = None,
|
||||
reasoning_parser_kwargs: dict[str, Any] | None = None,
|
||||
) -> AsyncGenerator[RequestOutput, None]:
|
||||
|
||||
@@ -16,6 +16,7 @@ from vllm.lora.request import LoRARequest
|
||||
from vllm.multimodal.inputs import MultiModalFeatureSpec
|
||||
from vllm.pooling_params import PoolingParams
|
||||
from vllm.sampling_params import SamplingParams
|
||||
from vllm.v1.kv_hints import KvHintsEnvelope
|
||||
from vllm.v1.metrics.stats import (
|
||||
PrefillStats,
|
||||
RequestSpecDecodeMetrics,
|
||||
@@ -157,6 +158,7 @@ class EngineCoreRequest(
|
||||
abort_immediately: bool = False
|
||||
|
||||
session_id: str | None = None
|
||||
kv_hints: KvHintsEnvelope | None = None
|
||||
|
||||
@property
|
||||
def params(self) -> SamplingParams | PoolingParams:
|
||||
|
||||
@@ -53,6 +53,7 @@ from vllm.v1.engine.output_processor import OutputProcessor, RequestOutputCollec
|
||||
from vllm.v1.engine.parallel_sampling import ParentRequest
|
||||
from vllm.v1.executor import Executor
|
||||
from vllm.v1.fault_tolerance.utils import FaultToleranceRequest, FaultToleranceResult
|
||||
from vllm.v1.kv_hints import KvHintsEnvelope
|
||||
from vllm.v1.metrics.loggers import (
|
||||
StatLoggerFactory,
|
||||
StatLoggerManager,
|
||||
@@ -386,6 +387,7 @@ class AsyncLLM(EngineClient):
|
||||
prompt_text: str | None = None,
|
||||
reasoning_ended: bool | None = None,
|
||||
reasoning_parser_kwargs: dict[str, Any] | None = None,
|
||||
kv_hints: KvHintsEnvelope | None = None,
|
||||
) -> RequestOutputCollector:
|
||||
"""Add new request to the AsyncLLM."""
|
||||
if self.errored:
|
||||
@@ -420,6 +422,7 @@ class AsyncLLM(EngineClient):
|
||||
priority,
|
||||
data_parallel_rank,
|
||||
session_id,
|
||||
kv_hints,
|
||||
)
|
||||
|
||||
# Convert Input --> Request.
|
||||
@@ -453,6 +456,7 @@ class AsyncLLM(EngineClient):
|
||||
priority=priority,
|
||||
data_parallel_rank=data_parallel_rank,
|
||||
session_id=session_id,
|
||||
kv_hints=kv_hints,
|
||||
)
|
||||
else:
|
||||
# Raw prompts require tokenization and possibly multimodal
|
||||
@@ -469,6 +473,7 @@ class AsyncLLM(EngineClient):
|
||||
priority=priority,
|
||||
data_parallel_rank=data_parallel_rank,
|
||||
session_id=session_id,
|
||||
kv_hints=kv_hints,
|
||||
)
|
||||
prompt_text, _, _ = extract_prompt_components(self.model_config, prompt)
|
||||
|
||||
@@ -557,6 +562,7 @@ class AsyncLLM(EngineClient):
|
||||
priority: int = 0,
|
||||
data_parallel_rank: int | None = None,
|
||||
session_id: str | None = None,
|
||||
kv_hints: KvHintsEnvelope | None = None,
|
||||
) -> RequestOutputCollector:
|
||||
self._validate_streaming_input_sampling_params(sampling_params)
|
||||
|
||||
@@ -569,6 +575,7 @@ class AsyncLLM(EngineClient):
|
||||
priority=priority,
|
||||
data_parallel_rank=data_parallel_rank,
|
||||
session_id=session_id,
|
||||
kv_hints=kv_hints,
|
||||
)
|
||||
|
||||
if not sampling_params.skip_clone:
|
||||
@@ -670,6 +677,7 @@ class AsyncLLM(EngineClient):
|
||||
priority: int = 0,
|
||||
data_parallel_rank: int | None = None,
|
||||
session_id: str | None = None,
|
||||
kv_hints: KvHintsEnvelope | None = None,
|
||||
reasoning_ended: bool | None = None,
|
||||
reasoning_parser_kwargs: dict[str, Any] | None = None,
|
||||
) -> AsyncGenerator[RequestOutput, None]:
|
||||
@@ -710,6 +718,7 @@ class AsyncLLM(EngineClient):
|
||||
priority=priority,
|
||||
data_parallel_rank=data_parallel_rank,
|
||||
session_id=session_id,
|
||||
kv_hints=kv_hints,
|
||||
prompt_text=prompt_text,
|
||||
reasoning_ended=reasoning_ended,
|
||||
reasoning_parser_kwargs=reasoning_parser_kwargs,
|
||||
|
||||
@@ -32,6 +32,7 @@ from vllm.utils import length_from_prompt_token_ids_or_embeds, random_uuid
|
||||
from vllm.utils.async_utils import make_async
|
||||
from vllm.utils.jsontree import json_iter_leaves
|
||||
from vllm.v1.engine import EngineCoreRequest
|
||||
from vllm.v1.kv_hints import KvHintsEnvelope
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -318,6 +319,7 @@ class InputProcessor:
|
||||
data_parallel_rank: int | None = None,
|
||||
resumable: bool = False,
|
||||
session_id: str | None = None,
|
||||
kv_hints: KvHintsEnvelope | None = None,
|
||||
) -> EngineCoreRequest:
|
||||
self._validate_params(params, supported_tasks)
|
||||
self._validate_lora(lora_request)
|
||||
@@ -461,6 +463,7 @@ class InputProcessor:
|
||||
trace_headers=trace_headers,
|
||||
resumable=resumable,
|
||||
session_id=session_id,
|
||||
kv_hints=kv_hints,
|
||||
)
|
||||
|
||||
def _validate_prompt_len(
|
||||
|
||||
@@ -34,6 +34,7 @@ from vllm.v1.engine.input_processor import InputProcessor
|
||||
from vllm.v1.engine.output_processor import OutputProcessor
|
||||
from vllm.v1.engine.parallel_sampling import ParentRequest
|
||||
from vllm.v1.executor import Executor
|
||||
from vllm.v1.kv_hints import KvHintsEnvelope
|
||||
from vllm.v1.metrics.loggers import StatLoggerFactory, StatLoggerManager
|
||||
from vllm.v1.metrics.reader import Metric, get_metrics_snapshot
|
||||
from vllm.v1.metrics.stats import IterationStats
|
||||
@@ -230,6 +231,7 @@ class LLMEngine:
|
||||
priority: int = 0,
|
||||
session_id: str | None = None,
|
||||
prompt_text: str | None = None,
|
||||
kv_hints: KvHintsEnvelope | None = None,
|
||||
) -> str:
|
||||
# Validate the request_id type.
|
||||
if not isinstance(request_id, str):
|
||||
@@ -263,6 +265,7 @@ class LLMEngine:
|
||||
trace_headers=trace_headers,
|
||||
priority=priority,
|
||||
session_id=session_id,
|
||||
kv_hints=kv_hints,
|
||||
)
|
||||
prompt_text, _, _ = extract_prompt_components(self.model_config, prompt)
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
from vllm.v1.kv_hints.protocol import KvHintAction, KvHintsEnvelope
|
||||
|
||||
__all__ = ["KvHintAction", "KvHintsEnvelope"]
|
||||
@@ -0,0 +1,26 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
from typing import Any
|
||||
|
||||
import msgspec
|
||||
|
||||
|
||||
class KvHintAction(msgspec.Struct, frozen=True): # type: ignore[call-arg]
|
||||
"""A versioned orchestrator-provided KV hint action."""
|
||||
|
||||
action_id: str
|
||||
action_type: str
|
||||
action_version: str
|
||||
# `payload` must contain JSON-compatible values. Action-specific schemas are
|
||||
# validated by the component implementing the action.
|
||||
payload: dict[str, Any]
|
||||
|
||||
|
||||
class KvHintsEnvelope(msgspec.Struct, frozen=True): # type: ignore[call-arg]
|
||||
"""Orchestrator-provided KV hints optionally attached to one inference request.
|
||||
A KV hint envelope is a collection of KV hint action(s)."""
|
||||
|
||||
protocol_version: str
|
||||
message_id: str
|
||||
actions: list[KvHintAction]
|
||||
@@ -16,6 +16,7 @@ if TYPE_CHECKING:
|
||||
OffloadingConnectorStats,
|
||||
)
|
||||
|
||||
from vllm.v1.kv_hints import KvHintsEnvelope
|
||||
from vllm.v1.kv_offload.config import OffloadingConfig
|
||||
|
||||
# `OffloadKey` identifies an offloaded block. It combines a block hash with
|
||||
@@ -89,10 +90,11 @@ TierFilter.ALL = TierFilter(matchers=(TierMatcher(),))
|
||||
class ReqContext:
|
||||
req_id: str
|
||||
kv_transfer_params: dict[str, Any] | None = None
|
||||
kv_hints: KvHintsEnvelope | None = None
|
||||
load_tier_filter: TierFilter = TierFilter.ALL
|
||||
# Per-request scratch space keyed by value type, so a tier can parse
|
||||
# kv_transfer_params once (in on_new_request) and read the result back
|
||||
# on later calls for the same request.
|
||||
# kv_transfer_params and kv_hints once (in on_new_request) and read the
|
||||
# result back on later calls for the same request.
|
||||
_state: dict[type, Any] = field(default_factory=dict, repr=False, init=False)
|
||||
# End-token position for each key in this request. The scheduler records
|
||||
# these positions so managers can recover prefix order even when store
|
||||
|
||||
@@ -7,15 +7,15 @@ import mmap
|
||||
import socket
|
||||
import time
|
||||
import uuid
|
||||
from collections.abc import Collection, Iterable, Mapping
|
||||
from collections.abc import Collection, Iterable
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Any, cast
|
||||
|
||||
import msgspec
|
||||
from kvcr import (
|
||||
DURATION_METRIC,
|
||||
KVCR,
|
||||
ROUTER_HINT_CAPABILITIES,
|
||||
ROUTER_HINT_KEY,
|
||||
STATE_METRIC,
|
||||
TRANSFER_BLOCKS_METRIC,
|
||||
TRANSFER_BYTES_METRIC,
|
||||
@@ -575,14 +575,11 @@ class KVCRSecondaryTierManager(SecondaryTierManager):
|
||||
|
||||
@override
|
||||
def on_new_request(self, req_context: ReqContext) -> RequestOffloadingContext:
|
||||
params = getattr(req_context, "kv_transfer_params", None)
|
||||
if isinstance(params, Mapping):
|
||||
hint = params.get(ROUTER_HINT_KEY)
|
||||
if hint is not None:
|
||||
self._kvcr.submit_hint(
|
||||
request_id=req_context.req_id,
|
||||
hints=hint,
|
||||
)
|
||||
if (hint := req_context.kv_hints) is not None:
|
||||
self._kvcr.submit_hint(
|
||||
request_id=req_context.req_id,
|
||||
hints=msgspec.to_builtins(hint),
|
||||
)
|
||||
return RequestOffloadingContext()
|
||||
|
||||
@override
|
||||
|
||||
@@ -20,6 +20,7 @@ from vllm.v1.engine import (
|
||||
EngineCoreRequest,
|
||||
FinishReason,
|
||||
)
|
||||
from vllm.v1.kv_hints import KvHintsEnvelope
|
||||
from vllm.v1.metrics.stats import PrefillStats, RequestSpecDecodeMetrics
|
||||
from vllm.v1.structured_output.request import StructuredOutputRequest
|
||||
from vllm.v1.utils import ConstantList
|
||||
@@ -78,6 +79,7 @@ class Request:
|
||||
reasoning_ended: bool | None = None,
|
||||
reasoning_parser_kwargs: dict[str, Any] | None = None,
|
||||
abort_immediately: bool = False,
|
||||
kv_hints: KvHintsEnvelope | None = None,
|
||||
) -> None:
|
||||
self.request_id = request_id
|
||||
self.client_index = client_index
|
||||
@@ -193,6 +195,7 @@ class Request:
|
||||
# trace_headers
|
||||
self.trace_headers = trace_headers
|
||||
self.session_id = session_id
|
||||
self.kv_hints = kv_hints
|
||||
|
||||
# True if this request is scheduled as a non-final prefill chunk.
|
||||
self.is_prefill_chunk = False
|
||||
@@ -260,6 +263,7 @@ class Request:
|
||||
block_hasher=block_hasher,
|
||||
resumable=request.resumable,
|
||||
session_id=request.session_id,
|
||||
kv_hints=request.kv_hints,
|
||||
reasoning_ended=request.reasoning_ended,
|
||||
reasoning_parser_kwargs=request.reasoning_parser_kwargs,
|
||||
abort_immediately=request.abort_immediately,
|
||||
|
||||
Reference in New Issue
Block a user