[Core] Refactor omni prefix cache into Manager/Controller (#6654)
Signed-off-by: ZhengWG <zwg0606@gmail.com> Co-authored-by: amy-why-3459 <wuhaiyan17@huawei.com> Co-authored-by: Hongsheng Liu <liuhongsheng4@huawei.com>
This commit is contained in:
25 files changed
+5710
-2149
No files matched your search
@@ -33,7 +33,8 @@ from vllm.v1.worker.gpu_input_batch import CachedRequestState
|
||||
from vllm.v1.worker.gpu_model_runner import GPUModelRunner, PerLayerAttnMetadata
|
||||
from vllm.v1.worker.ubatch_utils import maybe_create_ubatch_slices
|
||||
|
||||
from vllm_omni.core.prefix_cache import OmniTensorPrefixCache
|
||||
from vllm_omni.core.prefix_cache import stage_prefix_cache_config
|
||||
from vllm_omni.core.prefix_cache.runner_mixin import PrefixCacheRunnerMixin
|
||||
from vllm_omni.data_entry_keys import OmniPayload
|
||||
from vllm_omni.engine.serialization import deserialize_additional_information
|
||||
from vllm_omni.model_executor.layers.rotary_embedding.mrope import OmniMRotaryEmbedding as MRotaryEmbedding
|
||||
@@ -79,7 +80,7 @@ def _filter_mrope_kwargs_for_model(model: object, kwargs: dict[str, Any]) -> dic
|
||||
return {key: value for key, value in kwargs.items() if key in accepted}
|
||||
|
||||
|
||||
class OmniGPUModelRunner(GPUModelRunner):
|
||||
class OmniGPUModelRunner(PrefixCacheRunnerMixin, GPUModelRunner):
|
||||
intermediate_tensors: IntermediateTensors | None
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
@@ -87,11 +88,16 @@ class OmniGPUModelRunner(GPUModelRunner):
|
||||
self.model_intermediate_buffer: dict[str, dict[str, Any]] = {}
|
||||
self._omni_num_scheduled_tokens_np: np.ndarray | None = None
|
||||
self._omni_last_model_output: object | None = None
|
||||
# The Omni tensor prefix cache will be allocated
|
||||
# when we initialize the metadata builders if enabled
|
||||
# Omni prefix cache (hidden / per-token mm tensors reused on prefix
|
||||
# hits). Built once, on the first real step after load_model: the
|
||||
# config is staged at kv-cache init and cleared after construction.
|
||||
self.omni_prefix_cache = None
|
||||
self._omni_prefix_cache_cfg = None
|
||||
self._sampled_token_ids_cpu_override = None
|
||||
self._omni_query_start_loc_model_kwarg = False
|
||||
# Output-payload constant snapshotted once in load_model; the cache
|
||||
# policy counterpart lives on PrefixCacheRunnerMixin.
|
||||
self._pooler_payload_include_hidden_flag = True
|
||||
|
||||
def _to_list(self, sampled_token_ids: torch.Tensor) -> list[list[int]]:
|
||||
override_fn = self._sampled_token_ids_cpu_override
|
||||
@@ -172,15 +178,20 @@ class OmniGPUModelRunner(GPUModelRunner):
|
||||
device=sm.device,
|
||||
)
|
||||
|
||||
# Initialize the wrapper for both multimodal output tensors
|
||||
# and for hidden states to be passed between stages
|
||||
if self.cache_config.enable_prefix_caching:
|
||||
self.omni_prefix_cache = OmniTensorPrefixCache(
|
||||
num_blocks=kv_cache_config.num_blocks,
|
||||
block_size=self.cache_config.block_size,
|
||||
hidden_size=self.model_config.get_hidden_size(),
|
||||
hs_dtype=self.dtype,
|
||||
)
|
||||
# Stage the config; the manager is built on the first step once
|
||||
# input_batch exists. The gate (pooling stage, kv_consumer, hybrid
|
||||
# kv groups) is shared with the NPU runner.
|
||||
cfg = stage_prefix_cache_config(
|
||||
kv_cache_config=kv_cache_config,
|
||||
cache_config=self.cache_config,
|
||||
kv_transfer_config=getattr(self.vllm_config, "kv_transfer_config", None),
|
||||
scheduler_config=self.scheduler_config,
|
||||
model_config=self.model_config,
|
||||
is_pooling_model=self.is_pooling_model,
|
||||
speculative_config=self.speculative_config,
|
||||
)
|
||||
if cfg is not None:
|
||||
self._omni_prefix_cache_cfg = cfg
|
||||
|
||||
@instrument(span_name="Loading (GPU)")
|
||||
def load_model(self, *args, **kwargs) -> None:
|
||||
@@ -192,6 +203,8 @@ class OmniGPUModelRunner(GPUModelRunner):
|
||||
if callable(candidate):
|
||||
override_fn = candidate
|
||||
self._sampled_token_ids_cpu_override = override_fn
|
||||
self._snapshot_prefix_cache_model_policy(model)
|
||||
self._pooler_payload_include_hidden_flag = bool(getattr(model, "omni_pooler_payload_include_hidden", True))
|
||||
self._omni_query_start_loc_model_kwarg = bool(getattr(model, "supports_omni_query_start_loc", False))
|
||||
self._maybe_enable_output_token_ids_for_model_sampler()
|
||||
self._init_talker_mtp()
|
||||
@@ -515,10 +528,6 @@ class OmniGPUModelRunner(GPUModelRunner):
|
||||
The SamplingMetadata is updated and copied to the GPU if there is a
|
||||
new/resumed/paused/finished request in the batch.
|
||||
"""
|
||||
# Used for prefix cache
|
||||
if self.omni_prefix_cache is not None:
|
||||
self.omni_prefix_cache.reset_prefix_cached_new_req_ids()
|
||||
|
||||
# Remove finished requests from the cached states.
|
||||
# cleanup_finished_request lives on OmniConnectorModelRunnerMixin and
|
||||
# is only safe to call once init_omni_connectors() has finished
|
||||
@@ -535,8 +544,6 @@ class OmniGPUModelRunner(GPUModelRunner):
|
||||
self.requests.pop(req_id, None)
|
||||
self.model_intermediate_buffer.pop(req_id, None)
|
||||
self.num_prompt_logprobs.pop(req_id, None)
|
||||
if self.omni_prefix_cache is not None:
|
||||
self.omni_prefix_cache.discard_deferred_mm_outputs(req_id)
|
||||
if hasattr(self, "_downstream_payload_cache"):
|
||||
self._downstream_payload_cache.pop(req_id, None)
|
||||
if hasattr(self, "_talker_mtp_generators"):
|
||||
@@ -600,13 +607,6 @@ class OmniGPUModelRunner(GPUModelRunner):
|
||||
reqs_to_add.append(req_state)
|
||||
continue
|
||||
|
||||
# Since this is the first time the request has been scheduled,
|
||||
# num_computed_tokens > 0 means that we have a hit in prefix
|
||||
# caching; mark it so that we can manage the hidden states
|
||||
# later on as needed.
|
||||
if self.omni_prefix_cache is not None and new_req_data.num_computed_tokens > 0:
|
||||
self.omni_prefix_cache.add_prefix_cached_new_req_id(req_id)
|
||||
|
||||
sampling_params = new_req_data.sampling_params
|
||||
pooling_params = new_req_data.pooling_params
|
||||
|
||||
|
||||
Reference in new issue
Block a user