Files
strata/strata_control/user_args.py
T
Niko1221 8aaf188aa9 Your own engine settings are kept: user_args, a reserve of 0 on a compute-only card, control vectors survive a load (#3215 #3270 #3216)
The app and setup rewrote the run config's "args" on every Load: the resource preference forced
--vram-reserve-mib to 1536 (2048 with pictures, a fifth of the card on "work") whatever the file said, and
a setup run (started by the Load when the context or pictures differ) dropped every option it did not
choose itself, the control vector options included.

- run config: "user_args" holds the options set by hand; "args_written" is the args as setup/the app last
  wrote them, so a hand edit of "args" is noticed and moved into user_args (strata_control/user_args.py,
  apps/host/server/native-user-args.mjs: the same rule)
- every start (serve/server.py engine_args), every setup run (setup.carry_user_args), every host write and load
  and a saved tuning put the person's options over the generated ones; the same option: theirs wins
- old files without args_written: setup keeps the options the old args had, the new ones lack and setup.py never names
  (plus the control vector group); a reserve that is not setup's own 700
- --vram-reserve-mib: any number the person set (Expert setting, user_args, the key, a value in args) is used as it
  is, 0 included; the safety default (1536/1024, 2048 with pictures, 20% on "work") stays for a card that drives a
  display or is not known to be headless; a card the probe says drives no display keeps 512 (700 with pictures);
  a 0.2.0-beta file where the app wrote the number itself is told from a changed one
- hardware: drives_display per card (nvidia-smi display_active; the Windows adapter with a desktop resolution for AMD)
- setup --vram-reserve-mib, the web settings page and `start --vram-reserve-mib` record the value as the person's
- docs/TROUBLESHOOTING.md: what is kept and what the app still chooses
2026-10-20 00:11:11 +02:00

119 lines
5.6 KiB
Python

"""The engine options a person set by hand in a model's run config (strata-<model>.json).
Setup and the app write ``"args"`` from their own choices, and used to write it again on every run, so an option added by
hand (the control vector options, a --vram-reserve-mib of 0, --spec, ...) was gone after the next Load (#3216 #3215 #3270).
The run config now keeps what the person set apart from what was generated:
* ``"user_args"``: the person's options, in the same form as ``"args"`` (``["--vram-reserve-mib", "0", ...]``). Every start
(serve/server.py engine_args) and every setup run applies them on top of the generated ones; for the same flag the
person's value wins. The person can edit this list by hand; the app's settings window writes the options it shows here.
* ``"args_written"``: the whole ``"args"`` as setup or the app last wrote it. An option in ``"args"`` that is not in this
copy was added or changed by hand since: ``capture`` moves it into ``"user_args"``. A config without the copy (one an
older Strata wrote) has nothing to compare with: setup then keeps the options of the old file that its new arguments do not
have (see ``dropped_by_setup``).
Writers that change ``"args"`` themselves (setup, the app's resource profile, a tuning run) call ``stamp`` after they have
captured, so their own changes are never mistaken for the person's.
"""
from __future__ import annotations
from typing import Any
USER_KEY = "user_args"
WRITTEN_KEY = "args_written"
#: option groups the engine takes more than one of, or that make sense only together: kept whole
#: (the control vector options of the experimental speed projection and the person's own vectors, #3216)
USER_KEPT_FLAGS = frozenset({"--control-vector", "--control-vector-scaled", "--control-vector-layer-range", "--cvec-mode",
"--cvec-dir"})
def groups(args: Any) -> list[list[str]]:
"""Split an argument list into groups: an option and the values after it (up to the next ``--option``)."""
out: list[list[str]] = []
for a in args if isinstance(args, list) else []:
a = str(a)
if a.startswith("--") or not out:
out.append([a])
else:
out[-1].append(a)
return out
def flat(gs: list[list[str]]) -> list[str]:
return [t for g in gs for t in g]
def flag_of(group: list[str]) -> str:
return group[0] if group and group[0].startswith("--") else ""
def merge(args: Any, user: Any) -> list[str]:
"""``args`` with the person's options on top: every group of ``args`` whose option the person set is replaced."""
ug = groups(user)
mine = {flag_of(g) for g in ug if flag_of(g)}
kept = [g for g in groups(args) if flag_of(g) not in mine]
return flat(kept + ug)
def apply(cfg: dict) -> list[str]:
"""Puts the config's ``user_args`` over its ``args`` (in place); returns the options applied."""
ug = groups(cfg.get(USER_KEY))
if not ug or not isinstance(cfg.get("args"), list):
return []
cfg["args"] = merge(cfg["args"], flat(ug))
return [flag_of(g) for g in ug if flag_of(g)]
def _by_flag(gs: list[list[str]]) -> dict[str, list[list[str]]]:
d: dict[str, list[list[str]]] = {}
for g in gs:
d.setdefault(flag_of(g), []).append(g)
return d
def capture(cfg: dict, skip=frozenset()) -> list[str]:
"""The person's options of a config: its ``user_args``, with the options of ``args`` that differ from ``args_written``
(added or changed by hand since the last write) on top. Without ``args_written`` only ``user_args`` is returned.
``skip``: options the caller chooses itself (setup's context, KV type, pictures, draft layer), never taken from ``args``."""
mine = _by_flag(groups(cfg.get(USER_KEY)))
written = cfg.get(WRITTEN_KEY)
if isinstance(written, list) and isinstance(cfg.get("args"), list):
cur, was = _by_flag(groups(cfg["args"])), _by_flag(groups(written))
for flag, gs in cur.items():
if flag and flag not in skip and was.get(flag) != gs:
mine[flag] = gs # added, or its value changed, by hand
for flag in list(mine):
if flag in was and flag not in cur:
del mine[flag] # taken out of "args" by hand: the person no longer wants it
return flat([g for gs in mine.values() for g in gs])
def stamp(cfg: dict, user: list[str] | None = None) -> None:
"""Records ``cfg["args"]`` as written (call after the last change to it) and ``user`` as the person's options."""
if user is not None:
if user:
cfg[USER_KEY] = list(user)
else:
cfg.pop(USER_KEY, None)
if isinstance(cfg.get("args"), list):
cfg[WRITTEN_KEY] = list(cfg["args"])
def dropped_by_setup(old_args: Any, new_args: Any, setup_flags: set[str] | frozenset[str], skip=frozenset()) -> list[str]:
"""For an old config with no ``args_written``: its options that the new arguments do not have and that setup never
writes (``setup_flags``: every option setup.py names), plus the control vector options. These were added by hand."""
have = {flag_of(g) for g in groups(new_args)}
out = [g for g in groups(old_args) if flag_of(g) and flag_of(g) not in have and flag_of(g) not in skip
and (flag_of(g) not in setup_flags or flag_of(g) in USER_KEPT_FLAGS)]
return flat(out)
def set_flag(user: list[str], flag: str, value: Any) -> list[str]:
"""``user`` with ``flag`` set to ``value`` (None: the person's option removed)."""
rest = [g for g in groups(user) if flag_of(g) != flag]
if value is not None:
rest.append([flag, str(value)])
return flat(rest)