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 / tools / run-device-window.sh

179 lines · 9551 bytes Raw Blame History
1#!/bin/sh
2# One-shot GB10 device qualification for every row of MISSION-REPORT.md ยง3 that
3# a window can unblock. Opens ONE exclusive spark-window (chip lock exclusive,
4# no quiet lock: the rows compare values, so their timings are shared-machine
5# diagnostics, not speed receipts), waits its turn in line, and closes the
6# window by its id when done or on any exit.
7#
8# sh tools/run-device-window.sh --ledger L679 # device run (needs an authorized lease)
9# CUDA_VISIBLE_DEVICES= sh tools/run-device-window.sh --ledger L679 --dry-run
10#
11# Device mode does, in order, inside ONE window:
12# 1. cubin_probe on cubins exported by the CURRENT CUDApp writer (CUBIN_DIR):
13# matmul 16x16x8 load + launch against the probe's fp32 oracle; GQA
14# (hq 8, hkv 1, hd 128) load + symbol resolve only (cubin_probe launches the
15# matmul ABI; an attention launch needs an attention probe, MAX Gate 3);
16# negative control: the pre-fix image committed in CUDApp
17# evidence/MAX-INTEROP/68 (no .note.nv.tkinfo, CUDApp#202) must be rejected.
18# 2. conformance/run.sh --device (Omega native + MAX 26.7 max_runner.py --device gpu,
19# matmul and GQA, each judged by the independent oracle)
20# 3. paired runs: conformance/run.sh --device repeated PAIRS times (default 5, minimum 5)
21# 4. attach/torch/test_cudapp_torch.py (PR #30) against CUDAPP_TORCH_LIB
22# Export the cubins on the CPU before the window (opens no device; the exporter
23# refuses an existing file, so use an empty CUBIN_DIR):
24# (cd $CUDAPP && make artifact-export cubin-probe && mkdir -p build/window-cubins &&
25# build/artifact-export matmul --m 16 --k 16 --n 8 \
26# --contract-id 647116fc1ba1e1394982fc8ba181ce565293f160a6b56afc0f02b2128a675704 \
27# --out build/window-cubins/matmul-16x16x8 --commit $(git rev-parse HEAD) &&
28# build/artifact-export attention --q-heads 8 --kv-heads 1 \
29# --contract-id 003696e422bba7274877ba9e01a61076ea0f862f32760cbaa06c60f85a115359 \
30# --out build/window-cubins/attention-hd128-gqa8 --commit $(git rev-parse HEAD))
31# The exported cubins and manifests are copied into the evidence directory.
32# Raw stdout/stderr and rc of every step go to evidence/device-window/<ledger>/,
33# with the window id, host and source identity in window.txt and each
34# conformance row's device result files (.json metadata + .bin output) in
35# <row>.out/. Never resets the GPU, changes the driver, or touches the serving
36# process.
37#
38# --dry-run opens nothing: no spark-window, no probe, no device file. It
39# refuses unless CUDA_VISIBLE_DEVICES is set and empty, prints the plan, and
40# checks that every input file exists and that $PYTHON can find torch (an
41# importlib find_spec lookup; torch itself is not imported). rc 0 = plan complete.
42set -eu
43ledger="" dry=0 pairs=${PAIRS:-5}
44while [ $# -gt 0 ]; do
45 case $1 in
46 --ledger) [ $# -ge 2 ] || { echo "REFUSE: --ledger needs an ID" >&2; exit 2; }; ledger=$2; shift 2 ;;
47 --dry-run) dry=1; shift ;;
48 --pairs) pairs=$2; shift 2 ;;
49 *) echo "unknown $1" >&2; exit 2 ;;
50 esac
51done
52[ -n "$ledger" ] || { echo "REFUSE: no ledger id; usage: run-device-window.sh --ledger ID [--dry-run] [--pairs N]" >&2; exit 2; }
53case $ledger in *[!A-Za-z0-9._-]*) echo "REFUSE: ledger id must match [A-Za-z0-9._-]+" >&2; exit 2 ;; esac
54case $pairs in ''|*[!0-9]*) echo "REFUSE: --pairs must be an integer" >&2; exit 2 ;; esac
55[ "$pairs" -ge 5 ] || { echo "REFUSE: --pairs $pairs < 5" >&2; exit 2; }
56
57here=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
58cudapp=${CUDAPP:-$here/../cudapp}
59torchlib=${CUDAPP_TORCH_LIB:-$here/attach/torch/libcudapp_attach.so}
60out="$here/evidence/device-window/$ledger"
61name="maxinterop-device-$ledger"
62cubins=${CUBIN_DIR:-$cudapp/build/window-cubins}
63mm_cubin="$cubins/matmul-16x16x8.cubin"
64gqa_cubin="$cubins/attention-hd128-gqa8.cubin"
65# Pre-fix writer output (CUDApp evidence/MAX-INTEROP/G2/README.md: "do not use it
66# as the current writer's output"); only ever the expected-reject control.
67stale_cubin="$cudapp/evidence/MAX-INTEROP/68/matmul-16x16x8.cubin"
68
69# step NAME CMD...: one plan row. Dry-run prints; device mode runs under spark-window.
70n=0 fail=0
71step() {
72 n=$((n + 1)); label=$(printf '%02d-%s' "$n" "$1"); shift
73 if [ "$dry" = 1 ]; then echo "PLAN $label: spark-window run $*"; return 0; fi
74 set +e
75 spark-window run "$@" >"$out/$label.log" 2>&1
76 rc=$?
77 set -e
78 echo "$label rc=$rc" | tee -a "$out/rc.txt"
79 [ "$rc" = 0 ] || fail=1
80}
81# conf_step NAME: one conformance/run.sh --device row. Device results of an earlier
82# row are removed first, so <row>.out/ only ever holds files this row wrote.
83cbuild="$here/conformance/build"
84conf_step() {
85 if [ "$dry" != 1 ]; then rm -f "$cbuild"/omega-gpu-* "$cbuild"/max-gpu-*; fi
86 step "$1" "$here/conformance/run.sh" --device
87 if [ "$dry" != 1 ]; then
88 mkdir -p "$out/$label.out"
89 for f in "$cbuild"/omega-gpu-* "$cbuild"/max-gpu-*; do if [ -e "$f" ]; then cp "$f" "$out/$label.out/"; fi; done
90 (cd "$out/$label.out" && sha256sum -- * 2>/dev/null) >"$out/$label.out.sha256" || true
91 fi
92}
93need() { if [ -e "$1" ]; then echo "OK $1"; else echo "MISSING $1"; missing=1; fi; }
94
95plan() {
96 step cubin-matmul "$cudapp/build/cubin_probe" "$mm_cubin" omega_matmul_bf16 16 16 8
97 step cubin-gqa-load "$cudapp/build/cubin_probe" --load-only "$gqa_cubin" omega_gqa_attention_causal 2 8 1
98 step cubin-stale-reject "$cudapp/build/cubin_probe" --expect-reject "$stale_cubin" omega_matmul_bf16 16 16 8
99 conf_step conformance-device
100 i=1
101 while [ "$i" -le "$pairs" ]; do conf_step "pair$i-conformance-device"; i=$((i + 1)); done
102 step torch-cudapp env CUDAPP_TORCH_LIB="$torchlib" PYTHONPATH="$here/attach/torch" \
103 "${PYTHON:-python3}" "$here/attach/torch/test_cudapp_torch.py" "$out/torch-receipt.json"
104}
105
106if [ "$dry" = 1 ]; then
107 if [ "${CUDA_VISIBLE_DEVICES-unset}" = unset ] || [ -n "${CUDA_VISIBLE_DEVICES}" ]; then
108 echo "REFUSE: dry-run requires CUDA_VISIBLE_DEVICES set and empty (got '${CUDA_VISIBLE_DEVICES-unset}')" >&2
109 exit 3
110 fi
111 [ -z "${SPARK_WINDOW:-}" ] || { echo "REFUSE: dry-run inside an open SPARK_WINDOW=$SPARK_WINDOW" >&2; exit 3; }
112 echo "DRY-RUN ledger=$ledger window=$name pairs=$pairs out=evidence/device-window/$ledger"
113 echo "WOULD: spark-window open $name --ledger $ledger --gb10 exclusive --quiet none --lease 20 --wait"
114 plan
115 echo "INPUTS"
116 missing=0
117 need "$cudapp/build/cubin_probe"; need "$mm_cubin"; need "$gqa_cubin"; need "$stale_cubin"
118 need "$here/conformance/run.sh"; need "$here/conformance/max_runner.py"
119 need "$here/attach/torch/test_cudapp_torch.py"; need "$torchlib"
120 need "${MAXPY:-$HOME/workspace/maxinterop/venv/bin/python}"; need "$cudapp/build/libomega_gpu.a"
121 py=${PYTHON:-python3}
122 if "$py" -c 'import importlib.util, sys; sys.exit(importlib.util.find_spec("torch") is None)' 2>/dev/null; then
123 echo "OK torch importable by PYTHON=$py"
124 else
125 echo "MISSING torch for PYTHON=$py (set PYTHON to an interpreter with torch)"; missing=1
126 fi
127 if [ -e "$out" ]; then echo "EXISTS $out (device mode refuses to overwrite evidence)"; missing=1; fi
128 echo "ROWS $n (3 cubin + 1 conformance + $pairs paired + 1 torch); GPU rows NOT_RUN"
129 if [ "$missing" = 1 ]; then echo "DRY-RUN INCOMPLETE: build missing inputs before the window"; exit 1; fi
130 echo "DRY-RUN PASS: plan complete, no device opened"
131 exit 0
132fi
133
134# Evidence is additions only: a second run under the same ledger id never overwrites the first.
135# mkdir (no -p) is the check: it creates the directory or fails, in one step, so of two
136# concurrent runs with the same new ledger id exactly one gets past this line
137# (control: tests/device-window/race.om). This file is existing shell kept as a
138# migration; its Omega successor is tools/run-device-window.om (Linear DRA-441), and
139# this file is removed once a leased window run by the successor matches its rows.
140mkdir -p "$here/evidence/device-window"
141if ! mkdir "$out" 2>/dev/null; then
142 if [ -e "$out" ]; then
143 echo "REFUSE: $out exists; evidence is never overwritten, use a new --ledger id" >&2
144 else
145 echo "REFUSE: cannot create $out" >&2
146 fi
147 exit 2
148fi
149: >"$out/rc.txt"
150opened=0 wid=""
151trap '[ "$opened" = 1 ] && spark-window close "$wid" || true' EXIT
152trap 'exit 130' INT TERM
153if [ -z "${SPARK_WINDOW:-}" ]; then
154 wid=$(spark-window open "$name" --ledger "$ledger" --gb10 exclusive --quiet none --lease 20 --wait) ||
155 { echo "REFUSE: spark-window open failed; no row ran" >&2; exit 3; }
156 opened=1
157else
158 wid=$SPARK_WINDOW
159fi
160{
161 echo "window_id $wid"
162 echo "opened_here $opened"
163 echo "host $(hostname)"
164 echo "start_utc $(date -u +%Y-%m-%dT%H:%M:%SZ)"
165 echo "bridge_head $(git -C "$here" rev-parse HEAD 2>/dev/null || echo unknown)"
166 echo "bridge_dirty $(git -C "$here" status --porcelain --untracked-files=no 2>/dev/null | wc -l)"
167 echo "cudapp_head $(git -C "$cudapp" rev-parse HEAD 2>/dev/null || echo unknown)"
168 echo "script_sha256 $(sha256sum < "$0" | cut -d' ' -f1)"
169 for f in "$cudapp/build/cubin_probe" "$mm_cubin" "$gqa_cubin" "$stale_cubin" "$torchlib" "$cudapp/build/libomega_gpu.a"; do
170 echo "input_sha256 $(sha256sum < "$f" | cut -d' ' -f1) $(basename "$(dirname "$f")")/$(basename "$f")"
171 done
172 echo "pairs $pairs"
173} >"$out/window.txt"
174mkdir -p "$out/cubins"
175cp "$cubins"/matmul-16x16x8.* "$cubins"/attention-hd128-gqa8.* "$out/cubins/"
176plan
177echo "end_utc $(date -u +%Y-%m-%dT%H:%M:%SZ)" >>"$out/window.txt"
178[ "$fail" = 0 ] && echo "DEVICE WINDOW $ledger: all $n rows rc=0" || echo "DEVICE WINDOW $ledger: FAIL (see $out/rc.txt)"
179exit "$fail"