okami.mom/strata

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

Niko1221Merge f-own-5 56630caca into f-own-int (round 14: Strata's own VAE decoder for sd-cli and the images "vae_decoder" switch)736266e87

main
3.2 KiB65 linesraw
1"""tools/image/gate/vae_identity.py - TEST-ONLY (OWN-5 gate harness): the row-band decode (what Strata ships) against
2the one-pass reference decode, byte for byte, for every fixed latent of the stage sets named; and run to run.
3
4    python -I -B vae_identity.py <strata-vae-decode> <vae.safetensors> <pics root> <latent tag> [<latent tag> ...]
5
6For each <pics root>/<tag>/dump_<name>/latent.bin: decode with --stream off (one pass) and --stream on --repeat 2
7(row bands, twice in one process), and with two other band heights (--band-rows 48 and 100: other positions of the
8pixels in their GEMM blocks than the default 128 rows); prints the peak memory and time and IDENTICAL / DIFFERENT;
9exit 1 if anything differs (gate: all identical at 512, 1024 and 1536x1024, on CUDA and on HIP)."""
10from __future__ import annotations
11
12import re
13import subprocess
14import sys
15import tempfile
16from pathlib import Path
17
18OTHER_BAND_ROWS = (48, 100)
19LINE = re.compile(r"decode (\d+)x(\d+) run (\d+): ([\d.]+) ms, peak (\d+) MiB(?:.*(identical|DIFFERS))?")
20
21
22def run(exe, vae, lat, out, extra):
23    r = subprocess.run([exe, vae, str(lat), str(out)] + extra, capture_output=True, text=True)
24    runs = [m.groups() for m in map(LINE.search, r.stdout.splitlines()) if m]
25    return r.returncode, runs, r.stderr
26
27
28def main(argv) -> int:
29    if len(argv) < 4:
30        print(__doc__)
31        return 2
32    exe, vae, root, tags = argv[0], argv[1], Path(argv[2]), argv[3:]
33    bad = n = 0
34    with tempfile.TemporaryDirectory() as tmp:
35        for tag in tags:
36            for d in sorted((root / tag).glob("dump_p*_s*")):
37                name = f"{tag}/{d.name[len('dump_'):]}"
38                one, band = Path(tmp) / "one.f32", Path(tmp) / "band.f32"
39                rc1, r1, e1 = run(exe, vae, d / "latent.bin", one, ["--stream", "off"])
40                rc2, r2, e2 = run(exe, vae, d / "latent.bin", band, ["--stream", "on", "--repeat", "2"])
41                n += 1
42                if rc1 or rc2 or not r1 or not r2:
43                    bad += 1
44                    print(f"{name}: FAILED rc {rc1}/{rc2} {(e1 + e2).strip()[-300:]}")
45                    continue
46                ref = one.read_bytes()
47                same = ref == band.read_bytes()
48                others = []
49                for rows in OTHER_BAND_ROWS:
50                    rc3, r3, e3 = run(exe, vae, d / "latent.bin", band, ["--stream", "on", "--band-rows", str(rows)])
51                    ok = rc3 == 0 and bool(r3) and band.read_bytes() == ref
52                    others.append(f"{rows} rows {'IDENTICAL' if ok else 'DIFFERENT'}")
53                    same = same and ok
54                rtr = all(g[5] == "identical" for g in r2[1:])
55                bad += (not same) or (not rtr)
56                print(f"{name} {r1[0][0]}x{r1[0][1]}: one pass {float(r1[0][3]):.0f} ms peak {r1[0][4]} MiB, "
57                      f"bands {float(r2[0][3]):.0f} ms peak {r2[0][4]} MiB: "
58                      f"{'IDENTICAL' if same else 'DIFFERENT'} ({', '.join(others)}), bands run to run "
59                      f"{'identical' if rtr else 'DIFFERENT'}")
60    print(f"VAE-IDENTITY {'PASS' if bad == 0 else 'FAIL'}: {n - bad} of {n}")
61    return 0 if bad == 0 else 1
62
63
64if __name__ == "__main__":
65    sys.exit(main(sys.argv[1:]))