-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_memory.py
128 lines (91 loc) · 3.17 KB
/
benchmark_memory.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
import numpy as np
import faiss
import sys
import psutil
import gc
import os
from argparse import ArgumentParser
from pathlib import Path
import annoy
import matplotlib.pyplot as plt
import hnswlib
argp = ArgumentParser()
argp.add_argument("--db_size", default=1000000, type=int, help="Maximum database size.")
argp.add_argument("--search_size", default=20000, type=int, help="Number of lookups.")
argp.add_argument(
"--output", type=Path, default=Path("benchmark_results"), help="Output folder."
)
argp.add_argument("index", type=Path, help=".npz embedding file path.")
args = argp.parse_args()
N_embeddings = args.db_size
try:
data = np.load(args.index)
except Exception as e:
print("Failed to open embedding file:", e, file=sys.stderr)
exit(-1)
embeddings = data["index"]
embeddings = np.repeat(embeddings, N_embeddings // len(embeddings), axis=0)
embeddings = embeddings + 2 * np.random.default_rng().standard_normal(
(N_embeddings, embeddings.shape[1]), dtype=np.float32
)
faiss.normalize_L2(embeddings)
search_size = args.search_size
fig, ax = plt.subplots()
process = psutil.Process(os.getpid())
def benchmark_method(name, color, limit: int, init):
print(name)
mems = []
sizes = []
i = 4096
limit = min(limit, len(embeddings)) if limit > 0 else len(embeddings)
while i < limit:
gc.collect()
before = process.memory_info().rss
model = init(embeddings[:i, :])
after = process.memory_info().rss
setup_mem = (after - before) / (1024 * 1024)
print(i, setup_mem)
mems.append(setup_mem)
sizes.append(i)
i *= 2
del model
ax.loglog(sizes, mems, color, label=name)
def setup_faiss(db):
index = faiss.IndexFlatIP(db.shape[1])
index.train(db)
index.add(db)
return index
def setup_faiss_quantized(db):
quantizer = faiss.IndexFlatIP(db.shape[1])
ncenters = int(db.shape[0] ** 0.5)
index = faiss.IndexIVFFlat(quantizer, db.shape[1], ncenters)
index.train(db)
index.add(db)
return index
def setup_annoy(db, n_trees):
index = annoy.AnnoyIndex(db.shape[1], "dot")
for i, vec in enumerate(db):
index.add_item(i, vec)
index.build(n_trees)
return index
def setup_hnsw(db):
index = hnswlib.Index(space="ip", dim=db.shape[1])
index.init_index(max_elements=db.shape[0], ef_construction=30, M=48)
index.add_items(db)
return index
search_vector = np.random.default_rng().random(
(search_size, embeddings.shape[1]), dtype=np.float32
)
# benchmark_method("NumPy", "green", 200000, lambda db: search_vector @ db.T)
benchmark_method("FAISS", "blue", 0, setup_faiss)
benchmark_method("FAISS Clustered", "cornflowerblue", 0, setup_faiss_quantized)
benchmark_method("Annoy 10", "orangered", 0, lambda db: setup_annoy(db, 10))
benchmark_method("Annoy 50", "orange", 0, lambda db: setup_annoy(db, 50))
benchmark_method("HNSW", "red", 200000, setup_hnsw)
ax.set_xlabel(f"Database size [{embeddings.shape[1]}D elements]")
ax.set_ylabel("Memory [MB]")
ax.set_title("Database size vs. Memory requirements")
ax.legend()
args.output.mkdir(exist_ok=True, parents=True)
plt.savefig(str(args.output / "memory.svg"))
plt.close(fig)