ds41-dismantle / scripts /copy_multi.py
grantabejar's picture
Upload folder using huggingface_hub
c7267b1 verified
Raw History Blame Contribute Delete
2.18 kB
#!/usr/bin/env python3
"""Multi-string verbatim copy probe (engram-sensitive). N random strings x reps,
exact-match grading, per-string diagnostics."""
import json, random, sys, urllib.request
PORT = int(sys.argv[2]) if len(sys.argv) > 2 else 11234
MODEL = sys.argv[1]
N = int(sys.argv[3]) if len(sys.argv) > 3 else 16
REPS = int(sys.argv[4]) if len(sys.argv) > 4 else 2
ALPHA = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"
LENS = [160, 240, 320]
random.seed(20261115)
cases = []
for i in range(N):
n = LENS[i % len(LENS)]
cases.append(("".join(random.choice(ALPHA) for _ in range(n))))
def ask(s):
p = f"Copy the following string exactly, character for character, and output nothing else:\n\n{s}\n"
b = json.dumps({"model": MODEL, "messages": [{"role": "user", "content": p}],
"max_tokens": 4096, "temperature": 0}).encode()
r = urllib.request.Request(f"http://127.0.0.1:{PORT}/v1/chat/completions", data=b,
headers={"content-type": "application/json"})
d = json.load(urllib.request.urlopen(r, timeout=3600))
c = d["choices"][0]
return (c["message"].get("content") or "").strip(), c.get("finish_reason")
ok = tot = 0
detail = []
for i, s in enumerate(cases):
for rep in range(REPS):
try:
out, fin = ask(s)
good = out == s
first = next((j for j, (a, b) in enumerate(zip(s, out)) if a != b), None)
ok += good; tot += 1
detail.append({"i": i, "len": len(s), "rep": rep, "pass": good,
"finish": fin, "out_len": len(out), "first_diff": first})
print(f"case{i:02d} len={len(s)} rep{rep} {'PASS' if good else 'FAIL'} "
f"fin={fin} out={len(out)} diff@{first}", flush=True)
except Exception as e:
tot += 1
detail.append({"i": i, "len": len(s), "rep": rep, "pass": False, "error": repr(e)})
print(f"case{i:02d} ERROR {e!r}", flush=True)
print(f"COPY {MODEL} {ok}/{tot} = {ok/tot:.3f}")
json.dump({"model": MODEL, "cases": cases, "detail": detail, "score": ok / tot},
open(f"/tmp/re_stock/copy_multi_{MODEL}.json", "w"), indent=1)