okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
150c2394f
main
1"""tools/image/gate/test_limits.py - TEST-ONLY: the gate's limits (limits.py) accept the floors they were set from, refuse a 2result just past the limit, and check_limits.py reads compare.py's and stage_cmp.py's files. 3 4python -m pytest tools/image/gate/test_limits.py""" 5import json 6import subprocess 7import sys 8import tempfile 9import unittest 10from pathlib import Path 11 12HERE = Path (__file__ ).resolve ().parent 13sys .path .insert (0 ,str (HERE )) 14import limits as L # noqa: E402 15 16 17class Limits (unittest .TestCase ): 18def test_every_floor_passes_its_limit (self ): 19for name ,e in L .LIMITS .items (): 20self .assertEqual (L .passes (name ,e ["floor" ]), [],name ) 21 22def test_just_past_the_limit_fails (self ): 23for name ,e in L .LIMITS .items (): 24for key ,lim in e ["limit" ].items (): 25if isinstance (lim ,bool ): 26self .assertTrue (L .passes (name , {** e ["floor" ],key :not lim }), (name ,key )) 27continue 28past = lim * 1.001 if key .startswith ("rel_l2" )else lim * 0.999 29self .assertTrue (L .passes (name , {** e ["floor" ],key :past }), (name ,key )) 30 31def test_the_limit_is_not_far_from_the_floor (self ): 32# about 1.2x the worst floor: a limit far looser than that would hide a real bug 33for name ,e in L .LIMITS .items (): 34for key ,lim in e ["limit" ].items (): 35fl = e ["floor" ].get (key ) 36if isinstance (lim ,bool )or fl is None or name == "vae_across_tiling" : 37continue 38if key .startswith ("rel_l2" ): 39self .assertLessEqual (lim / fl ,1.4 , (name ,key )) 40elif key .startswith ("psnr" ): 41self .assertLessEqual (fl - lim ,12.0 , (name ,key )) 42else : 43self .assertLessEqual (abs (fl - lim ),0.02 , (name ,key )) 44 45 46class CheckLimits (unittest .TestCase ): 47def run_cli (self ,* args ): 48return subprocess .run ([sys .executable ,"-I" ,"-B" ,str (HERE / "check_limits.py" ),* args ], 49capture_output = True ,text = True ) 50 51def test_pictures_and_stage_files (self ): 52with tempfile .TemporaryDirectory ()as d : 53pic = Path (d )/ "cmp.json" 54pic .write_text (json .dumps ({"summary" : {"pairs" :5 ,"bytes_equal" :0 ,"psnr_min" :56.6 ,"psnr_mean" :57.4 , 55"ssim_min" :0.9988 ,"ssim_mean" :0.999 }})) 56self .assertEqual (self .run_cli ("vae_same_tiling" ,str (pic )).returncode ,0 ) 57pic .write_text (json .dumps ({"summary" : {"pairs" :5 ,"bytes_equal" :0 ,"psnr_min" :44.0 ,"psnr_mean" :57.4 , 58"ssim_min" :0.9988 ,"ssim_mean" :0.999 }})) 59r = self .run_cli ("vae_same_tiling" ,str (pic )) 60self .assertEqual (r .returncode ,1 ,r .stdout ) 61st = Path (d )/ "stage.json" 62st .write_text (json .dumps ({"cond" : {"summary" : {"n" :8 ,"identical" :0 ,"rel_l2_max" :0.0603 , 63"cos_min" :0.9982 ,"token_cos_min" :0.9909 }}})) 64self .assertEqual (self .run_cli ("text_encoder_same_backend" ,str (st ),"cond" ).returncode ,0 ) 65self .assertEqual (self .run_cli ("text_encoder_same_backend" ,str (st ),"vel_0" ).returncode ,2 ) 66st .write_text (json .dumps ({"vel_0" : {"summary" : {"n" :8 ,"identical" :0 ,"rel_l2_max" :0.02 ,"cos_min" :0.9999 }}})) 67self .assertEqual (self .run_cli ("velocity" ,str (st ),"vel_0" ).returncode ,1 ) 68 69 70if __name__ == "__main__" : 71unittest .main ()