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 / no_posix_bf16.py

102 lines · 5283 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3"""Run only inside the lane's device-hidden bwrap. No device access is needed."""
4import os
5from pathlib import Path
6import subprocess
7
8ROOT = Path(__file__).resolve().parents[1]
9OUT = ROOT / 'build/no_posix_bf16'
10OUT.mkdir(parents=True, exist_ok=True)
11BRIDGE = Path(os.environ['OMEGA_BRIDGE'])
12ENV = dict(os.environ, OMEGA_NATIVE_OMC2=os.environ['OMEGA_OMC'])
13subprocess.run(['sh', ROOT / 'om/no-posix.sh', '--tools', OUT], check=True)
14ENV.update(OMEGA_NATIVE_OMC=str(OUT / 'tools/omc-gen'),
15 OMEGA_NATIVE_X86_OMC=str(OUT / 'tools/omc-x86'))
16NATIVE = ROOT / 'build/no_posix/native'
17SOURCE = ROOT / 'build/no_posix/sources/no_avbf.om'
18CALLER = ROOT / 'tests/no_posix_bf16.c'
19FLAGS = ['gcc', '-std=gnu11', '-O2', '-Wall', '-Wextra', '-Werror', '-Wno-unused-function', '-ffp-contract=off', '-I' + str(ROOT / 'src')]
20
21
22def run(args, expected=0):
23 p = subprocess.run([str(a) for a in args], cwd=ROOT, env=ENV, capture_output=True)
24 if p.returncode != expected:
25 raise AssertionError((args, p.returncode, p.stdout[-2000:], p.stderr[-4000:]))
26 return p.stdout
27
28
29def dump(name, extra, include, objects):
30 binary = OUT / name
31 run([*FLAGS, *extra, '-I' + str(include), CALLER, *objects, '-lm', '-o', binary])
32 data = run([binary])
33 (OUT / (name + '.bin')).write_bytes(data)
34 return data
35
36
37hosted = dump('canonical-hosted', [], NATIVE, [ROOT / 'src/cudapp_om_av_bf16.c'])
38fs = dump('freestanding', ['-DOMEGA_GPU_NO_POSIX'], NATIVE,
39 [NATIVE / 'omega_native.o', NATIVE / 'omega_api_defs.o'])
40assert fs == hosted, 'canonical hosted view versus freestanding scalar differs'
41red = dump('c-oracle', ['-DOMEGA_GPU_NO_POSIX', '-DCUDAPP_MIGRATION_PENDING_C_NO_POSIX_SESSION=1'], NATIVE, [])
42assert red == hosted, 'explicit C oracle differs from canonical hosted view'
43# Native-hosted and native-freestanding bindings must also implement the same scalar.
44scalarhost = OUT / 'scalar-hosted'
45run(['sh', BRIDGE / 'tools/native', scalarhost, SOURCE])
46assert dump('native-hosted', ['-DOMEGA_GPU_NO_POSIX'], scalarhost,
47 [scalarhost / 'libomega_native.so', '-Wl,-rpath,' + str(scalarhost)]) == hosted
48print('PASS canonical hosted view, native hosted scalar and freestanding scalar differential; C flag ON control')
49
50# The production matmul object is inspected at O0 so C computations cannot hide
51# behind inlining. Both nm and relocation/disassembly must name the selected arm.
52for enabled in (0, 1):
53 obj = OUT / ('matmul-' + str(enabled) + '.o')
54 run([*FLAGS, '-O0', '-ffreestanding', '-fno-builtin', '-fno-stack-protector',
55 '-DOMEGA_GPU_NO_POSIX', '-DOMEGA_NUMERIC_CPU_ONLY',
56 '-DCUDAPP_MIGRATION_PENDING_C_NO_POSIX_SESSION=' + str(enabled),
57 '-I' + str(NATIVE), '-c', ROOT / 'src/omega_gpu_matmul_api.c', '-o', obj])
58 nm = run(['nm', obj]).decode()
59 dis = run(['objdump', '-dr', obj]).decode()
60 (OUT / (obj.stem + '.nm')).write_text(nm)
61 (OUT / (obj.stem + '.objdump')).write_text(dis)
62 assert 'omega_av_bf16' not in nm
63 if enabled:
64 assert 'av_bf16_c' in nm and '<av_bf16_c>' in dis
65 assert 'omega2_no_avbf' not in nm
66 else:
67 assert 'av_bf16_c' not in nm and 'av_bf16_c' not in dis
68 assert 'omega2_no_avbf' in nm and 'omega2_no_avbf' in dis
69print('PASS production NO_POSIX matmul nm/objdump OFF Omega / ON C red control')
70
71# Strong reference: removing the native objects must fail at the selected export.
72p = subprocess.run([*FLAGS, '-DOMEGA_GPU_NO_POSIX', '-I' + str(NATIVE), str(CALLER), '-o', str(OUT / 'missing')], cwd=ROOT, env=ENV, capture_output=True)
73assert p.returncode != 0 and b'omega2_no_avbf' in p.stderr
74(OUT / 'missing-link.txt').write_bytes(p.stderr)
75# The combined freestanding unit object must not import libc, TLS or an oracle.
76combined = OUT / 'combined.o'
77run(['ld', '-r', NATIVE / 'omega_native.o', NATIVE / 'omega_api_defs.o', '-o', combined])
78assert run(['nm', '-u', combined]) == b''
79print('PASS strong missing-unit link refusal; combined native objects have zero undefined imports')
80
81text = SOURCE.read_text()
82mutants = [
83 ('round-bias', 'ret shr add add u 32767 and shr u 16 1 16', 'ret shr add add u 32766 and shr u 16 1 16'),
84 ('tie-parity', 'ret shr add add u 32767 and shr u 16 1 16', 'ret shr add add u 32767 and shr u 16 0 16'),
85 ('quiet-bit', 'ret or shr u 16 64', 'ret shr u 16'),
86 ('nan-payload', 'ret or shr u 16 64', 'ret 32704'),
87 ('canonical-nan', 'ret 32704', 'ret 0'),
88 ('mode-select', 'if eq md 0', 'if ne md 0'),
89]
90for name, old, new in mutants:
91 assert old in text
92 directory = OUT / name
93 directory.mkdir(exist_ok=True)
94 source = directory / 'mutant.om'
95 source.write_text(text.replace(old, new, 1))
96 unit = directory / 'native'
97 run(['sh', BRIDGE / 'tools/native', '--freestanding', unit, source])
98 observed = dump(name + '-bin', ['-DOMEGA_GPU_NO_POSIX'], unit,
99 [unit / 'omega_native.o', unit / 'omega_api_defs.o'])
100 assert observed != hosted, 'SURVIVED ' + name
101 print('KILLED ' + name + ': compiled native behavior differs from canonical hosted')
102print('PASS no-posix BF16: canonical/native hosted/freestanding differential; symbol OFF/ON red; 6 killed mutants')