okami.mom/strata

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

Niko1221Merge f-own-5b adfcf9028 into f-own-int (round 16: the strata-image test program and its parts, DiT runner, sampler, noise, PNG writer; hooked into no build)e4309aaf3

main
2.6 KiB61 linesraw
1"""tools/image/gate/test_dit_step_check.py - TEST-ONLY: dit_step_check.py on a fake folder (no model, no GPU).
2
3    python -m pytest tools/image/gate/test_dit_step_check.py"""
4import contextlib
5import io
6import sys
7import tempfile
8import unittest
9from pathlib import Path
10
11import numpy as np
12
13sys.path.insert(0, str(Path(__file__).resolve().parent))
14import dit_step_check as K  # noqa: E402
15
16
17def put(path: Path, a: np.ndarray, npy: bool = False) -> None:
18    path.parent.mkdir(parents=True, exist_ok=True)
19    if npy:
20        np.save(path, a)
21    else:
22        a.astype("<f4").tofile(path)
23
24
25class StepCheck(unittest.TestCase):
26    def test_table_and_rule(self):
27        rng = np.random.default_rng(3)
28        with tempfile.TemporaryDirectory() as td:
29            root, ours = Path(td) / "own5b", Path(td) / "ours"
30            for p, scale in ((0, 1.0), (5, 3.0)):
31                P = K.case_paths(root, f"512 p{p}")
32                ref = rng.standard_normal((64, 4, 4)).astype(np.float32)
33                noise = lambda s: ref + s * rng.standard_normal(ref.shape).astype(np.float32)  # noqa: E731
34                put(Path(str(P["ref"]) + "_f32.npy"), ref, True)
35                put(Path(str(P["ref"]) + "_f16op.npy"), noise(1e-3), True)
36                put(P["acc32"], noise(1e-3 * scale), True)
37                put(P["stock"], noise(1e-2))
38                put(P["native"], noise(1e-2))
39                put(P["d1d2"], noise(1e-3))
40                put(ours / P["dump"].parent.name / P["dump"].name / "vel.bin", noise(1e-3 * scale))
41            out = io.StringIO()
42            with contextlib.redirect_stdout(out):
43                K.main(["--root", str(root), "--ours", str(ours)])
44            text = out.getvalue()
45            self.assertIn("| 512 p0 |", text)
46            self.assertIn("| 512 p5 |", text)
47            f32_rule = next(line for line in text.splitlines() if line.startswith("RULE vs the float32"))
48            exact_rule = next(line for line in text.splitlines() if line.startswith("RULE vs the exact"))
49            self.assertIn("(iii) PASS", f32_rule)                 # 1e-3 vs 1e-2: ours closer than stock and native
50            self.assertIn("(ii) largest", f32_rule)
51            self.assertIn("FAIL", exact_rule)                     # p5 ours ~3x the exact-sum emulation
52
53    def test_case_paths(self):
54        P = K.case_paths(Path("/r"), "1024 p0")
55        self.assertEqual(P["acc32"].name, "1024_p0_acc32.npy")
56        self.assertEqual(K.case_paths(Path("/r"), "512 p6")["d1d2"].parent.parent.name, "z_ours_512")
57        self.assertEqual(K.case_paths(Path("/r"), "512 p2")["d1d2"].parent.parent.name, "d2_g15")
58
59
60if __name__ == "__main__":
61    unittest.main()