Cion-lab commited on
Commit
03388e8
·
verified ·
1 Parent(s): 0a619e6

add p6 launcher with a Hub heartbeat: a kernel that cannot report is a kernel you cannot debug (E-056)

Browse files
Files changed (1) hide show
  1. kernels/p6_evals_bootstrap.py +111 -0
kernels/p6_evals_bootstrap.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Phase 6 launcher with a heartbeat. Kaggle kernel body: 2xT4, billed 1x wall clock.
2
+ #
3
+ # Why this exists: run_benchmarks.py is silent until its first push (the smoke pass writes smoke.json only
4
+ # after all eight smoke runs), and on this account `kernels.get` is denied, so a kernel that is queued,
5
+ # booting, installing or dead all look identical from outside. A heartbeat in the report repo makes those
6
+ # states distinguishable and costs a few kilobytes per push.
7
+ #
8
+ # Before launching: book the QUOTA.md row, then set REV/PUB/SHA pins to the files actually published at that
9
+ # revision, and read the pins back after fetch -- a kernel that runs code it did not verify is not reproducible.
10
+ import hashlib, os, runpy, subprocess, sys, threading, time
11
+
12
+ REV = "55f23ef5d0e8e6a60adad23bf8a2ffffc6bc7580"
13
+ RB_SHA = "8196d853f3e3d8f1c729ff0d76517ddc7814b9d460a09497e1f289b03ca8da69"
14
+ CRED_SHA = "6525f62f03f2d73650a1eb4f70fcb52d1194caad4ca88b2d8bd8fd54f88339b6"
15
+ BASE = "https://huggingface.co/Cion-lab/ounce100m-code/resolve/" + REV
16
+ REPORT_REPO = "Cion-lab/ounce100m-p5-report" # private scratch; keeps the deliverable repo clean
17
+ LOG = "/kaggle/working/p6.log"
18
+ STAGE = {"name": "submit", "detail": ""}
19
+
20
+
21
+ def log(msg):
22
+ line = "%s %s\n" % (time.strftime("%H:%M:%S", time.gmtime()), msg)
23
+ with open(LOG, "a") as fh:
24
+ fh.write(line)
25
+ sys.__stdout__.write(line)
26
+ sys.__stdout__.flush()
27
+
28
+
29
+ def push_heartbeat(api, tok):
30
+ try:
31
+ body = open(LOG, "rb").read()[-60000:]
32
+ api.upload_file(path_or_fileobj=body, path_in_repo="p6_heartbeat.log",
33
+ repo_id=REPORT_REPO, repo_type="dataset",
34
+ commit_message="heartbeat %s" % STAGE["name"], token=tok)
35
+ except Exception as e: # a lost beat must never kill the eval
36
+ log("heartbeat push failed %r" % (e,))
37
+
38
+
39
+ def beat(api, tok):
40
+ while True:
41
+ time.sleep(120)
42
+ push_heartbeat(api, tok)
43
+
44
+
45
+ os.chdir("/kaggle/working")
46
+ os.makedirs("/kaggle/working/lib/eval", exist_ok=True)
47
+ for rel, want in (("eval/run_benchmarks.py", RB_SHA), ("ounce100m_credentials.py", CRED_SHA)):
48
+ dest = "/kaggle/working/lib/" + rel
49
+ assert subprocess.run(["curl", "-sfL", BASE + "/" + rel, "-o", dest]).returncode == 0, rel
50
+ got = hashlib.sha256(open(dest, "rb").read()).hexdigest()
51
+ assert got == want, "%s: %s != %s" % (rel, got[:16], want[:16])
52
+ log("PINNED %s %s" % (rel, got[:16]))
53
+
54
+ sys.path.insert(0, "/kaggle/working/lib")
55
+ import ounce100m_credentials
56
+
57
+ STAGE["name"] = "creds"
58
+ log("creds " + str(ounce100m_credentials.install(verify=True)))
59
+ tok = os.environ["HF_TOKEN"]
60
+
61
+ from huggingface_hub import HfApi
62
+
63
+ api = HfApi(token=tok)
64
+ threading.Thread(target=beat, args=(api, tok), daemon=True).start()
65
+ push_heartbeat(api, tok) # first beat immediately: the kernel is alive
66
+
67
+ import site
68
+
69
+ up = site.getusersitepackages() # E-047: ~/.local is not on sys.path when created
70
+ if up and up not in sys.path:
71
+ sys.path.append(up) # mid-process, so add it explicitly
72
+ log("user_site " + str(up))
73
+
74
+ # tee: the runner streams its own progress, and that stream is the only status a denied-kernels.get account
75
+ # can still see once it is being shipped somewhere.
76
+ real = sys.stdout
77
+
78
+
79
+ class Tee:
80
+ def write(self, s):
81
+ real.write(s)
82
+ real.flush()
83
+ with open(LOG, "a") as fh:
84
+ fh.write(s)
85
+ return len(s)
86
+
87
+ def flush(self):
88
+ real.flush()
89
+
90
+ def isatty(self): # transformers' progress bars ask
91
+ return False
92
+
93
+
94
+ STAGE["name"] = "run_benchmarks"
95
+ sys.argv = ["run_benchmarks.py", "--model", "Cion-lab/ounce100m-v1",
96
+ "--repo", "Cion-lab/ounce100m-v1", "--full-gpu-hours", "0.3"]
97
+ log("ARGV " + " ".join(sys.argv))
98
+ sys.stdout = Tee()
99
+ err = None
100
+ try:
101
+ runpy.run_path("/kaggle/working/lib/eval/run_benchmarks.py", run_name="__main__")
102
+ except SystemExit as e:
103
+ err = None if e.code in (0, None) else "SystemExit(%r)" % (e.code,)
104
+ except BaseException as e:
105
+ err = repr(e)
106
+ finally:
107
+ sys.stdout = real
108
+ STAGE["name"] = "done"
109
+ log("P6_DONE err=%s" % (err,))
110
+ push_heartbeat(api, tok)
111
+ print("VERDICT P6 err=%s" % (bool(err),), flush=True)