Files
leleka/engine/strata/tools/make_profile_mix.py
T
Pavlo RatushnyiandClaude Sonnet 5.5 87e2633afe engine/strata: bring the Strata subtree to upstream 41cf05a0 (engine 0.1.50) with the local patches re-applied
Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree
(570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base =
the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main).

Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR
experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details,
conflict table and left-out files are in docs/upstream.md.

Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61),
docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out.

scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run").
Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR
macros on. No GPU run, no quality gate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 12:49:15 +01:00

63 lines
2.9 KiB
Python

"""tools/make_profile_mix.py - an expert-cache profile from several routing traces, each trace counting the same.
`make_profile.py` adds the traces' raw counts, so the longest trace decides the ranking. A cache profile should
rank the experts by how often a USER's tokens need them, across the kinds of text that user writes, so here every
trace is first turned into a distribution (its counts / its total) and multiplied by its weight; the ranking is the
weighted sum. Weights are per trace, so a kind of text with more traces counts more - give a group of N traces the
weight 1/N each to make the group count once.
python tools/make_profile_mix.py TRACE[:WEIGHT] [TRACE[:WEIGHT] ...] [--out PATH] [--n-expert 512]
A trace is a `--dump-routing` file, or a `STRATA_USAGE_DUMP` file of the SYCL engine (same record format). The output
is a profile for `--expert-profile`; the pairs no trace uses are appended in make_profile.py's fill order.
Measured on an Arc Pro B70 (IQ3_S, 11.5k cached experts, decode of 250-300 tokens): the shipped profile holds 60% of the
experts 45 chat/code/language prompts route and 85% of those a document summary routes; a profile from the 45 chat prompts
and the 8 summaries (weight 1 : 2 per prompt), tested on prompts left out of it, holds 71% and 84%.
"""
import argparse
import sys
from collections import defaultdict
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
import make_profile as mp # noqa: E402
def main():
ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
ap.add_argument("traces", nargs="+", help="TRACE or TRACE:WEIGHT")
ap.add_argument("--out", required=True)
ap.add_argument("--n-expert", type=int, default=mp.N_EXPERT)
a = ap.parse_args()
ne = a.n_expert
score = defaultdict(float)
for spec in a.traces:
path, weight = spec, 1.0
head, sep, tail = spec.rpartition(":")
if sep: # TRACE:WEIGHT (a Windows drive letter's colon is not followed by a number)
try:
path, weight = head, float(tail)
except ValueError:
pass
freq = mp.read_trace(path, ne)
total = float(sum(freq.values()))
if total <= 0:
print(f"{path}: empty trace, skipped")
continue
for p, c in freq.items():
score[p] += weight * c / total
ranked = [p for p, _ in sorted(score.items(), key=lambda kv: (-kv[1], kv[0]))]
seen = set(ranked)
for e in range(ne): # the fill, as make_profile.py: the pairs no trace used
for layer in range(mp.N_LAYER):
if (layer, e) not in seen:
ranked.append((layer, e))
mp.write_profile(a.out, ranked, ne)
assert mp.read_profile(a.out, ne) == ranked, "the profile did not survive the round trip"
print(f"wrote {a.out}: {len(ranked)} ranked pairs, {len(score)} from {len(a.traces)} traces")
if __name__ == "__main__":
main()