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 / norm.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | """RMSNorm5120 instruction-boundary Fraction/Decimal oracle, test only.""" |
| 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,add,mul,point,imul,rsqbound,execute,export = ( |
| 14 | ref[n] for n in ('finite','round32','fma','add','mul','point','imul','rsqbound','execute','export')) |
| 15 | CASES = ((0,'varied'),(1,'zero'),(2,'varied'),(3,'large'),(1,'varied')) |
| 16 | CONTROLS = ('rmsnorm5120-no-one','rmsnorm5120-wrong-mean','rmsnorm5120-dropped-chunk') |
| 17 | ONE,EPS,MEAN = 0x3f800000,0x358637bd,0x394ccccd |
| 18 | assert round32(Fraction(1,5120)) == MEAN |
| 19 | |
| 20 | |
| 21 | def fixture(rows,kind): |
| 22 | xp,wp,op = 64,64+rows*5120*4+64,64+rows*5120*4+64+5120*4+64 |
| 23 | image = bytearray([0xa5])*(op+rows*5120*4+64) |
| 24 | struct.pack_into('<3QI',image,0,xp,wp,op,rows) |
| 25 | weights = [round32(Fraction((i*19)%67-33,32)) for i in range(5120)] |
| 26 | struct.pack_into('<5120I',image,wp,*weights) |
| 27 | intervals=[] |
| 28 | for row in range(rows): |
| 29 | x = [round32(Fraction((i*17+row*5)%97-48,16)) for i in range(5120)] |
| 30 | if kind=='zero': x=[0]*5120 |
| 31 | elif kind=='large': x=[mul(v,0x43000000) for v in x] |
| 32 | else: x[:4]=[0,0x80000000,1,0x80000001] |
| 33 | struct.pack_into('<5120I',image,xp+row*5120*4,*x) |
| 34 | lanes=[0]*64 |
| 35 | for lane in range(64): |
| 36 | for chunk in range(20): |
| 37 | for j in range(4): |
| 38 | a=x[chunk*256+lane*4+j] |
| 39 | lanes[lane]=fma(a,a,lanes[lane]) |
| 40 | for shift in (32,16,8,4,2,1): lanes=[add(lanes[i],lanes[i^shift]) for i in range(64)] |
| 41 | variance=add(mul(lanes[0],MEAN),EPS) |
| 42 | inv=rsqbound(finite(variance)) |
| 43 | for i,(a,w) in enumerate(zip(x,weights)): |
| 44 | lo,hi=imul(imul(point(a),inv),point(add(ONE,w))) |
| 45 | intervals.append([op+(row*5120+i)*4,str(lo),str(hi)]) |
| 46 | return dict(label=f'rows={rows} hidden=5120 kind={kind}',memory=base64.b64encode(image).decode(), |
| 47 | intervals=intervals,args=[['ptr',xp],['ptr',wp],['ptr',op],['u32',rows]], |
| 48 | gx=rows+1,gy=2,input_bytes=(rows+1)*5120*4+4,timing_grid=[max(1,rows),1]) |
| 49 | |
| 50 | |
| 51 | def mutate(src,out): |
| 52 | text=(src/'rmsnorm5120.isa').read_text() |
| 53 | changes={'rmsnorm5120-no-one':('I 5 242 12 12 1','I 8 268 1 12'), |
| 54 | 'rmsnorm5120-wrong-mean':('L '+str(MEAN),'L '+str(0x3b800000)), |
| 55 | 'rmsnorm5120-dropped-chunk':('I 3 147 0 13','I 3 146 0 13')} |
| 56 | for name,(old,new) in changes.items(): |
| 57 | assert old in text,name |
| 58 | (out/(name+'.isa')).write_text(text.replace(old,new,1)) |
| 59 | |
| 60 | |
| 61 | def suite(exe,out): |
| 62 | cases=[fixture(rows,kind) for rows,kind in CASES] |
| 63 | for case in cases: |
| 64 | widest=execute(exe,out/'rmsnorm5120.text',out,case) |
| 65 | print(f'PASS rmsnorm5120 {case["label"]} all-outputs/manual-interval all-inputs/guards overdispatch=XY widest_interval={widest}',flush=True) |
| 66 | for name in CONTROLS: |
| 67 | try: execute(exe,out/(name+'.text'),out,cases[-1]) |
| 68 | except AssertionError as error: |
| 69 | assert str(error).startswith('token mismatch'),error |
| 70 | print(f'CONTROL {name} CAUGHT {str(error).split(":")[0]}',flush=True) |
| 71 | else: raise AssertionError(f'control escaped {name}') |
| 72 | export('rmsnorm5120',cases,CONTROLS,out,fixture,CASES) |
| 73 | print(f'PASS RMSNorm5120 rows={",".join(map(str,sorted(set(r for r,_ in CASES))))} cases={len(CASES)} hardware=NOT_RUN',flush=True) |
| 74 | |
| 75 | |
| 76 | if __name__=='__main__': |
| 77 | if sys.argv[1]=='mutate': mutate(pathlib.Path(sys.argv[2]),pathlib.Path(sys.argv[3])) |
| 78 | elif sys.argv[1]=='suite': suite(sys.argv[2],pathlib.Path(sys.argv[3])) |
| 79 | else: raise SystemExit('unknown norm mode') |