okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ba8745e13
main
1# Copyright (c) 2026 Peasant Smith and the Project Maya contributors (MIT; https://github.com/mw00/project-maya, v1.1.17). 2# Ported into Strata (third_party/project-maya/NOTICE.md, LICENSE); changes are in the git history of this file. 3"""tools/ds4/compare.py - the engine's logits (maya-ds4 --dump-logits) against the reference's (tools/ds4/ref.py --dump). 4 5python tools/ds4/compare.py --ref ref.npz --engine engine.f32 [--vocab 129280] 6 7Per position: the cosine of the two logit rows, the largest difference, whether the top token agrees, and the KL 8divergence of the engine's distribution from the reference's. 9""" 10from __future__import annotations 11 12import argparse 13import sys 14 15import numpy as np 16 17 18def main ()-> int : 19ap = argparse .ArgumentParser () 20ap .add_argument ("--ref" ,required = True ) 21ap .add_argument ("--engine" ,required = True ) 22ap .add_argument ("--vocab" ,type = int ,default = 129280 ) 23ap .add_argument ("--layers" ,help = "the engine's DS4_DUMP_H file: each layer's residual stream against the ref's" ) 24a = ap .parse_args () 25r = np .load (a .ref ) 26ref = r ["logits" ].astype (np .float64 ) 27eng = np .fromfile (a .engine ,dtype = np .float32 ).reshape (- 1 ,a .vocab ).astype (np .float64 ) 28n = min (len (ref ),len (eng )) 29agree = 0 30kls = [] 31for i in range (n ): 32x ,y = ref [i ],eng [i ] 33cos = float (x @y / (np .linalg .norm (x )* np .linalg .norm (y ))) 34px = np .exp (x - x .max ()) 35px /= px .sum () 36py = np .exp (y - y .max ()) 37py /= py .sum () 38kl = float (np .sum (px * (np .log (px + 1e-30 )- np .log (py + 1e-30 )))) 39kls .append (kl ) 40top = int (x .argmax ())== int (y .argmax ()) 41agree += top 42"pos %3d cos %.6f max|d| %8.4f top %s (ref %6d eng %6d) KL %.2e" % 43 (i ,cos ,np .abs (x - y ).max (),"same" if top else "DIFF" ,x .argmax (),y .argmax (),kl )) 44"%d positions: top-1 agrees at %d, mean KL %.3e, max KL %.3e" % (n ,agree ,np .mean (kls ),np .max (kls ))) 45if a .layers : 46hs = r ["hs" ]# [layers, T, hc, dim] 47nl ,T = hs .shape [0 ],hs .shape [1 ] 48eh = np .fromfile (a .layers ,dtype = np .float32 ).reshape (- 1 ,nl ,* hs .shape [2 :])# [T, layers, hc, dim] 49T = min (T ,len (eh )) 50"relative error of each layer's residual stream (rows: layers, columns: positions)" ) 51" " + " " .join ("%6d" % t for t in range (T ))) 52for li in range (nl ): 53row = [] 54for t in range (T ): 55x ,y = hs [li ,t ].astype (np .float64 ),eh [t ,li ].astype (np .float64 ) 56row .append (np .linalg .norm (x - y )/ np .linalg .norm (x )) 57"L%-3d " % li + " " .join ("%6.4f" % v for v in row )) 58return 0 59 60 61if __name__ == "__main__" : 62sys .exit (main ())