-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
216 lines (183 loc) · 4.74 KB
/
config.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
package hnsw
import (
"encoding/binary"
"fmt"
"io"
"github.com/kuentra-official/hnsw/mathk"
)
var hnswSearchAlgorithmNames = [...]string{
"Simple",
"Heuristic",
}
type hnswSearchAlgorithm int
const (
HnswSearchSimple hnswSearchAlgorithm = iota
HnswSearchHeuristic
)
func (a hnswSearchAlgorithm) String() string {
return hnswSearchAlgorithmNames[a]
}
// Options
type HnswOption interface {
apply(*hnswConfig)
}
type hnswOption struct {
applyFunc func(*hnswConfig)
}
func (opt *hnswOption) apply(config *hnswConfig) {
opt.applyFunc(config)
}
func HnswLevelMultiplier(value float32) HnswOption {
return &hnswOption{func(config *hnswConfig) {
config.levelMultiplier = value
}}
}
func HnswEf(value int) HnswOption {
return &hnswOption{func(config *hnswConfig) {
config.ef = value
}}
}
func HnswEfConstruction(value int) HnswOption {
return &hnswOption{func(config *hnswConfig) {
config.efConstruction = value
}}
}
func HnswM(value int) HnswOption {
return &hnswOption{func(config *hnswConfig) {
config.m = value
}}
}
func HnswMmax(value int) HnswOption {
return &hnswOption{func(config *hnswConfig) {
config.mMax = value
}}
}
func HnswMmax0(value int) HnswOption {
return &hnswOption{func(config *hnswConfig) {
config.mMax0 = value
}}
}
func HnswSearchAlgorithm(value hnswSearchAlgorithm) HnswOption {
return &hnswOption{func(config *hnswConfig) {
config.searchAlgorithm = value
}}
}
func HnswHeuristicExtendCandidates(value bool) HnswOption {
return &hnswOption{func(config *hnswConfig) {
config.heuristicExtendCandidates = value
}}
}
func HnswHeuristicKeepPruned(value bool) HnswOption {
return &hnswOption{func(config *hnswConfig) {
config.heuristicKeepPruned = value
}}
}
type hnswConfig struct {
searchAlgorithm hnswSearchAlgorithm
levelMultiplier float32
ef int
efConstruction int
m int
mMax int
mMax0 int
heuristicExtendCandidates bool
heuristicKeepPruned bool
}
func newHnswConfig(options []HnswOption) *hnswConfig {
config := &hnswConfig{
searchAlgorithm: HnswSearchSimple,
levelMultiplier: -1,
ef: 20,
efConstruction: 200,
m: 16,
mMax: -1,
mMax0: -1,
heuristicExtendCandidates: false,
heuristicKeepPruned: true,
}
for _, option := range options {
option.apply(config)
}
if config.levelMultiplier == -1 {
config.levelMultiplier = 1.0 / mathk.Log(float32(config.m))
}
if config.mMax == -1 {
config.mMax = config.m
}
if config.mMax0 == -1 {
config.mMax0 = 2 * config.m
}
return config
}
func (this *hnswConfig) String() string {
return fmt.Sprintf(
"searchAlgorithm: %s, ef: %d, efConstruction: %d, m: %d, mMax: %d, mMax0: %d, levelMultiplier: %.4f, extendCandidates: %t, keepPruned: %t",
this.searchAlgorithm,
this.ef,
this.efConstruction,
this.m,
this.mMax,
this.mMax0,
this.levelMultiplier,
this.heuristicExtendCandidates,
this.heuristicKeepPruned,
)
}
func (this *hnswConfig) save(w io.Writer) error {
if err := binary.Write(w, binary.BigEndian, uint32(this.searchAlgorithm)); err != nil {
return err
}
if err := binary.Write(w, binary.BigEndian, this.levelMultiplier); err != nil {
return err
}
if err := binary.Write(w, binary.BigEndian, int32(this.ef)); err != nil {
return err
}
if err := binary.Write(w, binary.BigEndian, int32(this.efConstruction)); err != nil {
return err
}
if err := binary.Write(w, binary.BigEndian, int32(this.m)); err != nil {
return err
}
if err := binary.Write(w, binary.BigEndian, int32(this.mMax)); err != nil {
return err
}
if err := binary.Write(w, binary.BigEndian, int32(this.mMax0)); err != nil {
return err
}
return nil
}
func (this *hnswConfig) load(r io.Reader) error {
var uint32Val uint32
var int32Val int32
var float32Val float32
if err := binary.Read(r, binary.BigEndian, &uint32Val); err != nil {
return err
}
this.searchAlgorithm = hnswSearchAlgorithm(uint32Val)
if err := binary.Read(r, binary.BigEndian, &float32Val); err != nil {
return err
}
this.levelMultiplier = float32Val
if err := binary.Read(r, binary.BigEndian, &int32Val); err != nil {
return err
}
this.ef = int(int32Val)
if err := binary.Read(r, binary.BigEndian, &int32Val); err != nil {
return err
}
this.efConstruction = int(int32Val)
if err := binary.Read(r, binary.BigEndian, &int32Val); err != nil {
return err
}
this.m = int(int32Val)
if err := binary.Read(r, binary.BigEndian, &int32Val); err != nil {
return err
}
this.mMax = int(int32Val)
if err := binary.Read(r, binary.BigEndian, &int32Val); err != nil {
return err
}
this.mMax0 = int(int32Val)
return nil
}