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 / amd-kfd-launch / notebook-check.py
| 1 | #!/usr/bin/env python3 |
| 2 | # External CPU measuring harness only; never a launcher dependency. |
| 3 | import hashlib,json,os,platform,shutil,subprocess,sys,tempfile |
| 4 | from pathlib import Path |
| 5 | bundle=Path(sys.argv[1]).resolve(); gate=Path(sys.argv[2]).resolve() |
| 6 | assert not Path('/dev/kfd').exists(), 'CPU gate refuses a host with a KFD device' |
| 7 | qemu=shutil.which('qemu-x86_64'); assert qemu |
| 8 | cell=(bundle/'cell.py').read_text(); records=[] |
| 9 | with tempfile.TemporaryDirectory(prefix='launch4-cpu-') as tmp: |
| 10 | prior=Path.cwd(); os.chdir(tmp) |
| 11 | ns={'__name__':'__notebook_test__'} |
| 12 | exec(compile(cell,'cell.py','exec'),ns) |
| 13 | oldmachine=platform.machine; oldpopen=subprocess.Popen |
| 14 | def emulated(args,*a,**k): |
| 15 | args=list(args) |
| 16 | if str(args[0])==str(ns['BIN']): args=[qemu,*args] |
| 17 | return oldpopen(args,*a,**k) |
| 18 | try: |
| 19 | platform.machine=lambda:'x86_64' |
| 20 | subprocess.Popen=emulated |
| 21 | ns['run']() |
| 22 | finally: |
| 23 | subprocess.Popen=oldpopen; platform.machine=oldmachine |
| 24 | binary=ns['BIN'] |
| 25 | report=Path('amdserve-launch4.txt').read_text() |
| 26 | assert 'argument-header=64 PASS' in report and 'errno=2 exit=77 checked' in report |
| 27 | assert report=='\n'.join(ns['ROWS'])+'\n' |
| 28 | assert report.splitlines()[0].startswith('CONTROLS ') and report.splitlines()[-1]=='RESULT NOT_RUN' |
| 29 | assert 'OMEGA step open_kfd fail errno=2 phase=after detail=0\n' in report |
| 30 | (gate/'notebook-absent-receipt.txt').write_text(report) |
| 31 | records.append({'case':'actual notebook x86 via qemu, absent KFD and refusals','exit':0}) |
| 32 | for name,path in ns['ASSETS'].items(): |
| 33 | if not name.endswith('.hsaco'): continue |
| 34 | for target,command in (('a64',[gate/'launch-a64']),('x86',[qemu,binary])): |
| 35 | command=[*command,'inspect',path,Path(name).stem] |
| 36 | p=subprocess.run(list(map(str,command)),capture_output=True) |
| 37 | assert p.returncode==0,(name,target,p.returncode,p.stdout,p.stderr) |
| 38 | records.append({'case':target+' inspect '+name,'exit':p.returncode,'stdout':p.stdout.decode()}) |
| 39 | # The child holds itself alive until all scheduled read-only proc samples and |
| 40 | # its first stdout line have reached disk. This rejects capture-after-exit. |
| 41 | ns['BIN']=Path(sys.executable) |
| 42 | ns['KFD_PROC_ROOT']=Path(tmp)/'proc-fixture' |
| 43 | ns['SAMPLE_DELAYS']=(0.0,0.01,0.02) |
| 44 | child=r'''import os,pathlib,sys,time |
| 45 | root=pathlib.Path(sys.argv[1])/str(os.getpid()) |
| 46 | (root/'queues'/'7').mkdir(parents=True) |
| 47 | (root/'stats_37820').mkdir() |
| 48 | for path,value in [('queues/7/size','1024'),('queues/7/type','2'),('queues/7/gpuid','37820'),('stats_37820/cu_occupancy','0'),('vram_37820','4096')]: |
| 49 | (root/path).write_text(value) |
| 50 | print('OMEGA streaming-first',flush=True) |
| 51 | print('OMEGA step signal_wait ok errno=0 phase=before detail=1',flush=True) |
| 52 | deadline=time.monotonic()+5 |
| 53 | while True: |
| 54 | receipt=pathlib.Path('amdserve-launch4.txt').read_text() |
| 55 | if 'sample=2 pid='+str(os.getpid()) in receipt and 'OMEGA streaming-first\n' in receipt: |
| 56 | break |
| 57 | if time.monotonic()>deadline: sys.exit(91) |
| 58 | time.sleep(0.01) |
| 59 | for path,value in [('queues/7/size','1024'),('queues/7/type','2'),('queues/7/gpuid','37820'),('stats_37820/cu_occupancy','0'),('vram_37820','4096')]: |
| 60 | assert (root/path).read_text()==value |
| 61 | print('OMEGA streaming-last',flush=True) |
| 62 | sys.stderr.write('diagnostic-tail') |
| 63 | sys.exit(75) |
| 64 | ''' |
| 65 | p,_=ns['invoke'](['-c',child,ns['KFD_PROC_ROOT']],'streaming-proc-control',expected=None,sample=True) |
| 66 | assert p.returncode==75 and p.stdout.endswith(b'OMEGA streaming-last\n') |
| 67 | assert p.stderr==b'diagnostic-tail' |
| 68 | report=Path('amdserve-launch4.txt').read_text() |
| 69 | assert 'OMEGA stderr diagnostic-tail\n' in report and 'OMEGA proc samples=3;' in report |
| 70 | for sample in range(3): |
| 71 | lines=[s for s in report.splitlines() if s.startswith('OMEGA proc sample='+str(sample)+' ')] |
| 72 | for field,value in [('size','1024'),('type','2'),('gpuid','37820'),('cu_occupancy','0'),('vram_37820','4096')]: |
| 73 | assert any('/'+field+' value='+json.dumps(value) in s for s in lines),(sample,field,lines) |
| 74 | ns['sample_kfd'](0,3) |
| 75 | assert 'missing/unreadable errno=2' in Path('amdserve-launch4.txt').read_text() |
| 76 | (gate/'notebook-proc-receipt.txt').write_text(Path('amdserve-launch4.txt').read_text()) |
| 77 | records.append({'case':'real child observes streaming and three proc samples while waiting; fields unchanged; nonzero exit retained; missing files explicit','exit':p.returncode,'expected':75}) |
| 78 | ns['BIN']=binary |
| 79 | original_main=ns['main'] |
| 80 | def failure(): raise RuntimeError('CONTROL forced execution failure') |
| 81 | ns['main']=failure |
| 82 | try: ns['run']() |
| 83 | except RuntimeError: pass |
| 84 | else: raise AssertionError('forced failure survived') |
| 85 | ns['main']=original_main |
| 86 | report=Path('amdserve-launch4.txt').read_text() |
| 87 | assert report.splitlines()[0].startswith('CONTROLS ') and report.splitlines()[-1]=='RESULT FAIL' |
| 88 | (gate/'notebook-failure-receipt.txt').write_text(report) |
| 89 | records.append({'case':'CONTROL execution failure keeps RESULT last','exit':1,'expected':1}) |
| 90 | digest=ns['BUNDLE']['assets']['omega-launch']['sha256'] |
| 91 | mutated=cell.replace(digest,'0'*64,1); assert mutated!=cell |
| 92 | control=Path(tmp)/'digest-control'; control.mkdir(); os.chdir(control) |
| 93 | try: |
| 94 | badns={'__name__':'__notebook_test__'} |
| 95 | exec(compile(mutated,'bad-cell.py','exec'),badns) |
| 96 | badns['run']() |
| 97 | except RuntimeError as exc: |
| 98 | assert str(exc)=='asset digest omega-launch' |
| 99 | else: raise AssertionError('asset digest mutant survived') |
| 100 | report=Path('amdserve-launch4.txt').read_text() |
| 101 | assert 'FAIL asset unpack: asset digest omega-launch' in report |
| 102 | assert report.splitlines()[0].startswith('CONTROLS ') and report.splitlines()[-1]=='RESULT FAIL' |
| 103 | (gate/'notebook-digest-receipt.txt').write_text(report) |
| 104 | records.append({'case':'CONTROL corrupted binary digest refused before execution','exit':1,'expected':1}) |
| 105 | os.chdir(prior) |
| 106 | (gate/'notebook-check.json').write_text(json.dumps({'records':records,'device':'NOT_RUN','HIP_comparison':'NOT_RUN CPU host; no GPU runtime is imported by the CPU gate'},indent=2)+'\n') |
| 107 | print('NOTEBOOK_CPU PASS',len(records),'rows; all device rows NOT_RUN') |