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); docs/MAC.md table recomputed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Thomas ZenkelandClaude Opus 5.5 committed 2026-10-24 18:13:24 +02:00
1 parent 7dd5cf18e6
commit 358cc5e0c0
3 files changed
+30 -16

No files matched your search

+8 -8
View File
@@ -29,17 +29,17 @@ with 32 GB and an M1 Max with 34 GB, where the experts stream from the SSD (the
| Pack | The file the GPU maps | Needs about (with the prompt buffers, KV cache, draft layer) | Recommended from |
|---|---|---|---|
| Q2_0 | 37.6 GB | 46 GB | 64 GB |
| IQ3_XXS | 47.0 GB | 55 GB | 74 GB (where IQ3_S does not fit) |
| IQ3_S (recommended; the default from 96 GB) | 54.8 GB | 63 GB | 96 GB |
| UD-Q4_K_XL (Unsloth, recommended: higher quality) | 82.6 GB | 91 GB | 128 GB |
| Q2_0 | 37.6 GB | 50 GB | 96 GB; streams below that |
| IQ3_XXS | 47.0 GB | 60 GB | 80 GB (where IQ3_S does not fit) |
| IQ3_S (recommended; the default from 96 GB) | 54.8 GB | 68 GB | 96 GB |
| UD-Q4_K_XL (Unsloth, recommended: higher quality) | 82.6 GB | 95 GB | 128 GB |
| UD-Q5_K_XL, UD-Q6_K_XL (Unsloth) | 103.9 / 114.8 GB | stream from the SSD on 128 GB | 128 GB, streamed |
| Gyro-S (agentionai, one 58.5 GB file) | 29.7 GB (the file's other 28.8 GB is the n-gram table) | 38 GB | 48 GB; streams below that (measured on 32 GB) |
| Gyro-M (agentionai, one 92.0 GB file) | 63.2 GB (+ the 28.8 GB table) | 71 GB | 96 GB; streams below that (not measured on a Mac) |
| Gyro-S (agentionai, one 58.5 GB file) | 29.7 GB (the file's other 28.8 GB is the n-gram table) | 43 GB | 64 GB; streams below that (measured on 32 and 48 GB) |
| Gyro-M (agentionai, one 92.0 GB file) | 63.2 GB (+ the 28.8 GB table) | 76 GB | 128 GB; streams below that (not measured on a Mac) |
- **The n-gram table:** each pack also has a 28.8 GB n-gram table (the model's second file). It is read from the SSD a few rows at a time and does not count against the memory above.
- **Disk:** the download is 66-84 GB, plus about 7 GB for the pack and the draft layer. A Mac that streams also needs room for the experts' copy (below): 34-40 GB.
- **A Mac whose memory cannot hold the pack (32-48 GB):** the engine streams the experts from the SSD instead. It decides this by itself when the experts are more than 60% of Metal's share.
- **A Mac whose memory cannot hold the pack (32-64 GB):** the engine streams the experts from the SSD instead. It decides this by itself when the mapped weights and 12 GiB beside them (the prompt buffers, the KV cache, the draft layer, macOS) do not fit Metal's share. Setup uses the same rule (`strata_control/mac_fit.py`). With the 8 GB it used before, setup kept Gyro-S resident on a 48 GB M4 Pro; the weights were paged in and out and a 53-token prompt did not finish in 60 s. Streamed on the same Mac (engine 0.1.48, no draft layer) it answered a short question in 4 s.
- It keeps the most-used experts in an arena: Metal's share, or the memory macOS reports as available less what it keeps for macOS, whichever is smaller. It keeps 7 GB, or 15% of the RAM when that is more, and it also keeps the n-gram table's share. It counts as free only what macOS can give without swapping (free and purgeable memory and the clean file cache, not other programs' idle memory). It checks again just before it takes the arena, and fills the arena in 1.5 GB steps, checking again before each one; it stops early rather than push macOS below what it keeps. On a Mac with 64 GB or more the arena, the Metal buffers and the host memory together also stay under 60% of the RAM (see "The engine's whole anonymous footprint" below).
- It reads the rest as each token needs them, and reads ahead the experts the next layer will probably use.
- Setup writes a second copy of the experts for such a Mac (`<model>.experts`, 40 GB for IQ3_XXS). In it each expert's three matrices sit together, so the SSD reads an expert with one request instead of three. On the 32 GB M4 that is about 17% faster decoding and 30% faster prompts, with the same tokens. Setup writes it only when the disk has room for it plus 10 GB; otherwise it warns, and the engine reads the model file as before.
@@ -178,7 +178,7 @@ Hadamard-rotated basis, read from the published file unchanged. The kernels are
`moe_mm.metal`); the codebook and the decoder are the CPU engine's, the rotation is applied to the gate/up and down inputs as on CUDA.
Setup offers Gyro-S and Gyro-M next to the other sizes (`./setup.sh --family gyro --model Gyro-S`), never as the default, and says
what the choice risks on your Mac's memory. The 28.8 GB n-gram table is inside the file and is read from the SSD like every other
pack's, so Gyro-S needs 58.5 GB of disk and about 38 GB of memory to stay resident (29.7 GB of weights).
pack's, so Gyro-S needs 58.5 GB of disk and about 43 GB of memory to stay resident (29.7 GB of weights).
**Measured on the M4 Mac mini, 32 GB** (the experts stream from the SSD; no experts' copy, because the copy would need 26 GB more
disk than this Mac had; one token per step, no draft layer; the arena was 11-12 GB of the 25.8 GB of experts):
+11 -7
View File
@@ -45,8 +45,11 @@ STREAM_COPY_GAIN = "decoding about 15%, prompts about 25%" # without the exper
STREAM_MTP_FROM_GB = 96
STREAM_SPEC = 3
# 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
@@ -121,12 +124,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)
def model_need_gb(model: str) -> float:
+11 -1
View File
@@ -73,7 +73,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)
@@ -207,6 +207,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