diff --git a/engine/src/ling32/ling32_engine.cu b/engine/src/ling32/ling32_engine.cu index 3d20bae4d6..b846ed1add 100644 --- a/engine/src/ling32/ling32_engine.cu +++ b/engine/src/ling32/ling32_engine.cu @@ -483,13 +483,27 @@ struct Ling32Engine::StageCtx { if (E.provider_) E.provider_->grow_bytes(~static_cast(0)); } + // lazy KV / scratch regrow: before an allocation, make room in the pool if the card has less than BYTES + a margin free (STRATA_L32_ALLOC_MARGIN_MB, default 256 MiB): the store releases whole chunks of pool + // slots for the shortfall. Waiting for cudaMalloc to fail is not enough where the driver over-commits (Windows WDDM pages the allocation to shared system memory instead of failing, and the kernels then + // read it over PCIe: a 33K-token prompt took 600 s instead of 100 s on a 12 GB card); the failure retry below stays as the backstop. + void make_room(size_t bytes) { + if (!E.provider_ || E.opt.kv_init_ctx <= 0) return; + static const size_t margin = [] { const char* e = std::getenv("STRATA_L32_ALLOC_MARGIN_MB"); return static_cast(e && e[0] ? std::max(0, std::atoi(e)) : 256) * 1048576ull; }(); + for (int t = 0; t < 64; ++t) { + size_t fr = 0, tot = 0; + if (cudaMemGetInfo(&fr, &tot) != cudaSuccess) { (void)cudaGetLastError(); return; } + if (fr >= bytes + margin) return; + if (E.provider_->release_bytes(bytes + margin - fr) == 0) return; + } + } + // one cudaMalloc; with a tier store attached (lazy KV / scratch regrow) a failure makes the store release whole chunks of pool slots and the allocation is tried again bool alloc_bytes(void** pp, size_t bytes, const char* what, std::string& err) { if (std::getenv("STRATA_L32_TRACE")) std::fprintf(stderr, "ling32: alloc %.1f MiB\n", bytes / 1048576.0); static const bool inject = [] { const char* f = std::getenv("STRATA_L32_LAZY_OOM"); return f && f[0] == '1'; }(); // test hook: the first allocation after the store is attached fails once, so the release-and-retry path runs cudaError_t e = cudaSuccess; if (inject && E.provider_ && E.opt.kv_init_ctx > 0 && !oom_injected) { oom_injected = true; e = cudaErrorMemoryAllocation; } - else e = cudaMalloc(pp, bytes); + else { make_room(bytes); e = cudaMalloc(pp, bytes); } // lazy latent KV: the tier store gave all the room the cache did not need to its pool; a later allocation (the prompt span's buffers, the prefill scratch, ...) takes chunks of pool slots back instead of failing for (int t = 0; e == cudaErrorMemoryAllocation && E.provider_ && E.opt.kv_init_ctx > 0 && t < 64; ++t) { (void)cudaGetLastError(); @@ -705,6 +719,7 @@ struct Ling32Engine::StageCtx { for (size_t il = 0; il < cache.size(); ++il) { if (!cache[il] || kv_cap_l[il] >= ncap) continue; void* nb = nullptr; + make_room(nbytes); cudaError_t e = cudaMalloc(&nb, nbytes); for (int t = 0; e != cudaSuccess && E.provider_ && t < 64; ++t) { (void)cudaGetLastError(); diff --git a/terminal/data/settings-register.json b/terminal/data/settings-register.json index c9969c85ea..49e648648a 100644 --- a/terminal/data/settings-register.json +++ b/terminal/data/settings-register.json @@ -32236,6 +32236,39 @@ "retire_after": "", "retired_in": "" }, + { + "name": "STRATA_L32_ALLOC_MARGIN_MB", + "backends": [ + "cuda-hip" + ], + "reads": [ + { + "file": "engine/src/ling32/ling32_engine.cu", + "line": 491, + "backend": "cuda-hip", + "fn": "getenv" + } + ], + "parse": "int", + "default_state": null, + "code_default": "256", + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [], + "first_commit": "d7b86c64b5", + "first_date": "2026-12-03", + "since": null, + "last_commit": "d7b86c64b5", + "last_date": "2026-12-03", + "status": "research", + "owner": "L32", + "note": "MiB of the card the lazy KV / scratch regrow keeps free: before a big allocation the store releases pool chunks when free memory is below the size plus this margin (default 256; Windows WDDM over-commits instead of failing; L32-SMALL)", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_L32_BIGSLOTS", "backends": [