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 / gdnconv.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | """BF16 conv4/cache + SiLU oracle, following the reference's dtype boundaries.""" |
| 4 | import base64 |
| 5 | from fractions import Fraction |
| 6 | import pathlib |
| 7 | import runpy |
| 8 | import struct |
| 9 | import sys |
| 10 | |
| 11 | HERE=pathlib.Path(__file__).parent |
| 12 | ref=runpy.run_path(str(HERE/'token_ref.py')) |
| 13 | finite,round32,fma,point,imul,sigmoid,execute,export=(ref[n] for n in ('finite','round32','fma','point','imul','sigmoid','execute','export')) |
| 14 | bf16=ref['ref']['bf16'] |
| 15 | CHANNELS=16*128*2+48*128 |
| 16 | CASES=((64,'zero'),(128,'range'),(CHANNELS,'history')) |
| 17 | CONTROLS=('gdnconv4-no-input-round','gdnconv4-no-conv-round','gdnconv4-no-silu-round','gdnconv4-wrong-cache-shift') |
| 18 | |
| 19 | |
| 20 | def fixture(active,kind): |
| 21 | pointers=[]; end=64 |
| 22 | for size in (CHANNELS*4,CHANNELS*8,CHANNELS*8,CHANNELS*8,CHANNELS*4): |
| 23 | pointers.append(end); end+=size+64 |
| 24 | xp,wp,sp,np,op=pointers |
| 25 | image=bytearray([0xa5])*end |
| 26 | struct.pack_into('<5Q',image,0,*pointers) |
| 27 | inputs=[round32(Fraction((i*19)%127-63,32)) for i in range(CHANNELS)] |
| 28 | inputs[:8]=[0x3f808000,0x3f818000,0xbf808000,0xbf818000,1,0x80000001,0x42c80000,0xc2c80000] |
| 29 | if kind=='range': inputs[8:12]=[0x7f800000,0xff800000,0x7f801234,0xffc01234] |
| 30 | struct.pack_into('<'+str(CHANNELS)+'I',image,xp,*inputs) |
| 31 | intervals=[]; exact=[] |
| 32 | for c in range(CHANNELS): |
| 33 | weights=[bf16(round32(Fraction((c*7+j*11)%31-15,16))) for j in range(4)] |
| 34 | old=[0 if kind=='zero' else bf16(round32(Fraction((c*17+j*5)%61-30,16))) for j in range(4)] |
| 35 | struct.pack_into('<4H',image,wp+c*8,*weights) |
| 36 | struct.pack_into('<4H',image,sp+c*8,*old) |
| 37 | if c>=active: continue |
| 38 | new=old[1:]+[bf16(inputs[c])] |
| 39 | exact.extend([np+c*8+j*2,x,2] for j,x in enumerate(new)) |
| 40 | total=0 |
| 41 | for a,w in zip(new,weights): total=fma(a<<16,w<<16,total) |
| 42 | conv=bf16(total)<<16 |
| 43 | if conv & 0x7fffffff >= 0x7f800000: |
| 44 | answer=0x7f800000 if conv==0x7f800000 else 0x7fc00000 |
| 45 | exact.append([op+c*4,answer,4]) |
| 46 | else: |
| 47 | endpoints=imul(point(conv),sigmoid(conv)) |
| 48 | ends=[finite(bf16(round32(x))<<16) for x in endpoints] |
| 49 | intervals.append([op+c*4,str(min(ends)),str(max(ends))]) |
| 50 | return dict(label=f'channels={active}/{CHANNELS} taps=4 kind={kind}',memory=base64.b64encode(image).decode(), |
| 51 | intervals=intervals,exact=exact,bf16_outputs=[v[0] for v in intervals],args=[['ptr',p] for p in pointers], |
| 52 | gx=CHANNELS//64+1 if active==CHANNELS else active//64,gy=2, |
| 53 | timing_grid=[CHANNELS//64,1],input_bytes=CHANNELS*(4+8+8)) |
| 54 | |
| 55 | |
| 56 | def mutate(src,out): |
| 57 | text=(src/'gdnconv4.isa').read_text() |
| 58 | for name,tag in zip(CONTROLS[:3],('INPUT','CONV','SILU')): |
| 59 | start=text.index('# BF16_ROUND_'+tag+'\n') |
| 60 | stop=text.index('# END_BF16_ROUND_'+tag+'\n',start)+len('# END_BF16_ROUND_'+tag+'\n') |
| 61 | (out/(name+'.isa')).write_text(text[:start]+text[stop:]) |
| 62 | old='I 5 144 16 18 16' |
| 63 | assert text.count(old)==1 |
| 64 | (out/(CONTROLS[3]+'.isa')).write_text(text.replace(old,'I 5 128 16 18 16')) |
| 65 | |
| 66 | |
| 67 | def suite(exe,out): |
| 68 | cases=[fixture(n,kind) for n,kind in CASES] |
| 69 | for case in cases: |
| 70 | widest=execute(exe,out/'gdnconv4.text',out,case) |
| 71 | print(f'PASS gdnconv4 {case["label"]} BF16-input/conv/SiLU cache-exact every-output all-inputs/guards widest_interval={widest}',flush=True) |
| 72 | for name in CONTROLS: |
| 73 | try: execute(exe,out/(name+'.text'),out,cases[-1]) |
| 74 | except AssertionError as error: |
| 75 | assert str(error).startswith('token mismatch'),error |
| 76 | print(f'CONTROL {name} CAUGHT {str(error).split(":")[0]}',flush=True) |
| 77 | else: raise AssertionError(f'control escaped {name}') |
| 78 | export('gdnconv4',cases,CONTROLS,out,fixture,CASES) |
| 79 | print(f'PASS GDN convolution channels={CHANNELS} cases={len(CASES)} hardware=NOT_RUN',flush=True) |
| 80 | |
| 81 | |
| 82 | if __name__=='__main__': |
| 83 | if sys.argv[1]=='mutate': mutate(pathlib.Path(sys.argv[2]),pathlib.Path(sys.argv[3])) |
| 84 | elif sys.argv[1]=='suite': suite(sys.argv[2],pathlib.Path(sys.argv[3])) |
| 85 | else: raise SystemExit('unknown convolution mode') |