2023-12-01 09:31:31 +00:00
|
|
|
from typing import List
|
|
|
|
|
|
|
|
|
2023-12-01 09:02:44 +00:00
|
|
|
class RequestHandler:
|
2023-12-01 09:31:31 +00:00
|
|
|
"""
|
|
|
|
RequestHandler is the core for handling existing requests and updating current batch.
|
|
|
|
During generation process, we call schedule function each iteration to update current batch.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
cache_config: Configuration for initialize and manage kv cache.
|
|
|
|
"""
|
|
|
|
|
2023-12-01 09:02:44 +00:00
|
|
|
def __init__(self, cache_config) -> None:
|
|
|
|
self.cache_config = cache_config
|
|
|
|
self._init_cache()
|
2023-12-01 09:31:31 +00:00
|
|
|
self.waiting_list: List["Reqseq"] = []
|
|
|
|
self.running_list: List["Reqseq"] = []
|
2023-12-01 09:02:44 +00:00
|
|
|
|
|
|
|
def _init_cache(self):
|
2023-12-01 09:31:31 +00:00
|
|
|
"""
|
|
|
|
Initialize the cache manager with cache config.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def schedule(self):
|
|
|
|
"""
|
|
|
|
The main logic of request handler.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def add_sequence(self, reqseq: "Reqseq"):
|
|
|
|
"""
|
|
|
|
Add the request to waiting list.
|
|
|
|
"""
|
|
|
|
self.waiting_list.append(reqseq)
|
|
|
|
|
|
|
|
def abort_sequence(self, seq_id: str):
|
|
|
|
"""
|
|
|
|
Abort the request. #TODO :implement this
|
|
|
|
"""
|
|
|
|
self._find_sequence(seq_id)
|
|
|
|
return
|
|
|
|
|
|
|
|
def _find_sequence(self, seq_id: str) -> "Reqseq":
|
|
|
|
"""
|
|
|
|
Find the request by seq_id.
|
|
|
|
"""
|
2023-12-01 09:02:44 +00:00
|
|
|
|
2023-12-01 09:31:31 +00:00
|
|
|
def check_unfinished_seqs(self) -> bool:
|
|
|
|
return self.waiting_list or self.running_list
|