-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.go
331 lines (287 loc) · 7.47 KB
/
interface.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
package hnswindex
import (
"bytes"
"fmt"
"math"
"math/rand/v2"
"github.com/bmeg/hnsw-index/distqueue"
"github.com/cockroachdb/pebble"
)
type Node struct {
Id []byte
Vector []float32
}
type Graph struct {
graphid uint32
name string
m uint8 //number of layers
efCount int //number of friends in KNN construction
dim int //dimensions of stored vectors
levelMult float64 //multipler to calculate random layer
db *DB
}
type batchInsert struct {
key, value []byte
}
func (graph *Graph) Insert(name []byte, vec []float32) error {
layer := uint8(math.Floor(-math.Log(rand.Float64() * graph.levelMult)))
//fmt.Printf("Insert Layer: %d\n", layer)
eLayer, ep, eVec, err := graph.getEntryPoint()
if err != nil {
return err
}
if ep == 0 {
//no entrypoint, so this node becomes it
nid, err := graph.db.insertGraphVector(graph.graphid, name, vec)
if err != nil {
return err
}
if nid != 1 {
return fmt.Errorf("entrypoint init error")
}
//fmt.Printf("entrypoint id: %d\n", nid)
return nil
}
id, err := graph.db.insertGraphVector(graph.graphid, name, vec)
if err != nil {
return err
}
if id == 0 {
return fmt.Errorf("invalid node id (0) generated")
}
eDist := Euclidean(vec, eVec)
//move down the layers to the target layer, attempting to get close along the way
for l := eLayer; l > layer+1; l-- {
changed := true
for changed {
changed = false
lf, err := graph.getLayerFriends(eLayer, ep, graph.efCount)
if err != nil {
return err
}
fds, err := graph.getDistances(vec, lf)
if err != nil {
return err
}
for i := range lf {
if fds[i] < eDist {
ep = lf[i]
eDist = fds[i]
changed = true
}
}
}
}
inserts := make([]*batchInsert, 0, 100)
for l := int(layer); l >= 0; l-- {
res, resDist, err := graph.layerSearch(vec, uint8(l), ep, graph.efCount)
if err != nil {
return err
}
//fmt.Printf("Layer Search %#v %#v\n", res, resDist)
//record links for current layer
for i := range res {
//fmt.Printf("Inserting link: %d %d %d %f\n", l, id, res[i], resDist[i])
kS, vS := graph.genInsertLink(graph.graphid, uint8(l), id, res[i], resDist[i])
kD, vD := graph.genInsertLink(graph.graphid, uint8(l), res[i], id, resDist[i])
inserts = append(inserts, &batchInsert{key: kS, value: vS}, &batchInsert{key: kD, value: vD})
}
}
batch := graph.db.db.NewBatch()
for _, i := range inserts {
batch.Set(i.key, i.value, nil)
}
batch.Commit(nil)
batch.Close()
return nil
}
func (graph *Graph) Search(vec []float32, K int, ef int) ([][]byte, error) {
eLevel, ePoint, eVec, err := graph.getEntryPoint()
if err != nil {
return nil, err
}
eDist := Euclidean(vec, eVec)
for l := int(eLevel); l >= 0; l-- {
changed := true
for changed {
changed = false
eFriends, err := graph.getLayerFriends(uint8(l), ePoint, graph.efCount)
if err != nil {
return nil, err
}
fDists, err := graph.getDistances(vec, eFriends)
if err != nil {
return nil, err
}
for i := range eFriends {
if fDists[i] < eDist {
ePoint = eFriends[i]
eDist = fDists[i]
changed = true
}
}
}
}
ids, _, err := graph.layerSearch(vec, 0, ePoint, ef)
if err != nil {
return nil, err
}
out := make([][]byte, 0, K)
for i := 0; i < K && i < len(ids); i++ {
n, err := graph.db.getVectorName(graph.graphid, ids[i])
if err == nil {
out = append(out, n)
}
}
return out, nil
}
/*
func (graph *Graph) FindLayerEntryPoint(layer uint8) (uint64, error) {
prefix := LayerPrefixEncode(graph.graphid, layer)
iter, err := graph.db.db.NewIter(&pebble.IterOptions{})
if err != nil {
return 0, err
}
defer iter.Close()
iter.SeekGE(prefix)
if iter.Valid() && bytes.HasPrefix(iter.Key(), prefix) {
_, _, source, _ := LayerKeyParse(iter.Key())
return source, nil
}
return 0, nil
}
*/
func (graph *Graph) layerSearch(vec []float32, layer uint8, entryPoint uint64, K int) ([]uint64, []float32, error) {
if entryPoint == 0 {
return []uint64{}, []float32{}, fmt.Errorf("invalid entryPoint id")
}
visited := map[uint64]bool{}
candidates := distqueue.NewMin[float32, uint64]()
w := distqueue.NewMinCapped[float32, uint64](K)
eVec, err := graph.GetVec(entryPoint)
if err != nil {
return nil, nil, err
}
d := Euclidean(vec, eVec)
w.Insert(d, entryPoint)
candidates.Insert(d, entryPoint)
visited[entryPoint] = true
for len(candidates) > 0 {
cdist, c := candidates.Pop()
fdist := w.Max()
if cdist > fdist {
break
}
neighbors, err := graph.getLayerFriends(layer, c, graph.efCount)
if err != nil {
return nil, nil, err
}
ndists, err := graph.getDistances(vec, neighbors)
if err != nil {
return nil, nil, err
}
for n := range neighbors {
if _, ok := visited[neighbors[n]]; !ok {
if len(w) > 0 {
fdist := w.Max()
if (ndists[n] < fdist) || !w.Filled() {
w.Insert(ndists[n], neighbors[n])
candidates.Insert(ndists[n], neighbors[n])
}
}
visited[neighbors[n]] = true
}
}
}
outI := make([]uint64, len(w))
outD := make([]float32, len(w))
for i := range w {
outI[i] = w[i].Value
outD[i] = w[i].Dist
}
return outI, outD, nil
}
func (graph *Graph) getDistances(v []float32, n []uint64) ([]float32, error) {
out := make([]float32, len(n))
iter, err := graph.db.db.NewIter(&pebble.IterOptions{})
if err != nil {
return nil, err
}
defer iter.Close()
for i := range n {
key := VectorKeyEncode(graph.graphid, n[i])
if iter.SeekGE(key) {
if bytes.Equal(iter.Key(), key) {
out[i] = Euclidean(v, VectorValueParse(iter.Value()))
}
}
}
return out, nil
}
func (graph *Graph) GetVec(id uint64) ([]float32, error) {
key := VectorKeyEncode(graph.graphid, id)
out, closer, err := graph.db.db.Get(key)
defer closer.Close()
if err != nil {
return nil, err
}
return VectorValueParse(out), nil
}
func (graph *Graph) getLayerFriends(l uint8, a uint64, count int) ([]uint64, error) {
prefix := LayerKeyPrefixEncode(graph.graphid, l, a)
iter, err := graph.db.db.NewIter(&pebble.IterOptions{LowerBound: prefix})
if err != nil {
return nil, err
}
defer iter.Close()
out := make([]uint64, 0, 10)
i := 0
for iter.SeekGE(prefix); iter.Valid() && bytes.HasPrefix(iter.Key(), prefix) && i < count; iter.Next() {
dest := LayerValueParse(iter.Value())
out = append(out, dest)
i++
}
return out, nil
}
func (graph *Graph) getEntryPoint() (uint8, uint64, []float32, error) {
key := VectorKeyEncode(graph.graphid, 1)
out, closer, err := graph.db.db.Get(key)
if err != nil {
if err == pebble.ErrNotFound {
return 0, 0, nil, nil
}
return 0, 0, nil, err
}
defer closer.Close()
v := VectorValueParse(out)
return graph.m, 1, v, nil
}
type LayerEdge struct {
Source, Dest uint64
Dist float32
}
func (graph *Graph) ListLayer(layer uint8) chan *LayerEdge {
out := make(chan *LayerEdge, 10)
go func() {
defer close(out)
prefix := LayerPrefixEncode(graph.graphid, layer)
iter, err := graph.db.db.NewIter(&pebble.IterOptions{LowerBound: prefix})
if err != nil {
return
}
defer iter.Close()
for iter.SeekGE(prefix); iter.Valid() && bytes.HasPrefix(iter.Key(), prefix); iter.Next() {
_, _, src, dist := LayerKeyParse(iter.Key())
dest := LayerValueParse(iter.Value())
out <- &LayerEdge{
Source: src, Dest: dest, Dist: dist,
}
}
}()
return out
}
func (graph *Graph) genInsertLink(graphId uint32, layer uint8, src uint64, dst uint64, dist float32) ([]byte, []byte) {
key := LayerKeyEncode(graphId, layer, src, dist)
value := LayerValueEncode(dst)
return key, value
}