Repository navigation
Expand file tree
/
Copy patheval_samples.py
More file actions
71 lines (56 loc) · 2.35 KB
/
Copy patheval_samples.py
File metadata and controls
71 lines (56 loc) · 2.35 KB
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
"""Generate fixed-prompt samples from each successful run's adapter.
python scripts/eval_samples.py # every run with status=ok
python scripts/eval_samples.py r1 r2 # specific runs
Prompts use the same Alpaca template as training. Greedy decoding (temp 0),
fixed seed and max tokens. Output: results/samples/<run_id>.md
"""
from __future__ import annotations
import csv
import sys
from pathlib import Path
import mlx.core as mx
import yaml
from mlx_lm import generate, load
from mlx_lm.sample_utils import make_sampler
ROOT = Path(__file__).resolve().parent.parent
SAMPLES = ROOT / "results" / "samples"
MAX_TOKENS = 256
SEED = 0
TEMPLATE = (
"Below is an instruction that describes a task. Write a response that "
"appropriately completes the request.\n\n"
"### Instruction:\n{instruction}\n\n### Response:\n"
)
def ok_runs() -> list[str]:
with (ROOT / "results" / "runs.csv").open() as f:
return [r["run_id"] for r in csv.DictReader(f) if r["status"] == "ok"]
def main() -> None:
run_ids = sys.argv[1:] or ok_runs()
prompts = (ROOT / "prompts" / "eval_prompts.txt").read_text().strip().splitlines()
configs = {p.stem.split("_")[0]: p for p in (ROOT / "configs").glob("*.yaml")}
SAMPLES.mkdir(parents=True, exist_ok=True)
for run_id in run_ids:
cfg = yaml.safe_load(configs[run_id].read_text())
print(f"== {run_id}: {cfg['model']} + {cfg['adapter_path']}")
model, tokenizer = load(
str(ROOT / cfg["model"]) if (ROOT / cfg["model"]).exists() else cfg["model"],
adapter_path=str(ROOT / cfg["adapter_path"]),
)
sampler = make_sampler(temp=0.0)
lines = [
f"# {run_id} samples\n",
f"Base: `{cfg['model']}` · adapter: `{cfg['adapter_path']}` (unfused) · "
f"greedy, max_tokens={MAX_TOKENS}, seed={SEED}\n",
]
for i, instruction in enumerate(prompts, 1):
mx.random.seed(SEED)
text = generate(
model, tokenizer, prompt=TEMPLATE.format(instruction=instruction),
max_tokens=MAX_TOKENS, sampler=sampler,
)
lines += [f"## {i}. {instruction}\n", f"```\n{text.strip()}\n```\n"]
(SAMPLES / f"{run_id}.md").write_text("\n".join(lines))
del model, tokenizer
mx.clear_cache()
if __name__ == "__main__":
main()