Expert cache: retry smaller when the allocation fails; setup warns about a tiny page file (#60)

On Windows every allocation on the graphics card is also charged to the system commit (RAM + page file).
With the page file off or tiny, the expert cache's one big cudaMalloc failed while the VRAM was free and
the engine stopped. An auto-sized cache now retries at three quarters of the size (up to 8 times) and
says how much commit Windows has left; a failure that remains names the page-file setting. Setup warns
when the page file is under 4 GB; DETAILS has a troubleshooting row. STRATA_TEST_CACHE_FAIL=N fakes N
failed opens to test the retry.
This commit is contained in:
Niko1221 committed 2026-09-28 17:11:57 +02:00
1 parent de71982be9
commit 069b92b38d
3 files changed
+93 -26

No files matched your search

+62 -16
View File
@@ -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");
#endif
return 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",