okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2fb096b93
main
1#!/usr/bin/env python3 2"""Gate check: a Qwen serve engine answers a request that makes prompt lookup propose long windows. 3 4The #4040 default (--lookup-max-t 12) builds verify windows of up to 12 rows when the build holds 8: on Metal that was refused 5with "metal window: window size out of range" at the first request that repeated text, and no gate request repeated any. This 6starts the engine as the Terminal would (serve, the build's defaults - no --lookup-max-t), sends a chat that asks for a text to be 7repeated three times, twice, and fails on an ERR line or on a request that did not end with DONE. With --need-drafts it also 8requires that drafts were offered, i.e. that lookup windows really ran. 9 10check_lookup_window_serve.py --exe build/strata --pack-tokenizer PACK/tokenizer -- <engine args without --serve> 11Exit 0 = pass, 1 = fail. 12""" 13import argparse 14import json 15import queue 16import subprocess 17import sys 18import threading 19import time 20from pathlib import Path 21 22TEXT = ("The quick brown fox jumps over the lazy dog near the quiet river bank, while the old farmer counts seventeen sheep, " 23"nine goats and four noisy geese before supper. The quick brown fox jumps over the lazy dog near the quiet river bank." ) 24PROMPT = ("<|im_start|>user\nRepeat the following text exactly, three times in a row, with no other words:\n\n" + TEXT + 25"<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n" ) 26 27 28def prompt_ids (tokenizer_dir :Path )-> list : 29sys .path .insert (0 ,str (Path (__file__ ).resolve ().parent )) 30import strata_tokenizer as ST 31vocab = json .loads ((tokenizer_dir / "vocab.json" ).read_text (encoding = "utf-8" )) 32tokens = [None ]* len (vocab ) 33for t ,i in vocab .items (): 34tokens [i ]= t 35merges = (tokenizer_dir / "merges.txt" ).read_text (encoding = "utf-8" ).split ("\n" ) 36types = json .loads ((tokenizer_dir / "token_type.json" ).read_text ()) 37try : 38tcfg = json .loads ((tokenizer_dir / "tokenizer.json" ).read_text (encoding = "utf-8" )) 39except (OSError ,ValueError ): 40tcfg = {} 41tok = ST .Tokenizer (tokens ,merges ,types ,tcfg .get ("pre" )or "qwen35" ,tcfg .get ("special_ids" )or None ) 42return list (tok .encode (PROMPT ,parse_special = True )) 43 44 45def check (exe ,engine_args ,ids ,max_new = 96 ,start_timeout = 900 ,request_timeout = 1200 ,need_drafts = False ,env = None ,cwd = None ): 46"""Returns (ok, lines): one summary line per request (or the reason it failed).""" 47p = subprocess .Popen ([exe ,"--serve" ,* engine_args ],stdin = subprocess .PIPE ,stdout = subprocess .PIPE ,stderr = subprocess .DEVNULL , 48text = True ,env = env ,cwd = cwd ) 49q = queue .Queue () 50 51def pump (): 52for line in p .stdout : 53q .put (line .rstrip ("\n" )) 54q .put (None ) 55 56threading .Thread (target = pump ,daemon = True ).start () 57 58def until (prefixes ,timeout ): 59end = time .monotonic ()+ timeout 60got = [] 61while time .monotonic ()< end : 62try : 63line = q .get (timeout = 1 ) 64except queue .Empty : 65continue 66if line is None : 67got .append ("<engine exited>" ) 68return got 69got .append (line ) 70if line .startswith (prefixes ): 71return got 72got .append ("<timeout>" ) 73return got 74 75lines ,ok = [],True 76try : 77r = until (("READY" ,),start_timeout ) 78if not r [- 1 ].startswith ("READY" ): 79return False , ["engine did not start: " + " | " .join (r [- 3 :])] 80for n in (1 ,2 ): 81p .stdin .write (f"GEN { max_new } " + "," .join (map (str ,ids ))+ "\n" ) 82p .stdin .flush () 83out = until (("DONE" ,"ERR" ),request_timeout ) 84last = out [- 1 ] 85toks = sum (1 for x in out if x .startswith ("T " )) 86f = last .split () 87offered = int (f [7 ])if last .startswith ("DONE" )and len (f )> 7 and f [7 ].isdigit ()else 0 88good = last .startswith ("DONE" )and (offered > 0 or not need_drafts ) 89ok = ok and good 90lines .append (f"request { n } : { last [: 160 ] } | { toks } tokens | drafts offered { offered } " + ("" if good else " <-- FAIL" )) 91if not good : 92break 93return ok ,lines 94finally : 95try : 96p .stdin .write ("QUIT\n" ) 97p .stdin .flush () 98except (OSError ,ValueError ): 99pass 100time .sleep (1 ) 101if p .poll ()is None : 102p .terminate () 103 104 105def main (argv = None ): 106ap = argparse .ArgumentParser (description = __doc__ ,formatter_class = argparse .RawDescriptionHelpFormatter ) 107ap .add_argument ("--exe" ,required = True ) 108ap .add_argument ("--pack-tokenizer" ,required = True ,help = "the pack's tokenizer folder (vocab.json, merges.txt, token_type.json)" ) 109ap .add_argument ("--max-new" ,type = int ,default = 96 ) 110ap .add_argument ("--need-drafts" ,action = "store_true" ,help = "also fail if no draft was offered (lookup windows did not run)" ) 111ap .add_argument ("--cwd" ) 112ap .add_argument ("engine_args" ,nargs = argparse .REMAINDER ) 113a = ap .parse_args (argv ) 114args = a .engine_args [1 :]if a .engine_args [:1 ]== ["--" ]else a .engine_args 115ok ,lines = check (a .exe ,args ,prompt_ids (Path (a .pack_tokenizer )),a .max_new ,need_drafts = a .need_drafts ,cwd = a .cwd ) 116"\n" .join (lines )) 117"LOOKUP-WINDOW SERVE CHECK:" ,"PASS" if ok else "FAIL" ) 118return 0 if ok else 1 119 120 121if __name__ == "__main__" : 122sys .exit (main ())