forked from LMCache/LMCache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
138 lines (117 loc) · 3.9 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import re
from dataclasses import dataclass
from typing import Optional
import yaml
@dataclass
class LMCacheEngineMetadata:
"""name of the LLM model"""
model_name: str
""" world size when running under a distributed setting """
world_size: int
""" worker id when running under a distributed setting """
worker_id: int
""" the format of kv tensors """
fmt: str
@dataclass
class LMCacheEngineConfig:
chunk_size: int
local_device: Optional[str]
remote_url: Optional[str]
remote_serde: Optional[str] # Can be "torch" or "cachegen"
pipelined_backend: bool
save_decode_cache: bool # whether to store decode kv cache
@staticmethod
def from_defaults(
chunk_size: int = 256,
local_device: str = "cuda",
remote_url: str = "redis://localhost:6379",
remote_serde: str = "torch",
pipelined_backend: bool = False,
save_decode_cache: bool = False,
) -> "LMCacheEngineConfig":
return LMCacheEngineConfig(
chunk_size,
local_device,
remote_url,
remote_serde,
pipelined_backend,
save_decode_cache,
)
@staticmethod
def from_legacy(
chunk_size: int = 256,
backend: str = "cuda",
persist_path: Optional[str] = None,
remote_serde: Optional[str] = "torch",
pipelined_backend: bool = False,
save_decode_cache: bool = False,
) -> "LMCacheEngineConfig":
local_device: Optional[str] = None
remote_url: Optional[str] = None
match backend:
case "cpu" | "cuda":
local_device = backend
remote_url = None
case path if re.match(r"file://(.*)/",
path): # local disk directory
local_device = path[7:]
remote_url = None
case url if re.match(r"(.*)://(.*):(\d+)", url):
local_device = None
remote_url = url
return LMCacheEngineConfig(
chunk_size,
local_device,
remote_url,
remote_serde,
pipelined_backend,
save_decode_cache,
)
@staticmethod
def from_file(file_path: str) -> "LMCacheEngineConfig":
"""
Load the config from a yaml file
"""
with open(file_path, "r") as fin:
config = yaml.safe_load(fin)
chunk_size = config.get("chunk_size", 256)
local_device = config.get("local_device", None)
remote_url = config.get("remote_url", None)
remote_serde = config.get("remote_serde", "torch")
pipelined_backend = config.get("pipelined_backend", False)
save_decode_cache = config.get("save_decode_cache", False)
match local_device:
case "cpu" | "cuda" | None:
pass
case path if re.match(r"file://(.*)/",
path): # local disk directory
local_device = path[7:]
case _:
raise ValueError(
f"Invalid local storage device: {local_device}")
match remote_url:
case None:
pass
case url if re.match(r"(.*)://(.*):(\d+)", url):
pass
case _:
raise ValueError(f"Invalid remote storage url: {remote_url}")
return LMCacheEngineConfig(
chunk_size,
local_device,
remote_url,
remote_serde,
pipelined_backend,
save_decode_cache,
)
### SOME GLOBAL CONFIGS
# TODO: it needs to be manually updated in the code here, but cannot be really
# configured
class GlobalConfig:
enable_debug: bool = True
@classmethod
def set_debug(cls, enable: bool):
cls.enable_debug = enable
@classmethod
def is_debug(cls) -> bool:
return cls.enable_debug