okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
db4dd65e5
main
1"""q36_serve_par.py PORT [--n 128] [--conc 4] [--img PATH] : N concurrent chat requests through the server (parallel: N), against 2the same requests one after the other (the solo path). Greedy, thinking off: the texts must be equal. Prints the 3aggregate rate of the concurrent round. With --img: half of the requests carry the picture (and ask about it).""" 4import argparse ,base64 ,json ,sys ,threading ,time ,urllib .request 5 6PROMPTS = json .load (open (sys .argv [0 ].rsplit ("/" ,1 )[0 ]+ "/parity_prompts.json" ))if False else [ 7"Write a short story about a lighthouse keeper who finds a message in a bottle." , 8"Implement an LRU cache in Python with get and put in O(1), and add three unit tests." , 9"What is 17 * 23 + 144 / 12? Show the steps." , 10"Explain how a refrigerator works to a ten-year-old." , 11"Translate into German: The train to Munich leaves at half past seven, platform four." , 12"List five differences between TCP and UDP." , 13"Summarize the plot of Romeo and Juliet in three sentences." , 14"Write a JSON object describing a book with title, author, year and three tags." , 15] 16 17 18def ask (port ,content ,n ,out ,i ,stream = False ): 19body = {"model" :"x" ,"max_tokens" :n ,"temperature" :0 ,"chat_template_kwargs" : {"enable_thinking" :False }, 20"messages" : [{"role" :"user" ,"content" :content }]} 21t0 = time .time () 22try : 23r = json .loads (urllib .request .urlopen (urllib .request .Request (f"http://127.0.0.1: { port } /v1/chat/completions" , 24json .dumps (body ).encode (), {"Content-Type" :"application/json" }),timeout = 900 ).read ()) 25out [i ]= (r ["choices" ][0 ]["message" ]["content" ],r ["usage" ]["completion_tokens" ],time .time ()- t0 ) 26except Exception as e :# noqa: BLE001 27out [i ]= ("ERROR %s" % e ,0 ,time .time ()- t0 ) 28 29 30ap = argparse .ArgumentParser () 31ap .add_argument ("port" ,type = int ) 32ap .add_argument ("--n" ,type = int ,default = 128 ) 33ap .add_argument ("--conc" ,type = int ,default = 4 ) 34ap .add_argument ("--img" ,default = None ) 35a = ap .parse_args () 36contents = [] 37for i in range (a .conc ): 38p = PROMPTS [i % len (PROMPTS )] 39if a .img and i % 2 == 1 : 40b64 = base64 .b64encode (open (a .img ,"rb" ).read ()).decode () 41contents .append ([{"type" :"image_url" ,"image_url" : {"url" :"data:image/png;base64," + b64 }}, 42 {"type" :"text" ,"text" :"What text is written in the image? Answer with the text only." }]) 43else : 44contents .append (p ) 45solo = [None ]* a .conc 46for i ,c in enumerate (contents ): 47ask (a .port ,c ,a .n ,solo ,i ) 48"one after the other: %d requests, %d tokens, %.1fs" % (a .conc ,sum (x [1 ]for x in solo ),sum (x [2 ]for x in solo )),flush = True ) 49for rnd in range (2 ): 50conc = [None ]* a .conc 51ts = [threading .Thread (target = ask ,args = (a .port ,c ,a .n ,conc ,i ))for i ,c in enumerate (contents )] 52t0 = time .time () 53for t in ts : 54t .start () 55for t in ts : 56t .join () 57wall = time .time ()- t0 58tot = sum (x [1 ]for x in conc ) 59same = [conc [i ][0 ]== solo [i ][0 ]for i in range (a .conc )] 60"concurrent round %d: %d requests, %d tokens in %.1fs = %.1f tok/s aggregate; texts equal to the solo run: %s" 61% (rnd ,a .conc ,tot ,wall ,tot / wall ,same ),flush = True ) 62for i ,ok in enumerate (same ): 63if not ok : 64" request %d: %r vs %r" % (i ,conc [i ][0 ][:80 ],solo [i ][0 ][:80 ])) 65sys .exit (0 )