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
|
//go:build !chunkdemo
package main
import (
"fmt"
"math"
"os"
"time"
)
// main is omitted under -tags chunkdemo, where chunkdemo.go provides its own.
// Everything below this point (the embedder, the downloader) is shared by both.
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "fedisearch-spike: %v\n", err)
os.Exit(1)
}
}
func run() error {
modelDir, err := ensureModel()
if err != nil {
return fmt.Errorf("obtaining model: %w", err)
}
emb, err := newEmbedder(modelDir)
if err != nil {
return fmt.Errorf("loading model: %w", err)
}
defer emb.close()
fmt.Printf("model inputs: %v\n", emb.inputNames)
fmt.Printf("model outputs: %v\n", emb.outputNames)
const text = "robert smith"
start := time.Now()
vec, err := emb.embed(text)
if err != nil {
return fmt.Errorf("embedding %q: %w", text, err)
}
elapsed := time.Since(start)
fmt.Printf("\nembedding of %q: %d dims in %s\n", text, len(vec), elapsed.Round(time.Millisecond))
fmt.Printf("first 8: %v\n", vec[:8])
fmt.Printf("L2 norm: %.5f\n", l2Norm(vec))
if err := checkAgainstReference(vec); err != nil {
return err
}
fmt.Printf("\nOK: matches reference vector within %g\n", tolerance)
return nil
}
// checkAgainstReference compares the computed embedding against the known-good
// vector, reporting the worst offender rather than just the first mismatch, so
// a systematic error is easier to recognise than a one-off.
func checkAgainstReference(got []float32) error {
if len(got) != 384 {
return fmt.Errorf("expected 384 dimensions, got %d", len(got))
}
worstIdx, worstDiff := -1, 0.0
for i, want := range expectedRobertSmith {
diff := math.Abs(float64(got[i] - want))
if diff > worstDiff {
worstIdx, worstDiff = i, diff
}
}
if worstDiff > tolerance {
return fmt.Errorf(
"embedding does not match reference: worst mismatch at dim %d: got %v, want %v (diff %g > %g)",
worstIdx, got[worstIdx], expectedRobertSmith[worstIdx], worstDiff, tolerance)
}
return nil
}
func l2Norm(v []float32) float64 {
var sum float64
for _, x := range v {
sum += float64(x) * float64(x)
}
return math.Sqrt(sum)
}
|