okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-143-intel: B70 prompt and decode speed (copy engine, wide kv_append, no GEMM staging on bmg, lend mirror, fused dequant, lean verify graph, the xe expert ranking); same output tokens, B70 4K prompt 8.5 -> 3.3 s (128K config), decode +14-21% on prompts outside the ranking's corpus2eba71548

main
2.9 KiB62 linesraw
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
9    python 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():
28    ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
29    ap.add_argument("traces", nargs="+", help="TRACE or TRACE:WEIGHT")
30    ap.add_argument("--out", required=True)
31    ap.add_argument("--n-expert", type=int, default=mp.N_EXPERT)
32    a = ap.parse_args()
33    ne = a.n_expert
34    score = defaultdict(float)
35    for spec in a.traces:
36        path, weight = spec, 1.0
37        head, sep, tail = spec.rpartition(":")
38        if sep:   # TRACE:WEIGHT (a Windows drive letter's colon is not followed by a number)
39            try:
40                path, weight = head, float(tail)
41            except ValueError:
42                pass
43        freq = mp.read_trace(path, ne)
44        total = float(sum(freq.values()))
45        if total <= 0:
46            print(f"{path}: empty trace, skipped")
47            continue
48        for p, c in freq.items():
49            score[p] += weight * c / total
50    ranked = [p for p, _ in sorted(score.items(), key=lambda kv: (-kv[1], kv[0]))]
51    seen = set(ranked)
52    for e in range(ne):          # the fill, as make_profile.py: the pairs no trace used
53        for layer in range(mp.N_LAYER):
54            if (layer, e) not in seen:
55                ranked.append((layer, e))
56    mp.write_profile(a.out, ranked, ne)
57    assert mp.read_profile(a.out, ne) == ranked, "the profile did not survive the round trip"
58    print(f"wrote {a.out}: {len(ranked)} ranked pairs, {len(score)} from {len(a.traces)} traces")
59
60
61if __name__ == "__main__":
62    main()