okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
4cfeaa0f4
main
1"""tools/qwen35/compare_dumps.py - two runs of the Qwen3.6 parity dumps against each other (e.g. llama.cpp GPU vs 2llama.cpp CPU: the noise floor the strata-q36ref comparison is judged against). 3 4python tools/qwen35/compare_dumps.py DIR_A KIND_A DIR_B KIND_B (KIND: llama or strata) 5Per prompt: greedy tokens equal up to the answer's end (llama.cpp's first <|im_end|> in run A), the first divergence's 6top-1/top-2 margins, and the layer outputs' median and max ||a-b||/||b|| over the positions before the divergence. 7""" 8from __future__import annotations 9 10import sys 11from pathlib import Path 12 13import numpy as np 14 15N_EMBD ,N_LAYER ,EOS = 2048 ,40 ,248046 16 17 18def gen_of (d :Path ,tag :str ,kind :str )-> list [int ]: 19f = d / (f" { tag } .strata.gen" if kind == "strata" else f" { tag } .llama.log" ) 20for line in f .read_text (encoding = "utf-8" ).splitlines (): 21if line .startswith ("gen:" ): 22return [int (x )for x in line .split (":" ,1 )[1 ].split ()] 23raise SystemExit (f"no gen line in { f } " ) 24 25 26def prompt_len (d :Path ,tag :str )-> int : 27for line in (d / f" { tag } .llama.log" ).read_text (encoding = "utf-8" ).splitlines (): 28if line .startswith ("prompt:" ): 29return len (line .split (":" ,1 )[1 ].split ()) 30raise SystemExit ("no prompt line" ) 31 32 33def main ()-> int : 34da ,ka ,db ,kb = Path (sys .argv [1 ]),sys .argv [2 ],Path (sys .argv [3 ]),sys .argv [4 ] 35tags = sorted ({p .name .split ("." )[0 ]for p in da .glob (f"p*. { ka } .layers" )}) 36rows = [] 37for tag in tags : 38ga ,gb = gen_of (da ,tag ,ka ),gen_of (db ,tag ,kb ) 39upto = min (len (ga ),len (gb )) 40if EOS in ga [:upto ]: 41upto = ga .index (EOS )+ 1 42same = 0 43while same < upto and ga [same ]== gb [same ]: 44same += 1 45la = np .fromfile (da / f" { tag } . { ka } .logits" ,dtype = np .float32 ).reshape (- 1 ,4 ) 46lb = np .fromfile (db / f" { tag } . { kb } .logits" ,dtype = np .float32 ).reshape (- 1 ,4 ) 47A = np .fromfile (da / f" { tag } . { ka } .layers" ,dtype = np .float32 ).reshape (- 1 ,N_LAYER ,N_EMBD ) 48B = np .fromfile (db / f" { tag } . { kb } .layers" ,dtype = np .float32 ).reshape (- 1 ,N_LAYER ,N_EMBD ) 49npos = min (len (A ),len (B ),prompt_len (da ,tag )+ same ) 50nb = np .linalg .norm (B [:npos ],axis = 2 ) 51na = np .linalg .norm (A [:npos ],axis = 2 ) 52err = np .where ((nb > 0 )& (na > 0 ),np .linalg .norm (A [:npos ]- B [:npos ],axis = 2 )/ np .maximum (nb ,1e-30 ),np .nan ) 53div = "" 54if same < upto : 55div = (f"; step { same } : A { int ( la [ same , 0 ]) } (margin { la [ same , 1 ] - la [ same , 3 ]:.3f } ) vs B { int ( lb [ same , 0 ]) } " 56f"(margin { lb [ same , 1 ] - lb [ same , 3 ]:.3f } )" ) 57per_layer = np .nanmedian (err ,axis = 0 ) 58rows .append ((same == upto ,np .nanmedian (err ),np .nanmax (err ))) 59f" { tag } : { same } / { upto } equal; layer err median { np . nanmedian ( err ):.4f } max { np . nanmax ( err ):.4f } ; " 60f"median by layer 0/10/20/30/39: " + " " .join (f" { per_layer [ i ]:.4f } " for i in (0 ,10 ,20 ,30 ,39 ))+ div ) 61f"answers identical: { sum ( r [ 0 ] for r in rows ) } of { len ( rows ) } ; layer err median of medians " 62f" { np . median ([ r [ 1 ] for r in rows ]):.4f } , worst max { max ( r [ 2 ] for r in rows ):.4f } " ) 63return 0 64 65 66if __name__ == "__main__" : 67sys .exit (main ())