File size: 3,137 Bytes
602054e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
"""Acceptance grader for bench test fixture FT-2.

Runs /app/controller.py through the gauntlet on the hidden episodes, with this directory's own copies of the
gauntlet, task.py, scoring.py, the scene and the robot model. The controller runs in a separate, unprivileged
process (gauntlet/harness.py, the same module run_gauntlet.py uses): it receives each observation pickled by this
side and answers with raw float64 bytes or a small JSON status object, so nothing from the controller is ever
unpickled here. Writes /logs/verifier/reward.txt (mean episode score) and details.json.
"""
import hashlib
import json
import os
import sys
import time

HERE = os.path.dirname(os.path.abspath(__file__))
WORLD = os.path.join(HERE, "world")
sys.path.insert(0, WORLD)
import task as task_mod  # noqa: E402  (also puts WORLD/gauntlet on sys.path)
import gauntlet as G  # noqa: E402
import harness  # noqa: E402
import scoring  # noqa: E402

APP = os.environ.get("FT2_APP_DIR", "/app")
LOG_DIR = os.environ.get("FT2_LOG_DIR", "/logs/verifier")
CONTROLLER = os.path.join(APP, "controller.py")


def sha256(path):
  with open(path, "rb") as f:
    return hashlib.sha256(f.read()).hexdigest()


def write_result(reward, details):
  os.makedirs(LOG_DIR, exist_ok=True)
  with open(os.path.join(LOG_DIR, "reward.txt"), "w") as f:
    f.write(f"{reward:.6f}\n")
  with open(os.path.join(LOG_DIR, "details.json"), "w") as f:
    json.dump(details, f, indent=1, default=str)
  print(f"reward {reward:.6f}")


def main():
  with open(os.path.join(HERE, "hidden_seeds.json")) as f:
    spec = json.load(f)
  with open(os.path.join(HERE, "app_sha256.json")) as f:
    expected = json.load(f)
  details = {"episodes": []}
  for rel, h in expected.items():
    p = os.path.join(APP, rel)
    if not os.path.isfile(p) or sha256(p) != h:
      details["error"] = f"/app/{rel} differs from the shipped file (it is read-only)"
      return write_result(0.0, details)
  if not os.path.isfile(CONTROLLER):
    details["error"] = "/app/controller.py not found"
    return write_result(0.0, details)
  if os.geteuid() == 0:
    try:
      os.chmod(HERE, 0o700)        # the controller runs as nobody and cannot read the hidden seeds
    except Exception:
      pass

  os.makedirs(LOG_DIR, exist_ok=True)
  ws = harness.Workspace(CONTROLLER, APP, stderr_path=os.path.join(LOG_DIR, "controller_stderr.log"))
  task = task_mod.TASK
  rows, t_all = [], time.monotonic()
  try:
    for tier, n in spec["episodes"].items():
      for idx in range(n):
        r = harness.run_episode(G, task, ws, tier, idx, int(spec["base_seed"]), os.path.join(APP, "scene.xml"))
        x = scoring.summarize(r)
        rows.append(x)
        print(f"{tier} {idx:2d} band={x['band']} score={x['score']:.3f} fully_done={x['fully_done']} "
              f"violations={x['violations']}", flush=True)
  finally:
    ws.close()
  details.update(scoring.aggregate(rows))
  details["mix"] = spec["episodes"]
  details["episodes"] = rows
  print(f"grading wall time {time.monotonic() - t_all:.1f}s", file=sys.stderr)
  write_result(details["reward"], details)


if __name__ == "__main__":
  main()