[Core][KV events] Report prefix-cache-reused blocks in full report mode (#45261)

Signed-off-by: Lei Gong <gonglei25@huawei.com>
Co-authored-by: Lei Gong <gonglei25@huawei.com>
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
GongLei-HW
2026-07-09 22:46:54 -07:00
committed by GitHub
co-authored by Lei Gong Claude
parent 95ed0feaa5
commit e5588e49bc
5 changed files with 270 additions and 25 deletions
+118 -1
View File
@@ -12,7 +12,12 @@ import torch
import vllm.v1.core.kv_cache_manager as kv_cache_manager
import vllm.v1.core.kv_cache_utils as kv_cache_utils
from vllm.distributed.kv_events import AllBlocksCleared, BlockRemoved, BlockStored
from vllm.distributed.kv_events import (
MEDIUM_GPU,
AllBlocksCleared,
BlockRemoved,
BlockStored,
)
from vllm.lora.request import LoRARequest
from vllm.multimodal.inputs import (
MultiModalFeatureSpec,
@@ -2454,6 +2459,118 @@ def test_block_removed_event_group_idx(group_id: int):
assert event.group_idx == group_id
def test_emit_cached_block_events():
"""emit_cached_block_events emits one BlockStored for already-cached
(reused) prefix blocks, carrying the correct group_idx /
parent_block_hash / token_ids, and without mutating block state."""
block_size = 4
num_cached_blocks = 3
kv_cache_group_id = 1
num_tokens = block_size * 4 # 4 full blocks; reuse the first 3
pool = BlockPool(
num_gpu_blocks=8,
enable_caching=True,
hash_block_size=block_size,
enable_kv_cache_events=True,
)
req = make_request(
"req_emit_cached",
prompt_token_ids=list(range(num_tokens)),
block_size=block_size,
hash_fn=sha256,
)
assert len(req.block_hashes) >= num_cached_blocks
# Snapshot block state to prove emit_cached_block_events does not mutate it.
free_before = pool.get_num_free_blocks()
assert len(pool.cached_block_hash_to_block) == 0
pool.emit_cached_block_events(
request=req,
num_cached_blocks=num_cached_blocks,
block_size=block_size,
kv_cache_group_id=kv_cache_group_id,
)
# No block-state mutation: nothing allocated, nothing inserted into the
# prefix-cache map.
assert pool.get_num_free_blocks() == free_before
assert len(pool.cached_block_hash_to_block) == 0
events = pool.take_events()
assert len(events) == 1
event = events[0]
assert isinstance(event, BlockStored)
expected_hashes = [
kv_cache_utils.maybe_convert_block_hash(req.block_hashes[i])
for i in range(num_cached_blocks)
]
assert event.block_hashes == expected_hashes
# Reused blocks start from block 0, so there is no parent block hash.
assert event.parent_block_hash is None
assert event.token_ids == list(req.all_token_ids[: num_cached_blocks * block_size])
assert event.group_idx == kv_cache_group_id
assert event.block_size == block_size
assert event.medium == MEDIUM_GPU
assert event.lora_id is None
assert event.lora_name is None
def test_emit_cached_block_events_disabled():
"""No events are emitted when enable_kv_cache_events is False."""
block_size = 4
pool = BlockPool(
num_gpu_blocks=8,
enable_caching=True,
hash_block_size=block_size,
enable_kv_cache_events=False,
)
req = make_request(
"req_emit_disabled",
prompt_token_ids=list(range(block_size * 4)),
block_size=block_size,
hash_fn=sha256,
)
pool.emit_cached_block_events(
request=req,
num_cached_blocks=3,
block_size=block_size,
kv_cache_group_id=0,
)
assert pool.take_events() == []
def test_emit_cached_block_events_zero_cached():
"""No events are emitted when num_cached_blocks == 0."""
block_size = 4
pool = BlockPool(
num_gpu_blocks=8,
enable_caching=True,
hash_block_size=block_size,
enable_kv_cache_events=True,
)
req = make_request(
"req_emit_zero",
prompt_token_ids=list(range(block_size * 4)),
block_size=block_size,
hash_fn=sha256,
)
pool.emit_cached_block_events(
request=req,
num_cached_blocks=0,
block_size=block_size,
kv_cache_group_id=0,
)
assert pool.take_events() == []
def test_eagle_enabled_removes_last_block():
"""Verify Eagle does NOT remove blocks when request
length is divisible by block size."""