interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / tools / extract / drift.sh

150 lines · 9358 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3# drift.sh: fails when Omega's copy of a GPU file moved away from CUDA++ after the cutover.
4#
5# tools/extract/drift.sh OMEGA_DIR OMEGA_REF [BASELINE]
6# tools/extract/drift.sh --self-test OMEGA_DIR OMEGA_REF [BASELINE]
7#
8# CUDA++ is the source of truth for the GPU stack (aien-dev/CUDApp); omega-mv's GPU files are superseded.
9# BASELINE (default tools/extract/drift-baseline.tsv: omega_path, omega_blob) records Omega's blob of every
10# GPU path at the cutover commit. A path DRIFTS when, at OMEGA_REF,
11# - Omega holds it, and
12# - Omega's blob differs from CUDA++'s blob at HEAD (map.sh status differs or omega-only), and
13# - Omega's blob is not the one recorded in BASELINE (a new path counts: it has no baseline blob).
14# So the differences that existed at the cutover stay quiet, a GPU edit landed in Omega but not in CUDA++
15# is red, and porting it to CUDA++ (same blob) turns it green again.
16# Output: one "DRIFT path status omega_blob" line per drifting path, a "DIVERGENCE" summary of the whole map
17# (blob compare; `differs` split into license-header-only and code), then "DRIFT-GATE: PASS|FAIL (...)".
18# Exit 0 on PASS, 1 on drift, 2 on a broken setup.
19#
20# --self-test runs negative controls that must all come out red, then the real check:
21# 1. baseline control: the baseline blob of one cutover-time `differs` path is replaced; the gate must name it.
22# 2. edit control: in a scratch clone of OMEGA_DIR a commit appends one line to a GPU source whose Omega blob
23# is still the baseline one (preferring one `same` in both repos); the gate must name that file (other DRIFT
24# lines, from real drift at OMEGA_REF, may appear too; the real check below fails on those).
25# 3. each Ampere numeric cutover source/header is changed in a scratch Omega commit and must be named.
26# A gate that stays green under either control is broken and the self-test exits 1.
27set -euo pipefail
28export LC_ALL=C
29
30selftest=0
31if [[ ${1:-} == --self-test ]]; then selftest=1; shift; fi
32omega=${1:?omega dir}; oref=${2:?omega ref}
33here=$(cd "$(dirname "$0")" && pwd)
34baseline=${3:-$here/drift-baseline.tsv}
35[[ -f $baseline && -r $baseline && -s $baseline ]] || { echo "DRIFT-GATE: NOT_RUN (baseline $baseline is not a readable nonempty file)"; exit 2; }
36
37scratch=$(mktemp -d); trap 'rm -rf "$scratch"' EXIT
38cudapp=$(git -C "$here" rev-parse --show-toplevel)
39
40# summary MAP OMEGA_DIR: one DIVERGENCE line. Shared = path present in both repos. A `differs` file is
41# header-only when every changed line is a licence-header line (the SPDX/Apache boilerplate).
42summary() {
43 local map=$1 dir=$2 hdr=0 code=0 op cp st ob cb rc kind
44 while IFS=$'\t' read -r op cp st ob cb; do
45 [[ $st == differs ]] || continue
46 # Process substitutions hide cat-file failures. Materialize both blobs and
47 # the whole diff: status 1 means different, not a pipeline failure, and no
48 # early-exit consumer can give diff a broken pipe.
49 git -C "$dir" cat-file blob "$ob" > "$scratch/omega-blob" || return 2
50 git -C "$cudapp" cat-file blob "$cb" > "$scratch/cudapp-blob" || return 2
51 rc=0
52 diff "$scratch/omega-blob" "$scratch/cudapp-blob" > "$scratch/changes" || rc=$?
53 if [[ $rc -ne 1 ]]; then
54 echo "summary: expected differing blobs for $op, diff returned $rc" >&2
55 return 2
56 fi
57 kind=$(awk '/^[<>]/ { changed=1
58 if (tolower($0) !~ /spdx|copyright|licen[cs]e|apache|llvm|applicable law|as is|you may not use|obtain a copy|distributed under|without warranties|limitations under|^[<>] *(\/\/|#|\/?\*|\*\/)? *$/) code=1 }
59 END { print (changed && !code ? "header" : "code") }' "$scratch/changes") || return 2
60 if [[ $kind == code ]]; then code=$((code + 1)); else hdr=$((hdr + 1)); fi
61 done < "$map"
62 awk -F'\t' -v hdr="$hdr" -v code="$code" 'FNR > 1 { n[$3]++ }
63 END { sh = n["same"] + n["differs"]
64 printf "DIVERGENCE: %d shared, %d same, %d differ (%d licence-header only, %d code); %d cudapp-only, %d omega-only\n",
65 sh, n["same"], n["differs"], hdr, code, n["cudapp-only"], n["omega-only"] }' "$map"
66}
67# check OMEGA_DIR OMEGA_REF BASELINE: returns 0 PASS / 1 FAIL / 2 broken setup.
68check() {
69 local dir=$1 ref=$2 base=$3 map=$scratch/map.$RANDOM
70 "$here/map.sh" "$dir" "$ref" HEAD > "$map" || { echo "DRIFT-GATE: NOT_RUN (map failed)"; return 2; }
71 awk -F'\t' 'NR==FNR { if ($1 !~ /^#/) b[$1]=$2; next }
72 FNR==1 { next }
73 $4 != "-" && ($3 == "differs" || $3 == "omega-only") && b[$1] != $4 {
74 printf "DRIFT\t%s\t%s\t%s\n", $1, $3, $4 }' "$base" "$map" > "$map.drift" \
75 || { echo "DRIFT-GATE: NOT_RUN (baseline comparison failed)"; return 2; }
76 cat "$map.drift"
77 summary "$map" "$dir" || { echo "DRIFT-GATE: NOT_RUN (summary failed)"; return 2; }
78 local n rows
79 n=$(wc -l < "$map.drift"); rows=$(($(wc -l < "$map") - 1))
80 if [[ $n -eq 0 ]]; then
81 echo "DRIFT-GATE: PASS ($rows GPU paths, omega $(git -C "$dir" rev-parse --short=12 "$ref^{commit}"), none moved away from CUDA++ since the baseline)"
82 return 0
83 fi
84 echo "DRIFT-GATE: FAIL ($n of $rows GPU paths changed in omega $(git -C "$dir" rev-parse --short=12 "$ref^{commit}") and not in CUDA++)"
85 return 1
86}
87
88if [[ $selftest -eq 0 ]]; then
89 if check "$omega" "$oref" "$baseline"; then exit 0; else exit $?; fi
90fi
91
92fail=0
93"$here/map.sh" "$omega" "$oref" HEAD > "$scratch/now" || { echo "DRIFT-GATE: NOT_RUN (map failed)"; exit 2; }
94
95# Control 1: baseline blob of one cutover-time `differs` path replaced.
96victim=$(awk -F'\t' 'NR==FNR { if ($1 !~ /^#/) b[$1]=$2; next } FNR>1 && $3=="differs" && b[$1]==$4 { print $1; exit }' \
97 "$baseline" "$scratch/now")
98if [[ -z $victim ]]; then echo "control 1: NOT_RUN (no differs path recorded in the baseline)"; fail=1
99else
100 awk -F'\t' -v OFS='\t' -v v="$victim" '$1==v { $2="0000000000000000000000000000000000000000" } 1' "$baseline" > "$scratch/base1"
101 rc=0; out=$(check "$omega" "$oref" "$scratch/base1") || rc=$?
102 if [[ $rc -eq 2 ]]; then echo "$out"; exit 2; fi
103 if grep -qxF "DRIFT $victim differs $(awk -F'\t' -v v="$victim" '$1==v {print $4}' "$scratch/now")" <<< "$out" \
104 && grep -q '^DRIFT-GATE: FAIL' <<< "$out"; then echo "control 1: red as required ($victim)"
105 else echo "control 1: BROKEN, gate stayed green with a tampered baseline for $victim"; echo "$out"; fail=1; fi
106fi
107
108# Control 2: a real Omega commit that edits one GPU source still at its cutover blob.
109target=$(awk -F'\t' 'NR==FNR { if ($1 !~ /^#/) b[$1]=$2; next }
110 FNR>1 && $1 ~ /^src\/omega_gpu_.*\.c$/ && b[$1]==$4 && ($3=="same" || $3=="differs") { if ($3=="same") { print $1; exit } else if (!d) d=$1 }
111 END { if (d) print d }' "$baseline" "$scratch/now" | head -1)
112if [[ -z $target ]]; then echo "control 2: NOT_RUN (no src/omega_gpu_*.c still at its baseline blob)"; fail=1
113else
114 git clone -q --shared --no-checkout "$omega" "$scratch/om"
115 git -C "$scratch/om" checkout -q --detach "$(git -C "$omega" rev-parse "$oref^{commit}")"
116 echo "/* drift.sh self-test edit */" >> "$scratch/om/$target"
117 git -C "$scratch/om" -c user.name=drift-selftest -c user.email=drift@selftest.invalid commit -q -am "drift self-test"
118 rc=0; out=$(check "$scratch/om" HEAD "$baseline") || rc=$?
119 if [[ $rc -eq 2 ]]; then echo "$out"; exit 2; fi
120 if [[ $(grep -c '^DRIFT ' <<< "$out" || true) -ge 1 ]] && grep -q "^DRIFT $target differs " <<< "$out" \
121 && grep -q '^DRIFT-GATE: FAIL' <<< "$out"; then echo "control 2: red as required ($target edited in a scratch Omega commit)"
122 else echo "control 2: BROKEN, gate stayed green after an Omega edit to $target"; echo "$out"; fail=1; fi
123fi
124
125# Control 3: pinning an intentional x86 fork must not hide subsequent edits to
126# any retained Omega numeric CPU-oracle source or public header.
127for numeric in src/omega_numeric.c src/omega_numeric.h src/omega_numeric_reduce.c src/omega_numeric_reduce.h; do
128 if [[ ! -d $scratch/om/.git ]]; then
129 git clone -q --shared --no-checkout "$omega" "$scratch/om"
130 git -C "$scratch/om" checkout -q --detach "$(git -C "$omega" rev-parse "$oref^{commit}")"
131 fi
132 if ! git -C "$omega" cat-file -e "$oref:$numeric" 2>/dev/null; then
133 echo "control 3: $numeric is not in $oref (fixture Omega); this row is not exercised"; continue
134 fi
135 echo "/* Ampere numeric drift mutant */" >> "$scratch/om/$numeric"
136 git -C "$scratch/om" -c user.name=drift-selftest -c user.email=drift@selftest.invalid commit -q -am "numeric cutover drift self-test"
137 out=$(check "$scratch/om" HEAD "$baseline" || true)
138 blob=$(git -C "$scratch/om" rev-parse "HEAD:$numeric")
139 if grep -qxF "DRIFT $numeric differs $blob" <<< "$out" && grep -q '^DRIFT-GATE: FAIL' <<< "$out"; then
140 echo "control 3: red as required ($numeric changed beyond its explicit cutover pin)"
141 else
142 echo "control 3: BROKEN, gate hid a numeric source/header change: $numeric"
143 echo "$out"; fail=1
144 fi
145done
146
147echo "real check:"
148check "$omega" "$oref" "$baseline" || { rc=$?; [[ $rc -ne 2 ]] || exit 2; fail=1; }
149[[ $fail -eq 0 ]] && echo "SELF-TEST: PASS (all controls red, real check green)" || echo "SELF-TEST: FAIL"
150exit "$fail"