okami.mom/strata

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

Niko1221Merge main c1ff07f1a into f-own-intfefaa985c

main
6.6 KiB105 linesraw
1"""Which conversation context fits a graphics card for Qwen3.6-35B-A3B (the strata-q36 engine path, GitHub #5715): the one rule setup's context
2menu, the start line in serve/server.py and the app's estimate (via the catalog, tools/strata_mcp.py) use.  The app's copy of the formula
3(apps/host/src/lib/native-fit.ts `ownPathCacheGb` / `ownPathContextFits`) is pinned to this one by apps/host/tests/own-path-fit.test.mjs.
4
5    need(ctx) GiB = BASE_GIB + the size's hybrid minimum + STATE_GIB + ctx x BYTES_PER_TOKEN / 2^30
6    fits          = need(ctx) <= the card's memory as setup and the catalog read it (nvidia-smi memory.total, in GiB)
7
8The parts:
9  BYTES_PER_TOKEN 47,104 B: derived from the engine's allocations, not measured.  Each session keeps fp32 K and V of the 10 attention layers and the
10      MTP block (11 x 2 KV heads x 256 x 2 x 4 B = 45,056 B, q36_engine.cu alloc_session; setup.py's kv_bytes_per_token, a parallel slot's figure)
11      and the engine keeps one fp16 copy of K and V at the full context for the prompt attention (2 x 256 x 2 x 2 B = 2,048 B, Engine::init k16 / v16).
12  STATE_GIB: the 30 GDN layers' recurrent state of one session (setup.py state_gb 0.07 GB).
13  BASE_GIB 8.55: everything on the card before the context: the dense weights (4.40 GiB, the same for every size in the field logs), the engine's
14      buffers at a 2048-token prompt chunk, the CUDA context and what Windows keeps of the card.  Derived from the #5715 log (RTX 5070 12 GB, Windows 11,
15      engine 0.1.59, UD-Q4_K_XL at --max-context 65536: "0.45 GiB of VRAM free" after the engine's buffers and the session):
16      11.94 - 0.45 - 65536 x 47,104 / 2^30 - 0.065 = 8.55.  ONE field log.  The other field logs came out at 7.2-7.65 (Linux #3303 #2747,
17      Windows #3508 #3687), so this is the strict end: on those cards the rule can mark a smaller context than would start (12 GB: 32K where
18      48-64K may start).  Nothing the product reads today (the OS, memory.total) tells the two apart.  NOT MEASURED HERE: a start on the
19      owner's RTX 5070 (with the card's used memory logged before it) replaces this one number.
20  HYBRID_MIN_GIB: what the hybrid mode allocates after the session even with no expert slot (the staging pair for the prompt layers and the CPU
21      part's outputs, q36_engine.cu place_experts): UD-Q4_K_XL 1256 MiB (#5715 log), UD-Q6_K_XL 1760 MiB (#3687 log, RTX 3060 12 GB); Q8_0 taken as
22      UD-Q6_K_XL (#2747 log: the same 1.59 GiB staging pair).
23
24Nothing here changes a context: the result is a recommendation and a warning (recommend, never force).  A family or size without these figures (a
25model of the update feed) gets None everywhere, and the callers keep what they did before.
26"""
27
28from __future__ import annotations
29
30GIB = 2 ** 30
31BYTES_PER_TOKEN = 11 * 2 * 256 * 2 * 4 + 2 * 256 * 2 * 2      # 45,056 + 2,048 = 47,104
32STATE_GIB = 0.07e9 / GIB
33BASE_GIB = 8.55                                                # the one figure a measured start replaces ("5070 calibration owed")
34HYBRID_MIN_GIB = {"UD-Q4_K_XL": 1256 / 1024, "UD-Q6_K_XL": 1760 / 1024, "Q8_0": 1760 / 1024}
35FAMILY_NAME = "qwen3.6-35b-a3b"                                # setup.py OWN_PATH_FAMILIES['qwen36']['name'], the config's model_name prefix
36DEFAULT_CONTEXT = 131072                                       # 128K (owner, 2026-11-01): the mark never goes above it
37MENU = [8192, 32768, 65536, 131072, 204800, 262144]           # setup's own-path context menu (setup.own_path_contexts(); a test keeps them equal)
38
39
40def fixed_gib(model: str | None) -> float | None:
41    """What the card holds before the context for this size (GiB), None for a size without figures."""
42    h = HYBRID_MIN_GIB.get(str(model or ""))
43    return None if h is None else BASE_GIB + h + STATE_GIB
44
45
46def inputs(name: str | None, model: str | None) -> dict | None:
47    """The formula's inputs for a family name ("qwen3.6-35b-a3b") and size, as the catalog row carries them to the app; None: not known."""
48    f = fixed_gib(model) if str(name or "").lower() == FAMILY_NAME else None
49    return None if f is None else {"bytes_per_token": BYTES_PER_TOKEN, "fixed_gb": round(f, 4)}
50
51
52def from_model_name(model_name: str | None) -> dict | None:
53    """The inputs from a config's model_name ("qwen3.6-35b-a3b-ud-q4_k_xl"), None for any other model."""
54    low = str(model_name or "").lower()
55    if not low.startswith(FAMILY_NAME + "-"):
56        return None
57    size = low[len(FAMILY_NAME) + 1:]
58    return next((inputs(FAMILY_NAME, m) for m in HYBRID_MIN_GIB if m.lower() == size), None)
59
60
61def cache_gib(ctx: int) -> float:
62    """The conversation memory of one session at this context (GiB)."""
63    return int(ctx) * BYTES_PER_TOKEN / GIB
64
65
66def need_gib(fit: dict, ctx: int) -> float:
67    return float(fit["fixed_gb"]) + int(ctx) * float(fit["bytes_per_token"]) / GIB
68
69
70def fits(fit: dict, vram_gb: float, ctx: int) -> bool:
71    return need_gib(fit, ctx) <= float(vram_gb)
72
73
74def largest_fit(fit: dict | None, vram_gb: float | None, choices) -> int | None:
75    """The largest of `choices` that fits the card; 0 when none does; None when it cannot be said (no figures, no card)."""
76    if not fit or not vram_gb or float(vram_gb) <= 0:
77        return None
78    ok = [int(c) for c in choices if fits(fit, float(vram_gb), int(c))]
79    return max(ok) if ok else 0
80
81
82def recommended(fit: dict | None, vram_gb: float | None, choices, default: int = DEFAULT_CONTEXT) -> int:
83    """The context to mark "(recommended)": the largest choice that fits the card, at most `default` (128K); the smallest choice when none fits;
84    `default` when it cannot be said."""
85    best = largest_fit(fit, vram_gb, choices)
86    if best is None:
87        return default
88    if best == 0:
89        return min(int(c) for c in choices)
90    return min(best, default)
91
92
93def label(ctx: int) -> str:
94    return f"{ctx // 1024}K" if ctx % 1024 == 0 else f"{ctx:,} tokens"
95
96
97def warning(fit: dict | None, vram_gb: float | None, ctx: int, choices) -> str | None:
98    """The sentence said before the start when `ctx` does not fit the card (owner's wording, 11-10/11-11), else None.  Nothing is said when no
99    choice fits at all (the sentence needs a size that fits) or when it cannot be said."""
100    best = largest_fit(fit, vram_gb, choices)
101    if not best or fits(fit, float(vram_gb), int(ctx)):
102        return None
103    return (f"At {label(int(ctx))}, this model's conversation memory needs about {int(ctx) * float(fit['bytes_per_token']) / GIB:.1f} GB of graphics memory. With the model "
104            f"itself that is more than this card's {float(vram_gb):.0f} GB, so it will probably not start. {label(best)} fits on this card: choose "
105            f"{label(best)} or less.")