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: the streaming arena keeps max(5 GB, 15% of the RAM) and reserves 0.5 GB for the n-gram table read row by row

Niko12218dc89b0f4aa1
diff --git a/metal/src/engine/arena_budget.h b/metal/src/engine/arena_budget.h
index 606d495d1..05303e4b5 100644
--- a/metal/src/engine/arena_budget.h
+++ b/metal/src/engine/arena_budget.h
@@ -3,12 +3,13 @@
//
// The arena is anonymous memory the GPU keeps resident: macOS cannot page it out or drop it like file cache. It must
// leave room for macOS and the desktop, and for the other big memory the engine itself uses while it runs:
-// keep macOS and the desktop: max(7 GB, 15% of the RAM) - STRATA_METAL_KEEP_FREE_GB sets it. The M5 Max
+// keep macOS and the desktop: max(5 GB, 15% of the RAM) - STRATA_METAL_KEEP_FREE_GB sets it. The M5 Max
// (128 GB) froze with an 89 GB arena and 7 GB kept beside a 50 GB n-gram table; with 15% it keeps 20.6 GB.
-// On the 32 GB M4, 7 GB kept left macOS 55% of its memory free all run (memory_pressure) and decodes
-// 7.8 tok/s; 12 GB kept cut the arena from 9.4 to 4.4 GB and decode to 4.8 tok/s (Q2_0, 200 tokens).
-// ple the n-gram table: mapped whole in RAM (--ple-io ram) it is all of it; read row by row (the default)
-// its row cache and the OS's read-ahead, 2 GB.
+// The 32 GB M4 (IQ3_XXS after a 6K prompt, 5 interleaved runs, memory_pressure every 3 s, vm_stat
+// swapouts): 7 GB kept + 2 GB reserve 5.93 tok/s (arena ~8.5 GB, least free 50%), 7 + 0.5 6.59 (~10 GB,
+// 49%), 5 + 0.5 7.47 (~12 GB, 43%); no swap-out in any run.
+// ple the n-gram table: mapped whole in RAM (--ple-io ram) it is all of it; read row by row (the default,
+// F_NOCACHE: no file cache) only its bounded row cache, 1M rows of at most 192 bytes: 0.5 GB reserved.
// GPU the Metal working set less what is already allocated, less 1 GB.
// STRATA_METAL_ARENA_GB sets the arena outright. The arena is placed just before it is filled, and the memory macOS
// can give is asked again then: when it has dropped (another program), the arena shrinks (arena_shrink).
@@ -31,10 +32,10 @@ struct ArenaMem {
constexpr double kGB = 1e9;
inline double arena_keep(const ArenaMem& m) {
- return m.keep_gb >= 0 ? m.keep_gb * kGB : std::max(7.0 * kGB, 0.15 * m.phys);
+ return m.keep_gb >= 0 ? m.keep_gb * kGB : std::max(5.0 * kGB, 0.15 * m.phys);
}
-inline double arena_ple_reserve(const ArenaMem& m) { return m.ple_mapped > 0 ? m.ple_mapped : 2.0 * kGB; }
+inline double arena_ple_reserve(const ArenaMem& m) { return m.ple_mapped > 0 ? m.ple_mapped : 0.5 * kGB; }
/// The arena's bytes (at least 1 GB: a smaller arena still runs, at a low hit rate).
inline double arena_budget(const ArenaMem& m) {
diff --git a/metal/tests/arena_budget_test.cpp b/metal/tests/arena_budget_test.cpp
index ca1fb6fd3..6314ef271 100644
--- a/metal/tests/arena_budget_test.cpp
+++ b/metal/tests/arena_budget_test.cpp
@@ -18,20 +18,22 @@ int main() {
ArenaMem m5{137.4 * kGB, 115.4 * kGB, 7.0 * kGB, 97.7 * kGB};
check(std::fabs(arena_keep(m5) - 0.15 * 137.4 * kGB) < 1e6, "M5 Max: macOS keeps 15% of the RAM (20.6 GB)", arena_keep(m5));
const double b5 = arena_budget(m5);
- check(b5 <= 97.7 * kGB - 20.6 * kGB - 2.0 * kGB + 1e6, "M5 Max: the arena leaves >= 20 GB + the n-gram reserve", b5);
+ check(b5 <= 97.7 * kGB - 20.6 * kGB - 0.5 * kGB + 1e6, "M5 Max: the arena leaves >= 20 GB + the n-gram reserve", b5);
check(b5 > 70 * kGB, "M5 Max: still a large arena (UD-Q5's 98 GB of experts mostly cached)", b5);
// the n-gram table mapped whole (--ple-io ram, 50.7 GB): it comes out of the arena
ArenaMem m5r = m5;
m5r.ple_mapped = 50.7 * kGB;
check(arena_budget(m5r) <= b5 - 48.0 * kGB, "M5 Max, table in RAM: the arena gives up the table's 50.7 GB", arena_budget(m5r));
- // the M4 Mac mini, 32 GB: 7 GB kept (15% would be 5 GB)
+ // the M4 Mac mini, 32 GB: 15% = 5.16 GB kept
ArenaMem m4{34.4 * kGB, 26.8 * kGB, 3.3 * kGB, 17.8 * kGB};
- check(std::fabs(arena_keep(m4) - 7.0 * kGB) < 1e6, "M4 32 GB: macOS keeps 7 GB", arena_keep(m4));
- check(std::fabs(arena_budget(m4) - (17.8 - 7.0 - 2.0) * kGB) < 1e6, "M4 32 GB: available - 7 - 2", arena_budget(m4));
+ check(std::fabs(arena_keep(m4) - 5.16 * kGB) < 1e6, "M4 32 GB: macOS keeps 15% (5.16 GB)", arena_keep(m4));
+ check(std::fabs(arena_budget(m4) - (17.8 - 5.16 - 0.5) * kGB) < 1e6, "M4 32 GB: available - 5.16 - 0.5", arena_budget(m4));
+ ArenaMem m16{17.2 * kGB, 11 * kGB, 3 * kGB, 9 * kGB};
+ check(std::fabs(arena_keep(m16) - 5.0 * kGB) < 1e6, "a 16 GB Mac: the 5 GB floor", arena_keep(m16));
// the overrides
ArenaMem o = m4;
o.keep_gb = 12.0;
- check(std::fabs(arena_budget(o) - (17.8 - 12.0 - 2.0) * kGB) < 1e6, "STRATA_METAL_KEEP_FREE_GB=12", arena_budget(o));
+ check(std::fabs(arena_budget(o) - (17.8 - 12.0 - 0.5) * kGB) < 1e6, "STRATA_METAL_KEEP_FREE_GB=12", arena_budget(o));
o.arena_gb = 10.0;
check(std::fabs(arena_budget(o) - 10.0 * kGB) < 1e6, "STRATA_METAL_ARENA_GB=10 wins", arena_budget(o));
check(std::fabs(arena_shrink(o, 10.0 * kGB, 1.0 * kGB) - 10.0 * kGB) < 1e6, "an explicit arena is not shrunk", 10.0 * kGB);
@@ -43,7 +45,7 @@ int main() {
check(std::fabs(arena_budget(gpu) - (115.4 - 7.0 - 1.0) * kGB) < 1e6, "the working set bounds it", arena_budget(gpu));
// memory taken between planning and committing: it shrinks, never grows
const double s1 = arena_shrink(m5, b5, 60.0 * kGB);
- check(s1 < b5 && std::fabs(s1 - (60.0 - 20.61 - 2.0) * kGB) < 0.1 * kGB, "less free when committing: it shrinks", s1);
+ check(s1 < b5 && std::fabs(s1 - (60.0 - 20.61 - 0.5) * kGB) < 0.1 * kGB, "less free when committing: it shrinks", s1);
check(std::fabs(arena_shrink(m5, b5, 130.0 * kGB) - b5) < 1e6, "more free when committing: it stays as planned", b5);
std::printf("%s\n", failures ? "FAILED" : "all passed");
return failures ? 1 : 0;