mrwl666 (Discord #help, v0.1.2554, Docker on a 2-core EPYC) saw one
core at 100% for 2-3 s on every request with masking and AstrLink Guard
on, and took it for every message of a ~66k-token conversation being
scanned again. It isn't. Probed on the Linux VM (GOMAXPROCS=2, taskset
-c 0,1) with a 50-60 turn Chat session of ~4 KB tool results, ~240-270 KB
a body:
- rules masking (MaskJSON without the model): 5-11 ms a turn;
- the history is cached by hash (LRU + found-<rev>.bin), and a turn
hands the model only what it added. Bounded caps that at 4 KB;
- the request waits masking's 2 s defaultWait for the model on that
text, and guard.Cores() is 1 on a 2-core box, so one core runs the
int8 BERT at about 1 KB a second. That is the spike.
This change makes the model's CPU cheaper; it does not take the 2 s
wait away. AVX2 kernels for the int8 dot products (dot4, attention
scores), exp in softmax and the GELU epilogue, picked at start when
the CPU has AVX2, plus plain compares in place of the builtin float
min/max (a third of the non-matmul time, for their NaN/±0 rules).
Every float op is the same per lane and in the same order as the Go
code (release builds are GOAMD64=v1, no FMA), the int8 sums are exact,
mix32 and the softmax sum keep their order, so the model finds the
same spans with the same scores to the bit.
Numbers, Linux VM (AMD Ryzen AI MAX+ 395), one core of guard:
guard.Find on 4,247 bytes: 4.05 s before, 2.75 s after (same 20 spans)
turn 50-57 with the model: MaskJSON wall 2.0 s both (the wait), CPU
after the wait 0.1-3.0 s before, 0.0-2.0 s after
mote (M5 Pro, arm64, rules path, unchanged by this):
BenchmarkMaskLongSession chat 10.3-11.7 ms, responses 10.8-13.0 ms,
messages 12.1-13.9 ms per turn of a ~260 KB body
Tests:
- TestKernelsSameBits: the AVX2 exp/GELU/scores give Float32bits equal
to the Go code, edge values included. Broken by hand: swapping two
adds in the asm exp made it red (0x175156d6 vs 0x175156d5).
- TestMaxAndRangeAsBuiltins: maxOf/rangeOf agree with the builtins on
±0 and NaN. Red with maxOf's NaN check taken out.
- TestModelSameWithoutAVX2 (needs MAGPIE_GUARD_DIR): the real model
finds the same spans on ~6 KB of mixed text with AVX2 and with SSE2.
- TestSessionTurnReadsOnlyWhatItAdds (internal/pii): turn 61 of a
275 KB session, after turn 60, has the model read only turn 61's text
(5.2 KB for 4.6 KB added). It passes on the old code too: it guards
the claim above, not this speedup. Red (10.5 KB, earlier turns read
again) with the cache lookup in known() turned off by hand.
- BenchmarkMaskLongSession (internal/redact): Chat, Responses and
Messages sessions with large tool results, turn 61 after turn 60.
Run at this tree (7b6b7492a + this):
- mote: go vet ./internal/pii/... ./internal/redact, GOOS=linux amd64
(-tags nogui), GOOS=windows amd64, GOOS=linux arm64 guard: ok.
go test -count=1 -timeout 45m ./internal/pii/... ./internal/redact: ok.
- WSL magpie-desktop (amd64, AVX2) with MAGPIE_GUARD_DIR: the same
packages ok (guard 28.9 s, model tests included); the three kernel
tests again under GOMAXPROCS=2 taskset: ok.
- Windows xiaomi-gpu: the same packages ok (no model dir there).
Not tried: the reporter's EPYC itself or Docker; AVX-VNNI or AVX-512
(would need the EPYC generation; VPDPWSSD is exact, so it could come
next); mix32 on AVX2 (more accumulators change its sum order, so not
byte-identical); the full ./... suite. For no spike at all, the model
can be turned off with rules masking kept on.
204 lines
5.3 KiB
Go
204 lines
5.3 KiB
Go
package guard
|
|
|
|
import (
|
|
"fmt"
|
|
"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)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The softmax's exponentials, the GELU and the scores give the Go's very
|
|
// bits with the kernels (AVX2 on amd64), not only values near them: what
|
|
// the model finds, and so what is masked, is the same on every CPU.
|
|
func TestKernelsSameBits(t *testing.T) {
|
|
if expSubFast == nil || geluFast == nil {
|
|
t.Log("no exp or gelu kernel on this CPU; the Go runs both sides")
|
|
}
|
|
r := rand.New(rand.NewSource(2))
|
|
same := func(what string, got, want []float32) {
|
|
t.Helper()
|
|
for i := range got {
|
|
if math.Float32bits(got[i]) != math.Float32bits(want[i]) {
|
|
t.Fatalf("%s [%d]: %v (%#x), Go %v (%#x)", what, i, got[i], math.Float32bits(got[i]), want[i], math.Float32bits(want[i]))
|
|
}
|
|
}
|
|
}
|
|
edges := []float32{0, float32(math.Copysign(0, -1)), -87.2, -87.3, -87.4, -100, -1e30, 88.6, 88.7, 88.8, 1e-40, -1e-40, 1, -1}
|
|
for _, n := range []int{1, 8, 13, 512, 1536 + 5} {
|
|
x := make([]float32, n)
|
|
for i := range x {
|
|
switch {
|
|
case i < len(edges) && n > len(edges):
|
|
x[i] = edges[i]
|
|
case i%3 == 0:
|
|
x[i] = -r.Float32() * 90
|
|
default:
|
|
x[i] = (r.Float32()*2 - 1) * 12
|
|
}
|
|
}
|
|
mx := float32(r.Float32())
|
|
got, want := append([]float32(nil), x...), append([]float32(nil), x...)
|
|
expSub(got, mx)
|
|
f := expSubFast
|
|
expSubFast = nil
|
|
expSub(want, mx)
|
|
expSubFast = f
|
|
same(fmt.Sprintf("expSub over %d", n), got, want)
|
|
|
|
bias, smooth := make([]float32, n), make([]float32, n)
|
|
for i := range bias {
|
|
bias[i] = r.Float32()*0.2 - 0.1
|
|
smooth[i] = r.Float32() + 0.5
|
|
}
|
|
got, want = append([]float32(nil), x...), append([]float32(nil), x...)
|
|
geluRow(got, bias, smooth)
|
|
g := geluFast
|
|
geluFast = nil
|
|
geluRow(want, bias, smooth)
|
|
geluFast = g
|
|
same(fmt.Sprintf("geluRow over %d", n), got, want)
|
|
}
|
|
for _, n := range []int{1, 16, 17, 33, 48, 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
|
|
same(fmt.Sprintf("scores over %d keys", n), got, want)
|
|
}
|
|
}
|
|
|
|
// maxOf and rangeOf give what the builtins give, to a zero's sign: NaN
|
|
// for a NaN anywhere.
|
|
func TestMaxAndRangeAsBuiltins(t *testing.T) {
|
|
r := rand.New(rand.NewSource(3))
|
|
nan, neg0 := float32(math.NaN()), float32(math.Copysign(0, -1))
|
|
for c := 0; c < 2000; c++ {
|
|
x := make([]float32, r.Intn(40))
|
|
for i := range x {
|
|
switch r.Intn(12) {
|
|
case 0:
|
|
x[i] = 0
|
|
case 1:
|
|
x[i] = neg0
|
|
case 2:
|
|
if c%10 == 0 {
|
|
x[i] = nan
|
|
}
|
|
default:
|
|
x[i] = (r.Float32()*2 - 1) * 50
|
|
}
|
|
}
|
|
mx, lo, hi := float32(math.Inf(-1)), float32(0), float32(0)
|
|
for _, v := range x {
|
|
mx = max(mx, v)
|
|
lo, hi = min(lo, v), max(hi, v)
|
|
}
|
|
eq := func(a, b float32) bool { return a == b || a != a && b != b }
|
|
gl, gh := rangeOf(x)
|
|
if !eq(maxOf(x), mx) || !eq(gl, lo) || !eq(gh, hi) {
|
|
t.Fatalf("%v: maxOf %v rangeOf %v %v, builtins %v %v %v", x, maxOf(x), gl, gh, mx, lo, hi)
|
|
}
|
|
}
|
|
}
|