omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / max-cpu / run.sh
| 1 | #!/bin/sh |
| 2 | # Reproduce the MAX CPU route for the Omega matmul custom op (evidence/MAX-CPU/README.md). |
| 3 | # Run from anywhere, pinned: TMPDIR=/dev/shm taskset -c 5-9 nice -n 19 ionice -c3 max-cpu/run.sh |
| 4 | # CPU only: every graph is built on DeviceRef.CPU(). MAX 26.7.0 Graph() still opens a CUDA |
| 5 | # context when a GPU is visible, so the GPU is hidden here and omega_max.py refuses otherwise. |
| 6 | set -eu |
| 7 | export CUDA_VISIBLE_DEVICES= |
| 8 | cd "$(dirname "$0")/.." |
| 9 | MAXPY=${MAXPY:-$HOME/workspace/maxinterop/venv/bin/python} |
| 10 | OMEGA_REPO=${OMEGA_REPO:-$HOME/workspace/omega} |
| 11 | BUILD=${BUILD:-/dev/shm/omega-max-cpu} |
| 12 | ITERS=${ITERS:-300} |
| 13 | OMC1_REV=55f9b0af OMC1_SHA=734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61 |
| 14 | OMC2_REV=ec07d25d OMC2_SHA=499236398aabc652e2dae27fe4bdc4c5db3d3726314356e0dbef23e3345803ec |
| 15 | GLUE_CALL='process.get_function[UInt32]("omega2_omatmul")(' |
| 16 | # Mutant: A's row stride N instead of K (one token). A transposed C write (C[j*M+i]) does not |
| 17 | # compile: brow is already at the 16-live-value limit (omc error 12). |
| 18 | MUT_FROM='ai = mul i k' |
| 19 | MUT_TO='ai = mul i n' |
| 20 | |
| 21 | # Never replay into the committed run of record or an earlier evidence directory. |
| 22 | # Resolve aliases before checking containment. These are caller-owned paths on a |
| 23 | # stable local filesystem, not a sandbox against concurrent namespace changes. |
| 24 | inside() { |
| 25 | case "$1/" in "${2%/}/"*) return 0 ;; *) return 1 ;; esac |
| 26 | } |
| 27 | build_path=$(realpath -m -- "$BUILD") || exit 2 |
| 28 | if [ -n "${EV:-}" ]; then |
| 29 | evidence_path=$(realpath -m -- "$EV") || exit 2 |
| 30 | if inside "$evidence_path" "$build_path" || inside "$build_path" "$evidence_path"; then |
| 31 | echo 'run.sh: EV must not overlap BUILD' >&2 |
| 32 | exit 2 |
| 33 | fi |
| 34 | # Check the supplied path before resolving away a dangling leaf symlink. |
| 35 | evidence_input=$EV |
| 36 | while [ "$evidence_input" != / ] && [ "${evidence_input%/}" != "$evidence_input" ]; do |
| 37 | evidence_input=${evidence_input%/} |
| 38 | done |
| 39 | evidence_input_parent=$(dirname -- "$evidence_input") |
| 40 | if [ -e "$evidence_input" ] || [ -L "$evidence_input" ] || |
| 41 | [ ! -d "$evidence_input_parent" ] || [ ! -w "$evidence_input_parent" ]; then |
| 42 | echo 'run.sh: EV must name a new directory with an existing writable parent' >&2 |
| 43 | exit 2 |
| 44 | fi |
| 45 | # A lexical path may traverse a BUILD symlink that the reset removes. |
| 46 | EV=$evidence_path |
| 47 | else |
| 48 | evidence_parent=$(realpath -e -- "${TMPDIR:-/tmp}") || exit 2 |
| 49 | if [ ! -d "$evidence_parent" ] || inside "$evidence_parent" "$build_path"; then |
| 50 | echo 'run.sh: default evidence TMPDIR must be a directory outside BUILD' >&2 |
| 51 | exit 2 |
| 52 | fi |
| 53 | fi |
| 54 | # Refuse before resetting BUILD or opening any output log. |
| 55 | rm -rf "$BUILD" |
| 56 | mkdir -p "$BUILD/omc" |
| 57 | # Atomic creation refuses a destination that appeared after the preflight. |
| 58 | if [ -n "${EV:-}" ]; then |
| 59 | if ! mkdir -- "$EV"; then |
| 60 | echo 'run.sh: EV must name a new directory with an existing writable parent' >&2 |
| 61 | exit 2 |
| 62 | fi |
| 63 | else |
| 64 | EV=$(mktemp -d "$evidence_parent/omega-max-cpu-evidence.XXXXXXXX") |
| 65 | fi |
| 66 | printf 'evidence=%s\n' "$EV" |
| 67 | for v in 1 2; do |
| 68 | eval rev=\$OMC${v}_REV sha=\$OMC${v}_SHA |
| 69 | git -C "$OMEGA_REPO" show "$rev:seed/out/omc" >"$BUILD/omc/omc$v" |
| 70 | chmod +x "$BUILD/omc/omc$v" |
| 71 | echo "$sha $BUILD/omc/omc$v" | sha256sum -c - |
| 72 | done |
| 73 | export OMEGA_NATIVE_OMC="$BUILD/omc/omc1" OMEGA_NATIVE_OMC2="$BUILD/omc/omc2" |
| 74 | |
| 75 | once() { # once FILE FROM: FROM must occur exactly once |
| 76 | n=$(grep -cF -- "$2" "$1" || true) |
| 77 | [ "$n" = 1 ] || { echo "run.sh: '$2' occurs $n times in $1" >&2; exit 2; } |
| 78 | } |
| 79 | swap() { # swap SRC DST FROM TO |
| 80 | once "$1" "$3" |
| 81 | FROM=$3 TO=$4 "$MAXPY" -c 'import os,sys; s=open(sys.argv[1]).read(); open(sys.argv[2],"w").write(s.replace(os.environ["FROM"],os.environ["TO"]))' "$1" "$2" |
| 82 | once "$2" "$4" |
| 83 | } |
| 84 | |
| 85 | # Omega library, and the transposed-write mutant (negative control c). |
| 86 | tools/native "$BUILD/lib" max-cpu/omatmul.om >"$EV/build-lib.log" 2>&1 |
| 87 | mkdir -p "$BUILD/mut" |
| 88 | swap max-cpu/omatmul.om "$BUILD/mut/omatmul.om" "$MUT_FROM" "$MUT_TO" |
| 89 | tools/native "$BUILD/lib-mutant" "$BUILD/mut/omatmul.om" >"$EV/build-mutant.log" 2>&1 |
| 90 | grep -h 'omega-ident' "$BUILD/lib/omega_native.h" "$BUILD/lib-mutant/omega_native.h" >"$EV/idents.txt" || true |
| 91 | |
| 92 | # Glue variants (negative control a): a missing symbol, and the plain external_call route. |
| 93 | mkdir -p "$BUILD/wrongsym/kernels" "$BUILD/extcall/kernels" |
| 94 | swap max-cpu/kernels/__init__.mojo "$BUILD/wrongsym/kernels/__init__.mojo" "$GLUE_CALL" \ |
| 95 | 'process.get_function[UInt32]("omega2_omatmul_missing")(' |
| 96 | swap max-cpu/kernels/__init__.mojo "$BUILD/extcall/kernels/__init__.mojo" "$GLUE_CALL" \ |
| 97 | 'external_call["omega2_omatmul", UInt32](' |
| 98 | |
| 99 | # Each run gets an empty MAX cache: MAX 26.7.0 reuses compiled kernels across glue edits. |
| 100 | run() { # run NAME ARGS... |
| 101 | name=$1; shift |
| 102 | rm -rf "$BUILD/cache-$name" |
| 103 | MODULAR_CACHE_DIR="$BUILD/cache-$name" "$MAXPY" max-cpu/omega_max.py "$@" >"$EV/$name.log" 2>&1 |
| 104 | } |
| 105 | fail=0 |
| 106 | L="--lib $BUILD/lib/libomega_native.so" |
| 107 | run check check $L --out "$EV/check.json" || fail=1 |
| 108 | run neg-shape neg-shape $L --out "$EV/neg-shape.json" || fail=1 |
| 109 | run neg-wrongsym neg-load $L --kernels "$BUILD/wrongsym/kernels" \ |
| 110 | --want 'symbol not found: omega2_omatmul_missing' || fail=1 |
| 111 | run neg-extcall neg-load $L --kernels "$BUILD/extcall/kernels" \ |
| 112 | --want 'Failed to materialize symbols' || fail=1 |
| 113 | grep -qF 'Symbols not found: [ omega2_omatmul ]' "$EV/neg-extcall.log" || fail=1 |
| 114 | if run neg-mutant check --lib "$BUILD/lib-mutant/libomega_native.so" --out "$EV/neg-mutant.json"; then |
| 115 | echo "neg-mutant: the A-stride mutant passed the numeric check" >&2; fail=1 |
| 116 | else |
| 117 | grep -q 'OMEGA_MAX_CHECK: FAIL' "$EV/neg-mutant.log" || fail=1 |
| 118 | fi |
| 119 | for f in check neg-shape neg-wrongsym neg-extcall neg-mutant; do |
| 120 | printf '%-13s %s\n' "$f" "$(grep -h 'OMEGA_MAX_' "$EV/$f.log" | tail -1)" |
| 121 | done |
| 122 | if [ "${MEASURE:-1}" = 1 ]; then |
| 123 | "$MAXPY" max-cpu/omega_max.py measure-all $L --iters "$ITERS" --out "$EV/measure" \ |
| 124 | >"$EV/measure.log" 2>&1 || fail=1 |
| 125 | rm -rf "$EV"/measure/cache-* |
| 126 | fi |
| 127 | [ $fail -eq 0 ] && echo "OMEGA MAX CPU: PASS (controls green, negatives red as expected)" |
| 128 | exit $fail |