Files
magpie/internal/pii/real_test.go
T
yetone b85475e57e pii: pin pf-v4, whose pf-serve maps the model on the CPU
kongbai01203 (plugins#853) saw pf-serve hold ~1.82 GB of private memory
on Windows after the first redaction, until it was ended for idling. On
the CPU path pf-v3 read the whole 1.6 GB q8 GGUF into a malloc'd buffer;
the rest is ~200-270 MB of compute buffers. pf-v4 (691a10446,
weights-paged.patch) maps the file read-only instead, so the weights are
file pages the system can drop and share, not pf-serve's own memory.
Metal and Vulkan load as before.

This pins pf-v4's five zips (sha256 checked against the release's
downloads) and keeps pf-v3 in oldTags, so a user with pf-v3 on disk runs
it until upgrade() has fetched pf-v4. The idle unload stays 10 minutes
after the last request (idleAfter); the reporter's ~25 min was that
timer restarting on each request.

Measured, pf-serve on the CPU, private bytes (commit charge):
- Windows (xiaomi-gpu): v3 1,749 MB after load, 1,822 MB after a 78K-char
  text; v4 200 MB and 273 MB. pf-serve-base the same. Working set v4
  1,363 MB, file pages.
- macOS (mote): physical footprint 1.7-1.8 GB on v3, 209-303 MB on v4.
- Spans (text, offsets, score, all hashed) are identical v3 vs v4 for
  pf-serve, pf-serve-base and pf-serve-vulkan on four texts, 261 to
  78,300 chars. Vulkan on the iGPU: ~1.9-2.1 GB private on both, the
  driver's.
- End to end on Windows through pii.Find with a sandbox XDG_CACHE_HOME
  holding pf-v3 and the model, PF_DEVICE=cpu: magpie downloaded pf-v4
  through the pinned sha, ran it at 220 MB private, and removed pf-v3.

Test: TestRealServeOnTheCPUMapsTheModel (PII_REAL_SERVE, PII_REAL_DIR)
starts the real pf-serve on the CPU, checks the first span of a long
text, and fails when its own memory is over half the model. Broken by
hand by pointing it at pf-v3's binary: red on Windows (1,821 MB), WSL
Linux (1,822 MB) and mote (1,843 MB); green on pf-v4 at 272, 276 and
297 MB. It skips without the env vars, so CI doesn't download 1.6 GB.

Run on mote at 691a10446 plus this change: gofmt -l internal/pii
(clean), GOOS=windows/linux go vet ./internal/pii, go test -count=1
./internal/pii, and under lockf suite.lock go test -count=1
-timeout 45m ./...: every package ok, exit 0 (a first try was cut off
when mote rebooted). Main then gained 2fe4e9c70 in internal/pii
(serve.go, not classifyTimeout); rebased onto c66cdbea9 and run again
there: gofmt, the three go vets and go test ./internal/pii ok on mote,
go test ./internal/pii and the real test ok on Windows (272 MB). The
real test on mote at the rebased head ran out of classifyTimeout's 34s
at load 108 (others' suites; 9.5s on Windows, green on mote before at
normal load) and was not rerun there; the full suite was not rerun at
the rebased head.

Not tried: a machine actually under memory pressure (that the pages are
dropped and read back is the OS's doing), CUDA builds (none shipped),
and linux-arm64/darwin-amd64 beyond their checksums.

Reported-by: kongbai01203
2026-11-08 19:10:44 +08:00

58 lines
2.0 KiB
Go

package pii
import (
"os"
"path/filepath"
"strings"
"testing"
)
// The real pf-serve and model, where PII_REAL_SERVE names a pf-serve and
// PII_REAL_DIR a folder holding the model under modelName(): both are
// 1.6 GB and more to download, so CI has neither and the test is skipped
// there.
func realServe(t *testing.T) string {
t.Helper()
serve, d := os.Getenv("PII_REAL_SERVE"), os.Getenv("PII_REAL_DIR")
if serve == "" || d == "" {
t.Skip("PII_REAL_SERVE and PII_REAL_DIR name no real pf-serve and model")
}
if fi, err := os.Stat(filepath.Join(d, modelName())); err != nil || fi.Size() != modelSize {
t.Fatalf("no model of %d bytes at %s: %v", modelSize, filepath.Join(d, modelName()), err)
}
old := dir
dir = func() string { return d }
t.Cleanup(func() { dir = old })
return serve
}
// On the CPU pf-serve maps the model's file and computes on its pages,
// rather than reading the 1.6 GB into memory of its own (#853 in
// magpie-community/plugins: 1.82 GB private on Windows). The pages are
// the file's: the system reads them in as they are used and lets them
// go under pressure without writing them anywhere. What the process
// holds of its own is the graph's buffers, a few hundred MB at most.
func TestRealServeOnTheCPUMapsTheModel(t *testing.T) {
h, err := startOne(realServe(t), true, nil)
if err != nil {
t.Fatal(err)
}
defer h.kill()
base := "Hi, I'm Jane Doe (jane.doe@example.com, +1 415 555 0100), living at 221B Baker Street, London. "
got, err := h.classify([]string{strings.Repeat(base, 200)})
if err != nil {
t.Fatal(err)
}
if len(got[0]) == 0 || base[got[0][0].Start:got[0][0].End] != "Jane Doe" {
t.Fatalf("found %v, want Jane Doe first", got[0][:min(len(got[0]), 4)])
}
own, ok := privateBytes(h.cmd.Process.Pid)
if !ok {
t.Skip("this system's private memory isn't read here")
}
t.Logf("pf-serve on the CPU holds %d MB of its own", own>>20)
if own > modelSize/2 {
t.Fatalf("pf-serve holds %d MB of its own, the model is %d MB: it was read into memory, not mapped", own>>20, modelSize>>20)
}
}