okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e4309aaf3
main
1"""tools/image/gate/test_dit_step_check.py - TEST-ONLY: dit_step_check.py on a fake folder (no model, no GPU). 2 3python -m pytest tools/image/gate/test_dit_step_check.py""" 4import contextlib 5import io 6import sys 7import tempfile 8import unittest 9from pathlib import Path 10 11import numpy as np 12 13sys .path .insert (0 ,str (Path (__file__ ).resolve ().parent )) 14import dit_step_check as K # noqa: E402 15 16 17def put (path :Path ,a :np .ndarray ,npy :bool = False )-> None : 18path .parent .mkdir (parents = True ,exist_ok = True ) 19if npy : 20np .save (path ,a ) 21else : 22a .astype ("<f4" ).tofile (path ) 23 24 25class StepCheck (unittest .TestCase ): 26def test_table_and_rule (self ): 27rng = np .random .default_rng (3 ) 28with tempfile .TemporaryDirectory ()as td : 29root ,ours = Path (td )/ "own5b" ,Path (td )/ "ours" 30for p ,scale in ((0 ,1.0 ), (5 ,3.0 )): 31P = K .case_paths (root ,f"512 p { p } " ) 32ref = rng .standard_normal ((64 ,4 ,4 )).astype (np .float32 ) 33noise = lambda s :ref + s * rng .standard_normal (ref .shape ).astype (np .float32 )# noqa: E731 34put (Path (str (P ["ref" ])+ "_f32.npy" ),ref ,True ) 35put (Path (str (P ["ref" ])+ "_f16op.npy" ),noise (1e-3 ),True ) 36put (P ["acc32" ],noise (1e-3 * scale ),True ) 37put (P ["stock" ],noise (1e-2 )) 38put (P ["native" ],noise (1e-2 )) 39put (P ["d1d2" ],noise (1e-3 )) 40put (ours / P ["dump" ].parent .name / P ["dump" ].name / "vel.bin" ,noise (1e-3 * scale )) 41out = io .StringIO () 42with contextlib .redirect_stdout (out ): 43K .main (["--root" ,str (root ),"--ours" ,str (ours )]) 44text = out .getvalue () 45self .assertIn ("| 512 p0 |" ,text ) 46self .assertIn ("| 512 p5 |" ,text ) 47f32_rule = next (line for line in text .splitlines ()if line .startswith ("RULE vs the float32" )) 48exact_rule = next (line for line in text .splitlines ()if line .startswith ("RULE vs the exact" )) 49self .assertIn ("(iii) PASS" ,f32_rule )# 1e-3 vs 1e-2: ours closer than stock and native 50self .assertIn ("(ii) largest" ,f32_rule ) 51self .assertIn ("FAIL" ,exact_rule )# p5 ours ~3x the exact-sum emulation 52 53def test_case_paths (self ): 54P = K .case_paths (Path ("/r" ),"1024 p0" ) 55self .assertEqual (P ["acc32" ].name ,"1024_p0_acc32.npy" ) 56self .assertEqual (K .case_paths (Path ("/r" ),"512 p6" )["d1d2" ].parent .parent .name ,"z_ours_512" ) 57self .assertEqual (K .case_paths (Path ("/r" ),"512 p2" )["d1d2" ].parent .parent .name ,"d2_g15" ) 58 59 60if __name__ == "__main__" : 61unittest .main ()