ac4af582301a4e8a29245dbba767eba31334ddf9
2
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
1c3591b5d1 |
guard: the local masking model reads text a third faster on amd64 (mrwl666)
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 (
|
||
|
|
6e9820a3a4 |
pii/guard: run AstrLink Guard's int8 model in Go (#3578)
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. |