File size: 2,783 Bytes
9de6be7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Replay example actions over /ws and assert every episode ends done=True with reward 0.5.

Usage: python replay.py ws://127.0.0.1:8000/ws [log.json]
"""
import asyncio
import json
import sys
from datetime import datetime, timezone

import websockets

TASK_IDS = ["null"]
ACTIONS = [
    {"answer": "done"},                      # example action (submission example_actions[0])
    {},                                      # empty object
    {"answer": "42", "tool": "calc", "x": 1},  # stray keys a policy may emit
    {"answer": 0},                           # non-string answer
    {"answer": None},                        # null answer
]
# Replies that fail the schema never reach step(): the arena ends the episode and
# scores it through finish_action (guide, "How the model sees your environment").
REWARD = 0.5


async def episode(url, task_id, action):
    async with websockets.connect(url) as ws:
        await ws.send(json.dumps({"type": "reset", "data": {"task_id": task_id}}))
        reset = json.loads(await ws.recv())
        await ws.send(json.dumps({"type": "step", "data": action}))
        step = json.loads(await ws.recv())
        await ws.send(json.dumps({"type": "close"}))
    return {"task_id": task_id, "action": action, "reset": reset, "step": step}


async def main(url, log_path):
    results = []
    for task_id in TASK_IDS:
        for action in ACTIONS:
            r = await episode(url, task_id, action)
            data = r["step"].get("data", {})
            r["ok"] = (r["reset"].get("type") == "observation" and r["step"].get("type") == "observation"
                       and data.get("done") is True and data.get("reward") == REWARD)
            results.append(r)
            print(("OK  " if r["ok"] else "FAIL"), task_id, json.dumps(action), "->", json.dumps(data))
    # One GRPO group: 5 rollouts of the same task in parallel sessions.
    group = await asyncio.gather(*(episode(url, TASK_IDS[0], ACTIONS[0]) for _ in range(5)))
    rewards = [g["step"].get("data", {}).get("reward") for g in group]
    group_ok = all(g["step"].get("data", {}).get("done") is True for g in group) and rewards == [REWARD] * 5
    print(("OK  " if group_ok else "FAIL"), "concurrent group of 5 rewards", rewards)
    results.append({"task_id": TASK_IDS[0], "concurrent_group_rewards": rewards, "ok": group_ok})
    log = {"url": url, "at_utc": datetime.now(timezone.utc).isoformat(timespec="seconds"),
           "episodes": results, "all_ok": all(r["ok"] for r in results)}
    if log_path:
        with open(log_path, "w") as f:
            json.dump(log, f, indent=1)
            f.write("\n")
    return 0 if log["all_ok"] else 1


if __name__ == "__main__":
    sys.exit(asyncio.run(main(sys.argv[1], sys.argv[2] if len(sys.argv) > 2 else None)))