-
Notifications
You must be signed in to change notification settings - Fork 0
/
RefHnsw.scala
287 lines (263 loc) · 10.3 KB
/
RefHnsw.scala
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
package io.github.tuannh982.hnsw
import io.github.tuannh982.hnsw.RefHnsw.{CandidateList, EntryPoint, NeighborList}
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.util.Random
import scala.util.control.Breaks.{break, breakable}
/**
* Reference implementation of HNSW algorithm described in paper https://arxiv.org/pdf/1603.09320.pdf
*
* **note**: this is a reference implementation, it only focus on correctness, not about performance
*/
class RefHnsw[T, D](
override val dimension: Int,
override val df: DistanceFunction[T, D],
override val distanceOrd: Ordering[D],
val m: Int = 16,
val mL: Double = 1.0 / math.log(16),
val efConstruction: Int = 20,
val ef: Int = 100
) extends BaseGraph[T, D] {
private val maxM = m + 1
private val maxM0 = m * 2 + 1
private val candidateOrd = Candidate.fromDistanceOrd(distanceOrd)
private var vectors: Array[Vec[T]] = _
private var nextIndex = 0
private var neighborGraph: Array[Array[NeighborList]] = _
private var hnswEntryPoint: Option[EntryPoint] = None
private val random = new Random()
/**
* pre-allocate space for the model. must only called once before inserting any vectors
* @param n total number of vectors
*/
def allocate(n: Int): Unit = {
vectors = new Array[Vec[T]](n)
neighborGraph = new Array[Array[NeighborList]](n)
}
private def maxNeighborCount(level: Int) = if (level == 0) maxM0 else maxM
private def getOrElseUpdateEntryPoint(index: Int, level: Int): EntryPoint = {
hnswEntryPoint match {
case Some(value) =>
value
case None =>
val newEntry = EntryPoint(index, level)
hnswEntryPoint = Some(newEntry)
newEntry
}
}
private def addVector(vector: Vec[T]): Int = {
val index = nextIndex
vectors(index) = vector
nextIndex += 1
index
}
/**
* reserve space in the graph when a new node is added
* @param index node index
* @param level node level
*/
private def allocateGraphSpace(index: Int, level: Int): Unit = {
neighborGraph(index) = new Array[NeighborList](level + 1)
for (l <- 0 to level) {
neighborGraph(index)(l) = NeighborList(new ArrayBuffer[Int]())
}
}
// Algorithm 2
// SEARCH-LAYER(q, ep, ef, lc)
// Input: query element q, enter points ep, number of nearest to q elements to return ef, layer number lc
// Output: ef closest neighbors to q
private def searchLayer(q: Vec[T], ep: Int, ef: Int, lc: Int): Seq[Candidate[D]] = {
// v ← ep // set of visited elements
// W ← ep // dynamic list of found nearest neighbors
// C ← ep // set of candidates
val v = new mutable.HashSet[Int]()
val W = CandidateList(candidateOrd)
val C = CandidateList(candidateOrd.reverse)
// add candidate to W and C
def addCandidate(index: Int): Unit = {
val candidate = Candidate(index, df(vectors(index), q))
W.push(candidate)
C.push(candidate)
}
// add ep to those lists
v.add(ep)
addCandidate(ep)
// while │C│ > 0
breakable {
while (C.nonEmpty) {
// c ← extract nearest element from C to q
// f ← get furthest element from W to q
val c = C.pop()
var f = W.peek()
// if distance(c, q) > distance(f, q)
if (distanceOrd.gt(c.distance, f.distance)) {
// break // all elements in W are evaluated
break
} else {
// for each e ∈ neighbourhood(c) at layer lc // update C and W
val neighbourhood = neighborGraph(c.index)(lc)
neighbourhood.arr.foreach { e =>
// if e ∉ v
if (!v.contains(e)) {
// v ← v ⋃ e
v.add(e)
// f ← get furthest element from W to q
f = W.peek()
// if distance(e, q) < distance(f, q) or │W│ < ef
if (distanceOrd.lt(df(vectors(e), q), df(vectors(f.index), q)) || W.size < ef) {
// C ← C ⋃ e
// W ← W ⋃ e
addCandidate(e)
// if │W│ > ef
if (W.size > ef) {
// remove furthest element from W to q
W.pop()
}
}
}
}
}
}
}
W.toList
}
// Algorithm 3
// SELECT-NEIGHBORS-SIMPLE(q, C, M)
// Input: base element q, candidate elements C, number of neighbors to
// return M
// Output: M nearest elements to q
// return M nearest elements from C to q
// SELECT-NEIGHBORS(q, W, M, lc)
private def selectNeighbors1Alg3(candidates: CandidateList[D], M: Int): Seq[Candidate[D]] = {
candidates.toList.sorted(candidateOrd).take(M)
}
// SELECT-NEIGHBORS(e, eConn, Mmax, lc)
private def selectNeighbors2Alg3(e: Int, C: Seq[Int], M: Int): Seq[Candidate[D]] = {
val vector = vectors(e)
val candidateList = CandidateList(candidateOrd.reverse)
C.foreach { candidate =>
candidateList.push(Candidate(candidate, df(vectors(candidate), vector)))
}
candidateList.take(M)
}
// Algorithm 1
// INSERT(hnsw, q, M, Mmax, efConstruction, mL)
// Input: multilayer graph hnsw, new element q, number of established
// connections M, maximum number of connections for each element
// per layer Mmax, size of the dynamic candidate list efConstruction, normalization factor for level generation mL
// Output: update hnsw inserting element q
override def add(vector: Vec[T]): Unit = {
Utils.assert(vector.dimension == dimension, s"input vector dim=${vector.dimension}, knn dim=$dimension")
val q = vector
val index = addVector(vector)
// l ← ⌊-ln(unif(0..1))∙mL⌋ // new element’s level
val level = (-math.log(random.nextDouble()) * mL).floor.toInt
allocateGraphSpace(index, level)
// W ← ∅ // list for the currently found nearest elements
val W = CandidateList(candidateOrd.reverse)
// ep ← get enter point for hnsw
// L ← level of ep // top layer for hnsw
val currentEntryPoint = getOrElseUpdateEntryPoint(index, level)
var ep = currentEntryPoint.index
val topLevel = currentEntryPoint.level
// for lc ← L … l+1
for (lc <- topLevel to level + 1 by -1) {
// W ← SEARCH-LAYER(q, ep, ef=1, lc)
W.pushAll(searchLayer(q, ep, 1, lc))
// ep ← get the nearest element from W to q
ep = W.pop().index
}
// for lc ← min(L, l) … 0
for (lc <- math.min(topLevel, level) to 0 by -1) {
val maxNeighborCountAtLc = maxNeighborCount(lc)
// W ← SEARCH-LAYER(q, ep, efConstruction, lc)
W.pushAll(searchLayer(q, ep, efConstruction, lc))
// neighbors ← SELECT-NEIGHBORS(q, W, M, lc) // alg. 3 or alg. 4
val neighbors = selectNeighbors1Alg3(W, maxNeighborCountAtLc)
// add bidirectional connections from neighbors to q at layer lc
neighbors.foreach { neighbor =>
if (neighbor.index != index) { // don't self link
neighborGraph(neighbor.index)(lc).arr += index
neighborGraph(index)(lc).arr += neighbor.index
}
}
// for each e ∈ neighbors // shrink connections if needed
neighbors.foreach { neighbor =>
// eConn ← neighbourhood(e) at layer lc
val eConn = neighborGraph(neighbor.index)(lc)
// if │eConn│ > Mmax // shrink connections of e
// // if lc = 0 then Mmax = Mmax0
if (eConn.arr.size > maxNeighborCountAtLc) {
// eNewConn ← SELECT-NEIGHBORS(e, eConn, Mmax, lc)
// // alg. 3 or alg. 4
val eNewConn = selectNeighbors2Alg3(neighbor.index, eConn.arr, maxNeighborCountAtLc)
// set neighbourhood(e) at layer lc to eNewConn
neighborGraph(neighbor.index)(lc) = NeighborList(ArrayBuffer(eNewConn.map(_.index): _*))
}
}
// ep ← W
ep = W.pop().index
}
// if l > L
// set enter point for hnsw to q
if (level > topLevel) {
hnswEntryPoint = Some(EntryPoint(index, level))
}
}
// Algorithm 5
// K-NN-SEARCH(hnsw, q, K, ef)
// Input: multilayer graph hnsw, query element q, number of nearest
// neighbors to return K, size of the dynamic candidate list ef
// Output: K nearest elements to q
override def knn(vector: Vec[T], k: Int): Seq[Vec[T]] = {
Utils.assert(vector.dimension == dimension, s"input vector dim=${vector.dimension}, knn dim=$dimension")
val q = vector
// W ← ∅ // set for the current nearest elements
val W = CandidateList(candidateOrd.reverse)
// ep ← get enter point for hnsw
// L ← level of ep // top layer for hnsw
val currentEntryPoint = hnswEntryPoint.get // the entry point must be set, or else we will throw since there's no vectors in the model
var ep = currentEntryPoint.index
val topLevel = currentEntryPoint.level
// for lc ← L … 1
for (lc <- topLevel to 1 by -1) {
// W ← SEARCH-LAYER(q, ep, ef=1, lc)
W.pushAll(searchLayer(q, ep, 1, lc))
// ep ← get nearest element from W to q
ep = W.pop().index
}
// W ← SEARCH-LAYER(q, ep, ef, lc =0)
W.pushAll(searchLayer(q, ep, ef, 0))
W.take(k).map(c => vectors(c.index))
}
}
object RefHnsw {
case class EntryPoint(index: Int, level: Int)
case class NeighborList(arr: ArrayBuffer[Int])
case class CandidateList[D](ord: Ordering[Candidate[D]]) {
private val lst = new mutable.HashSet[Int]()
private val pq = new mutable.PriorityQueue[Candidate[D]]()(ord)
def push(candidates: Candidate[D]*): Unit = pushAll(candidates)
def pushAll(candidates: Seq[Candidate[D]]): Unit = {
candidates.foreach { candidate =>
if (!lst.contains(candidate.index)) {
lst.add(candidate.index)
pq.enqueue(candidate)
}
}
}
def peek(): Candidate[D] = pq.head
def pop(): Candidate[D] = {
val head = pq.head
lst.remove(head.index)
pq.dequeue()
}
def nonEmpty: Boolean = pq.nonEmpty
def isEmpty: Boolean = pq.isEmpty
def toList: Seq[Candidate[D]] = pq.toList
def size: Int = pq.size
def take(n: Int): Seq[Candidate[D]] = {
toList.sorted(ord.reverse).take(n)
}
}
}