mac_fit: setup decides resident vs streaming with the engine's rule (12 GiB beside the weights, resident_cap)

Setup wrote STRATA_METAL_STREAM=0 when the pack plus 8 GB fit 3/4 of the RAM, and the engine follows that
value instead of its own rule (stream.cpp: the mapped weights + 12 GiB against resident_cap). On a 48 GB
M4 Pro, Gyro-S (29.7 GB of weights) was forced resident where the engine would stream: its pages went in
and out, a 53-token prompt did not finish in 60 s, and the watchdog restarted the engine in a loop.
Streamed on the same Mac (engine 0.1.48, no draft layer) a short question was answered in 4 s.

- OVERHEAD_GB is 12 GiB, the engine's figure (the process held 10.8 GB beside the weights there)
- gpu_budget_gb also leaves macOS 7 GB or 15% on a small Mac, as resident_cap does
- three cases change, all to what the engine itself decides: Q2_0 on 64 GB, Gyro-S on 48 GB and Gyro-M
  on 96 GB now stream (Q2_0 on 64 GB also loses the draft layer: not measured)
- tools/test_setup_mac.py: a test of the rule (fails on the old code)

(#3983, cherry-picked from 358cc5e0c without its trailer line; its docs/MAC.md table waits for review and is not part of this commit)
This commit is contained in:
Thomas Zenkel authored and Niko1221 committed 2026-11-16 18:15:51 +01:00
1 parent 677616cc91
commit cf5abac234
2 files changed
+22 -8

No files matched your search

+11 -7
View File
@@ -78,8 +78,11 @@ def spec_args(model: str, stream: bool, mtp: bool) -> list[str]:
return ["--spec", "3", "--spec-min-p", SPEC3_MIN_P]
return ["--spec", "2"]
# beside the mapped file: the prompt kernels' buffers (chunks of 2,048 rows), the KV cache, the draft layer, the
# Q8_0 copies of the hyper-connection matrices and the macOS desktop
OVERHEAD_GB = 8.0
# Q8_0 copies of the hyper-connection matrices and the macOS desktop. 12 GiB, the engine's own figure
# (metal/src/engine/stream.cpp, ExpertStream::wanted): with 8 GB setup kept Gyro-S resident on a 48 GB M4 Pro where the
# engine streams, and the forced resident run paged its weights in and out until the watchdog stopped it (engine 0.1.48;
# the process held 10.8 GB beside the weights there, without the draft layer)
OVERHEAD_GB = 12.0 * 1.073741824
# the share of the unified memory Metal lets one process keep resident (recommendedMaxWorkingSetSize): 107.5 of 128 GB
# measured on the M5 Max; 3/4 is the conservative rule for the other sizes
GPU_SHARE = 0.75
@@ -165,12 +168,13 @@ def mac_problems(info: dict, need_compiler: bool = True) -> list[str]:
def gpu_budget_gb(ram_gb: float) -> float:
"""The most a resident pack may hold, in GB: Metal's working set, and on a big Mac the RAM less what macOS and the file
cache keep (the engine's resident_cap: min of the two)."""
ws = ram_gb * GPU_SHARE * GIB_GB
"""The most a resident pack may hold, in GB: Metal's working set, and the RAM less what macOS keeps (7 GB or 15%, on a
big Mac 30%) - the engine's resident_cap in arena_budget.h: min of the two."""
phys = ram_gb * GIB_GB
leave = max(7.0, 0.15 * phys)
if ram_gb >= BIG_MAC_GB:
return min(ws, ram_gb * GIB_GB * (1.0 - RESIDENT_LEAVE_SHARE))
return ws
leave = max(leave, RESIDENT_LEAVE_SHARE * phys)
return min(phys * GPU_SHARE, phys - leave)
# The families the Mac app (setup.py's mac_control_setup and the app's model list) installs: the GSQ-RCO Qwen files, Unsloth's sets (several shards:
+11 -1
View File
@@ -80,7 +80,7 @@ class Recommendation(unittest.TestCase):
rec, notes = M.recommend_model(32)
self.assertEqual(rec, "Q2_0")
self.assertTrue(notes and "stream" in notes[0] and "~14 tok/s" in notes[0])
self.assertTrue(M.streams("Q2_0", 32) and M.streams("Q2_0", 48) and not M.streams("Q2_0", 64))
self.assertTrue(M.streams("Q2_0", 32) and M.streams("Q2_0", 64) and not M.streams("Q2_0", 96))
def test_a_bigger_choice_is_kept_with_a_note(self):
notes = M.model_note("IQ3_XXS", 64)
@@ -277,6 +277,16 @@ class RepackGuard(unittest.TestCase):
self.assertTrue(M.streams("UD-Q5_K_XL", 128))
self.assertTrue(M.streams("UD-Q4_K_XL", 96))
def test_setup_decides_streaming_as_the_engine_does(self):
# setup writes STRATA_METAL_STREAM=0/1 and the engine follows it: both must use stream.cpp's rule (the mapped bytes
# + 12 GiB against resident_cap), or a pack is forced resident where the engine would stream. Gyro-S on a 48 GB
# M4 Pro (working set 40.2 GB): 29.7 + 12.9 GB does not fit, the forced resident run paged until the watchdog stopped it
self.assertAlmostEqual(M.OVERHEAD_GB, 12 * 2 ** 30 / 1e9, places=6)
self.assertTrue(M.streams("Gyro-S", 48))
self.assertFalse(M.streams("Gyro-S", 64))
self.assertTrue(M.streams("Gyro-M", 96))
self.assertAlmostEqual(M.gpu_budget_gb(16), 16 * 1.073741824 - 7.0, places=3) # a small Mac: macOS keeps 7 GB
@unittest.skipIf(sys.platform == "win32", "os.kill(pid, 0) is a probe on macOS and Linux only")
def test_a_live_lock_is_seen_a_stale_one_is_dropped(self):
import os