Files
magpie/internal/pii/guard/coreml.mlmodel
T
yetone f0ffee34ce guard: on a Mac AstrLink Guard runs through CoreML, 512 tokens in 12ms where the CPU takes 400
yyhhyyyyyy on X asked for CoreML. internal/pii/guard now runs the
encoder (everything after the embeddings) as a CoreML program on a Mac
with macOS 13 or later, on the Neural Engine or GPU (MLComputeUnitsAll).
bert.go still does the embeddings and stays the path everywhere else.

- coreml_gen.py builds the program with coremltools' MIL builder from
  the pinned int8 model: the int8 weights dequantized, float16 compute.
  The repo keeps only the graph (coreml.mlmodel, 112KB) and coreml.json
  (each blob's offset, weight.bin's size and sha256). On each Mac the
  weights are written from the model already downloaded (coremlWeights,
  float16 rounded to nearest even as numpy does), checked against that
  sha256, compiled once into <model dir>/coreml/<sha>.mlmodelc (about 5s,
  41MB), and opened from there after that (40-170ms). Nothing new is
  downloaded or hosted.
- The cgo lives in internal/pii/guard/coreml, because guard has Go
  assembly. Without cgo or off darwin it is a stub (Available false).
- A window under 16 tokens (coremlMin) stays on the CPU, which is faster
  there (8 tokens: 8.7ms CPU, 12.5ms CoreML; 16: 17 vs 13). A Mac before
  macOS 13, a failed write, compile or open, or a failed run goes to the
  CPU too. Model.MLErr keeps why.

Accuracy against the publisher's fp32 ONNX (onnxruntime): CoreML's
probabilities are within .067, the CPU's within .129, and both agree
with fp32's labels on the same tokens. The CPU quantizes activations as
the int8 model does and CoreML doesn't; fake quantization in the program
was tried and didn't match the CPU (.75 on CPU fp32, .70 on GPU,
unstable in fp16), so it was dropped. A span near the threshold can fall
the other side: TestModelFinds' go.mod module path is a common_secret at
.814 on CoreML, .788 on the CPU and .785 in fp32, so on a Mac it is
masked at the default 0.8. TestModelFinds now tests bert.go alone
(m.ml = nil), and TestCoreMLMatchesGo holds CoreML to it.

Run (M4 Pro, macOS 26.2, under load ~50):
- gofmt -l internal/pii: clean. go vet ./internal/pii/... with GOOS
  darwin, linux, windows and CGO_ENABLED=0: clean.
- MAGPIE_GUARD_DIR=<pinned model> go test ./internal/pii/...
  ./internal/redact/... ./internal/masking/... -count=1: ok. In guard:
  TestHalfRounds, TestCoreMLWeights (weight.bin's sha256),
  TestCoreMLMatchesGo (99%+ argmax agreement on a 512-token window, the
  same spans as the CPU with scores within .2; loaded in 6.3s cold,
  17ms for 512 tokens), TestCoreMLFailureFallsBack and TestModelFinds.
- TestMaskEval/astrlink-guard replayed with outputs recorded through
  CoreML: the same verdicts at the default threshold; scores moved a
  little and one account_number span at .508 is new, under 0.8.
  eval-astrlink.json stays the CPU's recording.

Broken by hand: half() rounding ties up instead of to even turns
TestHalfRounds red; a wrong sha256 in coreml.json makes Load fall back to
the CPU with MLErr set, find the same spans and leave the compiled
program in place (TestCoreMLFailureFallsBack).

Not tried: an Intel Mac, macOS 13 or 14 (only 26.2), the real app's GUI
with Find with a local model on (the gateway path calls the same Find).
2026-10-27 15:01:32 +08:00

109 KiB