This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
forked from coder/hnsw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.go
574 lines (492 loc) · 13.2 KB
/
graph.go
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
package hnsw
import (
"cmp"
"fmt"
"math"
"math/rand"
"slices"
"sync"
"time"
"github.com/hypermodeinc/hnsw/heap"
"golang.org/x/exp/maps"
)
type Vector = []float32
// Node is a node in the graph.
type Node[K cmp.Ordered] struct {
Key K
Value Vector
}
func MakeNodes[K cmp.Ordered](keys []K, vecs []Vector) ([]Node[K], error) {
if len(keys) != len(vecs) {
return nil, fmt.Errorf("keys and vecs must have the same length")
}
nodes := make([]Node[K], len(keys))
for i := range keys {
nodes[i] = MakeNode(keys[i], vecs[i])
}
return nodes, nil
}
func MakeNode[K cmp.Ordered](key K, vec Vector) Node[K] {
return Node[K]{Key: key, Value: vec}
}
// layerNode is a node in a layer of the graph.
type layerNode[K cmp.Ordered] struct {
Node[K]
// neighbors is map of neighbor keys to neighbor nodes.
// It is a map and not a slice to allow for efficient deletes, esp.
// when M is high.
neighbors map[K]*layerNode[K]
}
// addNeighbor adds a o neighbor to the node, replacing the neighbor
// with the worst distance if the neighbor set is full.
func (n *layerNode[K]) addNeighbor(newNode *layerNode[K], m int, dist DistanceFunc) error {
if n.neighbors == nil {
n.neighbors = make(map[K]*layerNode[K], m)
}
n.neighbors[newNode.Key] = newNode
if len(n.neighbors) <= m {
return nil
}
// Find the neighbor with the worst distance.
var (
worstDist = float32(math.Inf(-1))
worst *layerNode[K]
)
for _, neighbor := range n.neighbors {
d, err := dist(neighbor.Value, n.Value)
if err != nil {
return err
}
// d > worstDist may always be false if the distance function
// returns NaN, e.g., when the embeddings are zero.
if d > worstDist || worst == nil {
worstDist = d
worst = neighbor
}
}
delete(n.neighbors, worst.Key)
// Delete backlink from the worst neighbor.
delete(worst.neighbors, n.Key)
worst.replenish(m)
return nil
}
type searchCandidate[K cmp.Ordered] struct {
node *layerNode[K]
dist float32
}
func (s searchCandidate[K]) Less(o searchCandidate[K]) bool {
return s.dist < o.dist
}
// search returns the layer node closest to the target node
// within the same layer.
func (n *layerNode[K]) search(
// k is the number of candidates in the result set.
k int,
efSearch int,
target Vector,
distance DistanceFunc,
) ([]searchCandidate[K], error) {
// This is a basic greedy algorithm to find the entry point at the given level
// that is closest to the target node.
if n == nil {
return nil, fmt.Errorf("node is nil")
}
candidates := heap.Heap[searchCandidate[K]]{}
candidates.Init(make([]searchCandidate[K], 0, efSearch))
dist, err := distance(n.Value, target)
if err != nil {
return nil, err
}
candidates.Push(
searchCandidate[K]{
node: n,
dist: dist,
},
)
var (
result = heap.Heap[searchCandidate[K]]{}
visited = make(map[K]bool)
)
result.Init(make([]searchCandidate[K], 0, k))
// Begin with the entry node in the result set.
result.Push(candidates.Min())
visited[n.Key] = true
for candidates.Len() > 0 {
var (
current = candidates.Pop().node
improved = false
)
// We iterate the map in a sorted, deterministic fashion for
// tests.
neighborKeys := maps.Keys(current.neighbors)
slices.Sort(neighborKeys)
for _, neighborID := range neighborKeys {
neighbor := current.neighbors[neighborID]
if visited[neighborID] {
continue
}
visited[neighborID] = true
dist, err := distance(neighbor.Value, target)
if err != nil {
return nil, err
}
improved = improved || dist < result.Min().dist
if result.Len() < k {
result.Push(searchCandidate[K]{node: neighbor, dist: dist})
} else if dist < result.Max().dist {
result.PopLast()
result.Push(searchCandidate[K]{node: neighbor, dist: dist})
}
candidates.Push(searchCandidate[K]{node: neighbor, dist: dist})
// Always store candidates if we haven't reached the limit.
if candidates.Len() > efSearch {
candidates.PopLast()
}
}
// Termination condition: no improvement in distance and at least
// kMin candidates in the result set.
if !improved && result.Len() >= k {
break
}
}
return result.Slice(), nil
}
func (n *layerNode[K]) replenish(m int) {
if len(n.neighbors) >= m {
return
}
// Restore connectivity by adding new neighbors.
// This is a naive implementation that could be improved by
// using a priority queue to find the best candidates.
for _, neighbor := range n.neighbors {
for key, candidate := range neighbor.neighbors {
if _, ok := n.neighbors[key]; ok {
// do not add duplicates
continue
}
if candidate == n {
continue
}
n.addNeighbor(candidate, m, CosineDistance)
if len(n.neighbors) >= m {
return
}
}
}
}
// isolates remove the node from the graph by removing all connections
// to neighbors.
func (n *layerNode[K]) isolate(m int) {
for _, neighbor := range n.neighbors {
delete(neighbor.neighbors, n.Key)
neighbor.replenish(m)
}
}
type layer[K cmp.Ordered] struct {
// nodes is a map of nodes IDs to nodes.
// All nodes in a higher layer are also in the lower layers, an essential
// property of the graph.
//
// nodes is exported for interop with encoding/gob.
nodes map[K]*layerNode[K]
}
// entry returns the entry node of the layer.
// It doesn't matter which node is returned, even that the
// entry node is consistent, so we just return the first node
// in the map to avoid tracking extra state.
func (l *layer[K]) entry() *layerNode[K] {
if l == nil {
return nil
}
for _, node := range l.nodes {
return node
}
return nil
}
func (l *layer[K]) size() int {
if l == nil {
return 0
}
return len(l.nodes)
}
// Graph is a Hierarchical Navigable Small World graph.
// All public parameters must be set before adding nodes to the graph.
// K is cmp.Ordered instead of of comparable so that they can be sorted.
type Graph[K cmp.Ordered] struct {
mu sync.RWMutex
// Distance is the distance function used to compare embeddings.
Distance DistanceFunc
// Rng is used for level generation. It may be set to a deterministic value
// for reproducibility. Note that deterministic number generation can lead to
// degenerate graphs when exposed to adversarial inputs.
Rng *rand.Rand
// M is the maximum number of neighbors to keep for each node.
// A good default for OpenAI embeddings is 16.
M int
// Ml is the level generation factor.
// E.g., for Ml = 0.25, each layer is 1/4 the size of the previous layer.
Ml float64
// EfSearch is the number of nodes to consider in the search phase.
// 20 is a reasonable default. Higher values improve search accuracy at
// the expense of memory.
EfSearch int
// EfConstruction is the number of nodes to consider in the construction phase.
// 16 is a reasonable default. Higher values improve graph quality at the
// expense of memory.
EfConstruction int
// layers is a slice of layers in the graph.
layers []*layer[K]
}
func defaultRand() *rand.Rand {
return rand.New(rand.NewSource(time.Now().UnixNano()))
}
// NewGraph returns a new graph with default parameters, roughly designed for
// storing OpenAI embeddings.
func NewGraph[K cmp.Ordered]() *Graph[K] {
return &Graph[K]{
M: 16,
Ml: 0.25,
Distance: CosineDistance,
EfSearch: 20,
EfConstruction: 40,
Rng: defaultRand(),
}
}
// maxLevel returns an upper-bound on the number of levels in the graph
// based on the size of the base layer.
func maxLevel(ml float64, numNodes int) (int, error) {
if ml == 0 {
return 0, fmt.Errorf("ml must be greater than 0")
}
if numNodes == 0 {
return 1, nil
}
l := math.Log(float64(numNodes))
l /= math.Log(1 / ml)
m := int(math.Round(l)) + 1
return m, nil
}
// randomLevel generates a random level for a new node.
func (h *Graph[K]) randomLevel() (int, error) {
// max avoids having to accept an additional parameter for the maximum level
// by calculating a probably good one from the size of the base layer.
max := 1
if len(h.layers) > 0 {
if h.Ml == 0 {
return 0, fmt.Errorf("(*Graph).Ml must be greater than 0")
}
var err error
max, err = maxLevel(h.Ml, h.layers[0].size())
if err != nil {
return 0, err
}
}
for level := 0; level < max; level++ {
if h.Rng == nil {
h.Rng = defaultRand()
}
r := h.Rng.Float64()
if r > h.Ml {
return level, nil
}
}
return max, nil
}
func (g *Graph[K]) assertDims(n Vector) error {
if len(g.layers) == 0 {
return nil
}
dims := g.Dims()
if dims != len(n) {
return fmt.Errorf("embedding dimension mismatch: %d != %d", dims, len(n))
}
return nil
}
// Dims returns the number of dimensions in the graph, or
// 0 if the graph is empty.
func (g *Graph[K]) Dims() int {
if len(g.layers) == 0 {
return 0
}
return len(g.layers[0].entry().Value)
}
func ptr[T any](v T) *T {
return &v
}
// Add inserts nodes into the graph.
// If another node with the same ID exists, it is replaced.
func (g *Graph[K]) Add(nodes ...Node[K]) error {
g.mu.Lock()
defer g.mu.Unlock()
for _, node := range nodes {
wasUpdated := false
key := node.Key
vec := node.Value
g.assertDims(vec)
insertLevel, err := g.randomLevel()
if err != nil {
return err
}
// Create layers that don't exist yet.
for insertLevel >= len(g.layers) {
g.layers = append(g.layers, &layer[K]{})
}
if insertLevel < 0 {
return fmt.Errorf("invalid level: %d", insertLevel)
}
var elevator *K
preLen := g.Len()
// Insert node at each layer, beginning with the highest.
for i := len(g.layers) - 1; i >= 0; i-- {
layer := g.layers[i]
newNode := &layerNode[K]{
Node: Node[K]{
Key: key,
Value: vec,
},
}
// Insert the new node into the layer.
if layer.entry() == nil {
layer.nodes = map[K]*layerNode[K]{key: newNode}
continue
}
// Now at the highest layer with more than one node, so we can begin
// searching for the best way to enter the graph.
searchPoint := layer.entry()
// On subsequent layers, we use the elevator node to enter the graph
// at the best point.
if elevator != nil {
searchPoint = layer.nodes[*elevator]
}
if g.Distance == nil {
return fmt.Errorf("(*Graph).Distance must be set")
}
neighborhood, err := searchPoint.search(g.M, g.EfConstruction, vec, g.Distance)
if err != nil {
return err
}
if len(neighborhood) == 0 {
// This should never happen because the searchPoint itself
// should be in the result set.
return fmt.Errorf("empty neighborhood")
}
// Re-set the elevator node for the next layer.
elevator = ptr(neighborhood[0].node.Key)
if insertLevel >= i {
if node, ok := layer.nodes[key]; ok {
delete(layer.nodes, key)
node.isolate(g.M)
wasUpdated = true
}
// Insert the new node into the layer.
layer.nodes[key] = newNode
for _, node := range neighborhood {
// Create a bi-directional edge between the new node and the best node.
node.node.addNeighbor(newNode, g.M, g.Distance)
newNode.addNeighbor(node.node, g.M, g.Distance)
}
}
}
// Invariant check: the node should have been added to the graph.
if wasUpdated {
if g.Len() != preLen {
return fmt.Errorf("node not updated")
}
} else {
if g.Len() != preLen+1 {
return fmt.Errorf("node not added")
}
}
}
return nil
}
type SearchResultNode[K cmp.Ordered] struct {
Node[K]
Distance float32
}
// Search finds the k nearest neighbors from the target node.
func (h *Graph[K]) Search(near Vector, k int) ([]SearchResultNode[K], error) {
h.mu.RLock()
defer h.mu.RUnlock()
h.assertDims(near)
if len(h.layers) == 0 {
return nil, fmt.Errorf("graph is empty")
}
var (
efSearch = h.EfSearch
elevator *K
)
for layer := len(h.layers) - 1; layer >= 0; layer-- {
searchPoint := h.layers[layer].entry()
if elevator != nil {
searchPoint = h.layers[layer].nodes[*elevator]
}
// Descending hierarchies
if layer > 0 {
nodes, err := searchPoint.search(1, efSearch, near, h.Distance)
if err != nil {
return nil, err
}
elevator = ptr(nodes[0].node.Key)
continue
}
nodes, err := searchPoint.search(k, efSearch, near, h.Distance)
if err != nil {
return nil, err
}
out := make([]SearchResultNode[K], 0, len(nodes))
for _, node := range nodes {
resNode := SearchResultNode[K]{
Node: node.node.Node,
Distance: node.dist,
}
out = append(out, resNode)
}
return out, nil
}
return nil, fmt.Errorf("unreachable")
}
// Len returns the number of nodes in the graph.
func (h *Graph[K]) Len() int {
if len(h.layers) == 0 {
return 0
}
return h.layers[0].size()
}
// Delete removes a node from the graph by key.
// It tries to preserve the clustering properties of the graph by
// replenishing connectivity in the affected neighborhoods.
func (h *Graph[K]) Delete(key K) bool {
h.mu.Lock()
defer h.mu.Unlock()
return h.DeleteWithLock(key)
}
func (h *Graph[K]) DeleteWithLock(key K) bool {
if len(h.layers) == 0 {
return false
}
var deleted bool
for _, layer := range h.layers {
node, ok := layer.nodes[key]
if !ok {
continue
}
delete(layer.nodes, key)
node.isolate(h.M)
deleted = true
}
return deleted
}
// Lookup returns the vector with the given key.
func (h *Graph[K]) Lookup(key K) (Vector, bool) {
h.mu.RLock()
defer h.mu.RUnlock()
if len(h.layers) == 0 {
return nil, false
}
node, ok := h.layers[0].nodes[key]
if !ok {
return nil, false
}
return node.Value, ok
}