okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6abee7f39
main
1"""Teacher-forced logits, stock vs the experimental-speed-projection vector, on the same texts and fixed experts. 2 3python esp_kl.py run # the dumps (per-token path) 4python esp_kl.py compare # same top-1, mean/median KL(stock || projection), perplexities 5""" 6import json 7import os 8import subprocess 9import sys 10 11import numpy as np 12 13S = os .path .dirname (os .path .abspath (__file__ )) 14E = r"C:\Users\AI-Server\Desktop\Strata\Public\Engine" 15EXE = E + r"\build-dev\strata.exe" 16VEC = r"C:\Users\AI-Server\Desktop\Strata\experimental-speed-projection\Qwen3.8-Flash-Next-experimental-speed-projection.gguf" 17TEXTS = {"code" :S + r"\p512.ids" ,"doc" :S + r"\4k-02-long-doc-1k.ids" ,"chat" :S + r"\4k-04-chat-1k.ids" } 18OUT = S + r"\esp-kl" 19ARMS = {"stock" : [], 20"proj" : ["--control-vector-scaled" ,VEC + ":1.0" ,"--control-vector-layer-range" ,"4" ,"44" , 21"--cvec-mode" ,"project" ,"--cvec-dir" ,"per-layer" ]} 22 23 24def base_args (): 25c = json .load (open (S + r"\cfg-rel.json" )) 26a = list (c ["args" ]) 27i = a .index ("--expert-cache" ) 28a [i + 1 ]= "4000" 29j = a .index ("--prefill" )# the prompt one token at a time: a logits row per position 30del a [j :j + 2 ] 31return a + ["--pcie-frac" ,"0" ,"--adapt-swaps" ,"0" ],c 32 33 34def run (): 35os .makedirs (OUT ,exist_ok = True ) 36args ,c = base_args () 37env = dict (os .environ ) 38env ["PATH" ]= os .pathsep .join (c ["lib_dirs" ])+ os .pathsep + env ["PATH" ] 39for name ,path in TEXTS .items (): 40for arm ,extra in ARMS .items (): 41out = f" { OUT } \\ { name } - { arm } .bin" 42if os .path .exists (out ): 43continue 44cmd = [EXE ,* args ,* extra ,"--tokens-file" ,path ,"--max-new" ,"1" ,"--dump-logits" ,out ] 45with open (f" { OUT } \\ { name } - { arm } .log" ,"w" )as log : 46r = subprocess .run (cmd ,cwd = c ["cwd" ],stdout = log ,stderr = subprocess .STDOUT ,env = env ) 47name ,arm ,"exit" ,r .returncode ,flush = True ) 48 49 50def load (p ): 51h = np .fromfile (p ,dtype = np .int32 ,count = 2 ) 52return np .memmap (p ,dtype = np .float32 ,mode = "r" ,offset = 8 ,shape = (int (h [1 ]),int (h [0 ]))) 53 54 55def lsm (x ): 56x = x .astype (np .float64 ) 57m = x .max (axis =- 1 ,keepdims = True ) 58return x - (m + np .log (np .exp (x - m ).sum (axis =- 1 ,keepdims = True ))) 59 60 61def compare (): 62f" { 'text' :<6 } { 'tokens' :>7 } { 'same top-1' :>11 } { 'mean KL' :>9 } { 'median KL' :>11 } { 'p99 KL' :>8 } { 'ppl stock' :>10 } { 'ppl proj' :>9 } " ) 63allkl = [] 64for name ,path in TEXTS .items (): 65ids = [int (x )for x in open (path ).read ().replace ("," ," " ).split ()] 66a ,b = load (f" { OUT } \\ { name } -stock.bin" ),load (f" { OUT } \\ { name } -proj.bin" ) 67n = min (a .shape [0 ],b .shape [0 ],len (ids )- 1 ) 68kl ,same ,la ,lb = [],0 , [], [] 69for r in range (0 ,n ,64 ): 70pa ,pb = lsm (a [r :min (n ,r + 64 )]),lsm (b [r :min (n ,r + 64 )]) 71kl += list ((np .exp (pa )* (pa - pb )).sum (axis = 1 )) 72same += int ((pa .argmax (1 )== pb .argmax (1 )).sum ()) 73nxt = np .array (ids [r + 1 :min (n ,r + 64 )+ 1 ]) 74la += list (pa [np .arange (len (nxt )),nxt ]) 75lb += list (pb [np .arange (len (nxt )),nxt ]) 76kl = np .array (kl ) 77allkl += list (kl ) 78f" { name :<6 } { n :>7 } { 100 * same / n :>10.1f } % { kl . mean ():>9.3f } { np . median ( kl ):>11.4f } { np . percentile ( kl , 99 ):>8.2f } " 79f" { np . exp ( - np . mean ( la )):>10.3f } { np . exp ( - np . mean ( lb )):>9.3f } " ) 80allkl = np .array (allkl ) 81f"all { len ( allkl ) } tokens: mean KL { allkl . mean ():.3f } , max { allkl . max ():.2f } , p99 { np . percentile ( allkl , 99 ):.2f } " ) 82 83 84if __name__ == "__main__" : 85 {"run" :run ,"compare" :compare }[sys .argv [1 ]]()