omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / tests / device-window / compare.py
| 1 | #!/usr/bin/env python3 |
| 2 | """compare.py: tools/run-device-window.sh against its Omega successor, on CPU only. |
| 3 | |
| 4 | usage: python3 tests/device-window/compare.py OLD_SH NEW_BIN SCRATCH |
| 5 | |
| 6 | SCRATCH must not exist. Two identical fake trees are built under it, one per program |
| 7 | (SCRATCH/sh and SCRATCH/om), with a fake `spark-window` first on PATH that runs each row |
| 8 | on the CPU (`open` prints a window id, `run` runs the row, `close` logs the id), fake |
| 9 | cubin_probe / conformance/run.sh / torch test scripts, and a CUDApp git repository with a |
| 10 | fixed commit. No GPU and no real spark-window is touched: PATH is set explicitly and does |
| 11 | not contain ~/.local/bin. |
| 12 | |
| 13 | Every case runs both programs with the same arguments and environment and compares exit |
| 14 | status, stdout and stderr (the tree root is replaced by <T>, the program's own name is |
| 15 | normalised, and dash's "NAME: LINE: cannot open X: No such file" becomes the successor's |
| 16 | "run-device-window: cannot open X"), and in device mode the whole evidence directory: rc.txt, every row log, |
| 17 | window.txt (start_utc, end_utc and script_sha256 are checked for form and for the hash of |
| 18 | the program that ran, then masked), the NN-*.out/ copies, NN-*.out.sha256 and cubins/, and |
| 19 | the fake spark-window's close log. A case listed in RC_ONLY compares exit status and |
| 20 | evidence only, because the shell's message there is a shell error text (named per case). |
| 21 | |
| 22 | The fake tree root is a git repository whose tracked README is modified (bridge_dirty 1; |
| 23 | the untracked program and evidence are not counted), and the second conformance row writes |
| 24 | a device result file over 64 KiB. |
| 25 | |
| 26 | Signal cases: device-signal-closes kills the whole process group mid-row (both close the |
| 27 | window). device-parent-term and device-parent-kill kill only the driver while a row sleeps |
| 28 | on the fake chip; the fake probe logs row-start and row-exit. On SIGTERM both programs close |
| 29 | the window only after the row exited. On kill -9 the shell script never closes (its lease |
| 30 | runs out) and the successor closes after the row exited; that case checks those two |
| 31 | expected outcomes rather than equality, and never accepts a close before row-exit. |
| 32 | |
| 33 | om-refuse-wid-* run the successor only: `spark-window open` prints an id that is not |
| 34 | 1..4095 bytes of [A-Za-z0-9._-] (or prints over 64 KiB); the successor must exit 3, say so, |
| 35 | run no row and close nothing. The shell script took any output as the id. |
| 36 | |
| 37 | device-kill-in-open kills only the driver (kill -9) while `spark-window open` is still running |
| 38 | (FAKE_OPEN_SLEEP): the shell script never closes that window; the successor's closer gets the |
| 39 | id from the open itself and closes the window once the open has exited, and no row runs. |
| 40 | |
| 41 | om-refuse-env-* run the successor only: an environment entry over 65535 bytes, or an |
| 42 | environment too large for the execprog packet, is refused with exit 3 before anything is |
| 43 | printed, opened or created. The shell script has no such limit. |
| 44 | |
| 45 | Prints one line per case and "COMPARE PASS cases=N" (exit 0) or "COMPARE FAIL ..." (exit 1). |
| 46 | """ |
| 47 | import hashlib |
| 48 | import os |
| 49 | import re |
| 50 | import shutil |
| 51 | import signal |
| 52 | import subprocess |
| 53 | import sys |
| 54 | import time |
| 55 | |
| 56 | FAKE_SW = r"""#!/bin/sh |
| 57 | # fake spark-window for compare.py: CPU only |
| 58 | cmd=$1; shift |
| 59 | case $cmd in |
| 60 | open) |
| 61 | echo "open $*" >> "$FAKE_LOG" |
| 62 | [ "${FAKE_OPEN_FAIL:-0}" = 1 ] && { echo "fake open refused" >&2; exit 4; } |
| 63 | [ -n "${FAKE_OPEN_SLEEP:-}" ] && sleep "$FAKE_OPEN_SLEEP" |
| 64 | [ -n "${FAKE_WID_BIG:-}" ] && { head -c 70000 /dev/zero | tr '\0' w; echo; exit 0; } |
| 65 | printf '%s\n\n' "${FAKE_WID-FAKEWID-$3}" |
| 66 | ;; |
| 67 | run) exec "$@" ;; |
| 68 | close) echo "close $*" >> "$FAKE_LOG"; echo "closed $1" ;; |
| 69 | *) exit 9 ;; |
| 70 | esac |
| 71 | """ |
| 72 | FAKE_PROBE = r"""#!/bin/sh |
| 73 | echo "probe $*" |
| 74 | echo "probe-stderr" >&2 |
| 75 | case $1 in --load-only) exit "${FAKE_LOAD_RC:-0}" ;; esac |
| 76 | if [ -n "${FAKE_SLEEP:-}" ]; then |
| 77 | echo "row-start" >> "$FAKE_LOG" |
| 78 | sleep "$FAKE_SLEEP" |
| 79 | echo "row-exit" >> "$FAKE_LOG" |
| 80 | fi |
| 81 | exit 0 |
| 82 | """ |
| 83 | FAKE_RUN = r"""#!/bin/sh |
| 84 | d=$(dirname "$0")/build |
| 85 | mkdir -p "$d" |
| 86 | n=$(cat "$d/.n" 2>/dev/null || echo 0); n=$((n + 1)); echo "$n" > "$d/.n" |
| 87 | echo "row $n $*" > "$d/omega-gpu-row$n.json" |
| 88 | printf 'bin%s' "$n" > "$d/max-gpu-row$n.bin" |
| 89 | echo hidden > "$d/.omega-hidden" |
| 90 | [ "$n" = 2 ] && head -c 70000 /dev/zero | tr '\0' x > "$d/omega-gpu-row$n.json" |
| 91 | # lengths 55..61 (mod 64): the SHA-256 padding cases where the length field spills into a new block |
| 92 | head -c $((54 + n)) /dev/zero | tr '\0' p > "$d/omega-pad-row$n.json" |
| 93 | echo "conformance row $n" |
| 94 | """ |
| 95 | FAKE_TORCH = "#!/bin/sh\necho \"torch test $*\"\nexit 0\n" |
| 96 | PY_TORCH = "#!/bin/sh\n# fake interpreter: torch importable\nexit 0\n" |
| 97 | PY_NOTORCH = "#!/bin/sh\nexit 1\n" |
| 98 | |
| 99 | RC_ONLY = { |
| 100 | # the shell fails `pairs=$2` under set -u with a shell error; the successor refuses in words |
| 101 | "pairs-no-value", |
| 102 | # the shell prints its own set -u error for $HOME; the successor names HOME in its message |
| 103 | "dry-home-unset", |
| 104 | } |
| 105 | |
| 106 | |
| 107 | def put(path, text, mode=0o644): |
| 108 | os.makedirs(os.path.dirname(path), exist_ok=True) |
| 109 | with open(path, "w") as f: |
| 110 | f.write(text) |
| 111 | os.chmod(path, mode) |
| 112 | |
| 113 | |
| 114 | def build_tree(root, prog, is_sh): |
| 115 | os.makedirs(root) |
| 116 | tools = os.path.join(root, "tools") |
| 117 | os.makedirs(tools) |
| 118 | dst = os.path.join(tools, "run-device-window.sh" if is_sh else "run-device-window") |
| 119 | shutil.copyfile(prog, dst) |
| 120 | os.chmod(dst, 0o755) |
| 121 | put(os.path.join(root, "bin/spark-window"), FAKE_SW, 0o755) |
| 122 | put(os.path.join(root, "bin/python-torch"), PY_TORCH, 0o755) |
| 123 | put(os.path.join(root, "bin/python-notorch"), PY_NOTORCH, 0o755) |
| 124 | put(os.path.join(root, "conformance/run.sh"), FAKE_RUN, 0o755) |
| 125 | put(os.path.join(root, "conformance/max_runner.py"), "# fake\n") |
| 126 | put(os.path.join(root, "attach/torch/test_cudapp_torch.py"), FAKE_TORCH, 0o755) |
| 127 | put(os.path.join(root, "attach/torch/libcudapp_attach.so"), "fake torch lib\n") |
| 128 | put(os.path.join(root, "home/workspace/maxinterop/venv/bin/python"), PY_TORCH, 0o755) |
| 129 | c = os.path.join(root, "cudapp") |
| 130 | put(os.path.join(c, "build/cubin_probe"), FAKE_PROBE, 0o755) |
| 131 | for stem in ("matmul-16x16x8", "attention-hd128-gqa8"): |
| 132 | for ext, body in ((".cubin", stem + " cubin\n"), (".json", '{"k":"%s"}\n' % stem)): |
| 133 | put(os.path.join(c, "build/window-cubins", stem + ext), body) |
| 134 | put(os.path.join(c, "build/window-cubins/unrelated.cubin"), "not copied\n") |
| 135 | put(os.path.join(c, "evidence/MAX-INTEROP/68/matmul-16x16x8.cubin"), "stale\n") |
| 136 | put(os.path.join(c, "build/libomega_gpu.a"), "!<arch>\n") |
| 137 | genv = dict(os.environ, GIT_AUTHOR_NAME="c", GIT_AUTHOR_EMAIL="c@c", GIT_COMMITTER_NAME="c", |
| 138 | GIT_COMMITTER_EMAIL="c@c", GIT_AUTHOR_DATE="2026-01-01T00:00:00Z", |
| 139 | GIT_COMMITTER_DATE="2026-01-01T00:00:00Z") |
| 140 | put(os.path.join(root, "README"), "fixed\n") |
| 141 | put(os.path.join(c, "README"), "fixed\n") |
| 142 | for args in (["init", "-q", "-b", "main"], ["add", "README"], ["commit", "-q", "-m", "fixed"]): |
| 143 | subprocess.run(["git", "-C", c] + args, check=True, env=genv) |
| 144 | for args in (["init", "-q", "-b", "main"], ["add", "README"], ["commit", "-q", "-m", "fixed"]): |
| 145 | subprocess.run(["git", "-C", root] + args, check=True, env=genv) |
| 146 | put(os.path.join(root, "README"), "fixed\nmodified\n") |
| 147 | return dst |
| 148 | |
| 149 | |
| 150 | def base_env(root, extra): |
| 151 | env = { |
| 152 | "PATH": os.path.join(root, "bin") + ":/usr/bin:/bin", |
| 153 | "HOME": os.path.join(root, "home"), |
| 154 | "CUDAPP": os.path.join(root, "cudapp"), |
| 155 | "FAKE_LOG": os.path.join(root, "fake-spark-window.log"), |
| 156 | "LC_ALL": "C", |
| 157 | } |
| 158 | for k, v in extra.items(): |
| 159 | if v is None: |
| 160 | env.pop(k, None) |
| 161 | else: |
| 162 | env[k] = v.replace("@T", root) |
| 163 | return env |
| 164 | |
| 165 | |
| 166 | def norm(text, root, is_sh): |
| 167 | text = text.replace(root, "<T>") |
| 168 | if is_sh: |
| 169 | text = text.replace("run-device-window.sh", "run-device-window") |
| 170 | # a failed `< file` redirect in sha_of: dash names the script and its line; the |
| 171 | # successor writes "run-device-window: cannot open FILE" (same file, same order) |
| 172 | text = re.sub(r"(?m)^\S+: \d+: cannot open (.*): No such file$", |
| 173 | r"run-device-window: cannot open \1", text) |
| 174 | return text |
| 175 | |
| 176 | |
| 177 | def sha_file(path): |
| 178 | h = hashlib.sha256() |
| 179 | with open(path, "rb") as f: |
| 180 | h.update(f.read()) |
| 181 | return h.hexdigest() |
| 182 | |
| 183 | |
| 184 | def snapshot(root, ledger, prog): |
| 185 | """The evidence directory as {relative path: normalised content}.""" |
| 186 | snap = {} |
| 187 | out = os.path.join(root, "evidence/device-window", ledger) |
| 188 | if os.path.isdir(out): |
| 189 | for dirpath, dirnames, filenames in os.walk(out): |
| 190 | dirnames.sort() |
| 191 | rel = os.path.relpath(dirpath, out) |
| 192 | snap["dir:" + rel] = "" |
| 193 | for fn in sorted(filenames): |
| 194 | p = os.path.join(dirpath, fn) |
| 195 | body = open(p, "rb").read().decode("utf-8", "replace") |
| 196 | if fn == "window.txt": |
| 197 | body = mask_window(body, prog) |
| 198 | mode = oct(os.stat(p).st_mode & 0o777) |
| 199 | snap[os.path.join(rel, fn)] = mode + "\n" + body.replace(root, "<T>") |
| 200 | log = os.path.join(root, "fake-spark-window.log") |
| 201 | snap["fake-log"] = open(log).read().replace(root, "<T>") if os.path.exists(log) else "" |
| 202 | return snap |
| 203 | |
| 204 | |
| 205 | def mask_window(body, prog): |
| 206 | lines = [] |
| 207 | for line in body.split("\n"): |
| 208 | key, _, val = line.partition(" ") |
| 209 | if key in ("start_utc", "end_utc"): |
| 210 | if not re.fullmatch(r"\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ", val): |
| 211 | lines.append("BAD-TIME " + line) |
| 212 | continue |
| 213 | now = time.strftime("%Y-%m-%dT%H", time.gmtime()) |
| 214 | ok = abs(time.mktime(time.strptime(val, "%Y-%m-%dT%H:%M:%SZ")) - |
| 215 | time.mktime(time.strptime(now, "%Y-%m-%dT%H"))) < 7300 |
| 216 | lines.append(key + (" <time>" if ok else " BAD-CLOCK " + val)) |
| 217 | elif key == "script_sha256": |
| 218 | lines.append(key + (" <prog>" if val == sha_file(prog) else " BAD-HASH " + val)) |
| 219 | else: |
| 220 | lines.append(line) |
| 221 | return "\n".join(lines) |
| 222 | |
| 223 | |
| 224 | def run_case(name, args, extra, roots, progs, mode, pre=None): |
| 225 | results = [] |
| 226 | for is_sh in (True, False): |
| 227 | root = roots[is_sh] |
| 228 | prog = progs[is_sh] |
| 229 | argv = (["/bin/sh", prog] if is_sh else [prog]) + args |
| 230 | env = base_env(root, extra) |
| 231 | if pre: |
| 232 | pre(root) |
| 233 | if mode == "signal": |
| 234 | p = subprocess.Popen(argv, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 235 | start_new_session=True) |
| 236 | log = os.path.join(root, "fake-spark-window.log") |
| 237 | deadline = time.time() + 20 |
| 238 | while time.time() < deadline: |
| 239 | rc1 = os.path.join(root, "evidence/device-window", args[1], "rc.txt") |
| 240 | if os.path.exists(rc1) and "01-cubin-matmul" in open(rc1).read(): |
| 241 | break |
| 242 | time.sleep(0.05) |
| 243 | time.sleep(0.3) |
| 244 | os.killpg(p.pid, signal.SIGTERM) |
| 245 | so, se = p.communicate(timeout=30) |
| 246 | time.sleep(1.0) |
| 247 | rc = "signalled" if p.returncode in (130, -15) else "rc=%d" % p.returncode |
| 248 | closed = open(log).read() if os.path.exists(log) else "" |
| 249 | results.append((rc, "close FAKEWID-" + args[1] in closed, "")) |
| 250 | continue |
| 251 | if mode in ("parent-term", "parent-kill"): |
| 252 | p = subprocess.Popen(argv, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 253 | log = os.path.join(root, "fake-spark-window.log") |
| 254 | |
| 255 | def logtext(): |
| 256 | return open(log).read() if os.path.exists(log) else "" |
| 257 | |
| 258 | deadline = time.time() + 30 |
| 259 | while time.time() < deadline and "row-start" not in logtext(): |
| 260 | time.sleep(0.05) |
| 261 | os.kill(p.pid, signal.SIGTERM if mode == "parent-term" else signal.SIGKILL) |
| 262 | p.communicate(timeout=60) |
| 263 | deadline = time.time() + 30 |
| 264 | while time.time() < deadline and "row-exit" not in logtext(): |
| 265 | time.sleep(0.05) |
| 266 | time.sleep(1.5) |
| 267 | text = logtext() |
| 268 | if p.returncode in (130, -15): |
| 269 | rc = "signalled" |
| 270 | elif p.returncode == -9: |
| 271 | rc = "killed" |
| 272 | else: |
| 273 | rc = "rc=%d" % p.returncode |
| 274 | key = "close FAKEWID-" + args[1] |
| 275 | closed = key in text |
| 276 | after = closed and "row-exit" in text and text.index("row-exit") < text.index(key) |
| 277 | results.append((rc, closed, after)) |
| 278 | continue |
| 279 | if mode == "kill-in-open": |
| 280 | p = subprocess.Popen(argv, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 281 | log = os.path.join(root, "fake-spark-window.log") |
| 282 | |
| 283 | def opentext(): |
| 284 | return open(log).read() if os.path.exists(log) else "" |
| 285 | |
| 286 | deadline = time.time() + 30 |
| 287 | while time.time() < deadline and "open " not in opentext(): |
| 288 | time.sleep(0.05) |
| 289 | os.kill(p.pid, signal.SIGKILL) |
| 290 | p.communicate(timeout=60) |
| 291 | key = "close FAKEWID-" + args[1] |
| 292 | deadline = time.time() + 30 |
| 293 | while time.time() < deadline and key not in opentext(): |
| 294 | time.sleep(0.05) |
| 295 | time.sleep(1.5) |
| 296 | text = opentext() |
| 297 | rc = "killed" if p.returncode == -9 else "rc=%d" % p.returncode |
| 298 | results.append((rc, key in text, "row-start" in text)) |
| 299 | continue |
| 300 | if mode == "om-closer-gone": |
| 301 | # the closer (a child that made itself a session leader) is killed during a row: |
| 302 | # nothing can close the window, and the successor must say so (review N2) |
| 303 | if is_sh: |
| 304 | results.append(None) |
| 305 | continue |
| 306 | p = subprocess.Popen(argv, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 307 | log = os.path.join(root, "fake-spark-window.log") |
| 308 | deadline = time.time() + 30 |
| 309 | while time.time() < deadline and "row-start" not in (open(log).read() if os.path.exists(log) else ""): |
| 310 | time.sleep(0.05) |
| 311 | closer = [] |
| 312 | for d in os.listdir("/proc"): |
| 313 | if not d.isdigit(): |
| 314 | continue |
| 315 | try: |
| 316 | st = open("/proc/%s/stat" % d).read() |
| 317 | except OSError: |
| 318 | continue |
| 319 | f = st[st.rindex(")") + 2:].split() |
| 320 | if int(f[1]) == p.pid and int(f[3]) == int(d): |
| 321 | closer.append(int(d)) |
| 322 | for c in closer: |
| 323 | os.kill(c, signal.SIGKILL) |
| 324 | so, se = p.communicate(timeout=120) |
| 325 | text = open(log).read() if os.path.exists(log) else "" |
| 326 | wt = os.path.join(root, "evidence/device-window", args[1], "window.txt") |
| 327 | said = "closer is gone; window FAKEWID-%s was not closed" % args[1] |
| 328 | results.append((len(closer), said in se.decode(), |
| 329 | os.path.exists(wt) and said in open(wt).read(), "close " in text)) |
| 330 | continue |
| 331 | if mode == "om-refuse-env": |
| 332 | if is_sh: |
| 333 | results.append(None) |
| 334 | continue |
| 335 | cp = subprocess.run(argv, env=env, capture_output=True, timeout=120) |
| 336 | log = os.path.join(root, "fake-spark-window.log") |
| 337 | ev = os.path.join(root, "evidence/device-window", args[1]) |
| 338 | err = cp.stderr.decode() |
| 339 | said = "execprog packet" in err or "longer than 65535 bytes" in err |
| 340 | results.append((cp.returncode, cp.stdout == b"", said, os.path.exists(log), os.path.exists(ev))) |
| 341 | continue |
| 342 | if mode == "om-badwid": |
| 343 | if is_sh: |
| 344 | results.append(None) |
| 345 | continue |
| 346 | cp = subprocess.run(argv, env=env, capture_output=True, timeout=120) |
| 347 | log = os.path.join(root, "fake-spark-window.log") |
| 348 | text = open(log).read() if os.path.exists(log) else "" |
| 349 | ev = os.path.join(root, "evidence/device-window", args[1]) |
| 350 | rows = sorted(f for f in os.listdir(ev) if re.match(r"\d\d-", f)) if os.path.isdir(ev) else [] |
| 351 | results.append((cp.returncode, "printed no usable window id" in cp.stderr.decode(), |
| 352 | "close" in text, rows)) |
| 353 | continue |
| 354 | cp = subprocess.run(argv, env=env, capture_output=True, timeout=120) |
| 355 | so = norm(cp.stdout.decode(), root, is_sh) |
| 356 | se = norm(cp.stderr.decode(), root, is_sh) |
| 357 | snap = snapshot(root, args[1] if len(args) > 1 else "", prog) if mode == "device" else {} |
| 358 | results.append((cp.returncode, so, se, snap)) |
| 359 | a, b = results |
| 360 | if mode == "om-closer-gone": |
| 361 | return b == (1, True, True, False), "om=%s" % (b,) |
| 362 | if mode == "om-badwid": |
| 363 | # the shell script took any output of `spark-window open` as the id; the successor |
| 364 | # refuses (exit 3), runs no row and closes nothing |
| 365 | return b == (3, True, False, []), "om=%s" % (b,) |
| 366 | if mode == "om-refuse-env": |
| 367 | # refused before anything was printed, opened or created |
| 368 | return b == (3, True, True, False, False), "om=%s" % (b,) |
| 369 | if mode == "kill-in-open": |
| 370 | # the shell's trap never runs on kill -9: the window is left to its lease; the |
| 371 | # successor's closer closes it after the open exited; no row runs in either |
| 372 | ok = a == ("killed", False, False) and b == ("killed", True, False) |
| 373 | return ok, "sh=%s om=%s" % (a, b) |
| 374 | if mode == "signal": |
| 375 | ok = a == b and a[1] |
| 376 | return ok, "sh=%s om=%s" % (a, b) |
| 377 | if mode == "parent-term": |
| 378 | ok = a == b == ("signalled", True, True) |
| 379 | return ok, "sh=%s om=%s" % (a, b) |
| 380 | if mode == "parent-kill": |
| 381 | ok = a == ("killed", False, False) and b == ("killed", True, True) |
| 382 | return ok, "sh=%s om=%s" % (a, b) |
| 383 | if name in RC_ONLY: |
| 384 | ok = a[0] == b[0] and a[1] == b[1] and a[3] == b[3] |
| 385 | else: |
| 386 | ok = a == b |
| 387 | detail = "" |
| 388 | if not ok: |
| 389 | for label, x, y in (("rc", a[0], b[0]), ("stdout", a[1], b[1]), ("stderr", a[2], b[2])): |
| 390 | if x != y: |
| 391 | detail += "\n %s sh=%r\n %s om=%r" % (label, x, label, y) |
| 392 | for key in sorted(set(a[3]) | set(b[3])): |
| 393 | if a[3].get(key) != b[3].get(key): |
| 394 | detail += "\n file %s\n sh=%r\n om=%r" % (key, a[3].get(key), b[3].get(key)) |
| 395 | return ok, detail |
| 396 | |
| 397 | |
| 398 | def main(): |
| 399 | if len(sys.argv) != 4 or os.path.exists(sys.argv[3]): |
| 400 | print("usage: compare.py OLD_SH NEW_BIN SCRATCH; SCRATCH must not exist", file=sys.stderr) |
| 401 | return 2 |
| 402 | old_sh, new_bin, scratch = (os.path.abspath(a) for a in sys.argv[1:]) |
| 403 | roots = {True: os.path.join(scratch, "sh"), False: os.path.join(scratch, "om")} |
| 404 | progs = {True: build_tree(roots[True], old_sh, True), False: build_tree(roots[False], new_bin, False)} |
| 405 | dry = {"CUDA_VISIBLE_DEVICES": "", "PYTHON": "@T/bin/python-torch"} |
| 406 | dev = {"PYTHON": "@T/bin/python-torch"} |
| 407 | |
| 408 | def rm_torchlib(root): |
| 409 | p = os.path.join(root, "attach/torch/libcudapp_attach.so") |
| 410 | if os.path.exists(p): |
| 411 | os.remove(p) |
| 412 | |
| 413 | def put_torchlib(root): |
| 414 | put(os.path.join(root, "attach/torch/libcudapp_attach.so"), "fake torch lib\n") |
| 415 | |
| 416 | def rm_conf_build(root): |
| 417 | shutil.rmtree(os.path.join(root, "conformance/build"), ignore_errors=True) |
| 418 | log = os.path.join(root, "fake-spark-window.log") |
| 419 | if os.path.exists(log): |
| 420 | os.remove(log) |
| 421 | |
| 422 | cases = [ |
| 423 | # the eight refusal controls |
| 424 | ("refuse-no-ledger", [], {}, "plain", None), |
| 425 | ("refuse-ledger-needs-id", ["--ledger"], {}, "plain", None), |
| 426 | ("refuse-ledger-chars", ["--ledger", "a/b"], {}, "plain", None), |
| 427 | ("refuse-unknown-arg", ["--ledger", "X", "--bogus"], {}, "plain", None), |
| 428 | ("refuse-pairs-not-integer", ["--ledger", "X", "--pairs", "5x"], {}, "plain", None), |
| 429 | ("refuse-pairs-lt-5", ["--ledger", "X", "--pairs", "4"], {}, "plain", None), |
| 430 | ("refuse-dry-cvd-unset", ["--ledger", "X", "--dry-run"], {"PYTHON": "@T/bin/python-torch"}, "plain", None), |
| 431 | ("refuse-dry-in-window", ["--ledger", "X", "--dry-run"], dict(dry, SPARK_WINDOW="W1"), "plain", None), |
| 432 | ("refuse-dry-cvd-huge", ["--ledger", "X", "--dry-run"], dict(dry, CUDA_VISIBLE_DEVICES="0" * 5000), "plain", None), |
| 433 | ("refuse-dry-in-window-huge", ["--ledger", "X", "--dry-run"], dict(dry, SPARK_WINDOW="W" * 5000), "plain", None), |
| 434 | # more refusals |
| 435 | ("refuse-dry-cvd-nonempty", ["--ledger", "X", "--dry-run"], dict(dry, CUDA_VISIBLE_DEVICES="0"), "plain", None), |
| 436 | ("refuse-pairs-empty", ["--ledger", "X", "--pairs", ""], {}, "plain", None), |
| 437 | ("refuse-env-pairs-lt-5", ["--ledger", "X", "--dry-run"], dict(dry, PAIRS="3"), "plain", None), |
| 438 | ("refuse-ledger-empty", ["--ledger", ""], {}, "plain", None), |
| 439 | ("refuse-ledger-space", ["--ledger", "a b"], {}, "plain", None), |
| 440 | ("pairs-no-value", ["--ledger", "X", "--pairs"], {}, "plain", None), |
| 441 | ("dry-home-unset", ["--ledger", "X", "--dry-run"], dict(dry, HOME=None), "plain", None), |
| 442 | # dry-run plans |
| 443 | ("dry-pass", ["--ledger", "L1", "--dry-run"], dry, "plain", None), |
| 444 | ("dry-pass-pairs-07", ["--dry-run", "--pairs", "07", "--ledger", "L1"], dry, "plain", None), |
| 445 | ("dry-pass-env-pairs", ["--ledger", "L.1-a_b", "--dry-run"], dict(dry, PAIRS="6"), "plain", None), |
| 446 | ("dry-pass-last-ledger-wins", ["--ledger", "A", "--ledger", "B", "--dry-run"], dry, "plain", None), |
| 447 | ("dry-pass-maxpy", ["--ledger", "L1", "--dry-run"], dict(dry, MAXPY="@T/bin/python-torch"), "plain", None), |
| 448 | ("dry-incomplete-maxpy", ["--ledger", "L1", "--dry-run"], dict(dry, MAXPY="@T/nope"), "plain", None), |
| 449 | ("dry-incomplete-no-torch", ["--ledger", "L1", "--dry-run"], dict(dry, PYTHON="@T/bin/python-notorch"), "plain", None), |
| 450 | ("dry-incomplete-python-missing", ["--ledger", "L1", "--dry-run"], dict(dry, PYTHON="no-such-python-x"), "plain", None), |
| 451 | ("dry-python-unset", ["--ledger", "L1", "--dry-run"], dict(dry, PYTHON=None), "plain", None), |
| 452 | ("dry-incomplete-cubin-dir", ["--ledger", "L1", "--dry-run"], dict(dry, CUBIN_DIR="@T/nowhere"), "plain", None), |
| 453 | ("dry-incomplete-torchlib", ["--ledger", "L1", "--dry-run"], dict(dry, CUDAPP_TORCH_LIB="@T/none.so"), "plain", None), |
| 454 | ("dry-incomplete-cudapp", ["--ledger", "L1", "--dry-run"], dict(dry, CUDAPP="@T/nocudapp"), "plain", None), |
| 455 | ("dry-incomplete-torchlib-removed", ["--ledger", "L1", "--dry-run"], dry, "plain", rm_torchlib), |
| 456 | # device mode under the fake spark-window |
| 457 | ("device-pass", ["--ledger", "D1"], dev, "device", put_torchlib), |
| 458 | ("dry-exists", ["--ledger", "D1", "--dry-run"], dry, "plain", None), |
| 459 | ("device-exists", ["--ledger", "D1"], dev, "device", None), |
| 460 | ("device-pass-pairs-6", ["--ledger", "D2", "--pairs", "6"], dev, "device", rm_conf_build), |
| 461 | ("device-row-fails", ["--ledger", "D3"], dict(dev, FAKE_LOAD_RC="3"), "device", rm_conf_build), |
| 462 | ("device-in-window", ["--ledger", "D4"], dict(dev, SPARK_WINDOW="OUTER-7"), "device", rm_conf_build), |
| 463 | ("device-open-fails", ["--ledger", "D5"], dict(dev, FAKE_OPEN_FAIL="1"), "device", rm_conf_build), |
| 464 | ("device-no-cubins", ["--ledger", "D6"], dict(dev, CUBIN_DIR="@T/nowhere"), "device", rm_conf_build), |
| 465 | ("device-python-missing", ["--ledger", "D7"], dict(dev, PYTHON="no-such-python-x"), "device", rm_conf_build), |
| 466 | ("device-no-cudapp-git", ["--ledger", "D8"], dict(dev, CUDAPP="@T/nocudapp", CUBIN_DIR="@T/cudapp/build/window-cubins"), "device", rm_conf_build), |
| 467 | ("device-signal-closes", ["--ledger", "D9"], dict(dev, FAKE_SLEEP="30"), "signal", rm_conf_build), |
| 468 | ("device-parent-term", ["--ledger", "D10"], dict(dev, FAKE_SLEEP="3"), "parent-term", rm_conf_build), |
| 469 | ("device-parent-kill", ["--ledger", "D11"], dict(dev, FAKE_SLEEP="3"), "parent-kill", rm_conf_build), |
| 470 | ("om-closer-gone", ["--ledger", "D40"], dict(dev, FAKE_SLEEP="3"), "om-closer-gone", rm_conf_build), |
| 471 | ("refuse-unknown-arg-huge", ["--ledger", "X", "u" * 70000], {}, "plain", None), |
| 472 | ("om-refuse-wid-chars", ["--ledger", "D12"], dict(dev, FAKE_WID="a/b"), "om-badwid", rm_conf_build), |
| 473 | ("om-refuse-wid-empty", ["--ledger", "D13"], dict(dev, FAKE_WID=""), "om-badwid", rm_conf_build), |
| 474 | ("om-refuse-wid-long", ["--ledger", "D14"], dict(dev, FAKE_WID="w" * 5000), "om-badwid", rm_conf_build), |
| 475 | ("om-refuse-wid-huge", ["--ledger", "D15"], dict(dev, FAKE_WID_BIG="1"), "om-badwid", rm_conf_build), |
| 476 | ("device-kill-in-open", ["--ledger", "D16"], dict(dev, FAKE_OPEN_SLEEP="2"), "kill-in-open", rm_conf_build), |
| 477 | ("om-refuse-env-entry", ["--ledger", "D17"], dict(dev, BIGVAR="v" * 70000), "om-refuse-env", rm_conf_build), |
| 478 | ("om-refuse-env-packet", ["--ledger", "D18"], dict(dev, **{"BIG%02d" % i: "v" * 4000 for i in range(20)}), "om-refuse-env", rm_conf_build), |
| 479 | ("om-refuse-env-packet-dry", ["--ledger", "D19", "--dry-run"], dict(dry, **{"BIG%02d" % i: "v" * 4000 for i in range(20)}), "om-refuse-env", rm_conf_build), |
| 480 | ] |
| 481 | fails = 0 |
| 482 | for name, args, extra, mode, pre in cases: |
| 483 | ok, detail = run_case(name, args, extra, roots, progs, mode, pre) |
| 484 | print("%s %s%s" % ("SAME" if ok else "DIFF", name, "" if ok else detail)) |
| 485 | if not ok: |
| 486 | fails += 1 |
| 487 | if fails: |
| 488 | print("COMPARE FAIL cases=%d differ=%d" % (len(cases), fails)) |
| 489 | return 1 |
| 490 | print("COMPARE PASS cases=%d" % len(cases)) |
| 491 | return 0 |
| 492 | |
| 493 | |
| 494 | if __name__ == "__main__": |
| 495 | sys.exit(main()) |