-
Notifications
You must be signed in to change notification settings - Fork 361
Expand file tree
/
Copy pathserial_runner.py
More file actions
323 lines (282 loc) · 12.5 KB
/
serial_runner.py
File metadata and controls
323 lines (282 loc) · 12.5 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
312
313
314
315
316
317
318
319
320
321
322
323
import concurrent
import logging
import math
import multiprocessing as mp
import time
import traceback
import numpy as np
import psutil
from vectordb_bench.backend.dataset import DatasetManager
from vectordb_bench.backend.filter import Filter, FilterOp, non_filter
from ... import config
from ...metric import calc_ndcg, calc_recall, get_ideal_dcg
from ...models import LoadTimeoutError, PerformanceTimeoutError
from .. import utils
from ..clients import api
NUM_PER_BATCH = config.NUM_PER_BATCH
LOAD_MAX_TRY_COUNT = config.LOAD_MAX_TRY_COUNT
log = logging.getLogger(__name__)
class SerialInsertRunner:
def __init__(
self,
db: api.VectorDB,
dataset: DatasetManager,
normalize: bool,
filters: Filter = non_filter,
timeout: float | None = None,
):
self.timeout = timeout if isinstance(timeout, int | float) else None
self.dataset = dataset
self.db = db
self.normalize = normalize
self.filters = filters
def retry_insert(self, db: api.VectorDB, retry_idx: int = 0, **kwargs):
_, error = db.insert_embeddings(**kwargs)
if error is not None:
log.warning(f"Insert Failed, try_idx={retry_idx}, Exception: {error}")
retry_idx += 1
if retry_idx <= config.MAX_INSERT_RETRY:
time.sleep(retry_idx)
self.retry_insert(db, retry_idx=retry_idx, **kwargs)
else:
msg = f"Insert failed and retried more than {config.MAX_INSERT_RETRY} times"
raise RuntimeError(msg) from None
def task(self) -> int:
count = 0
with self.db.init():
log.info(f"({mp.current_process().name:16}) Start inserting embeddings in batch {config.NUM_PER_BATCH}")
start = time.perf_counter()
for data_df in self.dataset:
all_metadata = data_df[self.dataset.data.train_id_field].tolist()
emb_np = np.stack(data_df[self.dataset.data.train_vector_field])
if self.normalize:
log.debug("normalize the 100k train data")
all_embeddings = (emb_np / np.linalg.norm(emb_np, axis=1)[:, np.newaxis]).tolist()
else:
all_embeddings = emb_np.tolist()
del emb_np
log.debug(f"batch dataset size: {len(all_embeddings)}, {len(all_metadata)}")
labels_data = None
if self.filters.type == FilterOp.StrEqual:
if self.dataset.data.scalar_labels_file_separated:
labels_data = self.dataset.scalar_labels[self.filters.label_field][all_metadata].to_list()
else:
labels_data = data_df[self.filters.label_field].tolist()
insert_count, error = self.db.insert_embeddings(
embeddings=all_embeddings,
metadata=all_metadata,
labels_data=labels_data,
)
if error is not None:
self.retry_insert(
self.db,
embeddings=all_embeddings,
metadata=all_metadata,
labels_data=labels_data,
)
assert insert_count == len(all_metadata)
count += insert_count
if count % 100_000 == 0:
log.info(f"({mp.current_process().name:16}) Loaded {count} embeddings into VectorDB")
log.info(
f"({mp.current_process().name:16}) Finish loading all dataset into VectorDB, "
f"dur={time.perf_counter() - start}"
)
return count
def endless_insert_data(self, all_embeddings: list, all_metadata: list, left_id: int = 0) -> int:
with self.db.init():
# unique id for endlessness insertion
all_metadata = [i + left_id for i in all_metadata]
num_batches = math.ceil(len(all_embeddings) / NUM_PER_BATCH)
log.info(
f"({mp.current_process().name:16}) Start inserting {len(all_embeddings)} "
f"embeddings in batch {NUM_PER_BATCH}"
)
count = 0
for batch_id in range(num_batches):
retry_count = 0
already_insert_count = 0
metadata = all_metadata[batch_id * NUM_PER_BATCH : (batch_id + 1) * NUM_PER_BATCH]
embeddings = all_embeddings[batch_id * NUM_PER_BATCH : (batch_id + 1) * NUM_PER_BATCH]
log.debug(
f"({mp.current_process().name:16}) batch [{batch_id:3}/{num_batches}], "
f"Start inserting {len(metadata)} embeddings"
)
while retry_count < LOAD_MAX_TRY_COUNT:
insert_count, error = self.db.insert_embeddings(
embeddings=embeddings[already_insert_count:],
metadata=metadata[already_insert_count:],
)
already_insert_count += insert_count
if error is not None:
retry_count += 1
time.sleep(10)
log.info(f"Failed to insert data, try {retry_count} time")
if retry_count >= LOAD_MAX_TRY_COUNT:
raise error
else:
break
log.debug(
f"({mp.current_process().name:16}) batch [{batch_id:3}/{num_batches}], "
f"Finish inserting {len(metadata)} embeddings"
)
assert already_insert_count == len(metadata)
count += already_insert_count
log.info(
f"({mp.current_process().name:16}) Finish inserting {len(all_embeddings)} embeddings in "
f"batch {NUM_PER_BATCH}"
)
return count
@utils.time_it
def _insert_all_batches(self) -> int:
"""Performance case only"""
with concurrent.futures.ProcessPoolExecutor(
mp_context=mp.get_context("spawn"),
max_workers=1,
) as executor:
future = executor.submit(self.task)
try:
count = future.result(timeout=self.timeout)
except TimeoutError as e:
msg = f"VectorDB load dataset timeout in {self.timeout}"
log.warning(msg)
for pid, _ in executor._processes.items():
psutil.Process(pid).kill()
raise PerformanceTimeoutError(msg) from e
except Exception as e:
log.warning(f"VectorDB load dataset error: {e}")
raise e from e
else:
return count
def run_endlessness(self) -> int:
"""run forever util DB raises exception or crash"""
# datasets for load tests are quite small, can fit into memory
# only 1 file
data_df = next(iter(self.dataset))
all_embeddings, all_metadata = (
np.stack(data_df[self.dataset.data.train_vector_field]).tolist(),
data_df[self.dataset.data.train_id_field].tolist(),
)
start_time = time.perf_counter()
max_load_count, times = 0, 0
try:
while time.perf_counter() - start_time < self.timeout:
count = self.endless_insert_data(
all_embeddings,
all_metadata,
left_id=max_load_count,
)
max_load_count += count
times += 1
log.info(
f"Loaded {times} entire dataset, current max load counts={utils.numerize(max_load_count)}, "
f"{max_load_count}"
)
except Exception as e:
log.info(
f"Capacity case load reach limit, insertion counts={utils.numerize(max_load_count)}, "
f"{max_load_count}, err={e}"
)
traceback.print_exc()
return max_load_count
else:
raise LoadTimeoutError(self.timeout)
def run(self) -> int:
count, _ = self._insert_all_batches()
return count
class SerialSearchRunner:
def __init__(
self,
db: api.VectorDB,
test_data: list[list[float]],
ground_truth: list[list[int]],
k: int = 100,
filters: Filter = non_filter,
):
self.db = db
self.k = k
self.filters = filters
if isinstance(test_data[0], np.ndarray):
self.test_data = [query.tolist() for query in test_data]
else:
self.test_data = test_data
self.ground_truth = ground_truth
def _get_db_search_res(self, emb: list[float], retry_idx: int = 0) -> list[int]:
try:
results = self.db.search_embedding(emb, self.k)
except Exception as e:
log.warning(f"Serial search failed, retry_idx={retry_idx}, Exception: {e}")
if retry_idx < config.MAX_SEARCH_RETRY:
return self._get_db_search_res(emb=emb, retry_idx=retry_idx + 1)
msg = f"Serial search failed and retried more than {config.MAX_SEARCH_RETRY} times"
raise RuntimeError(msg) from e
return results
def search(self, args: tuple[list, list[list[int]]]) -> tuple[float, float, float, float, float]:
log.info(f"{mp.current_process().name:14} start search the entire test_data to get recall and latency")
with self.db.init():
self.db.prepare_filter(self.filters)
test_data, ground_truth = args
ideal_dcg = get_ideal_dcg(self.k)
log.debug(f"test dataset size: {len(test_data)}")
log.debug(f"ground truth size: {len(ground_truth)}")
latencies, recalls, ndcgs = [], [], []
for idx, emb in enumerate(test_data):
s = time.perf_counter()
try:
results = self._get_db_search_res(emb)
except Exception as e:
log.warning(f"VectorDB search_embedding error: {e}")
raise e from None
latencies.append(time.perf_counter() - s)
if ground_truth is not None:
gt = ground_truth[idx]
recalls.append(calc_recall(self.k, gt[: self.k], results))
ndcgs.append(calc_ndcg(gt[: self.k], results, ideal_dcg))
else:
recalls.append(0)
ndcgs.append(0)
if len(latencies) % 100 == 0:
log.debug(
f"({mp.current_process().name:14}) search_count={len(latencies):3}, "
f"latest_latency={latencies[-1]}, latest recall={recalls[-1]}"
)
avg_latency = round(np.mean(latencies), 4)
avg_recall = round(np.mean(recalls), 4)
avg_ndcg = round(np.mean(ndcgs), 4)
cost = round(np.sum(latencies), 4)
p99 = round(np.percentile(latencies, 99), 4)
p95 = round(np.percentile(latencies, 95), 4)
log.info(
f"{mp.current_process().name:14} search entire test_data: "
f"cost={cost}s, "
f"queries={len(latencies)}, "
f"avg_recall={avg_recall}, "
f"avg_ndcg={avg_ndcg}, "
f"avg_latency={avg_latency}, "
f"p99={p99}, "
f"p95={p95}"
)
return (avg_recall, avg_ndcg, p99, p95, avg_latency)
def _run_in_subprocess(self) -> tuple[float, float, float, float]:
with concurrent.futures.ProcessPoolExecutor(max_workers=1) as executor:
future = executor.submit(self.search, (self.test_data, self.ground_truth))
return future.result()
@utils.time_it
def run(self) -> tuple[float, float, float, float, float]:
log.info(f"{mp.current_process().name:14} start serial search")
if self.test_data is None:
msg = "empty test_data"
raise RuntimeError(msg)
return self._run_in_subprocess()
@utils.time_it
def run_with_cost(self) -> tuple[tuple[float, float, float, float], float]:
"""
Search all test data in serial.
Returns:
tuple[tuple[float, float, float, float], float]: (avg_recall, avg_ndcg, p99_latency, p95_latency), cost
"""
log.info(f"{mp.current_process().name:14} start serial search")
if self.test_data is None:
msg = "empty test_data"
raise RuntimeError(msg)
return self._run_in_subprocess()