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 / tests / amd-kfd / run.sh

146 lines · 9216 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3# Host data-layer gate. Production decisions/serialization are seeded Omega; C and Python here
4# are independent test oracles only (PROVENANCE.md). No device is opened, not even for discovery.
5# usage: sh tests/amd-kfd/run.sh /absolute/path/to/pinned/omc
6# Optional: AMD_KFD_OUT (keep output), KFD_CC (native C oracle), KFD_X86_CC (cross C oracle),
7# KFD_OBJCOPY (defaults llvm-objcopy-18), KFD_MC (llvm-mc-18), KFD_READELF (llvm-readelf-18).
8# Same exit meanings as multitarget/run.sh: 0 PASS (NOT_RUN listed); 1 FAIL; 64 usage;
9# 69 pin refused; 74 I/O; 77 NOT_RUN required tool or Linux AArch64 unavailable.
10set -u
11LC_ALL=C
12export LC_ALL
13[ "$#" -eq 1 ] || { echo 'usage: run.sh /absolute/path/to/pinned-omc' >&2; exit 64; }
14omc=$1
15case $omc in /*) ;; *) echo 'usage: omc path must be absolute' >&2; exit 64 ;; esac
16for tool in dirname sed uname basename sha256sum python3; do
17 command -v "$tool" >/dev/null 2>&1 || { echo "NOT_RUN required tool missing: $tool"; exit 77; }
18done
19root=$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd) || exit 74
20# Use the existing gate as the one source of this repository's compiler digest.
21omc_pin=$(sed -n 's/^omc_pin=//p' "$root/tests/multitarget/run.sh")
22[ "${#omc_pin}" -eq 64 ] || { echo 'FAIL: cannot read repository compiler pin'; exit 1; }
23digest() { d=$(sha256sum < "$1") || return 1; printf '%s\n' "${d%% *}"; }
24[ -f "$omc" ] && [ -x "$omc" ] || { echo 'NOT_RUN pinned omc missing/not executable'; exit 77; }
25python3 -I "$root/tests/amd-kfd/control.py" compare digest "$omc" "$omc_pin" || { echo "REFUSED: compiler digest differs from pin $omc_pin" >&2; exit 69; }
26# Digest is verified before any compiler invocation, other oracle, or output-directory creation.
27if [ "$(uname -s)" != Linux ] || [ "$(uname -m)" != aarch64 ]; then
28 echo 'NOT_RUN: the amd-kfd host gate needs Linux AArch64 and the pinned seeded omc'; exit 77
29fi
30cc=${KFD_CC:-cc}; objcopy=${KFD_OBJCOPY:-llvm-objcopy-18}
31for tool in "$cc" "$objcopy" python3 cat cmp chmod mkdir mktemp timeout od head tail grep; do
32 command -v "$tool" >/dev/null 2>&1 || { echo "NOT_RUN required tool missing: $tool"; exit 77; }
33done
34[ -f /usr/include/linux/kfd_ioctl.h ] || { echo 'NOT_RUN native KFD header missing'; exit 77; }
35cd "$root" || exit 74
36out=${AMD_KFD_OUT:-$(mktemp -d "${TMPDIR:-/tmp}/amd-kfd.XXXXXX")} || exit 74
37mkdir -p "$out" || exit 74
38python3 -I "$root/tests/amd/receipt.py" write "$root" "$out" || exit 1
39npass=0; nfail=0; nnr=0
40ok() { npass=$((npass + 1)); echo "PASS $*"; }
41bad() { nfail=$((nfail + 1)); echo "FAIL $*"; }
42notrun() { nnr=$((nnr + 1)); echo "NOT_RUN $*"; }
43echo "PIN omc $omc_pin (from tests/multitarget/run.sh)"
44echo "Outputs: $out"
45echo "ORACLE native-header sha256=$(digest /usr/include/linux/kfd_ioctl.h)"
46
47# Python oracles run with -I: PYTHON* variables (PYTHONOPTIMIZE strips asserts) are ignored.
48# A fake compiler leaves a marker if it is ever executed. The recursive gate must refuse its
49# digest before running it or creating its requested outputs. No alternate pin is injectable.
50printf '#!/bin/sh\nprintf executed > "%s"\nexit 0\n' "$out/WRONG-COMPILER-EXECUTED" > "$out/wrong-omc" || exit 74
51chmod 700 "$out/wrong-omc" || exit 74
52wrong=$(CDPATH= cd -- "$out" && pwd)/wrong-omc
53AMD_KFD_OUT="$out/wrong-pin-output" sh tests/amd-kfd/run.sh "$wrong" > "$out/wrong-pin.log" 2>&1
54rc=$?
55if [ "$rc" -eq 69 ] && [ ! -e "$out/WRONG-COMPILER-EXECUTED" ] && [ ! -e "$out/wrong-pin-output" ]; then
56 python3 -I "$root/tests/amd-kfd/control.py" prove "$out" wrong-compiler-digest digest "$wrong" "$omc_pin" || exit 1
57 ok 'wrong compiler refused before invocation or output creation'
58else bad "wrong-compiler-digest exit=$rc"; fi
59
60for u in aql kernarg records ring; do
61 cat "backend/amd/kfd/$u.om" backend/amd/kfd/cli.om > "$out/$u.source.om" || exit 74
62 for suffix in '' .repeat; do
63 timeout 120 "$omc" < "$out/$u.source.om" > "$out/$u$suffix" 2> "$out/$u$suffix.compile.err"
64 rc=$?
65 if [ "$rc" -ne 0 ] || [ -s "$out/$u$suffix.compile.err" ] || [ ! -s "$out/$u$suffix" ]; then
66 cat "$out/$u$suffix.compile.err"; echo "AMD-KFD-HOST FAIL compile=$u$suffix exit=$rc"; exit 1
67 fi
68 chmod 700 "$out/$u$suffix" || exit 74
69 done
70 if cmp -s "$out/$u" "$out/$u.repeat"; then
71 ok "deterministic build $u sha256=$(digest "$out/$u")"
72 else bad "nonidentical builds $u"; fi
73done
74
75# Live C system-header oracle: sizeof, offsetof, request macros. Test-only, no device calls.
76"$cc" -std=c11 -O2 -Wall -Wextra -Werror tests/amd-kfd/layout-oracle.c -o "$out/native-layout" > "$out/native-build.log" 2>&1
77rc=$?
78if [ "$rc" -ne 0 ]; then cat "$out/native-build.log"; echo "AMD-KFD-HOST FAIL native oracle build exit=$rc"; exit 1; fi
79"$out/native-layout" > "$out/native-table.tsv" || exit 1
80"$objcopy" --dump-section ".kfdlayout=$out/native-layout.bin" "$out/native-layout" || exit 1
81xc=${KFD_X86_CC:-x86_64-linux-gnu-gcc}
82if [ -d /usr/x86_64-linux-gnu/include ] && [ -f /usr/x86_64-linux-gnu/include/linux/kfd_ioctl.h ] && command -v "$xc" >/dev/null 2>&1; then
83 echo "ORACLE x86-64-header sha256=$(digest /usr/x86_64-linux-gnu/include/linux/kfd_ioctl.h)"
84 "$xc" -std=c11 -O2 -Wall -Wextra -Werror -I/usr/x86_64-linux-gnu/include -c tests/amd-kfd/layout-oracle.c -o "$out/x86-layout.o" > "$out/x86-build.log" 2>&1
85 rc=$?
86 if [ "$rc" -ne 0 ]; then cat "$out/x86-build.log"; bad "x86-64 header oracle build exit=$rc"
87 elif python3 -I -c 'import pathlib,sys; assert pathlib.Path(sys.argv[1]).read_bytes()[18:20] == bytes([62,0])' "$out/x86-layout.o" \
88 && "$objcopy" --dump-section ".kfdlayout=$out/x86-layout.bin" "$out/x86-layout.o" \
89 && cmp -s "$out/native-layout.bin" "$out/x86-layout.bin"; then
90 ok 'x86-64 cross-compiled header layout: all requests/sizes/offsets/widths equal native table (no x86 execution claimed)'
91 else bad 'x86-64 header values/layout differ or compiler not targeting x86-64'; fi
92else notrun 'x86-64 layout: cross header tree or x86-64 compiler missing'; fi
93
94# Establish the assembly oracle's code-object version without linking or launching it.
95mc=${KFD_MC:-llvm-mc-18}; re=${KFD_READELF:-llvm-readelf-18}
96if command -v "$mc" >/dev/null 2>&1 && command -v "$re" >/dev/null 2>&1; then
97 "$mc" --version | head -1
98 "$mc" -triple amdgcn-amd-amdhsa -mcpu=gfx1151 -filetype=obj backend/amd/vadd-gfx1151.s -o "$out/vadd-oracle.o" > "$out/asm.log" 2>&1
99 rc=$?
100 if [ "$rc" -eq 0 ] && "$re" -h --notes "$out/vadd-oracle.o" > "$out/vadd-oracle.readelf" \
101 && grep -q 'ABI Version: *3$' "$out/vadd-oracle.readelf" \
102 && grep -q 'gfx1151' "$out/vadd-oracle.readelf" \
103 && grep -q '.kernarg_segment_size: *28$' "$out/vadd-oracle.readelf"; then
104 ok 'assembly oracle ELF ABI 3 = code object v5; metadata declares kernarg size 28'
105 else cat "$out/asm.log"; bad "assembly oracle code-object version/metadata exit=$rc"; fi
106else notrun 'assembly ELF version: llvm-mc/llvm-readelf missing (source metadata still checked)'; fi
107
108# KFD cannot currently be *issued* by the pinned compiler: retain compile-only minimal
109# reproductions. The data layer itself uses no devctl. Never run a refused compiler output.
110python3 -I - "$out/native-table.tsv" "$out" <<'PY'
111import pathlib, sys
112rows = pathlib.Path(sys.argv[1]).read_text().splitlines()
113out = pathlib.Path(sys.argv[2])
114i = 0
115while i < len(rows):
116 nums, name = rows[i].split('#', 1)
117 req, size, count = map(int, nums.split())
118 if req:
119 (out / ('allowlist-' + name.strip() + '.om')).write_text(
120 f'global b uchar {size}\nfn main to int\nr = devctl neg int 1 uint {req} b 0 {size}\nret 0\nend\n')
121 i += count + 1
122PY
123for src in "$out"/allowlist-*.om; do
124 timeout 120 "$omc" < "$src" > "$src.refused-output" 2> "$src.err"
125 rc=$?
126 if [ "$rc" -eq 9 ]; then ok "allowlist compile-only refusal $(basename "$src" .om) exit=9"
127 else bad "allowlist expected refusal $(basename "$src" .om) exit=$rc"; fi
128done
129python3 -I tests/amd-kfd/oracle.py . "$out" "$omc" > "$out/byte-oracle.log" 2>&1
130rc=$?
131cat "$out/byte-oracle.log"
132if [ "$rc" -eq 0 ]; then ok 'independent byte-oracle suite'; else bad "independent byte-oracle exit=$rc"; fi
133set -- "$root/backend/amd/kfd/requests.tsv"
134for t in "$out"/*.tsv; do [ "$(basename "$t")" = field-count-over-fuel.tsv ] || set -- "$@" "$t"; done
135if sh "$root/tests/amd/loop-fuel.sh" "$@" > "$out/loop-fuel.log"; then
136 ok "loop-fuel alarm quiet on the $# KFD tables the gate feeds (threshold 500000 bytes)"
137else cat "$out/loop-fuel.log"; bad 'loop-fuel alarm fired'; fi
138if sh "$root/tests/amd/loop-fuel.sh" "$out/field-count-over-fuel.tsv" > "$out/loop-fuel-control.log"; then
139 bad 'loop-fuel alarm control escaped on the over-fuel table'
140else ok 'loop-fuel alarm control CAUGHT the over-fuel table that traps 104'; fi
141notrun 'AMD gfx942 hardware: KFD device calls, queue creation, mapped memory, AQL dispatch, completion and vadd execution'
142notrun 'MI300X model loading, Qwen 27B inference, speed and hardware coherency'
143if [ "$nfail" -ne 0 ]; then echo "AMD-KFD-HOST FAIL pass=$npass fail=$nfail not_run=$nnr"; exit 1; fi
144python3 -I "$root/tests/amd/receipt.py" check "$root" "$out" || exit 1
145echo "AMD-KFD-HOST PASS pass=$npass fail=0 not_run=$nnr"
146exit 0