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.2 KiB43 linesraw
1"""tools/test_qi21_sampler.py - our Turbo sampler step (src/image/sample/qi21_sampler.cpp) against stable-diffusion.cpp
22988060's own values: tools/image/gate/data/qi21_sampler_fixture.bin holds, for steps 0-6 of one picture (512x512,
3prompt 0, seed 42), 512 values spread over the latent of sd.cpp's DiT input x_k, its DiT output v_k and its next DiT
4input x_{k+1}, with the sigmas and sd.cpp's timestep bits (written by strata-sampler-check --fixture from the stage
5dumps).  Our step on x_k, v_k must give x_{k+1} byte for byte, our parsed schedule and timestep the same bits.
6Compiled twice: plain -O2, and -O3 with contraction allowed by default (-ffp-contract=fast; the file must still not
7fuse).  No GPU, no sd.cpp, no model file.
8
9    python -m pytest tools/test_qi21_sampler.py"""
10import shutil
11import subprocess
12import tempfile
13import unittest
14from pathlib import Path
15
16ROOT = Path(__file__).resolve().parents[1]
17CXX = shutil.which("g++") or shutil.which("c++") or shutil.which("clang++")
18FIXTURE = ROOT / "tools" / "image" / "gate" / "data" / "qi21_sampler_fixture.bin"
19
20
21@unittest.skipUnless(CXX, "no C++ compiler")
22class SamplerMatchesSdcpp(unittest.TestCase):
23    def build_and_check(self, flags):
24        with tempfile.TemporaryDirectory() as td:
25            exe = Path(td) / "strata-sampler-check"
26            r = subprocess.run([CXX, "-std=c++17", *flags, "-I", str(ROOT / "include"),
27                                str(ROOT / "src" / "image" / "sample" / "qi21_sampler.cpp"),
28                                str(ROOT / "src" / "image" / "sample" / "qi21_sampler_check_main.cpp"), "-o", str(exe)],
29                               capture_output=True, text=True, timeout=300)
30            self.assertEqual(r.returncode, 0, r.stderr[-3000:])
31            r = subprocess.run([str(exe), "--fixture-check", str(FIXTURE)], capture_output=True, text=True, timeout=60)
32            self.assertIn("SAMPLER-FIXTURE PASS", r.stdout, r.stdout[-3000:])
33            self.assertEqual(r.stdout.count("identical"), 7, r.stdout)
34
35    def test_o2(self):
36        self.build_and_check(["-O2"])
37
38    def test_o3_contract_fast(self):
39        self.build_and_check(["-O3", "-ffp-contract=fast"])
40
41
42if __name__ == "__main__":
43    unittest.main()