-
Notifications
You must be signed in to change notification settings - Fork 2
/
hnsw.go
640 lines (546 loc) · 14.4 KB
/
hnsw.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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
// Copyright 2023 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hnsw
import (
"compress/gzip"
"encoding/binary"
"fmt"
"io"
"math"
"math/rand"
"os"
"sync"
"time"
"github.com/casibase/go-hnsw/bitsetpool"
"github.com/casibase/go-hnsw/distqueue"
"github.com/casibase/go-hnsw/f32"
)
type Point []float32
func (a Point) Size() int {
return len(a) * 4
}
type node struct {
sync.RWMutex
locked bool
p Point
level int
friends [][]uint32
}
type Hnsw struct {
sync.RWMutex
M int
M0 int
efConstruction int
linkMode int
DelaunayType int
DistFunc func([]float32, []float32) float32
nodes []node
bitset *bitsetpool.BitsetPool
LevelMult float64
maxLayer int
enterpoint uint32
}
// Load opens a index file previously written by Save(). Returnes a new index and the timestamp the file was written
func Load(filename string) (*Hnsw, int64, error) {
f, err := os.Open(filename)
if err != nil {
return nil, 0, err
}
z, err := gzip.NewReader(f)
if err != nil {
return nil, 0, err
}
timestamp := readInt64(z)
h := new(Hnsw)
h.M = readInt32(z)
h.M0 = readInt32(z)
h.efConstruction = readInt32(z)
h.linkMode = readInt32(z)
h.DelaunayType = readInt32(z)
h.LevelMult = readFloat64(z)
h.maxLayer = readInt32(z)
h.enterpoint = uint32(readInt32(z))
h.DistFunc = f32.L2Squared8AVX
h.bitset = bitsetpool.New()
l := readInt32(z)
h.nodes = make([]node, l)
for i := range h.nodes {
l := readInt32(z)
h.nodes[i].p = make([]float32, l)
err = binary.Read(z, binary.LittleEndian, h.nodes[i].p)
if err != nil {
panic(err)
}
h.nodes[i].level = readInt32(z)
l = readInt32(z)
h.nodes[i].friends = make([][]uint32, l)
for j := range h.nodes[i].friends {
l := readInt32(z)
h.nodes[i].friends[j] = make([]uint32, l)
err = binary.Read(z, binary.LittleEndian, h.nodes[i].friends[j])
if err != nil {
panic(err)
}
}
}
z.Close()
f.Close()
return h, timestamp, nil
}
// Save writes to current index to a gzipped binary data file
func (h *Hnsw) Save(filename string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
z := gzip.NewWriter(f)
timestamp := time.Now().Unix()
writeInt64(timestamp, z)
writeInt32(h.M, z)
writeInt32(h.M0, z)
writeInt32(h.efConstruction, z)
writeInt32(h.linkMode, z)
writeInt32(h.DelaunayType, z)
writeFloat64(h.LevelMult, z)
writeInt32(h.maxLayer, z)
writeInt32(int(h.enterpoint), z)
l := len(h.nodes)
writeInt32(l, z)
if err != nil {
return err
}
for _, n := range h.nodes {
l := len(n.p)
writeInt32(l, z)
err = binary.Write(z, binary.LittleEndian, []float32(n.p))
if err != nil {
panic(err)
}
writeInt32(n.level, z)
l = len(n.friends)
writeInt32(l, z)
for _, f := range n.friends {
l := len(f)
writeInt32(l, z)
err = binary.Write(z, binary.LittleEndian, f)
if err != nil {
panic(err)
}
}
}
z.Close()
f.Close()
return nil
}
func writeInt64(v int64, w io.Writer) {
err := binary.Write(w, binary.LittleEndian, &v)
if err != nil {
panic(err)
}
}
func writeInt32(v int, w io.Writer) {
i := int32(v)
err := binary.Write(w, binary.LittleEndian, &i)
if err != nil {
panic(err)
}
}
func readInt32(r io.Reader) int {
var i int32
err := binary.Read(r, binary.LittleEndian, &i)
if err != nil {
panic(err)
}
return int(i)
}
func writeFloat64(v float64, w io.Writer) {
err := binary.Write(w, binary.LittleEndian, &v)
if err != nil {
panic(err)
}
}
func readInt64(r io.Reader) (v int64) {
err := binary.Read(r, binary.LittleEndian, &v)
if err != nil {
panic(err)
}
return
}
func readFloat64(r io.Reader) (v float64) {
err := binary.Read(r, binary.LittleEndian, &v)
if err != nil {
panic(err)
}
return
}
func (h *Hnsw) getFriends(n uint32, level int) []uint32 {
if len(h.nodes[n].friends) < level+1 {
return make([]uint32, 0)
}
return h.nodes[n].friends[level]
}
func (h *Hnsw) Link(first, second uint32, level int) {
maxL := h.M
if level == 0 {
maxL = h.M0
}
h.RLock()
node := &h.nodes[first]
h.RUnlock()
node.Lock()
// check if we have allocated friends slices up to this level?
if len(node.friends) < level+1 {
for j := len(node.friends); j <= level; j++ {
// allocate new list with 0 elements but capacity maxL
node.friends = append(node.friends, make([]uint32, 0, maxL))
}
// now grow it by one and add the first connection for this layer
node.friends[level] = node.friends[level][0:1]
node.friends[level][0] = second
} else {
// we did have some already... this will allocate more space if it overflows maxL
node.friends[level] = append(node.friends[level], second)
}
l := len(node.friends[level])
if l > maxL {
// to many links, deal with it
switch h.DelaunayType {
case 0:
resultSet := &distqueue.DistQueueClosestLast{Size: len(node.friends[level])}
for _, n := range node.friends[level] {
resultSet.Push(n, h.DistFunc(node.p, h.nodes[n].p))
}
for resultSet.Len() > maxL {
resultSet.Pop()
}
// FRIENDS ARE STORED IN DISTANCE ORDER, closest at index 0
node.friends[level] = node.friends[level][0:maxL]
for i := maxL - 1; i >= 0; i-- {
item := resultSet.Pop()
node.friends[level][i] = item.ID
}
case 1:
resultSet := &distqueue.DistQueueClosestFirst{Size: len(node.friends[level])}
for _, n := range node.friends[level] {
resultSet.Push(n, h.DistFunc(node.p, h.nodes[n].p))
}
h.getNeighborsByHeuristicClosestFirst(resultSet, maxL)
// FRIENDS ARE STORED IN DISTANCE ORDER, closest at index 0
node.friends[level] = node.friends[level][0:maxL]
for i := 0; i < maxL; i++ {
item := resultSet.Pop()
node.friends[level][i] = item.ID
}
}
}
node.Unlock()
}
func (h *Hnsw) getNeighborsByHeuristicClosestLast(resultSet1 *distqueue.DistQueueClosestLast, M int) {
if resultSet1.Len() <= M {
return
}
resultSet := &distqueue.DistQueueClosestFirst{Size: resultSet1.Len()}
tempList := &distqueue.DistQueueClosestFirst{Size: resultSet1.Len()}
result := make([]*distqueue.Item, 0, M)
for resultSet1.Len() > 0 {
resultSet.PushItem(resultSet1.Pop())
}
for resultSet.Len() > 0 {
if len(result) >= M {
break
}
e := resultSet.Pop()
good := true
for _, r := range result {
if h.DistFunc(h.nodes[r.ID].p, h.nodes[e.ID].p) < e.D {
good = false
break
}
}
if good {
result = append(result, e)
} else {
tempList.PushItem(e)
}
}
for len(result) < M && tempList.Len() > 0 {
result = append(result, tempList.Pop())
}
for _, item := range result {
resultSet1.PushItem(item)
}
}
func (h *Hnsw) getNeighborsByHeuristicClosestFirst(resultSet *distqueue.DistQueueClosestFirst, M int) {
if resultSet.Len() <= M {
return
}
tempList := &distqueue.DistQueueClosestFirst{Size: resultSet.Len()}
result := make([]*distqueue.Item, 0, M)
for resultSet.Len() > 0 {
if len(result) >= M {
break
}
e := resultSet.Pop()
good := true
for _, r := range result {
if h.DistFunc(h.nodes[r.ID].p, h.nodes[e.ID].p) < e.D {
good = false
break
}
}
if good {
result = append(result, e)
} else {
tempList.PushItem(e)
}
}
for len(result) < M && tempList.Len() > 0 {
result = append(result, tempList.Pop())
}
resultSet.Reset()
for _, item := range result {
resultSet.PushItem(item)
}
}
func New(M int, efConstruction int, first Point) *Hnsw {
h := Hnsw{}
h.M = M
// default values used in c++ implementation
h.LevelMult = 1 / math.Log(float64(M))
h.efConstruction = efConstruction
h.M0 = 2 * M
h.DelaunayType = 1
h.bitset = bitsetpool.New()
h.DistFunc = f32.L2Squared8AVX
// add first point, it will be our enterpoint (index 0)
h.nodes = []node{node{level: 0, p: first}}
return &h
}
func (h *Hnsw) Stats() string {
s := "HNSW Index\n"
s = s + fmt.Sprintf("M: %v, efConstruction: %v\n", h.M, h.efConstruction)
s = s + fmt.Sprintf("DelaunayType: %v\n", h.DelaunayType)
s = s + fmt.Sprintf("Number of nodes: %v\n", len(h.nodes))
s = s + fmt.Sprintf("Max layer: %v\n", h.maxLayer)
memoryUseData := 0
memoryUseIndex := 0
levCount := make([]int, h.maxLayer+1)
conns := make([]int, h.maxLayer+1)
connsC := make([]int, h.maxLayer+1)
for i := range h.nodes {
levCount[h.nodes[i].level]++
for j := 0; j <= h.nodes[i].level; j++ {
if len(h.nodes[i].friends) > j {
l := len(h.nodes[i].friends[j])
conns[j] += l
connsC[j]++
}
}
memoryUseData += h.nodes[i].p.Size()
memoryUseIndex += h.nodes[i].level*h.M*4 + h.M0*4
}
for i := range levCount {
avg := conns[i] / max(1, connsC[i])
s = s + fmt.Sprintf("Level %v: %v nodes, average number of connections %v\n", i, levCount[i], avg)
}
s = s + fmt.Sprintf("Memory use for data: %v (%v bytes / point)\n", memoryUseData, memoryUseData/len(h.nodes))
s = s + fmt.Sprintf("Memory use for index: %v (avg %v bytes / point)\n", memoryUseIndex, memoryUseIndex/len(h.nodes))
return s
}
func (h *Hnsw) Grow(size int) {
if size+1 <= len(h.nodes) {
return
}
newNodes := make([]node, len(h.nodes), size+1)
copy(newNodes, h.nodes)
h.nodes = newNodes
}
func (h *Hnsw) Add(q Point, id uint32) {
if id == 0 {
panic("Id 0 is reserved, use ID:s starting from 1 when building index")
}
// generate random level
curlevel := int(math.Floor(-math.Log(rand.Float64() * h.LevelMult)))
epID := h.enterpoint
currentMaxLayer := h.nodes[epID].level
ep := &distqueue.Item{ID: h.enterpoint, D: h.DistFunc(h.nodes[h.enterpoint].p, q)}
// assume Grow has been called in advance
newID := id
newNode := node{p: q, level: curlevel, friends: make([][]uint32, min(curlevel, currentMaxLayer)+1)}
// first pass, find another ep if curlevel < maxLayer
for level := currentMaxLayer; level > curlevel; level-- {
changed := true
for changed {
changed = false
for _, i := range h.getFriends(ep.ID, level) {
d := h.DistFunc(h.nodes[i].p, q)
if d < ep.D {
ep = &distqueue.Item{ID: i, D: d}
changed = true
}
}
}
}
// second pass, ef = efConstruction
// loop through every level from the new nodes level down to level 0
// create new connections in every layer
for level := min(curlevel, currentMaxLayer); level >= 0; level-- {
resultSet := &distqueue.DistQueueClosestLast{}
h.searchAtLayer(q, resultSet, h.efConstruction, ep, level)
switch h.DelaunayType {
case 0:
// shrink resultSet to M closest elements (the simple heuristic)
for resultSet.Len() > h.M {
resultSet.Pop()
}
case 1:
h.getNeighborsByHeuristicClosestLast(resultSet, h.M)
}
newNode.friends[level] = make([]uint32, resultSet.Len())
for i := resultSet.Len() - 1; i >= 0; i-- {
item := resultSet.Pop()
// store in order, closest at index 0
newNode.friends[level][i] = item.ID
}
}
h.Lock()
// Add it and increase slice length if neccessary
if len(h.nodes) < int(newID)+1 {
h.nodes = h.nodes[0 : newID+1]
}
h.nodes[newID] = newNode
h.Unlock()
// now add connections to newNode from newNodes neighbours (makes it visible in the graph)
for level := min(curlevel, currentMaxLayer); level >= 0; level-- {
for _, n := range newNode.friends[level] {
h.Link(n, newID, level)
}
}
h.Lock()
if curlevel > h.maxLayer {
h.maxLayer = curlevel
h.enterpoint = newID
}
h.Unlock()
}
func (h *Hnsw) searchAtLayer(q Point, resultSet *distqueue.DistQueueClosestLast, efConstruction int, ep *distqueue.Item, level int) {
var pool, visited = h.bitset.Get()
//visited := make(map[uint32]bool)
candidates := &distqueue.DistQueueClosestFirst{Size: efConstruction * 3}
visited.Set(uint(ep.ID))
//visited[ep.ID] = true
candidates.Push(ep.ID, ep.D)
resultSet.Push(ep.ID, ep.D)
for candidates.Len() > 0 {
_, lowerBound := resultSet.Top() // worst distance so far
c := candidates.Pop()
if c.D > lowerBound {
// since candidates is sorted, it wont get any better...
break
}
if len(h.nodes[c.ID].friends) >= level+1 {
friends := h.nodes[c.ID].friends[level]
for _, n := range friends {
if !visited.Test(uint(n)) {
visited.Set(uint(n))
d := h.DistFunc(q, h.nodes[n].p)
_, topD := resultSet.Top()
if resultSet.Len() < efConstruction {
item := resultSet.Push(n, d)
candidates.PushItem(item)
} else if topD > d {
// keep length of resultSet to max efConstruction
item := resultSet.PopAndPush(n, d)
candidates.PushItem(item)
}
}
}
}
}
h.bitset.Free(pool)
}
// SearchBrute returns the true K nearest neigbours to search point q
func (h *Hnsw) SearchBrute(q Point, K int) *distqueue.DistQueueClosestLast {
resultSet := &distqueue.DistQueueClosestLast{Size: K}
for i := 1; i < len(h.nodes); i++ {
d := h.DistFunc(h.nodes[i].p, q)
if resultSet.Len() < K {
resultSet.Push(uint32(i), d)
continue
}
_, topD := resultSet.Head()
if d < topD {
resultSet.PopAndPush(uint32(i), d)
continue
}
}
return resultSet
}
// Benchmark test precision by comparing the results of SearchBrute and Search
func (h *Hnsw) Benchmark(q Point, ef int, K int) float64 {
result := h.Search(q, ef, K)
groundTruth := h.SearchBrute(q, K)
truth := make([]uint32, 0)
for groundTruth.Len() > 0 {
truth = append(truth, groundTruth.Pop().ID)
}
p := 0
for result.Len() > 0 {
i := result.Pop()
for j := 0; j < K; j++ {
if truth[j] == i.ID {
p++
}
}
}
return float64(p) / float64(K)
}
func (h *Hnsw) Search(q Point, ef int, K int) *distqueue.DistQueueClosestLast {
h.RLock()
currentMaxLayer := h.maxLayer
ep := &distqueue.Item{ID: h.enterpoint, D: h.DistFunc(h.nodes[h.enterpoint].p, q)}
h.RUnlock()
resultSet := &distqueue.DistQueueClosestLast{Size: ef + 1}
// first pass, find best ep
for level := currentMaxLayer; level > 0; level-- {
changed := true
for changed {
changed = false
for _, i := range h.getFriends(ep.ID, level) {
d := h.DistFunc(h.nodes[i].p, q)
if d < ep.D {
ep.ID, ep.D = i, d
changed = true
}
}
}
}
h.searchAtLayer(q, resultSet, ef, ep, 0)
for resultSet.Len() > K {
resultSet.Pop()
}
return resultSet
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}