okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2eba71548
main
1"""tools/make_profile_mix.py - an expert-cache profile from several routing traces, each trace counting the same. 2 3`make_profile.py` adds the traces' raw counts, so the longest trace decides the ranking. A cache profile should 4rank the experts by how often a USER's tokens need them, across the kinds of text that user writes, so here every 5trace is first turned into a distribution (its counts / its total) and multiplied by its weight; the ranking is the 6weighted sum. Weights are per trace, so a kind of text with more traces counts more - give a group of N traces the 7weight 1/N each to make the group count once. 8 9python tools/make_profile_mix.py TRACE[:WEIGHT] [TRACE[:WEIGHT] ...] [--out PATH] [--n-expert 512] 10 11A trace is a `--dump-routing` file, or a `STRATA_USAGE_DUMP` file of the SYCL engine (same record format). The output 12is a profile for `--expert-profile`; the pairs no trace uses are appended in make_profile.py's fill order. 13 14Measured on an Arc Pro B70 (IQ3_S, 11.5k cached experts, decode of 250-300 tokens): the shipped profile holds 60% of the 15experts 45 chat/code/language prompts route and 85% of those a document summary routes; a profile from the 45 chat prompts 16and the 8 summaries (weight 1 : 2 per prompt), tested on prompts left out of it, holds 71% and 84%. 17""" 18import argparse 19import sys 20from collections import defaultdict 21from pathlib import Path 22 23sys .path .insert (0 ,str (Path (__file__ ).resolve ().parent )) 24import make_profile as mp # noqa: E402 25 26 27def main (): 28ap = argparse .ArgumentParser (description = __doc__ .split ("\n\n" )[0 ]) 29ap .add_argument ("traces" ,nargs = "+" ,help = "TRACE or TRACE:WEIGHT" ) 30ap .add_argument ("--out" ,required = True ) 31ap .add_argument ("--n-expert" ,type = int ,default = mp .N_EXPERT ) 32a = ap .parse_args () 33ne = a .n_expert 34score = defaultdict (float ) 35for spec in a .traces : 36path ,weight = spec ,1.0 37head ,sep ,tail = spec .rpartition (":" ) 38if sep :# TRACE:WEIGHT (a Windows drive letter's colon is not followed by a number) 39try : 40path ,weight = head ,float (tail ) 41except ValueError : 42pass 43freq = mp .read_trace (path ,ne ) 44total = float (sum (freq .values ())) 45if total <= 0 : 46f" { path } : empty trace, skipped" ) 47continue 48for p ,c in freq .items (): 49score [p ]+= weight * c / total 50ranked = [p for p ,_ in sorted (score .items (),key = lambda kv : (- kv [1 ],kv [0 ]))] 51seen = set (ranked ) 52for e in range (ne ):# the fill, as make_profile.py: the pairs no trace used 53for layer in range (mp .N_LAYER ): 54if (layer ,e )not in seen : 55ranked .append ((layer ,e )) 56mp .write_profile (a .out ,ranked ,ne ) 57assert mp .read_profile (a .out ,ne )== ranked ,"the profile did not survive the round trip" 58f"wrote { a . out } : { len ( ranked ) } ranked pairs, { len ( score ) } from { len ( a . traces ) } traces" ) 59 60 61if __name__ == "__main__" : 62main ()