aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / om / q38-head / check.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | """External test instrument for the compiled Omega selector. No model implementation.""" |
| 4 | import ctypes as C |
| 5 | from decimal import Decimal, localcontext |
| 6 | import json |
| 7 | import math |
| 8 | from pathlib import Path |
| 9 | import random |
| 10 | import statistics |
| 11 | import sys |
| 12 | import time |
| 13 | |
| 14 | N, H = 248320, 5120 |
| 15 | lib = C.CDLL(str(Path(sys.argv[1]).resolve())) |
| 16 | call = lib.omega_q38select |
| 17 | call.restype = C.c_uint32 |
| 18 | call.argtypes = [C.POINTER(C.c_uint64), C.c_void_p, C.c_uint64, C.c_void_p, C.c_uint64, |
| 19 | C.c_void_p, C.c_uint64, C.c_void_p, C.c_uint64, C.c_uint64] |
| 20 | h, s, coef, mask = (C.c_float * H)(), (C.c_float * N)(), (C.c_float * N)(), (C.c_ubyte * N)() |
| 21 | checks = 0 |
| 22 | |
| 23 | def run(n): |
| 24 | result = C.c_uint64(999999) |
| 25 | status = call(C.byref(result), h, H, s, N, coef, N, mask, N, n) |
| 26 | return status, result.value |
| 27 | |
| 28 | def reset(): |
| 29 | for a in (h, s, coef): C.memset(a, 0, C.sizeof(a)) |
| 30 | C.memset(mask, 123, C.sizeof(mask)) |
| 31 | |
| 32 | def require(ok, message): |
| 33 | global checks |
| 34 | checks += 1 |
| 35 | assert ok, message |
| 36 | |
| 37 | reset(); h[0] = 1; s[0] = s[1] = 1 |
| 38 | require(run(3) == (0, 2), 'ties must retain both maxima and reject the distant row') |
| 39 | require(list(mask[:4]) == [1, 1, 0, 123], 'selection and untouched tail') |
| 40 | |
| 41 | reset(); h[H-1] = 1; s[0] = 1; s[1] = 0.5; coef[1] = 1 |
| 42 | require(run(2) == (0, 2), 'norm must include the last hidden dimension') |
| 43 | |
| 44 | reset(); s[0] = -0.0; s[1] = 0.0 |
| 45 | require(run(2) == (0, 2), 'signed zeros tie') |
| 46 | |
| 47 | reset(); h[0] = 1e-40; s[0] = 1e-40; coef[1] = 2 |
| 48 | require(run(2) == (0, 2), 'subnormal hidden values must contribute') |
| 49 | |
| 50 | reset(); h[0] = 1; h[1] = 2**-26; s[0] = 1; s[1] = -(2**70); coef[1] = 2**70 |
| 51 | require(run(2) == (0, 2), 'outward rounding must preserve a cancellation-sensitive candidate') |
| 52 | |
| 53 | reset(); h[0] = 1; s[0] = 1; s[100] = math.nan; coef[100] = -1 |
| 54 | require(run(2) == (0,1), 'rows beyond count are not inputs') |
| 55 | |
| 56 | for target, value in [('h', math.nan), ('h', math.inf), ('s', -math.inf), ('c', math.nan), ('c', -1)]: |
| 57 | reset(); {'h':h,'s':s,'c':coef}[target][(H if target == 'h' else 3)-1] = value |
| 58 | require(run(3)[0] == 5, f'refuse {target}={value}') |
| 59 | require(bytes(mask) == bytes([123])*N, 'refusal must not partially publish a mask') |
| 60 | for n in (0, N+1): |
| 61 | reset(); require(run(n)[0] == 5, 'refuse invalid count') |
| 62 | require(mask[0] == 123, 'invalid count leaves output untouched') |
| 63 | |
| 64 | reset(); rng=random.Random(710) |
| 65 | for i in range(H): h[i] = rng.uniform(-2,2) |
| 66 | for i in range(512): s[i] = rng.uniform(-30,30); coef[i] = rng.uniform(0,0.05) |
| 67 | with localcontext() as ctx: |
| 68 | ctx.prec = 100 |
| 69 | norm = sum(Decimal.from_float(x)**2 for x in h).sqrt() |
| 70 | lo=[Decimal.from_float(s[i])-Decimal.from_float(coef[i])*norm for i in range(512)] |
| 71 | hi=[Decimal.from_float(s[i])+Decimal.from_float(coef[i])*norm for i in range(512)] |
| 72 | expected=[int(x >= max(lo)) for x in hi] |
| 73 | status, count=run(512) |
| 74 | require(status == 0 and count == sum(mask[:512]), 'count matches published mask') |
| 75 | require(list(mask[:512]) == expected, 'independent decimal intervals match') |
| 76 | |
| 77 | reset(); h[0]=3.4028234663852886e38; coef[0]=3.4028234663852886e38; s[1]=1 |
| 78 | require(run(2) == (0,2), 'finite binary32 extremes must not overflow bound calculations') |
| 79 | |
| 80 | reset(); h[0]=1 |
| 81 | for i in range(N): s[i]=-10; coef[i]=0.01 |
| 82 | s[N-1]=1 |
| 83 | require(run(N) == (0,1) and mask[N-1] == 1, 'maximum row count and final winner') |
| 84 | samples=[] |
| 85 | for _ in range(11): |
| 86 | start=time.perf_counter_ns(); rc,n=run(N); samples.append(time.perf_counter_ns()-start) |
| 87 | require(rc==0 and n==1, 'benchmark correctness') |
| 88 | print(json.dumps({'status':'PASS','checks':checks,'selector_ns':samples, |
| 89 | 'median_ns':statistics.median(samples),'calculation':'median of 11 full-vocabulary native Omega selector calls; host overhead screen only'},indent=2)) |