Apply LRU policy only to proper cache entries (#42656)

Signed-off-by: Stanislaw Wozniak <stw@zurich.ibm.com>
This commit is contained in:
Stan Wozniak
2026-06-16 21:49:15 +00:00
committed by GitHub
parent 9d4dc4ca2f
commit 520828789c
3 changed files with 29 additions and 56 deletions
+14 -15
View File
@@ -416,29 +416,28 @@ class BlockPool:
if self.metrics_collector:
self.metrics_collector.on_block_accessed(block)
def free_blocks(
self, ordered_blocks: Iterable[KVCacheBlock], prepend: bool = False
) -> None:
def free_blocks(self, ordered_blocks: Iterable[KVCacheBlock]) -> None:
"""Free a list of blocks. The blocks should be ordered by their
eviction priority, where the first block will be evicted first.
Args:
ordered_blocks: A list of blocks to free ordered by their eviction
priority.
prepend: Whether to put newly-free blocks at the front of the free
queue to be prioritized for reuse.
"""
# Materialize the iterable to allow multiple passes.
blocks_list = list(ordered_blocks)
for block in blocks_list:
# Identify blocks with hash (LRU cache) and without it (will never match in APC)
blocks_with_hash = []
blocks_without_hash = []
for block in ordered_blocks:
block.ref_cnt -= 1
freed_blocks = [
block for block in blocks_list if block.ref_cnt == 0 and not block.is_null
]
if prepend:
self.free_block_queue.prepend_n(freed_blocks)
else:
self.free_block_queue.append_n(freed_blocks)
if block.ref_cnt == 0 and not block.is_null:
if block.block_hash is None:
blocks_without_hash.append(block)
else:
blocks_with_hash.append(block)
# Blocks without hash always get evicted first - prepend them last to the tail
self.free_block_queue.prepend_n(blocks_without_hash)
self.free_block_queue.append_n(blocks_with_hash)
def evict_blocks(self, block_ids: set[int]) -> None:
"""evict blocks from the prefix cache by their block IDs.
+3 -31
View File
@@ -504,12 +504,7 @@ class SingleTypeKVCacheManager(ABC):
# range), so we must cap to the number of blocks that currently exist for
# this request.
num_skipped_blocks = min(num_skipped_blocks, len(blocks))
# Reuse skipped local blocks in order:
# scratch blocks: no prefix-cache value, reuse first.
# cached blocks: reusable prefix-cache value, reuse last.
removed_cached_blocks: list[KVCacheBlock] = []
removed_uncached_blocks: list[KVCacheBlock] = []
removed_blocks: list[KVCacheBlock] = []
# Because the block starts from index 0, the num_skipped_block-th block
# corresponds to index num_skipped_blocks - 1.
for i in range(num_skipped_blocks - 1, -1, -1):
@@ -518,16 +513,9 @@ class SingleTypeKVCacheManager(ABC):
# should also have been set to null blocks by the previous calls
# to this function.
break
if blocks[i].block_hash is None:
removed_uncached_blocks.append(blocks[i])
else:
removed_cached_blocks.append(blocks[i])
removed_blocks.append(blocks[i])
blocks[i] = self._null_block
# `prepend=True` makes uncached scratch blocks the next allocation
# candidates, while cached blocks stay behind them as best-effort
# prefix-cache entries.
self.block_pool.free_blocks(removed_cached_blocks)
self.block_pool.free_blocks(removed_uncached_blocks, prepend=True)
self.block_pool.free_blocks(removed_blocks)
def get_num_skipped_tokens(self, num_computed_tokens: int) -> int:
"""
@@ -777,22 +765,6 @@ class SlidingWindowManager(SingleTypeKVCacheManager):
return mask
def free(self, request_id: str) -> None:
# similar to remove_skipped_blocks(), prepend the uncached blocks
# and append the cached blocks to the free queue
req_blocks = self.req_to_blocks.pop(request_id, [])
if req_blocks:
cached_blocks: list[KVCacheBlock] = []
uncached_blocks: list[KVCacheBlock] = []
for block in reversed(req_blocks):
if block.block_hash is None:
uncached_blocks.append(block)
else:
cached_blocks.append(block)
self.block_pool.free_blocks(cached_blocks)
self.block_pool.free_blocks(uncached_blocks, prepend=True)
self.num_cached_block.pop(request_id, None)
def get_num_skipped_tokens(self, num_computed_tokens: int) -> int:
"""
Get the number of tokens that will be skipped for attention computation.