gpu-head-to-head
aien-dev/gpu-head-to-head
Brought by drakestapleton
gpu-head-to-head / bench / test-run-arm.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Exercise the real shell cell function with CPU-only instrument substitutes.""" |
| 3 | import json |
| 4 | import os |
| 5 | from pathlib import Path |
| 6 | import shlex |
| 7 | import subprocess |
| 8 | import tempfile |
| 9 | import unittest |
| 10 | |
| 11 | HERE = Path(__file__).resolve().parent |
| 12 | |
| 13 | class CellGateTests(unittest.TestCase): |
| 14 | def run_cell(self, *, quiet=0, client=0, monitor=0, summary=True, offenders=False, |
| 15 | contaminated=False, timeout=False, missing_samples=False, invalid_samples=False, elapsed=100): |
| 16 | source = (HERE / 'run_arm.sh').read_text() |
| 17 | function = source[source.index('cell_status() {'):source.index('# ---- session ----')] |
| 18 | with tempfile.TemporaryDirectory() as tmp: |
| 19 | root = Path(tmp) |
| 20 | (root / 'mem.tsv').write_text('' if missing_samples else '100 1000 100 0 %s\n' % ('NA' if invalid_samples else '5' if contaminated else '99')) |
| 21 | if timeout: |
| 22 | (root / 'TIMEOUT').touch() |
| 23 | summary_row = {'summary': True, 'elapsed_ticks': elapsed, 'offenders': |
| 24 | [{'pid': 42, 'cpu': 5, 'ticks': 99}] if offenders else []} |
| 25 | monitor_text = json.dumps(summary_row) + '\n' if summary else '' |
| 26 | prefix = 'source %s\n' % shlex.quote(str(HERE / 'session-lifecycle.sh')) + '''set -u |
| 27 | ROOT=%s |
| 28 | LOGD=$ROOT REPO=$ROOT RESULTS=$ROOT OUT=$ROOT/rows.jsonl |
| 29 | ARM=CONTROL ROUND=R0 PORT=1 MID=fixture |
| 30 | MON_CPU=2 CLI_CPUS=0-1 CLI_NICE=0 SRV_CPUS=5 SRV_LIST=' 5 ' |
| 31 | CELLS_COMPLETED=0 |
| 32 | wait_quiet() { return %d; } |
| 33 | mytree() { echo $$; } |
| 34 | date() { echo 100; } |
| 35 | sleep() { :; } |
| 36 | kill() { :; } |
| 37 | ts() { echo fixture-time; } |
| 38 | taskset() { |
| 39 | case "$*" in |
| 40 | *coremon.py*) printf '%%s' %s > "$LOGD/coremon/CONTROL-R0-decode-c1-t${try}.jsonl"; return %d ;; |
| 41 | *) echo client >> "$ROOT/client-calls"; return %d ;; |
| 42 | esac |
| 43 | } |
| 44 | ''' % (shlex.quote(tmp), quiet, shlex.quote(monitor_text), monitor, client) |
| 45 | result = subprocess.run(['bash', '-c', prefix + function + '\ncell decode-c1 1 2 decode.jsonl\n'], |
| 46 | capture_output=True, text=True, timeout=5) |
| 47 | calls = (root / 'client-calls').read_text().splitlines() if (root / 'client-calls').exists() else [] |
| 48 | return result.returncode, calls, result.stderr |
| 49 | |
| 50 | def test_clean_cell_passes(self): |
| 51 | rc, calls, err = self.run_cell() |
| 52 | self.assertEqual(rc, 0, err) |
| 53 | self.assertEqual(len(calls), 1) |
| 54 | |
| 55 | def test_quiet_failure_never_runs_client(self): |
| 56 | rc, calls, err = self.run_cell(quiet=1) |
| 57 | self.assertNotEqual(rc, 0, err) |
| 58 | self.assertEqual(calls, []) |
| 59 | |
| 60 | def test_client_failure_is_not_accepted(self): |
| 61 | self.assertNotEqual(self.run_cell(client=7)[0], 0) |
| 62 | |
| 63 | def test_missing_monitor_summary_is_not_accepted(self): |
| 64 | self.assertNotEqual(self.run_cell(summary=False)[0], 0) |
| 65 | |
| 66 | def test_monitor_crash_is_not_accepted(self): |
| 67 | self.assertNotEqual(self.run_cell(monitor=2)[0], 0) |
| 68 | |
| 69 | def test_exhausted_foreign_process_retries_fail(self): |
| 70 | rc, calls, _ = self.run_cell(monitor=1, offenders=True) |
| 71 | self.assertNotEqual(rc, 0) |
| 72 | self.assertEqual(len(calls), 3) |
| 73 | |
| 74 | def test_exhausted_kthread_retries_fail(self): |
| 75 | rc, calls, _ = self.run_cell(contaminated=True) |
| 76 | self.assertNotEqual(rc, 0) |
| 77 | self.assertEqual(len(calls), 3) |
| 78 | |
| 79 | def test_missing_kthread_samples_fail(self): |
| 80 | self.assertNotEqual(self.run_cell(missing_samples=True)[0], 0) |
| 81 | |
| 82 | def test_invalid_kthread_samples_fail(self): |
| 83 | self.assertNotEqual(self.run_cell(invalid_samples=True)[0], 0) |
| 84 | |
| 85 | def test_zero_monitor_duration_fails(self): |
| 86 | self.assertNotEqual(self.run_cell(elapsed=0)[0], 0) |
| 87 | |
| 88 | def test_completion_requires_all_cells_and_no_timeout(self): |
| 89 | source = (HERE / 'run_arm.sh').read_text() |
| 90 | function = source[source.index('mark_complete() {'):source.index('# ---- session ----')] |
| 91 | for count, timed_out, expected in [(0, False, False), (13, False, False), |
| 92 | (14, True, False), (14, False, True)]: |
| 93 | with self.subTest(count=count, timed_out=timed_out), tempfile.TemporaryDirectory() as tmp: |
| 94 | root = Path(tmp) |
| 95 | if timed_out: |
| 96 | (root / 'TIMEOUT').touch() |
| 97 | script = 'LOGD=%s\nCELLS_COMPLETED=%d\n' % (shlex.quote(tmp), count) |
| 98 | result = subprocess.run(['bash', '-c', script + function + '\nmark_complete'], |
| 99 | capture_output=True, timeout=5) |
| 100 | self.assertEqual(result.returncode == 0, expected) |
| 101 | self.assertEqual((root / 'DONE').exists(), expected) |
| 102 | |
| 103 | def test_existing_session_is_not_overwritten(self): |
| 104 | with tempfile.TemporaryDirectory() as tmp: |
| 105 | root = Path(tmp) |
| 106 | log = root / 'logs/OURS-R1' |
| 107 | log.mkdir(parents=True) |
| 108 | sentinel = log / 'DONE' |
| 109 | sentinel.write_text('historical') |
| 110 | env = dict(os.environ, H2H_RESULTS=tmp) |
| 111 | result = subprocess.run(['bash', str(HERE / 'run_arm.sh'), 'OURS', 'R1'], |
| 112 | env=env, capture_output=True, text=True, timeout=5) |
| 113 | self.assertEqual(result.returncode, 2) |
| 114 | self.assertEqual(sentinel.read_text(), 'historical') |
| 115 | |
| 116 | def test_timeout_never_runs_client(self): |
| 117 | rc, calls, _ = self.run_cell(timeout=True) |
| 118 | self.assertNotEqual(rc, 0) |
| 119 | self.assertEqual(calls, []) |
| 120 | |
| 121 | if __name__ == '__main__': |
| 122 | unittest.main() |