Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions python/infinilm/llm/cache_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,15 @@ def free_blocks(self, block_table: Sequence[int]) -> None:

def try_free_blocks(self, num_required: int) -> bool:
"""Evict unreferenced blocks until the requested capacity is available."""
to_free = [
block_id
for block_id in self.used_block_ids
if self.blocks[block_id].ref_count == 0
]
# Preserve one eviction, if possible, even when capacity is already enough.
num_to_free = max(1, num_required - len(self.free_block_ids))
to_free = []
for block_id in self.used_block_ids:
if self.blocks[block_id].ref_count == 0:
to_free.append(block_id)
if len(to_free) >= num_to_free:
break
# Collect candidates before mutating the set being traversed.
for block_id in to_free:
self._deallocate_block(block_id)
if self.can_allocate(num_required):
Expand Down