HelloSun commited on
Commit
2b58aa3
·
verified ·
1 Parent(s): 9468511

Delete run_negative.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. run_negative.py +0 -155
run_negative.py DELETED
@@ -1,155 +0,0 @@
1
- #!/usr/bin/env python3
2
- """Negative results: settings that looked promising and did not pay off.
3
-
4
- Two knobs are measured end to end under the same 8 GiB ceiling as the headline:
5
-
6
- prefetch
7
- Paging the next layer's experts while the current layer computes. The pager
8
- can predict them from the previous token's routing, but only a small share
9
- of the predicted experts are actually used, so the extra reads are mostly
10
- waste. ``SDQ_PREFETCH_LAYER_AHEAD=1`` turns it on.
11
-
12
- threads
13
- Only n_expert_used = 8 experts per token can run in parallel inside one
14
- layer, so more threads mostly contend for the same rows.
15
-
16
- Output: bench/results/strict_prefetch.json and bench/results/strict_threads.json
17
- """
18
-
19
- from __future__ import annotations
20
-
21
- import argparse
22
- import json
23
- import os
24
- import re
25
- import statistics
26
- import subprocess
27
- import sys
28
- from pathlib import Path
29
-
30
- HERE = Path(__file__).resolve().parent
31
- REPO = HERE.parent
32
-
33
- PROMPTS = [
34
- "Explain in one sentence why SSDs are slow.",
35
- "請用一句話介紹你自己。",
36
- "Write a short paragraph about SSD caching.",
37
- ]
38
-
39
-
40
- def run(strict8g: Path, binary: str, model: str, prompt: str, threads: int,
41
- n_predict: int, ctx: int, ubatch: int, budget_mb: int,
42
- record: Path, reserve_mb: int, env_extra: dict) -> dict:
43
- cmd = [
44
- sys.executable, str(strict8g),
45
- "--limit-mb", str(budget_mb), "--record", str(record), "--",
46
- binary, "-m", model, "-p", prompt, "-n", str(n_predict),
47
- "-c", str(ctx), "-t", str(threads), "-b", str(ubatch),
48
- "--temp", "0", "--no-chat", "--no-interactive",
49
- "--ram-budget-mb", str(budget_mb),
50
- "--stats-file", str(record.with_suffix(".stats")),
51
- ]
52
- env = dict(os.environ, SDQ_RESERVE_MB=str(reserve_mb))
53
- env.update(env_extra)
54
- p = subprocess.run(cmd, capture_output=True, text=True, env=env)
55
- out = p.stdout + p.stderr
56
-
57
- rec, stats = {}, {}
58
- if record.exists():
59
- try:
60
- rec = json.loads(record.read_text())
61
- except Exception:
62
- pass
63
- sp = record.with_suffix(".stats")
64
- if sp.exists():
65
- for line in sp.read_text().splitlines():
66
- if line.strip().startswith("{"):
67
- stats = json.loads(line)
68
-
69
- m = re.search(r"= ([\d.]+) tok/s", out)
70
- return {
71
- "rc": p.returncode,
72
- "strict_ok": bool(rec.get("strict_ok", False)),
73
- "tps": float(m.group(1)) if m else None,
74
- "hit_pct": stats.get("hit_rate"),
75
- "mb_per_token": (stats["ssd_bytes_per_token"] / 1048576.0) if stats.get("ssd_bytes_per_token") else None,
76
- "prefetch_issued": stats.get("prefetch_issued"),
77
- "prefetch_used": stats.get("prefetch_used"),
78
- "peak_anon_mb": rec.get("peak_anon_mb"),
79
- "peak_swap_mb": rec.get("peak_swap_mb"),
80
- }
81
-
82
-
83
- def sweep(arms: dict, a, label: str, out_name: str) -> None:
84
- strict8g = REPO / "tools" / "strict8g.py"
85
- (HERE / "results").mkdir(parents=True, exist_ok=True)
86
- rows = []
87
- for r in range(a.rounds):
88
- for name, cfg in arms.items():
89
- prompt = PROMPTS[r % len(PROMPTS)]
90
- rec = HERE / "results" / f".{out_name}-{name}-{r}.json"
91
- row = run(strict8g, a.bin, a.model, prompt, cfg["threads"],
92
- a.n_predict, a.ctx, a.ubatch, a.budget_mb, rec,
93
- cfg["reserve_mb"], cfg.get("env", {}))
94
- row["arm"] = name
95
- row["round"] = r
96
- rows.append(row)
97
- print(f"[{r + 1}/{a.rounds}] {name:<14} tps={row['tps']} "
98
- f"hit={row['hit_pct']} MB/tok={row['mb_per_token']} "
99
- f"ok={row['strict_ok']}", flush=True)
100
- rec.unlink(missing_ok=True)
101
- Path(str(rec) + ".stats").with_suffix(".stats").unlink(missing_ok=True)
102
-
103
- summary = {}
104
- for name in arms:
105
- tps = [r["tps"] for r in rows if r["arm"] == name and r["tps"]]
106
- summary[name] = {
107
- "n": len(tps),
108
- "median_tps": statistics.median(tps) if tps else None,
109
- "mean_tps": statistics.mean(tps) if tps else None,
110
- "stdev_tps": statistics.stdev(tps) if len(tps) > 1 else None,
111
- "min_tps": min(tps) if tps else None,
112
- "max_tps": max(tps) if tps else None,
113
- "all_strict_ok": all(r["strict_ok"] for r in rows if r["arm"] == name),
114
- }
115
- out = HERE / "results" / out_name
116
- out.write_text(json.dumps({"label": label, "rows": rows, "summary": summary},
117
- indent=2, ensure_ascii=False))
118
- print(json.dumps(summary, indent=2, ensure_ascii=False))
119
- print(f"-> {out}")
120
-
121
-
122
- def main() -> None:
123
- ap = argparse.ArgumentParser()
124
- ap.add_argument("--model", required=True)
125
- ap.add_argument("--bin", required=True)
126
- ap.add_argument("--which", choices=["prefetch", "threads"], required=True)
127
- ap.add_argument("--rounds", type=int, default=6)
128
- ap.add_argument("--budget-mb", type=int, default=8192)
129
- ap.add_argument("--reserve-mb", type=int, default=512)
130
- ap.add_argument("--ctx", type=int, default=4096)
131
- ap.add_argument("--ubatch", type=int, default=128)
132
- ap.add_argument("--n-predict", type=int, default=48)
133
- a = ap.parse_args()
134
-
135
- if a.which == "prefetch":
136
- arms = {
137
- "prefetch_off": {"threads": 8,
138
- "reserve_mb": a.reserve_mb, "env": {}},
139
- "prefetch_layer_ahead": {"threads": 8, "reserve_mb": a.reserve_mb,
140
- "env": {"SDQ_PREFETCH_LAYER_AHEAD": "1"}},
141
- "prefetch_end_token": {"threads": 8, "reserve_mb": a.reserve_mb,
142
- "env": {"SDQ_PREFETCH": "1",
143
- "SDQ_PREFETCH_LAYER_AHEAD": "0"}},
144
- }
145
- sweep(arms, a, "prefetch", "strict_prefetch.json")
146
- else:
147
- arms = {
148
- f"threads_{t}": {"threads": t, "reserve_mb": a.reserve_mb, "env": {}}
149
- for t in (4, 8, 10, 12, 16)
150
- }
151
- sweep(arms, a, "threads", "strict_threads.json")
152
-
153
-
154
- if __name__ == "__main__":
155
- main()