[CORE] Support Prefix Caching with Prompt Embeds (#27219)
Signed-off-by: Andrew Sansom <andrew@protopia.ai>
This commit is contained in:
@@ -1743,16 +1743,6 @@ class EngineArgs:
|
||||
if model_config.runner_type != "pooling":
|
||||
self.enable_chunked_prefill = True
|
||||
|
||||
# TODO: When prefix caching supports prompt embeds inputs, this
|
||||
# check can be removed.
|
||||
if self.enable_prompt_embeds and self.enable_prefix_caching is not False:
|
||||
logger.warning(
|
||||
"--enable-prompt-embeds and --enable-prefix-caching "
|
||||
"are not supported together in V1. Prefix caching has "
|
||||
"been disabled."
|
||||
)
|
||||
self.enable_prefix_caching = False
|
||||
|
||||
if self.enable_prefix_caching is None:
|
||||
# Disable prefix caching default for hybrid models
|
||||
# since the feature is still experimental.
|
||||
|
||||
@@ -26,6 +26,7 @@ from vllm.v1.kv_cache_interface import (
|
||||
UniformTypeKVCacheSpecs,
|
||||
)
|
||||
from vllm.v1.request import Request
|
||||
from vllm.v1.utils import tensor_data
|
||||
|
||||
# BlockHash represents the hash of a single KV-cache block used for
|
||||
# prefix caching. Treating it as a distinct type from `bytes` helps
|
||||
@@ -461,11 +462,33 @@ def _gen_lora_extra_hash_keys(request: Request) -> list[str]:
|
||||
return [request.lora_request.lora_name]
|
||||
|
||||
|
||||
def _gen_prompt_embeds_extra_hash_keys(
|
||||
request: Request, start_token_idx: int, end_token_idx: int
|
||||
) -> list[bytes]:
|
||||
"""Generate extra keys related to prompt embeds for block hash computation.
|
||||
|
||||
Args:
|
||||
request: The request object.
|
||||
start_token_idx: The start token index of the block.
|
||||
end_token_idx: The end token index of the block.
|
||||
|
||||
Returns:
|
||||
Return prompt embeddings data of the request if it has prompt embeds.
|
||||
Return empty list otherwise.
|
||||
"""
|
||||
if request.prompt_embeds is None:
|
||||
return []
|
||||
block_prompt_embeds = request.prompt_embeds[start_token_idx:end_token_idx]
|
||||
embeds_bytes = tensor_data(block_prompt_embeds).tobytes()
|
||||
return [embeds_bytes]
|
||||
|
||||
|
||||
def generate_block_hash_extra_keys(
|
||||
request: Request, start_token_idx: int, end_token_idx: int, start_mm_idx: int
|
||||
) -> tuple[tuple[Any, ...] | None, int]:
|
||||
"""Generate extra keys for the block hash. The extra keys can come from
|
||||
the multi-modal inputs and request specific metadata (e.g., LoRA name).
|
||||
the multi-modal inputs, request specific metadata (e.g., LoRA names), and
|
||||
data from prompt embeddings.
|
||||
|
||||
Args:
|
||||
request: The request object.
|
||||
@@ -484,8 +507,13 @@ def generate_block_hash_extra_keys(
|
||||
cache_salt_keys: list[str] = (
|
||||
[request.cache_salt] if (start_token_idx == 0 and request.cache_salt) else []
|
||||
)
|
||||
prompt_embeds_keys = _gen_prompt_embeds_extra_hash_keys(
|
||||
request, start_token_idx, end_token_idx
|
||||
)
|
||||
|
||||
extra_keys: list[Any] = lora_extra_keys + mm_extra_keys + cache_salt_keys
|
||||
extra_keys: list[Any] = (
|
||||
lora_extra_keys + mm_extra_keys + cache_salt_keys + prompt_embeds_keys
|
||||
)
|
||||
|
||||
if not extra_keys:
|
||||
return None, new_start_mm_idx
|
||||
|
||||
@@ -31,6 +31,7 @@ from vllm.multimodal.inputs import (
|
||||
NestedTensors,
|
||||
)
|
||||
from vllm.v1.engine import UtilityResult
|
||||
from vllm.v1.utils import tensor_data
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -218,14 +219,14 @@ class MsgpackEncoder:
|
||||
) -> tuple[str, tuple[int, ...], int | memoryview]:
|
||||
assert self.aux_buffers is not None
|
||||
# view the tensor as a contiguous 1D array of bytes
|
||||
arr = obj.flatten().contiguous().view(torch.uint8).numpy()
|
||||
arr_data = tensor_data(obj)
|
||||
if obj.nbytes < self.size_threshold:
|
||||
# Smaller tensors are encoded inline, just like ndarrays.
|
||||
data = msgpack.Ext(CUSTOM_TYPE_RAW_VIEW, arr.data)
|
||||
data = msgpack.Ext(CUSTOM_TYPE_RAW_VIEW, arr_data)
|
||||
else:
|
||||
# Otherwise encode index of backing buffer to avoid copy.
|
||||
data = len(self.aux_buffers)
|
||||
self.aux_buffers.append(arr.data)
|
||||
self.aux_buffers.append(arr_data)
|
||||
dtype = str(obj.dtype).removeprefix("torch.")
|
||||
return dtype, obj.shape, data
|
||||
|
||||
|
||||
@@ -396,3 +396,16 @@ def record_function_or_nullcontext(name: str) -> AbstractContextManager:
|
||||
|
||||
_PROFILER_FUNC = func
|
||||
return func(name)
|
||||
|
||||
|
||||
def tensor_data(tensor: torch.Tensor) -> memoryview:
|
||||
"""Get the raw data of a tensor as a uint8 memoryview, useful for
|
||||
serializing and hashing.
|
||||
|
||||
Args:
|
||||
tensor: The input tensor.
|
||||
|
||||
Returns:
|
||||
A memoryview of the tensor data as uint8.
|
||||
"""
|
||||
return tensor.flatten().contiguous().view(torch.uint8).numpy().data
|
||||
|
||||
Reference in New Issue
Block a user