diff --git a/strata_cli/launch/app.py b/strata_cli/launch/app.py index f27dab0e..f7ed0825 100644 --- a/strata_cli/launch/app.py +++ b/strata_cli/launch/app.py @@ -166,7 +166,10 @@ class Ctx: "The engine's own list could not be read here; names are not checked." if not cfg and not s.extra: self.engine_note = self.engine_note or "The model is not installed yet; the defaults and tuned values appear after it is." - self._adv = P.build_rows(text, base_args, tuned_args, base_env, tuned_env, s.extra) + from strata_control import speed_switches + gpus = list(getattr(self.computer, "gpus", None) or []) + switches = P.switch_params(speed_switches.applicable(cfg, gpus, self.catalog.installed_backend())) + self._adv = P.build_rows(text, base_args, tuned_args, base_env, tuned_env, s.extra, switches) return self._adv def command_text(self) -> str: diff --git a/strata_cli/launch/params.py b/strata_cli/launch/params.py index 9a3a98d8..01186407 100644 --- a/strata_cli/launch/params.py +++ b/strata_cli/launch/params.py @@ -67,6 +67,7 @@ class Param: help_default: str | None = None experimental: bool = False values: list[str] = field(default_factory=list) + full: str = "" # a sentence that is shown whole (the speed switches); empty: the first sentence of the description @property def short(self) -> str: @@ -125,6 +126,14 @@ def env_params() -> list[Param]: return list(out.values()) +def switch_params(applicable: list[Any]) -> list[Param]: + """The rows of the speed switches that apply to this launch (strata_control/speed_switches.py): the environment each one writes when it is off, + shown as "on" until the person types the off value. The same three features as the app's Expert settings.""" + from strata_control import speed_switches as sw + return [Param(name=name, kind="env", description=sw.env_words(name), help_default="on", full=sw.env_words(name)) + for switch in applicable for name, _ in switch.off] + + def argv_to_dict(argv: list[Any]) -> dict[str, str]: """`["--kv", "int8", "--kv-grow"]` -> {"--kv": "int8", "--kv-grow": ""}.""" out: dict[str, str] = {} @@ -160,12 +169,15 @@ class Row: def build_rows(engine_help: str | None, base_args: list[Any], tuned_args: list[Any] | None, base_env: dict | None, - tuned_env: dict | None, extra: dict[str, str]) -> list[Row]: + tuned_env: dict | None, extra: dict[str, str], switches: list[Param] | None = None) -> list[Row]: """The table. Settings the run config or the tuner use come first (what this launch really runs), then the rest.""" base, tuned = argv_to_dict(base_args), argv_to_dict(tuned_args if tuned_args is not None else base_args) params = {p.name: p for p in (parse_help(engine_help) if engine_help else [])} for p in env_params(): params.setdefault(p.name, p) + for p in switches or []: + params[p.name] = p + switch_order = {p.name: i for i, p in enumerate(switches or [])} names = list(dict.fromkeys([*base, *tuned, *(base_env or {}), *(tuned_env or {}), *extra, *params])) rows: list[Row] = [] for name in names: @@ -182,10 +194,10 @@ def build_rows(engine_help: str | None, base_args: list[Any], tuned_args: list[A shown = lambda v: "on" if v == "" and not is_env and name in (base if v is d else tuned) else v # noqa: E731 y = extra.get(name, "") rows.append(Row(name=name, default=str(d) if d else "", tuned=str(t) if t else "", yours=str(y), - description=(p.short if p else "(not in this engine's list: newer flag or a typing mistake)"), + description=((p.full or p.short) if p else "(not in this engine's list: newer flag or a typing mistake)"), experimental=bool(p and p.experimental), known=bool(p) or not params)) in_use = lambda r: (r.name in base or r.name in tuned or r.name in extra or r.name in (base_env or {}) or r.name in (tuned_env or {})) # noqa: E731 - rows.sort(key=lambda r: (not in_use(r), r.name)) + rows.sort(key=lambda r: (not in_use(r), switch_order.get(r.name, len(switch_order)), r.name)) return rows diff --git a/strata_control/speed_switches.py b/strata_control/speed_switches.py new file mode 100644 index 00000000..6b212559 --- /dev/null +++ b/strata_control/speed_switches.py @@ -0,0 +1,112 @@ +"""The three speed features that are on by default in 0.2.7, and the off switch each one has. + +Each switch, when off, writes exactly the engine environment below into the model's run config ("env"); turning it on again removes those entries, +so a default launch carries none of them and is byte for byte what 0.2.6 started. The names are the engine switch contract +(phaseA-tests/notes-027-plan.md). The app does the same in apps/host/server/native-settings.mjs (SPEED_SWITCHES); tests/terminal/test_speed_switches.py +reads that file and fails when the two disagree, so the Terminal and the app cannot drift apart. + +The Terminal shows the switches as rows of its Advanced table (strata_cli/launch/params.py), only where they apply: ``strata launch --set STRATA_KVA=0`` +turns "Faster long prompts" off, ``--set STRATA_KVA=off`` puts the default back (the same as removing the row). +""" + +from __future__ import annotations + +import re +from dataclasses import dataclass +from typing import Any, Iterable, Mapping + + +@dataclass(frozen=True) +class Switch: + id: str + label: str # the words the app shows + effect: str # one plain sentence: what it does and what turning it off does + off: tuple[tuple[str, str], ...] # the environment entries written when it is off + only: str # "qwen-gpu" | "qwen-radeon-9000" | "deepseek": where it applies + + +SWITCHES: tuple[Switch, ...] = ( + Switch("faster_long_prompts", "Faster long prompts", + "Reads long prompts faster by approximating part of the work. Turn off if answers about long documents look wrong.", + (("STRATA_KVA", "0"),), "qwen-gpu"), + Switch("faster_radeon_9000", "Faster answers on Radeon RX 9000 / AI PRO R9700", + "Shares the work between the graphics card and the processor in a way that makes answers faster on these cards. " + "Turn off to go back to the previous setting, which is slower.", + (("STRATA_PCIE_FRAC_DEFAULT", "old"),), "qwen-radeon-9000"), + Switch("deepseek_speedups", "DeepSeek speed-ups", + "Reads long prompts faster and loads the parts of the model it needs faster. " + "Turn off if DeepSeek stops working or its answers look wrong; it then runs as in version 0.2.6.", + (("DS4_FAST_ATTN", "0"), ("DS4_ATTN_SLAB", "0"), ("DS4_COPY_PIN", "0"), ("DS4_COPY_HUGETLB", "0"), + ("DS4_STORE_SPARE", "0"), ("DS4_STORE_PUT", "0")), "deepseek"), +) + +BY_ID = {s.id: s for s in SWITCHES} + +_GFX12 = re.compile(r"^gfx120[01]$", re.IGNORECASE) +_RADEON_9000 = re.compile(r"\b(?:RX\s*9\d{3}|AI\s*PRO\s*R97\d{2}|R97\d{2})\b", re.IGNORECASE) + + +def is_deepseek(cfg: Mapping[str, Any] | None) -> bool: + cfg = cfg or {} + return cfg.get("arch") == "deepseek41" or bool(re.search(r"strata-dsv41", str(cfg.get("exe") or ""), re.IGNORECASE)) + + +def _radeon_9000(gpus: Iterable[Mapping[str, Any]] | None) -> bool: + for g in gpus or []: + if g.get("vendor") not in (None, "", "amd"): + continue + arch = str(g.get("arch") or "") + if _GFX12.match(arch) or (not arch and _RADEON_9000.search(str(g.get("name") or ""))): + return True + return False + + +def applies(switch: Switch | str, cfg: Mapping[str, Any] | None, gpus: Iterable[Mapping[str, Any]] | None, backend: str | None = None) -> bool: + """Does the switch make sense for this model on this PC? ``backend`` is the engine's (cuda, hip, sycl, metal, cpu); the run config's own wins.""" + sw = BY_ID[switch] if isinstance(switch, str) else switch + if sw.only == "deepseek": + return is_deepseek(cfg) + if is_deepseek(cfg): + return False + backend = (cfg or {}).get("backend") or backend + if sw.only == "qwen-gpu": + return backend in ("cuda", "hip") + if sw.only == "qwen-radeon-9000": + return backend == "hip" and _radeon_9000(gpus) + return False + + +def applicable(cfg: Mapping[str, Any] | None, gpus: Iterable[Mapping[str, Any]] | None, backend: str | None = None) -> list[Switch]: + gpus = list(gpus or []) + return [s for s in SWITCHES if applies(s, cfg, gpus, backend)] + + +def is_off(env: Mapping[str, Any] | None, switch: Switch | str) -> bool: + """Off only when every entry the switch writes is there with its value; a file with only some of them reads as on and is left alone.""" + sw = BY_ID[switch] if isinstance(switch, str) else switch + return all((env or {}).get(name) == value for name, value in sw.off) + + +def set_switch(env: Mapping[str, Any] | None, switch: Switch | str, on: bool) -> dict[str, Any]: + """The environment with the switch on (its entries removed) or off (its entries written). Nothing else in it changes.""" + sw = BY_ID[switch] if isinstance(switch, str) else switch + out = dict(env or {}) + for name, value in sw.off: + if on: + out.pop(name, None) + else: + out[name] = value + return out + + +def env_names() -> dict[str, Switch]: + """Every environment name a switch writes -> its switch.""" + return {name: sw for sw in SWITCHES for name, _ in sw.off} + + +def env_words(name: str) -> str: + """The plain sentence of one environment row of the Terminal's Advanced table.""" + sw = env_names()[name] + value = dict(sw.off)[name] + together = f"; all {len(sw.off)} speed-up rows go together" if len(sw.off) > 1 else "" + return f"{sw.label} (on by default; set {value} to turn off{together}). {sw.effect}" diff --git a/tests/terminal/test_speed_switches.py b/tests/terminal/test_speed_switches.py new file mode 100644 index 00000000..0aa78bf3 --- /dev/null +++ b/tests/terminal/test_speed_switches.py @@ -0,0 +1,145 @@ +"""The three 0.2.7 speed features are on by default and have an off switch: the contract names, where each applies, and how the Terminal carries them. + +The app (apps/host/server/native-settings.mjs SPEED_SWITCHES) and Control (strata_control/speed_switches.py) say the same thing; the last test reads the +app's table with node and fails when the two differ. `strata launch` shows the environment entries as rows of its Advanced table and writes them into +the run config's "env" (`--set STRATA_KVA=0`); a default launch has none of them. +""" +from __future__ import annotations + +import json +import shutil +import subprocess +import unittest +from pathlib import Path + +from strata_cli.launch import params as P +from strata_cli.launch import settings as S +from strata_cli.launch.runner import derive_config +from strata_control import speed_switches as W + +ROOT = Path(__file__).resolve().parents[2] + +OFF = { + "faster_long_prompts": {"STRATA_KVA": "0"}, + "faster_radeon_9000": {"STRATA_PCIE_FRAC_DEFAULT": "old"}, + "deepseek_speedups": {"DS4_FAST_ATTN": "0", "DS4_ATTN_SLAB": "0", "DS4_COPY_PIN": "0", "DS4_COPY_HUGETLB": "0", + "DS4_STORE_SPARE": "0", "DS4_STORE_PUT": "0"}, +} +R9700 = {"name": "AMD Radeon AI PRO R9700", "vendor": "amd", "arch": "gfx1201"} +RX7900 = {"name": "AMD Radeon RX 7900 XTX", "vendor": "amd", "arch": "gfx1100"} +STRIX = {"name": "AMD Radeon 8060S", "vendor": "amd", "arch": "gfx1151", "uma": True} +RTX = {"name": "NVIDIA GeForce RTX 4090", "vendor": "nvidia", "arch": "89"} +QWEN = {"arch": "qwen35moe", "backend": "cuda", "args": ["--native", "m.gguf"], "env": {"PATH": "/bin"}} +DEEPSEEK = {"arch": "deepseek41", "exe": "C:/s/engine-dsv41/0.1.52-win/strata-dsv41.exe", "args": ["--pack", "p"], "env": {"DS4_ASYNC_T": "4"}} + + +def ids(cfg, gpus, backend=None): + return [s.id for s in W.applicable(cfg, gpus, backend)] + + +class Contract(unittest.TestCase): + def test_three_switches_with_the_contract_names(self): + self.assertEqual([s.id for s in W.SWITCHES], list(OFF)) + for s in W.SWITCHES: + self.assertEqual(dict(s.off), OFF[s.id], s.id) + + def test_an_effect_text_says_what_turning_it_off_does_in_plain_words(self): + for s in W.SWITCHES: + self.assertRegex(s.effect, r"[Tt]urn off", s.id) + self.assertNotRegex(s.effect, r"STRATA_|DS4_|--|env", s.id) + self.assertEqual(W.SWITCHES[0].effect, "Reads long prompts faster by approximating part of the work. Turn off if answers about long documents look wrong.") + + def test_where_each_switch_applies(self): + self.assertEqual(ids(QWEN, [RTX]), ["faster_long_prompts"]) + self.assertEqual(ids({**QWEN, "backend": "hip"}, [RX7900]), ["faster_long_prompts"]) + self.assertEqual(ids({**QWEN, "backend": "hip"}, [STRIX]), ["faster_long_prompts"], "Strix Halo is gfx1151, not gfx12") + self.assertEqual(ids({**QWEN, "backend": "hip"}, [R9700]), ["faster_long_prompts", "faster_radeon_9000"]) + self.assertEqual(ids({"arch": "qwen35moe"}, [R9700], "hip"), ["faster_long_prompts", "faster_radeon_9000"], "the installed engine's backend when the config has none") + self.assertEqual(ids({**QWEN, "backend": "hip"}, [{"name": "AMD Radeon RX 9070 XT", "vendor": "amd"}]), ["faster_long_prompts", "faster_radeon_9000"]) + self.assertEqual(ids({**QWEN, "backend": "cuda"}, [RTX, R9700]), ["faster_long_prompts"], "the Radeon switch needs the HIP engine") + for backend in ("sycl", "metal", "cpu", None): + self.assertEqual(ids({**QWEN, "backend": backend}, [R9700]), [], backend) + self.assertEqual(ids(DEEPSEEK, [R9700]), ["deepseek_speedups"]) + self.assertEqual(ids({**DEEPSEEK, "backend": "cuda"}, [RTX]), ["deepseek_speedups"]) + self.assertEqual(ids({"exe": "C:/x/strata-dsv41.exe"}, [RTX]), ["deepseek_speedups"], "found by the engine's name too") + + def test_off_and_on_again_is_the_identical_environment(self): + env = {"PATH": "/bin", "STRATA_PREFILL": "2", "DS4_ASYNC_T": "4"} + for s in W.SWITCHES: + off = W.set_switch(env, s, False) + self.assertEqual(off, {**env, **OFF[s.id]}) + self.assertTrue(W.is_off(off, s)) + back = W.set_switch(off, s, True) + self.assertEqual(json.dumps(back), json.dumps(env), s.id) + self.assertFalse(W.is_off({"DS4_FAST_ATTN": "0"}, "deepseek_speedups"), "only some of the entries reads as on") + self.assertFalse(W.is_off({"STRATA_KVA": "1"}, "faster_long_prompts")) + + +class Terminal(unittest.TestCase): + """`strata launch`: the run config a launch writes.""" + + def config(self, base, extra=None): + return derive_config(base, S.LaunchSettings(extra=extra or {}), bool_flags=frozenset()) + + def test_a_default_launch_has_none_of_the_new_entries(self): + for base in (QWEN, DEEPSEEK): + env = self.config(base).get("env") or {} + for name in W.env_names(): + self.assertNotIn(name, env, name) + self.assertEqual(env, base["env"], "the environment is exactly the installed one") + self.assertEqual(json.dumps(self.config(base), sort_keys=True), json.dumps(self.config(base, {}), sort_keys=True)) + + def test_each_switch_off_by_set_gives_exactly_its_entries(self): + for sid, entries in OFF.items(): + base = DEEPSEEK if sid == "deepseek_speedups" else QWEN + cfg = self.config(base, S.settings_from_pairs([f"{k}={v}" for k, v in entries.items()])) + self.assertEqual(cfg["env"], {**base["env"], **entries}, sid) + self.assertEqual(cfg["args"], base["args"], "no engine flag changes") + + def test_off_again_puts_the_default_back(self): + cfg = self.config(QWEN, S.settings_from_pairs(["STRATA_KVA=off"])) + self.assertEqual(cfg["env"], QWEN["env"]) + already = {**QWEN, "env": {**QWEN["env"], "STRATA_KVA": "0"}} + self.assertEqual(self.config(already, S.settings_from_pairs(["STRATA_KVA=off"]))["env"], QWEN["env"]) + + def test_the_advanced_table_lists_only_the_switches_that_apply(self): + def names(cfg, gpus, extra=None): + rows = P.build_rows(None, cfg["args"], None, cfg["env"], None, extra or {}, P.switch_params(W.applicable(cfg, gpus))) + return [r.name for r in rows if r.name in W.env_names()] + self.assertEqual(names(QWEN, [RTX]), ["STRATA_KVA"]) + self.assertEqual(names({**QWEN, "backend": "hip"}, [R9700]), ["STRATA_KVA", "STRATA_PCIE_FRAC_DEFAULT"]) + self.assertEqual(names(DEEPSEEK, [R9700]), list(OFF["deepseek_speedups"]), "the six, in the contract order") + self.assertEqual(names({**QWEN, "backend": "sycl"}, [R9700]), []) + + def test_a_row_says_on_until_it_is_turned_off_and_explains_itself(self): + rows = P.build_rows(None, QWEN["args"], None, QWEN["env"], None, {}, P.switch_params(W.applicable(QWEN, [RTX]))) + row = next(r for r in rows if r.name == "STRATA_KVA") + self.assertEqual((row.name, row.default, row.tuned, row.yours, row.known), ("STRATA_KVA", "on", "on", "", True)) + self.assertIn("Faster long prompts", row.description) + self.assertIn("set 0 to turn off", row.description) + self.assertIn("Turn off if answers about long documents look wrong", row.description) + off = P.build_rows(None, QWEN["args"], None, QWEN["env"], None, {"STRATA_KVA": "0"}, P.switch_params(W.applicable(QWEN, [RTX]))) + self.assertEqual(next(r for r in off if r.name == "STRATA_KVA").yours, "0") + + def test_the_six_deepseek_rows_say_they_go_together(self): + text = W.env_words("DS4_COPY_PIN") + self.assertIn("all 6 speed-up rows go together", text) + self.assertEqual({W.env_names()[n].id for n in OFF["deepseek_speedups"]}, {"deepseek_speedups"}) + + +class SameAsTheApp(unittest.TestCase): + @unittest.skipUnless(shutil.which("node"), "node is not installed") + def test_the_apps_table_has_the_same_names_and_words(self): + script = ("import {SPEED_SWITCHES} from './apps/host/server/native-settings.mjs';" + "console.log(JSON.stringify(SPEED_SWITCHES.map(s=>({id:s.id,label:s.label,hint:s.hint,off:s.off}))))") + out = subprocess.run(["node", "--input-type=module", "-e", script], cwd=ROOT, capture_output=True, text=True, timeout=60, check=True).stdout + app = json.loads(out) + self.assertEqual([a["id"] for a in app], [s.id for s in W.SWITCHES]) + for a, s in zip(app, W.SWITCHES): + self.assertEqual(a["label"], s.label) + self.assertEqual(a["hint"], s.effect, s.id) + self.assertEqual(a["off"], dict(s.off), s.id) + + +if __name__ == "__main__": + unittest.main()