okami.mom/strata

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

Niko1221Merge f-ds-out ac306afb2 into f-own-intaff3a132f

main
6.2 KiB108 linesraw
1"""The launch-policy tables the app's host (JavaScript) reads, generated from the Python that owns them.
2
3`strata_control` decides the speed switches (`speed_switches.py`) and which models have a Drafting choice (`drafting.py`).  The app's host used to carry its own
4copy of those tables.  It now imports `apps/host/server/policy-generated.mjs`, which this module writes, and a test (`tests/terminal/test_app_policy.py`, and
5`apps/host/tests/policy-generated.test.mjs` on the JavaScript side) fails when the committed file differs from what `render()` makes now.
6
7    python -m strata_control.app_policy            # rewrite apps/host/server/policy-generated.mjs
8    python -m strata_control.app_policy --check    # exit 1 when it is out of date
9"""
10
11from __future__ import annotations
12
13import json
14import sys
15from pathlib import Path
16
17from strata_control import drafting, speed_switches
18
19TARGET = Path(__file__).resolve().parents[1] / "apps" / "host" / "server" / "policy-generated.mjs"
20VECTORS = Path(__file__).resolve().parents[1] / "apps" / "host" / "tests" / "data" / "policy-vectors.json"
21
22HEADER = ("// GENERATED by `python -m strata_control.app_policy` from strata_control/speed_switches.py and strata_control/drafting.py. Do not edit it by hand:\n"
23          "// change the Python and regenerate; tests/terminal/test_app_policy.py and apps/host/tests/policy-generated.test.mjs fail when this file is out of date.\n")
24
25
26def _entries(pairs) -> dict:
27    return {name: value for name, value in pairs}
28
29
30def table() -> list[dict]:
31    out = []
32    for s in speed_switches.app_switches():
33        row = {"id": s.id, "label": s.label, "hint": s.effect, "default": s.default, "env": _entries(s.env)}
34        if s.legacy:
35            row["legacy"] = _entries(s.legacy)
36        if s.group != "Speed":
37            row["group"] = s.group
38        if s.words:
39            row["words"] = _entries(s.words)
40        out.append(row)
41    return out
42
43
44def render() -> str:
45    rows = ",\n".join(" " + json.dumps(r, ensure_ascii=False, separators=(", ", ": ")) for r in table())
46    return (HEADER
47            + "/** The speed switches of the app's Expert settings (strata_control/speed_switches.py app_switches()): id, words, default, the environment entries outside the default. */\n"
48            + f"export const SPEED_SWITCH_TABLE = [\n{rows},\n];\n"
49            + "/** The families that have a Drafting choice (strata_control/drafting.py SUPPORTED_FAMILIES). */\n"
50            + f"export const DRAFTING_FAMILIES = {json.dumps(list(drafting.SUPPORTED_FAMILIES))};\n"
51            + "/** The model names the DFlash2 drafter was made for (strata_control/drafting.py _SUPPORTED_NAME), a case-insensitive pattern. */\n"
52            + f"export const DFLASH2_MODEL_PATTERN = {json.dumps(drafting._SUPPORTED_NAME.pattern)};\n")
53
54
55#: Cases the host's copies of the RULES (speedSwitchApplies, resolveDrafter) must answer as Python does.  The table above is data and is generated; the rules are code, so the
56#: answers of the Python are written down here and apps/host/tests/policy-generated.test.mjs runs the host's rules over them.
57_CARDS = {
58    "rtx4090": {"name": "NVIDIA GeForce RTX 4090", "vendor": "nvidia", "arch": "89", "vram_gb": 24.0},
59    "rtx3060": {"name": "NVIDIA GeForce RTX 3060", "vendor": "nvidia", "arch": "86", "vram_gb": 12.0},
60    "rtx4060ti16": {"name": "NVIDIA GeForce RTX 4060 Ti 16GB", "vendor": "nvidia", "arch": "89", "vram_gb": 16.0},
61    "spark": {"name": "NVIDIA GB10", "vendor": "nvidia", "arch": "121", "vram_gb": 120.0, "uma": True},
62    "r9700": {"name": "AMD Radeon AI PRO R9700", "vendor": "amd", "arch": "gfx1201", "vram_gb": 32.0},
63    "rx7900": {"name": "AMD Radeon RX 7900 XTX", "vendor": "amd", "arch": "gfx1100", "vram_gb": 24.0},
64    "rx9070": {"name": "AMD Radeon RX 9070 XT", "vendor": "amd", "vram_gb": 16.0},
65    "strix": {"name": "AMD Radeon 8060S", "vendor": "amd", "arch": "gfx1151", "uma": True},
66    "arc": {"name": "Intel(R) Arc(TM) A750", "vendor": "intel", "vram_gb": 8.0},
67    "nameonly3060": {"name": "GeForce RTX 3060", "vram_gb": 12.0},
68}
69_MODELS = {"qwen": {}, "qwen36": {"arch": "qwen35moe"},
70           "qwen_pictures": {"vision": {"exe": "strata-vision"}}, "qwen36_pictures": {"arch": "qwen35moe", "vision": {"exe": "strata-vision"}}}
71_NAMES = ["qwen3.8-flash-next", "qwen3.8-flash-next-iq3_s", "qwen3.8-flash-next-Q2_0", "qwen3.8-flash-next-unsloth-ud-q4_k_xl", "qwen3.8-flash-next-unsloth-ud-iq4_xs-v31",
72          "qwen3.8-flash-next-gyro-iq4", "qwen3.8-flash-next-coder-iq1_m", "qwen3.8-flash-next-swift-iq3_s", "swift-1.5-iq3_s", "qwen3.6-35b-a3b-ud-q4_k_xl", ""]
73
74
75def vectors() -> dict:
76    switches = []
77    for model, cfg in _MODELS.items():
78        for backend in ("cuda", "hip", "sycl", "metal", "cpu", None):
79            for card, g in _CARDS.items():
80                row = {"model": model, "backend": backend, "card": card,
81                       "shows": sorted(s.id for s in speed_switches.applicable(cfg, [g], backend) if not s.terminal_only)}
82                switches.append(row)
83    names = [{"name": n, "supported": bool(drafting.supported_model(n, None))} for n in _NAMES]
84    families = [{"family": f, "supported": bool(drafting.supported_model(None, f))} for f in ("qwen", "gyro", "unsloth", "swift", "coder", "nvfp4")]
85    return {"cards": _CARDS, "models": _MODELS, "switches": switches, "names": names, "families": families}
86
87
88def render_vectors() -> str:
89    return json.dumps(vectors(), indent=1, ensure_ascii=False) + "\n"
90
91
92def main(argv: list[str] | None = None) -> int:
93    argv = sys.argv[1:] if argv is None else argv
94    files = ((TARGET, render()), (VECTORS, render_vectors()))
95    if "--check" in argv:
96        bad = [str(p) for p, text in files if (p.read_text(encoding="utf-8") if p.is_file() else "") != text]
97        for p in bad:
98            print(f"{p} is out of date: run python -m strata_control.app_policy", file=sys.stderr)
99        return 1 if bad else 0
100    for path, text in files:
101        path.parent.mkdir(parents=True, exist_ok=True)
102        path.write_text(text, encoding="utf-8", newline="\n")
103        print(f"wrote {path}")
104    return 0
105
106
107if __name__ == "__main__":
108    raise SystemExit(main())