-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.go
More file actions
348 lines (283 loc) · 7.6 KB
/
utils.go
File metadata and controls
348 lines (283 loc) · 7.6 KB
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
package antarys
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"math"
"math/rand"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
onnxruntime "github.com/yalue/onnxruntime_go"
"golang.org/x/term"
)
const VERSION string = "0.2.0"
const ANTARYS_TITLE = `
__
.---.-.-----.| |_.---.-.----.--.--.-----.
| _ | || _| _ | _| | |__ --|
|___._|__|__||____|___._|__| |___ |_____|
|_____|
`
func AntarysTitle() {
width, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
width = 80
}
lines := strings.Split(ANTARYS_TITLE, "\n")
for _, line := range lines {
if len(line) > width {
line = line[:width]
}
fmt.Printf("\033[32m%s\033[0m\n", line)
}
}
func euclideanDistanceSquared(a, b []float32) float32 {
if len(a) != len(b) {
return math.MaxFloat32
}
var sumSq float64
length := len(a)
i := 0
for ; i < length-7; i += 8 {
d0 := float64(a[i]) - float64(b[i])
d1 := float64(a[i+1]) - float64(b[i+1])
d2 := float64(a[i+2]) - float64(b[i+2])
d3 := float64(a[i+3]) - float64(b[i+3])
d4 := float64(a[i+4]) - float64(b[i+4])
d5 := float64(a[i+5]) - float64(b[i+5])
d6 := float64(a[i+6]) - float64(b[i+6])
d7 := float64(a[i+7]) - float64(b[i+7])
sumSq += d0*d0 + d1*d1 + d2*d2 + d3*d3 + d4*d4 + d5*d5 + d6*d6 + d7*d7
}
for ; i < length; i++ {
diff := float64(a[i]) - float64(b[i])
sumSq += diff * diff
}
return float32(sumSq)
}
func kMeansPlusPlusInit(vectors [][]float32, k int) [][]float32 {
n := len(vectors)
dims := len(vectors[0])
centroids := make([][]float32, k)
for i := range centroids {
centroids[i] = make([]float32, dims)
}
firstIdx := rand.Intn(n)
copy(centroids[0], vectors[firstIdx])
for c := 1; c < k; c++ {
distances := make([]float32, n)
sumDistances := float32(0)
for i, vec := range vectors {
minDist := float32(math.MaxFloat32)
for j := 0; j < c; j++ {
dist := euclideanDistanceSquared(vec, centroids[j])
if dist < minDist {
minDist = dist
}
}
distances[i] = minDist
sumDistances += minDist
}
target := rand.Float32() * sumDistances
cumSum := float32(0)
for i, dist := range distances {
cumSum += dist
if cumSum >= target {
copy(centroids[c], vectors[i])
break
}
}
}
return centroids
}
func flattenCentroids(centroids [][]float32) []float32 {
k := len(centroids)
if k == 0 {
return []float32{}
}
dims := len(centroids[0])
flat := make([]float32, k*dims)
for i, centroid := range centroids {
copy(flat[i*dims:], centroid)
}
return flat
}
func getCandidateSlice(capacity int) []queueItem {
if capacity <= 512 {
return candidatePool.Get().([]queueItem)
}
return make([]queueItem, 0, capacity)
}
func putCandidateSlice(s []queueItem) {
if cap(s) <= 512 {
candidatePool.Put(s[:0])
}
}
func sortResultsFast(results []SearchResult) {
if len(results) < 2 {
return
}
if len(results) < 20 {
for i := 1; i < len(results); i++ {
for j := i; j > 0 && results[j].Score > results[j-1].Score; j-- {
results[j], results[j-1] = results[j-1], results[j]
}
}
return
}
sort.Slice(results, func(i, j int) bool {
return results[i].Score > results[j].Score
})
}
func sortCandidates(candidates []queueItem) {
if len(candidates) < 2 {
return
}
if len(candidates) < 20 {
for i := 1; i < len(candidates); i++ {
for j := i; j > 0 && candidates[j].similarity > candidates[j-1].similarity; j-- {
candidates[j], candidates[j-1] = candidates[j-1], candidates[j]
}
}
return
}
sort.Slice(candidates, func(i, j int) bool {
return candidates[i].similarity > candidates[j].similarity
})
}
var ValidChecksumFiles map[string]string = map[string]string{
"libonnxruntime.1.22.0.dylib": "db045368293215c9d22aa7b8c983d688b3ae9ca1da3f64ffbe01ba7df31c3355",
"libonnxruntime_arm64.so": "0afd69a0ae38c5099fd0e8604dda398ac43dee67cd9c6394b5142b19e82528de",
"libonnxruntime_x64.so": "3da6146e14e7b8aaec625dde11d6114c7457c87a5f93d744897da8781e35c673",
}
var ModelsByIndex = map[int]string{
1: "fast-bge-base-en",
2: "fast-bge-base-en-v1.5",
3: "fast-bge-small-en",
4: "fast-bge-small-en-v1.5",
5: "fast-bge-small-zh-v1.5",
}
func LoadDynamicOnnx() (string, error) {
switch runtime.GOOS {
case "darwin":
return "libonnxruntime.1.22.0.dylib", nil
case "linux":
switch runtime.GOARCH {
case "arm64":
return "libonnxruntime_arm64.so", nil
case "amd64":
return "libonnxruntime_x64.so", nil
default:
return "", fmt.Errorf("unsupported Linux architecture: %s. Embedding is only supported on ARM64 and x64", runtime.GOARCH)
}
case "windows":
return "", fmt.Errorf("embedding is not supported on Windows")
default:
return "", fmt.Errorf("unsupported operating system: %s. Embedding is only supported on macOS and Linux", runtime.GOOS)
}
}
func ValidateChecksum(path string, expected string) bool {
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close()
hash := sha256.New()
if _, err := io.Copy(hash, file); err != nil {
panic(err)
}
actual := hex.EncodeToString(hash.Sum(nil))
return actual == expected
}
func IsOnnxValid(cacheDir string) bool {
for filename, expectedChecksum := range ValidChecksumFiles {
if filepath.Ext(filename) == ".gz" {
continue
}
filePath := filepath.Join(cacheDir, filename)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
continue
}
if !ValidateChecksum(filePath, expectedChecksum) {
fmt.Printf("Checksum validation failed for %s\n", filename)
return false
}
}
return true
}
func IsModelValid(id string, cacheDir string) bool {
modelDirs := map[string]string{
"1": "fast-bge-base-en",
"2": "fast-bge-base-en-v1.5",
"3": "fast-bge-small-en",
"4": "fast-bge-small-en-v1.5",
"5": "fast-bge-small-zh-v1.5",
}
modelDir, exists := modelDirs[id]
if !exists {
fmt.Printf("Unknown model ID: %s\n", id)
return false
}
modelPath := filepath.Join(cacheDir, modelDir)
if _, err := os.Stat(modelPath); os.IsNotExist(err) {
fmt.Printf("Model directory does not exist: %s\n", modelPath)
return false
}
requiredFiles := []string{
"config.json",
"model_optimized.onnx",
"ort_config.json",
"special_tokens_map.json",
"tokenizer_config.json",
"tokenizer.json",
"vocab.txt",
}
for _, filename := range requiredFiles {
filePath := filepath.Join(modelPath, filename)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
fmt.Printf("Required file missing: %s\n", filePath)
return false
}
}
onnxModelPath := filepath.Join(modelPath, "model_optimized.onnx")
if !onnxruntime.IsInitialized() {
runtimeLib := filepath.Join(cacheDir, "libonnxruntime.1.22.0.dylib")
onnxruntime.SetSharedLibraryPath(runtimeLib)
err := onnxruntime.InitializeEnvironment()
if err != nil {
fmt.Printf("Failed to initialize ONNX runtime: %v\n", err)
return false
}
defer func() {
if err := onnxruntime.DestroyEnvironment(); err != nil {
fmt.Printf("Warning: Failed to cleanup ONNX environment: %v\n", err)
}
}()
}
_, _, err := onnxruntime.GetInputOutputInfo(onnxModelPath)
if err != nil {
fmt.Printf("Failed to load ONNX model %s: %v\n", onnxModelPath, err)
return false
}
metadata, err := onnxruntime.GetModelMetadata(onnxModelPath)
if err != nil {
fmt.Printf("Failed to get model metadata for %s: %v\n", onnxModelPath, err)
return false
}
defer metadata.Destroy()
fmt.Printf("Model %s validation successful\n", id)
return true
}
func ValidateOnnxAndModel(modelID string, cacheDir string) error {
if !IsOnnxValid(cacheDir) {
return fmt.Errorf("ONNX runtime validation failed")
}
if !IsModelValid(modelID, cacheDir) {
return fmt.Errorf("model %s validation failed", modelID)
}
return nil
}