Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/tests/amd-exec/replay.py # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tests / amd-exec / replay.py` 296 lines · 16601 bytes Raw Blame History 1 #!/usr/bin/env python3 2 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 """External test instrument: exact replay parity, independent scalar controls, buffer bounds.""" 4 import base64 5 import hashlib 6 import json 7 import pathlib 8 import runpy 9 import struct 10 import subprocess 11 import sys 12 13 HERE = pathlib.Path(__file__).parent 14 ROOT = HERE.parent.parent 15 SRC = ROOT/'backend/amd/gfx942' 16 PIN = '`2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f`' 17 NAMES = ('embed5120_step','attn256_prep_step','attn256_part_step','attn256_merge_step', 18 'gdnconv4_step','gdnstate_step','step_advance','readfirst_probe') 19 BASELINES = ('embed5120','attn256_prep','attn256_part','attn256_merge','gdnconv4','gdnstate') 20 MUTANTS = { 21 'embed5120_step-stale': ('embed5120_step','I 7 0 0 2 0 0 20 0 30 0 127 0 29','I 8 128 1 29'), 22 'attn256_prep_step-row0': ('attn256_prep_step','I 8 22 1 45','I 8 128 1 45'), 23 'attn256_part_step-length1': ('attn256_part_step','I 2 14 129 14 0','I 3 129 0 14'), 24 'attn256_merge_step-length1': ('attn256_merge_step','I 2 14 129 14 0','I 3 129 0 14'), 25 'gdnconv4_step-bank': ('gdnconv4_step','I 4 @901 5','I 4 @901 4'), 26 'gdnstate_step-bank': ('gdnstate_step','I 4 @901 5','I 4 @901 4'), 27 'step_advance-noinc': ('step_advance','I 5 129 8 8 52','I 8 264 1 8'), 28 'readfirst_probe-zero': ('readfirst_probe','I 8 264 2 10','I 3 128 0 10'), 29 } 30 31 class Gate: 32 def __init__(self, omc, out): 33 self.omc, self.out = str(omc), out 34 self.commands, self.results, self.access = [], [], [] 35 self.source = ''.join((SRC/'exec'/n).read_text() for n in ('fp.om','fma.om','main.om')) 36 self.instrument = runpy.run_path(str(HERE/'token_access.py'))['instrument'] 37 self.extract = runpy.run_path(str(HERE/'oracle.py'))['extract'] 38 self.executors = {} 39 self.arg_controls = set() 40 self.spans = None 41 self.argbytes = {n:int(next(x.split()[2] for x in (SRC/(n+'.isa')).read_text().splitlines() 42 if x.startswith('K '))) for n in (*NAMES,*BASELINES)} 43 paths = [SRC/'exec'/n for n in ('fp.om','fma.om','main.om')] 44 paths += [SRC/(n+suffix) for n in NAMES for suffix in ('.isa','-gfx942.s')] 45 paths += [SRC/(n+'.isa') for n in BASELINES] 46 paths += [SRC/'hsaco.om',SRC/'rope.om'] 47 paths += [HERE/n for n in ('replay.py','semantics.py','semantics-probe.om','token_access.py', 48 'oracle.py','gdn.py','gdnconv.py','token_ref.py','attention.py','prep.py')] 49 self.pins = {str(p.relative_to(ROOT)):hashlib.sha256(p.read_bytes()).hexdigest() for p in paths} 50 def call(self, argv, data=None, rc=0): 51 p = subprocess.run(list(map(str,argv)), input=data, capture_output=True, timeout=900) 52 stdout_path = self.out/('command-'+str(len(self.commands))+'.stdout') 53 stdout_path.write_bytes(p.stdout) 54 self.commands.append(dict(argv=list(map(str,argv)),exit=p.returncode, 55 stdout_path=str(stdout_path),stdout_sha256=hashlib.sha256(p.stdout).hexdigest(),stderr=p.stderr.decode(errors='replace'))) 56 (self.out/'commands.json').write_text(json.dumps(self.commands,indent=2)+'\n') 57 assert p.returncode == rc, (argv,p.returncode,p.stderr.decode(errors='replace')) 58 assert not p.stderr, (argv,p.stderr) 59 return p.stdout 60 def compile(self, name, source): 61 (self.out/(name+'.om')).write_text(source) 62 binary = self.call([self.omc],source.encode()) 63 path = self.out/name 64 path.write_bytes(binary); path.chmod(0o700) 65 return path 66 def build(self): 67 producer = self.compile('hsaco', (SRC/'hsaco.om').read_text()) 68 self.compile('exec',self.source) 69 self.compile('rope',(SRC/'rope.om').read_text()) 70 sem = runpy.run_path(str(HERE/'semantics.py')) 71 source = self.source[:self.source.index('fn main to int\n')] + (HERE/'semantics-probe.om').read_text() 72 exe = self.compile('semantics',source) 73 self.call([sys.executable,'-I',HERE/'semantics.py','suite',exe,self.out]) 74 old,new = sem['MUTATIONS']['readfirst-lane-zero'] 75 assert source.count(old)==1 76 mutant = self.compile('readfirst-lane-zero',source.replace(old,new)) 77 self.call([sys.executable,'-I',HERE/'semantics.py','control',mutant,self.out,'readfirst-lane-zero']) 78 for name in (*NAMES,*BASELINES): 79 blob = self.call([producer,SRC/(name+'.isa')]) 80 path = self.out/(name+'.hsaco'); path.write_bytes(blob) 81 self.extract(path,self.out/(name+'.text')) 82 assert blob == self.call([producer,SRC/(name+'.isa')]) 83 if name in NAMES: 84 oracle = self.out/(name+'.o') 85 self.call(['llvm-mc-18','-triple=amdgcn-amd-amdhsa','-mcpu=gfx942','-filetype=obj',SRC/(name+'-gfx942.s'),'-o',oracle]) 86 self.extract(oracle,self.out/(name+'.llvm.text')) 87 assert (self.out/(name+'.text')).read_bytes()==(self.out/(name+'.llvm.text')).read_bytes(), name+' encoding mismatch' 88 for name,(base,old,new) in MUTANTS.items(): 89 source = (SRC/(base+'.isa')).read_text(); assert source.count(old)==1,(name,source.count(old)) 90 table = self.out/(name+'.isa'); table.write_text(source.replace(old,new)) 91 path = self.out/(name+'.hsaco'); path.write_bytes(self.call([producer,table])) 92 self.extract(path,self.out/(name+'.text')) 93 print('PASS build, independent LLVM code bytes, semantic oracle/control',flush=True) 94 def layout(self, name, spans): 95 self.layout_name, self.spans, self.executors = name, spans, {} 96 def bounded(self, argbytes): 97 spans=[(0,argbytes),*self.spans[1:]] 98 if argbytes not in self.executors: 99 self.executors[argbytes]=self.compile(self.layout_name+'-args'+str(argbytes), 100 self.instrument(self.source,spans)) 101 return self.executors[argbytes],spans 102 def run(self, name, memory, gx, gy, rc=0): 103 argbytes=self.argbytes[MUTANTS[name][0] if name in MUTANTS else name] 104 exe,spans=self.bounded(argbytes) 105 path = self.out/'replay.memory' 106 if name in NAMES and name not in self.arg_controls and rc==0: 107 short,_=self.bounded(argbytes-1) 108 path.write_bytes(memory) 109 self.call([short,self.out/(name+'.text'),path,gx*64,gy],rc=68) 110 self.results.append(dict(case=name+' truncated kernargs',result='CAUGHT',exit=68)) 111 self.arg_controls.add(name) 112 print('CAUGHT',name,'truncated kernargs',flush=True) 113 path.write_bytes(memory) 114 raw = self.call([exe,self.out/(name+'.text'),path,gx*64,gy],rc=rc) 115 if rc: return None 116 assert len(raw)==16*len(spans) 117 hits=struct.unpack('<'+str(len(spans)*2)+'Q',raw) 118 self.access.append(dict(kernel=name,buffers=[dict(start=p,bytes=n, 119 max_read_byte=hits[2*i]-1 if hits[2*i] else None, 120 max_write_byte=hits[2*i+1]-1 if hits[2*i+1] else None) for i,(p,n) in enumerate(spans)])) 121 return bytearray(path.read_bytes()) 122 def equal(self, name, actual, expected, start): 123 assert actual[start:]==expected[start:], name+' byte mismatch' 124 self.results.append(dict(case=name,result='PASS',mismatched_bytes=0)) 125 print('PASS',name,flush=True) 126 def caught(self, name, actual, expected, start): 127 assert actual[start:]!=expected[start:], name+' ESCAPED' 128 self.results.append(dict(case=name,result='CAUGHT')) 129 print('CAUGHT',name,flush=True) 130 131 132 def append(memory, payload): 133 memory.extend(bytes(64)); p=len(memory); memory.extend(payload); memory.extend(bytes(64)); return p 134 135 136 def advance(g,memory,pos,cap,variant='step_advance'): 137 struct.pack_into('0: 229 g.caught(f'attention part stale length pos{step}',g.run('attn256_part_step-length1',memory,tiles,24),part,64) 230 if step>=64: 231 g.caught('attention merge stale tile stride',g.run('attn256_merge_step-length1',premerge,1,24),expected,64) 232 memory=advance(g,observed,position,cap) 233 assert struct.unpack_from('