okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e4309aaf3
main
1"""tools/image/gate/dit_replay2.py - TEST-ONLY (OWN-5B trace, step 2): where does our stream pick up what makes a prompt 2far from the reference? 3 4--from K [K ...]: start the f16op emulation from OUR stream after block K (txt_KK.bin + img_KK.bin of a 5STRATA_DIT_TRACE_DIR dump; K = -1 = our prelude outputs txtin.bin + imgin.bin), run the rest of the blocks and the 6head in f16op, and give the velocity's rel L2 to the f32 reference velocity (dit_ref --out-dir npy). The first K 7where this jumps from the emulation's own value to ours is where our arithmetic put the difference in. 8--acc32: also run the whole call with f16 inputs and FLOAT32 accumulation in every GEMM (numpy float32 matmul, 9OpenBLAS summation order), i.e. the emulation with the accumulation our GPU really does, and give its distance. 10 11python -I -B dit_replay2.py <dit.gguf> --dump D --trace T --ref R.npy [--from K ...] [--acc32] 12""" 13from __future__import annotations 14 15import argparse 16import sys 17import time 18from pathlib import Path 19 20import numpy as np 21 22_HERE = Path (__file__ ).resolve ().parent 23if str (_HERE )not in sys .path : 24sys .path .insert (0 ,str (_HERE )) 25from dit_ref import DitWeights ,Mode ,ModeAcc32 ,block ,head ,layout_positions ,load_dump ,prelude ,rope_table # noqa: E402 26from te_ref import compare # noqa: E402 27 28F32 = np .float32 29 30 31def main (argv = None )-> int : 32ap = argparse .ArgumentParser () 33ap .add_argument ("gguf" ) 34ap .add_argument ("--dump" ,required = True ) 35ap .add_argument ("--trace" ,required = True ) 36ap .add_argument ("--ref" ,required = True ,help = "the f32 reference velocity (.npy, [64, H, W])" ) 37ap .add_argument ("--from" ,dest = "starts" ,type = int ,nargs = "*" ,default = []) 38ap .add_argument ("--acc32" ,action = "store_true" ) 39a = ap .parse_args (argv ) 40d ,t = Path (a .dump ),Path (a .trace ) 41name = d .name 42Wt = DitWeights (Path (a .gguf )) 43xv ,xs = load_dump (d ,"dit_x_0" ) 44tv ,_ = load_dump (d ,"dit_t_0" ) 45cv ,cs = load_dump (d ,"cond" ) 46Ww ,Hh ,C = xs [0 ],xs [1 ],xs [2 ] 47x ,tt ,cond = xv .reshape (C ,Hh ,Ww ),float (tv [0 ]),cv .reshape (cs [1 ],cs [0 ]) 48L ,n = cond .shape [0 ],Hh * Ww 49c ,s = rope_table (layout_positions (L ,Hh ,Ww )) 50ref = np .load (a .ref ).reshape (64 ,- 1 ).T 51ours_v = np .fromfile (t / "out.bin" ,dtype = "<f4" ).reshape (- 1 ,64 ) 52f"RESTART { name } | ours (no restart) | { compare ( ours_v , ref )[ 'rel_l2' ]:.6f } " ,flush = True ) 53nl = Wt .n_layer 54m = Mode (True ) 55mods ,scale ,_ = prelude (Wt ,m ,tt ,cond ,x ) 56rows = np .array ([1 ]* L + [0 ]* n ) 57t0 = time .time () 58for k in a .starts : 59if k < 0 : 60tx ,im = "txtin.bin" ,"imgin.bin" 61else : 62tx ,im = f"txt_ { k :02d } .bin" ,f"img_ { k :02d } .bin" 63xj = np .concatenate ([np .fromfile (t / tx ,dtype = "<f4" ).reshape (L ,- 1 ), 64np .fromfile (t / im ,dtype = "<f4" ).reshape (n ,- 1 )],0 ) 65for i in range (k + 1 ,nl ): 66xj = block (Wt ,m ,i ,xj ,rows ,mods ,L ,c ,s ) 67v = head (Wt ,m ,xj ,L ,scale ,Hh ,Ww ).reshape (64 ,- 1 ).T 68f"RESTART { name } | f16op from our stream after { 'the prelude' if k < 0 else 'block %d' % k } | " 69f" { compare ( v , ref )[ 'rel_l2' ]:.6f } | { compare ( v , ours_v )[ 'rel_l2' ]:.6f } to ours | { time . time () - t0 :.0f } s" , 70flush = True ) 71if a .acc32 : 72m = ModeAcc32 () 73mods ,scale ,xj = prelude (Wt ,m ,tt ,cond ,x ) 74for i in range (nl ): 75xj = block (Wt ,m ,i ,xj ,rows ,mods ,L ,c ,s ) 76v = head (Wt ,m ,xj ,L ,scale ,Hh ,Ww ).reshape (64 ,- 1 ).T 77f"ACC32 { name } | f16 inputs, float32 accumulation, whole call | { compare ( v , ref )[ 'rel_l2' ]:.6f } | " 78f" { compare ( v , ours_v )[ 'rel_l2' ]:.6f } to ours | { time . time () - t0 :.0f } s" ,flush = True ) 79return 0 80 81 82if __name__ == "__main__" : 83sys .exit (main ())