okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Int3 merge f-0291-term-small (da8c7f4a4): Terminal fixes and tests54ef8615b

main
4.3 KiB76 linesraw
1"""A fixed expert cache beside a RAM budget on a PC whose graphics chip shares the RAM (Strix Halo).
2
3The GPU's expert cache and the experts kept in RAM (--resident-budget-gib) draw on one pool there, and the RAM part is pinned, which costs
4TWICE its size in that pool (measured on UD-Q4_K_XL, 128 GB Strix Halo, 2026-11-01: budget 12 / 6 / 2 GiB left 4,979 / 17,267 / 25,456 MiB
5free at the first window, 2.00 x per MiB cut).  `--expert-cache auto` balances the two.  A smaller cache fixed by hand
6(--expert-cache 10000 or 5000) leaves more experts for the RAM part, a 40 GiB budget then pins a bigger complement (34.5 GiB), and the next
7allocation fails: the engine ended with exit code 1 after 41 s and no error text.
8
9    pool demand = fixed + cache slots x slot size + 2 x (experts held in RAM)
10    experts held in RAM = the smaller of the budget and the experts the cache does not hold
11
12The constants below are the measurements of that box (fixed 16.5 GiB: dense weights, session, draft layer, prompt buffers at a 16K chunk).
13It is an estimate for a recommendation, never a limit: the person's cache is kept, only the budget is lowered, with a line that says so.
14"""
15from __future__ import annotations
16
17SLOTS_TOTAL = 24576           # the experts of the Flash-Next model (48 layers x 512): the most a cache can hold
18FIXED_GIB = 16.5              # pool taken by everything but the cache and the RAM part (measured, UD-Q4_K_XL, chunk 16,384)
19MARGIN_GIB = 1.0              # kept free beside the estimate
20MIN_BUDGET_GIB = 4.0
21MEASURED_BUDGET_GIB = 12      # starts at caches of 10,000 slots and more on that box (0 hangs, 0 faults in 28 starts)
22GIB_PER_GB = 0.9313           # setup's "GB" of graphics memory seen by the GPU, in GiB
23
24
25def slot_gib(expert_gb: float) -> float:
26    """One expert's size in the cache (GiB), from the model's experts in GB (setup's MODELS arena_gb)."""
27    return float(expert_gb) * 1e9 / SLOTS_TOTAL / 2 ** 30
28
29
30def demand_gib(cache_slots: int, budget_gib: float, expert_gb: float, fixed_gib: float = FIXED_GIB) -> float:
31    s = slot_gib(expert_gb)
32    held = min(float(budget_gib), max(0, SLOTS_TOTAL - int(cache_slots)) * s)
33    return fixed_gib + int(cache_slots) * s + 2.0 * held
34
35
36def pool_gib(shared_vram_gb: float) -> float:
37    """The pool the GPU can use, in GiB, from setup's figure for the chip (the carve-out plus the shared memory, in GB)."""
38    return float(shared_vram_gb) * GIB_PER_GB
39
40
41def safe_budget_gib(cache_slots: int, expert_gb: float, shared_vram_gb: float, fixed_gib: float = FIXED_GIB) -> float:
42    """The largest RAM budget (GiB) the estimate fits beside a cache of `cache_slots`; not less than MIN_BUDGET_GIB."""
43    s = slot_gib(expert_gb)
44    room = pool_gib(shared_vram_gb) - MARGIN_GIB - fixed_gib - int(cache_slots) * s
45    return max(MIN_BUDGET_GIB, float(int(room / 2.0)))
46
47
48def too_much(cache_slots: int, budget_gib: float, expert_gb: float, shared_vram_gb: float) -> bool:
49    return demand_gib(cache_slots, budget_gib, expert_gb) > pool_gib(shared_vram_gb) - MARGIN_GIB
50
51
52def fixed_cache_and_budget(args) -> tuple[int, float] | None:
53    """(slots, GiB) when the arguments fix a numeric --expert-cache beside a --resident-budget-gib, else None (auto is balanced by the engine)."""
54    a = [str(x) for x in args or []]
55
56    def value(flag: str):
57        return a[a.index(flag) + 1] if flag in a[:-1] else None
58    cache, budget = value("--expert-cache"), value("--resident-budget-gib")
59    if cache is None or budget is None or not cache.isdigit():
60        return None
61    try:
62        return int(cache), float(budget)
63    except ValueError:
64        return None
65
66
67def stop_advice(args) -> str | None:
68    """The plain next step for an engine that stopped while loading on a PC whose graphics chip shares the RAM, when the settings fix the
69    expert cache beside a RAM budget (the exit had no error text of its own); None when they do not."""
70    fixed = fixed_cache_and_budget(args)
71    if fixed is None:
72        return None
73    cache, budget = fixed
74    return (f"This PC's graphics chip shares its memory with the RAM. The settings fix the expert cache at {cache} slots and keep a {budget:g} GiB RAM "
75            f"budget; the experts the smaller cache leaves out are pinned in that shared memory, and it can run out. Lower the budget "
76            f"(--resident-budget-gib {MEASURED_BUDGET_GIB} in the model's settings) or set --expert-cache auto, then start again.")