-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Woosuk Kwon <[email protected]>
- Loading branch information
1 parent
112fa0b
commit 04edd1c
Showing
10 changed files
with
527 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from typing import Dict, List, Set, Tuple | ||
|
||
from vllm.v1.request import Request | ||
|
||
|
||
class EncoderCacheManager: | ||
|
||
def __init__(self, cache_size: int): | ||
self.cache_size = cache_size | ||
self.num_free_slots = cache_size | ||
# req_id -> cached input ids | ||
self.cached: Dict[str, Set[int]] = {} | ||
# List of [req_id, input_id] | ||
self.freed: List[Tuple[str, int]] = [] | ||
|
||
def has_cache(self, request: Request, input_id: int) -> bool: | ||
req_id = request.request_id | ||
return req_id in self.cached and input_id in self.cached[req_id] | ||
|
||
def can_allocate(self, request: Request, input_id: int) -> bool: | ||
num_tokens = request.get_num_encoder_tokens(input_id) | ||
return num_tokens <= self.num_free_slots | ||
|
||
def allocate(self, request: Request, input_id: int) -> None: | ||
req_id = request.request_id | ||
if req_id not in self.cached: | ||
self.cached[req_id] = set() | ||
self.cached[req_id].add(input_id) | ||
self.num_free_slots -= request.get_num_encoder_tokens(input_id) | ||
|
||
def get_cached_input_ids(self, request: Request) -> Set[int]: | ||
return self.cached.get(request.request_id, set()) | ||
|
||
def free(self, request: Request, input_id: int) -> None: | ||
req_id = request.request_id | ||
if req_id not in self.cached: | ||
return | ||
|
||
self.cached[req_id].discard(input_id) | ||
if len(self.cached[req_id]) == 0: | ||
del self.cached[req_id] | ||
self.num_free_slots += request.get_num_encoder_tokens(input_id) | ||
self.freed.append((req_id, input_id)) | ||
|
||
def get_freed_ids(self) -> List[Tuple[str, int]]: | ||
freed = self.freed | ||
self.freed = [] | ||
return freed |
Oops, something went wrong.