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
CUDApp / tests / amd-exec / prep_section.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | """Compact notebook cases using the canonical attention prep/gate interval oracle.""" |
| 4 | import base64 |
| 5 | from fractions import Fraction |
| 6 | import pathlib |
| 7 | import runpy |
| 8 | import struct |
| 9 | import sys |
| 10 | HERE=pathlib.Path(__file__).parent |
| 11 | ref=runpy.run_path(str(HERE/'token_ref.py')) |
| 12 | execute,export,round32,finite,imul,point,sigmoid=(ref[n] for n in ('execute','export','round32','finite','imul','point','sigmoid')) |
| 13 | fixture,normrope,rope_reference,bf16=(ref['ref'][n] for n in ('fixture','normrope','rope_reference','bf16')) |
| 14 | |
| 15 | def prep_case(position): |
| 16 | cs=[round32(x) for x in rope_reference(position)] |
| 17 | image,p,q,k,v,qw,kw,cap=fixture(position,cs) |
| 18 | intervals=[]; half=[]; exact=[] |
| 19 | for h,row in enumerate(q): |
| 20 | for d,(lo,hi) in enumerate(normrope(row,qw,cs)): intervals.append([p[6]+h*1024+d*4,str(lo),str(hi)]) |
| 21 | for h,row in enumerate(k): |
| 22 | for d,bounds in enumerate(normrope(row,kw,cs)): |
| 23 | lo,hi=sorted(finite(bf16(round32(x))<<16) for x in bounds) |
| 24 | half.append([p[7]+(h*cap+position)*512+d*2,str(lo),str(hi)]) |
| 25 | exact.extend([p[8]+(h*cap+position)*512+d*2,bf16(x),2] for d,x in enumerate(v[h])) |
| 26 | sizes=[p[i+1]-p[i]-32 for i in range(8)]+[len(image)-p[8]-32] |
| 27 | return dict(label=f'QK norm RoPE64/256 KV append position={position} cap={cap}',memory=base64.b64encode(image).decode(), |
| 28 | args=[['ptr',x] for x in p]+[['u32',position],['u32',cap]],gx=25,gy=2,timing_grid=[24,1], |
| 29 | input_bytes=sum(sizes[:6]),intervals=intervals,half_intervals=half,exact=exact, |
| 30 | buffers=[dict(offset=x,bytes=n) for x,n in zip(p,sizes)]) |
| 31 | |
| 32 | def gate_case(kind): |
| 33 | p=[128,128+24*2048+32,128+24*2048+32+24*1024+32] |
| 34 | sizes=[24*2048,24*1024,24*1024]; image=bytearray([0xa5])*(p[2]+sizes[2]+32) |
| 35 | struct.pack_into('<3Q',image,0,*p); intervals=[] |
| 36 | for h in range(24): |
| 37 | x=[round32(Fraction((h*17+d*3)%37-18,8)) for d in range(256)] |
| 38 | g=[round32(Fraction((h*5+d*7)%41-20,4)) for d in range(256)] |
| 39 | if kind=='extreme': g[:4]=[0,0x80000000,0x42c80000,0xc2c80000] |
| 40 | struct.pack_into('<256I',image,p[0]+h*2048+1024,*g); struct.pack_into('<256I',image,p[1]+h*1024,*x) |
| 41 | for d,(a,b) in enumerate(zip(x,g)): |
| 42 | lo,hi=imul(point(a),sigmoid(b)); intervals.append([p[2]+h*1024+d*4,str(lo),str(hi)]) |
| 43 | return dict(label='attention sigmoid gate '+kind,memory=base64.b64encode(image).decode(),args=[['ptr',x] for x in p], |
| 44 | gx=97,gy=2,timing_grid=[96,1],input_bytes=sum(sizes[:2]),intervals=intervals, |
| 45 | buffers=[dict(offset=x,bytes=n) for x,n in zip(p,sizes)]) |
| 46 | |
| 47 | SPECS=(('attn256_prep',prep_case,(0,63,64,65,1),tuple('prep-'+s for s in ('no-one','wrong-pair','wrong-position','truncate-cache'))), |
| 48 | ('attn256_gate',gate_case,('extreme','varied'),('prep-extra-swish','prep-wrong-gate-stride'))) |
| 49 | def suite(exe,out): |
| 50 | for name,factory,parameters,controls in SPECS: |
| 51 | cases=[factory(arg) for arg in parameters] |
| 52 | for case in cases: |
| 53 | width=execute(exe,out/(name+'.text'),out,case) |
| 54 | worst=ref['midpoint_error']((out/'token.memory').read_bytes(),case) |
| 55 | print(f'PASS {name} {case["label"]} interval_escapes=0 worst_midpoint_error={float(worst):.12g} widest_interval={width} every-output/input/cache-history/guards=PASS',flush=True) |
| 56 | for control in controls: |
| 57 | try: execute(exe,out/(control+'.text'),out,cases[-1]) |
| 58 | except AssertionError as e: |
| 59 | assert str(e).startswith('token mismatch'),e |
| 60 | print(f'CONTROL {control} CAUGHT',flush=True) |
| 61 | else: raise AssertionError('control escaped '+control) |
| 62 | export(name,cases,controls,out,factory,[(p,) for p in parameters]) |
| 63 | if __name__=='__main__': suite(sys.argv[1],pathlib.Path(sys.argv[2])) |