okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
28ce98bf1
main
1"""gyro_column_profile.py - is the unevenness of agentionai's per-input-column error a property of the layer (an 2activation-aware objective shared by all experts that read the same input) or noise? Compares the per-column squared-error 3profile (rotated basis) between experts and between gate and up of one layer, for their file and for our plain encode. 4""" 5from __future__import annotations 6 7import argparse 8import mmap 9import pathlib 10import sys 11 12import numpy as np 13 14HERE = pathlib .Path (__file__ ).resolve ().parent 15sys .path .insert (0 ,str (HERE .parent )) 16sys .path .insert (0 ,str (HERE )) 17import gguf_reader # noqa: E402 18import hf_slice # noqa: E402 19import tqenc # noqa: E402 20 21 22def main ()-> int : 23ap = argparse .ArgumentParser () 24ap .add_argument ("--gguf" ,required = True ) 25ap .add_argument ("--repo" ,default = "Qwen/Qwen3.8-Flash-Next" ) 26ap .add_argument ("--layer" ,type = int ,default = 10 ) 27ap .add_argument ("--experts" ,default = "0,1,2,3" ) 28a = ap .parse_args () 29g = gguf_reader .GGUFFile (a .gguf ) 30fh = open (a .gguf ,"rb" ) 31mm = mmap .mmap (fh .fileno (),0 ,access = mmap .ACCESS_READ ) 32by = {t .name :t for t in g .tensors } 33sw = [int (x )for x in g .metadata ["prism.hadamard.sign_widths" ]] 34sv = np .array (g .metadata ["prism.hadamard.sign_values" ],dtype = np .float32 ) 35signs ,at = {},0 36for w in sw : 37signs [w ]= sv [at :at + w ] 38at += w 39repo = hf_slice .Repo (a .repo ) 40pre = "model.language_model.layers.%d.mlp.experts." % a .layer 41prof = {}# (who, role, expert) -> per-column mean squared error / mean 42for e in [int (x )for x in a .experts .split ("," )]: 43gu = repo .tensor (pre + "gate_up_proj" ,e )[0 ] 44for role ,W in (("gate" ,gu [:640 ]), ("up" ,gu [640 :])): 45ti = by ["blk.%d.ffn_%s_exps.weight" % (a .layer ,role )] 46t = tqenc .TYPE_BY_NAME [ti .type_name ] 47n_in ,n_out = ti .shape [0 ],ti .shape [1 ] 48bb = tqenc .block_bytes (t ) 49rb = n_in // 128 * bb 50raw = np .frombuffer (mm ,dtype = np .uint8 ,count = n_out * rb ,offset = g .data_start + ti .offset + e * n_out * rb ) 51Wd = tqenc .decode (raw ,t ).reshape (n_out ,n_in ) 52Wr = tqenc .rotate (W ,signs [n_in ]) 53packed ,_ ,_ ,_ = tqenc .encode (Wr ,t ,iters = 1 ,threads = 8 ) 54Wo = tqenc .decode (packed ,t ).reshape (n_out ,n_in ) 55for who ,Wq in (("theirs" ,Wd ), ("ours" ,Wo )): 56c = ((Wr - Wq ).astype (np .float64 )** 2 ).mean (axis = 0 ) 57# remove the part explained by the signal's own column energy (weights of large columns err more) 58prof [(who ,role ,e )]= c / c .mean () 59"expert %d %s fetched and encoded" % (e ,role ),flush = True ) 60es = [int (x )for x in a .experts .split ("," )] 61for who in ("theirs" ,"ours" ): 62cc = [np .corrcoef (prof [(who ,"gate" ,es [0 ])],prof [(who ,"gate" ,e )])[0 ,1 ]for e in es [1 :]] 63cu = [np .corrcoef (prof [(who ,"gate" ,e )],prof [(who ,"up" ,e )])[0 ,1 ]for e in es ] 64"%-7s corr of the per-column error profile: gate expert %d vs other experts' gate %s | gate vs up of the same expert %s" % ( 65who ,es [0 ]," " .join ("%.3f" % x for x in cc )," " .join ("%.3f" % x for x in cu ))) 66return 0 67 68 69if __name__ == "__main__" : 70sys .exit (main ())