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: the arena plus the engine's own buffers stay under 67% of the RAM (wired through the residency set; UD-Q6 on the 128 GB M5 Max with a 90 GB arena made macOS swap and then freeze, 84 GB ran clean)

Niko122143d14715bdeb
diff --git a/metal/src/engine/arena_budget.h b/metal/src/engine/arena_budget.h
index 4f6985290..fbb1c2210 100644
--- a/metal/src/engine/arena_budget.h
+++ b/metal/src/engine/arena_budget.h
@@ -39,6 +39,12 @@ struct ArenaMem {
constexpr double kGB = 1e9;
+// The GPU keeps the whole arena (and the engine's other buffers) resident through a residency set, i.e. wired. Measured on the
+// M5 Max (137.4 GB), UD-Q6_K_XL: 84 GB of arena + 7.5 GB allocated (91.5 GB, 67%) ran with no swap-out; 90 GB + 7.5 GB (97.5 GB)
+// made macOS swap 29,000 pages in 8 s with a "warn" pressure level and the Mac went on to freeze; UD-Q5 with 90 GB + 5.7 GB ran,
+// near the edge. So arena + allocated stay under this share of the RAM, whatever the free-memory count says.
+constexpr double kWiredShare = 0.67;
+
/// Bytes macOS can give without compressing or swapping: free + purgeable + the clean file cache (inactive and
/// speculative pages, at most the file-backed ones).
inline double arena_avail(double free_b, double purgeable_b, double inactive_b, double speculative_b, double file_b) {
@@ -56,7 +62,8 @@ inline double arena_budget(const ArenaMem& m) {
if (m.arena_gb >= 0) return m.arena_gb * kGB;
const double gpu = m.working_set - m.allocated - 1.0 * kGB;
const double ram = m.available - arena_keep(m) - arena_ple_reserve(m);
- return std::max(1.0 * kGB, std::min(gpu, ram));
+ const double wired = kWiredShare * m.phys - m.allocated;
+ return std::max(1.0 * kGB, std::min(std::min(gpu, ram), wired));
}
/// Just before the arena is filled: the bytes it may still have when macOS can now give `available_now` (it shrinks,
diff --git a/metal/tests/arena_budget_test.cpp b/metal/tests/arena_budget_test.cpp
index ce8050d1e..6a91a16bd 100644
--- a/metal/tests/arena_budget_test.cpp
+++ b/metal/tests/arena_budget_test.cpp
@@ -43,7 +43,11 @@ int main() {
check(std::fabs(arena_budget(tiny) - 1.0 * kGB) < 1e6, "a 16 GB Mac with nothing left: 1 GB (low hit rate)", arena_budget(tiny));
ArenaMem gpu = m5;
gpu.available = 200 * kGB;
- check(std::fabs(arena_budget(gpu) - (115.4 - 7.0 - 1.0) * kGB) < 1e6, "the working set bounds it", arena_budget(gpu));
+ check(std::fabs(arena_budget(gpu) - (0.67 * 137.4 - 7.0) * kGB) < 1e6, "plenty free: the wired share (67% of the RAM) bounds it", arena_budget(gpu));
+ ArenaMem q6 = m5; // UD-Q6_K_XL after a reboot: 7.5 GB allocated, 125 GB "available" (cache): 84 GB ran, 90 GB swapped
+ q6.allocated = 7.5 * kGB;
+ q6.available = 125 * kGB;
+ check(arena_budget(q6) <= 84.7 * kGB && arena_budget(q6) > 84.0 * kGB, "M5 Max, UD-Q6 with 125 GB available: 84.6 GB, not 104", arena_budget(q6));
// 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 - 0.5) * kGB) < 0.1 * kGB, "less free when committing: it shrinks", s1);