interplane.Get Omega
Read in your language

omega-bridge

Omega-first language interoperability: checked conversion backends and native language bindings

Brought by drakestapleton

branch main Branches Tags History

omega-bridge / max-cpu / run.sh

128 lines · 6094 bytes Raw Blame History
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.
6set -eu
7export CUDA_VISIBLE_DEVICES=
8cd "$(dirname "$0")/.."
9MAXPY=${MAXPY:-$HOME/workspace/maxinterop/venv/bin/python}
10OMEGA_REPO=${OMEGA_REPO:-$HOME/workspace/omega}
11BUILD=${BUILD:-/dev/shm/omega-max-cpu}
12ITERS=${ITERS:-300}
13OMC1_REV=55f9b0af OMC1_SHA=734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61
14OMC2_REV=ec07d25d OMC2_SHA=499236398aabc652e2dae27fe4bdc4c5db3d3726314356e0dbef23e3345803ec
15GLUE_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).
18MUT_FROM='ai = mul i k'
19MUT_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.
24inside() {
25 case "$1/" in "${2%/}/"*) return 0 ;; *) return 1 ;; esac
26}
27build_path=$(realpath -m -- "$BUILD") || exit 2
28if [ -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
47else
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
53fi
54# Refuse before resetting BUILD or opening any output log.
55rm -rf "$BUILD"
56mkdir -p "$BUILD/omc"
57# Atomic creation refuses a destination that appeared after the preflight.
58if [ -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
63else
64 EV=$(mktemp -d "$evidence_parent/omega-max-cpu-evidence.XXXXXXXX")
65fi
66printf 'evidence=%s\n' "$EV"
67for 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 -
72done
73export OMEGA_NATIVE_OMC="$BUILD/omc/omc1" OMEGA_NATIVE_OMC2="$BUILD/omc/omc2"
74
75once() { # 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}
79swap() { # 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).
86tools/native "$BUILD/lib" max-cpu/omatmul.om >"$EV/build-lib.log" 2>&1
87mkdir -p "$BUILD/mut"
88swap max-cpu/omatmul.om "$BUILD/mut/omatmul.om" "$MUT_FROM" "$MUT_TO"
89tools/native "$BUILD/lib-mutant" "$BUILD/mut/omatmul.om" >"$EV/build-mutant.log" 2>&1
90grep -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.
93mkdir -p "$BUILD/wrongsym/kernels" "$BUILD/extcall/kernels"
94swap max-cpu/kernels/__init__.mojo "$BUILD/wrongsym/kernels/__init__.mojo" "$GLUE_CALL" \
95 'process.get_function[UInt32]("omega2_omatmul_missing")('
96swap 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.
100run() { # 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}
105fail=0
106L="--lib $BUILD/lib/libomega_native.so"
107run check check $L --out "$EV/check.json" || fail=1
108run neg-shape neg-shape $L --out "$EV/neg-shape.json" || fail=1
109run neg-wrongsym neg-load $L --kernels "$BUILD/wrongsym/kernels" \
110 --want 'symbol not found: omega2_omatmul_missing' || fail=1
111run neg-extcall neg-load $L --kernels "$BUILD/extcall/kernels" \
112 --want 'Failed to materialize symbols' || fail=1
113grep -qF 'Symbols not found: [ omega2_omatmul ]' "$EV/neg-extcall.log" || fail=1
114if 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
116else
117 grep -q 'OMEGA_MAX_CHECK: FAIL' "$EV/neg-mutant.log" || fail=1
118fi
119for 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)"
121done
122if [ "${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-*
126fi
127[ $fail -eq 0 ] && echo "OMEGA MAX CPU: PASS (controls green, negatives red as expected)"
128exit $fail