okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0479b72e9
main
1#!/usr/bin/env python3 2"""Alternating-pairs speed test of two picture readers of the same helper (default: our fast CPU tower against mtmd): per picture one warm-up encode of each, then N pairs 3in the order A, B, A, B, ... so that drift of the machine (clocks, other jobs) hits both the same way. Reports the median, the minimum and the maximum of each (the `ms` the 4helper prints) and the ratio of the medians; also the ratio of every single pair (min / median / max), the spread that decides whether a difference is real. 5 6python3 speed_pairs.py --helper <strata-vision> --mmproj <mmproj> --model <text model> [--threads N] [--pairs 5] [--max-tokens 1024] 7[--a own --env-a STRATA_VIT_CPU=fast] [--b mtmd] <image> ... 8""" 9import argparse 10import os 11import statistics 12import subprocess 13import sys 14 15 16def start (helper ,a ,tower ,envs ,label ): 17args = [helper ,"--mmproj" ,a .mmproj ,"--model" ,a .model ,"--tower" ,tower ,"--max-tokens" ,str (a .max_tokens )] 18if a .threads : 19args += ["--threads" ,str (a .threads )] 20env = dict (os .environ ) 21for kv in envs : 22k ,_ ,v = kv .partition ("=" ) 23env [k ]= v 24p = subprocess .Popen (args ,stdin = subprocess .PIPE ,stdout = subprocess .PIPE ,stderr = subprocess .DEVNULL ,text = True ,env = env ) 25if not p .stdout .readline ().startswith ("READY" ): 26raise SystemExit ("%s reader did not start" % label ) 27return p 28 29 30def enc (p ,img ,out ): 31p .stdin .write ("ENC %s %s\n" % (img ,out )) 32p .stdin .flush () 33r = p .stdout .readline ().split () 34if not r or r [0 ]!= "OK" : 35raise SystemExit ("encode failed: %s" % " " .join (r )) 36return float (r [4 ]),int (r [1 ]) 37 38 39def main (): 40ap = argparse .ArgumentParser () 41ap .add_argument ("--helper" ,required = True ) 42ap .add_argument ("--mmproj" ,required = True ) 43ap .add_argument ("--model" ,required = True ) 44ap .add_argument ("--threads" ,type = int ,default = 0 ) 45ap .add_argument ("--pairs" ,type = int ,default = 5 ) 46ap .add_argument ("--max-tokens" ,type = int ,default = 1024 ) 47ap .add_argument ("--a" ,default = "own" ) 48ap .add_argument ("--b" ,default = "mtmd" ) 49ap .add_argument ("--env-a" ,action = "append" ,default = ["STRATA_VIT_CPU=fast" ]) 50ap .add_argument ("--env-b" ,action = "append" ,default = []) 51ap .add_argument ("--tmp" ,default = "/tmp" ) 52ap .add_argument ("images" ,nargs = "+" ) 53a = ap .parse_args () 54pa = start (a .helper ,a ,a .a ,a .env_a ,"A" ) 55pb = start (a .helper ,a ,a .b ,a .env_b ,"B" ) 56"A = %s %s, B = %s %s, %s threads, %d alternating pairs after one warm-up each (ms of the helper's OK line)" % (a .a ,a .env_a ,a .b ,a .env_b ,a .threads or "default" ,a .pairs )) 57"%-24s %6s | %-26s | %-26s | %6s | %s" % ("picture" ,"tokens" ,"A median (min..max)" ,"B median (min..max)" ,"A/B" ,"pair ratios min / median / max" )) 58worst = 0.0 59for img in a .images : 60name = os .path .basename (img ) 61oa ,ob = os .path .join (a .tmp ,"sp_a_" + name + ".sve" ),os .path .join (a .tmp ,"sp_b_" + name + ".sve" ) 62_ ,n = enc (pa ,img ,oa ) 63enc (pb ,img ,ob ) 64ta ,tb = [], [] 65for _ in range (a .pairs ): 66ta .append (enc (pa ,img ,oa )[0 ]) 67tb .append (enc (pb ,img ,ob )[0 ]) 68ra = [x / y for x ,y in zip (ta ,tb )] 69ma ,mb = statistics .median (ta ),statistics .median (tb ) 70worst = max (worst ,ma / mb ) 71"%-24s %6d | %8.0f (%6.0f..%6.0f) | %8.0f (%6.0f..%6.0f) | %6.3f | %.3f / %.3f / %.3f" % 72 (name ,n ,ma ,min (ta ),max (ta ),mb ,min (tb ),max (tb ),ma / mb ,min (ra ),statistics .median (ra ),max (ra )),flush = True ) 73for f in (oa ,ob ): 74try : 75os .remove (f ) 76except OSError : 77pass 78"largest A/B ratio of medians: %.3f" % worst ) 79for p in (pa ,pb ): 80p .stdin .write ("QUIT\n" ) 81p .stdin .flush () 82p .wait (timeout = 60 ) 83 84 85if __name__ == "__main__" : 86sys .exit (main ())