-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhnsw.py
336 lines (257 loc) · 9.94 KB
/
hnsw.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
from typing import Callable, TypeVar, Generic, TypeAlias
import numpy as np
import bisect
from threading import Lock
from time import time
def int_distance(x: int, y: int) -> int:
return (x ^ y).bit_count()
def identity(x):
return x
Query = TypeVar("Query")
Vector = TypeVar("Vector")
Index: TypeAlias = int
Layer: TypeAlias = dict[Index, "FurthestQueue"]
class HNSW(Generic[Query, Vector]):
def __init__(
self,
M: int = 128,
efConstruction: int = 128,
m_L: float = 0.3,
distance_func: Callable[[Query, Vector], float] = int_distance,
query_to_vector_func: Callable[[Query], Vector] = identity,
):
# Params.
self.M = int(M)
self.Mmax = self.M
self.Mmax0 = self.M * 2 # self.Mmax0 = self.M
self.efConstruction = int(efConstruction)
self.m_L = m_L
# User Functions.
self._distance_func = distance_func
self._query_to_vector_func = query_to_vector_func
# State.
self.lock = Lock()
self.vectors: list[Vector] = []
self.entry_point: list[Index] = []
# Layer format: [ { node: [(distance(node, neighbor), neighbor)] } ]
self.layers: list[Layer] = []
# Tracing.
self.search_log = {}
self.record_search_log = False
# Stats.
# Precompute the count of comparison of a list bisection.
self.n_cmp_per_len = [
int(np.ceil(np.log2(list_length + 1))) for list_length in range(10_000)
]
self._reset_stats()
# --- State Operations ---
def _mut_insert_vector(self, vec: Vector) -> Index:
with self.lock:
q = len(self.vectors)
self.vectors.append(vec)
self.n_insertions += 1
return q
def get_layer(self, lc: int) -> Layer:
return self.layers[lc] if lc < len(self.layers) else {}
def _mut_layer(self, lc: int) -> Layer:
while lc >= len(self.layers):
self.layers.append({})
return self.layers[lc]
@staticmethod
def _get_links(e: Index, layer: Layer) -> "FurthestQueue":
return layer.get(e) or FurthestQueue()
@staticmethod
def _mut_links(e: Index, layer: Layer) -> "FurthestQueue":
conn = layer.get(e)
if conn is None:
conn = FurthestQueue()
layer[e] = conn
return conn
def db_size(self) -> int:
return len(self.vectors)
def _mut_connect_bidir(
self,
q: Index,
neighbors: list[tuple[float, Index]],
lc: int,
max_links: int,
):
with self.lock:
layer = self._mut_layer(lc)
# Connect q -> n.
if q in layer:
print("Warning: _connect_bidir: q is already in the layer.")
return
layer[q] = FurthestQueue(neighbors, is_ascending=True)
for nq, n in neighbors:
# Connect n -> q.
n_links = HNSW._mut_links(n, layer)
self._record_list_comparison(len(n_links))
n_links.add(nq, q)
if len(n_links) > max_links:
n_links.trim_to_k_nearest(max_links)
# or select_neighbors_heuristic.
def _mut_may_be_entry_point(self, l: int, q: Index):
with self.lock:
if l >= len(self.layers):
while l >= len(self.layers):
self.layers.append({})
self.entry_point[:] = [q]
# --- Stats ---
def _reset_stats(self):
self.n_insertions = 0
self.n_searches = 0
self.n_distances = 0
self.n_comparisons = 0
self.n_improve = 0
self.stat_time = time()
def get_params(self) -> dict[str, int | float]:
return {
"M": self.M,
"efConstruction": self.efConstruction,
"m_L": self.m_L,
# "Mmax": self.Mmax,
# "Mmax0": self.Mmax0,
}
def get_stats(self) -> dict[str, int | float]:
return {
"db_size": len(self.vectors),
"n_layers": len(self.layers),
"n_insertions": self.n_insertions,
"n_searches": self.n_searches,
"n_distances": self.n_distances,
"n_comparisons": self.n_comparisons,
"n_improve": self.n_improve,
"duration_sec": time() - self.stat_time,
}
def reset_stats(self) -> dict[str, int | float]:
"Return the current stats, then reset them."
stats = self.get_stats()
self._reset_stats()
return stats
def _record_list_comparison(self, list_length: int):
self.n_comparisons += self.n_cmp_per_len[list_length]
# --- HNSW Algorithms ---
def _distance(self, x_vec: Query, y_id: Index) -> float:
self.n_distances += 1
y_vec = self.vectors[y_id]
return self._distance_func(x_vec, y_vec)
def _select_layer(self) -> int:
return int(-np.log(np.random.random()) * self.m_L)
def insert(self, q_vec: Query) -> Index:
q_vec_to_store = self._query_to_vector_func(q_vec)
q = self._mut_insert_vector(q_vec_to_store)
W = self._search_init(q_vec)
L = len(self.layers) - 1
l = self._select_layer()
# From the top layer down to the new node layer, non-inclusive.
for lc in range(L, l, -1):
self._search_layer(q_vec, W, 1, lc)
W.trim_to_k_nearest(1)
for lc in range(min(L, l), -1, -1):
self._search_layer(q_vec, W, self.efConstruction, lc)
neighbors = W.get_k_nearest(self.M) # or select_neighbors_heuristic
max_conn = self.Mmax if lc else self.Mmax0
self._mut_connect_bidir(q, neighbors, lc, max_conn)
self._mut_may_be_entry_point(l, q)
return q # ID of the inserted vector.
def search(
self, q_vec: Query, K: int, ef: int | None = None
) -> list[tuple[float, int]]:
"Return the K nearest neighbors of q_vec, as [(distance, vector_id)]."
self.n_searches += 1
if not ef:
ef = self.efConstruction
if K > ef:
ef = K
W = self._search_init(q_vec)
L = len(self.layers) - 1
for lc in range(L, 0, -1):
self._search_layer(q_vec, W, 1, lc)
W.trim_to_k_nearest(1)
self._search_layer(q_vec, W, ef, 0)
W.trim_to_k_nearest(K)
return W
def _search_init(self, q_vec: Query) -> "FurthestQueue":
if self.record_search_log:
self.search_log.clear()
W = FurthestQueue()
for e in self.entry_point:
eq = self._distance(q_vec, e)
self._record_list_comparison(len(W))
W.add(eq, e)
if self.record_search_log:
self.search_log[e] = (len(self.search_log), 0, eq, eq)
return W
def _search_layer(self, q_vec: Query, W: "FurthestQueue", ef: int, lc: int):
"Mutate W into the ef nearest neighbors of q_vec in the given layer."
layer = self.get_layer(lc)
v = set(e for eq, e in W) # set of visited elements
C = NearestQueue.from_furthest_queue(W) # set of candidates
fq, _ = W[-1] # W.get_furthest()
while len(C) > 0:
cq, c = C.pop() # C.take_nearest()
if self.record_search_log:
depth = self.search_log[c][1] + 1
else:
depth = None
if cq > fq:
break # all elements in W are evaluated
# update C and W
for _ec, e in self._get_links(c, layer):
# TODO: batch distance.
if e not in v:
v.add(e)
eq = self._distance(q_vec, e)
if self.record_search_log and e not in self.search_log:
self.search_log[e] = (len(self.search_log), depth, eq, fq)
if len(W) == ef: # W is full
self.n_comparisons += 1 # record_list_comparison(1)
if eq < fq:
W.pop() # W.take_furthest()
else:
continue
self.n_improve += 1
self._record_list_comparison(len(C))
self._record_list_comparison(len(W))
C.add(eq, e)
W.add(eq, e)
fq, _ = W[-1] # W.get_furthest()
# Sorted list.
class FurthestQueue(list[tuple[float, Index]]):
"A list sorted in ascending order, for fast pop of the furthest element."
def __init__(self, iterable=None, is_ascending=False):
if not iterable:
super().__init__()
else:
if not is_ascending:
iterable = sorted(iterable)
super().__init__(iterable)
def add(self, dist: float, to: Index):
bisect.insort(self, (dist, to))
def get_furthest(self) -> tuple[float, Index]:
return self[-1]
def take_furthest(self) -> tuple[float, Index]:
return self.pop()
def get_k_nearest(self, k) -> list[tuple[float, Index]]:
return self[:k]
def trim_to_k_nearest(self, k):
del self[k:]
class NearestQueue(list[tuple[float, Index]]):
"A list sorted in descending order, for fast pop of the nearest element."
def __init__(self, iterable=None, is_descending=False):
if not iterable:
super().__init__()
else:
if not is_descending:
iterable = sorted(iterable, reverse=True)
super().__init__(iterable)
@staticmethod
def from_furthest_queue(furthest_queue: FurthestQueue) -> "NearestQueue":
return NearestQueue(reversed(furthest_queue), is_descending=True)
def add(self, dist: float, to: Index):
bisect.insort(self, (dist, to), key=lambda x: -x[0])
def get_nearest(self) -> tuple[float, Index]:
return self[-1]
def take_nearest(self) -> tuple[float, Index]:
return self.pop()