Directory-backed cache for serialized llama.cpp sequence state.
| 12300 | |
| 12301 | |
| 12302 | class SequenceDiskCache(SequenceCache): |
| 12303 | """Directory-backed cache for serialized llama.cpp sequence state.""" |
| 12304 | |
| 12305 | @dataclass |
| 12306 | class Entry: |
| 12307 | entry_id: int |
| 12308 | path: Path |
| 12309 | tokens: Tuple[int, ...] |
| 12310 | size_bytes: int |
| 12311 | has_prompt_logits: bool |
| 12312 | last_accessed: float |
| 12313 | |
| 12314 | @dataclass(frozen=True) |
| 12315 | class Header: |
| 12316 | tokens: Tuple[int, ...] |
| 12317 | has_prompt_logits: bool |
| 12318 | |
| 12319 | @dataclass |
| 12320 | class Payload: |
| 12321 | tokens: List[int] |
| 12322 | state_bytes: np.ndarray |
| 12323 | prompt_logits: Optional[np.ndarray] |
| 12324 | |
| 12325 | FORMAT_VERSION = "1" |
| 12326 | |
| 12327 | TENSOR_TOKENS = "tokens" |
| 12328 | TENSOR_STATE = "state" |
| 12329 | TENSOR_PROMPT_LOGITS = "prompt_logits" |
| 12330 | |
| 12331 | METADATA_FORMAT = "sequence_cache_format" |
| 12332 | METADATA_COMPATIBILITY_KEY = "compatibility_key" |
| 12333 | |
| 12334 | def __init__( |
| 12335 | self, |
| 12336 | *, |
| 12337 | path: Union[str, Path], |
| 12338 | max_bytes: int, |
| 12339 | compatibility_key: str, |
| 12340 | min_tokens: int = 128, |
| 12341 | ) -> None: |
| 12342 | self.path = Path(path) |
| 12343 | self.path.mkdir(parents=True, exist_ok=True) |
| 12344 | self.max_bytes = max(0, int(max_bytes)) |
| 12345 | self.min_tokens = max(1, int(min_tokens)) |
| 12346 | self.compatibility_key = compatibility_key |
| 12347 | self._metadata = { |
| 12348 | self.METADATA_FORMAT: self.FORMAT_VERSION, |
| 12349 | self.METADATA_COMPATIBILITY_KEY: compatibility_key, |
| 12350 | } |
| 12351 | self._trie = RadixTrie() |
| 12352 | self._entries_by_id: Dict[int, SequenceDiskCache.Entry] = {} |
| 12353 | self._entries_by_tokens: Dict[Tuple[int, ...], SequenceDiskCache.Entry] = {} |
| 12354 | self._next_entry_id = 0 |
| 12355 | self._size_bytes = 0 |
| 12356 | self._load_entries() |
| 12357 | self._evict_if_needed() |
| 12358 | |
| 12359 | @staticmethod |
no outgoing calls
no test coverage detected
searching dependent graphs…