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++
Expert cache: retry smaller when the allocation fails; setup warns about a tiny page file (#60)
Niko1221069b92b38dc5
diff --git a/docs/DETAILS.md b/docs/DETAILS.mdindex b823de76f..a1b1d1730 100644--- a/docs/DETAILS.md+++ b/docs/DETAILS.md@@ -435,6 +435,7 @@ the document, +0.4% on the chat. Details: `bench/results/2026-09-27-esp/`.| Python or the build tools could not be installed | Install what it names (links are printed), then run it again. Everything already done is kept. || `port 8080 is already in use` | Strata is already running (look for its window), or another program uses the port: `START-HERE.bat --port 8081`. || `cudaHostRegister ... out of memory` in the log | Normal on Windows: the engine pins the experts in per-layer slices instead. Only a problem if the load then fails. |+| `ExpertCache: cudaMalloc(...) failed: out of memory` although VRAM is free | Windows' page file is off or tiny: every allocation on the graphics card is also charged to Windows' commit (RAM + page file). Set the page file to "System managed" (System > About > Advanced system settings > Performance > Advanced > Virtual memory) and restart. Since 0.1.19 the engine retries with a smaller cache instead of stopping, and setup warns about a page file under 4 GB (issue #60). || The first start takes minutes | It is reading 34-55 GB into RAM; the second start is faster while the files are in the OS cache. || The PC freezes for a few minutes at the start | Normal, most of all the first time (the server window says when it happens): the engine loads the experts into RAM, pins part of it for the GPU and sizes the expert cache. Wait; don't close the window. Still frozen after 10 minutes: restart the PC, close other programs, try again, or pick a smaller size. || `the engine stopped unexpectedly (exit code ...)` | The engine process ended mid-answer - usually out of RAM (Linux ends the biggest program: `sudo dmesg \| grep -i -E 'killed process\|out of memory'`). The next request starts it again by itself. If it repeats: close other programs or pick a smaller size. The server also warns at start when the model's experts leave less than ~6 GB of RAM for everything else. |diff --git a/setup.py b/setup.pyindex 0f09c18e6..e79632ff5 100644--- a/setup.py+++ b/setup.py@@ -181,24 +181,39 @@ def mark(path: Path, text=""):# ------------------------------------------------------------------------------------------------ the PC+def _memory_status():+ """Windows' GlobalMemoryStatusEx: RAM, and the commit limit (ullTotalPageFile = RAM + page file)."""+ class MS(ctypes.Structure):+ _fields_ = [("dwLength", ctypes.c_ulong), ("dwMemoryLoad", ctypes.c_ulong),+ ("ullTotalPhys", ctypes.c_ulonglong), ("ullAvailPhys", ctypes.c_ulonglong),+ ("ullTotalPageFile", ctypes.c_ulonglong), ("ullAvailPageFile", ctypes.c_ulonglong),+ ("ullTotalVirtual", ctypes.c_ulonglong), ("ullAvailVirtual", ctypes.c_ulonglong),+ ("ullAvailExtendedVirtual", ctypes.c_ulonglong)]+ m = MS()+ m.dwLength = ctypes.sizeof(MS)+ ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(m))+ return m++def ram_gb():if WIN:- class MS(ctypes.Structure):- _fields_ = [("dwLength", ctypes.c_ulong), ("dwMemoryLoad", ctypes.c_ulong),- ("ullTotalPhys", ctypes.c_ulonglong), ("ullAvailPhys", ctypes.c_ulonglong),- ("ullTotalPageFile", ctypes.c_ulonglong), ("ullAvailPageFile", ctypes.c_ulonglong),- ("ullTotalVirtual", ctypes.c_ulonglong), ("ullAvailVirtual", ctypes.c_ulonglong),- ("ullAvailExtendedVirtual", ctypes.c_ulonglong)]- m = MS()- m.dwLength = ctypes.sizeof(MS)- ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(m))- return m.ullTotalPhys / 2**30+ return _memory_status().ullTotalPhys / 2**30for line in open("/proc/meminfo"):if line.startswith("MemTotal"):return int(line.split()[1]) * 1024 / 2**30return 0.0+def page_file_gb():+ """The page file's current size (GB) on Windows, None elsewhere. The graphics card's memory needs room there+ too: under Windows' driver model every allocation on the card is also charged to the commit (RAM + page file),+ so with the page file off or tiny the engine cannot use the free VRAM (issue #60)."""+ if not WIN:+ return None+ m = _memory_status()+ return max(0.0, (m.ullTotalPageFile - m.ullTotalPhys) / 2**30)++def cpu_info():"""(name, avx2, avx512): avx512 means everything Strata's fast AVX-512 kernels use (F, BW, VL, VNNI, VBMI),the same test the engine makes (cpu_avx512_ok), not just AVX-512F."""@@ -1101,6 +1116,11 @@ def main() -> int:"Strata keeps all of the model's experts in RAM (23-50 GB, whatever the GPU) and the GPU holds a copy ""of the most-used ones: it needs 32 GB of RAM or more (48 GB for the full model)")ok(f"RAM: {ram:.0f} GB" if ram >= need - 4 else f"RAM: {ram:.0f} GB (less than the {need} GB the smallest model needs)")+ pf = page_file_gb()+ if pf is not None and pf < 4:+ warn(f"Windows' page file is {pf:.1f} GB: the graphics card's memory needs room there too (issue #60), so "+ "the model may not start or may use less VRAM. Set it to \"System managed\": System > About > "+ "Advanced system settings > Performance > Advanced > Virtual memory")ok(f"CPU: {cpu} ({'AVX-512' if avx512 else 'AVX2' if avx2 else 'no AVX2'})")if not avx2:fail("this CPU has no AVX2; Strata needs at least AVX2")diff --git a/src/program/generate.cpp b/src/program/generate.cppindex 7a8c6eb47..417309ab2 100644--- a/src/program/generate.cpp+++ b/src/program/generate.cpp@@ -1503,20 +1503,73 @@ int main(int argc, char** argv) {o.expert_cache = (int) sized_slots.size();}if (o.expert_cache > 0) {+ // keep the first `keep_bytes` of the cache (the profile's hottest experts first); false when nothing is left+ auto shrink_to = [&](int64_t keep_bytes) -> bool {+ if (keep_bytes <= 0) { o.expert_cache = 0; sized_slots.clear(); return false; }+ if (!sized_slots.empty()) {+ int64_t used = 0;+ size_t keep = 0;+ while (keep < sized_slots.size() && used + (sized_slots[keep] + 255) / 256 * 256 <= keep_bytes)+ used += (sized_slots[keep++] + 255) / 256 * 256;+ sized_slots.resize(keep);+ o.expert_cache = (int) keep;+ } else {+ o.expert_cache = (int) (keep_bytes / (int64_t) strata::kernels::cpu::expert_layout().max_blob);+ }+ if (o.expert_cache <= 0) { o.expert_cache = 0; sized_slots.clear(); return false; }+ return true;+ };+ auto cache_bytes = [&]() -> int64_t {+ if (sized_slots.empty()) return (int64_t) o.expert_cache * (int64_t) strata::kernels::cpu::expert_layout().max_blob;+ int64_t b = 0;+ for (const int64_t s : sized_slots) b += (s + 255) / 256 * 256;+ return b;+ };// With `--expert-cache auto` the reserve must still be free once the slots are WRITTEN: under WDDM an// allocation is not resident until it is touched, and the free figure read before it can be ~1 GB too// high. A cache sized from it filled the card to 0 MiB, the driver then paged, and a request that needed a// page back while the verify graph spun on a host flag never finished. So the slots are zeroed and the// free figure read again; while it is short of the reserve the cache is reopened smaller.+ // STRATA_TEST_CACHE_FAIL=N: the first N opens fail as an out-of-commit cudaMalloc does (tests the retry)+ int fake_fails = std::getenv("STRATA_TEST_CACHE_FAIL") ? std::atoi(std::getenv("STRATA_TEST_CACHE_FAIL")) : 0;+ int failed = 0;for (int attempt = 0;; ++attempt) {- const bool ok = sized_slots.empty()- ? xcache.open(o.expert_cache, g.n_layers, g.n_expert, (int64_t) strata::kernels::cpu::expert_layout().max_blob, err)- : xcache.open_sized(sized_slots, g.n_layers, g.n_expert, err);+ bool ok = false;+ if (fake_fails > 0) {+ --fake_fails;+ err = "ExpertCache: cudaMalloc failed: out of memory (STRATA_TEST_CACHE_FAIL)";+ } else {+ ok = sized_slots.empty()+ ? xcache.open(o.expert_cache, g.n_layers, g.n_expert, (int64_t) strata::kernels::cpu::expert_layout().max_blob, err)+ : xcache.open_sized(sized_slots, g.n_layers, g.n_expert, err);+ }if (!ok) {+ // Issue #60: on Windows a device allocation is also charged to the system commit (RAM + page file),+ // so with a small page file the cache's one big cudaMalloc fails while the VRAM is free. An auto+ // cache then tries three quarters of the size, a few times, instead of stopping the engine.+ char commit[96] = "";+#if defined(_WIN32)+ MEMORYSTATUSEX ms{};+ ms.dwLength = sizeof ms;+ if (GlobalMemoryStatusEx(&ms))+ std::snprintf(commit, sizeof commit, " (Windows has %.1f GiB of commit left: RAM + page file)",+ (double) ms.ullAvailPageFile / 1073741824.0);+#endif+ if (auto_cache && failed < 8 && shrink_to(cache_bytes() / 4 * 3)) {+ ++failed;+ std::fprintf(stderr, "strata generate: %s%s; trying a smaller expert cache: %d slots\n", err.c_str(),+ commit, o.expert_cache);+ continue;+ }std::fprintf(stderr, "strata generate: %s\n", err.c_str());+#if defined(_WIN32)+ std::fprintf(stderr, "strata generate: on Windows the graphics card's memory also needs room in the page "+ "file: set it to \"System managed\" (System > About > Advanced system settings > "+ "Performance > Advanced > Virtual memory), or lower --expert-cache\n");+#endifreturn 1;}- if (!auto_cache || attempt >= 6) break;+ if (!auto_cache || attempt - failed >= 6) break;cudaMemset(xcache.device_slot(0), 0, (size_t) xcache.bytes());cudaDeviceSynchronize();size_t free_b = 0, total_b = 0;@@ -1530,19 +1583,12 @@ int main(int argc, char** argv) {std::fprintf(stderr, "strata generate: only %lld MiB free once the slots are written (reserve %d MiB); ""shrinking the expert cache\n", (long long) (free_b >> 20), o.vram_reserve_mib);xcache.close();- if (keep_bytes <= 0) { o.expert_cache = 0; sized_slots.clear(); break; }- if (!sized_slots.empty()) {- int64_t used = 0;- size_t keep = 0;- while (keep < sized_slots.size() && used + (sized_slots[keep] + 255) / 256 * 256 <= keep_bytes)- used += (sized_slots[keep++] + 255) / 256 * 256;- sized_slots.resize(keep);- o.expert_cache = (int) keep;- } else {- o.expert_cache = (int) (keep_bytes / (int64_t) strata::kernels::cpu::expert_layout().max_blob);- }- if (o.expert_cache <= 0) { o.expert_cache = 0; sized_slots.clear(); break; }+ if (!shrink_to(keep_bytes)) break;}+ if (failed > 0 && o.expert_cache > 0)+ std::fprintf(stderr, "strata generate: expert cache: %d slots (%.2f GiB) after %d smaller tries - a bigger "+ "page file lets it use more of the free VRAM\n",+ o.expert_cache, (double) xcache.bytes() / 1073741824.0, failed);}if (o.expert_cache > 0) {std::fprintf(stderr, "strata generate: expert cache %lld slots, %.2f GiB of VRAM; policy is\n",