| |
| 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) |
|
|