mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 07:48:45 +00:00
image gate tools: run_strata_image.py runs strata-image over the gate set like run_set.py runs sd-cli (step 5, piece 3)
tools/image/gate/run_strata_image.py: one strata-image process per prompt x seed, run_set.py's gate prompts and file names (pics/<tag>/p<i>_s<seed>.png, .log, .json, results.jsonl with the stage times under run_set.py's names plus open_s / png_s and the memory per stage), so compare.py and the floor / speed tables read both alike. Test-only. Test: tools/test_run_strata_image.py with a stand-in program (command line, files, records, a failing picture); p1006 11 passed with the image part tests (p1006-20261111-0745-aa7e).
This commit is contained in:
1 parent
3c2dc2f137
commit
adfcf9028e
2 files changed
+173
No files matched your search
@@ -0,0 +1,100 @@
|
||||
"""tools/image/gate/run_strata_image.py - TEST-ONLY (OWN-5 gate harness, step 5): run the test program strata-image
|
||||
(src/image/qi21_image_main.cpp) over a prompt x seed set, one process per picture, and write what run_set.py writes for
|
||||
sd-cli, so compare.py, floor_table.py and speed_table.py read both alike.
|
||||
|
||||
python3 -I -B run_strata_image.py <root> <tag> <W>x<H> <models_dir> <prompt-indices e.g. 0-7> <seeds e.g. 42,1234>
|
||||
--exe PATH [--dump] [--min-free-gb N] [--threads N]
|
||||
|
||||
models_dir holds the three files under their published names (tools/image/qimage.py FILES: the Q8_0 DiT, the VAE, the
|
||||
text encoder). The prompts are run_set.py's PROMPTS (the I1/I2 gate set).
|
||||
Writes <root>/pics/<tag>/p<i>_s<seed>.png, .log and .json (strata-image --report) per picture, with --dump the stage
|
||||
tensors in <root>/pics/<tag>/dump_p<i>_s<seed>/ (stage_hooks.py names), and <root>/pics/<tag>/results.jsonl: rc,
|
||||
seconds, maxrss_mib, and from the report open_s, text_encode_s, sampling_s, vae_decode_s, png_s and the per-stage
|
||||
memory (ours_gb: the parts' own device memory; driver_gb: the driver's in-use peak above the start; host_gb)."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
HERE = Path(__file__).resolve().parent
|
||||
sys.path.insert(0, str(HERE))
|
||||
sys.path.insert(0, str(HERE.parent))
|
||||
from run_set import PROMPTS # noqa: E402
|
||||
|
||||
STAGE_KEYS = {"open": "open_s", "encode": "text_encode_s", "steps": "sampling_s", "decode": "vae_decode_s",
|
||||
"png": "png_s"}
|
||||
|
||||
|
||||
def parse_range(text: str) -> list[int]:
|
||||
lo, _, hi = text.partition("-")
|
||||
return list(range(int(lo), int(hi or lo) + 1))
|
||||
|
||||
|
||||
def record(tag: str, size: str, pi: int, seed: int, rc: int, secs: float, maxrss_kib: int, report: dict | None) -> dict:
|
||||
rec = {"tag": tag, "backend": "strata-image", "quant": "Q8_0", "size": size, "steps": 8, "prompt": pi,
|
||||
"seed": seed, "rc": rc, "seconds": round(secs, 2), "maxrss_mib": maxrss_kib >> 10}
|
||||
if report:
|
||||
rec["plan"] = report.get("plan")
|
||||
for st in report.get("stages", []):
|
||||
k = STAGE_KEYS.get(st.get("name"))
|
||||
if k:
|
||||
rec[k] = round(st["wall_ms"] / 1000.0, 3)
|
||||
stage = st["name"]
|
||||
for m in ("ours_gb", "driver_gb", "host_gb"):
|
||||
rec[f"{stage}_{m}"] = round(st[m], 3)
|
||||
return rec
|
||||
|
||||
|
||||
def main(argv: list[str]) -> int:
|
||||
if len(argv) < 6 or "--exe" not in argv:
|
||||
print(__doc__)
|
||||
return 2
|
||||
root, tag, size, models, pidx, seeds = argv[:6]
|
||||
root = Path(root).expanduser().resolve()
|
||||
exe = argv[argv.index("--exe") + 1]
|
||||
min_free = float(argv[argv.index("--min-free-gb") + 1]) if "--min-free-gb" in argv else 10.0
|
||||
threads = argv[argv.index("--threads") + 1] if "--threads" in argv else None
|
||||
w, _, h = size.partition("x")
|
||||
md = Path(models).expanduser()
|
||||
files = {"dit": md / "qwen-image-2.1-turbo-Q8_0.gguf", "vae": md / "qwen_image_2.1_vae_bf16.safetensors",
|
||||
"te": md / "Qwen3-VL-8B-Instruct-UD-Q4_K_XL.gguf"}
|
||||
outd = root / "pics" / tag
|
||||
outd.mkdir(parents=True, exist_ok=True)
|
||||
for pi in parse_range(pidx):
|
||||
for seed in [int(s) for s in seeds.split(",") if s.strip()]:
|
||||
if shutil.disk_usage(root).free / 1e9 < min_free:
|
||||
raise SystemExit("ABORT: disk under the floor")
|
||||
key = f"p{pi}_s{seed}"
|
||||
rep = outd / f"{key}.json"
|
||||
rep.unlink(missing_ok=True)
|
||||
cmd = [exe, "--te", str(files["te"]), "--dit", str(files["dit"]), "--vae", str(files["vae"]),
|
||||
"--prompt", PROMPTS[pi], "--width", w, "--height", h, "--seed", str(seed), "--steps", "8",
|
||||
"-o", str(outd / f"{key}.png"), "--report", str(rep)]
|
||||
if threads:
|
||||
cmd += ["--threads", threads]
|
||||
if "--dump" in argv:
|
||||
cmd += ["--dump", str(outd / f"dump_{key}")]
|
||||
t0 = time.monotonic()
|
||||
with open(outd / f"{key}.log", "w") as lf:
|
||||
proc = subprocess.Popen(cmd, stdout=lf, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)
|
||||
_, status, ru = os.wait4(proc.pid, 0)
|
||||
secs = time.monotonic() - t0
|
||||
rc = os.waitstatus_to_exitcode(status)
|
||||
report = json.loads(rep.read_text()) if rc == 0 and rep.is_file() else None
|
||||
rec = record(tag, size, pi, seed, rc, secs, ru.ru_maxrss, report)
|
||||
if rc != 0:
|
||||
rec["error"] = "\n".join((outd / f"{key}.log").read_text(errors="replace").strip().splitlines()[-8:])
|
||||
with open(outd / "results.jsonl", "a") as f:
|
||||
f.write(json.dumps(rec) + "\n")
|
||||
print(json.dumps(rec), flush=True)
|
||||
print("RUN-OK")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main(sys.argv[1:]))
|
||||
@@ -0,0 +1,73 @@
|
||||
"""tools/test_run_strata_image.py - tools/image/gate/run_strata_image.py with a stand-in for strata-image: the command
|
||||
line it builds (the three model files, the gate prompt, size, seed, 8 steps, output, report, dump), the files it writes
|
||||
(p<i>_s<seed>.png / .log / .json, results.jsonl) and the record (stage times under run_set.py's names, memory per stage,
|
||||
rc and the error tail of a failed picture). POSIX only (the runner uses os.wait4).
|
||||
|
||||
python -m pytest tools/test_run_strata_image.py"""
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
RUNNER = ROOT / "tools" / "image" / "gate" / "run_strata_image.py"
|
||||
sys.path.insert(0, str(RUNNER.parent))
|
||||
from run_set import PROMPTS # noqa: E402
|
||||
|
||||
STAND_IN = r'''#!/usr/bin/env python3
|
||||
import json, sys
|
||||
a = sys.argv[1:]
|
||||
open(sys.argv[0] + ".args", "a").write(json.dumps(a) + "\n")
|
||||
v = lambda k: a[a.index(k) + 1]
|
||||
if v("--seed") == "13":
|
||||
print("strata-image: something broke"); sys.exit(1)
|
||||
open(v("-o"), "wb").write(b"\x89PNG fake")
|
||||
st = [{"name": n, "wall_ms": ms, "gpu_ms": -1, "ours_gb": 1.5, "driver_gb": 2.0, "host_gb": 0.5, "note": ""}
|
||||
for n, ms in (("open", 700.0), ("encode", 150.0), ("steps", 9800.0), ("decode", 1200.0), ("png", 30.0))]
|
||||
json.dump({"plan": "resident", "stages": st}, open(v("--report"), "w"))
|
||||
print("STRATA-IMAGE PASS")
|
||||
'''
|
||||
|
||||
|
||||
@unittest.skipIf(os.name != "posix", "the runner uses os.wait4")
|
||||
class RunStrataImage(unittest.TestCase):
|
||||
def test_runs_and_records(self):
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
t = Path(td)
|
||||
exe = t / "fake-strata-image"
|
||||
exe.write_text(STAND_IN)
|
||||
exe.chmod(0o755)
|
||||
models = t / "models"
|
||||
models.mkdir()
|
||||
r = subprocess.run([sys.executable, "-I", "-B", str(RUNNER), str(t), "set1", "512x512", str(models), "1-2",
|
||||
"42,13", "--exe", str(exe), "--dump", "--min-free-gb", "0"],
|
||||
capture_output=True, text=True, timeout=120)
|
||||
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
|
||||
self.assertIn("RUN-OK", r.stdout)
|
||||
out = t / "pics" / "set1"
|
||||
rows = [json.loads(x) for x in (out / "results.jsonl").read_text().splitlines()]
|
||||
self.assertEqual([(x["prompt"], x["seed"], x["rc"]) for x in rows],
|
||||
[(1, 42, 0), (1, 13, 1), (2, 42, 0), (2, 13, 1)])
|
||||
ok = rows[0]
|
||||
self.assertEqual((ok["open_s"], ok["text_encode_s"], ok["sampling_s"], ok["vae_decode_s"], ok["png_s"]),
|
||||
(0.7, 0.15, 9.8, 1.2, 0.03))
|
||||
self.assertEqual((ok["steps_ours_gb"], ok["decode_driver_gb"], ok["plan"]), (1.5, 2.0, "resident"))
|
||||
self.assertIn("something broke", rows[1]["error"])
|
||||
self.assertTrue((out / "p1_s42.png").is_file() and (out / "p2_s42.json").is_file())
|
||||
args = [json.loads(x) for x in Path(str(exe) + ".args").read_text().splitlines()]
|
||||
a = args[0]
|
||||
v = lambda k: a[a.index(k) + 1]
|
||||
self.assertEqual(v("--prompt"), PROMPTS[1])
|
||||
self.assertEqual((v("--width"), v("--height"), v("--seed"), v("--steps")), ("512", "512", "42", "8"))
|
||||
self.assertEqual(Path(v("--dit")).name, "qwen-image-2.1-turbo-Q8_0.gguf")
|
||||
self.assertEqual(Path(v("--vae")).name, "qwen_image_2.1_vae_bf16.safetensors")
|
||||
self.assertEqual(Path(v("--te")).name, "Qwen3-VL-8B-Instruct-UD-Q4_K_XL.gguf")
|
||||
self.assertEqual(Path(v("--dump")).name, "dump_p1_s42")
|
||||
self.assertEqual(Path(v("-o")), out / "p1_s42.png")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user