feat(hicache): Support passing prefix keys for l3 store. (#9045)
Co-authored-by: pansicheng <sicheng.pan.chn@gmail.com> Co-authored-by: Zhiqiang Xie <xiezhq@stanford.edu>
This commit is contained in:
@@ -36,6 +36,7 @@ class HiCacheStorageConfig:
|
||||
|
||||
@dataclass
|
||||
class HiCacheStorageExtraInfo:
|
||||
prefix_keys: Optional[List[str]] = (None,)
|
||||
extra_info: Optional[dict] = None
|
||||
|
||||
|
||||
@@ -139,7 +140,9 @@ class HiCacheStorage(ABC):
|
||||
pass
|
||||
|
||||
# TODO: Use a finer-grained return type (e.g., List[bool])
|
||||
def batch_exists(self, keys: List[str]) -> int:
|
||||
def batch_exists(
|
||||
self, keys: List[str], extra_info: Optional[HiCacheStorageExtraInfo] = None
|
||||
) -> int:
|
||||
"""
|
||||
Check if the keys exist in the storage.
|
||||
return the number of consecutive existing keys from the start.
|
||||
|
||||
@@ -84,12 +84,14 @@ class HiRadixCache(RadixCache):
|
||||
prefetch_threshold,
|
||||
prefetch_timeout_base,
|
||||
prefetch_timeout_per_ki_token,
|
||||
hicache_storage_pass_prefix_keys,
|
||||
) = self._parse_storage_backend_extra_config(storage_backend_extra_config)
|
||||
self.prefetch_threshold = prefetch_threshold
|
||||
self.prefetch_timeout_base = prefetch_timeout_base
|
||||
self.prefetch_timeout_per_page = (
|
||||
page_size / 1024 * prefetch_timeout_per_ki_token
|
||||
)
|
||||
self.hicache_storage_pass_prefix_keys = hicache_storage_pass_prefix_keys
|
||||
# TODO: support more timeout check functions
|
||||
self.is_prefetch_timeout = self._prefetch_timeout_check_linear_func
|
||||
self.prefetch_stop_policy = hicache_storage_prefetch_policy
|
||||
@@ -149,7 +151,7 @@ class HiRadixCache(RadixCache):
|
||||
storage_backend_extra_config: JSON string containing extra configuration
|
||||
|
||||
Returns:
|
||||
tuple: (extra_config_dict, prefetch_threshold, prefetch_timeout_base, prefetch_timeout_per_ki_token)
|
||||
tuple: (extra_config_dict, prefetch_threshold, prefetch_timeout_base, prefetch_timeout_per_ki_token, hicache_storage_pass_prefix_keys)
|
||||
"""
|
||||
# Parse extra config JSON if provided
|
||||
extra_config = {}
|
||||
@@ -165,6 +167,9 @@ class HiRadixCache(RadixCache):
|
||||
prefetch_timeout_per_ki_token = extra_config.pop(
|
||||
"prefetch_timeout_per_ki_token", 0.25
|
||||
) # seconds per 1024 tokens
|
||||
hicache_storage_pass_prefix_keys = extra_config.pop(
|
||||
"hicache_storage_pass_prefix_keys", False
|
||||
)
|
||||
|
||||
if not isinstance(prefetch_threshold, int):
|
||||
raise ValueError(
|
||||
@@ -184,6 +189,7 @@ class HiRadixCache(RadixCache):
|
||||
prefetch_threshold,
|
||||
float(prefetch_timeout_base),
|
||||
float(prefetch_timeout_per_ki_token),
|
||||
hicache_storage_pass_prefix_keys,
|
||||
)
|
||||
|
||||
def reset(self):
|
||||
@@ -245,8 +251,14 @@ class HiRadixCache(RadixCache):
|
||||
return len(host_indices)
|
||||
|
||||
def write_backup_storage(self, node: TreeNode):
|
||||
prefix_keys = (
|
||||
node.get_prefix_hash_values(node.parent)
|
||||
if self.hicache_storage_pass_prefix_keys
|
||||
else None
|
||||
)
|
||||
|
||||
operation_id = self.cache_controller.write_storage(
|
||||
node.host_value, node.key, node.hash_value
|
||||
node.host_value, node.key, node.hash_value, prefix_keys
|
||||
)
|
||||
self.ongoing_backup[operation_id] = node
|
||||
node.protect_host()
|
||||
@@ -700,6 +712,7 @@ class HiRadixCache(RadixCache):
|
||||
last_host_node: TreeNode,
|
||||
new_input_tokens: List[int],
|
||||
last_hash: Optional[str] = None,
|
||||
prefix_keys: Optional[List[str]] = None,
|
||||
):
|
||||
# align the number of fetching tokens to the page size
|
||||
prefetch_length = len(new_input_tokens) - (
|
||||
@@ -723,7 +736,7 @@ class HiRadixCache(RadixCache):
|
||||
# no sufficient host memory for prefetch
|
||||
return
|
||||
operation = self.cache_controller.prefetch(
|
||||
req_id, host_indices, new_input_tokens, last_hash
|
||||
req_id, host_indices, new_input_tokens, last_hash, prefix_keys
|
||||
)
|
||||
self.ongoing_prefetch[req_id] = (
|
||||
last_host_node,
|
||||
|
||||
@@ -22,7 +22,7 @@ The radix tree data structure for managing the KV cache.
|
||||
import heapq
|
||||
import time
|
||||
from collections import defaultdict
|
||||
from functools import partial
|
||||
from functools import lru_cache, partial
|
||||
from typing import TYPE_CHECKING, Any, Iterator, List, Optional, Tuple, Union
|
||||
|
||||
import torch
|
||||
@@ -114,6 +114,13 @@ class TreeNode:
|
||||
return None
|
||||
return self.hash_value[-1]
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def get_prefix_hash_values(self, node: TreeNode) -> List[str]:
|
||||
if node is None or node.hash_value is None:
|
||||
return []
|
||||
|
||||
return node.get_prefix_hash_values(node.parent) + node.hash_value
|
||||
|
||||
def __lt__(self, other: "TreeNode"):
|
||||
return self.last_access_time < other.last_access_time
|
||||
|
||||
|
||||
@@ -13,7 +13,11 @@ from aibrix_kvcache import (
|
||||
)
|
||||
from aibrix_kvcache.common.absl_logging import log_every_n_seconds
|
||||
|
||||
from sglang.srt.mem_cache.hicache_storage import HiCacheStorage, HiCacheStorageConfig
|
||||
from sglang.srt.mem_cache.hicache_storage import (
|
||||
HiCacheStorage,
|
||||
HiCacheStorageConfig,
|
||||
HiCacheStorageExtraInfo,
|
||||
)
|
||||
from sglang.srt.mem_cache.memory_pool_host import HostKVCache
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -140,7 +144,9 @@ class AibrixKVCacheStorage(HiCacheStorage):
|
||||
) -> bool:
|
||||
return self.batch_set([key], [value], [target_location], [target_size])
|
||||
|
||||
def batch_exists(self, keys: List[str]) -> int:
|
||||
def batch_exists(
|
||||
self, keys: List[str], extra_info: Optional[HiCacheStorageExtraInfo] = None
|
||||
) -> int:
|
||||
block_hash = BlockHashes(keys, self.page_size)
|
||||
status = self.kv_cache_manager.exists(None, block_hash)
|
||||
if status.is_ok():
|
||||
|
||||
@@ -408,7 +408,9 @@ class EICStorage(HiCacheStorage):
|
||||
exist_num = self.batch_exists([key])
|
||||
return exist_num == 1
|
||||
|
||||
def batch_exists(self, keys) -> int:
|
||||
def batch_exists(
|
||||
self, keys, extra_info: Optional[HiCacheStorageExtraInfo] = None
|
||||
) -> int:
|
||||
if len(keys) == 0:
|
||||
return 0
|
||||
if self.use_zero_copy and not self.is_mla_model:
|
||||
|
||||
@@ -454,7 +454,9 @@ class HiCacheHF3FS(HiCacheStorage):
|
||||
result = self.metadata_client.exists(self.rank, [key])
|
||||
return result[0] if result else False
|
||||
|
||||
def batch_exists(self, keys: List[str]) -> int:
|
||||
def batch_exists(
|
||||
self, keys: List[str], extra_info: Optional[HiCacheStorageExtraInfo] = None
|
||||
) -> int:
|
||||
factor = 1
|
||||
if self.is_zero_copy and not self.is_mla_model:
|
||||
keys = self._get_mha_zero_copy_keys(keys)
|
||||
|
||||
@@ -399,7 +399,9 @@ class MooncakeStore(HiCacheStorage):
|
||||
exist_result = self._batch_exist([key])
|
||||
return exist_result[0] == 1
|
||||
|
||||
def batch_exists(self, keys) -> int:
|
||||
def batch_exists(
|
||||
self, keys, extra_info: Optional[HiCacheStorageExtraInfo] = None
|
||||
) -> int:
|
||||
if self.is_mla_backend:
|
||||
query_keys = [f"{key}_k" for key in keys]
|
||||
key_multiplier = 1
|
||||
|
||||
Reference in New Issue
Block a user