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 / ampere / native_omega_x86.py

112 lines · 4284 bytes Raw Blame History
1#!/usr/bin/env python3
2"""Explicit experimental #923 oracle. Host EMULATED only; never GPU evidence.
3
4The large cubin private-ABI unit is compiled, not called: local Omega arrays
5cannot supply its full 16 MiB/128 MiB view arguments. No signatures are shrunk.
6"""
7import argparse
8import hashlib
9from pathlib import Path
10import subprocess
11
12PIN = "becf5c817bcb7e0846d12ebfc2be91042d8ad89a"
13COMPILER_SHA = "5f0faab7cd4fc12158cf8e099418098d278df644efc10e4e7e2e5ca6141fca86"
14ROOT = Path(__file__).resolve().parents[2]
15ENCODER_MAIN = """
16fn main to int
17ins = new ulong 8192 0
18phys = new ulong 288 0
19code = new uchar 16384 0
20n = ENTRY view ins view phys 1 0 edit code
21if ne n 16
22ret 1
23end
24w = write 1 code 0 n
25if ne w 16
26ret 2
27end
28ret 0
29end
30"""
31RUNTIME_MAIN = """
32fn main to int
33m = new ulong 16 0
34q = new ulong 64 0
35put m 0 2
36put m 1 4096
37put m 2 1
38put m 3 4096
39r = amp_runtime view m edit q
40if ne r 0
41ret 1
42end
43put m 3 0
44r = amp_runtime view m edit q
45if ne r 1
46ret 2
47end
48ret 0
49end
50"""
51
52
53def run(command, **kw):
54 return subprocess.run(command, capture_output=True, timeout=180, **kw)
55
56
57def compile_program(compiler, source, path):
58 result = run([str(compiler)], input=source)
59 if result.returncode:
60 raise RuntimeError(f"compile refused {path.name}: {result.returncode}: {result.stderr!r}")
61 path.write_bytes(result.stdout)
62 path.chmod(0o755)
63
64
65def main():
66 parser = argparse.ArgumentParser(description=__doc__)
67 parser.add_argument("--compiler-tree", type=Path, required=True)
68 parser.add_argument("--output", type=Path, required=True)
69 args = parser.parse_args()
70 tree = args.compiler_tree.resolve()
71 observed = subprocess.check_output(["git", "-C", str(tree), "rev-parse", "HEAD"], text=True).strip()
72 compiler = tree / "seed/out/omc"
73 if observed != PIN or hashlib.sha256(compiler.read_bytes()).hexdigest() != COMPILER_SHA:
74 raise SystemExit("experimental compiler source/binary pin mismatch")
75 args.output.mkdir(parents=True, exist_ok=True)
76 units = (("om/encoder/bw_enc_sm121.om", "bw_enc_sm121"),
77 ("om/encoder/ampere_enc_sm86.om", "ampere_enc_sm86"),
78 ("om/encoder/amp_runtime.om", "amp_runtime"),
79 ("om/cubin/cubin_elf.om", "cubin_elf"))
80 for relative, entry in units:
81 source = (ROOT / relative).read_bytes()
82 bare = args.output / (entry + ".x86.unit")
83 compile_program(compiler, b"target x86linux\n" + source, bare)
84 print("EMULATED-ORACLE INPUT COMPILE", relative, hashlib.sha256(source).hexdigest(),
85 "x86-unit-sha256", hashlib.sha256(bare.read_bytes()).hexdigest())
86 if entry == "cubin_elf":
87 print("NOT_RUN native-cubin private-ABI execution: full-size external buffer caller required")
88 continue
89 suffix = RUNTIME_MAIN if entry == "amp_runtime" else ENCODER_MAIN.replace("ENTRY", entry)
90 program = source + suffix.encode()
91 arm = args.output / (entry + ".arm")
92 x86 = args.output / (entry + ".x86")
93 compile_program(compiler, program, arm)
94 compile_program(compiler, b"target x86linux\n" + program, x86)
95 native = run([str(arm)])
96 emulated = run(["qemu-x86_64", str(x86)])
97 if native.returncode or emulated.returncode or native.stdout != emulated.stdout:
98 raise RuntimeError(f"{entry} runtime parity failed: native={native.returncode}, x86={emulated.returncode}")
99 mutant_suffix = suffix.replace("if ne r 1", "if ne r 0") if entry == "amp_runtime" else suffix.replace("view phys 1 0", "view phys 0 0")
100 mutant = args.output / (entry + ".mutant.x86")
101 compile_program(compiler, b"target x86linux\n" + source + mutant_suffix.encode(), mutant)
102 negative = run(["qemu-x86_64", str(mutant)])
103 expected = 2 if entry == "amp_runtime" else 1
104 if negative.returncode != expected:
105 raise RuntimeError(f"{entry} negative control was not caught: {negative.returncode}")
106 print("EMULATED PASS", entry, "native-AArch64/x86-QEMU stdout-sha256",
107 hashlib.sha256(emulated.stdout).hexdigest(), "negative-exit", negative.returncode)
108 print("EXPERIMENTAL NATIVE X86 SMALL-UNIT PROBES PASS; native-cubin runtime NOT_RUN; device NOT_RUN")
109
110
111if __name__ == "__main__":
112 main()