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 / attention_kgen_mutants.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | """Recompile perturbed seeded attention units and require differential detection. |
| 4 | Verification oracle only, never part of the production codegen path. Invoke through |
| 5 | bwrap --dev-bind / / --dev /dev --proc /proc -- taskset -c 10-14 nice -n 10. |
| 6 | """ |
| 7 | import os |
| 8 | from pathlib import Path |
| 9 | import subprocess |
| 10 | import sys |
| 11 | import tempfile |
| 12 | |
| 13 | root = Path(__file__).resolve().parents[1] |
| 14 | build = (root / sys.argv[1]).resolve() |
| 15 | # The exact objects make linked into attention_diff besides the attention unit; a directory glob would also |
| 16 | # pick up stale objects of generated files that om/units.sh has since renamed. |
| 17 | objects = [str((root / p).resolve()) for p in sys.argv[2:]] |
| 18 | if not objects: |
| 19 | raise SystemExit('usage: attention_kgen_mutants.py BUILD_DIR LINK_OBJECTS...') |
| 20 | files = ['attention_emit.om', 'attention_tuned.om', 'attention_general.om', 'attention_ir.om'] |
| 21 | mutants = [ |
| 22 | ('descriptor-offset', 'attention_general.om', '775 atrg 0 AN AN AN 856 1', '775 atrg 0 AN AN AN 896 1'), |
| 23 | ('query-argument-offset', 'attention_general.om', 'get s add A i add 896 mul i 8', 'get s add A i add 904 mul i 8'), |
| 24 | ('thread-register', 'attention_general.om', 'get s TID 33', 'get s TID 37'), |
| 25 | ('argument-scalar-offset', 'attention_general.om', 'get s add MAXB i add 928 mul i 4', 'get s add MAXB i add 952 mul i 4'), |
| 26 | ('stall-control-field', 'attention_emit.om', 'shl st 9 shl wb 14', 'shl st 14 shl wb 14'), |
| 27 | ] |
| 28 | |
| 29 | def run(cmd, **kw): |
| 30 | return subprocess.run(cmd, cwd=root, text=True, stdout=subprocess.PIPE, |
| 31 | stderr=subprocess.STDOUT, **kw) |
| 32 | |
| 33 | if Path('/dev/nvidiactl').exists() or Path('/dev/nvidia0').exists(): |
| 34 | raise SystemExit('FAIL attention mutants require bwrap private /dev') |
| 35 | base = run([str(build / 'attention_diff')]) |
| 36 | if base.returncode: |
| 37 | raise SystemExit('FAIL baseline\n' + base.stdout) |
| 38 | print(base.stdout.strip(), flush=True) |
| 39 | for name, filename, old, new in mutants: |
| 40 | with tempfile.TemporaryDirectory(prefix='attention-mut-', dir=build) as tmpdir: |
| 41 | tmp = Path(tmpdir) |
| 42 | sources = [] |
| 43 | for f in files: |
| 44 | text = (root / 'om/kgen' / f).read_text() |
| 45 | if f == filename: |
| 46 | if text.count(old) != 1: |
| 47 | raise SystemExit(f'FAIL {name}: ambiguous mutation site count={text.count(old)}') |
| 48 | text = text.replace(old, new) |
| 49 | dest = tmp / f |
| 50 | dest.write_text(text) |
| 51 | sources.append(str(dest)) |
| 52 | generated = tmp / 'generated' |
| 53 | generated.mkdir() |
| 54 | env = dict(os.environ, OMEGA_UNITS='attention_ir=' + ','.join(sources), |
| 55 | OMEGA_UNITS_DEST=str(generated)) |
| 56 | result = run(['sh', 'om/units.sh'], env=env) |
| 57 | if result.returncode: |
| 58 | raise SystemExit(f'FAIL {name}: generation failure is not a semantic kill\n{result.stdout}') |
| 59 | obj, exe = tmp / 'mutant.o', tmp / 'differential' |
| 60 | cc = os.environ.get('CC', 'cc') |
| 61 | result = run([cc, '-O2', '-Isrc', '-ffunction-sections', '-fdata-sections', |
| 62 | '-c', str(generated / 'cudapp_freest_attention_ir.c'), '-o', str(obj)]) |
| 63 | if result.returncode: |
| 64 | raise SystemExit(f'FAIL {name}: compilation\n{result.stdout}') |
| 65 | result = run([cc, '--coverage', '-Wl,--gc-sections', '-o', str(exe), str(obj), |
| 66 | *objects, '-lpthread', '-lm']) |
| 67 | if result.returncode: |
| 68 | raise SystemExit(f'FAIL {name}: link\n{result.stdout}') |
| 69 | result = run([str(exe)]) |
| 70 | if result.returncode != 1 or 'attention parity failed ' not in result.stdout: |
| 71 | raise SystemExit(f'FAIL {name}: survived or crashed rc={result.returncode}\n{result.stdout}') |
| 72 | print(f'KILLED {name}: rc=1 {result.stdout.strip()}', flush=True) |
| 73 | print('PASS attention seeded semantic mutants: 5 killed', flush=True) |