From e68b8d1445fee67e036afed828b7f0dbcf4850f8 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 2 Oct 2026 22:11:44 +0200 Subject: [PATCH] A 6 GB card starts with a small expert cache, or says what is short (#496) On an RTX 3060 Laptop (6 GB) the weights, the KV cache and the draft layer left 0.29 GiB, the default 700 MiB reserve and the draft head took it, the expert cache got 0 slots and the verify window refused to start. - setup: a single card under 8 GB (nvidia-smi below 7.5 GB; an 8 GB card lists 7.99) gets --vram-reserve-mib 300 unless one is given (images on the GPU keep theirs), marked in the config as setup's own choice so a later setup decides again. It prints what frees more VRAM if the start still stops: an 8K context, --draft-vocab en, other programs closed. The draft layer stays (the server needs --mtp and --spec). 8 GB and larger cards: the same config (golden test). - engine: a cache too small to lend the prompt path a 256-token chunk (its buffers plus the 128-slot floor; one slot without --prefill) makes the prompt path allocate its own on top. When the default reserve leaves less than that, the reserve shrinks to what leaves exactly that cache, down to 300 MiB (what comes after the cache is paid from the reserve: at 300 an emulated small card ended with 5 MiB free, LOW by the serve check). With no slot at all the start stops with how many MiB are short, what the session and the draft head take, and the options. A start whose cache could lend that chunk is sized as before. Checked with a VRAM ballast on the 5070 (IQ3_XXS, 32K): 258 slots needed, 0.71 GiB free -> "about 333 MiB more", and with --vram-reserve-mib 700 given, "733 MiB more" plus the reserve tip. - verify's refusal points at that line. --- docs/INSTALL.md | 4 +++ setup.py | 42 ++++++++++++++++++++++++++- src/core/verify.cpp | 4 ++- src/program/generate.cpp | 57 +++++++++++++++++++++++++++++++++---- tools/test_setup_choices.py | 47 ++++++++++++++++++++++++++++++ 5 files changed, 147 insertions(+), 7 deletions(-) diff --git a/docs/INSTALL.md b/docs/INSTALL.md index 051f7bec..5e051451 100644 --- a/docs/INSTALL.md +++ b/docs/INSTALL.md @@ -201,6 +201,10 @@ By hand: add `"--vram-reserve-mib", "2048"` to the config's `"args"` list and re much smaller, so answers can be a little slower. The engine sizes its cache from the VRAM free when it starts: what another program already holds then is left alone anyway; the reserve is room for what it takes later. +**A card under 8 GB (#496):** setup writes a 300 MiB reserve there, and the engine itself tries 300 when the default +leaves the expert cache no slot. If the start still stops with "no VRAM is left for the expert cache", that log line +says how much is short; an 8K context and `--draft-vocab en` at setup free the most. + **Model files downloaded by hand, or from a mirror (#495):** setup's step 5 prints the folder it expects them in (`Strata-data\models\\`, e.g. `Strata-data\models\IQ3_XXS\`): put them there with their original names, or point setup at them with `--gguf-dir`. To let setup download from a Hugging Face mirror itself, set `HF_ENDPOINT` diff --git a/setup.py b/setup.py index 98bc56ab..8075f498 100644 --- a/setup.py +++ b/setup.py @@ -2416,8 +2416,10 @@ def choices_from_config(cfg_path: Path) -> dict: "host": cfg.get("host"), "api_key": cfg.get("api_key"), "port": cfg.get("port"), "gpu": cfg.get("gpu"), "layer_split": cfg.get("layer_split"), # #493: --vram-reserve-mib given at setup (images write the default 700 themselves) + # (#496: not the small card's reserve setup chose itself) "vram_reserve_mib": int(val("--vram-reserve-mib")) if (val("--vram-reserve-mib") or "").isdigit() and ( - vis is None or int(val("--vram-reserve-mib")) != VISION["gpu"]["reserve_mib"]) else None} + vis is None or int(val("--vram-reserve-mib")) != VISION["gpu"]["reserve_mib"]) and + not cfg.get("vram_reserve_auto") else None} def find_in(roots: list, rel: str): @@ -2724,6 +2726,29 @@ def draft_vocab_note(vram_gb: float, chosen: str | None) -> list[str]: " fix when the start stops with \"the draft head does not fit\". The model keeps the choice."] +SMALL_CARD_GB = 7.5 # #496: a card under 8 GB gets a smaller VRAM reserve (an 8 GB card lists 7.99) +SMALL_CARD_RESERVE_MIB = 300 + + +def small_card_note(ctx: int, draft_vocab: str | None) -> list[str]: + """#496: what frees VRAM on a card under 8 GB when the start still stops with "no VRAM is left for the expert + cache" - a recommendation, setup changes none of it. (The draft layer stays: the server needs it.)""" + start = "START-HERE.bat --setup" if WIN else "./setup.sh" + tips = [] + if ctx > 8192: + tips.append("an 8K context (a smaller KV cache)") + if draft_vocab != "en": + tips.append(f"--draft-vocab en (a draft head of ~{DRAFT_VOCAB_MIB['en']} MiB instead of " + f"~{DRAFT_VOCAB_MIB[draft_vocab or 'cjk']})") + lines = ["If the start stops with \"no VRAM is left for the expert cache\" (the engine's log says how much is " + "short):"] + if tips: + lines.append(f" run {start} again with " + " and ".join(tips) + ", or close other programs that use the GPU.") + else: + lines.append(" close other programs that use the GPU.") + return lines + + def mtp_corrupt(mtp: Path, env=None) -> bool: """#327: True when the MTP tensors an install fetched are not the pinned checkpoint's (tools/mtp_fetch.py verify, which hashes only files that changed since they last checked out). A mirror that ignored range requests left the @@ -3528,6 +3553,19 @@ def main() -> int: args += ["--vram-reserve-mib", str(a.vram_reserve_mib)] ok(f"VRAM kept free for other programs: {a.vram_reserve_mib} MiB (--vram-reserve-mib; the expert cache takes " "that much less)") + small_reserve = (a.vram_reserve_mib is None and not multi and vision != "gpu" and + 0 < gpu.get("vram_gb", 0.0) < SMALL_CARD_GB) + if small_reserve: + # #496: on a 6 GB card the engine's default 700 MiB reserve (plus the draft head) took all that the weights, + # the KV cache and the draft layer left, and the expert cache got no slot. A card under 8 GB gets 300 MiB. + if "--vram-reserve-mib" in args: + args[args.index("--vram-reserve-mib") + 1] = str(SMALL_CARD_RESERVE_MIB) + else: + args += ["--vram-reserve-mib", str(SMALL_CARD_RESERVE_MIB)] + ok(f"a {gpu['vram_gb']:.0f} GB card: {SMALL_CARD_RESERVE_MIB} MiB of VRAM kept free instead of 700, so the " + "expert cache gets the rest (--vram-reserve-mib N changes it)") + for line in small_card_note(ctx, draft_vocab): # a recommendation: nothing else changes + say(" " + line) if esp is not None: # the package's profile, with llama.cpp's flags (the engine takes the same ones) args += ["--control-vector-scaled", f"{esp}:1.0", "--control-vector-layer-range", "4", "44", @@ -3535,6 +3573,8 @@ def main() -> int: cfg = {"exe": str(eng / EXE), "args": args, "cwd": str(ROOT), "tokenizer": str(pack / "tokenizer"), "model_name": f"{fam['name']}-{model.lower()}", "log": str(ROOT / f"strata-{tag.lower()}.log"), "lib_dirs": lib_dirs, "port": port} + if small_reserve: # #496: setup's own choice - a later setup decides again instead of keeping it as the user's + cfg["vram_reserve_auto"] = True if hip: cfg["backend"] = "hip" # the dense prompt GEMMs through hipBLASLt with kernels measured on this GPU generation (tools/hip; +40-60% diff --git a/src/core/verify.cpp b/src/core/verify.cpp index ade718a0..f956d731 100644 --- a/src/core/verify.cpp +++ b/src/core/verify.cpp @@ -211,7 +211,9 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState& } if (hits.d_res == nullptr || hits.cache_base == nullptr || hits.blob <= 0) { err = "verify: needs the profile-filled VRAM expert tier (--expert-profile and --expert-cache); with " - "--expert-cache auto, no VRAM was left for it: lower --max-context, use --kv k8v4 or images on the CPU"; + "--expert-cache auto, no VRAM was left for it - the 'no VRAM is left for the expert cache' line above " + "says how much is short and what makes room (a smaller --max-context, --kv q4_0, setup --draft-vocab en, " + "images on the CPU)"; return false; } std::string why; diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 0b618bd7..d1d36cad 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -339,6 +339,7 @@ struct Options { /// Plan v0.3 P4: `--expert-cache auto` sizes the VRAM tier from what is free after the weights, the session /// and the KV state, minus this reserve for the graphs, the hit scratch and the head. int vram_reserve_mib = 700; + bool vram_reserve_given = false; ///< --vram-reserve-mib on the command line (#496: no smaller automatic reserve) /// Plan v0.3 P5: batched prompt processing in chunks of this many tokens (0 = the token path). int64_t prefill_chunk = 0; /// `--prefill auto`: the largest chunk (up to 8192) whose buffers the expert cache can lend. Every expert a chunk @@ -1191,7 +1192,7 @@ int main(int argc, char** argv) { else if (a == "--expert-cache-device3") o.expert_cache_remote[2] = std::atoi(next("--expert-cache-device3")); else if (a == "--expert-cache-remote-placement") o.expert_cache_remote_placement = next("--expert-cache-remote-placement"); - else if (a == "--vram-reserve-mib") o.vram_reserve_mib = std::atoi(next("--vram-reserve-mib")); + else if (a == "--vram-reserve-mib") { o.vram_reserve_mib = std::atoi(next("--vram-reserve-mib")); o.vram_reserve_given = true; } else if (a == "--prefill") { const std::string v = next("--prefill"); o.prefill_auto = v == "auto" || v.rfind("auto:", 0) == 0; @@ -2823,15 +2824,61 @@ int main(int argc, char** argv) { const int64_t mtp_bind = (!o.mtp.empty() && native_head.loaded()) ? (int64_t) mtp.bind_bytes(native_head.row_bytes(), n_vocab) : 0; const int64_t reserve = (((int64_t) o.vram_reserve_mib + prefill_mib) << 20) + mtp_bind; - int64_t slots = ((int64_t) free_b - reserve) / (int64_t) strata::kernels::cpu::expert_layout().max_blob; + const int64_t blob = (int64_t) strata::kernels::cpu::expert_layout().max_blob; + int64_t slots = ((int64_t) free_b - reserve) / blob; if (!profile.empty()) slots = std::min(slots, (int64_t) profile.size()); o.expert_cache = (int) std::max(slots, 0); std::fprintf(stderr, "strata generate: expert cache auto: %.2f GiB free, %d MiB reserved (+%lld MiB for the " "draft head) -> %d slots\n", (double) free_b / 1073741824.0, o.vram_reserve_mib, (long long) (mtp_bind >> 20), o.expert_cache); - if (o.expert_cache == 0) // the verify window cannot start without it (#174): say what makes room - std::fprintf(stderr, "strata generate: no VRAM is left for the expert cache: lower --max-context, use " - "--kv k8v4, run images on the CPU, or close other programs that use the GPU\n"); + // #496: the verify window cannot start without a cache (#174), and a cache too small to lend the prompt path + // a 256-token chunk's buffers (plus the 128 slots a loan leaves; one slot without --prefill) makes it + // allocate its own on top - more than the reserve. When the default reserve leaves less than that (a 6 GB + // card), the reserve shrinks to what leaves exactly that cache, down to kSmallReserveMib (what setup writes for + // cards under 8 GB): what is allocated after the cache - the prompt path's own part, the verify buffers, the + // draft head - comes out of the reserve, and below ~550 MiB a card ends with less than the 256 MiB the serve + // check calls LOW (IQ3_XXS, 32K, a 300 MiB reserve: 5 MiB left), so the cache gets no more than it needs. A + // reserve given on the command line is kept. No slot at all: the start stops, saying what is short and what + // makes room. A card the default reserve leaves that much is sized as before. + constexpr int kSmallReserveMib = 300; + const int64_t min_slots = (o.prefill_chunk > 0 && pf_borrow) + ? ((int64_t) strata::prefill::Prefill::bytes_needed(g, ss, 256) + blob - 1) / blob + 128 : 1; + if (o.expert_cache < min_slots && !o.vram_reserve_given && o.vram_reserve_mib > kSmallReserveMib) { + // the largest reserve (in MiB) that still leaves min_slots + const int64_t fit_mib = ((int64_t) free_b - mtp_bind - min_slots * blob) / (1 << 20) - prefill_mib; + if (fit_mib >= kSmallReserveMib) { + const int r = (int) std::min(fit_mib, o.vram_reserve_mib); + int64_t s2 = ((int64_t) free_b - ((((int64_t) r + prefill_mib) << 20) + mtp_bind)) / blob; + if (!profile.empty()) s2 = std::min(s2, (int64_t) profile.size()); + std::fprintf(stderr, "strata generate: expert cache auto: the %d MiB reserve leaves too few slots on " + "this card (a working cache needs %lld): a %d MiB reserve instead -> %lld slots\n", + o.vram_reserve_mib, (long long) min_slots, r, (long long) s2); + o.vram_reserve_mib = r; + o.expert_cache = (int) s2; + } + } + if (o.expert_cache == 0) { + // what is short, and what makes room: the numbers a small card picks from + const int64_t at_reserve = o.vram_reserve_given ? o.vram_reserve_mib + : std::min(o.vram_reserve_mib, kSmallReserveMib); + const int64_t need_b = (((int64_t) at_reserve + prefill_mib) << 20) + mtp_bind + min_slots * blob; + const int64_t short_mib = std::max(1, (need_b - (int64_t) free_b + (1 << 20) - 1) >> 20); + const int64_t session_mib = + (int64_t) (strata::core::session_bytes(g, o.max_context, K, 0, g.n_layers) >> 20); + const std::string reserve_tip = + o.vram_reserve_given && o.vram_reserve_mib > kSmallReserveMib + ? ", a smaller --vram-reserve-mib (" + std::to_string(o.vram_reserve_mib) + " now; " + + std::to_string(kSmallReserveMib) + " is enough on a small card)" + : std::string(); + std::fprintf(stderr, "strata generate: no VRAM is left for the expert cache: it needs at least %lld slots " + "(%lld MiB), about %lld MiB more than this card has free. To make room: a smaller " + "--max-context (the session, mostly its KV cache, takes %lld MiB at %lld tokens), " + "--kv q4_0, the English draft subset (setup --draft-vocab en; the draft head takes " + "%lld MiB now)%s, images on the CPU, or close other programs that use the GPU\n", + (long long) min_slots, (long long) ((min_slots * blob) >> 20), (long long) short_mib, + (long long) session_mib, (long long) o.max_context, (long long) (mtp_bind >> 20), + reserve_tip.c_str()); + } } else if (multi_gpu && o.expert_cache > 0) { // an explicit cache size leaves room for the prompt path's buffers and the reserve, or the first prompt // fails with "device buffers ... do not fit" (with borrowing - the default with a profile - the path lends diff --git a/tools/test_setup_choices.py b/tools/test_setup_choices.py index 398cf989..41e21b71 100644 --- a/tools/test_setup_choices.py +++ b/tools/test_setup_choices.py @@ -330,5 +330,52 @@ class VramReserve(unittest.TestCase): self.assertTrue(call.called) +class SmallCardReserve(unittest.TestCase): + """#496: a card under 8 GB (a 6 GB laptop RTX 3060) gets a 300 MiB VRAM reserve instead of the engine's 700, and a + tip for when the start still has no room for the expert cache; an 8 GB card's config is unchanged.""" + + def install(self, vram, *extra): + from test_setup_golden import card, install + return install(63.7, [card(0, "NVIDIA GeForce RTX 3060 Laptop GPU", vram, "86")], + ["--family", "qwen", "--model", "Q2_0", "--no-start", *extra]) + + def test_a_6gb_card(self): + code, out, cfg, _ = self.install(6.0) + self.assertEqual(code, 0, out) + a = cfg["args"] + self.assertEqual(a.count("--vram-reserve-mib"), 1) + self.assertEqual(a[a.index("--vram-reserve-mib") + 1], "300") + self.assertTrue(cfg.get("vram_reserve_auto")) + self.assertIn("300 MiB of VRAM kept free instead of 700", out) + self.assertIn("no VRAM is left for the expert cache", out) + self.assertIn("--draft-vocab en", out) + self.assertIn("--mtp", a) # the draft layer stays: the server needs it + with tempfile.TemporaryDirectory() as d: # setup's own choice is not kept as the user's + p = Path(d) / "strata-q2_0.json" + p.write_text(json.dumps(cfg)) + self.assertIsNone(setup.choices_from_config(p)["vram_reserve_mib"]) + + def test_an_8gb_card_is_unchanged(self): + for vram in (8188 / 1024, 8.0): # nvidia-smi lists an 8 GB card as 8188 MiB + code, out, cfg, _ = self.install(vram) + self.assertEqual(code, 0, out) + self.assertNotIn("--vram-reserve-mib", cfg["args"]) + self.assertNotIn("vram_reserve_auto", cfg) + + def test_a_given_reserve_wins(self): + code, out, cfg, _ = self.install(6.0, "--vram-reserve-mib", "500") + self.assertEqual(code, 0, out) + a = cfg["args"] + self.assertEqual(a[a.index("--vram-reserve-mib") + 1], "500") + self.assertNotIn("vram_reserve_auto", cfg) + + def test_the_tip(self): + self.assertIn("an 8K context", " ".join(setup.small_card_note(32768, None))) + self.assertIn("--draft-vocab en", " ".join(setup.small_card_note(32768, "cjk"))) + tip = " ".join(setup.small_card_note(8192, "en")) + self.assertNotIn("--draft-vocab", tip) + self.assertIn("close other programs", tip) + + if __name__ == "__main__": unittest.main()