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/linear/verify.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 / linear / verify.py` 280 lines ยท 11818 bytes Raw Blame History 1 """External numerical oracle and straight-line warp interpreter for the emitted ISA.""" 2 import argparse 3 import hashlib 4 import json 5 import os 6 import re 7 import struct 8 import subprocess 9 import tempfile 10 from pathlib import Path 11 12 import numpy as np 13 14 ROOT = Path.home() / "work/specialist-transfer-948" 15 U = np.uint32 16 F = np.float32 17 18 19 def digest(data): 20 return hashlib.sha256(data).hexdigest() 21 22 23 def decode(byte): 24 sign = -1 if byte & 128 else 1 25 e, f = (byte >> 3) & 15, byte & 7 26 if e == 15 and f == 7: 27 return np.copysign(np.nan, sign) 28 return sign * (f / 512 if e == 0 else (1 + f / 8) * 2 ** (e - 7)) 29 30 31 def bf(raw): 32 return (np.frombuffer(raw, '= base): 70 continue 71 ix = addr - base 72 if np.all(ix + width <= len(memoryview(buf).cast('B'))): 73 assert np.all(ix % width == 0) 74 return np.frombuffer(buf, dtype=f'> val(a[2]) 94 elif op == 'MOV': 95 regs[d] = val(a[1]) 96 elif op.startswith('LDG.E'): 97 assert a[1:3] == ['desc', 'UR4'] 98 width = {'LDG.E': 4, 'LDG.E.U8': 1, 'LDG.E.U16': 2}[op] 99 regs[d] = U(mem(val(a[3].split('.')[0]), width)) 100 elif op in ('FFMA', 'FMUL', 'FADD'): 101 lhs, rhs = (val(t).view(F) for t in a[1:3]) 102 if op == 'FFMA': 103 result = np.float64(lhs) * np.float64(rhs) + val(a[3]).view(F) 104 else: 105 result = lhs * rhs if op == 'FMUL' else lhs + rhs 106 regs[d] = F(result).view(U) 107 elif op == 'IADD3': 108 regs[d] = sum(U(val(t)) for t in a[-3:]).astype(U) 109 elif op.startswith('ISETP'): 110 lhs, rhs = val(a[2]), val(a[3]) 111 pred = lhs < rhs if op == 'ISETP.LT.U32.AND' else lhs >= rhs 112 elif op == '@P0': 113 assert d == 'BRA' and (np.all(pred) or not np.any(pred)) 114 if np.all(pred): 115 pc = int(a[1], 16) // 16 116 elif op == 'SHFL.BFLY': 117 assert a[-1] == '0x1f' 118 regs[a[1]] = regs[a[2]][:, np.arange(32) ^ int(a[3], 0)] 119 elif op == '@!P0': 120 assert a[:3] == ['STG.E', 'desc', 'UR4'] and np.array_equal(~pred, lanes == 0) 121 np.testing.assert_array_equal(regs[a[3].split('.')[0]][:, 0], addresses[3] + rows[:, 0] * np.uint64(4)) 122 output[:] = regs[a[4]][:, 0] 123 stores += 1 124 elif op == 'EXIT': 125 assert stores == 1 126 return output.view(F) 127 else: 128 raise AssertionError((pc, op, a)) 129 130 131 def get_tensor(f, start, meta, nrows): 132 rows, k = meta['shape'] 133 lo, hi = meta['data_offsets'] 134 size = {'BF16': 2, 'F8_E4M3': 1}[meta['dtype']] 135 assert hi - lo == size * rows * k and nrows <= rows 136 f.seek(start + lo) 137 data = f.read(nrows * k * size) 138 assert len(data) == nrows * k * size 139 return data 140 141 142 def examples(): 143 for name, root, keys in [ 144 ('q27', ROOT/'q27', ['linear_attn.in_proj_a', 'linear_attn.in_proj_qkv']), 145 ('q35', Path.home()/'models/qwen3.6-35b-a3b-fp8-`95a723d0`', 146 ['linear_attn.in_proj_b', 'mlp.shared_expert.up_proj', 'mlp.experts.7.down_proj']), 147 ]: 148 p = root/'layers-0.safetensors' 149 expected = dict(line.split()[::-1] for line in (ROOT/f'{name}-upstream.sha256').read_text().splitlines()) 150 with p.open('rb') as f: 151 sha = hashlib.file_digest(f, 'sha256').hexdigest() 152 assert sha == expected[p.name] 153 f.seek(0) 154 start = 8 + struct.unpack(' bound) 265 if sr != sc: 266 bad = header.copy() 267 bad[3:5] = [sc, sr] 268 assert subprocess.run([exe,'s'],input=struct.pack('<9Q',*bad)).returncode == 2 269 for index in range(9): 270 bad = header.copy() 271 bad[index] += 1 if index else 2 272 rc = subprocess.run([exe, 'r'], input=struct.pack('<9Q', *bad)+payload, stdout=subprocess.DEVNULL).returncode 273 assert rc == 2, (name,index,rc) 274 for bad in [packet[:-1], packet+b'X', packet[:71]]: 275 assert subprocess.run([exe,'r'],input=bad,stdout=subprocess.DEVNULL).returncode == 2 276 print('PASS oracle, instruction simulation, all decode bits, malformed fields, short and trailing input') 277 278 279 if __name__ == '__main__': 280 main()