okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ba8745e13
main
1#!/usr/bin/env python3 2"""serve_prompts.py PROMPTS.json OUT.json -- ENGINE CMD... 3Starts the engine in --serve mode (appended), waits for READY, sends GEN for every prompt (greedy), compares the 4generated ids with the llama.cpp reference's greedy ids (expect_ids), and prints one line each: 5id mode prompt_tokens n_gen match_prefix/n_expect first_divergence prefill ms decode tok/s 6""" 7import json ,os ,subprocess ,sys ,time 8 9def _bytes_map (): 10bs = list (range (33 ,127 ))+ list (range (161 ,173 ))+ list (range (174 ,256 )) 11cs = bs [:];n = 0 12for b in range (256 ): 13if b not in bs :bs .append (b );cs .append (256 + n );n += 1 14return {chr (c ):b for b ,c in zip (bs ,cs )} 15_BM = _bytes_map () 16try : 17_VOC = {i :t for t ,i in json .load (open (os .environ .get ("DS_VOCAB" ,"/data/dsv41-port/pack/tokenizer/vocab.json" ))).items ()} 18except Exception : 19_VOC = {} 20def detok (ids ): 21b = bytearray () 22for i in ids : 23for ch in _VOC .get (i ,"?" ): 24b .append (_BM .get (ch ,63 )) 25return b .decode ("utf-8" ,"replace" ) 26 27prompts_path ,out_path = sys .argv [1 ],sys .argv [2 ] 28cmd = sys .argv [sys .argv .index ("--" )+ 1 :]+ ["--serve" ] 29prompts = json .load (open (prompts_path )) 30only = [a for a in sys .argv [3 :sys .argv .index ("--" )]] 31if only : 32prompts = [p for p in prompts if p ["id" ]in only or (p ["mode" ]+ ":" + p ["id" ])in only ] 33p = subprocess .Popen (cmd ,stdin = subprocess .PIPE ,stdout = subprocess .PIPE ,stderr = sys .stderr ,text = True ,bufsize = 1 ) 34t0 = time .time () 35while True : 36line = p .stdout .readline () 37if not line : 38"engine exited before READY" ,file = sys .stderr ) 39sys .exit (2 ) 40if line .startswith ("READY" ): 41"READY after %.0f s: %s" % (time .time ()- t0 ,line .strip ()),flush = True ) 42break 43"engine>" ,line .rstrip ()[:200 ],flush = True ) 44res = [] 45for pr in prompts : 46ids = pr ["ids" ] 47mx = min (pr ["max_tokens" ],max (len (pr ["expect_ids" ])+ 4 ,8 )) 48if pr .get ("pic" ): 49p .stdin .write ("GENI %d %s %s\n" % (pr .get ("max_tokens" ,40 ),pr ["pic" ],"," .join (map (str ,ids )))) 50else : 51p .stdin .write ("GEN %d %s\n" % (mx ,"," .join (map (str ,ids )))) 52p .stdin .flush () 53gen ,done = [],None 54while True : 55line = p .stdout .readline () 56if not line : 57break 58if line .startswith ("T " ): 59gen .append (int (line .split ()[1 ])) 60elif line .startswith ("DONE" ): 61done = line .split () 62break 63exp = pr ["expect_ids" ] 64if pr .get ("pic" )or os .environ .get ("SHOW_TEXT" ): 65"TEXT" ,repr (detok (gen )),flush = True ) 66n = 0 67while n < min (len (gen ),len (exp ))and gen [n ]== exp [n ]: 68n += 1 69full = n == len (exp )or (n == len (gen )and n >= len (exp )- 1 ) 70# DONE <generated> <prompt> <prompt ms> <decode ms> <stop|length|cancel> ... 71pms = float (done [3 ])if done and len (done )> 4 else 0.0 72dms = float (done [4 ])if done and len (done )> 4 else 0.0 73row = {"id" :pr ["id" ],"mode" :pr ["mode" ],"prompt_tokens" :len (ids ),"n_gen" :len (gen ),"n_expect" :len (exp ), 74"match_prefix" :n ,"first_div" :None if n >= min (len (gen ),len (exp ))else n ,"prompt_ms" :pms , 75"decode_ms" :dms ,"decode_tps" : (len (gen )* 1000.0 / dms )if dms > 0 else 0.0 , 76"prompt_tps" : (len (ids )* 1000.0 / pms )if pms > 0 else 0.0 ,"gen" :gen } 77res .append (row ) 78"%-26s %-8s prompt %4d (%.1f tok/s) gen %3d match %3d/%3d div %s decode %.2f tok/s" % ( 79pr ["id" ],pr ["mode" ],len (ids ),row ["prompt_tps" ],len (gen ),n ,len (exp ),row ["first_div" ],row ["decode_tps" ]),flush = True ) 80json .dump (res ,open (out_path ,"w" )) 81p .stdin .write ("QUIT\n" ) 82p .stdin .flush () 83try : 84p .wait (timeout = 60 ) 85except Exception : 86p .terminate ()