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
omega-mv / seed / nki / oracle.py
| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # interop oracle, not Omega-native; never generates kernels. |
| 3 | # |
| 4 | # Host dependency (oracle only): CPython 3.12 + nki 0.7.0 (Apache-2.0, AWS) + numpy 2.2.6. |
| 5 | # Omega replacement: none yet. The kernels come from seed/nki/irnki.om; the expected |
| 6 | # values come from a native Omega program built by gate-irnki.sh. This script only |
| 7 | # (1) writes deterministic input lanes and (2) runs the emitted kernels on the public |
| 8 | # NKI CPU simulator (nki.simulate) and compares the result bits with the Omega values. |
| 9 | # |
| 10 | # oracle.py version |
| 11 | # oracle.py inputs uint32|float32 > in.bin a lanes then b lanes, little-endian |
| 12 | # oracle.py check kernels.py in.bin refdir name... exit 0 all equal, 1 any differ |
| 13 | # oracle.py limit kernels.py name exit 0 if a (128, 500) call is refused |
| 14 | import importlib.util |
| 15 | import os |
| 16 | import sys |
| 17 | |
| 18 | ROWS, COLS = 256, 1024 |
| 19 | N = ROWS * COLS |
| 20 | |
| 21 | |
| 22 | def die(msg, code): |
| 23 | sys.stderr.write("oracle: " + msg + "\n") |
| 24 | sys.exit(code) |
| 25 | |
| 26 | |
| 27 | try: |
| 28 | import numpy as np |
| 29 | import nki |
| 30 | except Exception as ex: # the gate maps exit 3 to NOT_RUN |
| 31 | die("cannot import the simulator: %s: %s" % (type(ex).__name__, ex), 3) |
| 32 | |
| 33 | |
| 34 | def load(path): |
| 35 | spec = importlib.util.spec_from_file_location("omnki", path) |
| 36 | mod = importlib.util.module_from_spec(spec) |
| 37 | spec.loader.exec_module(mod) |
| 38 | return mod.KERNELS |
| 39 | |
| 40 | |
| 41 | def inputs(dtype): |
| 42 | rng = np.random.default_rng(20261026) |
| 43 | a = rng.integers(0, 2 ** 32, N, dtype=np.uint64).astype(np.uint32) |
| 44 | b = rng.integers(0, 2 ** 32, N, dtype=np.uint64).astype(np.uint32) |
| 45 | if dtype == "uint32": |
| 46 | edge = [0, 1, 2, 0x7FFFFFFF, 0x80000000, 0x80000001, 0xFFFF, 0x10000, 0xFFFFFFFE, 0xFFFFFFFF] |
| 47 | elif dtype == "float32": |
| 48 | # +0 -0 +inf -inf qNaN max -max min-normal smallest/largest subnormal 1 -1 1+ulp 2^24 |
| 49 | edge = [0x00000000, 0x80000000, 0x7F800000, 0xFF800000, 0x7FC00000, 0x7F7FFFFF, |
| 50 | 0xFF7FFFFF, 0x00800000, 0x00000001, 0x007FFFFF, 0x3F800000, 0xBF800000, |
| 51 | 0x3F800001, 0x4B800000] |
| 52 | # the rest: half raw bit patterns (all exponents), half normal values near 1 |
| 53 | k = N // 2 |
| 54 | a[k:] = rng.standard_normal(N - k).astype(np.float32).view(np.uint32) |
| 55 | b[k:] = rng.standard_normal(N - k).astype(np.float32).view(np.uint32) |
| 56 | else: |
| 57 | die("inputs: dtype must be uint32 or float32", 2) |
| 58 | e = np.array(edge, dtype=np.uint32) |
| 59 | m = len(edge) |
| 60 | a[: m * m] = np.repeat(e, m) |
| 61 | b[: m * m] = np.tile(e, m) |
| 62 | sys.stdout.buffer.write(a.astype("<u4").tobytes() + b.astype("<u4").tobytes()) |
| 63 | |
| 64 | |
| 65 | def check(path, inpath, refdir, names): |
| 66 | kernels = load(path) |
| 67 | if sorted(kernels) != sorted(names): |
| 68 | die("kernel set %s differs from expected %s" % (sorted(kernels), sorted(names)), 1) |
| 69 | raw = np.frombuffer(open(inpath, "rb").read(), dtype="<u4") |
| 70 | if raw.size != 2 * N: |
| 71 | die("input size %d lanes, expected %d" % (raw.size, 2 * N), 1) |
| 72 | target = os.environ.get("NEURON_PLATFORM_TARGET_OVERRIDE", "default") |
| 73 | bad = 0 |
| 74 | for name in names: |
| 75 | fn, dtype = kernels[name] |
| 76 | a = raw[:N].copy().reshape(ROWS, COLS) |
| 77 | b = raw[N:].copy().reshape(ROWS, COLS) |
| 78 | if dtype == "float32": |
| 79 | a, b = a.view(np.float32), b.view(np.float32) |
| 80 | elif dtype != "uint32": |
| 81 | die("%s: unknown lane dtype %s" % (name, dtype), 1) |
| 82 | got = np.asarray(nki.simulate(fn)(a, b)) |
| 83 | if got.shape != (ROWS, COLS) or got.dtype.itemsize != 4: |
| 84 | die("%s: result shape %s dtype %s" % (name, got.shape, got.dtype), 1) |
| 85 | g = got.reshape(N).view(np.uint32) |
| 86 | r = np.frombuffer(open(os.path.join(refdir, name + ".bin"), "rb").read(), dtype="<u4") |
| 87 | if r.size != N: |
| 88 | die("%s: reference size %d lanes" % (name, r.size), 1) |
| 89 | diff = g != r |
| 90 | nanpair = 0 |
| 91 | if dtype == "float32": |
| 92 | # NaN payload and sign are not part of the IEEE result; compare NaN as a class. |
| 93 | gn = (g & 0x7F800000 == 0x7F800000) & (g & 0x007FFFFF != 0) |
| 94 | rn = (r & 0x7F800000 == 0x7F800000) & (r & 0x007FFFFF != 0) |
| 95 | nanpair = int((diff & gn & rn).sum()) |
| 96 | diff = diff & ~(gn & rn) |
| 97 | n = int(diff.sum()) |
| 98 | state = "PASS" if n == 0 else "FAIL" |
| 99 | print("%s target=%s kernel=%s dtype=%s lanes=%d mismatches=%d nan_class_only=%d" |
| 100 | % (state, target, name, dtype, N, n, nanpair)) |
| 101 | if n: |
| 102 | i = int(np.flatnonzero(diff)[0]) |
| 103 | print(" first lane %d a=%08x b=%08x simulator=%08x omega=%08x" |
| 104 | % (i, raw[i], raw[N + i], g[i], r[i])) |
| 105 | bad += 1 |
| 106 | sys.exit(1 if bad else 0) |
| 107 | |
| 108 | |
| 109 | def limit(path, name): |
| 110 | fn, dtype = load(path)[name] |
| 111 | a = np.zeros((128, 500), dtype=np.dtype(dtype)) |
| 112 | try: |
| 113 | nki.simulate(fn)(a, a) |
| 114 | except Exception as ex: |
| 115 | print("REFUSED kernel=%s shape=(128, 500) %s" % (name, type(ex).__name__)) |
| 116 | sys.exit(0) |
| 117 | die("%s accepted shape (128, 500)" % name, 1) |
| 118 | |
| 119 | |
| 120 | def main(argv): |
| 121 | if len(argv) == 2 and argv[1] == "version": |
| 122 | print("nki %s numpy %s python %s" % (nki.__version__, np.__version__, sys.version.split()[0])) |
| 123 | elif len(argv) == 3 and argv[1] == "inputs": |
| 124 | inputs(argv[2]) |
| 125 | elif len(argv) >= 6 and argv[1] == "check": |
| 126 | check(argv[2], argv[3], argv[4], argv[5:]) |
| 127 | elif len(argv) == 4 and argv[1] == "limit": |
| 128 | limit(argv[2], argv[3]) |
| 129 | else: |
| 130 | die("usage: see header", 2) |
| 131 | |
| 132 | |
| 133 | if __name__ == "__main__": |
| 134 | main(sys.argv) |