AstrLink Guard (QuantumNous/astrlink-guard, Apache-2.0, on Microsoft's MIT Multilingual-MiniLM-L12-H384) is a 36.9M-parameter BERT token classifier for Chinese and English: 21 BIO labels (email, phone, account, payment_card, ip_address, url, common_secret, private_address, private_date, private_person), 512-token windows overlapping by 128, a 0.8 score floor. Its int8 ONNX is 37 MB. This package runs it with no new dependency and nothing native: an ONNX protobuf reader for the graph and its external data (onnx.go), the XLM-R Unigram tokenizer from tokenizer.json (tokenizer.go), and the encoder with int8 x int8 matmuls (bert.go) on NEON/SDOT kernels on arm64, SSE2 on amd64 and Go elsewhere (kernel*). Word embeddings stay quantized (15 MB in memory, not 61 MB). uax29 moves from an indirect to a direct requirement for the tokenizer's pre-split. Checked against onnxruntime on the publisher's files: the same spans on 69 of 71 eval texts, 232 of 234 real code segments and 24 of 25 positive samples; the rest differ by a span edge at the score floor. One 512-token window takes ~0.5s CPU on arm64 and ~1.2s under Rosetta on amd64, on a box at load ~100. Run: go test ./internal/pii/guard (kernels against the Go reference, tokenizer against the publisher's tokenizer on testdata/tokens.json), gofmt -l, go vet, GOOS=linux go vet -tags nogui and GOOS=windows go vet on amd64 and arm64, at the pushed head.
98 lines
2.3 KiB
Go
98 lines
2.3 KiB
Go
package guard
|
|
|
|
import (
|
|
"math"
|
|
"math/rand"
|
|
"testing"
|
|
)
|
|
|
|
// The kernels in kernel_*.s give what the Go beside them gives: int8
|
|
// exactly, float32 to rounding. Each case is run with the kernel and with
|
|
// it switched off.
|
|
func TestKernelsMatchGo(t *testing.T) {
|
|
if dot4fast == nil || scoresFast == nil || mixFast == nil {
|
|
t.Log("no kernels on this CPU; the Go runs both sides")
|
|
}
|
|
r := rand.New(rand.NewSource(1))
|
|
for _, k := range []int{16, 32, 384, 1536, 40} {
|
|
x := make([]int8, k)
|
|
w := make([]int8, 4*k)
|
|
for i := range x {
|
|
x[i] = int8(r.Intn(256) - 128)
|
|
}
|
|
for i := range w {
|
|
w[i] = int8(r.Intn(256) - 128)
|
|
}
|
|
x[0], w[0] = -128, -128 // the extremes
|
|
var got, want [4]int32
|
|
dot4(x, w, &got)
|
|
f := dot4fast
|
|
dot4fast = nil
|
|
dot4(x, w, &want)
|
|
dot4fast = f
|
|
if got != want {
|
|
t.Fatalf("dot4 over %d: %v, Go %v", k, got, want)
|
|
}
|
|
}
|
|
for _, n := range []int{1, 7, 16, 37, 512} {
|
|
np := (n + 15) &^ 15
|
|
var q [32]float32
|
|
for i := range q {
|
|
q[i] = r.Float32()*2 - 1
|
|
}
|
|
kt := make([]float32, 32*np)
|
|
for i := range kt {
|
|
kt[i] = r.Float32()*2 - 1
|
|
}
|
|
got, want := make([]float32, np), make([]float32, np)
|
|
scores(&q, kt, got)
|
|
f := scoresFast
|
|
scoresFast = nil
|
|
scores(&q, kt, want)
|
|
scoresFast = f
|
|
for u := range got {
|
|
if math.Abs(float64(got[u]-want[u])) > 1e-4 {
|
|
t.Fatalf("scores over %d keys, [%d]: %v, Go %v", n, u, got[u], want[u])
|
|
}
|
|
}
|
|
p := make([]float32, n)
|
|
for i := range p {
|
|
p[i] = r.Float32()
|
|
}
|
|
v := make([]float32, 32*n)
|
|
for i := range v {
|
|
v[i] = r.Float32()*2 - 1
|
|
}
|
|
var c1, c2 [32]float32
|
|
c1[31], c2[31] = 1, 1 // added to, not overwritten
|
|
mix(&c1, p, v)
|
|
m := mixFast
|
|
mixFast = nil
|
|
mix(&c2, p, v)
|
|
mixFast = m
|
|
for i := range c1 {
|
|
if math.Abs(float64(c1[i]-c2[i])) > 1e-3 {
|
|
t.Fatalf("mix over %d, [%d]: %v, Go %v", n, i, c1[i], c2[i])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestExpfAndGelu(t *testing.T) {
|
|
for x := float32(-87); x <= 0; x += 0.0013 {
|
|
w := math.Exp(float64(x))
|
|
if e := math.Abs(float64(expf(x))-w) / w; e > 3e-7 {
|
|
t.Fatalf("expf(%v) = %v, want %v", x, expf(x), w)
|
|
}
|
|
}
|
|
if expf(-100) != 0 {
|
|
t.Fatal("expf(-100) not 0")
|
|
}
|
|
for x := float32(-10); x <= 10; x += 0.0007 {
|
|
w := 0.5 * float64(x) * (1 + math.Erf(float64(x)/math.Sqrt2))
|
|
if e := math.Abs(float64(gelu(x)) - w); e > 2e-6 {
|
|
t.Fatalf("gelu(%v) = %v, want %v", x, gelu(x), w)
|
|
}
|
|
}
|
|
}
|