File size: 5,738 Bytes
b80a86d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fcfc3d2
 
b80a86d
fcfc3d2
 
b80a86d
 
fcfc3d2
 
 
 
b80a86d
 
 
55036db
b80a86d
 
 
 
 
 
 
55036db
 
 
 
 
 
 
b80a86d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""Start the unattended pipeline on a GPU machine, or show its status if it is already running.

From a notebook cell (or `python3 launch.py` in a shell):

    import os as _os, urllib.request as _u
    _os.environ["HF_TOKEN"] = "<Hugging Face write token>"      # needed once; kept in a private file
    exec(_u.urlopen("https://raw.githubusercontent.com/Mudassiruddin7/EMC/main/scripts/launch.py").read(),
         {"__name__": "__main__"})

Picks the writable folder with the most free space, stores the token (mode 600), fetches the
`uv` tool if needed and starts scripts/supervise.py in its own session, so it keeps running
after the cell returns. Re-running the cell only prints the status.
"""
import io
import os
import re
import shutil
import subprocess
import sys
import tarfile
import urllib.request
from pathlib import Path

SOURCE = "Mudassiruddin7/EMC"


def pick_root():
    if os.environ.get("EMC_ROOT"):
        return Path(os.environ["EMC_ROOT"])
    for c in (Path.cwd(), Path.home(), Path("/workspace"), Path("/data"), Path("/mnt/data"), Path("/mnt"), Path("/tmp")):
        if (c / "emc" / "supervisor.pid").exists():
            return c / "emc"
    best, free = Path.home(), -1
    for c in (Path.cwd(), Path.home(), Path("/workspace"), Path("/data"), Path("/mnt/data"), Path("/mnt"), Path("/tmp")):
        try:
            if c.is_dir() and os.access(c, os.W_OK) and shutil.disk_usage(c).free > free:
                best, free = c, shutil.disk_usage(c).free
        except OSError:
            pass
    return best / "emc"


def get_uv(root):
    found = shutil.which("uv")
    if found:
        return found
    exe = root / "tools" / "uv"
    if not exe.exists():
        exe.parent.mkdir(parents=True, exist_ok=True)
        url = "https://github.com/astral-sh/uv/releases/latest/download/uv-x86_64-unknown-linux-gnu.tar.gz"
        with tarfile.open(fileobj=io.BytesIO(urllib.request.urlopen(url, timeout=300).read())) as tf:
            for m in tf.getmembers():
                if m.isfile() and Path(m.name).name in ("uv", "uvx"):
                    p = exe.parent / Path(m.name).name
                    p.write_bytes(tf.extractfile(m).read())
                    p.chmod(0o755)
    return str(exe)


def alive(pidfile):
    """True only if the recorded process exists and is our supervisor (a pid file left over
    from an earlier machine can name an unrelated process)."""
    try:
        pid = int(pidfile.read_text())
        os.kill(pid, 0)
    except (OSError, ValueError):
        return False
    try:
        return b"supervise.py" in Path(f"/proc/{pid}/cmdline").read_bytes()
    except OSError:
        return False


def status(root):
    print(f"working folder : {root}  ({shutil.disk_usage(root).used / 1e9:.0f} GB used)")
    print(f"supervisor     : {'running' if alive(root / 'supervisor.pid') else 'not running'}")
    try:
        print("GPU            :", subprocess.run(["nvidia-smi", "--query-gpu=name,memory.used,memory.total,utilization.gpu",
                                                  "--format=csv,noheader"], capture_output=True, text=True,
                                                 timeout=20).stdout.strip())
    except Exception:  # noqa: BLE001
        pass
    stats = root / "repo" / "scripts" / "sysstats.py"
    if stats.exists():
        try:
            print("machine        :", subprocess.run([sys.executable, str(stats), str(root)], capture_output=True,
                                                     text=True, timeout=60).stdout.strip())
        except Exception:  # noqa: BLE001
            pass
    done = sorted(p.stem for p in (root / "state").glob("*.done"))
    failed = sorted(p.stem for p in (root / "state").glob("*.failed"))
    print("stages done    :", " ".join(done) or "-")
    if failed:
        print("stages failed  :", " ".join(failed))
    for name in ("pipeline.log", "supervisor.log"):
        p = root / "logs" / name
        if p.exists():
            print(f"--- last lines of {name}")
            print("\n".join(p.read_text(errors="replace").splitlines()[-12:]))


def main():
    root = pick_root()
    for sub in ("logs", "state", "tmp"):
        (root / sub).mkdir(parents=True, exist_ok=True)
    token = os.environ.get("HF_TOKEN", "").strip()
    if token.startswith("hf_"):
        p = root / ".hf_token"
        p.write_text(token)
        p.chmod(0o600)
    if os.environ.get("EMC_SCRUB"):
        (root / ".scrub").write_text(os.environ["EMC_SCRUB"])
    if not alive(root / "supervisor.pid"):
        uv = get_uv(root)
        req = urllib.request.Request(f"https://github.com/{SOURCE}.git/info/refs?service=git-upload-pack",
                                     headers={"User-Agent": "git/2.45.0"})
        sha = re.search(r"([0-9a-f]{40}) refs/heads/main", urllib.request.urlopen(req, timeout=30)
                        .read().decode("latin-1")).group(1)
        sup = root / "supervise.py"
        sup.write_bytes(urllib.request.urlopen(
            f"https://raw.githubusercontent.com/{SOURCE}/{sha}/scripts/supervise.py", timeout=60).read())
        env = {k: v for k, v in os.environ.items() if k != "HF_TOKEN"}
        env.update(EMC_ROOT=str(root), UV=uv, RUN_ID=os.environ.get("RUN_ID", "r1"))
        with open(root / "logs" / "supervisor.log", "a") as out:
            proc = subprocess.Popen([sys.executable, str(sup)], stdout=out, stderr=subprocess.STDOUT,
                                    stdin=subprocess.DEVNULL, env=env, cwd=str(root), start_new_session=True)
        (root / "supervisor.pid").write_text(str(proc.pid))
        print(f"started the supervisor (pid {proc.pid}); re-run this cell any time to see progress\n")
    status(root)


if __name__ == "__main__":
    main()