okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
736266e87
main
1"""tools/image/gate/vae_decode_set.py - TEST-ONLY (OWN-5 gate harness): decode the fixed latents of a stage set with 2Strata's own VAE decoder (strata-vae-decode, src/image/vae) and write the pictures as sd-cli would, so compare.py and 3check_limits.py can put them next to sd.cpp's decode of the same latents. 4 5python -I -B vae_decode_set.py <strata-vae-decode> <vae.safetensors> <pics root> <latent tag> <out tag> [--repeat N] [--stream off] 6 7<pics root>/<latent tag>/dump_<name>/latent.bin (run_set.py --dump) -> <pics root>/<out tag>/<name>.png (RGBA, sd.cpp's 88-bit conversion) and results.jsonl (decode ms, peak MiB, run-to-run identity when --repeat > 1). 9The gate for the VAE stage (limits.py vae_same_tiling, PSNR >= 45 dB and SSIM >= 0.998 on every picture): 10python -I -B compare.py <pics root> <sd.cpp tag> <out tag> then check_limits.py vae_same_tiling <cmp json> 11where <sd.cpp tag> is run_set.py --latent-from <latent tag> --steps 1 (sd.cpp's VAE on the same latents).""" 12from __future__import annotations 13 14import json 15import re 16import subprocess 17import sys 18import tempfile 19from pathlib import Path 20 21import numpy as np 22from PIL import Image 23 24sys .path .insert (0 ,str (Path (__file__ ).resolve ().parent )) 25import vae_ref as V # noqa: E402 26 27LINE = re .compile (r"decode (\d+)x(\d+) run (\d+): ([\d.]+) ms, peak (\d+) MiB \(weights\+work (\d+) MiB\)(.*)" ) 28OPEN = re .compile (r"open ([\d.]+) ms" ) 29 30 31def main (argv )-> int : 32if len (argv )< 5 : 33__doc__ ) 34return 2 35exe ,vae ,root ,src ,dst = argv [0 ],argv [1 ],Path (argv [2 ]),argv [3 ],argv [4 ] 36repeat = int (argv [argv .index ("--repeat" )+ 1 ])if "--repeat" in argv else 1 37stream = argv [argv .index ("--stream" )+ 1 ]if "--stream" in argv else "on" 38out = root / dst 39out .mkdir (parents = True ,exist_ok = True ) 40rc_all = 0 41with open (out / "results.jsonl" ,"w" ,encoding = "utf-8" )as res ,tempfile .TemporaryDirectory ()as tmp : 42for d in sorted ((root / src ).glob ("dump_p*_s*" )): 43name = d .name [len ("dump_" ):] 44f32 = Path (tmp )/ f" { name } .f32" 45r = subprocess .run ([exe ,vae ,str (d / "latent.bin" ),str (f32 ),"--repeat" ,str (repeat ), 46"--stream" ,stream ], 47capture_output = True ,text = True ) 48runs = [m .groups ()for m in map (LINE .match ,r .stdout .splitlines ())if m ] 49opened = [float (m .group (1 ))for m in map (OPEN .match ,r .stdout .splitlines ())if m ] 50rec = {"name" :name ,"rc" :r .returncode ,"open_ms" :opened [0 ]if opened else None ,"runs" : [ 51 {"ms" :float (g [3 ]),"peak_mib" :int (g [4 ]),"weights_work_mib" :int (g [5 ]), 52"identical_to_run0" :None if g [2 ]== "0" else "identical" in g [6 ]}for g in runs ]} 53if r .returncode != 0 or not runs : 54rec ["stderr" ]= r .stderr [- 2000 :] 55rc_all = 1 56res .write (json .dumps (rec )+ "\n" ) 57f" { name } : FAILED rc { r . returncode } { r . stderr . strip ()[ - 300 :] } " ) 58continue 59W ,H = int (runs [0 ][0 ]),int (runs [0 ][1 ]) 60img = np .fromfile (f32 ,dtype = np .float32 ).reshape (4 ,H ,W ).transpose (1 ,2 ,0 ) 61Image .fromarray (V .to_u8 (img ),"RGBA" ).save (out / f" { name } .png" ) 62res .write (json .dumps (rec )+ "\n" ) 63f" { name } : open { rec [ 'open_ms' ] } ms, decode " + ", " .join (f" { x [ 'ms' ]:.0f } ms" for x in rec ["runs" ])+ 64f", peak { rec [ 'runs' ][ 0 ][ 'peak_mib' ] } MiB" + 65 ("" if repeat == 1 else f", run to run { 'identical' if all ( x [ 'identical_to_run0' ] for x in rec [ 'runs' ][ 1 :]) else 'DIFFERENT' } " )) 66if repeat > 1 and not all (x ["identical_to_run0" ]for x in rec ["runs" ][1 :]): 67rc_all = 1 68return rc_all 69 70 71if __name__ == "__main__" : 72sys .exit (main (sys .argv [1 :]))