Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PREFIX CACHING FOLLOW UP] OrderedDict-based evictor #3431

Merged
merged 9 commits into from
Mar 21, 2024
Merged
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
25 changes: 12 additions & 13 deletions vllm/core/evictor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import enum
from typing import Dict
from typing import OrderedDict
from abc import ABC, abstractmethod, abstractproperty

from vllm.block import PhysicalTokenBlock
Expand Down Expand Up @@ -58,27 +58,26 @@ class LRUEvictor(Evictor):
"""

def __init__(self):
self.free_table: Dict[int, PhysicalTokenBlock] = {}
self.free_table: OrderedDict[int, PhysicalTokenBlock] = OrderedDict()

def __contains__(self, block_hash: int) -> bool:
return block_hash in self.free_table

# TODO: The performance of this evict function can be optimized further.
def evict(self) -> PhysicalTokenBlock:
if len(self.free_table) == 0:
raise ValueError("No usable cache memory left")
free_blocks = self.free_table.values()

# Get evicted block
evicted_block: PhysicalTokenBlock = next(iter(free_blocks))

for block in free_blocks:
if (block.last_accessed < evicted_block.last_accessed
or block.last_accessed == evicted_block.last_accessed and
block.num_hashed_tokens > evicted_block.num_hashed_tokens):
evicted_block = next(iter(self.free_table.values()))
# The blocks with the lowest timestamps should be placed consecutively
# at the start of OrderedDict. Loop through all these blocks to
# find the one with maximum number of hashed tokens.
for _, block in self.free_table.items():
if evicted_block.last_accessed < block.last_accessed:
break
if evicted_block.num_hashed_tokens < block.num_hashed_tokens:
Comment on lines +74 to +77
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem correct to me? Say if a block's last_accessed time is changed, it's relative position in the ordereddict will stay the same and will not be updated. Then we might evict a block that is accessed recently but was added to the dict early.

Copy link
Contributor Author

@ElizaWszola ElizaWszola Mar 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The evictor contains only blocks with ref count zero. If a block is allocated, it will be popped (allocate() function in CachedBlockAllocator). So when we access the block, we first pop it, then update access time and process it, and then push it again if its ref count goes down to zero again.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I got it! This is smart lol

evicted_block = block

del self.free_table[evicted_block.block_hash]
self.free_table.pop(evicted_block.block_hash)

evicted_block.computed = False
return evicted_block
Expand All @@ -91,7 +90,7 @@ def remove(self, block_hash: int) -> PhysicalTokenBlock:
raise ValueError(
"Attempting to remove block that's not in the evictor")
block: PhysicalTokenBlock = self.free_table[block_hash]
del self.free_table[block_hash]
self.free_table.pop(block_hash)
return block

@property
Expand Down
Loading