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/CUDApp/blob/main/evidence/AMD-GFX942-NOTEBOOK-IMPORTS/mlpsilu.py # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. 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 `CUDApp / evidence / AMD-GFX942-NOTEBOOK-IMPORTS / mlpsilu.py` 244 lines · 10014 bytes Raw Blame History 1 # Generated from canonical host fixture/oracle functions; no embedded memory images. 2 import base64,struct 3 from decimal import Decimal,localcontext 4 from fractions import Fraction 5 from functools import lru_cache 6 LOG2E=`1069066811` 7 ONE=`1065353216` 8 def round32(value): 9 if not value: 10 return 0 11 sign = `2147483648` if value < 0 else 0 12 value = abs(value) 13 e = value.numerator.bit_length() - value.denominator.bit_length() 14 if value < Fraction(2) ** e: 15 e -= 1 16 quantum = max(-149, e - 23) 17 scaled = value / Fraction(2) ** quantum 18 q, r = divmod(scaled.numerator, scaled.denominator) 19 if 2 * r > scaled.denominator or (2 * r == scaled.denominator and q & 1): 20 q += 1 21 if q == 1 << 24: 22 q >>= 1 23 quantum += 1 24 if quantum > 104: 25 return sign | `2139095040` 26 exponent = quantum + 150 if q >= 1 << 23 else 0 27 return sign | exponent << 23 | q & `8388607` 28 def finite(bits): 29 exponent = bits >> 23 & 255 30 significand = bits & `8388607` | (`8388608` if exponent else 0) 31 power = max(1, exponent) - 150 32 value = Fraction(significand) * Fraction(2) ** power 33 return -value if bits >> 31 else value 34 def rounded(x): 35 return finite(round32(x)) 36 @lru_cache(None) 37 def imul(a, b): 38 vals = [x * y for x in a for y in b] 39 return (rounded(min(vals)), rounded(max(vals))) 40 def point(bits): 41 x = finite(bits) 42 return (x, x) 43 @lru_cache(None) 44 def fma(a, b, c): 45 ax, bx, cx = (x & `2147483647` for x in (a, b, c)) 46 if max(ax, bx, cx) > `2139095040`: 47 return `2143289344` 48 product_sign = (a ^ b) & `2147483648` 49 if ax == `2139095040` or bx == `2139095040`: 50 if not ax or not bx or (cx == `2139095040` and product_sign != c & `2147483648`): 51 return `2143289344` 52 return product_sign | `2139095040` 53 if cx == `2139095040`: 54 return c 55 if (not ax or not bx) and (not cx): 56 return product_sign & c & `2147483648` 57 return round32(finite(a) * finite(b) + finite(c)) 58 def mul(a, b): 59 return fma(a, b, (a ^ b) & `2147483648`) 60 def ftz(b): 61 return b & `2147483648` if b & `2147483647` < `8388608` else b 62 def nextdown(b): 63 if b == 0: 64 return `2147483649` 65 return b + 1 if b >> 31 else b - 1 66 def nextup(b): 67 if b == `2147483648`: 68 return 1 69 return b - 1 if b >> 31 else b + 1 70 def enclosure(value): 71 center = round32(value) 72 exponent = max(1, center >> 23 & 255) - 150 73 ulp = Fraction(2) ** exponent 74 uncertainty = max(abs(value), Fraction(1)) * Fraction(1, 10 ** 100) 75 lower, upper = (value - ulp - uncertainty, value + ulp + uncertainty) 76 lo, hi = (round32(lower), round32(upper)) 77 if finite(lo) > lower: 78 lo = nextdown(lo) 79 if finite(hi) < upper: 80 hi = nextup(hi) 81 return (finite(ftz(lo)), finite(ftz(hi))) 82 @lru_cache(None) 83 def expbound(bits): 84 bits = ftz(bits) 85 if bits == `4286578688`: 86 return point(0) 87 if bits & `2147483647` == 0: 88 return point(ONE) 89 x = finite(bits) 90 if x < -126: 91 return point(0) 92 assert x < 128, 'reference only accepts finite softmax exponent range' 93 with localcontext() as ctx: 94 ctx.prec = 120 95 value = Decimal(2) ** (Decimal(x.numerator) / Decimal(x.denominator)) 96 return enclosure(Fraction(value)) 97 @lru_cache(None) 98 def rcpbound(x): 99 assert x > 0 100 return enclosure(1 / x) 101 def intervalrcp(a): 102 return (rcpbound(a[1])[0], rcpbound(a[0])[1]) 103 @lru_cache(None) 104 def iadd(a, b): 105 return (rounded(a[0] + b[0]), rounded(a[1] + b[1])) 106 @lru_cache(None) 107 def sigmoid(g): 108 exponent = mul(g & `2147483647` | `2147483648`, LOG2E) 109 t = expbound(exponent) 110 answer = intervalrcp(iadd(point(ONE), t)) 111 return imul(answer, t) if g >> 31 else answer 112 def mlp_case(n): 113 gp, up, op = (64, 64 + 4 * n + 64, 64 + 8 * n + 128) 114 image = bytearray([165]) * (op + 4 * n + 64) 115 struct.pack_into('<3QI', image, 0, gp, up, op, n) 116 gates = [round32(Fraction(i * 17 % 193 - 96, 8)) for i in range(n)] 117 ups = [round32(Fraction(i * 29 % 257 - 128, 16)) for i in range(n)] 118 special = [0, `2147483648`, 1, `2147483649`, `1120403456`, `3267887104`, `1258291200`, `3405774848`] 119 gates[:min(n, len(special))] = special[:n] 120 struct.pack_into('<' + str(n) + 'I', image, gp, *gates) 121 struct.pack_into('<' + str(n) + 'I', image, up, *ups) 122 intervals = [] 123 for i, (g, u) in enumerate(zip(gates, ups)): 124 lo, hi = imul(imul(point(g), sigmoid(g)), point(u)) 125 intervals.append([op + 4 * i, str(lo), str(hi)]) 126 return dict(label=f'N={n}', memory=base64.b64encode(image).decode(), intervals=intervals, args=[['ptr', gp], ['ptr', up], ['ptr', op], ['u32', n]], gx=(n + 63) // 64 + 1, gy=2, input_bytes=8 * n + 4, timing_grid=[max(1, (n + 63) // 64), 1]) 127 def _make_payload(): 128 cases=[mlp_case(*args) for args in ((0,), (1,), (63,), (64,), (65,), (17408,))] 129 return dict(name='mlpsilu',cases=cases,controls=['mlpsilu-no-silu', 'mlpsilu-wrong-up'],timing=len(cases)-1) 130 """Test-only notebook instrument. token.py exports this with generated fixtures. 131 No production dependency; the numerical intervals come from the host oracle. 132 """ 133 134 def run_section(ctx): 135 import base64 136 import ctypes as C 137 import math 138 import struct 139 import time 140 from fractions import Fraction 141 p = _make_payload() 142 name = p['name'] 143 say, put, get = (ctx[k] for k in ('say', 'put', 'get')) 144 load, Params, launch = (ctx[k] for k in ('load', 'Params', 'launch')) 145 objects, controls = (ctx['objs'], ctx['controls']) 146 fn = load(objects[name], name) 147 owned = [] 148 149 def alloc(data): 150 d = put(data) 151 owned.append(d) 152 return d 153 154 def raw(case): 155 return base64.b64decode(case['memory']) 156 157 def step(case, base, function=fn): 158 vals = [C.c_void_p(base.value + v) if kind == 'ptr' else C.c_uint32(v) for kind, v in case['args']] 159 return (function, case['gx'], case['gy'], Params(vals)) 160 161 def check(actual, case): 162 before = raw(case) 163 assert len(actual) == len(before), 'notebook memory size mismatch' 164 unchanged = bytearray(actual) 165 widest = Fraction(0) 166 for off in case.get('bf16_outputs', []): 167 assert struct.unpack_from('