From 502c7296c94d42207196fb2b038ea67b07b89d72 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 3 Oct 2026 18:12:58 +0200 Subject: [PATCH] Load the native head and the logits before the expert arena; name the free VRAM when they fail (#620) The native head's cudaMalloc ran after the expert arena had registered ~45 GiB of host pages. WDDM then refused it on a 16 GB card with the desktop on the iGPU ("native head upload: out of memory" with GiBs free) - the drafter is loaded before the arena for the same reason. Now the head and the logits buffer load right after the drafter, still before the expert cache is sized, so the slots are the same. The head's, the drafter's and a split stage's head failures print "(N MiB of M MiB VRAM free on this GPU)". setup: a context of 64K or more whose KV streaming does not fit the RAM test now says so (it was silent, and a regenerated config lost --kv-resident without a word). --- setup.py | 4 +++ src/program/generate.cpp | 69 +++++++++++++++++++++++++--------------- 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/setup.py b/setup.py index bc5cdd30..c1990b7d 100644 --- a/setup.py +++ b/setup.py @@ -3597,6 +3597,10 @@ def main() -> int: elif budget is not None and budget > resident_budget_gib(model, ram, kv_ram_gb): warn(f"the KV cache's {kv_ram_gb:.1f} GB of RAM come on top of your {budget:g} GiB RAM budget (setup " f"would take them out of it: {resident_budget_gib(model, ram, kv_ram_gb)} GiB); kept as you chose") + elif ctx >= 65536: # #620: say why, so a regenerated config that lost --kv-resident is not a surprise + ok(f"KV streaming off: it needs ~{kv_ram_gb:.1f} GB of RAM beside the ~{MODELS[model]['ram_gb']} GB {model} " + f"uses, and this PC has {ram:.0f}; the KV cache stays in VRAM (fewer cached experts). --kv-streaming on " + "turns it on anyway") elif a.kv_streaming == "on": warn("--kv-streaming on: a context under 64K is not streamed (the attention's window holds all of it): off") if budget is not None and not q4_split: # UD-Q4_K_XL: the experts read from the GGUF in place, the most-used N diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 1e5988a3..cb991841 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -113,6 +113,20 @@ bool under_wddm() { #endif } +// #620 #486: " (N MiB of M MiB VRAM free on this GPU)" for an allocation's failure message (clears the error first) +std::string vram_free_note() { + (void) cudaGetLastError(); + size_t free_b = 0, total_b = 0; + if (cudaMemGetInfo(&free_b, &total_b) != cudaSuccess) { + (void) cudaGetLastError(); + return {}; + } + char buf[96]; + std::snprintf(buf, sizeof buf, " (%llu MiB of %llu MiB VRAM free on this GPU)", (unsigned long long) (free_b >> 20), + (unsigned long long) (total_b >> 20)); + return buf; +} + // perf-review D-4: the lent slots are refilled with queued copies and one wait; STRATA_REFILL_BLOCKING=1 waits on each bool refill_blocking() { static const bool v = std::getenv("STRATA_REFILL_BLOCKING") != nullptr; @@ -2637,8 +2651,9 @@ int main(int argc, char** argv) { const strata::core::WeightRef* wo_s = st.wt.find("output.weight"); if (wo_s == nullptr || (last && !o.native_head_gguf.empty() && !st.head.load(o.native_head_shards, g.n_embd, wo_s->ne1, err))) { - std::fprintf(stderr, "strata generate: layer split, CUDA%d head: %s\n", st.dev, - wo_s == nullptr ? "output.weight is missing" : err.c_str()); + std::fprintf(stderr, "strata generate: layer split, CUDA%d head: %s%s\n", st.dev, + wo_s == nullptr ? "output.weight is missing" : err.c_str(), + wo_s == nullptr ? "" : vram_free_note().c_str()); return 1; } size_t fb = 0, tb = 0; @@ -2662,7 +2677,32 @@ int main(int argc, char** argv) { static const strata::core::ModelGeometry draft_geometry{}; // with a layer split across GPUs the drafter reads the last stage's residual: it lives on that device const strata::core::OnDevice on_mtp(last_st ? last_st->dev : -1); - if (!o.mtp.empty() && !mtp.load(o.mtp, draft_geometry, last_st ? last_st->ss : ss, o.spec, err, o.mtp_window)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } + if (!o.mtp.empty() && !mtp.load(o.mtp, draft_geometry, last_st ? last_st->ss : ss, o.spec, err, o.mtp_window)) { std::fprintf(stderr, "strata generate: %s%s\n", err.c_str(), vram_free_note().c_str()); return 1; } + } + // THE HEAD BEFORE THE CACHE, AND BEFORE THE ARENA. The expert cache takes what is free minus the reserve, so + // everything allocated after it comes out of the reserve. The native head (~0.5 GB with IQ3_S) was loaded after + // it and ate most of the 700 MiB: 128K IQ3_S ended with 30 MiB free, the driver paged, and a request stalled for + // good at its first verify window. Loaded first, the cache is sized around it. #620: and before the expert + // arena registers tens of GiB of host pages, like the drafter above - WDDM then refused the head's cudaMalloc on + // a 16 GB card with the desktop on the iGPU ("native head upload: out of memory" with GiBs free). + const strata::core::WeightRef* wo = wt.find("output.weight"); + if (wo == nullptr) { std::fprintf(stderr, "strata generate: output.weight is missing\n"); return 1; } + const int64_t n_vocab = wo->ne1; + strata::core::NativeHead native_head; + if (!o.native_head_gguf.empty() && !multi_gpu) { // a layer split's head is on its last stage + if (!native_head.load(o.native_head_shards, g.n_embd, n_vocab, err)) { + std::fprintf(stderr, "strata generate: %s%s\n", err.c_str(), vram_free_note().c_str()); + return 1; + } + std::fprintf(stderr, "strata generate: experimental native Q5_K head, %llu bytes\n", + (unsigned long long) native_head.weight_bytes()); + } + std::vector logits((size_t) n_vocab); + float* d_logits = nullptr; + if (cudaMalloc(&d_logits, (size_t) n_vocab * 4) != cudaSuccess) { + (void) cudaGetLastError(); + std::fprintf(stderr, "strata generate: the logits buffer failed%s\n", vram_free_note().c_str()); + return 1; } // Create the additional contexts after MTP has secured CUDA0 memory, but // before the host arena maps its expert pages into their address spaces. @@ -2819,28 +2859,7 @@ int main(int argc, char** argv) { // numbers if the slots do not fit, instead of handing back a cache smaller than it was asked for. mem_mark("the weights, the session and the drafter"); strata::core::ExpertCache xcache; - // THE HEAD BEFORE THE CACHE. The expert cache takes what is free minus the reserve, so everything allocated - // after it comes out of the reserve. The native head (~0.5 GB with IQ3_S) was loaded after it and ate most of - // the 700 MiB: 128K IQ3_S ended with 30 MiB free, the driver paged, and a request stalled for good at its first - // verify window. Loaded first, the cache is sized around it. - const strata::core::WeightRef* wo = wt.find("output.weight"); - if (wo == nullptr) { std::fprintf(stderr, "strata generate: output.weight is missing\n"); return 1; } - const int64_t n_vocab = wo->ne1; - strata::core::NativeHead native_head; - if (!o.native_head_gguf.empty() && !multi_gpu) { // a layer split's head is on its last stage - if (!native_head.load(o.native_head_shards, g.n_embd, n_vocab, err)) { - std::fprintf(stderr, "strata generate: %s\n", err.c_str()); - return 1; - } - std::fprintf(stderr, "strata generate: experimental native Q5_K head, %llu bytes\n", - (unsigned long long) native_head.weight_bytes()); - } - std::vector logits((size_t) n_vocab); - float* d_logits = nullptr; - if (cudaMalloc(&d_logits, (size_t) n_vocab * 4) != cudaSuccess) { - std::fprintf(stderr, "strata generate: the logits buffer failed\n"); - return 1; - } + // THE HEAD BEFORE THE CACHE: the native head and the logits are allocated above, before the expert arena (#620) const bool auto_cache = o.expert_cache < 0; bool reserve_adapted = false; // #496: the auto sizing lowered the reserve so a small card's cache fits if (o.expert_cache < 0) {