Datasets:
Download scripts/replay.py from perpetual-research/shadowbench: direct link, hf CLI and curl.
- Browser
- Download file 4.61 kB
-
https://huggingface.co/datasets/perpetual-research/shadowbench/resolve/main/scripts/replay.py
- Command line
-
hf download hf://datasets/perpetual-research/shadowbench/scripts/replay.py
-
curl -L -o replay.py https://huggingface.co/datasets/perpetual-research/shadowbench/resolve/main/scripts/replay.py
4.61 kB
| """Re-simulate recorded episodes from the `steps` table and check that the environment reproduces them exactly. | |
| uv run python scripts/replay.py # the best episode of the evaluation | |
| uv run python scripts/replay.py --episodes gpt-6.1-sol@medium/bursill_bunny claude-haiku-5.5@max/trewey_swan | |
| uv run python scripts/replay.py --all # all 360 episodes (about two days on one core) | |
| Each episode is replayed through a fresh environment on its certified target. The script checks that the first | |
| observation and every image a call returned are pixel-identical to the recorded ones, and that every call is | |
| accepted or refused as recorded, returns the recorded text, leaves the pose that hashes to `pose_hash` and has the | |
| recorded hidden metrics. | |
| """ | |
| import os | |
| os.environ.setdefault("OPENBLAS_NUM_THREADS", "1") | |
| import argparse | |
| import io | |
| import json | |
| from pathlib import Path | |
| import pyarrow.compute as pc | |
| import pyarrow.parquet as pq | |
| from PIL import Image, ImageChops | |
| from shadowbench.factory import create | |
| from shadowbench.native_environment import state_hash | |
| DATA = Path(__file__).resolve().parents[1] / "data" | |
| METRICS = ("hidden_score", "score_before_peeks", "iou", "boundary_f1", "hole_preservation", "ease") | |
| def same_image(a: Image.Image, recorded: dict) -> bool: | |
| with Image.open(io.BytesIO(recorded["bytes"])) as b: | |
| return a.size == b.size and ImageChops.difference(a.convert("RGB"), b.convert("RGB")).getbbox() is None | |
| def replay(episode_id: str) -> int: | |
| model, rest = episode_id.split("@", 1) | |
| effort, task_id = rest.split("/", 1) | |
| table = pq.read_table(DATA / "steps" / f"{effort}_effort" / f"{model}.parquet") | |
| rows = sorted(table.filter(pc.equal(table["episode_id"], episode_id)).to_pylist(), key=lambda s: s["step"]) | |
| if not rows: | |
| raise SystemExit(f"no steps for {episode_id}") | |
| episode = next(x for x in pq.read_table(DATA / "episodes" / f"{effort}_effort.parquet").to_pylist() | |
| if x["episode_id"] == episode_id) | |
| env = create(task_id, revision=episode["target_revision"]) | |
| if env.version != episode["env_fingerprint"]: | |
| raise SystemExit(f"environment fingerprint {env.version} differs from the recorded {episode['env_fingerprint']}; " | |
| "install the pinned versions with `uv sync`") | |
| problems = [] | |
| if state_hash(env.pose) != rows[0]["pose_hash"] or not same_image(env.observe(), rows[0]["image"]): | |
| problems.append("first observation") | |
| for s in rows[1:]: | |
| out = env.call(s["tool"], json.loads(s["arguments"])) | |
| m = env.metrics() | |
| got = {"hidden_score": m["score"], "score_before_peeks": m["score_before_peeks"], "iou": m["iou"], | |
| "boundary_f1": m["boundary_f1"], "hole_preservation": m["hole_preservation"], "ease": m["ease"]} | |
| if env.steps[-1]["accepted"] != s["accepted"]: | |
| problems.append(f"step {s['step']}: accepted {env.steps[-1]['accepted']}, recorded {s['accepted']}") | |
| if state_hash(env.pose) != s["pose_hash"]: | |
| problems.append(f"step {s['step']}: pose differs") | |
| if out.text != s["response"]: | |
| problems.append(f"step {s['step']}: response differs") | |
| if any(got[k] != s[k] for k in METRICS): | |
| problems.append(f"step {s['step']}: metrics differ " + str({k: (got[k], s[k]) for k in METRICS if got[k] != s[k]})) | |
| if (out.image is None) != (s["image"] is None) or (out.image is not None and not same_image(out.image, s["image"])): | |
| problems.append(f"step {s['step']}: image differs") | |
| if not env.done or env.final["score"] != episode["score"]: | |
| problems.append(f"final score {env.final and env.final['score']} != recorded {episode['score']}") | |
| n = len(rows) - 1 | |
| print(f"{episode_id}: {n} call{'' if n == 1 else 's'} replayed, " | |
| + ("all states, decisions, texts, images and scores identical" if not problems else f"{len(problems)} differences"), | |
| flush=True) | |
| for p in problems[:20]: | |
| print(" ", p) | |
| return len(problems) | |
| ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) | |
| ap.add_argument("--episodes", nargs="+", default=["claude-opus-5.5@max/hilliar_giraffe"]) | |
| ap.add_argument("--all", action="store_true") | |
| a = ap.parse_args() | |
| ids = a.episodes | |
| if a.all: | |
| ids = [x for e in ("medium", "high", "max") for x in | |
| pq.read_table(DATA / "episodes" / f"{e}_effort.parquet", columns=["episode_id"]).column(0).to_pylist()] | |
| bad = sum(replay(i) for i in ids) | |
| raise SystemExit(1 if bad else 0) | |