interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / tests / gemm_ex_policy_check.py

77 lines · 4178 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3"""Read-only oracle/mutation runner. Invoke inside the documented device-hidden wrapper."""
4import os
5import pathlib
6import shutil
7import subprocess
8import sys
9
10root = pathlib.Path(__file__).resolve().parents[1]
11os.chdir(root)
12d = root / 'build/gemm-ex-policy'
13d.mkdir(parents=True, exist_ok=True)
14log = (d / 'check.log').open('w')
15def run(cmd, *, env=None, reject=False):
16 p = subprocess.run([str(x) for x in cmd], text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env)
17 text = p.stdout
18 log.write('$ ' + ' '.join(map(str,cmd)) + '\nrc=' + str(p.returncode) + '\n' + text + '\n');log.flush()
19 if (p.returncode == 0) == reject:
20 print(text);raise SystemExit('FAIL command expectation: ' + ' '.join(map(str,cmd)))
21 return p
22for key in ('OMEGA_OMC','OMEGA_BRIDGE','OMEGA_SEED'):
23 if not os.environ.get(key): raise SystemExit('NOT_RUN: missing ' + key)
24run(['sh','om/units.sh','--check'])
25run(['make','-j3','build/libomega_gpu.a'])
26cc = [os.environ.get('CC','cc'),'-std=gnu11','-O0','-ffp-contract=off','-Isrc']
27lib = ['build/libomega_gpu.a','-lpthread','-lm']
28base = d / 'base'
29for old in d.glob('base-*.gcda'):
30 old.unlink()
31run(cc + ['--coverage','tests/gemm_ex_policy_test.c',*lib,'-o',base])
32p=run([base]); print(p.stdout.strip())
33run(cc + ['-DCUDAPP_MIGRATION_PENDING_C_GEMM_EX_HOST_POLICY=0','tests/gemm_ex_policy_test.c',*lib,'-o',d/'shell'])
34p=run([d/'shell']);print('DEFAULT SHELL: '+p.stdout.strip())
35e=os.environ.copy();e['GEMM_POLICY_RED']='1'
36p=run([base],env=e,reject=True)
37if 'FAIL' not in p.stdout: raise SystemExit('FAIL red control did not reach comparator')
38print('RED COMPARATOR: PASS (nonzero mismatch)')
39# Real production archive, not the oracle harness, must refuse to link without this unit.
40missing=d/'missing.a';shutil.copyfile('build/libomega_gpu.a',missing)
41run(['ar','d',missing,'cudapp_om_gemm_ex_policy.o'])
42p=run(cc+['-Itests','tests/gpu_gemm_ex_test.c',missing,'-lpthread','-lm','-o',d/'missing'],reject=True)
43if 'omega_gemm_ex_policy' not in p.stdout: raise SystemExit('FAIL unrelated missing-unit failure')
44print('STRONG LINK NEGATIVE: PASS (omega_gemm_ex_policy undefined)')
45source=pathlib.Path('om/gemmex/policy.om').read_text()
46mutants=[
47 ('minimum','if lt a b','if gt a b'),
48 ('transpose-index','ret add mul d b c','ret add mul c b d'),
49 ('row-stride','if eq op 2\nif ne a 0\nret b','if eq op 2\nif ne a 0\nret 1'),
50 ('plane-pack','if and ne a 5 ne a 6','if and ne a 4 ne a 6'),
51 ('window-quota','ret div 65536 w','ret div 65535 w'),
52 ('contiguous','if or eq a 1 eq b mul c d','if or eq a 2 eq b mul c d'),
53 ('row-offset','ret mul mul a b c','ret add mul a b c'),
54 ('column-stride','if eq op 7\nif ne a 0','if eq op 7\nif eq a 0'),
55]
56for name,old,new in mutants:
57 if source.count(old)!=1: raise SystemExit('FAIL nonunique mutant '+name)
58 om=d/(name+'.om');om.write_text(source.replace(old,new))
59 env=os.environ.copy();env['OMEGA_SEEDED_OMC']=env['OMEGA_OMC']
60 p=run(['sh',pathlib.Path(env['OMEGA_BRIDGE'])/'tools/bridge-seeded','c',om],env=env)
61 c=d/(name+'.c');c.write_text(p.stdout)
62 exe=d/name
63 run(cc+['tests/gemm_ex_policy_test.c',c,*lib,'-o',exe])
64 p=run([exe],reject=True)
65 if 'FAIL' not in p.stdout: raise SystemExit('FAIL mutant not killed by comparison: '+name)
66 print('MUTANT '+name+': KILLED '+p.stdout.splitlines()[0])
67run(['gcov','-b','-c','-o',d,str(d/'base-gemm_ex_policy_test.gcno')])
68targets={'gx_min','gx_arow','gx_bcol','gx_brow','gx_pack','gx_conv'}
69coverage=pathlib.Path('omega_gpu_gemm_ex.c.gcov').read_text()
70summary=[line for line in coverage.splitlines() if line.startswith('function ') and line.split()[1] in targets]
71(d/'coverage.txt').write_text('\n'.join(summary)+'\n')
72for path in pathlib.Path('.').glob('*.gcov'):
73 shutil.move(str(path),d/path.name)
74print('\n'.join(summary))
75for target in ('test-gpu-gemm-ex','test-gpu-gemm-ex-sim'):
76 p=run(['make','-j3',target]);print(target+': rc=0 '+'; '.join(line for line in p.stdout.splitlines() if 'PASS' in line))
77print('GEMM EX HOST POLICY ACCEPTANCE PASS (8 mutants, red comparator, missing-unit link)')