File size: 2,037 Bytes
0cc835c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""watch.py name=job_id ...  — print one line per new epoch score, ONNX score, push, error or terminal stage."""
import os
import re
import sys
import json
import time
import subprocess

env = os.environ | {"HF_HUB_DISABLE_EXPERIMENTAL_WARNING": "1"}
jobs = dict(arg.split("=", 1) for arg in sys.argv[1:])
seen: set[tuple[str, str]] = set()


def hf(*args: str) -> str:
    return subprocess.run(["hf", "jobs", *args], capture_output=True, text=True, env=env).stdout


def events(logs: str) -> list[str]:
    out, stage = [], "teacher"
    for line in logs.splitlines():
        if line.startswith("$ ") and "train.py" in line and "runs/student" in line:
            stage = "student"
        try:
            if line.startswith("epoch ") and "{" in line:
                d = json.loads(line[line.index("{"):])
                tag = line[: line.index("{")].strip()
                out.append(f"{stage} {tag} " + " ".join(f"{k}={d[k]['best']['acc']:.3f}" for k in ("val", "test", "test_real", "hard") if k in d))
            elif line.startswith('{"model'):
                d = json.loads(line)
                out.append("onnx " + " ".join(f"{k}={d[k]['@val']['acc']:.3f}/sev{d[k]['@val']['severe']}" for k in ("test", "test_real", "hard")))
        except (ValueError, KeyError):
            pass
        if line.startswith("pushed") or "Traceback" in line or "Error" in line or "out of memory" in line:
            out.append(line[:200])
    return out


while True:
    active = False
    for name, job in jobs.items():
        stage = (re.search(r"'stage': '([A-Z_]+)'", hf("inspect", job)) or [None, "?"])[1]
        for event in events(hf("logs", job)) + ([f"job {stage}"] if stage in ("COMPLETED", "ERROR", "CANCELED", "DELETED", "TIMEOUT") else []):
            if (name, event) not in seen:
                seen.add((name, event))
                print(f"{name}: {event}", flush=True)
        active |= stage not in ("COMPLETED", "ERROR", "CANCELED", "DELETED", "TIMEOUT")
    if not active:
        break
    time.sleep(120)