A memory search now finds what means the same in other words or another
language ("什么时候上线" finds "Releases go out on Tuesdays"), for the
MCP server's memory_search and the Library's Memory tab alike. It is off
until switched on under Memory → Search by meaning, since it downloads
paraphrase-multilingual-MiniLM-L12-v2 (Xenova's int8 ONNX, ~135 MB,
pinned revision, size and sha256 checked); switched off, the model is
deleted.
- internal/pii/guard: the BERT code runs the embedder too (LoadEmbedder,
mean pooling; the tokenizer reads 250,002 pieces and WhitespaceSplit).
Its vectors match ONNX Runtime's int8 to cosine >= 0.995 and are no
further from fp32 than ORT's own; testdata carries both.
- internal/memembed: the switch, the download, vectors.bin keyed by each
memory's sha256 (read once, again when the text changes, a cut-short
file read as none), Keep reading in the backend, the model let go after
10 minutes unused.
- internal/memory: memory.Semantic fuses the meaning ranking with the word
ranking by reciprocal rank; a tie goes to the nearer in meaning, since
a first word hit may hold one common word ("when do we release" had
"Never delete images when..." first). Near 0.30 / Within 0.15 come from
97 real memories and 16 queries, half Chinese for English memories:
12 of 16 sought found with ~4 others each. Rank searches without noting
memories used.
- GUI: the Search by meaning row (download %, memories read, failures),
and the tab's search lists what the backend ranked first
(POST /api/memory/rank), then word matches. zh, zh-TW, ja, de.
- docs/subsystems/memory.md: the section, out of "Not built yet".
Why not multilingual-e5-small, which this was built on first: run for
real, its cosines sat at 0.78-0.89 for everything, and a Chinese search
ranked Chinese memories over the English one it meant. e5-small, e5-base
and MiniLM were compared in ONNX Runtime on the same 97 memories; MiniLM
put the sought one first 9 of 16 and in the top 3 12 of 16, with the
rest at a median cosine of 0.16.
Run before the rebases (mote, on origin/main as of the work):
- gofmt -l internal/ clean; go vet of memembed, memory, pii/..., gui,
library; GOOS=linux and GOOS=windows vet of memembed and pii/guard.
- MAGPIE_EMBED_DIR=~/models/pml go test -run Embedder ./internal/pii/guard:
7 texts (CJK, full width, emoji, digits before runs of spaces) at
0.9962-1.0000 with ORT int8.
- go test memembed, memory, memdistill, pii/..., library, browsermcp.
- Linux (magpie-desktop, -tags nogui, mote's disk was full): go test
memembed, memory, pii/guard, gui (461s), cli (1228s, past the default
10m timeout, so -timeout 30m); gateway -run 'MemoryMCP|OwnMCP'.
- Rebased on origin/main 3ebb1ba2c, on mote at this tree: gofmt -l
internal/ clean; go vet of memembed, memory, pii/..., library, gui,
gateway, and GOOS=linux and GOOS=windows vet of the first four; go
test memembed, memory, pii/..., library, browsermcp (Embedder test on
~/models/pml); gateway -run 'MemoryMCP|OwnMCP'; cli -run
'Settings|Parity|Budget'.
- Rebased again on origin/main 5d6935b11 (title bar, cli parity,
account export, provider catalog): go vet of gui and cli; go test -run
'Settings|Parity|Budget' of cli and gui, ok with the Bark test fix two
commits up (TestSettingsSaveKeepsWhatItDoesNotSend, red on main).
- GUI on mote, this commit plus the next (90f4a377b, which fixes
library-bar-more for #5982): every library-*.test.cjs, i18n-keys and
i18n-en-no-cjk, BROWSER=chromium and webkit, 701/702 each. The red is
library-loading's tab list, which has no "Claude plugins" (6af54dc9a)
and is red alone at origin/main faffb1e95 (#5997). library-bili's
webkit saved count times out about 1 run in 3, on main too (#6015).
- A real switch-on: the pinned files downloaded from Hugging Face and
checked in 11s, 5 memories read; 什么时候上线, 端口被占用怎么办, 清理磁盘,
回复用什么语言 and "when do we release" each find their memory first.
缩进用什么 finds nothing (0.23 to the tabs memory, under Near).
- Broken by hand: without the tie rule, TestSearchFindsByMeaning's new
case is red (images first).
Not run: the whole Go suite and the whole GUI suite; go test of
internal/gui and the whole of internal/cli after the rebases (WSL kept
restarting, and they passed on Linux before).
304 lines
8.7 KiB
Go
304 lines
8.7 KiB
Go
// Package guard runs AstrLink Guard (QuantumNous/astrlink-guard,
|
||
// Apache-2.0), a token classifier for secrets and personal data in Chinese
|
||
// and English text: its int8 ONNX model (37 MB) read by onnx.go and run
|
||
// by bert.go on the CPU in Go, or on a Mac by CoreML (coreml.go),
|
||
// its tokenizer by tokenizer.go. Nothing is started.
|
||
package guard
|
||
|
||
import (
|
||
"errors"
|
||
"fmt"
|
||
"math"
|
||
"path/filepath"
|
||
"runtime"
|
||
"sync"
|
||
"sync/atomic"
|
||
|
||
"github.com/yetone/magpie/internal/pii/guard/coreml"
|
||
)
|
||
|
||
// labels are the model's BIO tags, by output index (its labels.json).
|
||
var labels = []string{"O",
|
||
"B-email", "I-email", "B-phone", "I-phone", "B-account", "I-account",
|
||
"B-payment_card", "I-payment_card", "B-ip_address", "I-ip_address",
|
||
"B-url", "I-url", "B-common_secret", "I-common_secret",
|
||
"B-private_address", "I-private_address", "B-private_date", "I-private_date",
|
||
"B-private_person", "I-private_person"}
|
||
|
||
// Files are the model's files Load reads, in its folder.
|
||
var Files = []string{"model_int8.onnx", "model_int8.onnx_data", "tokenizer.json"}
|
||
|
||
// Model is the model, loaded.
|
||
type Model struct {
|
||
tok *tokenizer
|
||
net *net
|
||
ml *coreml.Model // nil where CoreML isn't, or couldn't be made
|
||
// MLErr is why CoreML isn't used, on a Mac.
|
||
MLErr error
|
||
// mlFail is why CoreML's last read failed and bert.go read the window
|
||
// instead; nil once one doesn't.
|
||
mlFail atomic.Pointer[string]
|
||
}
|
||
|
||
// Backend is what reads the model now, as Settings › Privacy names it:
|
||
// via "CoreML" on the device its compute plan puts most of the program
|
||
// on ("Neural Engine", "GPU" or "CPU"; "" where the plan can't be read),
|
||
// or bert.go on the CPU (via ""), and then on a Mac why not CoreML: it
|
||
// couldn't be made, or its last read failed. Settings said "on the CPU"
|
||
// while CoreML read every window (yyhhyyyyyy on Discord, 10-27).
|
||
func (m *Model) Backend() (via, device string, err error) {
|
||
if m.ml == nil {
|
||
if !coreml.Available {
|
||
return "", "CPU", nil
|
||
}
|
||
return "", "CPU", m.MLErr
|
||
}
|
||
if e := m.mlFail.Load(); e != nil {
|
||
return "", "CPU", errors.New(*e)
|
||
}
|
||
return "CoreML", m.ml.Device, nil
|
||
}
|
||
|
||
// Span is what the model found: text[Start:End], a Label of its (email,
|
||
// phone, account, payment_card, ip_address, url, common_secret,
|
||
// private_address, private_date, private_person), and the least of its
|
||
// tokens' probabilities.
|
||
type Span struct {
|
||
Start, End int
|
||
Label string
|
||
Score float64
|
||
}
|
||
|
||
// MinScore is the publisher's default threshold, under which masking
|
||
// drops a span unless the user set another (internal/masking).
|
||
const MinScore = 0.8
|
||
|
||
// Floor is the probability under which Find drops a span: the lowest
|
||
// threshold a user can set.
|
||
const Floor = 0.5
|
||
|
||
// window is how many tokens the model reads at once, besides <s> and
|
||
// </s>; a longer text is read in windows overlapping by overlap, each
|
||
// token labelled by the window it is furthest inside.
|
||
const (
|
||
window = maxPos - 2
|
||
overlap = 128
|
||
)
|
||
|
||
// Load reads the model in dir.
|
||
func Load(dir string) (*Model, error) {
|
||
tok, err := loadTokenizer(filepath.Join(dir, "tokenizer.json"), vocabSize)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("tokenizer: %w", err)
|
||
}
|
||
nt, err := loadNet(filepath.Join(dir, "model_int8.onnx"))
|
||
if err != nil {
|
||
return nil, fmt.Errorf("model: %w", err)
|
||
}
|
||
if nt.labels != len(labels) {
|
||
return nil, fmt.Errorf("model: %d labels, not %d", nt.labels, len(labels))
|
||
}
|
||
m := &Model{tok: tok, net: nt}
|
||
m.ml, m.MLErr = openCoreML(dir, nt)
|
||
return m, nil
|
||
}
|
||
|
||
// logits are the network's scores for ids, from CoreML when it is there
|
||
// and the window is long enough to gain by it, else over procs goroutines
|
||
// (0: as many of cores as are free, holding them while it reads).
|
||
func (m *Model) logits(ids []int, procs int) []float32 {
|
||
if m.ml != nil && len(ids) >= coremlMin {
|
||
lg, err := coremlRead(m.ml, m.net, ids)
|
||
if err == nil {
|
||
m.mlFail.Store(nil)
|
||
return lg
|
||
}
|
||
e := err.Error()
|
||
m.mlFail.Store(&e)
|
||
}
|
||
if procs == 0 { // CoreML's reader: a core of cores, and all that are free
|
||
procs = take(Cores())
|
||
defer give(procs)
|
||
}
|
||
return m.net.logits(ids, procs)
|
||
}
|
||
|
||
// Cores is how many cores the model reads on at most: half of them, and
|
||
// never more than 4. It reads beside the agents and everything else on
|
||
// the computer, and on all of them a long history unread kept every core
|
||
// busy for as long as requests came (xinvxueyuan and shensmart on Discord,
|
||
// 10-28: a 32-core Linux box spent 45 CPU seconds on each request).
|
||
func Cores() int { return max(1, min(4, runtime.GOMAXPROCS(0)/2)) }
|
||
|
||
// cores are the Cores() the model reads on, shared by every Find at once:
|
||
// a window read on the CPU holds one or more, so requests read side by
|
||
// side never take more than Cores() between them.
|
||
var cores = make(chan struct{}, Cores())
|
||
|
||
// take holds a core of cores, waiting for one, and up to want-1 more of
|
||
// those free now: how many it holds, to give back.
|
||
func take(want int) int {
|
||
cores <- struct{}{}
|
||
n := 1
|
||
for n < want {
|
||
select {
|
||
case cores <- struct{}{}:
|
||
n++
|
||
default:
|
||
return n
|
||
}
|
||
}
|
||
return n
|
||
}
|
||
|
||
func give(n int) {
|
||
for range n {
|
||
<-cores
|
||
}
|
||
}
|
||
|
||
// Find is what the model finds in text, at Floor, each span with its score.
|
||
// A text longer than a window is read a window at a time, overlapping.
|
||
// On the CPU those windows are read at once, one a core (of Cores()): a
|
||
// window's own matmuls split over the cores leave most of each idle in
|
||
// the steps between them (a 4-core server read a long prompt for minutes).
|
||
// Finds called at once share those cores (cores): a window takes one,
|
||
// waiting while others have them all, and splits its matmuls over as many
|
||
// more as are free, up to its share of them, so a lone short text is read
|
||
// on every core and several texts at once a core each.
|
||
func (m *Model) Find(text string) []Span {
|
||
toks := m.tok.encode(text)
|
||
n := len(toks)
|
||
if n == 0 {
|
||
return nil
|
||
}
|
||
var starts []int
|
||
for start := 0; ; start = start + window - overlap {
|
||
starts = append(starts, start)
|
||
if start+window >= n {
|
||
break
|
||
}
|
||
}
|
||
lgs := make([][]float32, len(starts))
|
||
ids := func(w int) []int {
|
||
start := starts[w]
|
||
end := min(start+window, n)
|
||
ids := make([]int, 0, end-start+2)
|
||
ids = append(ids, 0)
|
||
for _, t := range toks[start:end] {
|
||
ids = append(ids, t.id)
|
||
}
|
||
return append(ids, 2)
|
||
}
|
||
if m.ml != nil {
|
||
for w := range starts {
|
||
lgs[w] = m.logits(ids(w), 0)
|
||
}
|
||
} else {
|
||
at := min(Cores(), len(starts))
|
||
var next atomic.Int32
|
||
var wg sync.WaitGroup
|
||
var c carried
|
||
for range at {
|
||
wg.Go(func() {
|
||
defer c.catch()
|
||
for w := int(next.Add(1)) - 1; w < len(starts); w = int(next.Add(1)) - 1 {
|
||
func() {
|
||
procs := take(max(1, Cores()/at))
|
||
defer give(procs)
|
||
lgs[w] = m.net.logits(ids(w), procs)
|
||
}()
|
||
}
|
||
})
|
||
}
|
||
wg.Wait()
|
||
c.rethrow()
|
||
}
|
||
lab := make([]int, n)
|
||
prob := make([]float64, n)
|
||
margin := make([]int, n)
|
||
for i := range margin {
|
||
margin[i] = -1
|
||
}
|
||
k := len(labels)
|
||
for w, start := range starts {
|
||
end := min(start+window, n)
|
||
lg := lgs[w]
|
||
for i := 0; i < end-start; i++ {
|
||
j := start + i
|
||
mg := min(i, end-start-1-i)
|
||
if mg <= margin[j] {
|
||
continue
|
||
}
|
||
row := lg[(i+1)*k : (i+2)*k]
|
||
best, mx := 0, row[0]
|
||
for c, v := range row {
|
||
if v > mx {
|
||
best, mx = c, v
|
||
}
|
||
}
|
||
var sum float64
|
||
for _, v := range row {
|
||
sum += math.Exp(float64(v - mx))
|
||
}
|
||
margin[j], lab[j], prob[j] = mg, best, 1/sum
|
||
}
|
||
}
|
||
return decode(toks, lab, prob)
|
||
}
|
||
|
||
// carried is the first panic in the goroutines a read starts, panicked
|
||
// again by rethrow on the goroutine that waited for them, where Find's
|
||
// caller can recover it: a panic left on a goroutine of its own ends the
|
||
// whole process (#4553 ended magpie on Windows).
|
||
type carried struct{ p atomic.Pointer[any] }
|
||
|
||
// catch is deferred by each goroutine.
|
||
func (c *carried) catch() {
|
||
if r := recover(); r != nil {
|
||
c.p.CompareAndSwap(nil, &r)
|
||
}
|
||
}
|
||
|
||
// rethrow is called once they have all ended.
|
||
func (c *carried) rethrow() {
|
||
if p := c.p.Load(); p != nil {
|
||
panic(*p)
|
||
}
|
||
}
|
||
|
||
// decode reads the tokens' BIO tags (lab, by index into labels) into
|
||
// spans: a B tag, or an I tag of another kind than the span before it,
|
||
// starts one; an I tag of the same kind extends it; O ends it. A span's
|
||
// score is its least token's, and those under Floor are dropped.
|
||
// Tokens of no text (a lone "▁") neither start nor end a span.
|
||
func decode(toks []token, lab []int, prob []float64) []Span {
|
||
var spans []Span
|
||
cur := -1
|
||
for j, t := range toks {
|
||
if t.start == t.end {
|
||
continue
|
||
}
|
||
tag := labels[lab[j]]
|
||
if tag == "O" {
|
||
cur = -1
|
||
continue
|
||
}
|
||
kind := tag[2:]
|
||
if tag[0] == 'B' || cur < 0 || spans[cur].Label != kind {
|
||
spans = append(spans, Span{t.start, t.end, kind, prob[j]})
|
||
cur = len(spans) - 1
|
||
continue
|
||
}
|
||
spans[cur].End = t.end
|
||
spans[cur].Score = min(spans[cur].Score, prob[j])
|
||
}
|
||
out := spans[:0]
|
||
for _, s := range spans {
|
||
if s.Score >= Floor {
|
||
out = append(out, s)
|
||
}
|
||
}
|
||
return out
|
||
}
|