okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
53623e56f
main
1#!/usr/bin/env python3 2"""Parallel-Last: python3 parlast.py OUT.json LEVELS(1,2,4) ROUNDS PROMPT_TOK OUT_TOK. Streamt, misst TTFT, Dauer, Token je Strom und gesamt.""" 3import json ,sys ,time ,threading ,pathlib ,urllib .request ,statistics 4OUT = pathlib .Path (sys .argv [1 ]);LEVELS = [int (x )for x in sys .argv [2 ].split ("," )] 5ROUNDS = int (sys .argv [3 ]);PTOK = int (sys .argv [4 ]);OTOK = int (sys .argv [5 ]) 6ROOT = pathlib .Path .home ()/ "ai/Strata-0.1.36" 7files = [] 8for d ,g in [("docs" ,"*.md" ), ("src" ,"*.cpp" ), ("src" ,"*.cu" ), ("include" ,"*.hpp" ), ("serve" ,"*.py" ), ("tools" ,"*.py" ), ("third_party/llama.cpp/docs" ,"*.md" )]: 9files += sorted ((ROOT / d ).rglob (g )) 10corp = "" .join (f"\n### { f . name } \n" + f .read_text (errors = "ignore" )for f in files ) 11SEQ = [int (time .time ())% 1000000 ] 12def prompt (): 13SEQ [0 ]+= 1 14n = int (PTOK * 3.2 );off = (SEQ [0 ]* 37000 )% max (1 ,len (corp )- n ) 15return f"[nonce { SEQ [ 0 ] } ]\nHere is source code:\n" + corp [off :off + n ]+ "\n\nWrite a short summary of what this code does, then propose two refactorings with example code." 16def one (res ,i ): 17body = {"model" :"swift-1.5-iq3_xxs" ,"stream" :True ,"stream_options" : {"include_usage" :True }, 18"messages" : [{"role" :"user" ,"content" :prompt ()}],"max_tokens" :OTOK , 19"temperature" :0.7 ,"top_p" :0.95 ,"top_k" :20 } 20r = urllib .request .Request ("http://127.0.0.1:8080/v1/chat/completions" ,json .dumps (body ).encode (), {"Content-Type" :"application/json" }) 21t0 = time .time ();first = None ;chunks = 0 ;usage = None ;last = t0 22try : 23with urllib .request .urlopen (r ,timeout = 1800 )as f : 24for line in f : 25line = line .decode ().strip () 26if not line .startswith ("data:" )or line .endswith ("[DONE]" ):continue 27j = json .loads (line [5 :]) 28if j .get ("usage" ):usage = j ["usage" ].get ("completion_tokens" ) 29ch = j .get ("choices" )or [] 30if ch and (ch [0 ].get ("delta" , {}).get ("content" )or ch [0 ].get ("delta" , {}).get ("reasoning_content" )or ch [0 ].get ("delta" , {}).get ("reasoning" )): 31chunks += 1 ;last = time .time () 32if first is None :first = last 33except Exception as e : 34res [i ]= {"error" :repr (e )};return 35toks = usage or chunks 36res [i ]= {"ttft" :round ((first or last )- t0 ,2 ),"total_s" :round (last - t0 ,2 ),"tokens" :toks ,"chunks" :chunks , 37"decode_tps" :round (toks / max (0.01 ,last - (first or t0 )),1 )if first else None } 38out = [] 39one ({},0 )if False else None 40for c in LEVELS : 41for rd in range (ROUNDS ): 42res = [None ]* c ;th = [threading .Thread (target = one ,args = (res ,i ))for i in range (c )] 43t0 = time .time (); [t .start ()for t in th ]; [t .join ()for t in th ];wall = time .time ()- t0 44tot = sum ((r or {}).get ("tokens" ,0 )or 0 for r in res ) 45row = {"streams" :c ,"round" :rd + 1 ,"wall_s" :round (wall ,1 ),"total_tokens" :tot ,"total_tps" :round (tot / wall ,1 ),"per_stream" :res } 46out .append (row );json .dumps (row ),flush = True ) 47json .dump (out ,open (OUT ,"w" ),indent = 1 )