File size: 2,428 Bytes
f6c685a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8755f5e
f6c685a
8755f5e
f6c685a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
634b85a
 
f6c685a
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
import os
import sys
import json
import subprocess
import signal
from pathlib import Path

wandb_on = all("wandb_on=true" not in a for a in sys.argv[1:])
args = list(sys.argv[1:])
if wandb_on and not any(a.startswith("wandb_on") for a in args):
    args.append("wandb_on=false")

env_id = "unknown"
seed = "0"
for a in args:
    if a.startswith("env_id="):
        env_id = a.split("=")[1]
    if a.startswith("seed="):
        seed = a.split("=")[1]

output_dir = Path(f"/tmp/klent_results/{env_id}_{seed}")
output_dir.mkdir(parents=True, exist_ok=True)
os.environ["KLENT_OUTPUT_DIR"] = str(output_dir)

print(json.dumps({"event": "start", "env_id": env_id, "seed": seed, "args": args}), flush=True)

python_cmd = "python3" if sys.platform != "win32" else "python"
proc = subprocess.Popen(
    [python_cmd, "main.py"] + args,
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT,
    text=True,
    bufsize=1,
)

def timeout_handler(_signum, _frame):
    proc.kill()
    print(json.dumps({"event": "timeout", "env_id": env_id, "seed": seed}), flush=True)
    sys.exit(1)

signal.signal(signal.SIGALRM, timeout_handler)

results = []
for line in proc.stdout:
    print(line, end="", flush=True)
    line = line.strip()
    if line.startswith("{"):
        try:
            data = json.loads(line)
            results.append(data)
        except json.JSONDecodeError:
            pass

proc.wait()
print(json.dumps({"event": "finish", "env_id": env_id, "seed": seed, "exit_code": proc.returncode}), flush=True)

results_file = output_dir / "metrics.jsonl"
with open(results_file, "w") as f:
    for r in results:
        f.write(json.dumps(r) + "\n")
print(json.dumps({"event": "saved_local", "path": str(results_file)}), flush=True)

try:
    from huggingface_hub import HfApi
    hf_token = os.environ.get("HF_TOKEN")
    api = HfApi(token=hf_token)
    repo_id = "Firemedic15/klent-repro-results"
    try:
        api.create_repo(repo_id, repo_type="dataset", exist_ok=True)
    except Exception:
        pass
    api.upload_folder(
        folder_path=str(output_dir),
        repo_id=repo_id,
        repo_type="dataset",
        path_in_repo=f"experiments/{env_id}_{seed}",
    )
    print(json.dumps({"event": "uploaded", "repo": repo_id, "path": f"experiments/{env_id}_{seed}"}), flush=True)
except Exception as e:
    print(json.dumps({"event": "upload_failed", "error": str(e)}), flush=True)