File size: 5,774 Bytes
f27ab9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""Launch the bounded storage/recall controls on the released LM GPU 0/1."""

from __future__ import annotations

import argparse
import fcntl
import hashlib
import shutil
import subprocess
import sys
import time
import traceback
from pathlib import Path

import execute_parametric_architecture as scheduler
from execute_capacity_scaling import devices_free, predecessor_status

read, write, digest = scheduler.read, scheduler.write, scheduler.digest


def freeze(path):
    config = read(path)
    repo = Path.cwd().resolve()
    artifact = repo / "docs/development-artifacts" / config["batch"]
    source = artifact / "source"
    if source.exists():
        raise FileExistsError(source)
    assert config["gpus"] == [0, 1]
    assert config["runtime"]["docker_context"] == "lm-memory"
    config.update(
        repository=str(repo),
        source_root=str(source),
        source_files={},
        execution_lock=str(artifact / "execution-lock.json"),
        frozen_utc=scheduler.now(),
        git_commit=subprocess.check_output(["git", "rev-parse", "HEAD"], text=True).strip(),
    )
    for spec in config["runs"]:
        if spec.get("parent"):
            parent = Path(spec["parent"])
            assert read(parent / "audit.json")["passed"]
            spec["parent_checkpoint_sha256"] = digest(parent / "latest.pt")
    files = sorted(
        set(
            list(Path("src/llm_memory_editability").glob("*.py"))
            + [path.relative_to(repo), Path("configs/experiment-tracking-defaults.json")]
            + [Path(p) for p in config["additional_sources"]]
        )
    )
    for filename in files:
        target = source / filename
        target.parent.mkdir(parents=True, exist_ok=True)
        shutil.copy2(filename, target)
        config["source_files"][str(target)] = digest(target)
    frozen = artifact / "frozen-config.json"
    write(frozen, config)
    write(config["execution_lock"], {"config_sha256": digest(frozen), "utc": scheduler.now()})
    print(frozen)


def command_builder(config, config_path, spec, out, gpu, attempt):
    _, command = scheduler.container_command(config, config_path, spec, out, gpu, attempt)
    old = str(Path(config["source_root"]) / "scripts/execute_parametric_architecture.py")
    command[command.index(old)] = str(
        Path(config["source_root"]) / "scripts/execute_capacity_controls.py"
    )
    name = (
        "lm-capctrl-"
        + hashlib.sha256(f"{config['batch']}:{spec['name']}:{attempt}".encode()).hexdigest()[:20]
    )
    command[command.index("--name") + 1] = name
    return name, command


def worker(path, name):
    config = read(path)
    spec = next(s for s in config["runs"] if s["name"] == name)
    filename = (
        "evaluate_capacity_recall.py" if spec.get("evaluation_only") else "run_capacity_controls.py"
    )
    script = Path(config["source_root"]) / "scripts" / filename
    out = Path(config["results_root"]) / "runs" / name
    try:
        for action, timeout in (("train", spec["max_wall_seconds"]), ("audit", 1200)):
            subprocess.run(
                [sys.executable, "-u", str(script), action, "--config", str(path), "--run", name],
                check=True,
                timeout=timeout,
            )
    except BaseException:
        write(out / "failure.json", {"error": traceback.format_exc(), "utc": scheduler.now()})
        raise


def controller(path):
    config = read(path)
    root = Path(config["results_root"])
    root.mkdir(parents=True, exist_ok=True)
    with (root / "controller.lock").open("a") as lock:
        fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
        while config.get("predecessors"):
            ready, evidence = predecessor_status(config["predecessors"])
            if ready:
                break
            write(
                root / "controller-state.json",
                {
                    "state": "waiting_for_predecessor",
                    "evidence": evidence,
                    "updated_utc": scheduler.now(),
                },
            )
            time.sleep(20)
        while not devices_free(config["gpus"]):
            write(root / "controller-state.json", {"state": "waiting_for_gpu_0_1"})
            time.sleep(20)
        scheduler.control_locked(config, path, root, command_builder=command_builder)
    subprocess.run(
        [
            sys.executable,
            str(Path(config["source_root"]) / "scripts/report_capacity_controls.py"),
            "--root",
            str(root),
        ],
        check=True,
    )


def launch(path):
    config = read(path)
    root = Path(config["results_root"])
    root.mkdir(parents=True, exist_ok=True)
    script = Path(config["source_root"]) / "scripts/execute_capacity_controls.py"
    cmd = [sys.executable, "-u", str(script), "controller", "--config", str(path)]
    with (root / "controller.log").open("a") as log:
        proc = subprocess.Popen(
            cmd,
            env=scheduler.host_env(config["source_root"]),
            stdout=log,
            stderr=subprocess.STDOUT,
            start_new_session=True,
        )
    write(
        root / "controller-process.json", {"pid": proc.pid, "command": cmd, "utc": scheduler.now()}
    )
    print(proc.pid)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("action", choices=["freeze", "launch", "controller", "container-worker"])
    parser.add_argument("--config", type=Path, required=True)
    parser.add_argument("--run")
    args = parser.parse_args()
    if args.action == "container-worker":
        worker(args.config, args.run)
    else:
        {"freeze": freeze, "launch": launch, "controller": controller}[args.action](
            args.config.resolve()
        )