Foundational Compute

Grade evals in parallel

Grade model answers in 4 sandboxes at once with the CLI, then add up the score.

Templatebase (2 vCPU, 512 MiB, $0.039 per hour)
Run timeAbout 18 seconds
Cost of a runLess than $0.001. 100 sandboxes for 30 minutes cost $1.95.
Cleanuprun.sh ends with fdtn kill. If a step fails, set -e skips it: run fdtn kill --all -s running,paused -m eval=eval-1.

1The files

Put four files in one folder. make_tasks.py writes a model answer and a test per task. Three answers are wrong and one never ends.

make_tasks.py
# Write 8 graded tasks: model answers (code) and a test for each, in 4 parts.
import json, os
tasks = [
  ("add",      "def f(a, b): return a + b",            "assert f(2, 3) == 5"),
  ("reverse",  "def f(s): return s[::-1]",             "assert f('abc') == 'cba'"),
  ("max",      "def f(xs): return sorted(xs)[0]",      "assert f([3, 9, 1]) == 9"),
  ("fizz",     "def f(n): return 'Fizz' if n % 3 == 0 else str(n)", "assert f(9) == 'Fizz'"),
  ("square",   "def f(x): return x ** 2",              "assert f(7) == 49"),
  ("loop",     "def f(n):\n    while True: pass",      "assert f(1) == 1"),
  ("vowels",   "def f(s): return sum(c in 'aeiou' for c in s)", "assert f('banana') == 3"),
  ("evens",    "def f(xs): return [x for x in xs if x % 2]", "assert f([1, 2, 4]) == [2, 4]"),
]
os.makedirs("tasks", exist_ok=True)
for part in range(4):
    with open(f"tasks/part-{part + 1}.jsonl", "w") as out:
        for name, answer, test in tasks[part::4]:
            out.write(json.dumps({"id": name, "answer": answer, "test": test}) + "\n")

eval.py runs in each sandbox and grades each task with a 5-second limit.

eval.py
# Runs in each sandbox: grade every task in tasks.jsonl, write results.
import json, os, subprocess
os.makedirs("/home/user/results", exist_ok=True)
with open("/home/user/tasks.jsonl") as f, open("/home/user/results/scores.jsonl", "w") as out:
    for line in f:
        t = json.loads(line)
        try:
            p = subprocess.run(["python3", "-c", t["answer"] + "\n" + t["test"]],
                               capture_output=True, timeout=5)
            passed = p.returncode == 0
        except subprocess.TimeoutExpired:
            passed = False
        out.write(json.dumps({"id": t["id"], "passed": passed}) + "\n")
        print(t["id"], "pass" if passed else "fail")

score.py adds up the results on your computer.

score.py
# Runs on your computer: add up the results from every sandbox.
import glob, json
rows = [json.loads(l) for p in glob.glob("results-1/*/scores.jsonl") for l in open(p)]
passed = sum(r["passed"] for r in rows)
print(f"{passed}/{len(rows)} passed:", ", ".join(sorted(r["id"] for r in rows if not r["passed"])), "failed")

run.sh does the whole run.

run.sh
#!/bin/sh
set -e
RUN=eval-1
python3 make_tasks.py
# 1. Start 4 sandboxes in one group: eval-1 to eval-4.
fdtn create base -d --count 4 --name eval --timeout 1800 -m eval=$RUN
# 2. Give each sandbox the grader and its part of the tasks.
for i in 1 2 3 4; do
  fdtn cp ./eval.py @eval-$i:/home/user/eval.py
  fdtn cp ./tasks/part-$i.jsonl @eval-$i:/home/user/tasks.jsonl
done
# 3. Grade in all sandboxes at the same time.
fdtn each -m eval=$RUN -- python3 /home/user/eval.py
# 4. Download the results and add them up.
fdtn collect -m eval=$RUN --path /home/user/results --destination ./results-1
python3 score.py
# 5. Stop the group.
fdtn kill --all -s running,paused -m eval=$RUN

2Run it

IDs and line order change from run to run:

$ sh run.sh
ieryw125eo7fgpozaqwjvpy8
is65lx45vmlpkbw4ha6albhb
id3zwht93lg3f3lwe4hkgkj8
ifg9yblvoaaqewgy5uh2ywz9
Copied 1 file (683 bytes) to /home/user/eval.py.
Copied 1 file (167 bytes) to /home/user/tasks.jsonl.
...
[id3zwht93lg3f3lwe4hkgkj8] fizz pass
[id3zwht93lg3f3lwe4hkgkj8] evens fail
[ifg9yblvoaaqewgy5uh2ywz9] max fail
[ifg9yblvoaaqewgy5uh2ywz9] vowels pass
[ieryw125eo7fgpozaqwjvpy8] add pass
[ieryw125eo7fgpozaqwjvpy8] square pass
[is65lx45vmlpkbw4ha6albhb] reverse pass
[is65lx45vmlpkbw4ha6albhb] loop fail
...
[1/4] Downloading is65lx45vmlpkbw4ha6albhb…
  Downloaded 1 file (66 bytes).
...
5/8 passed: evens, loop, max failed
Stopped 4 sandboxes.

Notes

  • --count 4 --name eval starts eval-1 to eval-4. Use @eval-2 in place of an ID.
  • Use a new -m eval= value for each run, so two runs never mix.
  • fdtn each runs 5 commands at a time by default, up to 20 with --concurrency 20.
  • If a download fails, run fdtn collect --retry-failed --destination ./results-1.

Scale it up

  1. Raise --count (up to 100 per create) and split the tasks into as many parts.
  2. When machines are busy, extra starts wait in the queue for up to 30 minutes. Queue
  3. Put a slow install into a template. Templates
  4. For Terminal-Bench and other Harbor tasks, use fdtn harbor. Evals with Harbor

Next