Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

metal stream: wire the filled arena with mlock (a file write beside the idle engine froze the M5 Max in a control run)

Niko122162be0f3843f9
diff --git a/docs/MAC.md b/docs/MAC.md
index 17cd931f2..b7db8e95e 100644
--- a/docs/MAC.md
+++ b/docs/MAC.md
@@ -28,7 +28,7 @@ with 32 GB, where the experts stream from the SSD. Other chips and memory sizes
- **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.
- - 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.
+ - 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.
- Setup configures such a Mac without the MTP draft layer: one token per step is faster there.
@@ -103,9 +103,9 @@ of the second request on (the first request after the start pays a few seconds o
| llama.cpp Metal, decode (story / code) | 51.1 / 44.1 | 48.4 / 47.3 | 45.1 / 43.2 | does not run (Insufficient Memory) |
| llama.cpp Metal, prompt 4K / 32K | 791 / 730 | 821 / 739 | 729 / 693 | - |
-- **UD-Q5 and UD-Q6 cannot stay resident:** their repacked file (103.9 and 114.8 GB) is bigger than the largest buffer Metal gives (86.6 GB), so the experts stream from the SSD into an arena of about 80-86 GB (80% of Q5's experts, 74% of Q6's). 98% of the lookups hit it.
+- **UD-Q5 and UD-Q6 cannot stay resident:** their repacked file (103.9 and 114.8 GB) is bigger than the largest buffer Metal gives (86.6 GB), so the experts stream from the SSD into an arena of about 65-67 GB on the 128 GB M5 Max (about 68% of Q5's experts, 62% of Q6's; it was 80-86 GB before the footprint cap below, and the decode and prompt speeds did not change: Q6 story 51 tok/s, 15K prompt 1,020 tok/s at both sizes).
- **The cost is a fixed hand-shake per layer** (the engine reads the router's choice and makes sure the experts are in the arena), not the SSD. A window of 2 tokens costs 38 ms against ~17 resident, so a longer window pays: Q5 story 28 tok/s without MTP, 41.8 with `--spec 2`, 50.6 with `--spec 3`, 46 with `--spec 4`. Setup writes `--spec 3` and keeps the MTP draft layer for a streaming Mac with 96 GB or more.
-- **The arena is limited to 67% of the RAM together with the engine's other buffers.** Wired memory above that made macOS swap: with UD-Q6, a 90 GB arena swapped 29,000 pages in 8 seconds and the Mac later froze, an 84 GB arena ran clean. The "keep 15% free" rule alone did not catch it, because macOS counts file cache as available.
+- **The engine's whole anonymous footprint is limited to 60% of the RAM on a Mac with 64 GB or more (82.4 GB of the M5 Max's 137.4 GB): the arena, every Metal buffer and the host memory the process holds, not only the Metal buffers.** macOS keeps a floor of file cache (`vm.vm_page_filecache_min`: 37.2 GB here) and compresses anonymous memory instead of dropping cache below it. The arena is anonymous, and the GPU wires it only while a command buffer runs, so while it is filled it is ordinary pageable memory. Two kernel panics on the M5 Max (the watchdog daemon starved for 92 s) had the engine at 95 and 90 GiB resident and the compressor at 2-4 million pages. Measured again with a memory watchdog every second: an old-build arena of 100 GB compressed 19 GB of itself in 7 seconds and swapped 24,000 pages; the old automatic arena (69 GB) with a 40 GB file write and a 16 GB program beside it took the compressor from 21,000 to 4.07 million pages in 10 seconds (pressure "warn"). With 30% left (footprint 94.5 GB) and the file write alone, 12,000 pages were swapped out; with 35% left, 316; with 40% left and the write and the 16 GB program, none and the compressor at 0.3 million pages. The old rule (arena + Metal buffers under 67%) counted 7.5 GB of buffers where the process held 11-15 GB, so its real total was about 100 GB. A resident pack is different (clean file pages that macOS drops, never compresses): UD-Q4_K_XL resident (wired 91.7 GB) took the same 40 GB write and 16 GB program with no swap-out and nothing compressed, so resident packs are limited to 70% of the RAM as before. The arena is also wired (`mlock`) once it is filled: unwired it is ordinary pageable memory whenever the GPU is idle (the GPU's own wiring ends a few seconds after its last command buffer; a Metal residency set did not hold it), and a 20 GB file written beside the idle engine took the arena into the compressor and froze the Mac (a third watchdog panic, in a control run of this work). Wired, the same write at full speed raised the compressor by 0.7 million pages (the engine's non-arena memory), swapped out 408 pages and changed nothing else. `STRATA_METAL_WIRE_ARENA=0` turns it off; it is on for Macs with 64 GB or more. The engine prints the planned total (`planned resident footprint ... cap ...`) and the footprint after the fill. `STRATA_METAL_ARENA_GB` still sets the arena as you say, with a warning when it is over the cap.
- **The experts' copy** (`<model>.experts`, 98 GB for Q5) did not pay on the M5 Max: decode 50.3 against 50.2 tok/s without it, 4K prompt 741 against 685 (noisy). Setup does not write it on a Mac with 96 GB or more.
- **Prompts stream in chunks of 4096** on a streaming Mac with 96 GB or more (the engine's own default there; 1024 on a smaller one): every chunk makes one pass over the experts that are not in the arena, so a longer chunk reads them fewer times. UD-Q5, warm, 2 interleaved rounds: chunks of 1024 gave 700 tok/s on the 4K prompt, 2048 gave 920 and 4096 gave 1,100 (32K: 710, 970, 1,100); the decode did not change. `STRATA_METAL_PROMPT_CHUNK=N` sets it. On a resident model 4096 instead of 2048 gained only 2-4% (Q2_0), so that default stays.
@@ -194,6 +194,9 @@ The arena was 11.5-14.7 GB. Tokens per second, one token per step (no MTP), medi
| `STRATA_METAL_STREAM=1` / `=0` | Stream the experts from the SSD even when the pack fits / never (the default decides by size) |
| `STRATA_METAL_CONTIG=0` | Streaming: read the model file even when the experts' copy is there |
| `STRATA_METAL_ARENA_GB=N`, `STRATA_METAL_KEEP_FREE_GB=N` | Streaming: the arena's size, or the memory left to macOS when the arena is sized |
+| `STRATA_METAL_LEAVE_SHARE=0.4` | Streaming, Macs with 64 GB or more: the share of the RAM left to macOS and its file cache when the footprint cap is set (for measuring) |
+| `STRATA_METAL_WIRE_ARENA=0` | Streaming, Macs with 64 GB or more: leave the filled arena pageable (default: wired with mlock) |
+| `STRATA_METAL_IGNORE_REPACK=1` | Start the engine although a model repack or setup's preparation is running beside it (the engine refuses by default: it froze this Mac) |
| `STRATA_METAL_KEEP_HOST_WEIGHTS=1` | Streaming: keep the host code's own copies of the weights (2-3 GB) instead of giving them back to macOS |
| `STRATA_METAL_TRACE=1`, `STRATA_METAL_PROFILE=1`, `STRATA_METAL_KPROF=T` | Per-window traces, a time breakdown, per-kernel GPU times |
diff --git a/metal/src/engine/arena_budget.h b/metal/src/engine/arena_budget.h
index bdf7e0c67..4cc024b7d 100644
--- a/metal/src/engine/arena_budget.h
+++ b/metal/src/engine/arena_budget.h
@@ -61,6 +61,11 @@ constexpr double kGB = 1e9;
// 35% left (89 GB): clear with the write alone (compressor 0.28M pages, 316 pages swapped), not with more beside it;
// 40% left (cap 82.5 GB, footprint 77 GB): 40 GB write + 16 GB program, swapouts 0, compressor 0.3M pages, decode and
// prompt speed equal to the 80 GB arena's (51 tok/s story, 1,020 tok/s for 15K).
+// And the arena is wired (mlock, stream.cpp) once it is filled: unwired it is pageable whenever the GPU is idle (the GPU's own wiring
+// ends seconds after its last command buffer; a Metal residency set did not hold it either), and a 20 GB file written beside the
+// idle engine took tens of GB of the arena into the compressor and froze the Mac (a third watchdog panic, in a control run of this
+// work: wired 2.4 GB, file cache 50 GB, compressor 1.9M pages, 45M compressions). Wired, the same write (full speed, 20 GB in 2.5 s)
+// raised the compressor by 0.7M pages (the ~11 GB of the engine that is not arena), swapped out 408 pages and changed nothing else.
// A pack that stays RESIDENT is different: its bytes are clean file pages (macOS drops them, never compresses them) and they
// count as file cache, not against the floor. UD-Q4_K_XL resident on the M5 Max (wired 91.7 GB, 67%), 40 GB write + 16 GB
// program beside it: swapouts 0, compressor 5,500 pages, 63 tok/s. So two caps:
diff --git a/metal/src/engine/stream.cpp b/metal/src/engine/stream.cpp
index f25d4e4f5..9db74e251 100644
--- a/metal/src/engine/stream.cpp
+++ b/metal/src/engine/stream.cpp
@@ -539,6 +539,31 @@ bool ExpertStream::make_arena(mtl::Context& cx, uint64_t budget, const std::stri
"profile's top (%zu ranked) in %.1f s (%.2f GB/s); %d I/O threads, read-ahead top %d; %zu buffers\n",
(arena_bytes_ - retired_bytes) / 1e9, (unsigned long long) (pairs - nretired), cls_.size(), done, from_profile, fill_s_,
fill_bytes_ / 1e9 / std::max(1e-9, fill_s_), nio_, prefetch_ ? pre_k_ : 0, chunks_.size());
+ // Wire the filled arena (mlock). Unwired, the arena is ordinary anonymous memory whenever the GPU is not running a command buffer
+ // (the GPU's own wiring ends a few seconds after the last one; a Metal residency set did not hold it either, measured), and
+ // macOS compresses it when anything else wants memory: a 20 GB file written beside the idle engine took tens of GB of the arena
+ // into the compressor in seconds and froze the 128 GB M5 Max (watchdog panic, 2026-10-13). Wired, a file write can only
+ // shrink the file cache. STRATA_METAL_WIRE_ARENA=0/1; the default is on for a Mac of 64 GB or more (the M4 32 GB was measured
+ // unwired). Not when the fill stopped early for lack of memory (the unused slots would be wired too).
+ {
+ uint64_t phys = 0;
+ size_t plen = sizeof phys;
+ sysctlbyname("hw.memsize", &phys, &plen, nullptr, 0);
+ const char* we = std::getenv("STRATA_METAL_WIRE_ARENA");
+ const bool want = we && *we ? std::atoi(we) != 0 : (double) phys >= kBigMac;
+ if (want && !stopped) {
+ const double tp = now_ms();
+ uint64_t pinned = 0;
+ int failed = 0;
+ for (const auto& c : chunks_) {
+ if (mlock(c.ptr, c.bytes) == 0) pinned += c.bytes;
+ else ++failed;
+ }
+ std::fprintf(stderr, "strata metal stream: %.1f GB of the arena wired (mlock) in %.0f ms%s; macOS cannot compress it while "
+ "the engine idles (STRATA_METAL_WIRE_ARENA=0 turns it off)\n", pinned / 1e9, now_ms() - tp,
+ failed ? "; SOME CHUNKS COULD NOT BE WIRED" : "");
+ }
+ }
(void) err;
return true;
}