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 at691a10446plus 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 gained2fe4e9c70in internal/pii (serve.go, not classifyTimeout); rebased ontoc66cdbea9and 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
45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
//go:build !windows
|
|
|
|
package pii
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"regexp"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// privateBytes is the memory pid holds of its own, not a file's pages:
|
|
// Linux's RssAnon, macOS's physical footprint.
|
|
func privateBytes(pid int) (int64, bool) {
|
|
switch runtime.GOOS {
|
|
case "linux":
|
|
b, err := os.ReadFile(fmt.Sprintf("/proc/%d/status", pid))
|
|
if err != nil {
|
|
return 0, false
|
|
}
|
|
for _, l := range strings.Split(string(b), "\n") {
|
|
if f := strings.Fields(l); len(f) == 3 && f[0] == "RssAnon:" {
|
|
kb, err := strconv.ParseInt(f[1], 10, 64)
|
|
return kb << 10, err == nil
|
|
}
|
|
}
|
|
case "darwin":
|
|
out, err := exec.Command("vmmap", "-summary", strconv.Itoa(pid)).Output()
|
|
if err != nil {
|
|
return 0, false
|
|
}
|
|
m := regexp.MustCompile(`(?m)^Physical footprint:\s+([\d.]+)([KMG])`).FindSubmatch(out)
|
|
if m == nil {
|
|
return 0, false
|
|
}
|
|
v, err := strconv.ParseFloat(string(m[1]), 64)
|
|
shift := map[string]uint{"K": 10, "M": 20, "G": 30}[string(m[2])]
|
|
return int64(v * float64(int64(1)<<shift)), err == nil
|
|
}
|
|
return 0, false
|
|
}
|