[Core][BugFix] Tag prefix-cache extra keys by source (#51899)
Signed-off-by: Nick Hill <nickhill123@gmail.com> Co-authored-by: Lucas Bourtoule <35483370+dhalf@users.noreply.github.com> Co-authored-by: Tai An <antai12232931@outlook.com> Co-authored-by: Nick Hill <nickhill123@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Lucas Bourtoule
Tai An
Nick Hill
Claude Opus 5.5
parent
09c47db1ca
commit
765872e7ed
@@ -68,9 +68,11 @@ class BlockStored(KVCacheEvent):
|
||||
lora_name: str | None
|
||||
|
||||
"""Extra keys used in block hash computation, one entry per block in
|
||||
block_hashes. Each entry contains MM identifiers, LoRA name, cache_salt,
|
||||
prompt embedding hashes, etc. for that specific block. Exposed for external
|
||||
KV cache consumers to reconstruct block hashes.
|
||||
block_hashes. Each entry contains, in order, the LoRA name (if
|
||||
`lora_name` is set), `(mm_identifier, offset)` pairs, the cache_salt
|
||||
string and prompt embedding digests for that specific block. These are
|
||||
published untagged; the block hash itself uses each value tagged with
|
||||
its source, e.g. `("lora", name)` or `("mm", mm_identifier, offset)`.
|
||||
"""
|
||||
extra_keys: list[tuple[Any, ...] | None] | None = None
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ from vllm.v1.core.kv_cache_utils import (
|
||||
BlockHash,
|
||||
maybe_convert_block_hash,
|
||||
resolve_block_hashes,
|
||||
to_event_extra_keys,
|
||||
)
|
||||
from vllm.v1.kv_cache_interface import (
|
||||
KVCacheGroupSpec,
|
||||
@@ -393,9 +394,7 @@ class OffloadingEventsTracker:
|
||||
lora_id=meta.lora_id,
|
||||
medium=_MEDIUM_TO_EVENT_STR[event.medium],
|
||||
lora_name=meta.lora_name,
|
||||
extra_keys=(
|
||||
list(meta.extra_keys) if meta.extra_keys is not None else None
|
||||
),
|
||||
extra_keys=to_event_extra_keys(meta.extra_keys),
|
||||
group_idx=meta.group_idx,
|
||||
kv_cache_spec_kind=meta.kv_cache_spec.kv_cache_spec_kind,
|
||||
kv_cache_spec_sliding_window=(
|
||||
|
||||
@@ -25,6 +25,7 @@ from vllm.v1.core.kv_cache_utils import (
|
||||
make_block_hash_with_group_id,
|
||||
maybe_convert_block_hash,
|
||||
resolve_block_hashes,
|
||||
to_event_extra_keys,
|
||||
)
|
||||
from vllm.v1.request import Request
|
||||
|
||||
@@ -367,7 +368,7 @@ class BlockPool:
|
||||
lora_id=request.lora_request.adapter_id if request.lora_request else None,
|
||||
medium=self.medium,
|
||||
lora_name=request.lora_request.name if request.lora_request else None,
|
||||
extra_keys=extra_keys_list if extra_keys_list else None,
|
||||
extra_keys=to_event_extra_keys(extra_keys_list),
|
||||
group_idx=kv_cache_group_id,
|
||||
session_id=request.session_id,
|
||||
)
|
||||
@@ -557,7 +558,7 @@ class BlockPool:
|
||||
lora_name=request.lora_request.name
|
||||
if request.lora_request
|
||||
else None,
|
||||
extra_keys=[extra_keys],
|
||||
extra_keys=to_event_extra_keys([extra_keys]),
|
||||
group_idx=kv_cache_group_id,
|
||||
session_id=request.session_id,
|
||||
)
|
||||
|
||||
@@ -502,8 +502,8 @@ def _gen_mm_extra_hash_keys(
|
||||
) -> tuple[list[Any], int]:
|
||||
"""Generate extra keys related to MultiModal request for block hash
|
||||
computation. For multi-modal inputs, the extra keys are
|
||||
(mm_hash, start_offset) that indicate a mm input contained in the
|
||||
block and its starting offset in the block tokens.
|
||||
("mm", mm_hash, start_offset) tuples that indicate a mm input contained
|
||||
in the block and its starting offset in the block tokens.
|
||||
|
||||
Args:
|
||||
request: The request object.
|
||||
@@ -549,7 +549,7 @@ def _gen_mm_extra_hash_keys(
|
||||
# relative to the start of the block so prefix-cache keys stay
|
||||
# distinct when the same MM item appears at different positions
|
||||
# within otherwise-identical placeholder blocks.
|
||||
extra_keys.append((mm_feature.identifier, offset - start_token_idx))
|
||||
extra_keys.append(("mm", mm_feature.identifier, offset - start_token_idx))
|
||||
|
||||
if end_token_idx >= offset + length:
|
||||
# If this block contains the end of the current mm input,
|
||||
@@ -565,7 +565,7 @@ def _gen_mm_extra_hash_keys(
|
||||
return extra_keys, curr_mm_idx
|
||||
|
||||
|
||||
def _gen_lora_extra_hash_keys(request: Request) -> list[str]:
|
||||
def _gen_lora_extra_hash_keys(request: Request) -> list[tuple[str, str]]:
|
||||
"""Generate extra keys related to LoRA for block hash computation.
|
||||
|
||||
Args:
|
||||
@@ -578,12 +578,12 @@ def _gen_lora_extra_hash_keys(request: Request) -> list[str]:
|
||||
"""
|
||||
if not request.lora_request:
|
||||
return []
|
||||
return [request.lora_request.lora_name]
|
||||
return [("lora", request.lora_request.lora_name)]
|
||||
|
||||
|
||||
def _gen_prompt_embeds_extra_hash_keys(
|
||||
request: Request, start_token_idx: int, end_token_idx: int
|
||||
) -> list[bytes]:
|
||||
) -> list[tuple[str, bytes]]:
|
||||
"""Generate extra keys related to prompt embeds for block hash computation.
|
||||
|
||||
Args:
|
||||
@@ -605,7 +605,7 @@ def _gen_prompt_embeds_extra_hash_keys(
|
||||
# Hash prompt embeds once per block and cache on request
|
||||
embeds_hash = hashlib.sha256(tensor_data(block_prompt_embeds)).digest()
|
||||
request._prompt_embeds_per_block_hashes[block_range] = embeds_hash
|
||||
return [embeds_hash]
|
||||
return [("prompt_embeds", embeds_hash)]
|
||||
|
||||
|
||||
def generate_block_hash_extra_keys(
|
||||
@@ -629,9 +629,11 @@ def generate_block_hash_extra_keys(
|
||||
mm_extra_keys, new_start_mm_idx = _gen_mm_extra_hash_keys(
|
||||
request, start_token_idx, end_token_idx, start_mm_idx
|
||||
)
|
||||
lora_extra_keys: list[str] = _gen_lora_extra_hash_keys(request)
|
||||
cache_salt_keys: list[str] = (
|
||||
[request.cache_salt] if (start_token_idx == 0 and request.cache_salt) else []
|
||||
lora_extra_keys = _gen_lora_extra_hash_keys(request)
|
||||
cache_salt_keys: list[tuple[str, str]] = (
|
||||
[("cache_salt", 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
|
||||
@@ -647,6 +649,35 @@ def generate_block_hash_extra_keys(
|
||||
return tuple(extra_keys), new_start_mm_idx
|
||||
|
||||
|
||||
def to_event_extra_keys(
|
||||
extra_keys: Iterable[tuple[Any, ...] | None] | None,
|
||||
) -> list[tuple[Any, ...] | None] | None:
|
||||
"""Convert block-hash extra keys to the untagged per-block list published
|
||||
in KV events.
|
||||
|
||||
External KV event consumers parse the pre-tagging shapes: bare LoRA names
|
||||
and cache salts, ``(mm_identifier, offset)`` pairs and bare prompt-embeds
|
||||
digests. Events keep that format until consumers handle the tagged keys.
|
||||
|
||||
Args:
|
||||
extra_keys: One entry per block, each as returned by
|
||||
`generate_block_hash_extra_keys`, or None.
|
||||
|
||||
Returns:
|
||||
One untagged entry per block, or None if there are no entries.
|
||||
|
||||
"""
|
||||
if not extra_keys:
|
||||
return None
|
||||
event_keys = [
|
||||
None
|
||||
if keys is None
|
||||
else tuple(key[1:] if key[0] == "mm" else key[1] for key in keys)
|
||||
for keys in extra_keys
|
||||
]
|
||||
return event_keys or None
|
||||
|
||||
|
||||
def hash_block_tokens(
|
||||
hash_function: Callable[[Any], bytes],
|
||||
parent_block_hash: BlockHash | None,
|
||||
|
||||
@@ -34,6 +34,7 @@ from vllm.v1.core.kv_cache_utils import (
|
||||
maybe_convert_block_hash,
|
||||
resolve_block_hashes,
|
||||
resolve_dcp_kv_block_size,
|
||||
to_event_extra_keys,
|
||||
)
|
||||
from vllm.v1.core.sched.output import SchedulerOutput
|
||||
from vllm.v1.kv_cache_interface import (
|
||||
@@ -1147,9 +1148,7 @@ class SimpleCPUOffloadScheduler:
|
||||
lora_id=meta.lora_id if meta else None,
|
||||
medium=self.kv_event_medium,
|
||||
lora_name=meta.lora_name if meta else None,
|
||||
extra_keys=(
|
||||
[extra_keys] if extra_keys is not None else None
|
||||
),
|
||||
extra_keys=to_event_extra_keys(extra_keys and [extra_keys]),
|
||||
group_idx=group_idx,
|
||||
kv_cache_spec_kind=get_kv_cache_spec_kind(spec).value,
|
||||
kv_cache_spec_sliding_window=(
|
||||
|
||||
Reference in New Issue
Block a user