okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
28ce98bf1
main
1"""sanity_greedy.py - a few greedy answers from an engine config (the same config files tools/quant_quality.py uses), through the 2normal batched prompt path (not the yardstick's decode windows). 3 4python3 tools/trellis/sanity_greedy.py --config CFG.json --out answers.txt [--wrap "flock ... env HIP_VISIBLE_DEVICES=3"] 5""" 6from __future__import annotations 7 8import argparse 9import json 10import os 11import pathlib 12import subprocess 13import sys 14 15HERE = pathlib .Path (__file__ ).resolve ().parent 16sys .path .insert (0 ,str (HERE .parent )) 17sys .path .insert (0 ,str (HERE )) 18import make_corpus # noqa: E402 19 20PROMPTS = [ 21"What is the capital of Australia, and what is it known for? Answer in two sentences." , 22"Write a Python function that returns the n-th Fibonacci number iteratively, with a docstring." , 23"Translate into German: 'The library opens at nine and closes at five, except on Sundays.'" , 24"A train leaves at 14:20 and arrives at 17:05. How long is the trip? Think step by step, then give the answer." , 25] 26 27 28def main ()-> int : 29ap = argparse .ArgumentParser () 30ap .add_argument ("--config" ,required = True ) 31ap .add_argument ("--out" ,required = True ) 32ap .add_argument ("--wrap" ,default = "" ) 33ap .add_argument ("--new" ,type = int ,default = 80 ) 34a = ap .parse_args () 35cfg = json .load (open (a .config )) 36tok = make_corpus .load_tokenizer (cfg ["tokenizer" ]) 37env = dict (os .environ ) 38env .update (cfg .get ("env" , {})) 39env ["LD_LIBRARY_PATH" ]= ":" .join (cfg .get ("lib_dirs" , [])+ [env .get ("LD_LIBRARY_PATH" ,"" )]) 40args = [cfg ["exe" ],"--serve" ]+ [x for x in cfg ["args" ]if x != "--serve" ] 41if a .wrap : 42args = a .wrap .split ()+ args 43p = subprocess .Popen (args ,stdin = subprocess .PIPE ,stdout = subprocess .PIPE ,stderr = open (a .out + ".stderr" ,"w" ),text = True , 44cwd = cfg .get ("cwd" ),env = env ,bufsize = 1 ) 45for line in p .stdout : 46if line .startswith ("READY" ): 47break 48else : 49raise SystemExit ("the engine ended before READY" ) 50out = [] 51for q in PROMPTS : 52text = "<|im_start|>user\n%s<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n" % q 53ids = tok .encode (text ,parse_special = True ) 54p .stdin .write ("GEN %d %s\n" % (a .new ,"," .join (map (str ,ids )))) 55p .stdin .flush () 56gen = [] 57for line in p .stdout : 58if line .startswith ("T " ): 59gen .append (int (line .split ()[1 ])) 60elif line .startswith (("DONE" ,"ERR" )): 61done = line .strip () 62break 63out .append ("### %s\n%s\n[%s]\n" % (q ,tok .decode (gen ),done )) 64out [- 1 ],flush = True ) 65p .stdin .write ("QUIT\n" ) 66p .stdin .flush () 67try : 68p .wait (timeout = 300 ) 69except subprocess .TimeoutExpired : 70p .kill () 71pathlib .Path (a .out ).write_text ("\n" .join (out ),encoding = "utf-8" ) 72return 0 73 74 75if __name__ == "__main__" : 76sys .exit (main ())