From 152f7198b9d0b545907ed47e387ddb6412e2801c Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Thu, 1 Oct 2026 21:00:48 +0200 Subject: [PATCH] engine: a RAM budget clamped to the free RAM no longer fails its own safety check (#403) FileExpertSource::pin_cache_complement read the available RAM twice: the budget was clamped to the first reading (less the 4 GiB headroom) and the resident-memory check then failed it on a second reading a few MB lower, so a 64 GB PC with setup's --resident-budget-gib 40 could exit at start. With a budget, the check now reuses the reading the budget was sized from, and a clamped budget stops 256 MiB under the room. An unclamped budget holds the same experts as before (the lend region is off with a budget, so `budget` there is unused). If the budget still cannot be kept (no RAM reading, an allocation that fails), generate.cpp warns and serves every expert the GPU does not hold from the files through the OS file cache (--mmap-experts) instead of exiting; the complement is left unbuilt on every failure path. --- src/core/expert_source.cpp | 19 ++++++++++++++----- src/program/generate.cpp | 6 ++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp index c426d3ac..5197e1e2 100644 --- a/src/core/expert_source.cpp +++ b/src/core/expert_source.cpp @@ -1024,6 +1024,7 @@ bool FileExpertSource::pin_cache_complement( uint64_t bytes = 0; if (!detail::make_cache_complement_plan(n_layers_, n_expert_, layer_blob_bytes_, primary_gpu_pairs, additional_gpu_pairs, offsets, bytes, err)) return false; + uint64_t budget_physical = 0; // #403: the RAM reading a budget was sized from (0: no budget) if (budget_bytes > 0) { // CS-T: a RAM budget. The complement's experts in `rank` order (the expert profile, hottest first) while // they fit, the rest left on the mapped files; clamped to what the RAM has room for. @@ -1032,13 +1033,19 @@ bool FileExpertSource::pin_cache_complement( err = "FileExpertSource: cannot determine available RAM for --resident-budget-gib"; return false; } + budget_physical = physical; const uint64_t room = physical > headroom_bytes ? physical - headroom_bytes : 0; if (budget_bytes > room) { + // #403: 256 MiB under the room, so the engine's own allocations after this reading still leave the + // headroom (a budget clamped to exactly the room failed the safety check below on a reading a few MB + // lower). An unclamped budget is unchanged. + const uint64_t margin = 256ull << 20; + const uint64_t clamped = room > margin ? room - margin : 0; std::fprintf(stderr, "FileExpertSource: --resident-budget-gib %.2f is more than the RAM has room for " - "(%.2f GiB available minus %.0f GiB headroom): %.2f GiB\n", + "(%.2f GiB available minus %.0f GiB headroom and a 0.25 GiB margin): %.2f GiB\n", (double) budget_bytes / 1073741824.0, (double) physical / 1073741824.0, - (double) headroom_bytes / 1073741824.0, (double) room / 1073741824.0); - budget_bytes = room; + (double) headroom_bytes / 1073741824.0, (double) clamped / 1073741824.0); + budget_bytes = clamped; } std::vector ranked(offsets.size(), kNoComplement); uint64_t at = 0; @@ -1065,8 +1072,10 @@ bool FileExpertSource::pin_cache_complement( const bool lend = lend_from_slot >= 0 && lend_from_slot < n_slots && additional_gpu_pairs.empty(); uint64_t budget = std::numeric_limits::max(); if (bytes > 0 || lend) { - uint64_t physical = 0; - if (!available_memory_bytes(physical)) { + // #403: with a budget, the reading it was sized from - a second reading a few MB lower (the engine's own + // allocations, the file cache) failed a budget the first one had clamped. (A budget turns `lend` off.) + uint64_t physical = budget_physical; + if (physical == 0 && !available_memory_bytes(physical)) { err = "FileExpertSource: cannot determine available RAM for the resident-memory safety check"; return false; } diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 7f1e3e96..2bbeb80b 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -3775,6 +3775,12 @@ int main(int argc, char** argv) { std::fprintf(stderr, "strata generate: WARNING: the resident RAM mode does not fit (%s); the experts the " "GPU does not hold are read from the model folder through the OS file cache " "(--mmap-experts), which is slower when the RAM cannot keep them\n", err.c_str()); + } else if (o.resident_budget > 0) { + // #403: a RAM budget that cannot be kept is not a reason to stop - the experts it would have held are + // read from the files like the ones outside it (pin_cache_complement leaves nothing half-built) + std::fprintf(stderr, "strata generate: WARNING: the RAM budget (--resident-budget-gib) cannot be kept (%s); " + "every expert the GPU does not hold is read from the model files through the OS file " + "cache (--mmap-experts), which is slower\n", err.c_str()); } else { std::fprintf(stderr, "strata generate: CPU expert residency: %s\n", err.c_str()); return 1;