diff --git a/docs/TERMINAL_LAUNCH.md b/docs/TERMINAL_LAUNCH.md index 80549729..23444e1d 100644 --- a/docs/TERMINAL_LAUNCH.md +++ b/docs/TERMINAL_LAUNCH.md @@ -70,7 +70,7 @@ strata launch --list-presets | --delete-preset NAME | --status | --stop [--prese `strata launch --help` lists everything with examples. An unknown model or flag gets a plain message with the closest match. Presets are small files in `launch-presets/` beside the profiles folder; the run uses the shared profile `launch-` and the shared tuning presets (`strata presets`), so the app sees them too. Flags next to `--preset` change that start only -(add `--save-preset` to keep them). +(add `--save-preset` to keep them). `--plan` shows the model (also for your own files, which it recognises), the context, the picture setting and what would be downloaded and how big (0 when everything is already here). A model that was tuned before for the same choices on the same computer is not measured again by `--tune quick`; the stored result is used and the run says so. Add `--retune` to measure again. ## Connect your apps: `strata connect` diff --git a/strata_cli/launch/app.py b/strata_cli/launch/app.py index b0a8500a..8a327b95 100644 --- a/strata_cli/launch/app.py +++ b/strata_cli/launch/app.py @@ -43,6 +43,7 @@ class Ctx: glyphs: Glyphs = field(default_factory=Glyphs) feed: str | None = None move_data: bool = False + retune: bool = False notes: list[str] = field(default_factory=list) start_own_files: bool = False picked_in_list: bool = False @@ -263,7 +264,7 @@ class LaunchApp(App): s = self.ctx.settings self.cancel = threading.Event() self.runner = Runner(s, catalog=self.ctx.catalog, emit=self._emit, cancel=self.cancel, feed=self.ctx.feed, confirm=self._ask_from_run, - move_data=self.ctx.move_data) + move_data=self.ctx.move_data, retune=self.ctx.retune) gb = getattr(self.runner, "download_gb", lambda: 0.0)() if not confirmed else 0.0 if gb >= 1.0: free = self.ctx.catalog.disk_free(s.data_dir or self.ctx.catalog.data_dir()) @@ -404,7 +405,7 @@ def run_monitor(info: dict, *, keep_default: bool = True, color: str = "auto") - def run_tui(settings: S.LaunchSettings | None, *, catalog: Catalog | None = None, feed: str | None = None, auto_run: bool = False, - save: bool = False, open_page: bool = False, move_data: bool = False) -> int: + save: bool = False, open_page: bool = False, move_data: bool = False, retune: bool = False) -> int: """Open the launch screens. `settings` given and `auto_run`: skip the questions and go straight to the run.""" catalog = catalog or Catalog() print("Looking at this computer…", flush=True) @@ -419,7 +420,7 @@ def run_tui(settings: S.LaunchSettings | None, *, catalog: Catalog | None = None s.name = f"{s.model or 'model'} {S.context_label(s.context)}" if save: store.save(s, s.name) - ctx = Ctx(catalog=catalog, computer=computer, rows=rows, settings=s, glyphs=Glyphs(), feed=feed, move_data=move_data) + ctx = Ctx(catalog=catalog, computer=computer, rows=rows, settings=s, glyphs=Glyphs(), feed=feed, move_data=move_data, retune=retune) app = LaunchApp(ctx, auto_run=auto_run) result = app.run() if getattr(app, "return_code", 0) not in (0, None) or getattr(app, "_exception", None) is not None: diff --git a/strata_cli/launch/cmd.py b/strata_cli/launch/cmd.py index 477d43b9..62807cf7 100644 --- a/strata_cli/launch/cmd.py +++ b/strata_cli/launch/cmd.py @@ -113,6 +113,9 @@ def add_parsers(sp: argparse._SubParsersAction) -> list[argparse.ArgumentParser] a.add_argument("--no-api-key", action="store_true", help="share beyond this computer without an API key (a warning is shown, nothing is refused)") o = p.add_argument_group("running") o.add_argument("--save-preset", metavar="NAME", help="save these choices under this name") + o.add_argument("--retune", action="store_true", + help="with --tune (quick is used when --tune is not given): measure again even when this model was already tuned on this computer " + "(by default a stored tuning result is reused and no measuring is done)") o.add_argument("--no-tui", action="store_true", help="no screens: serve, print the address and return") o.add_argument("--open", action="store_true", help="open the server's web page in your browser once it is serving (nothing opens a browser without this)") o.add_argument("--yes", action="store_true", help="do not ask questions (for one: whether to download the model on a computer with no engine yet)") @@ -562,7 +565,10 @@ def run_launch(a: argparse.Namespace) -> int: return _tui(app.run_tui, None, catalog=catalog, feed=a.feed, open_page=bool(a.open)) if catalog is None: return fail("This Terminal does not carry the model list. Install it again with the one-line installer.", 5) - runner = Runner(s, catalog=catalog, emit=_printer(machine), feed=a.feed, assume_yes=bool(a.yes or machine), confirm=_ask_yes) + if getattr(a, "retune", False) and s.tune == "skip": + s.tune = "quick" + runner = Runner(s, catalog=catalog, emit=_printer(machine), feed=a.feed, assume_yes=bool(a.yes or machine), confirm=_ask_yes, + retune=bool(getattr(a, "retune", False))) try: runner.check_own_files() # the person's own files: a wrong path is said now, and the files name the model except LaunchError as e: @@ -618,7 +624,7 @@ def run_launch(a: argparse.Namespace) -> int: if interactive and have_choices: from strata_cli.launch import app return _tui(app.run_tui, s, catalog=catalog, feed=a.feed, auto_run=True, save=bool(a.save_preset), open_page=bool(a.open), - move_data=move_data) + move_data=move_data, retune=bool(getattr(a, "retune", False))) try: result = runner.run() except LaunchError as e: diff --git a/strata_cli/launch/runner.py b/strata_cli/launch/runner.py index c51f0967..078ccc9f 100644 --- a/strata_cli/launch/runner.py +++ b/strata_cli/launch/runner.py @@ -323,8 +323,10 @@ class Runner: def __init__(self, s: S.LaunchSettings, *, catalog: Catalog | None = None, emit: Callable[[Event], None] | None = None, cancel: threading.Event | None = None, feed: str | None = None, python: str | None = None, components_dir: str | None = None, hardware: dict | None = None, - confirm: Callable[[str, str], bool] | None = None, assume_yes: bool = False, move_data: bool = False): + confirm: Callable[[str, str], bool] | None = None, assume_yes: bool = False, move_data: bool = False, + retune: bool = False): self.s = s + self.retune = retune # --retune: measure again even when a tuning result for this model on this computer is stored self.move_data = move_data # --move-data / the answer "move": the model files already here go to the new --data-dir self.confirm = confirm # asked (title, text) before a model is downloaded with no engine; True = go on self.assume_yes = assume_yes # --yes: no questions @@ -890,16 +892,23 @@ class Runner: if s.tune == "skip": return from strata_control import presets - if s.tuned_preset: - try: - presets.find(s.tuned_preset) - self.emit("tune", "This preset is already tuned for this computer.", 100) + if not self.retune: + if s.tuned_preset: + try: + presets.find(s.tuned_preset) + self.emit("tune", "This preset is already tuned for this computer.", 100) + return + except presets.PresetError: + pass + elif s.tuned_at: # measured before: the standard settings were already the fastest, nothing was saved + self.emit("tune", f"Already tuned on {s.tuned_at}: this computer runs best with the standard settings. Nothing to do.", 100) + return + stored = self.stored_tuning() + if stored: # the same model and choices were tuned on this computer before (a run by flags, no preset) + s.tuned_preset, s.tuned_at = stored["id"], stored["date"] + self.emit("tune", f"This model was already tuned for this computer on {stored['date']} ('{stored['name']}'), so that result is used " + "and nothing is measured again. Add --retune to measure again.", 100) return - except presets.PresetError: - pass - elif s.tuned_at: # measured before: the standard settings were already the fastest, nothing was saved - self.emit("tune", f"Already tuned on {s.tuned_at}: this computer runs best with the standard settings. Nothing to do.", 100) - return mode = "thorough" if s.tune == "thorough" else "quick" status_file = launch_dir() / f"{self.name}.tune.json" try: @@ -945,6 +954,22 @@ class Runner: s.tuned_at = time.strftime("%Y-%m-%d") self.emit("tune", f"Tuning result: {word}. {sentence}", 100, level="info" if word in ("faster", "already fastest") else "warn", word=word) + def stored_tuning(self) -> dict | None: + """The tuning result stored for this launch's model, choices and computer (the active preset of its slot in the shared presets store, + which the start applies), or None. A result whose computer changed in a way that matters (a different card, processor or memory, + another model file) is not reused: it is measured again.""" + if self.config_path is None: + return None + from strata_control import presets + try: + _cfg, info = presets.overlay(read_config(self.config_path)) + if not info.get("applied") or not info.get("preset"): + return None + made = str(presets.load(info["preset"]["id"]).get("created") or "")[:10] + return {"id": info["preset"]["id"], "name": info["preset"]["name"], "date": made or time.strftime("%Y-%m-%d")} + except Exception: # noqa: BLE001 - an unreadable store means: measure + return None + tune_word: str | None = None # ---- 5. start diff --git a/tests/terminal/test_flow2_022.py b/tests/terminal/test_flow2_022.py index 2cd8c38f..5a6d2f31 100644 --- a/tests/terminal/test_flow2_022.py +++ b/tests/terminal/test_flow2_022.py @@ -301,5 +301,87 @@ class QuietStep(Base): +class StoredTuning(Base): + """Finding 6: a run by flags (no saved preset) reuses the tuning result stored for the same model, choices and computer.""" + + IDENT = {"hardware": {"hard": {"gpus": ["RTX 5070"], "cpu": "test", "logical": 16, "ram_gib": 64}, "soft": {}}, + "engine": {"backend": "cuda"}, "model": {"sha256_shard1": "abc"}} + + def setUp(self): + super().setUp() + from strata_control import presets + self.presets = presets + self.cfg_path = self.base / "launch.json" + self.cfg = {"args": ["--native", "x/model-00001-of-00002.gguf", "--max-context", "32768", "--kv", "int8"], "model_name": "qwen"} + self.cfg_path.write_text(json.dumps(self.cfg), encoding="utf-8") + self.ident = json.loads(json.dumps(self.IDENT)) + p = mock.patch.object(presets, "current_identity", lambda cfg, **kw: self.ident) + p.start() + self.addCleanup(p.stop) + + def store(self): + return self.presets.create({"settings": {"args": {}, "env": {}, "per_request": {}}, "outcome": "improved"}, + choices=self.presets.choices_from_config(self.cfg), identity=self.ident, name="Q2 32K - Full speed") + + def runner_for_tune(self, retune=False): + r = self.runner(family="qwen", model="Q2_0", tune="quick") + r.retune = retune + r.config_path = self.cfg_path + return r + + def run_tune(self, r): + seen = [] + r.emit_cb = lambda ev: seen.append(ev.text) + with mock.patch.object(Runner, "_spawn", side_effect=RuntimeError("the tuner started")) as spawn: + try: + r.tune() + started = False + except RuntimeError: + started = True + return seen, started + + def test_nothing_stored_means_it_is_measured(self): + seen, started = self.run_tune(self.runner_for_tune()) + self.assertTrue(started) + + def test_a_stored_result_is_used_and_said(self): + saved = self.store() + r = self.runner_for_tune() + seen, started = self.run_tune(r) + self.assertFalse(started) + text = seen[-1] + self.assertIn("already tuned for this computer", text) + self.assertIn("Q2 32K - Full speed", text) + self.assertIn("--retune", text) + self.assertEqual(r.s.tuned_preset, saved["id"]) + self.assertTrue(r.s.is_tuned) + + def test_retune_measures_again(self): + self.store() + seen, started = self.run_tune(self.runner_for_tune(retune=True)) + self.assertTrue(started) + + def test_another_context_is_another_result(self): + self.store() + self.cfg["args"][3] = "65536" + self.cfg_path.write_text(json.dumps(self.cfg), encoding="utf-8") + seen, started = self.run_tune(self.runner_for_tune()) + self.assertTrue(started) + + def test_a_changed_graphics_card_is_measured_again(self): + self.store() + self.ident["hardware"]["hard"]["gpus"] = ["RTX 4090"] + seen, started = self.run_tune(self.runner_for_tune()) + self.assertTrue(started) + + def test_the_flag_asks_for_a_new_measurement_and_implies_quick(self): + code, out, err = run_cli(["launch", "--plan", "--output", "json", "--model", "q2_0", "--retune"]) + self.assertEqual(code, 0, err) + self.assertEqual(json.loads(out)["plan"]["settings"]["tune"], "quick") + code, out, err = run_cli(["launch", "--help"]) + self.assertIn("--retune", out) + + + if __name__ == "__main__": unittest.main()