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
CUDApp / tests / test_chip_helpers.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | """Host-only chip-helper checks: every child is a dummy, every lock is temporary.""" |
| 4 | import fcntl |
| 5 | import os |
| 6 | from pathlib import Path |
| 7 | import shutil |
| 8 | import subprocess |
| 9 | import tempfile |
| 10 | import unittest |
| 11 | |
| 12 | |
| 13 | ROOT = Path(os.environ.get("CUDAPP_CHIP_HELPER_ROOT", Path(__file__).resolve().parents[1])) |
| 14 | |
| 15 | |
| 16 | class ChipHelperTests(unittest.TestCase): |
| 17 | def setUp(self): |
| 18 | self.tmp = tempfile.TemporaryDirectory(prefix="cudapp-chip-helpers-") |
| 19 | self.addCleanup(self.tmp.cleanup) |
| 20 | self.work = Path(self.tmp.name) |
| 21 | self.calls = self.work / "calls" |
| 22 | self.lock = self.work / "lock" |
| 23 | self.out = self.work / "evidence" |
| 24 | self.out.mkdir() |
| 25 | self.home = self.work / "home" |
| 26 | repo = self.home / "workspace/omega-gpu-channels" |
| 27 | base = self.home / "workspace/omega-gpu-channels-base/build" |
| 28 | (repo / "build").mkdir(parents=True) |
| 29 | base.mkdir(parents=True) |
| 30 | subprocess.run(["git", "init", "-q", str(repo)], check=True) |
| 31 | subprocess.run(["git", "-C", str(repo), "-c", "user.name=Test", "-c", |
| 32 | "user.email=test@example.invalid", "commit", "--allow-empty", |
| 33 | "-qm", "host fixture"], check=True) |
| 34 | self.env = dict(os.environ, HOME=str(self.home), LOCK=str(self.lock), |
| 35 | CALLS=str(self.calls), REAL_FLOCK=shutil.which("flock") or "", |
| 36 | CHILD_MODE="pass", AIEN_OMEGA_GPU_CHANNELS="unexpected") |
| 37 | self.assertTrue(self.env["REAL_FLOCK"], "flock is required") |
| 38 | dummy = '''#!/bin/sh |
| 39 | held=locked |
| 40 | if "$REAL_FLOCK" -n "$LOCK" true; then held=unlocked; fi |
| 41 | printf '%s %s %s\\n' "$(basename "$0")" "${AIEN_OMEGA_GPU_CHANNELS-unset}" "$held" >> "$CALLS" |
| 42 | case "$CHILD_MODE" in |
| 43 | fail) echo 'dummy child failure'; exit 37 ;; |
| 44 | late) if [ "${AIEN_OMEGA_GPU_CHANNELS-unset}" = 2 ]; then |
| 45 | echo 'dummy child failure'; exit 37 |
| 46 | fi ;; |
| 47 | silent) echo 'dummy child without a verdict'; exit 0 ;; |
| 48 | pass) echo 'C4 streams chip: PASS (dummy host test, not GPU evidence)' ;; |
| 49 | esac |
| 50 | ''' |
| 51 | for folder in (repo / "build", base): |
| 52 | for name in ("gpu_streams_test", "gpu_q38_graph_test", "gpu_cmdlist_test", |
| 53 | "gpu_decode_graph_test", "gpu_q38_channels_test"): |
| 54 | path = folder / name |
| 55 | path.write_text(dummy) |
| 56 | path.chmod(0o755) |
| 57 | self.env["BIN"] = str(repo / "build/gpu_streams_test") |
| 58 | |
| 59 | def run_helper(self, name, *args, **env): |
| 60 | return subprocess.run(["sh", str(ROOT / "tools" / name), str(self.out), *args], |
| 61 | env=dict(self.env, **env), text=True, capture_output=True, |
| 62 | timeout=15) |
| 63 | |
| 64 | def invocations(self): |
| 65 | return self.calls.read_text().splitlines() if self.calls.exists() else [] |
| 66 | |
| 67 | def test_c4_refuses_zero_without_running_a_child(self): |
| 68 | result = self.run_helper("c4_streams_chip.sh", "0") |
| 69 | self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr) |
| 70 | self.assertEqual(self.invocations(), []) |
| 71 | |
| 72 | def test_c4_refuses_malformed_or_unrepresentable_counts(self): |
| 73 | for count in ("", "00", "-1", "abc", "1.5", "999999999999999999999999999"): |
| 74 | with self.subTest(count=count): |
| 75 | self.calls.unlink(missing_ok=True) |
| 76 | result = self.run_helper("c4_streams_chip.sh", count) |
| 77 | self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr) |
| 78 | self.assertEqual(self.invocations(), []) |
| 79 | |
| 80 | def test_c4_runs_the_requested_number_under_the_lock(self): |
| 81 | result = self.run_helper("c4_streams_chip.sh", "2") |
| 82 | self.assertEqual(result.returncode, 0, result.stdout + result.stderr) |
| 83 | self.assertEqual(self.invocations(), ["gpu_streams_test unexpected locked"] * 2) |
| 84 | self.assertIn("runs passing: 2 of 2", (self.out / "summary.txt").read_text()) |
| 85 | |
| 86 | def test_c4_keeps_the_default_count_when_omitted(self): |
| 87 | result = self.run_helper("c4_streams_chip.sh") |
| 88 | self.assertEqual(result.returncode, 0, result.stdout + result.stderr) |
| 89 | self.assertEqual(len(self.invocations()), 10) |
| 90 | |
| 91 | def test_c4_rejects_child_failure_and_missing_verdict(self): |
| 92 | for mode in ("fail", "silent"): |
| 93 | with self.subTest(mode=mode): |
| 94 | result = self.run_helper("c4_streams_chip.sh", "1", CHILD_MODE=mode) |
| 95 | self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr) |
| 96 | |
| 97 | def test_l136_holds_the_lock_for_every_child(self): |
| 98 | result = self.run_helper("l136_channel_gates.sh") |
| 99 | self.assertEqual(result.returncode, 0, result.stdout + result.stderr) |
| 100 | self.assertEqual(len(self.invocations()), 11) |
| 101 | self.assertTrue(all(line.endswith(" locked") for line in self.invocations()), |
| 102 | self.invocations()) |
| 103 | self.assertEqual([line.split()[1] for line in self.invocations()], |
| 104 | ["unset"] * 9 + ["2", "2"]) |
| 105 | |
| 106 | def test_l136_records_failure_and_stops_with_the_child_status(self): |
| 107 | result = self.run_helper("l136_channel_gates.sh", CHILD_MODE="fail") |
| 108 | self.assertEqual(result.returncode, 37, result.stdout + result.stderr) |
| 109 | self.assertEqual(len(self.invocations()), 1) |
| 110 | summary = (self.out / "summary.txt").read_text() |
| 111 | self.assertIn("g1-base-q38-l4 rc=37 dummy child failure", summary) |
| 112 | self.assertNotIn("done", summary) |
| 113 | |
| 114 | def test_l136_propagates_a_late_channel_child_failure(self): |
| 115 | result = self.run_helper("l136_channel_gates.sh", CHILD_MODE="late") |
| 116 | self.assertEqual(result.returncode, 37, result.stdout + result.stderr) |
| 117 | self.assertEqual(len(self.invocations()), 10) |
| 118 | summary = (self.out / "summary.txt").read_text() |
| 119 | self.assertIn("g2-l4 rc=37 dummy child failure", summary) |
| 120 | self.assertNotIn("done", summary) |
| 121 | |
| 122 | def test_contended_lock_prevents_any_child(self): |
| 123 | # Keep real flock semantics; shorten only the wait to make contention |
| 124 | # deterministic without waiting for the production wait budget. |
| 125 | shimdir = self.work / "bin" |
| 126 | shimdir.mkdir() |
| 127 | shim = shimdir / "flock" |
| 128 | shim.write_text('''#!/bin/sh |
| 129 | if [ "$1" = -w ]; then shift; shift; exec "$REAL_FLOCK" -n "$@"; fi |
| 130 | exec "$REAL_FLOCK" "$@" |
| 131 | ''') |
| 132 | shim.chmod(0o755) |
| 133 | with self.lock.open("a") as holder: |
| 134 | fcntl.flock(holder, fcntl.LOCK_EX | fcntl.LOCK_NB) |
| 135 | for name in ("c4_streams_chip.sh", "l136_channel_gates.sh"): |
| 136 | with self.subTest(helper=name): |
| 137 | result = self.run_helper(name, *(["1"] if name.startswith("c4") else []), |
| 138 | PATH=str(shimdir) + os.pathsep + os.environ["PATH"]) |
| 139 | self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr) |
| 140 | self.assertEqual(self.invocations(), []) |
| 141 | |
| 142 | |
| 143 | if __name__ == "__main__": |
| 144 | unittest.main() |