Upload code_and_results/run_all.py with huggingface_hub
Browse files- code_and_results/run_all.py +61 -0
code_and_results/run_all.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Runs one queue of jobs per GPU in parallel; each job is train_one.py in its own process.
|
| 2 |
+
import json, os, subprocess, sys, threading, time
|
| 3 |
+
|
| 4 |
+
PLAN = json.load(open(sys.argv[1])) # {"gpu0": [job, ...], "gpu1": [...]}
|
| 5 |
+
RES, LOGS = "/kaggle/working/results", "/kaggle/working/logs"
|
| 6 |
+
os.makedirs(RES, exist_ok = True); os.makedirs(LOGS, exist_ok = True)
|
| 7 |
+
t0 = time.time()
|
| 8 |
+
status = {}
|
| 9 |
+
|
| 10 |
+
def done(tag):
|
| 11 |
+
try:
|
| 12 |
+
return json.load(open(f"{RES}/{tag}.json")).get("status") == "done"
|
| 13 |
+
except Exception:
|
| 14 |
+
return False
|
| 15 |
+
|
| 16 |
+
def worker(gpu, jobs):
|
| 17 |
+
for job in jobs:
|
| 18 |
+
tag = job["tag"]
|
| 19 |
+
if done(tag):
|
| 20 |
+
status[tag] = "skip (done)"; continue
|
| 21 |
+
dl = float(os.environ.get("DEADLINE_TS", "0") or 0)
|
| 22 |
+
if dl and time.time() > dl - float(job.get("min_time_s", 5400)):
|
| 23 |
+
status[tag] = "skipped: not enough time left before the deadline"; continue
|
| 24 |
+
job = {k: v for k, v in job.items() if k != "min_time_s"}
|
| 25 |
+
cmd = [sys.executable, "train_one.py"]
|
| 26 |
+
for k, v in job.items():
|
| 27 |
+
cmd += [f"--{k.replace('_', '-')}", str(v)]
|
| 28 |
+
env = dict(os.environ, CUDA_VISIBLE_DEVICES = str(gpu), PYTHONUNBUFFERED = "1")
|
| 29 |
+
status[tag] = f"running on GPU{gpu}"
|
| 30 |
+
with open(f"{LOGS}/{tag}.log", "w") as f:
|
| 31 |
+
rc = subprocess.call(cmd, stdout = f, stderr = subprocess.STDOUT, env = env, cwd = "/kaggle/working")
|
| 32 |
+
status[tag] = "done" if done(tag) else f"FAILED rc={rc}"
|
| 33 |
+
|
| 34 |
+
threads = [threading.Thread(target = worker, args = (i, jobs)) for i, jobs in enumerate(PLAN.values())]
|
| 35 |
+
for t in threads: t.start()
|
| 36 |
+
|
| 37 |
+
def last_line(tag):
|
| 38 |
+
try:
|
| 39 |
+
lines = [l for l in open(f"{LOGS}/{tag}.log", errors = "ignore").read().replace("\r", "\n").split("\n") if l.strip()]
|
| 40 |
+
return lines[-1][-220:] if lines else ""
|
| 41 |
+
except Exception:
|
| 42 |
+
return ""
|
| 43 |
+
|
| 44 |
+
while any(t.is_alive() for t in threads):
|
| 45 |
+
time.sleep(120)
|
| 46 |
+
print(f"--- {(time.time() - t0) / 60:.0f} min", flush = True)
|
| 47 |
+
for tag, s in status.items():
|
| 48 |
+
print(f" {tag}: {s} | {last_line(tag) if s.startswith('running') else ''}", flush = True)
|
| 49 |
+
for t in threads: t.join()
|
| 50 |
+
print("ALL DONE", json.dumps(status, indent = 1), flush = True)
|
| 51 |
+
for tag in status:
|
| 52 |
+
try:
|
| 53 |
+
r = json.load(open(f"{RES}/{tag}.json"))
|
| 54 |
+
print(tag, r.get("status"), r.get("error", ""), r.get("pooled_core_acc"), json.dumps(r.get("after")), flush = True)
|
| 55 |
+
for l in open(f"{LOGS}/{tag}.log", errors = "ignore").read().replace("\r", "\n").split("\n"):
|
| 56 |
+
if l.startswith(f"[{tag} ") or "Unsloth:" in l:
|
| 57 |
+
print(" ", l[:400], flush = True)
|
| 58 |
+
if r.get("status") != "done":
|
| 59 |
+
print(open(f"{LOGS}/{tag}.log", errors = "ignore").read()[-6000:], flush = True)
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(tag, "no result", e)
|