interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aien-sovereign-core-mv / om / q38-head / check.py

89 lines · 3728 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0
3"""External test instrument for the compiled Omega selector. No model implementation."""
4import ctypes as C
5from decimal import Decimal, localcontext
6import json
7import math
8from pathlib import Path
9import random
10import statistics
11import sys
12import time
13
14N, H = 248320, 5120
15lib = C.CDLL(str(Path(sys.argv[1]).resolve()))
16call = lib.omega_q38select
17call.restype = C.c_uint32
18call.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]
20h, s, coef, mask = (C.c_float * H)(), (C.c_float * N)(), (C.c_float * N)(), (C.c_ubyte * N)()
21checks = 0
22
23def 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
28def reset():
29 for a in (h, s, coef): C.memset(a, 0, C.sizeof(a))
30 C.memset(mask, 123, C.sizeof(mask))
31
32def require(ok, message):
33 global checks
34 checks += 1
35 assert ok, message
36
37reset(); h[0] = 1; s[0] = s[1] = 1
38require(run(3) == (0, 2), 'ties must retain both maxima and reject the distant row')
39require(list(mask[:4]) == [1, 1, 0, 123], 'selection and untouched tail')
40
41reset(); h[H-1] = 1; s[0] = 1; s[1] = 0.5; coef[1] = 1
42require(run(2) == (0, 2), 'norm must include the last hidden dimension')
43
44reset(); s[0] = -0.0; s[1] = 0.0
45require(run(2) == (0, 2), 'signed zeros tie')
46
47reset(); h[0] = 1e-40; s[0] = 1e-40; coef[1] = 2
48require(run(2) == (0, 2), 'subnormal hidden values must contribute')
49
50reset(); h[0] = 1; h[1] = 2**-26; s[0] = 1; s[1] = -(2**70); coef[1] = 2**70
51require(run(2) == (0, 2), 'outward rounding must preserve a cancellation-sensitive candidate')
52
53reset(); h[0] = 1; s[0] = 1; s[100] = math.nan; coef[100] = -1
54require(run(2) == (0,1), 'rows beyond count are not inputs')
55
56for 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')
60for 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
64reset(); rng=random.Random(710)
65for i in range(H): h[i] = rng.uniform(-2,2)
66for i in range(512): s[i] = rng.uniform(-30,30); coef[i] = rng.uniform(0,0.05)
67with 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]
73status, count=run(512)
74require(status == 0 and count == sum(mask[:512]), 'count matches published mask')
75require(list(mask[:512]) == expected, 'independent decimal intervals match')
76
77reset(); h[0]=3.4028234663852886e38; coef[0]=3.4028234663852886e38; s[1]=1
78require(run(2) == (0,2), 'finite binary32 extremes must not overflow bound calculations')
79
80reset(); h[0]=1
81for i in range(N): s[i]=-10; coef[i]=0.01
82s[N-1]=1
83require(run(N) == (0,1) and mask[N-1] == 1, 'maximum row count and final winner')
84samples=[]
85for _ 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')
88print(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))