okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e4309aaf3
main
1"""tools/image/gate/dit_acc32_cases.py - TEST-ONLY (OWN-5B): the stage check's nine cases against the emulation with 2float32 accumulation (dit_ref mode "f16acc32", ModeAcc32: f16 weights and GEMM inputs, float32 sums as cuBLAS GemmEx compute 32F 3does) next to the exact-accumulation emulation (dit_ref f16op) it replaces. 4 5python -I -B dit_acc32_cases.py <dit.gguf> --root R --out O 6R = the Spark own5b folder (pics/, d0ref/); writes O/<case>_acc32.npy and prints the table and the rule lines. 7""" 8from __future__import annotations 9 10import argparse 11import sys 12import time 13from pathlib import Path 14 15import numpy as np 16 17_HERE = Path (__file__ ).resolve ().parent 18if str (_HERE )not in sys .path : 19sys .path .insert (0 ,str (_HERE )) 20from dit_ref import DitWeights ,ModeAcc32 ,block ,head ,layout_positions ,load_dump ,prelude ,rope_table # noqa: E402 21from te_ref import compare # noqa: E402 22 23 24def vel (path )-> np .ndarray : 25return np .fromfile (path ,dtype = "<f4" ).reshape (64 ,- 1 ).T 26 27 28def run_acc32 (Wt ,d :Path )-> np .ndarray : 29xv ,xs = load_dump (d ,"dit_x_0" ) 30tv ,_ = load_dump (d ,"dit_t_0" ) 31cv ,cs = load_dump (d ,"cond" ) 32Ww ,Hh ,C = xs [0 ],xs [1 ],xs [2 ] 33x ,tt ,cond = xv .reshape (C ,Hh ,Ww ),float (tv [0 ]),cv .reshape (cs [1 ],cs [0 ]) 34L ,n = cond .shape [0 ],Hh * Ww 35c ,s = rope_table (layout_positions (L ,Hh ,Ww )) 36m = ModeAcc32 () 37mods ,scale ,xj = prelude (Wt ,m ,tt ,cond ,x ) 38rows = np .array ([1 ]* L + [0 ]* n ) 39for i in range (Wt .n_layer ): 40xj = block (Wt ,m ,i ,xj ,rows ,mods ,L ,c ,s ) 41return head (Wt ,m ,xj ,L ,scale ,Hh ,Ww ) 42 43 44def main (argv = None )-> int : 45ap = argparse .ArgumentParser () 46ap .add_argument ("gguf" ) 47ap .add_argument ("--root" ,required = True ) 48ap .add_argument ("--out" ,required = True ) 49a = ap .parse_args (argv ) 50R ,out = Path (a .root ),Path (a .out ) 51out .mkdir (parents = True ,exist_ok = True ) 52Wt = DitWeights (Path (a .gguf )) 53cases = [] 54for p in range (8 ): 55dn = f"dump_p { p } _s42" 56lo = "d0" if p < 4 else "z" 57ours = R / ("pics/d2_g15" if p < 4 else "pics/z_ours_512" )/ dn / "vel_0.bin" 58cases .append ((f"512 p { p } " ,R / f"pics/ { lo } _stock_512" / dn ,R / "d0ref" / dn , 59R / f"pics/ { lo } _stock_512" / dn / "vel_0.bin" ,R / f"pics/ { lo } _native_512" / dn / "vel_0.bin" ,ours )) 60dn = "dump_p0_s42" 61cases .append (("1024 p0" ,R / "pics/d0_stock_1024" / dn ,R / "d0ref/r1024" / dn ,R / "pics/d0_stock_1024" / dn / "vel_0.bin" , 62R / "pics/d0_native_1024" / dn / "vel_0.bin" ,R / "pics/z_ours_1024" / dn / "vel_0.bin" )) 63t0 = time .time () 64rows = [] 65for name ,dump ,ref ,st ,nat ,ours in cases : 66f32 = np .load (str (ref )+ "_f32.npy" ).reshape (64 ,- 1 ).T 67exact = compare (np .load (str (ref )+ "_f16op.npy" ).reshape (64 ,- 1 ).T ,f32 )["rel_l2" ] 68npy = out / (name .replace (" " ,"_" )+ "_acc32.npy" ) 69if npy .is_file (): 70v = np .load (npy ) 71else : 72v = run_acc32 (Wt ,dump ) 73np .save (npy ,v ) 74acc = compare (v .reshape (64 ,- 1 ).T ,f32 )["rel_l2" ] 75o = compare (vel (ours ),f32 )["rel_l2" ] 76s = compare (vel (st ),f32 )["rel_l2" ] 77nv = compare (vel (nat ),f32 )["rel_l2" ] 78oa = compare (vel (ours ),v .reshape (64 ,- 1 ).T )["rel_l2" ] 79rows .append ((name ,o ,exact ,acc ,s ,nv ,oa )) 80f"case { name } done { time . time () - t0 :.0f } s" ,flush = True ) 81"| case | ours | emulation, exact sums (before) | ratio | emulation, f32 sums (corrected) | ratio | stock | native | ours vs corrected emulation |" ) 82"|---|---|---|---|---|---|---|---|---|" ) 83for name ,o ,e ,ac ,s ,nv ,oa in rows : 84f"| { name } | { o :.5f } | { e :.5f } | { o / e :.3f } | { ac :.5f } | { o / ac :.3f } | { s :.5f } | { nv :.5f } | { oa :.5f } |" ) 85O = np .mean ([r [1 ]for r in rows ]) 86for label ,k in (("exact sums" ,2 ), ("f32 sums" ,3 )): 87E = np .mean ([r [k ]for r in rows ]) 88worst = max (rows ,key = lambda r :r [1 ]/ r [k ]) 89f"RULE vs emulation with { label } : (i) mean ours { O :.5f } / emulation { E :.5f } = { O / E :.3f } " 90f"( { 'PASS' if O <= 1.25 * E else 'FAIL' } ); (ii) largest ratio { worst [ 1 ] / worst [ k ]:.3f } at { worst [ 0 ] } " 91f"( { 'PASS' if all ( r [ 1 ] <= 1.5 * r [ k ] for r in rows ) else 'FAIL' } ); (iii) " 92f" { 'PASS' if all ( r [ 1 ] < r [ 4 ] and r [ 1 ] < r [ 5 ] for r in rows ) else 'FAIL' } " ) 93return 0 94 95 96if __name__ == "__main__" : 97sys .exit (main ())