File size: 3,298 Bytes
1c4cb91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
"""Measure the expert-cache miss curve -- the decisive Stage E experiment.

If hit rate does not rise steeply with cache size, routing has no temporal
locality, expert streaming cannot hide NVMe latency, and the architecture must
fall back to a smaller resident model. Run this before building anything else.

Usage:
    python scripts/benchmark_runtime.py --routes traces.npy --expert-mib 20
"""
from __future__ import annotations

import argparse
import json

import numpy as np

from spectra_moe.analysis.budget import decode_tokens_per_s
from spectra_moe.models.surrogate import build_surrogate, simulate_routes
from spectra_moe.runtime.engine import ExpertStore, StreamingMoERuntime


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--routes", default=None, help="npy routing trace (tokens x topk)")
    ap.add_argument("--expert-mib", type=float, default=20.0)
    ap.add_argument("--cache-gib", type=float, nargs="+", default=[2, 4, 8, 14])
    ap.add_argument("--policy", nargs="+", default=["arc", "lru"])
    ap.add_argument("--nvme-gbs", type=float, default=5.0)
    ap.add_argument("--bandwidth-gbs", type=float, default=273.0)
    ap.add_argument("--accepted", type=float, default=3.0)
    ap.add_argument("--speculate", action="store_true",
                    help="supply the trace as lookahead (perfect draft model)")
    ap.add_argument("--out", default=None)
    args = ap.parse_args()

    if args.routes:
        routes = np.load(args.routes)
        n_experts = int(routes.max()) + 1
    else:
        print("no --routes given: using a synthetic locality-0.7 trace")
        m = build_surrogate(n_layers=1, n_experts=896, expert_shape=(8, 8), seed=0)
        routes = simulate_routes(m, n_tokens=4000, locality=0.7, seed=1)
        n_experts = 896

    eb = int(args.expert_mib * 1024 ** 2)
    store = ExpertStore(lambda l, e: np.zeros(1), eb)   # payload irrelevant for timing

    rows = []
    print(f"{'policy':>7} {'cache GiB':>10} {'experts':>8} {'hit':>7} {'tok/s':>8}")
    for policy in args.policy:
        for gib in args.cache_gib:
            rt = StreamingMoERuntime(
                store, int(gib * 1024 ** 3), nvme_gbs=args.nvme_gbs,
                memory_bandwidth_gbs=args.bandwidth_gbs, policy=policy,
            )
            st = rt.run_trace(routes, lookahead=routes if args.speculate else None,
                              accepted_per_step=args.accepted)
            rt.shutdown()
            tps = st.tokens / max(st.decode_seconds, 1e-9)
            rows.append({"policy": policy, "cache_gib": gib, "hit_rate": st.hit_rate,
                         "tokens_per_s": tps, **st.to_dict()})
            print(f"{policy:>7} {gib:>10.1f} {int(gib * 1024**3 // eb):>8} "
                  f"{st.hit_rate:>7.3f} {tps:>8.2f}")

    print("\nanalytic cross-check (20B active @ 2.4 bits):")
    for hit in (0.5, 0.8, 0.9, 0.95):
        print(f"  hit {hit:.0%} -> "
              f"{decode_tokens_per_s(2e10, 2.4, args.bandwidth_gbs, hit, args.nvme_gbs, args.accepted):.2f} tok/s")

    if args.out:
        with open(args.out, "w") as fh:
            json.dump(rows, fh, indent=2, default=float)
        print(f"\nwrote {args.out}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())