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/rmsnorm5120.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 / rmsnorm5120.py` 239 lines · 10125 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 EPS=`897988541` 7 MEAN=`961334477` 8 ONE=`1065353216` 9 def round32(value): 10 if not value: 11 return 0 12 sign = `2147483648` if value < 0 else 0 13 value = abs(value) 14 e = value.numerator.bit_length() - value.denominator.bit_length() 15 if value < Fraction(2) ** e: 16 e -= 1 17 quantum = max(-149, e - 23) 18 scaled = value / Fraction(2) ** quantum 19 q, r = divmod(scaled.numerator, scaled.denominator) 20 if 2 * r > scaled.denominator or (2 * r == scaled.denominator and q & 1): 21 q += 1 22 if q == 1 << 24: 23 q >>= 1 24 quantum += 1 25 if quantum > 104: 26 return sign | `2139095040` 27 exponent = quantum + 150 if q >= 1 << 23 else 0 28 return sign | exponent << 23 | q & `8388607` 29 def finite(bits): 30 exponent = bits >> 23 & 255 31 significand = bits & `8388607` | (`8388608` if exponent else 0) 32 power = max(1, exponent) - 150 33 value = Fraction(significand) * Fraction(2) ** power 34 return -value if bits >> 31 else value 35 @lru_cache(None) 36 def fma(a, b, c): 37 ax, bx, cx = (x & `2147483647` for x in (a, b, c)) 38 if max(ax, bx, cx) > `2139095040`: 39 return `2143289344` 40 product_sign = (a ^ b) & `2147483648` 41 if ax == `2139095040` or bx == `2139095040`: 42 if not ax or not bx or (cx == `2139095040` and product_sign != c & `2147483648`): 43 return `2143289344` 44 return product_sign | `2139095040` 45 if cx == `2139095040`: 46 return c 47 if (not ax or not bx) and (not cx): 48 return product_sign & c & `2147483648` 49 return round32(finite(a) * finite(b) + finite(c)) 50 def mul(a, b): 51 return fma(a, b, (a ^ b) & `2147483648`) 52 def add(a, b): 53 return fma(a, ONE, b) 54 def nextdown(b): 55 if b == 0: 56 return `2147483649` 57 return b + 1 if b >> 31 else b - 1 58 def nextup(b): 59 if b == `2147483648`: 60 return 1 61 return b - 1 if b >> 31 else b + 1 62 def ftz(b): 63 return b & `2147483648` if b & `2147483647` < `8388608` else b 64 def enclosure(value): 65 center = round32(value) 66 exponent = max(1, center >> 23 & 255) - 150 67 ulp = Fraction(2) ** exponent 68 uncertainty = max(abs(value), Fraction(1)) * Fraction(1, 10 ** 100) 69 lower, upper = (value - ulp - uncertainty, value + ulp + uncertainty) 70 lo, hi = (round32(lower), round32(upper)) 71 if finite(lo) > lower: 72 lo = nextdown(lo) 73 if finite(hi) < upper: 74 hi = nextup(hi) 75 return (finite(ftz(lo)), finite(ftz(hi))) 76 @lru_cache(None) 77 def rsqbound(x): 78 assert x > 0 79 with localcontext() as ctx: 80 ctx.prec = 120 81 result = 1 / (Decimal(x.numerator) / Decimal(x.denominator)).sqrt() 82 return enclosure(Fraction(result)) 83 def rounded(x): 84 return finite(round32(x)) 85 @lru_cache(None) 86 def imul(a, b): 87 vals = [x * y for x in a for y in b] 88 return (rounded(min(vals)), rounded(max(vals))) 89 def point(bits): 90 x = finite(bits) 91 return (x, x) 92 def fixture(rows, kind): 93 xp, wp, op = (64, 64 + rows * 5120 * 4 + 64, 64 + rows * 5120 * 4 + 64 + 5120 * 4 + 64) 94 image = bytearray([165]) * (op + rows * 5120 * 4 + 64) 95 struct.pack_into('<3QI', image, 0, xp, wp, op, rows) 96 weights = [round32(Fraction(i * 19 % 67 - 33, 32)) for i in range(5120)] 97 struct.pack_into('<5120I', image, wp, *weights) 98 intervals = [] 99 for row in range(rows): 100 x = [round32(Fraction((i * 17 + row * 5) % 97 - 48, 16)) for i in range(5120)] 101 if kind == 'zero': 102 x = [0] * 5120 103 elif kind == 'large': 104 x = [mul(v, `1124073472`) for v in x] 105 else: 106 x[:4] = [0, `2147483648`, 1, `2147483649`] 107 struct.pack_into('<5120I', image, xp + row * 5120 * 4, *x) 108 lanes = [0] * 64 109 for lane in range(64): 110 for chunk in range(20): 111 for j in range(4): 112 a = x[chunk * 256 + lane * 4 + j] 113 lanes[lane] = fma(a, a, lanes[lane]) 114 for shift in (32, 16, 8, 4, 2, 1): 115 lanes = [add(lanes[i], lanes[i ^ shift]) for i in range(64)] 116 variance = add(mul(lanes[0], MEAN), EPS) 117 inv = rsqbound(finite(variance)) 118 for i, (a, w) in enumerate(zip(x, weights)): 119 lo, hi = imul(imul(point(a), inv), point(add(ONE, w))) 120 intervals.append([op + (row * 5120 + i) * 4, str(lo), str(hi)]) 121 return dict(label=f'rows={rows} hidden=5120 kind={kind}', memory=base64.b64encode(image).decode(), intervals=intervals, args=[['ptr', xp], ['ptr', wp], ['ptr', op], ['u32', rows]], gx=rows + 1, gy=2, input_bytes=(rows + 1) * 5120 * 4 + 4, timing_grid=[max(1, rows), 1]) 122 def _make_payload(): 123 cases=[fixture(*args) for args in ((0, 'varied'), (1, 'zero'), (2, 'varied'), (3, 'large'), (1, 'varied'))] 124 return dict(name='rmsnorm5120',cases=cases,controls=['rmsnorm5120-no-one', 'rmsnorm5120-wrong-mean', 'rmsnorm5120-dropped-chunk'],timing=len(cases)-1) 125 """Test-only notebook instrument. token.py exports this with generated fixtures. 126 No production dependency; the numerical intervals come from the host oracle. 127 """ 128 129 def run_section(ctx): 130 import base64 131 import ctypes as C 132 import math 133 import struct 134 import time 135 from fractions import Fraction 136 p = _make_payload() 137 name = p['name'] 138 say, put, get = (ctx[k] for k in ('say', 'put', 'get')) 139 load, Params, launch = (ctx[k] for k in ('load', 'Params', 'launch')) 140 objects, controls = (ctx['objs'], ctx['controls']) 141 fn = load(objects[name], name) 142 owned = [] 143 144 def alloc(data): 145 d = put(data) 146 owned.append(d) 147 return d 148 149 def raw(case): 150 return base64.b64decode(case['memory']) 151 152 def step(case, base, function=fn): 153 vals = [C.c_void_p(base.value + v) if kind == 'ptr' else C.c_uint32(v) for kind, v in case['args']] 154 return (function, case['gx'], case['gy'], Params(vals)) 155 156 def check(actual, case): 157 before = raw(case) 158 assert len(actual) == len(before), 'notebook memory size mismatch' 159 unchanged = bytearray(actual) 160 widest = Fraction(0) 161 for off in case.get('bf16_outputs', []): 162 assert struct.unpack_from('