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.8 KiB59 linesraw
1"""tools/test_qi21_pixels.py - the decoded picture to 8-bit pixels (src/image/vae/qi21_pixels.cpp) as
2stable-diffusion.cpp 2988060 makes them: tools/image/gate/data/qi21_pixels_fixture.bin holds 8,184 values of sd.cpp's
3decoded float picture (512x512, prompt 0, seed 42; 729 of them within 1e-3 of a rounding boundary) with the bytes of
4sd-cli's own PNG at the same places; plus edge values whose expected bytes are computed here with numpy's float32
5(v x 255 rounded, + 0.5 rounded, truncated; 0 / 255 at or outside the ends).  Compiled twice (-O2; -O3
6-ffp-contract=fast: the file must not fuse).  No GPU, no sd.cpp, no model file.
7
8    python -m pytest tools/test_qi21_pixels.py"""
9import shutil
10import struct
11import subprocess
12import tempfile
13import unittest
14from pathlib import Path
15
16import numpy as np
17
18ROOT = Path(__file__).resolve().parents[1]
19CXX = shutil.which("g++") or shutil.which("c++") or shutil.which("clang++")
20FIXTURE = ROOT / "tools" / "image" / "gate" / "data" / "qi21_pixels_fixture.bin"
21
22
23def edge_fixture(path: Path) -> None:
24    k = np.arange(256, dtype=np.float32)
25    v = np.concatenate([
26        np.array([-1.0, -0.0, 0.0, 1e-45, 1.0, 1.5, 0.99999994], dtype=np.float32),
27        k / np.float32(255), (k + np.float32(0.5)) / np.float32(255),
28        np.nextafter((k + np.float32(0.5)) / np.float32(255), np.float32(0)),
29        np.nextafter((k + np.float32(0.5)) / np.float32(255), np.float32(1))]).astype(np.float32)
30    m = (v * np.float32(255)).astype(np.float32)
31    s = (m + np.float32(0.5)).astype(np.float32)
32    want = np.where(v <= 0, 0, np.where(v >= 1, 255, np.clip(s, 0, 255).astype(np.int64))).astype(np.uint8)
33    path.write_bytes(struct.pack("<i", len(v)) + v.astype("<f4").tobytes() + want.tobytes())
34
35
36@unittest.skipUnless(CXX, "no C++ compiler")
37class PixelsMatchSdcpp(unittest.TestCase):
38    def build_and_check(self, flags):
39        with tempfile.TemporaryDirectory() as td:
40            exe = Path(td) / "strata-pixels-check"
41            r = subprocess.run([CXX, "-std=c++17", *flags, "-I", str(ROOT / "include"),
42                                str(ROOT / "src" / "image" / "vae" / "qi21_pixels.cpp"),
43                                str(ROOT / "src" / "image" / "vae" / "qi21_pixels_check_main.cpp"), "-o", str(exe)],
44                               capture_output=True, text=True, timeout=300)
45            self.assertEqual(r.returncode, 0, r.stderr[-3000:])
46            edges = Path(td) / "edges.bin"
47            edge_fixture(edges)
48            r = subprocess.run([str(exe), str(FIXTURE), str(edges)], capture_output=True, text=True, timeout=60)
49            self.assertIn("PIXELS-CHECK PASS", r.stdout, r.stdout)
50
51    def test_o2(self):
52        self.build_and_check(["-O2"])
53
54    def test_o3_contract_fast(self):
55        self.build_and_check(["-O3", "-ffp-contract=fast"])
56
57
58if __name__ == "__main__":
59    unittest.main()