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.
125 lines
3.0 KiB
Go
125 lines
3.0 KiB
Go
package guard
|
|
|
|
import "math"
|
|
|
|
// dot4fast, where the CPU has the instructions for it (kernel_*.go), sets
|
|
// out[j] to the dot product of a with b[j*len(a):(j+1)*len(a)], for
|
|
// len(a) a multiple of 32.
|
|
var dot4fast func(a, b []int8, out *[4]int32)
|
|
|
|
// dot4 is dot4fast, or the same in Go.
|
|
func dot4(a, b []int8, out *[4]int32) {
|
|
k := len(a)
|
|
if dot4fast != nil && k%32 == 0 && k > 0 {
|
|
dot4fast(a, b, out)
|
|
return
|
|
}
|
|
for j := range out {
|
|
out[j] = dot(a, b[j*k:j*k+k])
|
|
}
|
|
}
|
|
|
|
func dot(a, b []int8) int32 {
|
|
b = b[:len(a)]
|
|
var s0, s1, s2, s3 int32
|
|
i := 0
|
|
for ; i+4 <= len(a); i += 4 {
|
|
s0 += int32(a[i]) * int32(b[i])
|
|
s1 += int32(a[i+1]) * int32(b[i+1])
|
|
s2 += int32(a[i+2]) * int32(b[i+2])
|
|
s3 += int32(a[i+3]) * int32(b[i+3])
|
|
}
|
|
for ; i < len(a); i++ {
|
|
s0 += int32(a[i]) * int32(b[i])
|
|
}
|
|
return s0 + s1 + s2 + s3
|
|
}
|
|
|
|
// dot32 is a·b for one attention head's rows.
|
|
func dot32(a, b *[32]float32) float32 {
|
|
var s0, s1, s2, s3 float32
|
|
for i := 0; i < 32; i += 4 {
|
|
s0 += a[i] * b[i]
|
|
s1 += a[i+1] * b[i+1]
|
|
s2 += a[i+2] * b[i+2]
|
|
s3 += a[i+3] * b[i+3]
|
|
}
|
|
return (s0 + s1) + (s2 + s3)
|
|
}
|
|
|
|
// axpy32 adds w·v to c.
|
|
func axpy32(c *[32]float32, w float32, v *[32]float32) {
|
|
for i := range c {
|
|
c[i] += w * v[i]
|
|
}
|
|
}
|
|
|
|
// scoresFast and mixFast, where the CPU has the instructions for them,
|
|
// do what scores and mix do.
|
|
var (
|
|
scoresFast func(q *[32]float32, kt []float32, out []float32)
|
|
mixFast func(c *[32]float32, p, v []float32)
|
|
)
|
|
|
|
// scores sets out[u] to Σ q[i]·kt[i*len(out)+u]: q against each key, with
|
|
// the keys transposed, len(out) of them (a multiple of 16).
|
|
func scores(q *[32]float32, kt []float32, out []float32) {
|
|
if scoresFast != nil && len(out) > 0 {
|
|
scoresFast(q, kt, out)
|
|
return
|
|
}
|
|
np := len(out)
|
|
clear(out)
|
|
for i, w := range q {
|
|
row := kt[i*np : i*np+np]
|
|
for u, x := range row {
|
|
out[u] += w * x
|
|
}
|
|
}
|
|
}
|
|
|
|
// mix adds Σ p[u]·v[u] to c, for v's rows of 32.
|
|
func mix(c *[32]float32, p, v []float32) {
|
|
if mixFast != nil && len(p) > 0 {
|
|
mixFast(c, p, v)
|
|
return
|
|
}
|
|
for u, w := range p {
|
|
axpy32(c, w, (*[32]float32)(v[u*32:]))
|
|
}
|
|
}
|
|
|
|
// expf is e^x in float32, to about an ulp (Cephes' expf), for the
|
|
// softmax's x ≤ 0 and the GELU's erf.
|
|
func expf(x float32) float32 {
|
|
if x < -87.3 {
|
|
return 0
|
|
}
|
|
if x > 88.7 {
|
|
return float32(math.Inf(1))
|
|
}
|
|
n := float32(math.Floor(float64(x*1.44269504088896341 + 0.5)))
|
|
x -= n * 0.693359375
|
|
x -= n * -2.12194440e-4
|
|
z := x * x
|
|
y := ((((1.9875691500e-4*x+1.3981999507e-3)*x+8.3334519073e-3)*x+4.1665795894e-2)*x+1.6666665459e-1)*x + 5.0000001201e-1
|
|
y = y*z + x + 1
|
|
return math.Float32frombits(math.Float32bits(y) + uint32(int32(n))<<23)
|
|
}
|
|
|
|
// gelu is x·Φ(x), with erf from Abramowitz and Stegun 7.1.26 (absolute
|
|
// error under 1.5e-7, below float32's own at the values it meets).
|
|
func gelu(x float32) float32 {
|
|
z := x * 0.70710678118654752
|
|
a := z
|
|
if a < 0 {
|
|
a = -a
|
|
}
|
|
t := 1 / (1 + 0.3275911*a)
|
|
e := 1 - (((((1.061405429*t-1.453152027)*t)+1.421413741)*t-0.284496736)*t+0.254829592)*t*expf(-a*a)
|
|
if z < 0 {
|
|
e = -e
|
|
}
|
|
return 0.5 * x * (1 + e)
|
|
}
|