File size: 3,585 Bytes
c4c86e0
c0b76d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4c86e0
c0b76d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
"""Re-run a pull request on MacroSwan/pilot-tasks in simulation and post the score on the PR page.

    python scripts/score_pr.py 7                      # PR number; needs `pip install -e <cablepilot>[sim,hub]`
    python scripts/score_pr.py 7 --seeds 0 1 2 --worlds bedroom playroom --post

The contributor's own result is a claim; this is the check. It downloads the PR's card and tuning (falling back to the
task's merged ones), runs each (world, seed), and compares with the merged best on the same worlds and seeds when the
board has them. Designed to run on the Mac or as a Hugging Face Job (CPU is enough: ~25 min per world and seed).
Nothing from the PR is executed as code: cards and tunings are YAML, validated before the run.
"""
from __future__ import annotations

import argparse
import json
import subprocess
import sys
import tempfile
from pathlib import Path

REPO = "MacroSwan/pilot-tasks"


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("pr", type=int)
    ap.add_argument("--seeds", type=int, nargs="+", default=[0])
    ap.add_argument("--worlds", nargs="+", default=["bedroom"])
    ap.add_argument("--post", action="store_true", help="comment the result on the PR (else print it)")
    a = ap.parse_args()

    from huggingface_hub import HfApi, snapshot_download
    api = HfApi()
    rev = f"refs/pr/{a.pr}"
    details = api.get_discussion_details(REPO, a.pr, repo_type="dataset")
    work = Path(tempfile.mkdtemp(prefix=f"pr{a.pr}_"))
    snap = Path(snapshot_download(REPO, repo_type="dataset", revision=rev, local_dir=work / "pr"))
    tasks = sorted(p.parent.name for p in snap.glob("tasks/*/card.yaml"))
    title = details.title
    task = next((t for t in tasks if f"[{t}]" in title), None)
    if task is None:
        sys.exit(f"PR title must start with [<task>]: {title!r}")
    tdir = snap / "tasks" / task
    card, tuning = tdir / "card.yaml", tdir / "tuning" / "cable.yaml"
    lines = [f"**Re-run of PR #{a.pr}** (`{task}`, cable body, simulation)", "",
             "| world | seed | done | s / item | furniture ticks | max tension | plan ms |", "|---|---|---|---|---|---|---|"]
    rows = []
    for w in a.worlds:
        for s in a.seeds:
            out = work / f"run_{w}_s{s}"
            cmd = ["cablepilot", "run", str(card), "--world", w, "--seed", str(s), "--out", str(out), "--by", "pilot-scorer"]
            if tuning.exists():
                cmd += ["--tuning", str(tuning)]
            r = subprocess.run(cmd, capture_output=True, text=True)
            if r.returncode != 0 or not (out / "result.json").exists():
                lines.append(f"| {w} | {s} | **failed** | | | | |")
                print(r.stdout[-2000:], r.stderr[-2000:])
                continue
            res = json.loads((out / "result.json").read_text())
            sc = res["score"]
            rows.append(res)
            lines.append(f"| {w} | {s} | {sc['done']}/{sc['of']} | {sc['s_per_item']:.0f} | {sc['furniture_contact_ticks']} | "
                         f"{sc['max_tension_N']:.0f} N | {res['compute']['plan_ms_mean']:.0f} |")
    done = sum(r["score"]["done"] for r in rows)
    of = sum(r["score"]["of"] for r in rows)
    lines += ["", f"Total: **{done}/{of}**. card `{rows[0]['card_sha'] if rows else '?'}`, tuning "
              f"`{rows[0]['tuning_sha'] if rows else '?'}`. Same worlds and seeds as the board's best are the fair comparison."]
    text = "\n".join(lines)
    print(text)
    if a.post:
        api.comment_discussion(REPO, a.pr, text, repo_type="dataset")


if __name__ == "__main__":
    main()