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.3 KiB47 linesraw
1"""tools/test_qi21_noise.py - our initial noise (src/image/noise/qi21_noise.cpp, Strata's Philox generator) against
2stable-diffusion.cpp 2988060's own PhiloxRNG as its x86 build computes it: tools/image/gate/data/qi21_noise_sha.json
3holds the sha256 of sd.cpp's noise for seeds 42, 1234, 7, 2026 at 65,536 and 262,144 values (the 512 and 1024
4latents), made by compiling sd.cpp's core/rng_philox.hpp with g++ -O3 on x86 (p1006).  Compiles our generator with
5the system C++ compiler; no GPU, no sd.cpp, no model file.
6
7    python -m pytest tools/test_qi21_noise.py"""
8import hashlib
9import json
10import shutil
11import subprocess
12import sys
13import tempfile
14import unittest
15from pathlib import Path
16
17ROOT = Path(__file__).resolve().parents[1]
18CXX = shutil.which("g++") or shutil.which("c++") or shutil.which("clang++")
19
20
21@unittest.skipUnless(CXX, "no C++ compiler")
22class NoiseMatchesSdcppX86(unittest.TestCase):
23    def test_seeds(self):
24        cases = json.loads((ROOT / "tools" / "image" / "gate" / "data" / "qi21_noise_sha.json").read_text())
25        self.assertEqual(len(cases), 8)
26        with tempfile.TemporaryDirectory() as td:
27            inc = Path(td) / "inc"
28            subprocess.run([sys.executable, "-I", "-B", str(ROOT / "tools" / "image" / "sdcpp_noise" / "apply.py"),
29                            "--prepare", str(inc)], check=True, capture_output=True)
30            exe = Path(td) / "strata-noise-check"
31            r = subprocess.run([CXX, "-std=c++17", "-O2", "-I", str(ROOT / "include"),
32                                "-I", str(ROOT / "tools" / "image" / "sdcpp_noise"), "-I", str(inc),
33                                str(ROOT / "src" / "image" / "noise" / "qi21_noise.cpp"),
34                                str(ROOT / "src" / "image" / "noise" / "qi21_noise_check_main.cpp"), "-o", str(exe)],
35                               capture_output=True, text=True, timeout=600)
36            self.assertEqual(r.returncode, 0, r.stderr[-3000:])
37            bad = []
38            for c in cases:
39                out = Path(td) / "n.bin"
40                subprocess.run([str(exe), "--write", str(c["seed"]), str(c["count"]), str(out)], check=True)
41                if hashlib.sha256(out.read_bytes()).hexdigest() != c["sha256"]:
42                    bad.append((c["seed"], c["count"]))
43            self.assertEqual(bad, [])
44
45
46if __name__ == "__main__":
47    unittest.main()