[Prefix Caching] DeepSeekv4 - Support selective prefix-cache retention for sliding-window KV cache (#43447)

Signed-off-by: wzhao18 <wzhao18.sz@gmail.com>
Signed-off-by: Yifan Qiao <yifanqiao@inferact.ai>
Co-authored-by: Yifan Qiao <yifanqiao@inferact.ai>
This commit is contained in:
Wei Zhao
2026-06-04 00:48:31 -07:00
committed by GitHub
co-authored by Yifan Qiao
parent 22c2e87555
commit a6183563b6
7 changed files with 792 additions and 45 deletions
+12 -4
View File
@@ -416,21 +416,29 @@ class BlockPool:
if self.metrics_collector:
self.metrics_collector.on_block_accessed(block)
def free_blocks(self, ordered_blocks: Iterable[KVCacheBlock]) -> None:
def free_blocks(
self, ordered_blocks: Iterable[KVCacheBlock], prepend: bool = False
) -> 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:
block.ref_cnt -= 1
self.free_block_queue.append_n(
[block for block in blocks_list if block.ref_cnt == 0 and not block.is_null]
)
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)
def evict_blocks(self, block_ids: set[int]) -> None:
"""evict blocks from the prefix cache by their block IDs.