|
| 1 | +from typing import Tuple, Optional, Iterator, List |
| 2 | +from safetensors import safe_open |
| 3 | +from safetensors.torch import save_file |
| 4 | +import re |
| 5 | +import io |
| 6 | +import torch |
| 7 | +import redis |
| 8 | +import os |
| 9 | +import pickle |
| 10 | + |
| 11 | +from lmcache_server.storage_backend.abstract_backend import LMSBackendInterface |
| 12 | +from lmcache.logging import init_logger |
| 13 | +from lmcache.utils import _lmcache_nvtx_annotate |
| 14 | + |
| 15 | +logger = init_logger(__name__) |
| 16 | + |
| 17 | +class LMSLocalBackend(LMSBackendInterface): |
| 18 | + """ |
| 19 | + Cache engine for storing the KV cache of the tokens in the local cpu/gpu memory. |
| 20 | + """ |
| 21 | + def __init__( |
| 22 | + self, |
| 23 | + ): |
| 24 | + """ |
| 25 | + Throws: |
| 26 | + RuntimeError if the loaded configuration does not match the current configuration |
| 27 | + """ |
| 28 | + super().__init__() |
| 29 | + |
| 30 | + self.dict = {} |
| 31 | + |
| 32 | + def list_keys( |
| 33 | + self |
| 34 | + ) -> List[str]: |
| 35 | + |
| 36 | + return list(self.dict.keys()) |
| 37 | + |
| 38 | + def contains( |
| 39 | + self, |
| 40 | + key: str, |
| 41 | + ) -> bool: |
| 42 | + """ |
| 43 | + Check if the cache engine contains the key. |
| 44 | +
|
| 45 | + Input: |
| 46 | + key: the key of the token chunk, including prefix hash and format |
| 47 | +
|
| 48 | + Returns: |
| 49 | + True if the cache engine contains the key, False otherwise |
| 50 | + """ |
| 51 | + return key in self.dict |
| 52 | + |
| 53 | + def put( |
| 54 | + self, |
| 55 | + key: str, |
| 56 | + kv_chunk_bytes: bytes, |
| 57 | + blocking: bool = True, |
| 58 | + ) -> None: |
| 59 | + """ |
| 60 | + Store the KV cache of the tokens into the cache engine. |
| 61 | +
|
| 62 | + Input: |
| 63 | + key: the key of the token chunk, including prefix hash and format |
| 64 | + kv_chunk: the kv cache of the token chunk, in the format of nested tuples |
| 65 | +
|
| 66 | + Returns: |
| 67 | + None |
| 68 | +
|
| 69 | + Note: |
| 70 | + The KV cache should NOT have the "batch" dimension. |
| 71 | + """ |
| 72 | + if not blocking: |
| 73 | + logger.warn("Non-blocking is not implemented for local backend") |
| 74 | + self.dict[key] = kv_chunk_bytes |
| 75 | + |
| 76 | + |
| 77 | + @_lmcache_nvtx_annotate |
| 78 | + def get( |
| 79 | + self, |
| 80 | + key: str, |
| 81 | + ) -> Optional[bytes]: |
| 82 | + """ |
| 83 | + Retrive the KV cache chunk by the given key |
| 84 | +
|
| 85 | + Input: |
| 86 | + key: the key of the token chunk, including prefix hash and format |
| 87 | + Output: |
| 88 | + the kv cache of the token chunk, in the format of nested tuples |
| 89 | + None if the key is not found |
| 90 | + """ |
| 91 | + return self.dict.get(key, None) |
| 92 | + |
| 93 | + |
| 94 | +# TODO(Jiayi): need to optimize disk loading |
| 95 | +# current impl. with "naive open read/write" might not be efficient (better than torch.load) |
| 96 | +class LMSLocalDiskBackend(LMSBackendInterface): |
| 97 | + """ |
| 98 | + Cache engine for storing the KV cache of the tokens in the local disk. |
| 99 | + """ |
| 100 | + def __init__( |
| 101 | + self, |
| 102 | + path: str, |
| 103 | + ): |
| 104 | + """ |
| 105 | + Throws: |
| 106 | + RuntimeError if the loaded configuration does not match the current configuration |
| 107 | + """ |
| 108 | + super().__init__() |
| 109 | + |
| 110 | + self.path = path |
| 111 | + if not os.path.exists(self.path): |
| 112 | + os.makedirs(self.path) |
| 113 | + self.filenames = set() |
| 114 | + |
| 115 | + def list_keys( |
| 116 | + self |
| 117 | + ) -> List[str]: |
| 118 | + |
| 119 | + return list(self.filenames) |
| 120 | + |
| 121 | + def contains( |
| 122 | + self, |
| 123 | + key: str, |
| 124 | + ) -> bool: |
| 125 | + """ |
| 126 | + Check if the cache engine contains the key. |
| 127 | +
|
| 128 | + Input: |
| 129 | + key: the key of the token chunk, including prefix hash and format |
| 130 | +
|
| 131 | + Returns: |
| 132 | + True if the cache engine contains the key, False otherwise |
| 133 | + """ |
| 134 | + return key in self.filenames |
| 135 | + |
| 136 | + def _key_to_path( |
| 137 | + self, |
| 138 | + key: str, |
| 139 | + ) -> str: |
| 140 | + """ |
| 141 | + Covert key to path_name |
| 142 | +
|
| 143 | + Input: |
| 144 | + key: the key of the token chunk, including prefix hash and format |
| 145 | +
|
| 146 | + Returns: |
| 147 | + returns the path name |
| 148 | + """ |
| 149 | + return self.path + key.replace("/","-") + ".bin" |
| 150 | + |
| 151 | + |
| 152 | + def put( |
| 153 | + self, |
| 154 | + key: str, |
| 155 | + kv_chunk_bytes: bytes, |
| 156 | + blocking: bool = True, |
| 157 | + ) -> None: |
| 158 | + """ |
| 159 | + Store the KV cache of the tokens into the cache engine. |
| 160 | +
|
| 161 | + Input: |
| 162 | + key: the key of the token chunk, including prefix hash and format |
| 163 | + kv_chunk: the kv cache of the token chunk, in the format of nested tuples |
| 164 | +
|
| 165 | + Returns: |
| 166 | + None |
| 167 | +
|
| 168 | + Note: |
| 169 | + The KV cache should NOT have the "batch" dimension. |
| 170 | + """ |
| 171 | + if not blocking: |
| 172 | + logger.warn("Non-blocking is not implemented for local backend") |
| 173 | + self.filenames.add(key) |
| 174 | + logger.info(f"Saving cache to {self._key_to_path(key)}") |
| 175 | + #torch.save(kv_chunk_bytes, self._key_to_path(key)) |
| 176 | + with open(self._key_to_path(key), "wb") as binary_file: |
| 177 | + binary_file.write(kv_chunk_bytes) |
| 178 | + |
| 179 | + |
| 180 | + @_lmcache_nvtx_annotate |
| 181 | + def get( |
| 182 | + self, |
| 183 | + key: str, |
| 184 | + ) -> Optional[bytes]: |
| 185 | + """ |
| 186 | + Retrive the KV cache chunk by the given key |
| 187 | +
|
| 188 | + Input: |
| 189 | + key: the key of the token chunk, including prefix hash and format |
| 190 | + Output: |
| 191 | + the kv cache of the token chunk, in the format of nested tuples |
| 192 | + None if the key is not found |
| 193 | + """ |
| 194 | + if key not in self.filenames: |
| 195 | + return None |
| 196 | + |
| 197 | + with open(self._key_to_path(key), "rb") as binary_file: |
| 198 | + return binary_file.read() |
| 199 | + |
| 200 | + #return torch.load(self._key_to_path(key)) |
0 commit comments