danym commited on
Commit
efcfd0d
·
verified ·
1 Parent(s): e55784f

Upload mi300x_9b/niah.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. mi300x_9b/niah.py +127 -0
mi300x_9b/niah.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ """Minimal needle-in-a-haystack long-context retrieval check (stdlib only).
3
+
4
+ Builds a long filler haystack, inserts a unique passcode needle at a chosen
5
+ depth, asks for the passcode, and checks the greedy completion contains it.
6
+ Used to gate the LCM page-unify split (L19 kept NATURAL (3 retr,1 windowed-local))
7
+ against the phantom baseline (L19 DEMOTED to (4,0) all-retrieval) at the long
8
+ contexts where the windowed-local heads actually matter.
9
+
10
+ Usage (run on the GPU host, server on :8000):
11
+ python bench/niah.py --model rtp9 --tag lcm --out /work/scratch/niah_lcm.json
12
+ python bench/niah.py --model rtp9 --tag phantom --out /work/scratch/niah_phantom.json
13
+ python bench/niah.py --summary /work/scratch/niah_lcm.json
14
+ """
15
+ from __future__ import annotations
16
+
17
+ import argparse
18
+ import json
19
+ import sys
20
+ import urllib.request
21
+
22
+ # Filler sentence ~ a handful of tokens; repeated to reach target length.
23
+ FILLER = (
24
+ "The city archives record the slow turning of the seasons over the river "
25
+ "delta, where merchants once traded salt and amber along the old stone road. "
26
+ )
27
+ NEEDLE = "The secret passcode for the vault is {code}. Remember it well. "
28
+ QUESTION = (
29
+ "\n\nQuestion: What is the secret passcode for the vault? "
30
+ "Answer with only the number.\nAnswer:"
31
+ )
32
+
33
+
34
+ def gen(url, model, prompt, n_decode):
35
+ body = json.dumps({
36
+ "model": model,
37
+ "prompt": prompt,
38
+ "max_tokens": n_decode,
39
+ "temperature": 1.0,
40
+ "top_p": 0.95,
41
+ "top_k": 20,
42
+ "min_p": 0.0,
43
+ "presence_penalty": 1.5,
44
+ "repetition_penalty": 1.0,
45
+ "seed": 0,
46
+ "ignore_eos": False,
47
+ "stream": False,
48
+ }).encode("utf-8")
49
+ req = urllib.request.Request(
50
+ url, data=body,
51
+ headers={"Content-Type": "application/json"},
52
+ method="POST",
53
+ )
54
+ with urllib.request.urlopen(req, timeout=1800) as resp:
55
+ return json.loads(resp.read().decode("utf-8"))
56
+
57
+
58
+ def build_prompt(approx_tokens, depth_frac, code):
59
+ # ~4 chars/token heuristic; FILLER is ~30 tokens.
60
+ target_chars = approx_tokens * 4
61
+ needle = NEEDLE.format(code=code)
62
+ body_chars = max(0, target_chars - len(needle) - len(QUESTION))
63
+ n_filler = max(1, body_chars // len(FILLER))
64
+ pre_n = int(n_filler * depth_frac)
65
+ post_n = n_filler - pre_n
66
+ pre = FILLER * pre_n
67
+ post = FILLER * post_n
68
+ return f"{pre}{needle}{post}{QUESTION}"
69
+
70
+
71
+ def run(args):
72
+ code = "738291"
73
+ lengths = [int(x) for x in args.lengths.split(",")]
74
+ depths = [float(x) for x in args.depths.split(",")]
75
+ results = []
76
+ for L in lengths:
77
+ for d in depths:
78
+ prompt = build_prompt(L, d, code)
79
+ resp = gen(args.url, args.model, prompt, args.decode_tokens)
80
+ choice = resp["choices"][0]
81
+ text = choice.get("text", "")
82
+ ptok = resp.get("usage", {}).get("prompt_tokens")
83
+ ok = code in text
84
+ results.append({
85
+ "approx_len": L, "prompt_tokens": ptok,
86
+ "depth": d, "found": ok, "out": text[:80],
87
+ })
88
+ print(f"[{args.tag}] len~{L} ptok={ptok} depth={d:.2f} "
89
+ f"found={ok} out={text[:48]!r}")
90
+ rec = {"tag": args.tag, "code": code, "results": results}
91
+ if args.out:
92
+ with open(args.out, "w", encoding="utf-8") as f:
93
+ json.dump(rec, f)
94
+ n_ok = sum(r["found"] for r in results)
95
+ print(f"[{args.tag}] PASS {n_ok}/{len(results)}")
96
+ return 0
97
+
98
+
99
+ def summary(path):
100
+ rec = json.load(open(path, encoding="utf-8"))
101
+ rs = rec["results"]
102
+ n_ok = sum(r["found"] for r in rs)
103
+ print(f"[{rec['tag']}] PASS {n_ok}/{len(rs)}")
104
+ for r in rs:
105
+ mark = "OK " if r["found"] else "MISS"
106
+ print(f" {mark} ptok={r['prompt_tokens']} depth={r['depth']:.2f}")
107
+ return 0
108
+
109
+
110
+ def main():
111
+ ap = argparse.ArgumentParser()
112
+ ap.add_argument("--url", default="http://localhost:8000/v1/completions")
113
+ ap.add_argument("--model", default="rtp9")
114
+ ap.add_argument("--lengths", default="8000,32000,64000,120000")
115
+ ap.add_argument("--depths", default="0.1,0.5,0.9")
116
+ ap.add_argument("--decode-tokens", type=int, default=8192)
117
+ ap.add_argument("--tag", default="run")
118
+ ap.add_argument("--out", default="")
119
+ ap.add_argument("--summary", default="")
120
+ args = ap.parse_args()
121
+ if args.summary:
122
+ return summary(args.summary)
123
+ return run(args)
124
+
125
+
126
+ if __name__ == "__main__":
127
+ sys.exit(main())