#!/usr/bin/env python3 """Persistent supervisor for the four-GPU UnityShotsBench run.""" import json import os import signal import subprocess import time from pathlib import Path ROOT = Path("/datadrive1/v-zihanyang") EVAL = ROOT / "evals/unityshotsbench" STATE = EVAL / "run_state" LOGS = EVAL / "logs" H3_OUT = EVAL / "h3_method" STATUS = STATE / "monitor_status.json" def atomic(path: Path, value): tmp = path.with_name(path.name + f".tmp.{os.getpid()}") tmp.write_text(json.dumps(value, ensure_ascii=False, indent=2) + "\n") os.replace(tmp, path) def alive(pid): try: os.kill(pid, 0) stat = Path(f"/proc/{pid}/stat") if stat.is_file() and stat.read_text().split()[2] == "Z": return False return True except (ProcessLookupError, PermissionError): return False def pid_from(name): path = STATE / f"{name}.pid" try: return int(path.read_text().strip()) except Exception: return None def launch(name, command, cwd, log, cuda): handle = open(log, "a", buffering=1) env = dict(os.environ, CUDA_VISIBLE_DEVICES=cuda, PYTHONUNBUFFERED="1") process = subprocess.Popen(command, cwd=cwd, env=env, stdout=handle, stderr=subprocess.STDOUT, start_new_session=True) (STATE / f"{name}.pid").write_text(str(process.pid) + "\n") return process.pid def h3_command(preflight): cmd = [str(ROOT / "MiniMax-H3/.conda/envs/minimax-h3-zeroshot/bin/python"), "-u", "zeroshot/unityshotsbench_runner.py"] return cmd + (["--preflight"] if preflight else []) def launch_h3(preflight): return launch("h3_preflight" if preflight else "h3_full", h3_command(preflight), ROOT / "MiniMax-H3", LOGS / "h3_method.log", "0,1") def launch_joy(gpu): shard = "gpu2" if gpu == 2 else "gpu3" return launch(f"joyai_{shard}", [str(Path("/home/qid/.conda/envs/echo-long/bin/python")), "-u", "inference.py", "--config", "configs/inference.yaml", "--prompts-dir", str(EVAL / f"joyai_shards/{shard}"), "--prompts-glob", "story_*.json", "--output-root", str(EVAL / f"joyai_echo/{shard}")], ROOT / "JoyAI-Echo", LOGS / f"joyai_{shard}.log", str(gpu)) def valid_h3_shot(story, idx): base = H3_OUT / story return all((base / name).is_file() and (base / name).stat().st_size > 0 for name in ( f"shot_{idx}.mp4", f"shot_{idx}.wav", f"shot_{idx}.metadata.json")) def preflight_valid(): if not all(valid_h3_shot("story_001", i) for i in range(3)): return False if not all(valid_h3_shot("story_091", i) for i in range(5)): return False for story in ("story_001", "story_091"): registry_path = H3_OUT / story / "voice_reference_registry.json" if not registry_path.is_file(): return False reg = json.loads((H3_OUT / "story_091/voice_reference_registry.json").read_text()) if not all(Path(reg[c]["ref_audio_path"]).is_file() for c in ("char1", "char2")): return False if not (reg["char1"]["ref_audio_path"] != reg["char2"]["ref_audio_path"] and ( reg["char1"]["clip_start"], reg["char1"]["clip_end"]) != ( reg["char2"]["clip_start"], reg["char2"]["clip_end"])): return False shot4 = json.loads((H3_OUT / "story_091/shot_4.metadata.json").read_text()) return shot4.get("reference_input_order") == [ {"kind": "picture", "label": ""}, {"kind": "picture", "label": ""}, {"kind": "audio", "label": "