Files
magpie/internal/pii/main_test.go
T
yetone 2fe4e9c70c pii: a GPU the driver reset under an idle pf-serve is loaded again on the GPU, not left on the CPU
kongbai01203 (plugins#853): after ~12 minutes idle the Vulkan pf-serve
exited 3 (vk::DeviceLostError) and Privacy Filter then said "GPU error,
running on CPU" until the GPU was tried again.

Cause: the driver's, not ours. pf-serve sits blocked on stdin while idle
and the idle unload only takes a free turn, so there is no race of ours
with a GPU call; the driver lost or reset the device under it, and the
next GPU call threw (an uncaught vk::DeviceLostError ends in abort, exit
3 on MinGW). magpie then counted one death on the GPU as a lost GPU.

Now one that dies on the GPU is started again on the GPU: a new
pf-serve gets a new device. Only a second death (or hang) in a row on the
GPU moves it to the CPU, until gpuRetryAfter. Status says gpuReset when
the error is a device lost, and gpuAgain when the GPU is tried again;
Settings > Privacy says "On, on the CPU until 14:38: the graphics driver
reset the GPU (device lost)" (and "until {time}" on a GPU failure), in
en, zh, zh-TW, ja and de.

Not changed: the model's size. Privacy Filter has q8 (1.64GB) and f16
only, no smaller quant; AstrLink Guard (39MB) is the small choice.
Its ~1.8GB private on the CPU goes with pf-v4 (mmap, 691a10446), whose
pin is another session's work; on Vulkan the ~1.9GB is the driver's.

Run (mote, through mote-run):
- gofmt -l, go vet (darwin, linux, windows), go test -count=1
  ./internal/pii ./internal/gui ./internal/masking: ok
- GUI: privacy-model, i18n-keys, i18n-en-no-cjk, masking-records in
  chromium and webkit (privacy-model at 1100 and 640, every language):
  219 of 224, then privacy-model 104 of 104 after zh-TW's expected time
  was made 下午02:38 (hhmm's zh-TW form). The one other red is i18n-keys
  "a plugin's words are its own", red on main and filed (#5332, #5426,
  #5482); this change adds no plugin words.
At this head, rebased on 8f6298fb5: gofmt, go vet and go test
./internal/pii ok; privacy-model and i18n-en-no-cjk 108 of 108 in
chromium and webkit.
Rebased after on 37df73b1b (key balances; the one file it shares,
i18n.js, holds other keys there), not run again.
Broken by hand: serve.go put back to HEAD's: TestGPUDeviceLostReloadsOnGPU,
TestGPUDeathMovesToCPU and TestLostGPUTriedAgain red (starts [gpu CPU]).
app.js put back to HEAD's: the new privacy-model test 10 of 10 red
(chromium).
Not tried: a real device lost on a GPU (it can't be forced here); a
crash in pf_free's teardown at idle unload (harmless: end() ignores the
exit status; a fix needs a native change and a new pf tag).
Full suite: not run at push.

Reported-by: kongbai01203
2026-11-08 15:35:59 +08:00

275 lines
8.5 KiB
Go

package pii
import (
"bufio"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/testenv"
)
// The tests run in a home of their own; the test binary started again
// with fakeVar set is a stand-in pf-serve (fakeServe).
func TestMain(m *testing.M) {
if mode := os.Getenv(fakeVar); mode != "" {
fakeServe(mode)
return
}
testenv.Main(m)
}
const (
fakeVar = "PII_FAKE_SERVE"
// fakeLog is a file the stand-in appends what it did to, a line each
fakeLog = "PII_FAKE_LOG"
)
// fakeServe speaks pf-serve's protocol and finds each "Alice" as a
// person. mode says how else it behaves:
//
// ok as pf-serve does
// gpudies on the GPU, ends at its first request
// hang never answers a request
// loadhang never says the model is loaded
// slow answers each request after 300ms
// noload says it couldn't load the model
// badmodel says it couldn't read the model's file, by the name
// it was given
// novulkan as pf-serve-vulkan, says Vulkan is missing; else ok
// gpuloadhang on the GPU, never says the model is loaded; else ok
// asserts fails a ggml assert at its first request
// gpuhang on the GPU, never answers a request; else ok
// gpuhangonce on the GPU, never answers the first request of the
// test; else ok
// gpudiestwice on the GPU, ends at the first two requests of the
// test; else ok
// gpulostidle on the GPU, answers the first request of the test and
// ends at the second, its device lost as Vulkan's is
// after an idle spell (plugins#853); else ok
//
// Like pf-serve, it first writes ggml's startup lines to stderr.
func fakeServe(mode string) {
name := strings.TrimSuffix(filepath.Base(os.Args[0]), ".exe")
gpu := os.Getenv("PF_DEVICE") != "cpu"
device := "MTL0"
if !gpu {
device = "CPU"
}
logf := func(format string, a ...any) {
if p := os.Getenv(fakeLog); p != "" {
f, err := os.OpenFile(p, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err == nil {
fmt.Fprintf(f, format+"\n", a...)
f.Close()
}
}
}
logf("start %s %s", name, device)
// as ggml_metal writes them on an M2, GPU or CPU alike
fmt.Fprintln(os.Stderr, "ggml_metal_device_init: tensor API disabled for pre-M5 and pre-A19 devices")
fmt.Fprintln(os.Stderr, "ggml_metal_library_init: using embedded metal library")
fmt.Fprintln(os.Stderr, "ggml_metal_device_init: GPU name: MTL0 (Apple M2)")
fmt.Fprintln(os.Stderr, "ggml_metal_device_init: recommendedMaxWorkingSetSize = 12713.12 MB")
logf("path %s", filepath.Base(filepath.Dir(os.Args[0])))
logf("residency %s", os.Getenv("GGML_METAL_NO_RESIDENCY"))
logf("moechunk %s %s", name, os.Getenv("PF_MOE_CHUNK"))
// as pf-serve on Windows, whose argv is in the ANSI code page and
// whose ggml reads it as UTF-8: a model path with a byte outside
// ASCII can't be opened. And it opens the file from where it runs.
model := ""
if len(os.Args) > 1 {
model = os.Args[1]
}
logf("model %s", model)
_, err := os.Stat(model)
if strings.IndexFunc(model, func(r rune) bool { return r > 0x7f }) >= 0 || err != nil || mode == "badmodel" {
json.NewEncoder(os.Stdout).Encode(map[string]string{"error": "failed to read GGUF: " + model})
os.Exit(1)
}
if mode == "noload" || mode == "novulkan" && name == "pf-serve-vulkan" {
json.NewEncoder(os.Stdout).Encode(map[string]string{"error": "can't load " + mode})
os.Exit(1)
}
if mode == "loadhang" || mode == "gpuloadhang" && gpu {
time.Sleep(time.Hour)
}
json.NewEncoder(os.Stdout).Encode(map[string]any{"device": device, "gpu": gpu, "ms": 7})
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
u32 := func() (uint32, error) {
var b [4]byte
_, err := io.ReadFull(in, b[:])
return binary.LittleEndian.Uint32(b[:]), err
}
put := func(v any) { binary.Write(out, binary.LittleEndian, v) }
for {
n, err := u32()
if err != nil {
os.Exit(0) // stdin ended: magpie let it go
}
texts := make([]string, n)
for i := range texts {
l, _ := u32()
b := make([]byte, l)
io.ReadFull(in, b)
texts[i] = string(b)
}
logf("request %d %q", n, texts)
// nth: which request of the test this is on the GPU, counted
// across the pf-serves the test starts
nth := 0
if p := os.Getenv(fakeLog); p != "" && gpu {
for nth = 1; ; nth++ {
f, err := os.OpenFile(fmt.Sprintf("%s.gpu%d", p, nth), os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o644)
if err == nil {
f.Close()
break
}
}
}
once := strings.HasSuffix(mode, "once") && nth == 1
switch {
case mode == "gpudies" && gpu, mode == "gpudiestwice" && gpu && nth <= 2:
fmt.Fprintln(os.Stderr, "ggml_metal: device lost")
os.Exit(3)
case mode == "gpulostidle" && nth == 2:
// as pf-serve-vulkan on Windows (MinGW) ends with it
fmt.Fprintln(os.Stderr, "terminate called after throwing an instance of 'vk::DeviceLostError'")
fmt.Fprintln(os.Stderr, " what(): vk::Device::waitForFences: ErrorDeviceLost")
os.Exit(3)
case mode == "hang", mode == "gpuhang" && gpu, mode == "gpuhangonce" && once:
time.Sleep(time.Hour)
case mode == "asserts":
fmt.Fprintln(os.Stderr, "ggml_metal_library_compile_pipeline: compiling pipeline: base = 'kernel_add_id'")
fmt.Fprintln(os.Stderr, "/ggml/src/ggml-metal/ggml-metal-ops.cpp:1234: GGML_ASSERT(ne00 % 4 == 0) failed")
os.Exit(3)
case mode == "slow":
time.Sleep(300 * time.Millisecond)
}
put(uint32(0))
for _, t := range texts {
var at []int
for i := 0; ; {
j := strings.Index(t[i:], "Alice")
if j < 0 {
break
}
at = append(at, i+j)
i += j + 5
}
put(uint32(len(at)))
for _, a := range at {
put(int32(a))
put(int32(a + 5))
put(float32(0.98))
put(uint8(len("private_person")))
out.WriteString("private_person")
}
}
out.Flush()
}
}
// fakeInstall puts the stand-in where pf-serve goes (and as
// pf-serve-vulkan too, with vulkan), and a model file, in a folder of the
// test's own; it gives the file the stand-in logs to.
func fakeInstall(t *testing.T, mode string, vulkan bool) string {
t.Helper()
return fakeInstallAt(t, mode, vulkan, t.TempDir())
}
// fakeInstallAt is fakeInstall in folder d.
func fakeInstallAt(t *testing.T, mode string, vulkan bool, d string) string {
t.Helper()
upgrades.Wait() // one started before reads dir
if err := os.MkdirAll(d, 0o755); err != nil {
t.Fatal(err)
}
was, wasSize, wasIdle := dir, modelSize, idleAfter
dir, modelSize = func() string { return d }, 4
t.Cleanup(func() {
Stop()
// a request Stop failed lets its turn go
turn <- struct{}{}
<-turn
upgrades.Wait()
disk.mu.Lock()
if disk.timer != nil && disk.timer.Stop() {
disk.saves.Done()
}
disk.timer = nil
disk.loaded = ""
disk.mu.Unlock()
disk.saves.Wait() // a write under way reads dir
srv.mu.Lock()
srv.cpu, srv.gpuLost, srv.failed = false, "", ""
srv.gpuFails, srv.gpuAgain = 0, time.Time{}
srv.lastErr, srv.fails, srv.retryAt = nil, 0, time.Time{}
srv.mu.Unlock()
cache.mu.Lock()
cache.ll.Init()
clear(cache.m)
cache.mu.Unlock()
dir, modelSize, idleAfter = was, wasSize, wasIdle
})
if err := os.WriteFile(modelPath(), []byte("gguf"), 0o644); err != nil {
t.Fatal(err)
}
self, err := os.Executable()
if err != nil {
t.Fatal(err)
}
b, err := os.ReadFile(self)
if err != nil {
t.Fatal(err)
}
os.MkdirAll(serveDir(), 0o755)
names := []string{"pf-serve"}
if vulkan {
names = append(names, "pf-serve-vulkan")
}
for _, n := range names {
if err := os.WriteFile(exe(n), b, 0o755); err != nil {
t.Fatal(err)
}
}
log := filepath.Join(d, "fake.log")
t.Setenv(fakeVar, mode)
t.Setenv(fakeLog, log)
return log
}
// gpuInstall is fakeInstall with a GPU's pf-serve to start first:
// macOS's own, which runs on Metal, or elsewhere pf-serve-vulkan, as a
// computer with no Vulkan loader has none (#3741: these tests took
// macOS's for every system, and were red on Linux). It answers the line
// the stand-in logs as it starts there.
func gpuInstall(t *testing.T, mode string) (log, gpuStart string) {
t.Helper()
if runtime.GOOS == "darwin" {
return fakeInstall(t, mode, false), "start pf-serve MTL0"
}
return fakeInstall(t, mode, true), "start pf-serve-vulkan MTL0"
}
// fakeLines are the lines the stand-in logged that start with prefix.
func fakeLines(t *testing.T, log, prefix string) []string {
t.Helper()
b, _ := os.ReadFile(log)
var out []string
for _, l := range strings.Split(string(b), "\n") {
if strings.HasPrefix(l, prefix) {
out = append(out, l)
}
}
return out
}