Make runs resumable: append per generation, skip completed
Browse files
README.md
CHANGED
|
@@ -86,3 +86,17 @@ Report four numbers, and never one alone:
|
|
| 86 |
Verified against degenerate strategies: always-VULNERABLE scores 67 percent raw and **50 percent
|
| 87 |
balanced**; always-SAFE scores 33 percent raw and **50 percent balanced**. Raw accuracy is printed
|
| 88 |
but should never be quoted alone.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
Verified against degenerate strategies: always-VULNERABLE scores 67 percent raw and **50 percent
|
| 87 |
balanced**; always-SAFE scores 33 percent raw and **50 percent balanced**. Raw accuracy is printed
|
| 88 |
but should never be quoted alone.
|
| 89 |
+
|
| 90 |
+
## Running it
|
| 91 |
+
|
| 92 |
+
python3 guardbench/run.py --tag <name> [--model <path>] [--adapter <path>] [--prompts 1|2|3]
|
| 93 |
+
|
| 94 |
+
Generations are appended to `results/partial_<tag>.jsonl` as they complete, and a re-run skips
|
| 95 |
+
what is already there. An interrupted run resumes rather than starting over. `--prompts 1` drops
|
| 96 |
+
the brittleness measurement and cuts the run to a third.
|
| 97 |
+
|
| 98 |
+
To score a partial or finished run without generating anything:
|
| 99 |
+
|
| 100 |
+
python3 -c "import json,sys; sys.path.insert(0,'guardbench'); import run; \
|
| 101 |
+
rows=[json.loads(l) for l in open('guardbench/results/partial_<tag>.jsonl')]; \
|
| 102 |
+
run.report(rows,'<tag>')"
|
run.py
CHANGED
|
@@ -60,30 +60,45 @@ def main():
|
|
| 60 |
a = ap.parse_args()
|
| 61 |
|
| 62 |
items = [json.loads(l) for l in open(a.items) if l.strip()]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
from mlx_lm import load, stream_generate
|
| 64 |
from mlx_lm.sample_utils import make_sampler
|
| 65 |
m, tok = load(a.model, adapter_path=a.adapter)
|
| 66 |
s = make_sampler(temp=0.0)
|
| 67 |
|
| 68 |
-
|
| 69 |
total = len(items) * a.prompts
|
| 70 |
for i, it in enumerate(items):
|
| 71 |
for pi in range(a.prompts):
|
|
|
|
|
|
|
| 72 |
g = generate_raw(stream_generate, m, tok,
|
| 73 |
build_chat_prompt(tok, SYS, PROMPTS[pi].format(code=it["code"]), enable_thinking=False),
|
| 74 |
max_tokens=a.max_tokens, sampler=s)
|
| 75 |
v, cls = parse(g["raw_output"])
|
| 76 |
want = "VULNERABLE" if it["label"] == "unsafe" else "SAFE"
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
done = (i+1) * a.prompts
|
| 82 |
if (i+1) % 5 == 0:
|
| 83 |
r = done / (time.time()-t0)
|
| 84 |
print(f" {done}/{total} eta {(total-done)/r/60:.0f}m", flush=True)
|
| 85 |
|
| 86 |
-
|
| 87 |
out = os.path.join(a.outdir, f"guardbench_{a.tag}.json")
|
| 88 |
json.dump(dict(tag=a.tag, model=a.model, adapter=a.adapter, rows=rows), open(out, "w"), indent=1)
|
| 89 |
report(rows, a.tag, out)
|
|
|
|
| 60 |
a = ap.parse_args()
|
| 61 |
|
| 62 |
items = [json.loads(l) for l in open(a.items) if l.strip()]
|
| 63 |
+
os.makedirs(a.outdir, exist_ok=True)
|
| 64 |
+
partial = os.path.join(a.outdir, f"partial_{a.tag}.jsonl")
|
| 65 |
+
done, rows = set(), []
|
| 66 |
+
if os.path.exists(partial):
|
| 67 |
+
for line in open(partial):
|
| 68 |
+
try:
|
| 69 |
+
r = json.loads(line); done.add((r["id"], r["prompt"])); rows.append(r)
|
| 70 |
+
except Exception:
|
| 71 |
+
pass
|
| 72 |
+
print(f"resuming: {len(rows)} generations already on disk", flush=True)
|
| 73 |
+
fh = open(partial, "a")
|
| 74 |
from mlx_lm import load, stream_generate
|
| 75 |
from mlx_lm.sample_utils import make_sampler
|
| 76 |
m, tok = load(a.model, adapter_path=a.adapter)
|
| 77 |
s = make_sampler(temp=0.0)
|
| 78 |
|
| 79 |
+
t0 = time.time()
|
| 80 |
total = len(items) * a.prompts
|
| 81 |
for i, it in enumerate(items):
|
| 82 |
for pi in range(a.prompts):
|
| 83 |
+
if (it["id"], pi) in done:
|
| 84 |
+
continue
|
| 85 |
g = generate_raw(stream_generate, m, tok,
|
| 86 |
build_chat_prompt(tok, SYS, PROMPTS[pi].format(code=it["code"]), enable_thinking=False),
|
| 87 |
max_tokens=a.max_tokens, sampler=s)
|
| 88 |
v, cls = parse(g["raw_output"])
|
| 89 |
want = "VULNERABLE" if it["label"] == "unsafe" else "SAFE"
|
| 90 |
+
row = dict(id=it["id"], pattern=it["pattern"], shape=it["shape"],
|
| 91 |
+
label=it["label"], prompt=pi, verdict=v, verdict_class=cls,
|
| 92 |
+
correct=(v == want), tokens=g["generation_tokens"],
|
| 93 |
+
finish=g["finish_reason"], output=g["raw_output"])
|
| 94 |
+
rows.append(row)
|
| 95 |
+
fh.write(json.dumps(row) + "\n"); fh.flush()
|
| 96 |
done = (i+1) * a.prompts
|
| 97 |
if (i+1) % 5 == 0:
|
| 98 |
r = done / (time.time()-t0)
|
| 99 |
print(f" {done}/{total} eta {(total-done)/r/60:.0f}m", flush=True)
|
| 100 |
|
| 101 |
+
fh.close()
|
| 102 |
out = os.path.join(a.outdir, f"guardbench_{a.tag}.json")
|
| 103 |
json.dump(dict(tag=a.tag, model=a.model, adapter=a.adapter, rows=rows), open(out, "w"), indent=1)
|
| 104 |
report(rows, a.tag, out)
|