okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-5 fccdb48a3 (image noise switch, step 1 only) into f-own-int150c2394f

main
3.5 KiB71 linesraw
1"""tools/image/gate/test_limits.py - TEST-ONLY: the gate's limits (limits.py) accept the floors they were set from, refuse a
2result just past the limit, and check_limits.py reads compare.py's and stage_cmp.py's files.
3
4    python -m pytest tools/image/gate/test_limits.py"""
5import json
6import subprocess
7import sys
8import tempfile
9import unittest
10from pathlib import Path
11
12HERE = Path(__file__).resolve().parent
13sys.path.insert(0, str(HERE))
14import limits as L  # noqa: E402
15
16
17class Limits(unittest.TestCase):
18    def test_every_floor_passes_its_limit(self):
19        for name, e in L.LIMITS.items():
20            self.assertEqual(L.passes(name, e["floor"]), [], name)
21
22    def test_just_past_the_limit_fails(self):
23        for name, e in L.LIMITS.items():
24            for key, lim in e["limit"].items():
25                if isinstance(lim, bool):
26                    self.assertTrue(L.passes(name, {**e["floor"], key: not lim}), (name, key))
27                    continue
28                past = lim * 1.001 if key.startswith("rel_l2") else lim * 0.999
29                self.assertTrue(L.passes(name, {**e["floor"], key: past}), (name, key))
30
31    def test_the_limit_is_not_far_from_the_floor(self):
32        # about 1.2x the worst floor: a limit far looser than that would hide a real bug
33        for name, e in L.LIMITS.items():
34            for key, lim in e["limit"].items():
35                fl = e["floor"].get(key)
36                if isinstance(lim, bool) or fl is None or name == "vae_across_tiling":
37                    continue
38                if key.startswith("rel_l2"):
39                    self.assertLessEqual(lim / fl, 1.4, (name, key))
40                elif key.startswith("psnr"):
41                    self.assertLessEqual(fl - lim, 12.0, (name, key))
42                else:
43                    self.assertLessEqual(abs(fl - lim), 0.02, (name, key))
44
45
46class CheckLimits(unittest.TestCase):
47    def run_cli(self, *args):
48        return subprocess.run([sys.executable, "-I", "-B", str(HERE / "check_limits.py"), *args],
49                              capture_output=True, text=True)
50
51    def test_pictures_and_stage_files(self):
52        with tempfile.TemporaryDirectory() as d:
53            pic = Path(d) / "cmp.json"
54            pic.write_text(json.dumps({"summary": {"pairs": 5, "bytes_equal": 0, "psnr_min": 56.6, "psnr_mean": 57.4,
55                                                   "ssim_min": 0.9988, "ssim_mean": 0.999}}))
56            self.assertEqual(self.run_cli("vae_same_tiling", str(pic)).returncode, 0)
57            pic.write_text(json.dumps({"summary": {"pairs": 5, "bytes_equal": 0, "psnr_min": 44.0, "psnr_mean": 57.4,
58                                                   "ssim_min": 0.9988, "ssim_mean": 0.999}}))
59            r = self.run_cli("vae_same_tiling", str(pic))
60            self.assertEqual(r.returncode, 1, r.stdout)
61            st = Path(d) / "stage.json"
62            st.write_text(json.dumps({"cond": {"summary": {"n": 8, "identical": 0, "rel_l2_max": 0.0603,
63                                                           "cos_min": 0.9982, "token_cos_min": 0.9909}}}))
64            self.assertEqual(self.run_cli("text_encoder_same_backend", str(st), "cond").returncode, 0)
65            self.assertEqual(self.run_cli("text_encoder_same_backend", str(st), "vel_0").returncode, 2)
66            st.write_text(json.dumps({"vel_0": {"summary": {"n": 8, "identical": 0, "rel_l2_max": 0.02, "cos_min": 0.9999}}}))
67            self.assertEqual(self.run_cli("velocity", str(st), "vel_0").returncode, 1)
68
69
70if __name__ == "__main__":
71    unittest.main()