diff --git a/terminal/strata_cli/launch/runner.py b/terminal/strata_cli/launch/runner.py index a0c7109d03..240c3133e6 100644 --- a/terminal/strata_cli/launch/runner.py +++ b/terminal/strata_cli/launch/runner.py @@ -447,6 +447,23 @@ def fit_parallel_for(catalog, s: S.LaunchSettings, vram_gb: float | None) -> int failed after the download): there setup's own rule for that model and context decides (own_path_parallel_recommend; 0 = one at a time), never more than the table's number.""" table = S.fit_parallel(vram_gb) + if s.family == "inkling-2-mini": + # Every slot reserves K/V for the whole context; slots whose fp16 K/V does not fit next to the experts spill it to host RAM and a long session + # reads half of it over PCIe (Olares, 3 slots at 32K on 24 GB: decode 86 -> 44 tok/s after 30K tokens). The table's number, but no more + # slots than keep the K/V on the card (NVIDIA only; any other make or a failed look keeps the table). An explicit --parallel never gets here. + try: + from strata_control import inkling_fit + comp = catalog.computer() + gpus = comp.usable_gpus + if gpus and (comp.engine_vendor in (None, "nvidia")) and str(s.model or "") in inkling_fit.BUILDS: + arch = str(gpus[0].get("arch") or "") + # the card's total memory is not what is free: the driver and the desktop keep about 0.75 GiB + return inkling_fit.launch_parallel(str(s.model), max(0.0, float(vram_gb or 0.0) - 0.75), float(comp.ram_gb or 0.0), + int(s.context or inkling_fit.DEFAULT_CONTEXT), + table, backend="cuda", sm=int(arch) if arch.isdigit() else None) + except Exception: # noqa: BLE001 - the table as before when the plan cannot be made + return table + return table try: setup = catalog.strata.setup_module() fam = (getattr(setup, "OWN_PATH_FAMILIES", None) or {}).get(s.family) if setup is not None else None diff --git a/terminal/strata_control/inkling_fit.py b/terminal/strata_control/inkling_fit.py index 4c598562bb..7f1d413b12 100644 --- a/terminal/strata_control/inkling_fit.py +++ b/terminal/strata_control/inkling_fit.py @@ -656,3 +656,33 @@ def max_parallel(build: str, vram_gib: float | None, ram_gib: float, unified: bo msg = f"--parallel {want} does not fit at a context of {n_ctx} tokens on this {where}setup: the largest count that fits is {best}{tail}" return {"max_parallel": best, "requested": want, "fits": want is None or want <= best, "context": n_ctx, "max_context_at_requested": ctx_at_want, "message": msg} + + +def resident_parallel(build: str, vram_gib: float | None, ram_gib: float, ctx: int | None = None, *, cap: int = MAX_PARALLEL, vision: bool = False, + dspark: bool = True, backend: str | None = None, desktop: bool = False, ram_free_gib: float | None = None, + sm: int | None = None, extra_vram_gib: float = 0.0) -> int: + """The largest number of request slots (1..cap) whose K/V stays on the card next to what one slot holds there: the plan keeps the K/V in the card + (kv_offload off) and gives the experts the same share of the card as for one slot. More slots than that spill their K/V to host RAM: the engine keeps + a window of every slot's positions on the card and reads the rest over PCIe (Olares, RTX 5090 Laptop 24 GB, UD-Q4_K_XL, 32K, 3 slots: 11,008 of + 21,760 + 11,008 positions on the host, decode 86 tok/s after a short prompt, 82 after 4K, 44 after 30K). Never below 1.""" + kw = dict(vision=vision, dspark=dspark, backend=backend, desktop=desktop, ram_free_gib=ram_free_gib, sm=sm, extra_vram_gib=extra_vram_gib) + base = place(build, vram_gib, ram_gib, False, ctx, vision, 1, dspark, **{k: v for k, v in kw.items() if k not in ("vision", "dspark")}) + best = 1 + for n in range(2, max(1, int(cap)) + 1): + p = place(build, vram_gib, ram_gib, False, ctx, vision, n, dspark, **{k: v for k, v in kw.items() if k not in ("vision", "dspark")}) + if (p.get("kv_offload") != "off" or float(p["vram_gb"]) < float(base["vram_gb"]) - 0.05 + or float(p["ssd_gb"]) > float(base["ssd_gb"]) + 0.05): + break + best = n + return best + + +def launch_parallel(build: str, vram_gib: float | None, ram_gib: float, ctx: int | None, table: int, *, backend: str | None = "cuda", + sm: int | None = None, desktop: bool = True, dspark: bool = True) -> int: + """"Fit to your card" for Inkling-2-Mini (what `strata launch` uses when --parallel is not given): the VRAM table's number, but no more than the slots + whose K/V stays on the card (resident_parallel); an explicit --parallel is never touched by this. The card is planned with the desktop reserve (the + free memory is not known here), so a headless card gets a safe, not the largest, number.""" + if build not in BUILDS: + return table + return max(1, min(int(table), resident_parallel(build, vram_gib, ram_gib, ctx, cap=int(table), backend=backend, desktop=desktop, sm=sm, dspark=dspark))) + diff --git a/terminal/tests/terminal/test_inkling_launch_parallel.py b/terminal/tests/terminal/test_inkling_launch_parallel.py new file mode 100644 index 0000000000..0bc15777b1 --- /dev/null +++ b/terminal/tests/terminal/test_inkling_launch_parallel.py @@ -0,0 +1,112 @@ +"""`strata launch` for Inkling-2-Mini: "fit to your card" no longer plans more slots than keep their K/V on the card. + +Olares (RTX 5090 Laptop 24 GB, UD-Q4_K_XL, 32K context, 3 slots from the VRAM table): the engine keeps 21,760 positions of every slot on the card and +11,008 on the host, and a decode step after 30K tokens reads the host part over PCIe: 86.3 tok/s after a short prompt, 81.7 after 4K, 44.3 after 30K. +Each slot reserves K/V for the whole context, so the number of slots decides whether the K/V fits next to the experts.""" +from __future__ import annotations + +import re +import sys +import unittest +from pathlib import Path + +TERMINAL = Path(__file__).resolve().parents[2] +if str(TERMINAL) not in sys.path: + sys.path.insert(0, str(TERMINAL)) + +from strata_cli.launch import settings as S +from strata_cli.launch.runner import fit_parallel_for +from strata_control import inkling_fit as F +from tests.terminal.launch_support import HW_12, HW_24, HW_32GB_RAM, Home, catalog + + +def _fit(hw, family, model, context, vram): + with Home(): + return fit_parallel_for(catalog(hw), S.LaunchSettings(family=family, model=model, context=context), vram) + + +class ResidentParallel(unittest.TestCase): + def test_olares_class_card_keeps_two_slots_on_the_card_at_32k_one_at_64k(self): + free = 24.0 - 0.75 + self.assertEqual(F.resident_parallel("UD-Q4_K_XL", free, 64.0, 32768, cap=4, backend="cuda", desktop=True, sm=120), 2) + self.assertEqual(F.resident_parallel("UD-Q4_K_XL", free, 64.0, 65536, cap=4, backend="cuda", desktop=True, sm=120), 1) + + def test_the_slots_are_what_the_plan_keeps_on_the_card(self): + # the answer is the largest N whose plan says "K/V on the card" with the experts' share of one slot + for build in ("UD-Q4_K_XL", "SQ-20", "SQ-15", "SQ-10"): + n = F.resident_parallel(build, 23.25, 64.0, 32768, cap=6, backend="cuda", desktop=True, sm=120) + one = F.place(build, 23.25, 64.0, False, 32768, False, 1, True, backend="cuda", desktop=True, sm=120) + at = F.place(build, 23.25, 64.0, False, 32768, False, n, True, backend="cuda", desktop=True, sm=120) + self.assertEqual(at["kv_offload"], "off", build) + self.assertGreaterEqual(at["vram_gb"], one["vram_gb"] - 0.05, build) + if n < 6: + more = F.place(build, 23.25, 64.0, False, 32768, False, n + 1, True, backend="cuda", desktop=True, sm=120) + self.assertTrue(more["kv_offload"] != "off" or more["vram_gb"] < one["vram_gb"] - 0.05, build) + + def test_a_bigger_card_or_a_smaller_context_never_gives_fewer_slots(self): + for build in ("UD-Q4_K_XL", "SQ-20"): + last = 0 + for free in (11.0, 15.5, 18.5, 22.5, 30.0, 46.0): + n = F.resident_parallel(build, free, 64.0, 32768, cap=8, backend="cuda", desktop=True, sm=120) + self.assertGreaterEqual(n, last, (build, free)) + last = n + last = 99 + for ctx in (8192, 32768, 65536, 131072): + n = F.resident_parallel(build, 23.25, 64.0, ctx, cap=8, backend="cuda", desktop=True, sm=120) + self.assertLessEqual(n, last, (build, ctx)) + last = n + + def test_never_below_one(self): + self.assertEqual(F.resident_parallel("SQ-20", 8.0, 16.0, 131072, cap=4, backend="cuda", desktop=True), 1) + self.assertEqual(F.resident_parallel("SQ-20", 0.0, 16.0, 32768, cap=4, backend="cuda"), 1) + + +class LaunchParallel(unittest.TestCase): + def test_never_more_than_the_table(self): + for table in (1, 2, 3, 4): + for build in ("UD-Q4_K_XL", "SQ-10", "SQ-20"): + self.assertLessEqual(F.launch_parallel(build, 46.0, 64.0, 32768, table, sm=120), table) + self.assertGreaterEqual(F.launch_parallel(build, 46.0, 64.0, 32768, table, sm=120), 1) + + def test_an_unknown_build_keeps_the_table(self): + self.assertEqual(F.launch_parallel("no-such-build", 24.0, 64.0, 32768, 3), 3) + + def test_the_24_gb_default_is_two_slots_for_the_gguf_at_32k_not_three(self): + self.assertEqual(S.fit_parallel(24.0), 3) + n = F.launch_parallel("UD-Q4_K_XL", 24.0 - 0.75, 64.0, 32768, 3, sm=120) + self.assertEqual(n, 2) + + +class InTheLaunch(unittest.TestCase): + def test_inkling_on_a_24_gb_nvidia_card_gets_the_planned_number(self): + got = _fit(HW_24, "inkling-2-mini", "UD-Q4_K_XL", 32768, 24.0) + want = F.launch_parallel("UD-Q4_K_XL", 24.0 - 0.75, 64.0, 32768, S.fit_parallel(24.0), sm=None) + self.assertEqual(got, want) + self.assertLess(got, S.fit_parallel(24.0)) # not the table's 3: that spills the K/V + + def test_a_bigger_context_means_fewer_slots_never_more(self): + a = _fit(HW_24, "inkling-2-mini", "UD-Q4_K_XL", 32768, 24.0) + b = _fit(HW_24, "inkling-2-mini", "UD-Q4_K_XL", 131072, 24.0) + self.assertLessEqual(b, a) + self.assertGreaterEqual(b, 1) + + def test_a_small_card_keeps_the_table(self): + self.assertEqual(_fit(HW_12, "inkling-2-mini", "SQ-10", 32768, 12.0), S.fit_parallel(12.0)) + + def test_an_unknown_model_name_keeps_the_table(self): + self.assertEqual(_fit(HW_24, "inkling-2-mini", "my-own-gguf", 32768, 24.0), S.fit_parallel(24.0)) + self.assertEqual(_fit(HW_24, "inkling-2-mini", None, 32768, 24.0), S.fit_parallel(24.0)) + + def test_other_families_are_unchanged(self): + for vram in (12.0, 20.0, 24.0, 32.0): + self.assertEqual(_fit(HW_24, "qwen", "IQ3_S", 131072, vram), S.fit_parallel(vram)) + self.assertEqual(_fit(HW_24, "hy4-flash", "Hy4-Flash", 32768, vram), S.fit_parallel(vram)) + + def test_an_explicit_parallel_is_never_asked_for(self): + # launch/runner.py: `parallel = self.s.parallel if self.s.parallel else (four_card_parallel or catalog_parallel())`: the fit is only the default + text = (TERMINAL / "strata_cli" / "launch" / "runner.py").read_text(encoding="utf-8") + self.assertTrue(re.search(r"parallel = self\.s\.parallel if self\.s\.parallel else \(", text)) + + +if __name__ == "__main__": + unittest.main()