From d7b86c64b53bb8dc8f04fc0a9251228021071438 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Thu, 3 Dec 2026 05:08:46 +0100 Subject: [PATCH 1/2] ling32: make room in the tier pool before a big allocation instead of waiting for cudaMalloc to fail (lazy KV growth, scratch regrow; STRATA_L32_ALLOC_MARGIN_MB) The release-and-retry path of the lazy KV and the scratch regrow relied on cudaMalloc failing when the card is full. Where the driver over-commits (Windows WDDM pages the allocation into shared system memory instead of failing) the release never ran: on a 12 GB RTX 5070 in tier C at chunk 2048 the big prefill scratch was paged, the pool stayed at its full size, and a 33,784-token prompt took 622 s with regrow on against 99 s with it off. make_room(bytes) asks cudaMemGetInfo before the allocation and, when free < bytes + margin (STRATA_L32_ALLOC_MARGIN_MB, default 256), makes the store release whole chunks of pool slots for the shortfall. It runs before every allocation of the engine's stage context once a tier store is attached (alloc_bytes) and before each latent-cache growth (grow_kv); the failure retry stays as the backstop. Without a tier store or without lazy KV nothing changes. --- engine/src/ling32/ling32_engine.cu | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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(); From cd3fbe4c60ff5ebb31ad2a0386acd5086ed3c08c Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Thu, 3 Dec 2026 05:09:21 +0100 Subject: [PATCH 2/2] settings register: STRATA_L32_ALLOC_MARGIN_MB (research) --- terminal/data/settings-register.json | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/terminal/data/settings-register.json b/terminal/data/settings-register.json index c15c0fe7f2..1479297730 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": [