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.
34 lines
694 B
Go
34 lines
694 B
Go
package guard
|
|
|
|
import "golang.org/x/sys/cpu"
|
|
|
|
//go:noescape
|
|
func dot4sdot(a, b *int8, k int, out *[4]int32)
|
|
|
|
func init() {
|
|
if cpu.ARM64.HasASIMDDP {
|
|
dot4fast = func(a, b []int8, out *[4]int32) {
|
|
_ = b[4*len(a)-1]
|
|
dot4sdot(&a[0], &b[0], len(a), out)
|
|
}
|
|
}
|
|
}
|
|
|
|
//go:noescape
|
|
func scoresT(q, kt *float32, np int, out *float32)
|
|
|
|
//go:noescape
|
|
func mix32(c, p, v *float32, n int)
|
|
|
|
func init() {
|
|
// NEON is part of every arm64 Go supports.
|
|
scoresFast = func(q *[32]float32, kt []float32, out []float32) {
|
|
_ = kt[32*len(out)-1]
|
|
scoresT(&q[0], &kt[0], len(out), &out[0])
|
|
}
|
|
mixFast = func(c *[32]float32, p, v []float32) {
|
|
_ = v[32*len(p)-1]
|
|
mix32(&c[0], &p[0], &v[0], len(p))
|
|
}
|
|
}
|