diff --git a/internal/pii/main_test.go b/internal/pii/main_test.go index e25af4cb2..95c08a1e7 100644 --- a/internal/pii/main_test.go +++ b/internal/pii/main_test.go @@ -56,6 +56,7 @@ func fakeServe(mode string) { } } logf("start %s %s", name, device) + logf("residency %s", os.Getenv("GGML_METAL_NO_RESIDENCY")) 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) diff --git a/internal/pii/serve.go b/internal/pii/serve.go index 0dbc23539..70a83713a 100644 --- a/internal/pii/serve.go +++ b/internal/pii/serve.go @@ -93,7 +93,13 @@ func start(cpu bool) (*helper, error) { func startOne(path string, cpu bool) (*helper, error) { cmd := proc.Command(path, modelPath()) - cmd.Env = os.Environ() + // Without GGML_METAL_NO_RESIDENCY, ggml's Metal residency set keeps a + // heartbeat that woke an idle pf-serve ~146 times a second for the 10 + // minutes it stays up after a request, and macOS's Energy Impact + // counted magpie among the top for it (#3185). With it set: 0.3 a + // second, and the first request after an idle spell takes 0.1-0.45s + // more (51-78ms hot either way). + cmd.Env = append(os.Environ(), "GGML_METAL_NO_RESIDENCY=1") if cpu { cmd.Env = append(cmd.Env, "PF_DEVICE=cpu") } diff --git a/internal/pii/serve_test.go b/internal/pii/serve_test.go index a2edc06c1..99f76f2d7 100644 --- a/internal/pii/serve_test.go +++ b/internal/pii/serve_test.go @@ -114,6 +114,25 @@ func TestVulkanThatWontStartFallsBack(t *testing.T) { } } +// Every pf-serve starts with ggml's Metal residency heartbeat off: it +// woke an idle one ~146 times a second (#3185). The CPU one after a GPU +// death too. +func TestServeStartsWithoutResidencyHeartbeat(t *testing.T) { + log := fakeInstall(t, "gpudies", false) + if _, err := Find([]string{"hi Alice"}); err != nil { + t.Fatal(err) + } + got := fakeLines(t, log, "residency") + if len(got) != 2 { + t.Fatalf("residency lines = %q, want a GPU and a CPU start", got) + } + for _, l := range got { + if l != "residency 1" { + t.Fatalf("residency lines = %q, want GGML_METAL_NO_RESIDENCY=1 on each", got) + } + } +} + // A pf-serve that doesn't answer is killed, and the request fails rather // than wait for good. func TestHungServeIsKilled(t *testing.T) {