okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
28ce98bf1
main
1"""gyro_vs_original.py - the fair comparison: agentionai's Gyro quantization of Qwen3.8-Flash-Next against OUR encoder, both 2measured against the original BF16 expert weights (fetched tensor by tensor with HTTP range requests, ~10 MB per expert). 3 4For a layer and expert: W = original (gate, up from the fused gate_up_proj, down), rotated into the file's basis with the 5file's own sign vectors (prism.hadamard.sign_values, widths 2560 / 640): W' = H (s * w) per row and 128-block. 6agentionai: Wd = decode of the file's blocks 7ours: Wo = decode(encode(W')) at the same type (TQK6 gate/up, TQK7 down), several search settings 8Reported: relative MSE and SQNR of each against W'. 9 10python3 tools/trellis/gyro_vs_original.py --gguf <Gyro-S> --layers 0,20,39 --experts 0,5 --cfg fast,default,rot3 11""" 12from __future__import annotations 13 14import argparse 15import mmap 16import pathlib 17import sys 18import time 19 20import numpy as np 21 22HERE = pathlib .Path (__file__ ).resolve ().parent 23sys .path .insert (0 ,str (HERE .parent )) 24sys .path .insert (0 ,str (HERE )) 25import gguf_reader # noqa: E402 26import hf_slice # noqa: E402 27import tqenc # noqa: E402 28 29CFGS = { 30"fast" :dict (iters = 0 ,ncand = 1 ,scale_tries = 1 ), 31"default" :dict (iters = 1 ,ncand = 1 ,scale_tries = 1 ), 32"iters3" :dict (iters = 3 ,ncand = 1 ,scale_tries = 1 ), 33"rot3" :dict (iters = 3 ,ncand = 3 ,scale_tries = 1 ), 34"scale3" :dict (iters = 3 ,ncand = 3 ,scale_tries = 3 ), 35} 36 37 38def main ()-> int : 39ap = argparse .ArgumentParser () 40ap .add_argument ("--gguf" ,required = True ) 41ap .add_argument ("--repo" ,default = "Qwen/Qwen3.8-Flash-Next" ) 42ap .add_argument ("--layers" ,default = "0,20,39" ) 43ap .add_argument ("--experts" ,default = "0" ) 44ap .add_argument ("--cfg" ,default = "fast,default,rot3" ) 45ap .add_argument ("--threads" ,type = int ,default = 0 ) 46a = ap .parse_args () 47 48g = gguf_reader .GGUFFile (a .gguf ) 49fh = open (a .gguf ,"rb" ) 50mm = mmap .mmap (fh .fileno (),0 ,access = mmap .ACCESS_READ ) 51by_name = {t .name :t for t in g .tensors } 52sw = [int (x )for x in g .metadata ["prism.hadamard.sign_widths" ]] 53sv = np .array (g .metadata ["prism.hadamard.sign_values" ],dtype = np .float32 ) 54signs ,at = {},0 55for w in sw : 56signs [w ]= sv [at :at + w ] 57at += w 58"sign widths" ,sw ) 59repo = hf_slice .Repo (a .repo ) 60cfgs = a .cfg .split ("," ) 61tot = {k : [0.0 ,0.0 ]for k in ["agentionai" ]+ cfgs } 62tot_role = {} 63for layer in [int (x )for x in a .layers .split ("," )]: 64pre = "model.language_model.layers.%d.mlp.experts." % layer 65for e in [int (x )for x in a .experts .split ("," )]: 66t0 = time .time () 67gu = repo .tensor (pre + "gate_up_proj" ,e )[0 ]# [1280, 2560] 68dn = repo .tensor (pre + "down_proj" ,e )[0 ]# [2560, 640] 69"layer %d expert %d fetched in %.1f s: gate_up %s down %s" % (layer ,e ,time .time ()- t0 ,gu .shape ,dn .shape )) 70mats = {"gate" :gu [:640 ],"up" :gu [640 :],"down" :dn } 71for role ,W in mats .items (): 72ti = by_name ["blk.%d.ffn_%s_exps.weight" % (layer ,role )] 73t = tqenc .TYPE_BY_NAME [ti .type_name ] 74n_in ,n_out = ti .shape [0 ],ti .shape [1 ] 75assert W .shape == (n_out ,n_in ), (W .shape ,ti .shape ) 76bb = tqenc .block_bytes (t ) 77row_bytes = n_in // 128 * bb 78off = g .data_start + ti .offset + e * n_out * row_bytes 79raw = np .frombuffer (mm ,dtype = np .uint8 ,count = n_out * row_bytes ,offset = off ) 80wd = tqenc .decode (raw ,t ).reshape (n_out ,n_in ) 81Wr = tqenc .rotate (W ,signs [n_in ]) 82en = float (np .sum (Wr .astype (np .float64 )** 2 )) 83ea = float (np .sum ((Wr .astype (np .float64 )- wd )** 2 )) 84# sanity: the fused gate_up order (gate first) - if swapped the SQNR collapses 85line = ["agentionai %s %.2f dB" % (ti .type_name ,10 * np .log10 (en / ea ))] 86key = "%s" % role 87tot ["agentionai" ][0 ]+= ea ;tot ["agentionai" ][1 ]+= en 88tr = tot_role .setdefault (key , {k : [0.0 ,0.0 ]for k in ["agentionai" ]+ cfgs }) 89tr ["agentionai" ][0 ]+= ea ;tr ["agentionai" ][1 ]+= en 90for c in cfgs : 91t1 = time .time () 92packed ,err ,totr ,st = tqenc .encode (Wr ,t ,threads = a .threads ,** CFGS [c ]) 93sec = time .time ()- t1 94line .append ("%s %.2f dB (%.0fk w/s)" % (c ,10 * np .log10 (en / totr ),Wr .size / sec / 1e3 )) 95tot [c ][0 ]+= totr ;tot [c ][1 ]+= en 96tr [c ][0 ]+= totr ;tr [c ][1 ]+= en 97" L%d e%d %-4s " % (layer ,e ,role )+ " | " .join (line )) 98"\nTOTAL SQNR (dB) against the rotated original, all tested matrices" ) 99for k , (er ,en )in tot .items (): 100" %-10s %.3f dB (rel MSE %.4e)" % (k ,10 * np .log10 (en / er ),er / en )) 101for role ,d in tot_role .items (): 102" %-5s " % role + " " .join ("%s %.3f" % (k ,10 * np .log10 (v [1 ]/ v [0 ]))for k ,v in d .items ())) 103return 0 104 105 106if __name__ == "__main__" : 107sys .exit (main ())