diff --git a/clean/fg/FORMATS.md b/clean/fg/FORMATS.md index bf6a3915..eb3d2bdf 100644 --- a/clean/fg/FORMATS.md +++ b/clean/fg/FORMATS.md @@ -32,8 +32,10 @@ delivered by the spec side; the code never guesses it (it raises, or takes the c - `"channel_order": "storage"`: the weights are not yet in natural axes. The natural view comes with a later map revision (v1.1), with the same shapes and spans; whether net2's stored bias order is the natural channel order is confirmed then too (OPEN until it is). -- A conversion of a real library will be checked by digests only (the converter compares every stage digest the map - gives); no value or byte of it is ever written into the repository or any public text. +- A conversion of a real library is checked by digests only (the converter compares every stage digest the map + gives); no value or byte of it is ever written into the repository or any public text. `tools/real_check.py` runs + that check and keeps only the verdict and digests (`python -m clean.fg.tools.real_check --exe --dll + `). ## CPU reference (`reference/ops.py`) diff --git a/clean/fg/README.md b/clean/fg/README.md index ea10b790..3db37a23 100644 --- a/clean/fg/README.md +++ b/clean/fg/README.md @@ -13,6 +13,8 @@ front end or the launcher yet. Status, open items and formats: [FORMATS.md](FORM | `tests/test_fg_reference.py` | the building blocks against plain loop versions, on synthetic data | | `../converter` (preset FG) | the converter reads a version-1 map with `"preset": "FG"`: one block source per weight blob; see the converter's README, "Preset FG" | | `../../data/fg.v1.map.json` | the spec side's converter map v1 (weights as stored); `tests/test_fg_map.py` pins its digest and checks it against `layout.py` | +| `tools/real_check.py` | runs the converter once on one given frame-generation library (execute-only) and records only the verdict, the map's digest, the file version and size, the matching stage digests and the converted file's digest; deletes the converted file. Offsets and bytes of the library are never recorded | +| `tests/test_fg_real_check.py` | the tool on made-up converter output and a stand-in converter | | `reference/config.py` | the settled per-call configuration: the multi-frame intermediate time (one single-precision division), the index checks and flags, UI recomposition (FG preset A / B) with the profile values that force it | | `tests/test_fg_config.py` | the intermediate time against exactly rounded quotients, the index rules, every forcing profile value | diff --git a/clean/fg/tests/test_fg_real_check.py b/clean/fg/tests/test_fg_real_check.py new file mode 100644 index 00000000..2e896fa8 --- /dev/null +++ b/clean/fg/tests/test_fg_real_check.py @@ -0,0 +1,94 @@ +# SPDX-License-Identifier: MIT +"""Frame generation (#96): the real-file check tool (clean/fg/tools/real_check.py) on made-up converter output and a +stand-in converter (a small Python script written by the test). No library file is read. + + python -m unittest clean.fg.tests.test_fg_real_check +""" +import json +import os +import shutil +import sys +import tempfile +import unittest + +from clean.fg.tools import real_check as R + +MAP_SHA = "ab" * 32 +LOG_OK = ("[wronggpu-convert] map sha256 %s, 42 stages, model: x\n" + "[wronggpu-convert] size: 7000000 bytes\n" + "[wronggpu-convert] file version: 310.9.1.0\n" + "[wronggpu-convert] source A01 at offset 1234567\n" + "[wronggpu-convert] all 42 stage digests match (0.27 s)\n") % MAP_SHA + +STUB = r''' +import os, sys +a = sys.argv[1:] +out = a[a.index("--out") + 1] +mode = os.environ.get("FG_STUB_MODE", "ok") +sys.stderr.write(%r) +if mode == "ok": + os.makedirs(out) + open(os.path.join(out, "model.wgcm"), "wb").write(b"made-up model file") + print("The model was converted successfully.") + sys.exit(0) +print("A made-up failure message.") +sys.exit(13) +''' % LOG_OK + + +class Parse(unittest.TestCase): + def test_pass(self): + r = R.parse(0, R.SUCCESS + "\n", LOG_OK) + self.assertEqual((r["verdict"], r["map_sha256"], r["file_version"], r["file_size"]), + ("pass", MAP_SHA, "310.9.1.0", 7000000)) + self.assertEqual((r["stages_in_map"], r["stage_digests_matched"]), (42, 42)) + + def test_fail_forms(self): + self.assertEqual(R.parse(13, "A made-up failure message.", LOG_OK)["verdict"], "fail") + self.assertEqual(R.parse(0, R.SUCCESS, LOG_OK.replace("all 42 stage", "all 41 stage"))["verdict"], "fail") + self.assertEqual(R.parse(0, R.SUCCESS, "")["verdict"], "fail") + self.assertEqual(R.parse(0, "", LOG_OK)["verdict"], "fail") + + def test_record_holds_no_offsets(self): + text = json.dumps(R.parse(0, R.SUCCESS, LOG_OK)) + self.assertNotIn("1234567", text) + self.assertNotIn("offset", text) + + +class Run(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.mkdtemp(prefix="fg-rc-test-") + self.stub = os.path.join(self.tmp, "stub.py") + with open(self.stub, "w", encoding="utf-8") as f: + f.write(STUB) + self.map = os.path.join(self.tmp, "map.json") + with open(self.map, "wb") as f: + f.write(b"{}") + + def tearDown(self): + os.environ.pop("FG_STUB_MODE", None) + shutil.rmtree(self.tmp, ignore_errors=True) + + def test_pass_removes_the_model(self): + work = os.path.join(self.tmp, "w1") + r = R.run([sys.executable, "-I", self.stub], self.map, "C:/made/up/path.dll", work) + self.assertEqual(r["verdict"], "pass") + self.assertEqual(r["model_size"], len(b"made-up model file")) + self.assertEqual(len(r["model_sha256"]), 64) + self.assertFalse(os.path.exists(os.path.join(work, "out"))) + self.assertNotIn("1234567", json.dumps(r)) + + def test_fail(self): + os.environ["FG_STUB_MODE"] = "fail" + r = R.run([sys.executable, "-I", self.stub], self.map, "C:/made/up/path.dll", os.path.join(self.tmp, "w2")) + self.assertEqual((r["verdict"], r["exit_code"], r["model_sha256"]), ("fail", 13, None)) + + def test_refuses_a_used_work_folder(self): + work = os.path.join(self.tmp, "w3") + os.makedirs(os.path.join(work, "out")) + with self.assertRaises(SystemExit): + R.run([sys.executable, "-I", self.stub], self.map, "x.dll", work) + + +if __name__ == "__main__": + unittest.main() diff --git a/clean/fg/tools/__init__.py b/clean/fg/tools/__init__.py new file mode 100644 index 00000000..548d2d44 --- /dev/null +++ b/clean/fg/tools/__init__.py @@ -0,0 +1 @@ +# SPDX-License-Identifier: MIT diff --git a/clean/fg/tools/real_check.py b/clean/fg/tools/real_check.py new file mode 100644 index 00000000..fd4642ec --- /dev/null +++ b/clean/fg/tools/real_check.py @@ -0,0 +1,104 @@ +# SPDX-License-Identifier: MIT +"""Frame generation (issue #96): check that our converter converts a player's own frame-generation library with the +preset-FG map, recording digests and the verdict only. + + python -m clean.fg.tools.real_check --exe --dll + [--map data/fg.v1.map.json] [--work ] [--record ] + +Execute-only: the converter reads the library; this tool never opens it. It runs the converter once (no --raw, a +console-less child below normal priority), keeps from its output only the verdict, the map's SHA-256, the library's +file version and size, and the number of matching stage digests, then deletes the converted model file it wrote +(after taking its SHA-256). Offsets, values and bytes of the library are never recorded or printed; the converter's +log is not kept. +""" +import argparse +import hashlib +import json +import os +import re +import subprocess +import sys +import tempfile + +SUCCESS = "The model was converted successfully." +MODEL = "model.wgcm" + + +def parse(code, stdout, stderr): + """The record of one converter run, from its exit code and output. Only the fields listed here are kept.""" + def one(pattern): + m = re.search(pattern, stderr) + return m.group(1) if m else None + + matched = one(r"all (\d+) stage digests match") + size = one(r"\bsize: (\d+) bytes") + rec = { + "exit_code": code, + "converted": code == 0 and SUCCESS in stdout, + "message": stdout.strip().splitlines()[0] if stdout.strip() else "", + "map_sha256": one(r"map sha256 ([0-9a-f]{64})"), + "file_version": one(r"file version: (\d+(?:\.\d+){1,3})"), + "file_size": int(size) if size else None, + "stage_digests_matched": int(matched) if matched else 0, + } + stages = one(r"map sha256 [0-9a-f]{64}, (\d+) stages") + rec["stages_in_map"] = int(stages) if stages else None + rec["verdict"] = "pass" if (rec["converted"] and rec["stages_in_map"] and + rec["stage_digests_matched"] == rec["stages_in_map"]) else "fail" + return rec + + +def _popen_flags(): + if os.name != "nt": + return {} + return {"creationflags": subprocess.CREATE_NO_WINDOW | subprocess.BELOW_NORMAL_PRIORITY_CLASS} + + +def run(cmd_prefix, map_path, dll, work, threads=2, timeout=600): + """Runs `cmd_prefix + converter arguments` once and returns the record. `work`: an empty or missing folder; the + converter writes its model file there, which is removed afterwards (file by file, only what it wrote).""" + out = os.path.join(work, "out") + os.makedirs(work, exist_ok=True) + if os.path.exists(out): + raise SystemExit("work folder already has an out/ folder: %s" % out) + cmd = list(cmd_prefix) + ["--map", map_path, "--dll", dll, "--out", out, "--threads", str(threads)] + p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=timeout, **_popen_flags()) + rec = parse(p.returncode, p.stdout.decode("utf-8", "replace"), p.stderr.decode("utf-8", "replace")) + model = os.path.join(out, MODEL) + rec["model_sha256"] = None + if os.path.isfile(model): + with open(model, "rb") as f: + rec["model_sha256"] = hashlib.sha256(f.read()).hexdigest() + rec["model_size"] = os.path.getsize(model) + os.remove(model) + if os.path.isdir(out) and not os.listdir(out): + os.rmdir(out) + with open(map_path, "rb") as f: + rec["map_file_sha256"] = hashlib.sha256(f.read()).hexdigest() + return rec + + +def main(argv=None): + here = os.path.dirname(os.path.abspath(__file__)) + ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) + ap.add_argument("--exe", required=True, help="wronggpu-convert executable") + ap.add_argument("--dll", required=True, help="one exact path of the frame-generation library") + ap.add_argument("--map", default=os.path.normpath(os.path.join(here, "..", "..", "..", "data", "fg.v1.map.json"))) + ap.add_argument("--work", default=None, help="folder for the converter's output (removed again); default: a temp folder") + ap.add_argument("--threads", type=int, default=2) + ap.add_argument("--record", default=None, help="write the record (JSON) here as well") + a = ap.parse_args(argv) + work = a.work or tempfile.mkdtemp(prefix="fg-real-check-") + rec = run([a.exe], a.map, a.dll, work, threads=a.threads) + text = json.dumps(rec, indent=1, sort_keys=True) + print(text) + if a.record: + with open(a.record, "w", encoding="utf-8", newline="\n") as f: + f.write(text + "\n") + if a.work is None and not os.listdir(work): + os.rmdir(work) + return 0 if rec["verdict"] == "pass" else 1 + + +if __name__ == "__main__": + sys.exit(main())