Files
javis_bot/scripts/m0/m0lib/llm.py
javis-bot af0c773998 fix(m0): start the recommended model without the DFlash2 drafter
Strata 0.1.59 gives Qwen3.8-Flash-Next Q2_0 the DFlash2 drafter alone
(--draft-fallback none), but DFlash2 needs a native pack and the canonical
Q2_0 pack is not one, so the engine stops at start ("DFlash2 is off here:
it needs a native pack ... the start ends here"). Ask for the MTP drafter
instead; Strata falls back to no drafter when the MTP layer is missing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 13:29:58 +09:00

281 lines
12 KiB
Python

"""Stage: LLM through Strata (install, start, measure, restart timings).
Options (--opt key=value):
model=fast|recommended which preset to measure (default fast)
url=http://.../v1 measure an already running server instead (no Strata management)
context=32768 context size to start Strata with
"""
from __future__ import annotations
import json
import os
import re
import socket
import subprocess
import sys
import time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from m0lib.common import IS_WINDOWS, GpuSampler, StageResult, run_logged, say, scrub, stage_args # noqa: E402
from m0lib.llm_bench import bench_single, test_decode, test_images, test_parallel, test_vram_shrink, warmup # noqa: E402
from m0lib.llm_client import ChatClient, http_json # noqa: E402
PRESETS = {
"fast": {"family": "qwen36", "model": "ud-q4_k_xl", "label": "Qwen3.6-35B-A3B UD-Q4_K_XL"},
# Strata 0.1.59 gives Q2_0 the DFlash2 drafter alone, which needs a native pack; the canonical
# Q2_0 pack is not one, so the engine stops at start. The MTP drafter (or none) starts.
"recommended": {"family": "qwen", "model": "q2_0", "label": "Qwen3.8-Flash-Next Q2_0 (ISTA-DASLab)",
"drafting": "mtp"},
}
PORT = 8080
INSTALL_URL = "https://github.com/Niko1221/Strata/releases/latest/download/install.ps1"
class Strata:
def __init__(self, work: Path, log: Path):
self.home = work / "strata"
self.models = work / "models" / "llm"
self.log = log
# Keep Strata's engine, state and models inside the work folder: the
# installer honours STRATA_DATA_DIR, the engine components
# STRATA_COMPONENTS_DIR, and the per-user state follows LOCALAPPDATA.
self.env = dict(os.environ, STRATA_DATA_DIR=str(self.home), STRATA_NO_PATH="1",
STRATA_HOME=str(self.home / "home"), STRATA_COMPONENTS_DIR=str(self.home / "components"),
PYTHONUTF8="1")
if IS_WINDOWS:
self.env["LOCALAPPDATA"] = str(self.home / "localappdata")
(self.home / "localappdata").mkdir(parents=True, exist_ok=True)
self._flags: set[str] | None = None
@property
def exe(self) -> Path:
return self.home / "terminal" / "current" / "bin" / ("strata.cmd" if IS_WINDOWS else "strata")
def install(self) -> bool:
if self.exe.exists():
return True
self.home.mkdir(parents=True, exist_ok=True)
say(" Strata 터미널 설치 중 (작업 폴더 안에만 설치하고 PATH는 바꾸지 않습니다)")
if IS_WINDOWS:
cmd = ["powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", f"irm {INSTALL_URL} | iex"]
else:
cmd = ["sh", "-c", f"curl -fsSL {INSTALL_URL[:-3]}sh -o install.sh && sh install.sh"]
rc = run_logged(cmd, self.log, timeout=1800, env=self.env, cwd=str(self.home), echo=True)
return rc == 0 and self.exe.exists()
def cmd(self, *args: str) -> list[str]:
return [str(self.exe), *args]
def supported_flags(self) -> set[str]:
if self._flags is None:
p = subprocess.run(self.cmd("launch", "--help"), capture_output=True, text=True, env=self.env,
encoding="utf-8", errors="replace", timeout=120)
with open(self.log, "a", encoding="utf-8") as f:
f.write(scrub(p.stdout + p.stderr))
self._flags = set(re.findall(r"--[a-z0-9][a-z0-9-]*", p.stdout + p.stderr))
return self._flags
def launch_args(self, preset: dict, context: int, parallel: int, vision: str, extra_sets: list[str],
restart: bool) -> tuple[list[str], list[str]]:
want = [("--family", preset["family"]), ("--model", preset["model"]), ("--data-dir", str(self.models)),
("--context", str(context)), ("--vision", vision), ("--parallel", str(parallel)),
("--access", "local"), ("--port", str(PORT)), ("--no-api-key", None), ("--yes", None),
("--no-tui", None)]
if preset.get("drafting"):
want.append(("--drafting", preset["drafting"]))
if restart:
want.append(("--restart", None))
for s in extra_sets:
want.append(("--set", s))
flags = self.supported_flags()
args, dropped = [], []
for k, v in want:
if flags and k not in flags:
dropped.append(k)
continue
args += [k] + ([v] if v is not None else [])
return args, dropped
def plan(self, preset: dict, context: int) -> object:
args, _ = self.launch_args(preset, context, 1, "on", [], False)
args = [a for a in args if a not in ("--yes", "--no-tui")] + ["--plan", "--output", "json"]
p = subprocess.run(self.cmd("launch", *args), capture_output=True, text=True, env=self.env,
encoding="utf-8", errors="replace", timeout=600)
with open(self.log, "a", encoding="utf-8") as f:
f.write(scrub(p.stdout + p.stderr))
try:
return json.loads(p.stdout)
except ValueError:
return {"exit": p.returncode, "text": scrub(p.stdout[-3000:] + p.stderr[-2000:])}
def launch(self, preset: dict, context: int, parallel: int, vision: str = "on",
extra_sets: list[str] | None = None, restart: bool = False, timeout: float = 4 * 3600) -> dict:
args, dropped = self.launch_args(preset, context, parallel, vision, extra_sets or [], restart)
last = [0.0]
def progress(line: str) -> bool:
# Downloads print many progress lines; show one every 15 s.
if "%" in line:
if time.monotonic() - last[0] < 15:
return False
last[0] = time.monotonic()
return True
t0 = time.perf_counter()
rc = run_logged(self.cmd("launch", *args), self.log, timeout=timeout, env=self.env, echo=True,
echo_filter=progress)
t_cmd = time.perf_counter() - t0
# Only a successful launch counts: after a failed one, whatever still
# answers on the port is not the model that was asked for.
ready = wait_ready(f"http://127.0.0.1:{PORT}", timeout=1800) if rc == 0 else None
if ready is None:
self.keep_engine_log()
return {"args": scrub(" ".join(args)), "dropped_flags": dropped, "exit": rc,
"command_seconds": round(t_cmd, 1), "ready": ready is not None,
"ready_seconds": round(time.perf_counter() - t0, 1) if ready is not None else None}
def keep_engine_log(self) -> None:
"""Copy the newest engine log (scrubbed, its end) next to our log so the results zip carries it:
Strata's one-line reason can name a side effect, not the cause."""
found = sorted(self.home.rglob("*.engine.log"), key=lambda p: p.stat().st_mtime)
if not found:
return
src = found[-1]
text = src.read_text(encoding="utf-8", errors="replace")[-200_000:]
dest = self.log.with_name(f"{self.log.stem}_engine_{src.name.removesuffix('.engine.log')}.log")
dest.write_text(scrub(text), encoding="utf-8")
def stop(self) -> None:
run_logged(self.cmd("launch", "--stop"), self.log, timeout=300, env=self.env)
time.sleep(3)
def status(self) -> object:
p = subprocess.run(self.cmd("launch", "--status", "--output", "json"), capture_output=True, text=True,
env=self.env, encoding="utf-8", errors="replace", timeout=120)
try:
return json.loads(p.stdout)
except ValueError:
return scrub(p.stdout[-2000:])
def port_in_use(port: int) -> bool:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.settimeout(2)
return sock.connect_ex(("127.0.0.1", port)) == 0
def wait_ready(root: str, timeout: float) -> float | None:
t0 = time.perf_counter()
while time.perf_counter() - t0 < timeout:
try:
code, body = http_json("GET", root + "/v1/models", timeout=5)
if code == 200 and isinstance(body, dict) and body.get("data"):
return time.perf_counter() - t0
except Exception:
pass
time.sleep(2)
return None
def server_info(base: str) -> dict:
info = {}
for path in ("/models", "/status"):
try:
code, body = http_json("GET", base + path, timeout=10)
info[path] = body if code == 200 else {"http": code}
except Exception as e:
info[path] = {"error": str(e)}
return info
def measure_external(r: StageResult, url: str, context: int) -> None:
c = ChatClient(url)
r["server"] = {"url": url, "model": c.model, "info": server_info(c.base)}
with GpuSampler() as g:
bench_single(r, c, context)
test_parallel(r, c)
r["gpu"] = g.summary()
def measure_strata(r: StageResult, work: Path, which: str, context: int) -> None:
preset = PRESETS[which]
r["preset"] = dict(preset, key=which, context=context)
s = Strata(work, work / "logs" / f"strata_{which}.log")
if not s.install():
r.error("install", "Strata 설치 실패 (logs/strata_*.log 참고)")
return
r["plan"] = s.plan(preset, context)
r.save()
# 1) Start (the first start downloads the model and engine).
say(f" {preset['label']} 시작 중. 처음에는 모델을 내려받으므로 오래 걸립니다.")
s.stop()
if port_in_use(PORT):
# Another program owns the port; measuring it would report the wrong model.
r.error("port", f"다른 프로그램이 {PORT} 포트를 쓰고 있습니다. 그 프로그램을 끄고 다시 실행해 주세요.")
return
with GpuSampler(interval=2) as g:
r["launch_cold"] = s.launch(preset, context, parallel=1)
r["launch_cold"].update(g.summary())
r.save()
if not r["launch_cold"]["ready"]:
r.error("launch", "서버가 준비되지 않았습니다")
r["status_after_fail"] = s.status()
return
base = f"http://127.0.0.1:{PORT}/v1"
c = ChatClient(base)
r["server"] = {"model": c.model, "info": server_info(base), "status": s.status()}
with GpuSampler() as g:
bench_single(r, c, context)
r["gpu_single"] = g.summary()
r.save()
# 2) Restart with the files already in RAM/page cache: what a game-mode switch costs.
# Also: two slots, the elastic VRAM cache and the GPU picture reader.
say(" 다시 시작 시간 측정 (동시 2개, VRAM 조절 켜기, 이미지 GPU 처리)")
r["launch_restart"] = s.launch(preset, context, parallel=2, vision="gpu", extra_sets=["vram-elastic"],
restart=True, timeout=3600)
r.save()
if r["launch_restart"]["ready"]:
c = ChatClient(base)
r["server_restart"] = {"status": s.status()}
for fn in (lambda: warmup(r, c), lambda: test_decode(r, c, key="decode_parallel2"), lambda: test_parallel(r, c),
lambda: test_images(r, c, key="images_gpu_reader"), lambda: test_vram_shrink(r, c)):
try:
fn()
except Exception as e:
r.error("restart tests", e)
r.save()
# 3) Game mode (b): switching from the recommended model to the fast one.
if which == "recommended" and (work / "results" / "llm_fast.json").exists():
say(" 게임 모드 (b): 권장 모델 -> 빠름 모델 전환 시간")
r["switch_to_fast"] = s.launch(PRESETS["fast"], context, parallel=1, restart=True, timeout=3600)
s.stop()
def main() -> int:
out, work, opts = stage_args()
which = opts.get("model", "fast")
context = int(opts.get("context", "32768"))
r = StageResult(f"llm_{which}", out)
try:
if opts.get("url"):
measure_external(r, opts["url"], context)
else:
measure_strata(r, work, which, context)
except Exception as e:
r.error("llm", e)
# No server reached means nothing was measured: "failed" so the next run retries it.
if "server" not in r.data:
r.save("failed")
else:
r.save("ok" if not r["errors"] else "partial")
return 0
if __name__ == "__main__":
sys.exit(main())