okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2eba71548
main
1"""tools/collect_profile_traces.py - routing traces of a varied set of prompts, for tools/make_profile_mix.py. 2 3For every prompt of a corpus (default bench/profile_corpus.json: 45 chat, code, maths, reasoning, writing, science, tool 4use and six-language prompts) it runs the engine once (greedy, --max-new tokens) and keeps the routed experts of the 5generated text: 6 7python tools/collect_profile_traces.py CONFIG.json OUTDIR [--corpus FILE] [--max-new 250] [--mode usage|routing] 8 9CONFIG.json is a run config setup wrote (strata-*.json: "exe", "args", "env", "tokenizer", "cwd"). The engine is run as 10`exe args... --tokens-file F --max-new N --greedy` (--serve and --batch are dropped). `--mode usage` (default) is the 11SYCL engine's STRATA_USAGE_DUMP=PATH, which counts on the device and works with STRATA_VERIFY_NO_HOST; `--mode routing` 12adds `--dump-routing PATH` (the CUDA/HIP engines and the SYCL host path). Then, for example: 13 14python tools/make_profile_mix.py OUTDIR/c*.trace:1 --out data/expert-profile-new.bin 15 16A prompt takes a start-up of the engine (20-60 s) and ~10 s of decoding; 45 prompts are ~30 minutes. The traces are the 17generated text's routing only (decode windows); the prompt's own routing is not counted. 18""" 19import argparse 20import json 21import os 22import subprocess 23import sys 24from pathlib import Path 25 26ROOT = Path (__file__ ).resolve ().parent .parent 27sys .path .insert (0 ,str (Path (__file__ ).resolve ().parent )) 28import strata_tokenizer as ST # noqa: E402 29 30 31def load_tokenizer (tdir ): 32tdir = Path (tdir ) 33vocab = json .loads ((tdir / "vocab.json" ).read_text (encoding = "utf-8" )) 34toks = [None ]* len (vocab ) 35for t ,i in vocab .items (): 36toks [i ]= t 37return ST .Tokenizer (toks , (tdir / "merges.txt" ).read_text (encoding = "utf-8" ).split ("\n" ), 38json .loads ((tdir / "token_type.json" ).read_text ())) 39 40 41def drop (args ,flag ,with_value = False ): 42args = list (args ) 43while flag in args : 44i = args .index (flag ) 45del args [i :i + (2 if with_value else 1 )] 46return args 47 48 49def main (): 50ap = argparse .ArgumentParser (description = __doc__ .split ("\n\n" )[0 ]) 51ap .add_argument ("config" ) 52ap .add_argument ("outdir" ) 53ap .add_argument ("--corpus" ,default = str (ROOT / "bench" / "profile_corpus.json" )) 54ap .add_argument ("--max-new" ,type = int ,default = 250 ) 55ap .add_argument ("--mode" ,choices = ("usage" ,"routing" ),default = "usage" ) 56ap .add_argument ("--only" ,help = "comma-separated prompt numbers (default all)" ) 57a = ap .parse_args () 58cfg = json .load (open (a .config ,encoding = "utf-8-sig" )) 59tok = load_tokenizer (cfg ["tokenizer" ]) 60corpus = json .load (open (a .corpus ,encoding = "utf-8" )) 61out = Path (a .outdir ) 62out .mkdir (parents = True ,exist_ok = True ) 63args = drop (drop (cfg ["args" ],"--serve" ),"--batch" ,True ) 64only = {int (x )for x in a .only .split ("," )}if a .only else None 65env0 = dict (os .environ ) 66env0 .update ({k :str (v )for k ,v in cfg .get ("env" , {}).items ()}) 67for n ,c in enumerate (corpus ): 68if only is not None and n not in only : 69continue 70trace = out / f"c { n :02d } .trace" 71if trace .exists ()and trace .stat ().st_size > 0 : 72continue 73ids = tok .encode (f"<|im_start|>user\n { c [ 'p' ] } <|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n" , 74parse_special = True ) 75tf = out / f"c { n :02d } .tok" 76tf .write_text ("," .join (str (i )for i in ids )) 77cmd = [cfg ["exe" ]]+ args + ["--tokens-file" ,str (tf ),"--max-new" ,str (a .max_new ),"--greedy" ] 78env = dict (env0 ) 79if a .mode == "usage" : 80env ["STRATA_USAGE_DUMP" ]= str (trace ) 81else : 82cmd += ["--dump-routing" ,str (trace )] 83f"[ { n + 1 } / { len ( corpus ) } ] { c [ 'd' ] } : { len ( ids ) } prompt tokens" ,flush = True ) 84r = subprocess .run (cmd ,cwd = cfg .get ("cwd" )or None ,env = env ,capture_output = True ,text = True ) 85if r .returncode != 0 or not trace .exists (): 86f" failed (rc { r . returncode } ): { r . stderr [ - 300 :] } " ,file = sys .stderr ) 87sys .exit (1 ) 88f"traces in { out } " ) 89 90 91if __name__ == "__main__" : 92main ()