-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprocessor.py
More file actions
311 lines (278 loc) · 9.58 KB
/
Copy pathprocessor.py
File metadata and controls
311 lines (278 loc) · 9.58 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, as_completed
import json
import os
import pickle
import random
import threading
import time
import uuid
import faiss
import h5py
import numpy as np
import tqdm
import multiprocessing as mp
from util.analyze_datasets import dataset_filenames
from util.fetch_datasets import load_embeds
from caches.cache import (
LFU,
LRU,
DistanceLFU,
MissLFU,
SphereQueryLFU,
Surprisal,
SurprisalLFU,
DynamicAgingLFU,
ClusterLFU,
RAP,
RR,
FIFO,
LIFO,
SamplingMetaCache
)
from caches.arc import ARC
from caches.cluster_lru import ClusterLRU
from caches.lru_k import LRUK
from caches.OPT import OPT, ClusterOPT, FGRVB
from vector_stores.hnswlib_interface import HNSWVectorStore
from vector_stores.milvus_interface import MilvusVectorStore
from vector_stores.naive_interface import NaiveVectorStore
def get_flat_index_faiss(dim: int = 384):
return faiss.IndexIDMap2(faiss.IndexFlatL2(dim))
def yield_batch_slices(total_size, batch_size):
for start in range(0, total_size, batch_size):
stop = min(start + batch_size, total_size)
yield slice(start, stop), list(range(start, stop))
def get_hnsw_index_milvus(uri: str = "http://localhost:19530", dim: int = 384):
id = f"vector{uuid.uuid4()}".replace("-", "_")
return MilvusVectorStore(
uri=uri,
collection_name=id,
dim=dim,
metric_type="L2",
index_type="HNSW",
)
def get_flat_index_milvus_lite(dim: int = 384):
db_path = str(uuid.uuid4()).replace("-","").replace("_", "")+".db"
return MilvusVectorStore(
uri=db_path,
collection_name="col",
dim=dim,
metric_type="L2",
index_type="FLAT",
)
def get_flat_index_milvus_standalone(uri: str = "http://localhost:19530", dim: int = 384):
id = f"vector{uuid.uuid4()}".replace("-", "_")
return MilvusVectorStore(
uri=uri,
collection_name=id,
dim=dim,
metric_type="L2",
index_type="FLAT",
)
def get_hnsw_index_milvus_standalone(uri: str = "http://localhost:19530", dim: int = 384):
id = f"vector{uuid.uuid4()}".replace("-", "_")
return MilvusVectorStore(
uri=uri,
collection_name=id,
dim=dim,
metric_type="L2",
index_type="HNSW",
)
def get_ivf_index_milvus_standalone(uri: str = "http://localhost:19530", dim: int = 384):
id = f"vector{uuid.uuid4()}".replace("-", "_")
return MilvusVectorStore(
uri=uri,
collection_name=id,
dim=dim,
metric_type="L2",
index_type="IVF_FLAT",
)
def get_hnsw_index_hnswlib(dim: int = 384):
return HNSWVectorStore(
dim=dim,
allow_replace_delete=True
)
def get_flat_index_naive(dim: int = 384):
return NaiveVectorStore(dim=dim)
def get_ivf_index_milvus(
uri: str = "http://localhost:19530", dim: int = 384
):
id = f"vector{uuid.uuid4()}".replace("-", "_")
return MilvusVectorStore(
uri=uri,
collection_name=id,
dim=dim,
metric_type="L2",
index_type="IVF_FLAT",
)
def _run_single_worker(args):
(
dataset_name,
cache_name,
index_name,
same_embed_distance,
num_samples,
cache_size,
batch_size,
count_nn,
output_path,
progress_queue,
) = args
total_embeds, total_embeds_texts = load_embeds(dataset_name, num_samples)
caches = {
"MissLFU": (MissLFU, same_embed_distance),
"RGRVB": (OPT, same_embed_distance, total_embeds),
"CRVB": (ClusterOPT, same_embed_distance, total_embeds),
"SurprisalLFU": (SurprisalLFU, same_embed_distance),
"Surprisal": (Surprisal, same_embed_distance),
"SampleCache": (SamplingMetaCache, same_embed_distance, [LRU, LFU]),
"LIFO": (LIFO, same_embed_distance),
"FIFO": (FIFO, same_embed_distance),
"RR": (RR, same_embed_distance),
"LFU": (LFU, same_embed_distance),
"LRU": (LRU, same_embed_distance),
"LRUK": (LRUK, same_embed_distance, 2),
"LFUDA": (DynamicAgingLFU, same_embed_distance, 32),
"ARC": (ARC, same_embed_distance),
"ClusterLRU": (ClusterLRU, same_embed_distance),
"ClusterLFU": (ClusterLFU, same_embed_distance),
"DistanceLFU": (DistanceLFU, same_embed_distance),
"RAP": (RAP, same_embed_distance),
"SphereLFU": (SphereQueryLFU, same_embed_distance),
"FGRVB": (FGRVB, same_embed_distance, total_embeds)
}
indices = {
"milvus-lite": get_flat_index_milvus_lite,
"milvus-standalone-flat": get_flat_index_milvus_standalone,
"milvus-standalone-ivf": get_ivf_index_milvus_standalone,
"milvus-standalone-hnsw": get_hnsw_index_milvus_standalone,
"faiss": get_flat_index_faiss,
"HotSwap": get_flat_index_naive,
"hnswlib": get_hnsw_index_hnswlib
}
index = indices[index_name]()
# Initialize cache
cache_tuple = caches[cache_name]
cache_constructor = cache_tuple[0]
cache_args = cache_tuple[1:]
cache = cache_constructor(*cache_args)
cache.initialize(cache_size, index)
runtime = 0
# consts (simulated)
llm_call_time = 100
cache_access_time = 1
# Stats
total_hits = 0
at_least_1_hits = 0
simulated_runtime = 0
total_hit_distance = 0
for sl, i_embeds in yield_batch_slices(len(total_embeds), batch_size):
simulated_runtime += cache_access_time
embeds = total_embeds[sl]
embeds_texts = total_embeds_texts[sl]
_, cache_hits_dists = cache.get_cache_hits(embeds, count_nn)
total_hit_distance += sum([sum(dists) for dists in cache_hits_dists])
time_start = time.perf_counter()
iter_cache_hits, _ = cache.cache(embeds, i_embeds, count_nn, embeds_texts)
runtime += time.perf_counter() - time_start
total_hits += np.sum(iter_cache_hits)
reqs_hit = len(np.where(iter_cache_hits > 0)[0])
at_least_1_hits += reqs_hit
items_include = set(i_embeds).intersection(list(cache.items))
items_miss = set(np.array(i_embeds)[np.where(iter_cache_hits == 0)[0]])
items_llm = items_miss | items_include
simulated_runtime += len(items_llm) * llm_call_time
if progress_queue is not None:
try:
progress_queue.put(len(i_embeds))
except (EOFError, BrokenPipeError):
# Main process likely finished/cancelled; just continue quietly.
pass
fractional_recall_at_k = total_hits / (len(total_embeds) * count_nn)
hit_rate = at_least_1_hits / len(total_embeds)
mean_hit_distance = total_hit_distance / total_hits if total_hits > 0 else 0
iter_results = {
"Dataset": dataset_name,
"Index": index_name,
"Cache Name": cache_name,
"Recall@K": fractional_recall_at_k,
"Hit Rate": hit_rate,
"Runtime": runtime,
"Throughput": num_samples / runtime,
"Cache Size": cache_size,
"Same Embed Distance": same_embed_distance,
"Simulated Runtime": simulated_runtime,
"Output Path": output_path,
"Batch Size": batch_size,
"Count NN": count_nn,
"Mean Hit Distance": mean_hit_distance
}
return iter_results
class Processor:
def __init__(self, num_procs: int):
self.num_procs = num_procs
self.progress_queue = None
self.progress_queue_thread = None
self.stop_token = -1
self.submissions = []
self.num_batches = 0
def setup_progress_queue(self):
pbar = tqdm.tqdm(total=self.num_batches, desc="Running...")
manager = mp.Manager()
self.progress_queue = manager.Queue()
def consumer(pbar):
while True:
item = self.progress_queue.get()
if item == self.stop_token:
break
pbar.update(item)
self.progress_queue_thread = threading.Thread(
target=consumer, args=(pbar,), daemon=True
)
self.progress_queue_thread.start()
def submit(
self,
dataset_name: str,
cache_name: str,
index_name: str,
same_embed_distance: float,
dataset_size: int,
cache_size: int,
batch_size: int,
count_nn: int,
output_path: str,
) -> None:
if os.path.exists(output_path):
os.remove(output_path)
self.num_batches += dataset_size
self.submissions.append(
[
dataset_name,
cache_name,
index_name,
same_embed_distance,
dataset_size,
cache_size,
batch_size,
count_nn,
output_path,
]
)
def run(self) -> None:
random.shuffle(self.submissions) # shuffle for a better estimation of runtime
self.setup_progress_queue()
Pool = ProcessPoolExecutor if self.num_procs > 1 else ThreadPoolExecutor
random.shuffle(self.submissions)
with Pool(self.num_procs) as executor:
futures = [
executor.submit(_run_single_worker, arg + [self.progress_queue])
for arg in self.submissions
]
for future in as_completed(futures):
result = future.result()
output_path = result["Output Path"]
with open(output_path, "a") as f:
json.dump(result, f)
f.write("\n")
self.progress_queue.put(self.stop_token)
self.progress_queue_thread.join()