feat(m0): measure the LLM next to the STT, the STT on the CPU and a real game
gamemode now also starts the 2 GB expert placement with a 16K context and drops the --kv q4_0 and --mtp off rows the Strata 0.1.59 qwen36 engine ignores; the normal placement answers one request at a time. gamemode_stt loads the STT worker (stt.py mode=load) before the LLM picks its placement and keeps it transcribing ten speakers at once while the LLM is measured, so the VRAM left is the real game share. The game placements run with the STT on the GPU and on the CPU. gamemode_live repeats the game rows while the user runs a real game; it only runs when asked for (run_m0_game_live.bat) and is skipped when nobody is at the PC. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -454,19 +454,31 @@ def test_a_lying_model_fails_the_stage_and_the_next_model_is_measured(tmp_path):
|
||||
# --- gamemode stage: the one model in each placement the app switches to ----
|
||||
|
||||
|
||||
def _run_levels_stage(tmp_path, monkeypatch, fake_server, gpu, ready=lambda sets: True):
|
||||
def _run_levels_stage(tmp_path, monkeypatch, fake_server, gpu, ready=lambda sets: True, only="levels",
|
||||
extra_opts=(), stt=None):
|
||||
"""Run the stage with Strata replaced; ``gpu(running)`` is nvidia-smi's (used, total), where
|
||||
``running`` is the started placement ({"sets", "context", "kv", "mtp"}) or None."""
|
||||
``running`` is the started placement ({"sets", "context", "kv", "mtp"}) or None. ``stt`` is a
|
||||
_FakeStt whose loaded model adds to the used VRAM."""
|
||||
port = int(fake_server.rsplit(":", 1)[1].split("/")[0])
|
||||
monkeypatch.setattr(llm, "PORT", port)
|
||||
started = []
|
||||
running = {"now": None}
|
||||
if stt is not None:
|
||||
stt.running = running
|
||||
monkeypatch.setattr(llm, "SttLoad", stt.factory)
|
||||
base_gpu = gpu
|
||||
|
||||
def gpu(now):
|
||||
used, total = base_gpu(now)
|
||||
return used + stt.used_mib(), total
|
||||
|
||||
def launch(self, preset, context, parallel, vision="on", extra_sets=None, restart=False, timeout=0,
|
||||
kv=None, mtp=None):
|
||||
spec = {"parallel": parallel, "vision": vision, "sets": list(extra_sets or []), "context": context,
|
||||
"kv": kv, "mtp": mtp}
|
||||
started.append(spec)
|
||||
if stt is not None:
|
||||
stt.events.append("llm_start")
|
||||
running["now"] = spec if ready(extra_sets or []) else None
|
||||
return {"ready": running["now"] is not None}
|
||||
|
||||
@@ -482,8 +494,10 @@ def _run_levels_stage(tmp_path, monkeypatch, fake_server, gpu, ready=lambda sets
|
||||
monkeypatch.setattr(common, "gpu_memory_mib", lambda: gpu(running["now"]))
|
||||
out = tmp_path / "results" / "gamemode.json"
|
||||
out.parent.mkdir(parents=True)
|
||||
monkeypatch.setattr(sys, "argv", ["llm.py", "--out", str(out), "--work", str(tmp_path),
|
||||
"--opt", "model=fast", "--opt", "only=levels"])
|
||||
opts = ["--opt", "model=fast", "--opt", f"only={only}"]
|
||||
for o in extra_opts:
|
||||
opts += ["--opt", o]
|
||||
monkeypatch.setattr(sys, "argv", ["llm.py", "--out", str(out), "--work", str(tmp_path), *opts])
|
||||
llm.main()
|
||||
return json.loads(out.read_text(encoding="utf-8")), started, running
|
||||
|
||||
@@ -504,13 +518,18 @@ def _vram(running):
|
||||
return (2000 if running is None else 2000 + _model_mib(running)), 16303
|
||||
|
||||
|
||||
def _vram_20g(running):
|
||||
"""A 20 GB card, where the smaller game placements do leave the reserve."""
|
||||
return _vram(running)[0], 20480
|
||||
|
||||
|
||||
def _vram_big_floor(running):
|
||||
"""A card where even the smallest placement leaves the game less than its reserve."""
|
||||
return (2000 if running is None else 2000 + _model_mib(running) + 3000), 16303
|
||||
|
||||
|
||||
def test_gamemode_measures_every_placement_and_returns_to_normal(tmp_path, monkeypatch, fake_server):
|
||||
r, started, running = _run_levels_stage(tmp_path, monkeypatch, fake_server, _vram)
|
||||
r, started, running = _run_levels_stage(tmp_path, monkeypatch, fake_server, _vram_20g)
|
||||
assert r["stage"] == "gamemode" and r["status"] == "ok"
|
||||
assert [x["sets"] for x in started] == [lv["sets"] for lv in llm.LEVELS]
|
||||
# Each placement starts with its own context, conversation memory format and drafter.
|
||||
@@ -571,9 +590,9 @@ def test_placement_memory_format_and_drafter_reach_the_launch_command(tmp_path,
|
||||
|
||||
def test_a_placement_that_does_not_start_is_recorded_and_the_rest_still_run(tmp_path, monkeypatch, fake_server):
|
||||
r, started, _ = _run_levels_stage(tmp_path, monkeypatch, fake_server, _vram,
|
||||
ready=lambda sets: "expert-vram=1" not in sets)
|
||||
ready=lambda sets: "expert-vram=0" not in sets)
|
||||
assert len(started) == len(llm.LEVELS)
|
||||
failed = [lv["key"] for lv in llm.LEVELS if "expert-vram=1" in lv["sets"]]
|
||||
failed = [lv["key"] for lv in llm.LEVELS if "expert-vram=0" in lv["sets"]]
|
||||
row = next(x for x in r["levels"] if x["key"] == failed[0])
|
||||
assert row["switch_s"] is None and "decode_tok_s" not in row
|
||||
# A placement that never started is a gap in the measurement, not a pass.
|
||||
@@ -619,3 +638,218 @@ def test_a_measurement_error_inside_a_placement_marks_the_stage_partial(tmp_path
|
||||
r, _, _ = _run_levels_stage(tmp_path, monkeypatch, fake_server, _vram)
|
||||
assert r["status"] == "partial"
|
||||
assert any(e["error"] == "decode: TimeoutError" for e in r["errors"])
|
||||
|
||||
|
||||
def test_gamemode_measures_the_2g_expert_placement_with_a_16k_context(tmp_path, monkeypatch, fake_server):
|
||||
# The one placement between "2 GB, 32K" and "0 GB, 16K" that was never measured.
|
||||
_, started, _ = _run_levels_stage(tmp_path, monkeypatch, fake_server, _vram)
|
||||
assert any("expert-vram=2" in x["sets"] and x["context"] == 16384 for x in started)
|
||||
|
||||
|
||||
def test_normal_placement_answers_one_request_at_a_time(tmp_path, monkeypatch, fake_server):
|
||||
# One request at a time is the default; two only when a chat channel answers every message.
|
||||
_, started, _ = _run_levels_stage(tmp_path, monkeypatch, fake_server, _vram)
|
||||
assert started[0]["parallel"] == 1 and started[-1]["parallel"] == 1
|
||||
|
||||
|
||||
# --- gamemode_stt / gamemode_live: the LLM next to the STT that runs with it --
|
||||
|
||||
|
||||
STT_GPU_MIB = 2740 # M0 stt: the model 1300 MiB and 1440 MiB more for ten speakers at once
|
||||
|
||||
|
||||
class _FakeStt:
|
||||
"""Stands in for the STT worker process: on cuda its model holds VRAM while it is loaded."""
|
||||
|
||||
def __init__(self, fail_device=None, broken_at_load=False):
|
||||
self.events: list[str] = []
|
||||
self.devices: list[str] = []
|
||||
self.loaded: str | None = None
|
||||
self.running: dict = {}
|
||||
self.fail_device = fail_device
|
||||
self.broken_at_load = broken_at_load
|
||||
|
||||
def used_mib(self) -> int:
|
||||
return STT_GPU_MIB if self.loaded == "cuda" else 0
|
||||
|
||||
def factory(self, python, work, device, log):
|
||||
fake = self
|
||||
fake.devices.append(device)
|
||||
|
||||
class _One:
|
||||
def start(self, timeout=900):
|
||||
if fake.broken_at_load:
|
||||
return {"status": "failed", "errors": ["load: RuntimeError: CUDA failed"]}
|
||||
fake.loaded = device
|
||||
fake.events.append("stt_loaded")
|
||||
return {"status": "loaded", "load_s": 3.0}
|
||||
|
||||
def go(self):
|
||||
assert fake.running.get("now") is not None, "STT load must overlap a running LLM"
|
||||
fake.events.append("stt_go")
|
||||
|
||||
def finish(self, timeout=120):
|
||||
fake.events.append("stt_finish")
|
||||
errs = ["RuntimeError: CUDA out of memory"] if device == fake.fail_device else []
|
||||
return {"status": "done", "batch10_s": {"n": 4, "median": 0.9}, "single_s": {"n": 4, "median": 0.2},
|
||||
"errors": errs}
|
||||
|
||||
def close(self):
|
||||
fake.loaded = None
|
||||
|
||||
return _One()
|
||||
|
||||
|
||||
def test_together_stage_measures_the_llm_with_the_stt_loaded_and_busy(tmp_path, monkeypatch, fake_server):
|
||||
stt = _FakeStt()
|
||||
r, started, running = _run_levels_stage(tmp_path, monkeypatch, fake_server, _vram, only="together",
|
||||
extra_opts=["stt_python=py"], stt=stt)
|
||||
assert r["stage"] == "gamemode_stt" and r["status"] == "ok"
|
||||
assert len(started) == len(llm.TOGETHER)
|
||||
# The STT model is in VRAM before the LLM picks its placement, and its ten-speaker loop runs
|
||||
# while the LLM is measured (asserted by _FakeStt.go).
|
||||
for i in range(len(llm.TOGETHER)):
|
||||
assert stt.events[i * 4:(i + 1) * 4] == ["stt_loaded", "llm_start", "stt_go", "stt_finish"]
|
||||
rows = {x["key"]: x for x in r["levels"]}
|
||||
for lv in llm.TOGETHER:
|
||||
row = rows[lv["key"]]
|
||||
assert row["stt"]["device"] == lv["stt"]
|
||||
assert row["stt"]["batch10_s"]["n"] > 0 and row["decode_tok_s"]["n"] > 0
|
||||
# What is left for the game already has the STT taken out when it runs on the GPU.
|
||||
_, total = _vram(None)
|
||||
assert row["vram_free_mib"] == total - row["vram_peak_mib"]
|
||||
# Both places for the STT are measured in the same game placement.
|
||||
game = [lv for lv in llm.TOGETHER if lv["key"].startswith("game_")]
|
||||
assert {lv["stt"] for lv in game} == {"cuda", "cpu"}
|
||||
pairs = [(rows[a["key"]], rows[b["key"]]) for a in game for b in game
|
||||
if a["stt"] == "cuda" and b["stt"] == "cpu" and (a["sets"], a.get("context")) == (b["sets"], b.get("context"))]
|
||||
assert pairs
|
||||
for on_gpu, on_cpu in pairs:
|
||||
assert on_cpu["vram_free_mib"] - on_gpu["vram_free_mib"] == STT_GPU_MIB
|
||||
# A chat channel that answers every message runs two requests at once: measured with the STT too.
|
||||
assert any(lv["parallel"] == 2 for lv in llm.TOGETHER if lv["key"].startswith("normal"))
|
||||
assert stt.loaded is None and running["now"] is None # nothing is left running
|
||||
|
||||
|
||||
def test_stt_error_next_to_the_llm_marks_the_together_stage_partial(tmp_path, monkeypatch, fake_server):
|
||||
stt = _FakeStt(fail_device="cuda")
|
||||
r, _, _ = _run_levels_stage(tmp_path, monkeypatch, fake_server, _vram, only="together",
|
||||
extra_opts=["stt_python=py"], stt=stt)
|
||||
assert r["status"] == "partial"
|
||||
assert any("out of memory" in e["error"] for e in r["errors"])
|
||||
|
||||
|
||||
def test_stt_that_does_not_load_skips_that_placement_and_the_rest_still_run(tmp_path, monkeypatch, fake_server):
|
||||
stt = _FakeStt(broken_at_load=True)
|
||||
r, started, _ = _run_levels_stage(tmp_path, monkeypatch, fake_server, _vram, only="together",
|
||||
extra_opts=["stt_python=py"], stt=stt)
|
||||
assert started == [] # an LLM measured without its STT would be the wrong number
|
||||
assert len(stt.devices) == len(llm.TOGETHER)
|
||||
assert r["status"] == "failed"
|
||||
|
||||
|
||||
def test_together_stage_without_the_stt_environment_measures_nothing(tmp_path, monkeypatch, fake_server):
|
||||
r, started, _ = _run_levels_stage(tmp_path, monkeypatch, fake_server, _vram, only="together")
|
||||
assert started == [] and r["status"] == "failed"
|
||||
|
||||
|
||||
def test_live_stage_is_skipped_when_nobody_is_there_to_start_a_game(tmp_path, monkeypatch, fake_server):
|
||||
monkeypatch.setattr(llm, "interactive", lambda: False)
|
||||
stt = _FakeStt()
|
||||
r, started, _ = _run_levels_stage(tmp_path, monkeypatch, fake_server, _vram, only="live",
|
||||
extra_opts=["stt_python=py"], stt=stt)
|
||||
assert r["status"] == "skipped" and started == []
|
||||
|
||||
|
||||
def test_live_stage_measures_game_placements_while_the_game_runs(tmp_path, monkeypatch, fake_server):
|
||||
monkeypatch.setattr(llm, "interactive", lambda: True)
|
||||
monkeypatch.setattr(llm, "ask", lambda *a, **k: "y")
|
||||
monkeypatch.setattr(llm, "wait_enter", lambda *a, **k: "")
|
||||
monkeypatch.setattr(llm.time, "sleep", lambda s: None)
|
||||
stt = _FakeStt()
|
||||
r, started, running = _run_levels_stage(tmp_path, monkeypatch, fake_server, _vram, only="live",
|
||||
extra_opts=["stt_python=py"], stt=stt)
|
||||
assert r["stage"] == "gamemode_live" and r["status"] == "ok"
|
||||
# Only game placements: starting the normal one would take the game's VRAM.
|
||||
assert len(started) == len(llm.LIVE) and all(lv["key"].startswith("game_") for lv in llm.LIVE)
|
||||
assert {lv["stt"] for lv in llm.LIVE} == {"cuda", "cpu"}
|
||||
assert all(x["vram_idle_mib"] is not None for x in r["levels"]) # Windows and the game
|
||||
assert running["now"] is None and stt.loaded is None
|
||||
|
||||
|
||||
def _fake_kit_run(tmp_path, monkeypatch, *args):
|
||||
import m0
|
||||
ran = []
|
||||
monkeypatch.setattr(m0, "run_stage", lambda key, *a, **k: ran.append(key))
|
||||
monkeypatch.setattr(m0, "pack", lambda work: tmp_path / "x.zip")
|
||||
monkeypatch.setattr(m0.subprocess, "run", lambda *a, **k: None)
|
||||
monkeypatch.setattr(sys, "argv", ["m0.py", "--work", str(tmp_path / "w"), *args])
|
||||
m0.main()
|
||||
return ran
|
||||
|
||||
|
||||
def test_full_kit_run_leaves_out_the_stage_that_needs_a_running_game(tmp_path, monkeypatch):
|
||||
ran = _fake_kit_run(tmp_path, monkeypatch)
|
||||
assert "gamemode_stt" in ran and "gamemode_live" not in ran
|
||||
assert _fake_kit_run(tmp_path, monkeypatch, "--only", "gamemode_live", "--redo") == ["gamemode_live"]
|
||||
|
||||
|
||||
def test_stages_that_run_the_stt_next_to_the_llm_get_the_stt_python(tmp_path, monkeypatch):
|
||||
import m0
|
||||
cmds = []
|
||||
monkeypatch.setattr(m0.envs, "ensure_env", lambda name, work, log: Path(f"/venvs/{name}/python"))
|
||||
monkeypatch.setattr(m0.subprocess, "run", lambda cmd, **k: cmds.append(cmd) or type("P", (), {"returncode": 0}))
|
||||
for key in ("gamemode_stt", "gamemode"):
|
||||
m0.run_stage(key, "base", "llm.py", ["model=fast"], tmp_path, {})
|
||||
together, alone = cmds
|
||||
assert f"stt_python={Path('/venvs/stt/python')}" in together
|
||||
assert not any(str(x).startswith("stt_python=") for x in alone)
|
||||
|
||||
|
||||
def test_one_click_runners_cover_the_new_gamemode_stages():
|
||||
import m0
|
||||
bat = (Path(m0.KIT) / "run_m0_gamemode.bat").read_text(encoding="utf-8")
|
||||
assert "-Only gamemode,gamemode_stt" in bat
|
||||
live = (Path(m0.KIT) / "run_m0_game_live.bat").read_text(encoding="utf-8")
|
||||
assert "-Only gamemode_live" in live and "-Redo" in live
|
||||
|
||||
|
||||
def test_stt_load_loop_waits_for_go_and_runs_until_stop(tmp_path):
|
||||
from m0lib import stt
|
||||
go, stop = tmp_path / "go", tmp_path / "stop"
|
||||
calls = []
|
||||
|
||||
def batch():
|
||||
calls.append("b")
|
||||
if not go.exists():
|
||||
raise AssertionError("ran before go")
|
||||
|
||||
def single():
|
||||
calls.append("s")
|
||||
if calls.count("s") == 3:
|
||||
stop.write_text("")
|
||||
|
||||
go.write_text("")
|
||||
res = stt.load_loop(batch, single, go, stop)
|
||||
assert res["status"] == "done" and res["batch10_s"]["n"] == 3 and res["single_s"]["n"] == 3
|
||||
assert calls == ["b", "s"] * 3
|
||||
|
||||
|
||||
def test_stt_load_loop_keeps_an_out_of_memory_and_stops(tmp_path):
|
||||
from m0lib import stt
|
||||
go, stop = tmp_path / "go", tmp_path / "stop"
|
||||
go.write_text("")
|
||||
|
||||
def batch():
|
||||
raise RuntimeError("CUDA failed with error out of memory")
|
||||
|
||||
res = stt.load_loop(batch, lambda: None, go, stop)
|
||||
assert res["batch10_s"]["n"] == 0 and "out of memory" in res["errors"][0]
|
||||
|
||||
|
||||
def test_stt_load_loop_does_not_run_when_stopped_before_go(tmp_path):
|
||||
from m0lib import stt
|
||||
stop = tmp_path / "stop"
|
||||
stop.write_text("")
|
||||
res = stt.load_loop(lambda: 1 / 0, lambda: 1 / 0, tmp_path / "go", stop)
|
||||
assert res["status"] == "not_started" and res["errors"] == []
|
||||
|
||||
Reference in New Issue
Block a user