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++
Respect Windows commit capacity
Maxim Shipko2de94bca5eb7
diff --git a/docs/DETAILS.md b/docs/DETAILS.mdindex 0fffc11bc..61d84b548 100644--- a/docs/DETAILS.md+++ b/docs/DETAILS.md@@ -183,10 +183,11 @@ engine fetches a layer's missing experts on 8 threads (`STRATA_FETCH_THREADS`) w**A RAM budget (engine 0.1.31, `--resident-budget-gib N`):** the resident variant for a model whose experts do not allfit: the N GiB of experts the GPU cache does not hold that the expert profile ranks hottest are copied into RAM at-start (locked; page-locked when the driver allows the whole budget), and the rest are read from the files through the-OS file cache. It implies `--mmap-experts` and leaves 4 GB of free RAM (a larger N is clamped to that less 256 MiB,-with a message; #403: a clamped budget no longer fails the safety check that follows, and a budget that cannot be-kept at all is a warning, with every expert read from the files). Setup sets N with `--resident-budget-gib N`. With+start (locked; page-locked when the driver allows the whole budget), and the rest are read from the files.+It implies `--mmap-experts` and leaves 4 GiB of headroom. On Windows, available commit capacity also+limits the budget; a larger N is clamped to the smaller limit less 4 GiB and a 256 MiB margin, with a message.+A clamped budget no longer fails the safety check that follows (#403). A budget that cannot be kept at all is a+warning, with every expert read from the files. Setup sets N with `--resident-budget-gib N`. Withthe GGUF read in place it also warms the next layer's likely experts: while the CPU works on a layer, a thread appliesthe next layer's router to this layer's input and asks the OS for the pages of the predicted experts that neither theGPU nor the RAM budget holds (only pages - the experts computed are the same; `STRATA_LOOKAHEAD=0` turns it off). Thisdiff --git a/docs/UNSLOTH_Q4.md b/docs/UNSLOTH_Q4.mdindex 9e2601019..5a87468e9 100644--- a/docs/UNSLOTH_Q4.md+++ b/docs/UNSLOTH_Q4.md@@ -140,11 +140,17 @@ model's (`data/expert-profile.bin`); it ranks the same 48 x 512 experts and decibudget take first.**Choosing the RAM budget N:** your RAM minus 20-24 GB (the OS, the engine itself, the 126 MB router copy, and room for-the OS file cache that serves the rest). On 64 GB, 40. The engine clamps a budget larger than the RAM it finds free-(minus 4 GB and a 256 MiB margin) and says so; before #403's fix such a clamped budget could then fail the start. Everything above N comes from the SSD for every token, so N is the setting that matters+the OS file cache that serves the rest). On 64 GB, 40. The engine checks available RAM and, on Windows, available+commit capacity. If N exceeds the smaller value minus 4 GiB headroom, it reduces N to that amount minus a 256 MiB+margin and reports which limit applies. Before #403's fix a clamped budget could then fail the safety check.+Everything above N comes from the SSD for every token, so N is the setting that mattersmost; a bigger budget was faster in every measurement (24 / 32 / 40 GiB). When the driver page-locks the whole budget(24 GiB did on this PC, 32 and 40 did not), the GPU also computes a share of the misses over PCIe, as in the resident-low-RAM mode; `STRATA_RESIDENT_PIN=0` keeps the budget locked only (the CPU then computes every miss).+low-RAM mode; `STRATA_RESIDENT_PIN=0` uses ordinary pageable memory (the CPU then computes every miss).++On Windows, the allocation log reports available physical RAM and commit capacity before allocation and after a+failure. Free RAM alone does not guarantee that Windows can commit the requested memory. If the pageable fallback+also fails, the warning preserves both the GPU allocation or mapping error and the `malloc` error.**Context:** measured at 4K. The KV cache takes VRAM from the expert cache, so a longer context makes decoding slower;8K is a reasonable start on 12 GB. `--kv int8` halves the KV cache's VRAM.diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hppindex 1e6285ae4..af7d4db70 100644--- a/include/strata/core/expert_source.hpp+++ b/include/strata/core/expert_source.hpp@@ -72,6 +72,7 @@ bool cgroup_available_bytes(uint64_t limit, const CgroupMemoryStat& stat, uint64struct HostMemory {uint64_t available = 0;uint64_t cgroup_limit = ~uint64_t{0};+ uint64_t commit = ~uint64_t{0}; ///< available Windows commit capacity; ~0 when not reported};/// Linux reads `meminfo`, `self_cgroup` and the cgroup tree under `cgroup_root` (the parameters are for tests; the@@ -82,6 +83,10 @@ bool host_available_memory(HostMemory& m, const std::string& meminfo = "/proc/meconst std::string& self_cgroup = "/proc/self/cgroup",const std::string& cgroup_root = "/sys/fs/cgroup");+/// Bound a resident budget by RAM and commit capacity after headroom; leave 256 MiB more when clamping.+/// Pass UINT64_MAX for commit when the platform does not report it.+uint64_t clamp_resident_budget(uint64_t requested, uint64_t physical, uint64_t commit, uint64_t headroom);+/// Build compact offsets for experts absent from both the primary GPU cache and an optional second GPU tier./// Kept CPU-only so selection and byte accounting can be tested without initializing a GPU.bool make_cache_complement_plan(@@ -443,15 +448,16 @@ public:/// working set instead. `pin = false` is ordinary pageable memory (the ROCm arm: large pinned allocations/// can fail there, and it is what the HIP measurements used)./// - `lend_from_slot` >= 0: the GPU-cache slots from there to the end are the prompt path's lend region; their- /// experts are kept in RAM too, from the last slot down, as far as `available RAM - headroom_bytes` allows+ /// experts are kept in RAM too, from the last slot down, as far as available memory minus headroom allows/// (a lent slot's expert is streamed during the prompt and copied back after it)./// - the rest (the experts no slot holds) must fit that budget, or nothing is allocated and this returns false.////// CS-T, `budget_bytes` > 0 (`--resident-budget-gib`): only as many of those experts as fit `budget_bytes`, taken/// in `rank` order (the expert profile: the hottest after the GPU cache's), are copied; the rest stay on the/// mapped files (the SSD tier). No lend region then (a lent slot's expert is read from the files).- /// #467: `budget_bytes` = `kResidentWhatFits` is that path sized by the RAM alone (available minus the headroom- /// and the #403 margin) - the soft --resident-experts mode's second try when the whole complement does not fit;+ /// Available memory is limited by both RAM and commit capacity on Windows.+ /// #467: `budget_bytes` = `kResidentWhatFits` sizes that path from available memory minus headroom and the #403+ /// margin - the soft --resident-experts mode's second try when the whole complement does not fit;/// false when not even one expert fits. On Windows the mapped experts leave the working set before any reading.static constexpr uint64_t kResidentWhatFits = ~0ull;bool pin_cache_complement(diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cppindex f62ea07c8..f626c9da7 100644--- a/src/core/expert_source.cpp+++ b/src/core/expert_source.cpp@@ -17,6 +17,7 @@#include <algorithm>#include <atomic>+#include <cerrno>#include <chrono>#include <cstdio>#include <cstdlib>@@ -73,6 +74,14 @@ bool cgroup_available_bytes(uint64_t limit, const CgroupMemoryStat& stat, uint64return true;}+uint64_t clamp_resident_budget(uint64_t requested, uint64_t physical, uint64_t commit, uint64_t headroom) {+ const uint64_t available = std::min(physical, commit);+ const uint64_t room = available > headroom ? available - headroom : 0;+ if (requested <= room) return requested;+ const uint64_t margin = 256ull << 20;+ return room > margin ? room - margin : 0;+}+bool make_cache_complement_plan(int64_t n_layers, int64_t n_expert, const std::vector<uint64_t>& layer_blob_bytes,const std::vector<std::pair<int32_t, int32_t>>& primary_gpu_pairs,@@ -231,6 +240,7 @@ bool host_available_memory(HostMemory& m, const std::string& meminfo, const std:status.dwLength = sizeof(status);if (!GlobalMemoryStatusEx(&status)) return false;m.available = (uint64_t) status.ullAvailPhys;+ m.commit = (uint64_t) status.ullAvailPageFile;return m.available > 0;#elif defined(__linux__)// MemAvailable includes reclaimable page cache, unlike _SC_AVPHYS_PAGES.@@ -324,13 +334,33 @@ bool host_available_memory(HostMemory& m, const std::string& meminfo, const std:namespace {-bool available_memory_bytes(uint64_t& bytes) {+bool available_memory_bytes(uint64_t& bytes, uint64_t* commit = nullptr) {detail::HostMemory m;if (!detail::host_available_memory(m)) return false;bytes = m.available;+ if (commit != nullptr) *commit = m.commit;return bytes > 0;}+void log_complement_memory(const char* stage, uint64_t requested) {+#if defined(_WIN32)+ MEMORYSTATUSEX status{};+ status.dwLength = sizeof(status);+ if (!GlobalMemoryStatusEx(&status)) {+ std::fprintf(stderr, "FileExpertSource: %s: GlobalMemoryStatusEx failed (error %lu)\n", stage, GetLastError());+ return;+ }+ std::fprintf(stderr, "FileExpertSource: %s: requested %.2f GiB, available RAM %.2f GiB, available commit %.2f GiB "+ "(limit %.2f GiB), available virtual address space %.2f GiB\n",+ stage, (double) requested / 1073741824.0, (double) status.ullAvailPhys / 1073741824.0,+ (double) status.ullAvailPageFile / 1073741824.0, (double) status.ullTotalPageFile / 1073741824.0,+ (double) status.ullAvailVirtual / 1073741824.0);+#else+ (void) stage;+ (void) requested;+#endif+}+} // namespace// ================================ THE FILE-BACKED SOURCE ================================@@ -1383,32 +1413,33 @@ bool FileExpertSource::pin_cache_complement(}#endifconst bool what_fits = budget_bytes == kResidentWhatFits; // #467: the soft mode's second try- uint64_t budget_physical = 0; // #403: the RAM reading a budget was sized from (0: no budget)+ uint64_t budget_physical = 0; // #403: reuse the memory snapshot a budget was sized from+ uint64_t budget_commit = std::numeric_limits<uint64_t>::max();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.+ // they fit, the rest left on the mapped files; Windows commit capacity also limits the allocation (#730).uint64_t physical = 0;- if (!available_memory_bytes(physical)) {- err = "FileExpertSource: cannot determine available RAM for --resident-budget-gib";+ if (!available_memory_bytes(physical, &budget_commit)) {+ err = "FileExpertSource: cannot determine available memory for --resident-budget-gib";return false;}budget_physical = physical;- const uint64_t room = physical > headroom_bytes ? physical - headroom_bytes : 0;- if (budget_bytes > room) {+ const uint64_t available = std::min(physical, budget_commit);+ const char* limit = budget_commit < physical ? "commit" : "RAM";+ const uint64_t clamped = detail::clamp_resident_budget(budget_bytes, physical, budget_commit, headroom_bytes);+ if (budget_bytes > clamped) {// #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;if (what_fits)- std::fprintf(stderr, "FileExpertSource: RAM room for the complement: %.2f GiB (%.2f GiB available "+ std::fprintf(stderr, "FileExpertSource: room for the complement: %.2f GiB (%.2f GiB available %s ""minus %.0f GiB headroom and a 0.25 GiB margin)\n",- (double) clamped / 1073741824.0, (double) physical / 1073741824.0,+ (double) clamped / 1073741824.0, (double) available / 1073741824.0, limit,(double) headroom_bytes / 1073741824.0);else- std::fprintf(stderr, "FileExpertSource: --resident-budget-gib %.2f is more than the RAM has room for "- "(%.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,+ std::fprintf(stderr, "FileExpertSource: --resident-budget-gib %.2f exceeds available %s "+ "(%.2f GiB minus %.0f GiB headroom and a 0.25 GiB margin): %.2f GiB\n",+ (double) budget_bytes / 1073741824.0, limit, (double) available / 1073741824.0,(double) headroom_bytes / 1073741824.0, (double) clamped / 1073741824.0);budget_bytes = clamped;}@@ -1427,7 +1458,7 @@ bool FileExpertSource::pin_cache_complement(++held;}if (what_fits && held == 0) { // #467: nothing to keep - the caller's plain mmap fallback, not an empty copy- err = "FileExpertSource: the RAM has no room for any expert of the complement";+ err = "FileExpertSource: available memory cannot hold any expert of the complement";return false;}std::fprintf(stderr, "FileExpertSource: RAM budget %.2f GiB: %lld of the %.2f GiB of experts the GPU cache does "@@ -1444,18 +1475,20 @@ bool FileExpertSource::pin_cache_complement(// #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";+ uint64_t commit = budget_commit;+ if (physical == 0 && !available_memory_bytes(physical, &commit)) {+ err = "FileExpertSource: cannot determine available memory for the resident-memory safety check";return false;}- budget = physical > headroom_bytes ? physical - headroom_bytes : 0;+ const uint64_t available = std::min(physical, commit);+ budget = available > headroom_bytes ? available - headroom_bytes : 0;if (bytes > budget) {char message[320];std::snprintf(message, sizeof message,- "FileExpertSource: resident complement %.2f GiB exceeds available RAM (%.2f GiB) minus the "+ "FileExpertSource: resident complement %.2f GiB exceeds available %s (%.2f GiB) minus the ""%.0f GiB safety headroom",- (double) bytes / 1073741824.0, (double) physical / 1073741824.0,- (double) headroom_bytes / 1073741824.0);+ (double) bytes / 1073741824.0, commit < physical ? "commit" : "RAM",+ (double) available / 1073741824.0, (double) headroom_bytes / 1073741824.0);err = message;return false;}@@ -1498,6 +1531,7 @@ bool FileExpertSource::pin_cache_complement(if (bytes > 0) {std::fprintf(stderr, "FileExpertSource: allocating %.2f GiB %s cache complement\n",(double) bytes / 1073741824.0, pin ? "page-locked" : "pageable resident");+ log_complement_memory("before complement allocation", bytes);std::fflush(stderr);if (pin) {const cudaError_t allocated = cudaHostAlloc(&arena, (size_t) bytes,@@ -1524,9 +1558,19 @@ bool FileExpertSource::pin_cache_complement(}}if (arena == nullptr) {+ if (!note.empty()) {+ std::fprintf(stderr, "FileExpertSource: %s; trying pageable memory\n", note.c_str());+ log_complement_memory("before pageable allocation", bytes);+ std::fflush(stderr);+ }+ errno = 0;arena = std::malloc((size_t) bytes);if (arena == nullptr) {- err = "FileExpertSource: pageable resident complement allocation failed";+ const int allocation_errno = errno;+ err = "FileExpertSource: pageable resident complement allocation failed (malloc errno " ++ std::to_string(allocation_errno) + ": " + std::strerror(allocation_errno) + ")";+ if (!note.empty()) err += "; " + note;+ log_complement_memory("after pageable allocation failure", bytes);return false;}if (pin) {diff --git a/src/program/generate.cpp b/src/program/generate.cppindex 7be73397a..c7bf4b42a 100644--- a/src/program/generate.cpp+++ b/src/program/generate.cpp@@ -4876,7 +4876,7 @@ int main(int argc, char** argv) {std::string whole_err;if (!resident_ok && o.resident_soft) {// #467: the whole complement does not fit - keep what does, the hottest by the profile, through the #403- // budget path (sized by the RAM alone) instead of none: the misses outside it read the same file bytes+ // budget path (sized by available memory) instead of none: the misses outside it read the same file bytes// the mmap fallback reads, so the answers are unchanged. Nothing pinned: the old fallback below.whole_err = err;resident_ok = src.pin_cache_complement(xcache, err, o.resident_pin, stage_pairs, -1, o.resident_headroom,@@ -4919,8 +4919,9 @@ int main(int argc, char** argv) {// #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());+ "every expert the GPU does not hold is read from the model files %s "+ "(--mmap-experts), which is slower\n", err.c_str(),+ src.unbuffered() ? "with unbuffered I/O" : "through the OS file cache");} else {std::fprintf(stderr, "strata generate: CPU expert residency: %s\n", err.c_str());return 1;diff --git a/tests/core/file_expert_source_test.cpp b/tests/core/file_expert_source_test.cppindex f0b47c786..b0a0c3227 100644--- a/tests/core/file_expert_source_test.cpp+++ b/tests/core/file_expert_source_test.cpp@@ -256,6 +256,35 @@ void test_resident_exchange() {require(offsets == before, "exchanging back did not restore the plan");}+void test_resident_memory_budget() {+ using strata::core::detail::clamp_resident_budget;+ constexpr uint64_t GiB = 1ull << 30, margin = 256ull << 20, headroom = 4 * GiB;+ constexpr uint64_t unlimited = std::numeric_limits<uint64_t>::max();+ // #730: RAM can hold the requested cache, but Windows cannot commit it.+ require(clamp_resident_budget(66 * GiB, 69 * GiB, 47 * GiB, headroom) == 43 * GiB - margin,+ "a RAM budget exceeded available commit capacity");+ require(clamp_resident_budget(66 * GiB, 47 * GiB, 69 * GiB, headroom) == 43 * GiB - margin,+ "a RAM budget exceeded available physical memory");+ require(clamp_resident_budget(40 * GiB, 69 * GiB, 47 * GiB, headroom) == 40 * GiB,+ "a smaller explicit budget was changed");+ require(clamp_resident_budget(unlimited, 69 * GiB, 47 * GiB, headroom) == 43 * GiB - margin,+ "the what-fits budget ignored commit capacity");+ require(clamp_resident_budget(66 * GiB, 47 * GiB, unlimited, headroom) == 43 * GiB - margin,+ "a platform without a commit reading lost its RAM limit");+ require(clamp_resident_budget(43 * GiB, 69 * GiB, 47 * GiB, headroom) == 43 * GiB,+ "a budget that exactly fits received the clamping margin");+ require(clamp_resident_budget(66 * GiB, 69 * GiB, 0, headroom) == 0,+ "exhausted commit capacity was treated as unlimited");+ require(clamp_resident_budget(66 * GiB, 0, 69 * GiB, headroom) == 0,+ "exhausted RAM produced a positive budget");+ require(clamp_resident_budget(66 * GiB, 69 * GiB, headroom - 1, headroom) == 0 &&+ clamp_resident_budget(66 * GiB, 69 * GiB, headroom, headroom) == 0,+ "subtracting headroom underflowed");+ require(clamp_resident_budget(66 * GiB, 69 * GiB, headroom + margin - 1, headroom) == 0 &&+ clamp_resident_budget(66 * GiB, 69 * GiB, headroom + margin, headroom) == 0,+ "subtracting the clamping margin underflowed");+}+void test_cgroup_memory_budget() {using namespace strata::core::detail;constexpr uint64_t GiB = 1ull << 30;@@ -308,7 +337,10 @@ void test_host_memory() {root = (t.path / "fs").string();auto probe = [&](const std::string& self) {put("cgroup", self);- return host_available_memory(m, mi, cg, root);+ m.commit = 0;+ const bool ok = host_available_memory(m, mi, cg, root);+ require(m.commit == ~0ull, "Linux memory probe retained a commit limit");+ return ok;};// no cgroup line at all: MemAvailable alone (before #633: "cannot determine")require(probe("") && m.available == 100 * GiB && m.cgroup_limit == ~0ull, "no cgroup: MemAvailable alone");@@ -337,6 +369,11 @@ void test_host_memory() {require(probe("4:memory:/elsewhere\n") && m.available == 100 * GiB, "v1 group not mounted");#elserequire(host_available_memory(m) && m.available > 0 && m.cgroup_limit == ~0ull, "this PC's RAM");+#if defined(_WIN32)+ require(m.commit != ~0ull, "Windows memory probe did not report commit capacity");+#else+ require(m.commit == ~0ull, "a platform without commit accounting imposed a limit");+#endif(void) GiB;#endif}@@ -435,6 +472,7 @@ int main(int argc, char** argv) {test_complement_plan();test_resident_lend_region();test_resident_exchange();+ test_resident_memory_budget();test_cgroup_memory_budget();test_host_memory();test_canonical_layout();