interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / stablehlo / pjrt-oracle.py

101 lines · 4301 bytes Raw Blame History
1# SPDX-License-Identifier: Apache-2.0
2# Interop oracle, not Omega-native; never generates kernels.
3# Compiles an existing StableHLO MLIR text file (written by irstablehlo.om) with
4# the XLA CPU PJRT client shipped in jaxlib, executes @main on the host CPU and
5# compares every result element bit-for-bit with an expected vector.
6# jax.jit and every jax tracing API are unused: the module text is the only input.
7# Host dependency: jaxlib (Apache-2.0), version printed on every run. Omega
8# replacement: a PJRT C API caller in .om once Omega can load a C ABI plugin
9# (FR-7 in docs/one_layer/OM_COMPLETION.md).
10# Usage: pjrt-oracle.py MODULE ELEMTYPE N EXPECTED INPUT...
11# Lists are comma-separated decimals. Exit 0 match, 1 mismatch, 2 module refused
12# by the oracle, 3 oracle unavailable, 4 bad arguments, 5 execution error.
13import os
14import sys
15
16# PJRT_ORACLE_PLATFORM selects the PJRT client: cpu (default, this gate) or tpu
17# (a Cloud TPU VM with libtpu; the TPU receipt path, NOT_RUN so far).
18PLATFORM = os.environ.get("PJRT_ORACLE_PLATFORM", "cpu")
19os.environ["JAX_PLATFORMS"] = PLATFORM
20
21TYPES = {
22 "i8": "int8", "i16": "int16", "i32": "int32", "i64": "int64",
23 "ui8": "uint8", "ui16": "uint16", "ui32": "uint32", "ui64": "uint64",
24 "f32": "float32", "f64": "float64",
25}
26
27
28def main(argv):
29 if len(argv) < 5 or argv[2] not in TYPES:
30 print("usage: pjrt-oracle.py MODULE ELEMTYPE N EXPECTED INPUT...", file=sys.stderr)
31 return 4
32 try:
33 import numpy as np
34 import jax
35 import jaxlib
36 from jax._src.lib import xla_client as xc
37 except ImportError as exc:
38 print(f"ORACLE_MISSING {exc}", file=sys.stderr)
39 return 3
40 dtype = np.dtype(TYPES[argv[2]])
41 count = int(argv[3])
42
43 def vector(text):
44 words = text.split(",")
45 if len(words) != count:
46 raise ValueError(f"expected {count} elements, got {len(words)}")
47 if dtype.kind == "f":
48 return np.array([dtype.type(w) for w in words], dtype=dtype)
49 info = np.iinfo(dtype)
50 values = [int(w) for w in words]
51 if any(v < info.min or v > info.max for v in values):
52 raise ValueError(f"value outside {dtype}")
53 return np.array(values, dtype=dtype)
54
55 try:
56 expected = vector(argv[4])
57 inputs = [vector(t) for t in argv[5:]]
58 except ValueError as exc:
59 print(f"BAD_ARGUMENT {exc}", file=sys.stderr)
60 return 4
61 devices = jax.devices(PLATFORM)
62 client = devices[0].client
63 print(f"PJRT jax={jax.__version__} jaxlib={jaxlib.__version__} "
64 f"platform={client.platform} platform_version={client.platform_version}")
65 with open(argv[1], encoding="utf-8") as handle:
66 module = handle.read()
67 try:
68 executable = client.compile_and_load(
69 module, xc.DeviceList(tuple(devices[:1])), xc.CompileOptions())
70 except Exception as exc: # the oracle's own refusal is the evidence here
71 print(f"MODULE_REFUSED {type(exc).__name__}: {str(exc).splitlines()[0]}")
72 return 2
73 # jax_enable_x64 keeps 64-bit element types; device_put would otherwise
74 # narrow them to 32 bits. It changes buffer transfer only, not the module.
75 jax.config.update("jax_enable_x64", True)
76 try:
77 buffers = [jax.device_put(x, devices[0]) for x in inputs]
78 results = executable.execute_sharded(buffers).disassemble_into_single_device_arrays()
79 except Exception as exc:
80 print(f"EXECUTE_ERROR {type(exc).__name__}: {str(exc).splitlines()[0]}")
81 return 5
82 if len(results) != 1:
83 print(f"MISMATCH result count {len(results)}")
84 return 1
85 observed = np.asarray(results[0][0])
86 print("OBSERVED " + ",".join(repr(v.item()) for v in observed))
87 if observed.dtype != dtype or observed.shape != expected.shape:
88 print(f"MISMATCH type {observed.dtype}{observed.shape} expected {dtype}{expected.shape}")
89 return 1
90 bits = np.dtype(f"u{dtype.itemsize}")
91 diff = np.nonzero(observed.view(bits) != expected.view(bits))[0]
92 if diff.size:
93 i = int(diff[0])
94 print(f"MISMATCH index {i} observed {observed[i]!r} expected {expected[i]!r}")
95 return 1
96 print("MATCH bitwise")
97 return 0
98
99
100if __name__ == "__main__":
101 sys.exit(main(sys.argv))