okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6e1085c1d
main
1"""Complete missing quality cases within the original measurement clock.""" 2import argparse 3import json 4import socket 5import subprocess 6import sys 7import time 8 9from evalplus .sanitize import sanitize 10from strata_bench_continue import Continuation ,b 11 12 13def finish_small (runner ,scope ,context ): 14cases = json .loads ((b .OUT / "japanese-cases.json" ).read_text (encoding = "utf-8" )) 15tool_cases = json .loads ((b .OUT / "tool-cases.json" ).read_text (encoding = "utf-8" )) 16tools ,pairs = tool_cases ["schema" ],tool_cases ["pairs" ] 17def grade (model ,group ,index ,passed ): 18with (b .OUT / "grades.jsonl" ).open ("a" ,encoding = "utf-8" )as file : 19file .write (json .dumps ({"model" :model ,"group" :group ,"index" :index ,"pass" :passed , 20"context" :runner .ctx ,"stage" :"supplement" })+ "\n" ) 21for model ,needed in scope .items (): 22if not any (needed .values ())or runner .left ()< 30 : 23continue 24def run (): 25runner .start (model ,context ) 26for i in needed ["japanese" ]: 27prompt ,expected = cases [i ] 28row = runner .perform (f"supplement-japanese- { i } -ctx { context } " ,prompt ,group = "japanese" ,max_tokens = 128 ) 29text = row ["text" ].strip ().strip ("`" ).strip () 30try : 31actual = json .loads (text )if isinstance (expected , (list ,dict ))else text 32except ValueError : 33actual = None 34grade (model ,"japanese" ,i ,actual == expected ) 35runner .event ("japanese_grade" ,index = i ,passed = actual == expected ) 36for i in needed ["tool" ]: 37a ,value_b = pairs [i ] 38body = runner .body (f"Use the add tool to add { a } and { value_b } . Do not compute it yourself." ,128 ) 39body ["tools" ]= tools 40row = runner .perform (f"supplement-tool- { i } -ctx { context } " ,body = body ,group = "tool" ) 41calls = row ["tool_calls" ] 42call = calls [0 ]if len (calls )== 1 else None 43try : 44valid = bool (call and call ["function" ]["name" ]== "add" and json .loads (call ["function" ]["arguments" ])== {"a" :a ,"b" :value_b }) 45except ValueError : 46valid = False 47if valid : 48follow = runner .body ("" ,128 ) 49follow ["tools" ]= tools 50follow ["messages" ]= body ["messages" ]+ [{"role" :"assistant" ,"content" :row ["text" ],"tool_calls" :calls }, 51 {"role" :"tool" ,"tool_call_id" :call ["id" ],"content" :str (a + value_b )}, 52 {"role" :"user" ,"content" :"Reply with only the integer result returned by the tool." }] 53reply = runner .perform (f"supplement-tool-followup- { i } -ctx { context } " ,body = follow ,group = "tool_followup" ) 54valid = reply ["text" ].strip ()== str (a + value_b ) 55grade (model ,"tool" ,i ,valid ) 56runner .safe_block (run ) 57runner .stop () 58 59 60def finish_mtp (runner ,scope ): 61for model ,states in scope .items (): 62for state ,reps in states .items (): 63if not reps or runner .left ()< 30 : 64continue 65def run (): 66runner .start (model ,mtp = state == "on" ) 67for rep in reps : 68runner .perform (f"supplement-mtp- { state } - { rep } " ,runner .make (4096 ,rep = 50 + rep ),4096 , 69group = "mtp" ,max_tokens = 1024 ) 70runner .safe_block (run ) 71runner .stop () 72 73 74def main (): 75parser = argparse .ArgumentParser () 76parser .add_argument ("--max-minutes" ,type = float ,default = 20 ) 77parser .add_argument ("--small" ,action = "store_true" ,help = "Also finish ungraded Japanese/tool cases" ) 78parser .add_argument ("--small-context" ,type = int ,choices = (8192 ,131072 ),default = 131072 , 79help = "Explicit context for the supplemental small tasks; preserved in grades and configurations" ) 80parser .add_argument ("--mtp" ,action = "store_true" ,help = "Also finish missing MTP comparison repetitions" ) 81args = parser .parse_args () 82dataset = json .loads ((b .OUT / "humaneval-subset.json" ).read_text (encoding = "utf-8" )) 83missing = {} 84for model in b .MODELS : 85path = b .OUT / "code" / f" { model } -samples.jsonl" 86saved = {json .loads (s )["task_id" ]for s in path .read_text (encoding = "utf-8" ).splitlines ()if s .strip ()}if path .exists ()else set () 87missing [model ]= [tid for tid in dataset ["ids" ]if tid not in saved ] 88grades_path = b .OUT / "grades.jsonl" 89grades = [json .loads (s )for s in grades_path .read_text (encoding = "utf-8" ).splitlines ()if s .strip ()]if grades_path .exists ()else [] 90marked = {(r ["model" ],r ["group" ],r ["index" ])for r in grades } 91small_missing = {model : {group : [i for i in range (n )if (model ,group ,i )not in marked ] 92for group ,n in (("japanese" ,12 ), ("tool" ,8 ))}for model in b .MODELS } 93requests = [json .loads (s )for s in (b .OUT / "requests.jsonl" ).read_text (encoding = "utf-8" ).splitlines ()if s .strip ()] 94mtp_missing = {model : {state : [rep for rep in (1 ,2 ,3 )if not any ( 95r ["model" ]== model and r ["group" ]== "mtp" and r ["label" ].removeprefix ("supplement-" )== f"mtp- { state } - { rep } " 96and r .get ("ok" )and r .get ("token_count_matches" )and r .get ("engine" , {}).get ("reused" )== 0 for r in requests )] 97for state in ("on" ,"off" )}for model in ("iq2_xs" ,"iq3_s" )} 98if not any (missing .values ())and not (args .small and any (any (v .values ())for v in small_missing .values ()))and not ( 99args .mtp and any (any (v .values ())for v in mtp_missing .values ())): 100json .dumps ({"already_complete" :True }),flush = True ) 101return 102with socket .socket ()as sock : 103if sock .connect_ex (("127.0.0.1" ,b .PORT ))== 0 : 104raise RuntimeError ("Benchmark service is occupied; it was preserved" ) 105runner = Continuation (["iq3_xxs" ,"iq3_s" ]) 106 (b .OUT / "code-completion.pid" ).write_text (str (b .os .getpid ())) 107supervisor = subprocess .Popen ([sys .executable ,str (b .SCRIPT ),"--watchdog" ,str (b .os .getpid ()), 108str (runner .clock ["measurement_stop_epoch" ]+ 300 )],stdout = subprocess .DEVNULL , 109stderr = subprocess .DEVNULL ,creationflags = b .FLAGS ) 110try : 111runner .budget ("code_completion" ,min (args .max_minutes * 60 ,max (0 ,runner .hard - time .monotonic ()))) 112runner .event ("code_completion_scope" ,missing = missing ,small_missing = small_missing if args .small else {}, 113mtp_missing = mtp_missing if args .mtp else {}) 114runner .init_tokenizer () 115runner .inventory () 116for model ,ids in missing .items (): 117if not ids or runner .left ()< 30 : 118continue 119def generate (): 120runner .start (model ) 121path = b .OUT / "code" / f" { model } -samples.jsonl" 122with path .open ("a" ,encoding = "utf-8" )as file : 123for tid in ids : 124if runner .left ()< 5 : 125break 126problem = dataset ["problems" ][tid ] 127row = runner .perform ("supplement-code-" + tid .replace ("/" ,"-" ), 128"Complete this Python function. Output valid Python code only, no explanations.\n\n" + problem ["prompt" ], 129group = "code" ,max_tokens = 1024 ) 130solution = sanitize (row ["text" ],problem ["entry_point" ]) 131file .write (json .dumps ({"task_id" :tid ,"solution" :solution })+ "\n" ) 132file .flush () 133runner .safe_block (generate ) 134runner .stop () 135if args .small : 136finish_small (runner ,small_missing ,args .small_context ) 137if args .mtp : 138finish_mtp (runner ,mtp_missing ) 139runner .evaluate (min (720 ,runner .left ())) 140runner .event ("code_completion_finished" ) 141finally : 142if runner .active_worker and runner .active_worker .poll ()is None : 143b .kill_tree (runner .active_worker ) 144for job in runner .child_jobs : 145b .kill_tree (job ) 146runner .stop () 147runner .monitor .done .set () 148if supervisor .poll ()is None : 149supervisor .terminate () 150 151 152if __name__ == "__main__" : 153main ()