-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodal_run.py
More file actions
74 lines (60 loc) · 1.99 KB
/
Copy pathmodal_run.py
File metadata and controls
74 lines (60 loc) · 1.99 KB
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
"""Run an arbitrary Python module or script on Modal.
Usage (via Makefile):
make modal-run MODULE=scripts.investigate_phase_e
make modal-run MODULE=scripts.foo ARGS="--flag x"
make modal-run SCRIPT=path/to/one_shot.py ARGS="..."
make modal-run MODULE=scripts.cpu_only_thing CPU_ONLY=1
Direct:
uv run modal run modal_run.py --module scripts.investigate_phase_e
"""
import shlex
import subprocess
import sys
import time
import modal
from modal_image import IMAGE
app = modal.App("torchwright-run", image=IMAGE)
def _build_cmd(module: str, script: str, args: str) -> list[str]:
cmd = [sys.executable]
if module:
cmd += ["-m", module]
else:
cmd.append(script)
if args:
cmd += shlex.split(args)
return cmd
@app.function(gpu="a100-80gb", cpu=8, memory=32768, timeout=1800)
def run_gpu(module: str, script: str, args: str) -> int:
cmd = _build_cmd(module, script, args)
print(f"[remote/gpu] {' '.join(cmd)}")
t0 = time.time()
rc = subprocess.run(cmd, check=False).returncode
print(f"[remote/gpu] exit {rc} in {time.time() - t0:.0f}s")
return rc
@app.function(cpu=4, memory=8192, timeout=1800)
def run_cpu(module: str, script: str, args: str) -> int:
cmd = _build_cmd(module, script, args)
print(f"[remote/cpu] {' '.join(cmd)}")
t0 = time.time()
rc = subprocess.run(cmd, check=False).returncode
print(f"[remote/cpu] exit {rc} in {time.time() - t0:.0f}s")
return rc
@app.local_entrypoint()
def main(
module: str = "",
script: str = "",
args: str = "",
*,
cpu_only: bool = False,
) -> None:
if not module and not script:
print(
"error: pass --module <dotted.name> or --script <path>",
file=sys.stderr,
)
sys.exit(2)
if module and script:
print("error: pass --module OR --script, not both", file=sys.stderr)
sys.exit(2)
fn = run_cpu if cpu_only else run_gpu
sys.exit(fn.remote(module=module, script=script, args=args))