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/aien-sovereign-core-mv/blob/main/om/qwen-native/blocks/tests/check.py # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / om / qwen-native / blocks / tests / check.py` 198 lines ยท 9050 bytes Raw Blame History 1 """CPU-only numerical gate. Run with python3 tests/check.py /path/to/seed/out/omc.""" 2 import hashlib 3 from pathlib import Path 4 import struct 5 import subprocess 6 import sys 7 import tempfile 8 9 import numpy as np 10 11 ROOT = Path(__file__).resolve().parents[1] 12 PIN = "`34d611ca0415eab1728d3234911d90e74af17ba9ab0c581d559f48274d7ca6d3`" 13 F = np.float32 14 TOL = 0.00004 15 16 17 def sigmoid(x): 18 return 1 / (1 + np.exp(-x)) 19 20 21 def bf16(x): 22 raw = x.astype("> 16) & 1)) & 0xffff0000).view("hv", k, state)) * sigmoid(b[:, None]) 44 state += k[:, :, None] * innovation[:, None, :] 45 out = np.einsum("hk,hkv->hv", q, state) 46 return (out / np.sqrt((out**2).mean(1, keepdims=True) + 1e-6) * 47 w[41056:] * z * sigmoid(z)).ravel() 48 49 50 def att_ref(model, pos, x, w, keys, values): 51 nh, nk = {27: (24, 4), 35: (16, 2)}[model] 52 q, gate = x[:nh * 512].reshape(nh, 2, 256).transpose(1, 0, 2) 53 k, v = x[nh * 512:nh * 512 + nk * 512].reshape(2, nk, 256) 54 angles = pos * `10000000`.0 ** (-np.arange(32) / 32) 55 def rotate(row, weight): 56 # Preserve sequential f32 sum before the discontinuous BF16 cache rounding. 57 ss = np.cumsum(row * row, axis=1)[:, -1:] 58 row = row * (F(1) / np.sqrt(ss / F(256) + F(1e-6))) * (F(1) + weight) 59 a, b = row[:, :32].copy(), row[:, 32:64].copy() 60 c, s = np.cos(angles).astype(F), np.sin(angles).astype(F) 61 row[:, :32], row[:, 32:64] = a * c - b * s, a * s + b * c 62 return row 63 keys.append(bf16(rotate(k, w[256:]))) 64 values.append(bf16(v)) 65 query = rotate(q, w[:256]).astype(float) 66 logits = np.einsum("hd,thd->ht", query, np.repeat(keys, nh // nk, axis=1)) / 16 67 probs = np.exp(logits - logits.max(1, keepdims=True)) 68 probs /= probs.sum(1, keepdims=True) 69 return (np.einsum("ht,thd->hd", probs, np.repeat(values, nh // nk, axis=1)) * 70 sigmoid(gate.astype(float))).ravel() 71 72 73 def att_samples(keys, values): 74 j = np.arange(64) 75 h = j % keys[-1].shape[0] 76 d = (j * 19) % 256 77 return np.r_[keys[-1][h, d], values[-1][h, d]] 78 79 80 def fixtures(): 81 rng = np.random.default_rng(949) 82 records, expected = [], [] 83 for model, last in [(27, 63), (35, 39)]: 84 heads = 48 if model == 27 else 32 85 width = 4096 + heads * 128 86 layers = [0, 3, last - 1, last] 87 weights = {l: rng.normal(0, 0.45, 512 if l % 4 == 3 else 41184).astype(F) for l in layers} 88 for l in layers[::2]: 89 weights[l][41056:] += 1 90 # Restart the same slots at zero with new inputs after the first sequence. 91 for length in (6, 2): 92 states = {l: ([np.zeros(width)] * 3, np.zeros((heads, 128, 128))) 93 if l % 4 != 3 else ([], []) for l in layers} 94 for pos in range(length): 95 for l in layers: 96 attn = l % 4 == 3 97 x = rng.normal(0, 0.7, 14336 if attn else 16480).astype("= TOL: 171 raise AssertionError(baseline) 172 for label, old, new in [ 173 ("reverse taps", "get w add tap j", "get w add tap sub 3 j"), 174 ("swap a/b", "get x ab rate bias", "get x add ab 1 rate bias"), 175 ("discard prior state", "if not resetting\nsv =", "if false\nsv ="), 176 ("rotate wrong partner", "b = get qa_row add i 32", "b = get qa_row add i 128"), 177 ("truncate BF16", "shr add bits add 32767 and shr bits 16 1 16", "shr bits 16"), 178 ("use SiLU full gate", "mul get qa_row j f32 qb_sig", "mul gate mul get qa_row j f32 qb_sig"), 179 ]: 180 if unit.count(old) != 1: 181 raise AssertionError(label) 182 compile_unit(compiler, path, unit.replace(old, new)) 183 error = max(compare(path, data, expected).values()) 184 if error <= TOL: 185 raise AssertionError(f"survived: {label}") 186 print("rejected", label, error, flush=True) 187 compile_unit(compiler, path, unit) 188 for m, l, p in [(35, 39, 8192), (27, 64, 0), (35, 40, 0), (27, 0, 1), (38, 3, 0)]: 189 size = (14336 + 512) if l % 4 == 3 else (16480 + 41184) 190 bad = struct.pack("