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/omega-bridge/blob/main/tests/gpu/test_gpu_adapter.py # omega-bridge Omega-first language interoperability: checked conversion backends and native language bindings 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 `omega-bridge / tests / gpu / test_gpu_adapter.py` 182 lines ยท 7174 bytes Raw Blame History 1 #!/usr/bin/env python3 2 """Tests for the native Omega GPU adapter protocol (omega-bridge#3). 3 4 The adapter is a pure boundary: it reads one request from stdin and prints a 5 verdict. These tests exercise acceptance, every named refusal, determinism and 6 the no-silent-fallback rule. No device is opened. Hardware execution remains 7 NOT_RUN and is reported as such by the harness and the support matrix. 8 9 Usage: test_gpu_adapter.py [--adapter build/gpu-adapter] [--artifact DIR] 10 """ 11 import argparse 12 import hashlib 13 import os 14 import subprocess 15 import sys 16 17 HDR = "omega-bridge-gpu-adapter/1" 18 CONTRACT = "a" * 64 19 ARTIFACT = "b" * 64 20 21 22 def negotiate(**over): 23 fields = { 24 "mode": "negotiate", 25 "adapter": "gb10-native-cubin", 26 "consumer": "max", 27 "backend": "nvidia", 28 "isa": "sm_121", 29 "driver": "580.173.02", 30 "abi": "omega-gpu-platform/1.2", 31 "dtypes": "bf16,f32", 32 "capabilities": "cubin-load,launch-template,memory-handoff", 33 "rights": "read,execute", 34 "sync": "explicit", 35 "telemetry": "off", 36 "contract_id": CONTRACT, 37 } 38 fields.update(over) 39 order = ["mode", "adapter", "consumer", "backend", "isa", "driver", "abi", 40 "dtypes", "capabilities", "rights", "sync", "telemetry", "contract_id"] 41 return "\n".join([HDR] + [f"{k}={fields[k]}" for k in order]) + "\n" 42 43 44 def admit(**over): 45 fields = { 46 "mode": "admit", 47 "adapter": "gb10-native-cubin", 48 "consumer": "max", 49 "backend": "nvidia", 50 "isa": "sm_121", 51 "driver": "580.173.02", 52 "abi": "omega-gpu-platform/1.2", 53 "dtypes": "bf16,f32", 54 "capabilities": "cubin-load,launch-template,memory-handoff", 55 "rights": "read,execute", 56 "sync": "explicit", 57 "telemetry": "off", 58 "contract_id": CONTRACT, 59 "artifact_id": ARTIFACT, 60 "kernel_format": "nvidia-cubin-elf64", 61 "qmd_template": "zero-va-v1", 62 "receipt": "valid", 63 "ownership": "producer", 64 "alias": "none", 65 } 66 fields.update(over) 67 order = ["mode", "adapter", "consumer", "backend", "isa", "driver", "abi", 68 "dtypes", "capabilities", "rights", "sync", "telemetry", "contract_id", 69 "artifact_id", "kernel_format", "qmd_template", "receipt", "ownership", "alias"] 70 return "\n".join([HDR] + [f"{k}={fields[k]}" for k in order]) + "\n" 71 72 73 def run(binary, text): 74 p = subprocess.run([binary], input=text.encode(), stdout=subprocess.PIPE, 75 stderr=subprocess.PIPE) 76 return p.returncode, p.stdout.decode(errors="replace"), p.stderr.decode(errors="replace") 77 78 79 def parse(out): 80 d = {} 81 for line in out.splitlines(): 82 if "=" in line: 83 k, v = line.split("=", 1) 84 d[k] = v 85 return d 86 87 88 class T: 89 def __init__(self): 90 self.n = 0 91 self.fail = 0 92 93 def check(self, cond, msg): 94 self.n += 1 95 if not cond: 96 self.fail += 1 97 print("FAIL:", msg) 98 99 def verdict(self, binary, text, want): 100 rc, out, err = run(binary, text) 101 d = parse(out) 102 if want == "ACCEPT": 103 self.check(rc == 0 and d.get("verdict") == "ACCEPT", f"want ACCEPT, got rc={rc} {d} {err}") 104 if d.get("verdict") == "ACCEPT": 105 self.check(len(d.get("request_sha256", "")) == 64, "accept hash missing") 106 else: 107 self.check(rc == 3 and d.get("verdict") == "REFUSE" and d.get("code") == want, 108 f"want {want}, got rc={rc} {d} {err}") 109 return out 110 111 112 def main(): 113 ap = argparse.ArgumentParser() 114 ap.add_argument("--adapter", default=os.path.join(os.path.dirname(__file__), "../../build/gpu-adapter")) 115 ap.add_argument("--artifact", default=None, help="prefix of a CUDApp exported artifact") 116 args = ap.parse_args() 117 t = T() 118 b = args.adapter 119 120 # Acceptance. 121 t.verdict(b, negotiate(), "ACCEPT") 122 t.verdict(b, admit(), "ACCEPT") 123 t.verdict(b, negotiate(consumer="omega", adapter="fake-gpu", backend="fake", 124 isa="fake-1", driver="0.0.0"), "ACCEPT") 125 t.verdict(b, negotiate(driver="581.2.0"), "ACCEPT") # newer driver is compatible 126 127 # Determinism and request binding. 128 o1 = t.verdict(b, admit(), "ACCEPT") 129 o2 = t.verdict(b, admit(), "ACCEPT") 130 t.check(o1 == o2, "same request did not produce identical output") 131 o3 = t.verdict(b, admit(artifact_id="c" * 64), "ACCEPT") 132 t.check(parse(o1)["request_sha256"] != parse(o3)["request_sha256"], 133 "different request produced the same hash") 134 135 # Named refusals. Each mutation is one field. 136 t.verdict(b, HDR + "\n" + negotiate(), "E_ORDER") # duplicate header 137 t.verdict(b, negotiate().replace(HDR, "omega-bridge-gpu-adapter/2"), "E_ORDER") 138 t.verdict(b, negotiate(mode="probe"), "E_MODE") 139 t.verdict(b, negotiate(adapter="amd-native"), "E_ADAPTER") 140 t.verdict(b, negotiate(backend="rocm"), "E_BACKEND") 141 t.verdict(b, negotiate(isa="sm_120"), "E_ISA") 142 t.verdict(b, negotiate(driver="579.9.9"), "E_DRIVER") 143 t.verdict(b, negotiate(driver="580.173"), "E_DRIVER") 144 t.verdict(b, negotiate(driver="580.x.2"), "E_DRIVER") 145 t.verdict(b, negotiate(abi="omega-gpu-platform/1.1"), "E_ABI") 146 t.verdict(b, negotiate(dtypes="f16,f32"), "E_DTYPES") 147 t.verdict(b, negotiate(capabilities="cubin-load"), "E_CAPABILITY") 148 t.verdict(b, negotiate(rights="read"), "E_RIGHTS") 149 t.verdict(b, negotiate(sync="implicit"), "E_SYNC") 150 t.verdict(b, negotiate(telemetry="on"), "E_TELEMETRY") 151 t.verdict(b, negotiate(contract_id="A" * 64), "E_CONTRACT") 152 t.verdict(b, negotiate(contract_id="a" * 63), "E_CONTRACT") 153 t.verdict(b, negotiate(consumer="cuda"), "E_CAPABILITY") 154 # No silent fallback: asking the non-executing adapter to admit a native artifact. 155 t.verdict(b, admit(adapter="fake-gpu", backend="fake", isa="fake-1", driver="0.0.0"), "E_ADAPTER") 156 # Artifact refusals. 157 t.verdict(b, admit(artifact_id="z" * 64), "E_ARTIFACT") 158 t.verdict(b, admit(kernel_format="ptx"), "E_FORMAT") 159 t.verdict(b, admit(qmd_template="live-va-v1"), "E_RELOC") 160 t.verdict(b, admit(receipt="stale"), "E_RECEIPT") 161 t.verdict(b, admit(receipt="missing"), "E_RECEIPT") 162 t.verdict(b, admit(ownership="consumer"), "E_OWNERSHIP") 163 t.verdict(b, admit(alias="alias"), "E_ALIAS") 164 # Structural refusals. 165 t.verdict(b, "", "E_ORDER") 166 t.verdict(b, HDR + "\n", "E_ORDER") 167 t.verdict(b, negotiate() + "alias=none\n", "E_ORDER") # extra line 168 t.verdict(b, negotiate().replace("isa=sm_121\n", ""), "E_ORDER") # missing line 169 t.verdict(b, negotiate().replace("mode=negotiate", "modx=negotiate"), "E_ORDER") 170 171 # Real artifact from CUDApp, if supplied. 172 if args.artifact: 173 man = open(args.artifact + ".manifest").read() 174 f = dict(l.split("=", 1) for l in man.splitlines() if "=" in l) 175 t.verdict(b, admit(contract_id=f["contract_id"], artifact_id=f["artifact_id"]), "ACCEPT") 176 177 print(f'SUMMARY {{"checks": {t.n}, "fail": {t.fail}}}') 178 return 1 if t.fail else 0 179 180 181 if __name__ == "__main__": 182 sys.exit(main())