okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2b36e8a02
main
1#!/usr/bin/env python3 2"""dgx_compare - side-by-side medians and ratios of two dgx_bench result folders (or .jsonl files). 3 4python research/tools/dgx-spark-2026-10/dgx_compare.py bench/results/dgx-.../base.jsonl bench/results/dgx-.../gu.jsonl 5 6Prints, per cell, the median prompt and decode tokens/s of both arms, their ratio, and the geometric mean of the 7ratios over the cells (the "aggregate" the DGX Spark acceptance target is stated on). A request whose prompt was 8served from the conversation cache (cache_n > 0) is not counted. 9""" 10from __future__import annotations 11 12import json 13import math 14import random 15import statistics 16import sys 17from pathlib import Path 18 19 20def load (path :Path )-> list [dict ]: 21rows = [] 22for line in path .read_text ().splitlines (): 23line = line .strip () 24if not line : 25continue 26try : 27rows .append (json .loads (line )) 28except ValueError : 29continue 30return rows 31 32 33def cells (rows :list [dict ])-> list [tuple ]: 34return sorted ({(r ["len" ],r ["temp" ],r ["max_new" ])for r in rows }) 35 36 37def med (rows :list [dict ],key :str ): 38vals = [float (r [key ])for r in rows if r .get (key )and not r .get ("cache_n" )] 39return statistics .median (vals )if vals else None 40 41 42def arm_stats (rows :list [dict ])-> dict : 43out = {} 44for c in cells (rows ): 45sel = [r for r in rows if (r ["len" ],r ["temp" ],r ["max_new" ])== c ] 46out [c ]= (med (sel ,"prompt_per_second" ),med (sel ,"predicted_per_second" ),med (sel ,"ttft_s" ), 47len ([r for r in sel if not r .get ("cache_n" )])) 48return out 49 50 51 52def _by_cell (rows ,key ): 53out = {} 54for r in rows : 55if r .get (key )and not r .get ("cache_n" ): 56out .setdefault ((r ["len" ],r ["temp" ],r ["max_new" ]), []).append (float (r [key ])) 57return out 58 59 60def bootstrap (rows_a ,rows_b ,iters ,seed = 7 ): 61"""Per-cell and aggregate ratio confidence intervals, resampling each arm's rows within a cell.""" 62rng = random .Random (seed ) 63res = {} 64for key in ("prompt_per_second" ,"predicted_per_second" ): 65a ,b = _by_cell (rows_a ,key ),_by_cell (rows_b ,key ) 66cells = sorted (set (a )& set (b )) 67if not cells : 68continue 69draws = {c : []for c in cells } 70agg = [] 71for _ in range (iters ): 72logs = [] 73for c in cells : 74va ,vb = a [c ],b [c ] 75ra = statistics .median ([rng .choice (va )for _ in va ]) 76rb = statistics .median ([rng .choice (vb )for _ in vb ]) 77if ra <= 0 or rb <= 0 : 78continue 79draws [c ].append (rb / ra ) 80logs .append (math .log (rb / ra )) 81if logs : 82agg .append (math .exp (sum (logs )/ len (logs ))) 83pct = lambda xs ,p :sorted (xs )[min (len (xs )- 1 ,max (0 ,int (p * len (xs ))))] 84res [key ]= {c : (statistics .median (draws [c ]),pct (draws [c ],0.05 ),pct (draws [c ],0.95 ))for c in cells } 85res [key ]["aggregate" ]= (statistics .median (agg ),pct (agg ,0.05 ),pct (agg ,0.95 ))if agg else None 86return res 87 88def main ()-> int : 89if len (sys .argv )< 3 : 90__doc__ ) 91return 2 92ci = int (sys .argv [3 ])if len (sys .argv )> 3 else 0 93a_path ,b_path = Path (sys .argv [1 ]),Path (sys .argv [2 ]) 94a ,b = load (a_path ),load (b_path ) 95sa ,sb = arm_stats (a ),arm_stats (b ) 96name_a ,name_b = a_path .stem ,b_path .stem 97f"A = { name_a } B = { name_b } \n" ) 98f"| cell | A prompt | B prompt | ratio | A decode | B decode | ratio | A ttft | B ttft |" ) 99"|---|---|---|---|---|---|---|---|---|" ) 100pr ,dr = [], [] 101for c in sorted (set (sa )| set (sb )): 102pa ,da ,ta ,na = sa .get (c , (None ,None ,None ,0 )) 103pb ,db ,tb ,nb = sb .get (c , (None ,None ,None ,0 )) 104f = lambda v ,w = "{:.1f}" :"-" if v is None else w .format (v ) 105rp = (pb / pa )if (pa and pb )else None 106rd = (db / da )if (da and db )else None 107if rp : 108pr .append (rp ) 109if rd : 110dr .append (rd ) 111f"| { c [ 0 ] } T= { c [ 1 ] } n= { c [ 2 ] } | { f ( pa ) } | { f ( pb ) } | { f ( rp , '{:.3f}' ) } x | { f ( da ) } | { f ( db ) } | " 112f" { f ( rd , '{:.3f}' ) } x | { f ( ta , '{:.2f}' ) } | { f ( tb , '{:.2f}' ) } |" ) 113gm = lambda xs :math .exp (sum (math .log (x )for x in xs )/ len (xs ))if xs else float ("nan" ) 114f"\nprompt aggregate (geomean of ratios over { len ( pr ) } cells): { gm ( pr ):.3f } x" ) 115f"decode aggregate (geomean of ratios over { len ( dr ) } cells): { gm ( dr ):.3f } x" ) 116if pr : 117f"lowest prompt ratio: { min ( pr ):.3f } x lowest decode ratio: { min ( dr ) if dr else float ( 'nan' ):.3f } x" ) 118if ci > 0 : 119bs = bootstrap (a ,b ,ci ) 120f"\nbootstrap 90% intervals over { ci } resamples (5th .. 95th percentile of the ratio):" ) 121for key ,label in (("prompt_per_second" ,"prompt" ), ("predicted_per_second" ,"decode" )): 122if key not in bs : 123continue 124for c ,v in sorted (bs [key ].items (),key = lambda kv :str (kv [0 ])): 125if c == "aggregate" : 126continue 127f" { label :6s } { c [ 0 ]:6d } / { c [ 1 ] } / { c [ 2 ]:4d } : { v [ 0 ]:.3f } x [ { v [ 1 ]:.3f } , { v [ 2 ]:.3f } ]" ) 128agg = bs [key ].get ("aggregate" ) 129if agg : 130f" { label :6s } aggregate: { agg [ 0 ]:.3f } x [ { agg [ 1 ]:.3f } , { agg [ 2 ]:.3f } ]" ) 131return 0 132 133 134if __name__ == "__main__" : 135sys .exit (main ())