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/tests/amd-exec/token_ref.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 / tests / amd-exec / token_ref.py` 146 lines · 7180 bytes Raw Blame History 1 #!/usr/bin/env python3 2 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 """Token-kernel independent numerical oracle and generated notebook instruments.""" 4 import base64 5 from fractions import Fraction 6 from functools import lru_cache 7 import json 8 import pathlib 9 import runpy 10 import struct 11 import subprocess 12 import sys 13 14 HERE = pathlib.Path(__file__).parent 15 ref = runpy.run_path(str(HERE/'prep.py')) 16 finite,round32,fma,add,mul,point,imul,iadd,expbound,intervalrcp,rsqbound = ( 17 ref[n] for n in ('finite','round32','fma','add','mul','point','imul','iadd','expbound','intervalrcp','rsqbound')) 18 ONE,LOG2E = 0x3f800000,0x3fb8aa3b 19 COUNTS = (0,1,63,64,65,17408) 20 CONTROLS = ('mlpsilu-no-silu','mlpsilu-wrong-up') 21 22 @lru_cache(None) 23 def sigmoid(g): 24 exponent = mul((g & 0x7fffffff)|0x80000000,LOG2E) 25 t = expbound(exponent) 26 answer = intervalrcp(iadd(point(ONE),t)) 27 return imul(answer,t) if g>>31 else answer 28 29 30 def mlp_case(n): 31 gp,up,op = 64,64+4*n+64,64+8*n+128 32 image = bytearray([0xa5])*(op+4*n+64) 33 struct.pack_into('<3QI',image,0,gp,up,op,n) 34 gates = [round32(Fraction((i*17)%193-96,8)) for i in range(n)] 35 ups = [round32(Fraction((i*29)%257-128,16)) for i in range(n)] 36 special = [0,0x80000000,1,0x80000001,0x42c80000,0xc2c80000,0x4b000000,0xcb000000] 37 gates[:min(n,len(special))] = special[:n] 38 struct.pack_into('<'+str(n)+'I',image,gp,*gates) 39 struct.pack_into('<'+str(n)+'I',image,up,*ups) 40 intervals = [] 41 for i,(g,u) in enumerate(zip(gates,ups)): 42 lo,hi = imul(imul(point(g),sigmoid(g)),point(u)) 43 intervals.append([op+4*i,str(lo),str(hi)]) 44 return dict(label=f'N={n}',memory=base64.b64encode(image).decode(),intervals=intervals, 45 args=[['ptr',gp],['ptr',up],['ptr',op],['u32',n]],gx=(n+63)//64+1,gy=2,input_bytes=8*n+4, 46 timing_grid=[max(1,(n+63)//64),1]) 47 48 49 def check(actual,case): 50 before = base64.b64decode(case['memory']) 51 assert len(actual)==len(before),'token mismatch memory length' 52 untouched = bytearray(actual) 53 widest = Fraction(0) 54 for off in case.get('bf16_outputs',[]): 55 assert struct.unpack_from('