Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-bridge/blob/main/tests/device-window/compare.py # omega-bridge Omega-first language interoperability: checked conversion backends and native language bindings Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-bridge / tests / device-window / compare.py` 495 lines ยท 25554 bytes Raw Blame History 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 , 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"), "!\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, "") 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, "") 200 log = os.path.join(root, "fake-spark-window.log") 201 snap["fake-log"] = open(log).read().replace(root, "") 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 + ("