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 / moe / caller.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* Native ABI caller and unchanged-incumbent oracle comparison. Test logic's |
| 3 | * independent rank verifier lives in moe_verify.om; this is not production. */ |
| 4 | #include "cudapp_native.h" |
| 5 | #include <stdint.h> |
| 6 | #include <stdio.h> |
| 7 | #include <string.h> |
| 8 | #include <stdlib.h> |
| 9 | uint32_t moe_incumbent(uint32_t, const uint32_t *, uint32_t *, uint32_t *, uint32_t *); |
| 10 | static uint64_t cfg[8], meta[8], req[16]; |
| 11 | static uint64_t ids[1024], work[10368], seg[1028], maps[2320]; |
| 12 | static uint32_t words[16], oi[1024], ox[1152], oy[1152], os[1028]; |
| 13 | static uint64_t dirs[514]; |
| 14 | static uint8_t mapwire[9280], segwire[8224], idwire[4096]; |
| 15 | static uint64_t window_cfg[8], windows[1024], window_meta[4]; |
| 16 | #define CHECK(x) do { if (!(x)) { fprintf(stderr,"FAIL line %d: %s\n",__LINE__,#x); exit(1); } } while (0) |
| 17 | static uint64_t plan(void) { |
| 18 | uint64_t r = 99; |
| 19 | CHECK(omega_moe_plan(&r,cfg,8,ids,1024,work,10368,seg,1028,maps,2320,meta,8)==0); |
| 20 | return r; |
| 21 | } |
| 22 | static uint64_t verify(void) { |
| 23 | uint64_t r = 99; |
| 24 | CHECK(omega_moe_verify(&r,cfg,8,ids,1024,work,10368,seg,1028,maps,2320,meta,8)==0); |
| 25 | return r; |
| 26 | } |
| 27 | static void setup(uint32_t n, uint32_t slots) { |
| 28 | uint64_t c[8] = {1,n,slots,256,256,1152,257,1160}; memcpy(cfg,c,sizeof c); |
| 29 | memset(ids,0,sizeof ids); memset(work,0x5a,sizeof work); memset(seg,0x5a,sizeof seg); |
| 30 | memset(maps,0x5a,sizeof maps); memset(meta,0x5a,sizeof meta); |
| 31 | } |
| 32 | static void dense(uint32_t n, unsigned mode) { |
| 33 | setup(n,8); |
| 34 | for (uint32_t t=0;t<n;t++) for (uint32_t s=0;s<8;s++) { |
| 35 | uint32_t v=0; |
| 36 | switch(mode) { |
| 37 | case 0: v=7; break; /* all same, duplicate picks */ |
| 38 | case 1: v=(t*8+s)%256; break; /* all distinct where possible */ |
| 39 | case 2: v=s%3; break; /* duplicate within token */ |
| 40 | case 3: v=t%2 ? 255 : s; break; /* uneven occupancy */ |
| 41 | default: v=(t*17+s*13)%256; break; /* ragged expert segment sizes */ |
| 42 | } |
| 43 | ids[t*8+s]=v; |
| 44 | } |
| 45 | CHECK(plan()==0); CHECK(verify()==0); |
| 46 | uint64_t r=99; |
| 47 | memset(mapwire,0x5a,sizeof mapwire); memset(segwire,0x5a,sizeof segwire); |
| 48 | CHECK(omega_moe_maps32(&r,meta,8,maps,2320,mapwire,9280)==0 && r==0); |
| 49 | for (uint32_t j=0;j<257;j++) { dirs[j*2]=(uint64_t)j*256+4096; dirs[j*2+1]=dirs[j*2]+128; } |
| 50 | CHECK(omega_moe_segs32(&r,seg,1028,meta,8,dirs,514,segwire,8224)==0 && r==0); |
| 51 | for (uint32_t j=0;j<n*8;j++) oi[j]=(uint32_t)ids[j]; |
| 52 | uint32_t ns=moe_incumbent(n,oi,ox,oy,os); |
| 53 | CHECK(meta[1]==ns); |
| 54 | for (uint32_t j=0;j<n*9;j++) { CHECK(maps[j]==ox[j]); CHECK(maps[1160+j]==oy[j]); } |
| 55 | for (uint32_t j=0;j<ns*4;j++) CHECK(seg[j]==os[j]); |
| 56 | CHECK(!memcmp(mapwire,ox,n*9*sizeof *ox)); |
| 57 | CHECK(!memcmp(mapwire+1160*4,oy,n*9*sizeof *oy)); |
| 58 | for (uint32_t z=0;z<ns;z++) { |
| 59 | uint64_t addr[2], reserved; uint32_t range[2]; |
| 60 | memcpy(addr,segwire+z*32,16); memcpy(range,segwire+z*32+16,8); memcpy(&reserved,segwire+z*32+24,8); |
| 61 | CHECK(addr[0]==dirs[os[z*4]*2] && addr[1]==dirs[os[z*4]*2+1]); |
| 62 | CHECK(range[0]==os[z*4+1] && range[1]==os[z*4+2] && reserved==0); |
| 63 | } |
| 64 | /* Independent GROUP format oracle: rank (expert, original contribution row), |
| 65 | including shared. This is a format check, NOT execution of GPU instructions. */ |
| 66 | if (n*9<=128) for (uint32_t t=0;t<n;t++) for (uint32_t s=0;s<9;s++) { |
| 67 | uint32_t e=s==8?256:ids[t*8+s], rank=0; |
| 68 | for (uint32_t u=0;u<n;u++) for (uint32_t v=0;v<9;v++) { |
| 69 | uint32_t f=v==8?256:ids[u*8+v]; |
| 70 | if (f<e || (f==e && u*9+v<t*9+s)) rank++; |
| 71 | } |
| 72 | CHECK(maps[rank]==t); CHECK(maps[1160+rank]==t*9+s); |
| 73 | } |
| 74 | if (n) { |
| 75 | uint32_t v=work[8]; work[8]=v+1; CHECK(verify()!=0); work[8]=v; |
| 76 | uint32_t p=(uint32_t)meta[2]*9; v=work[p]; work[p]=0; CHECK(verify()!=0); work[p]=v; |
| 77 | v=maps[1160]; maps[1160]=UINT32_MAX; CHECK(verify()!=0); maps[1160]=v; |
| 78 | v=seg[2]; seg[2]=0; CHECK(verify()!=0); seg[2]=v; |
| 79 | CHECK(verify()==0); |
| 80 | } |
| 81 | } |
| 82 | static void refusal(uint64_t expected) { |
| 83 | uint64_t w[10368],s[1028],p[2320],m[8]; |
| 84 | memcpy(w,work,sizeof w); memcpy(s,seg,sizeof s); memcpy(p,maps,sizeof p); memcpy(m,meta,sizeof m); |
| 85 | CHECK(plan()==expected); CHECK(!memcmp(w,work,sizeof w)); CHECK(!memcmp(s,seg,sizeof s)); |
| 86 | CHECK(!memcmp(p,maps,sizeof p)); CHECK(!memcmp(m,meta,sizeof m)); |
| 87 | } |
| 88 | static uint64_t group(void) { |
| 89 | uint64_t r=99; CHECK(omega_moe_group(&r,req,16,(uint8_t *)words,64)==0); return r; |
| 90 | } |
| 91 | int main(void) { |
| 92 | const uint32_t sizes[]={0,1,2,3,7,14,15,31,127,128}; |
| 93 | for (unsigned j=0;j<sizeof sizes/sizeof *sizes;j++) for (unsigned k=0;k<5;k++) dense(sizes[j],k); |
| 94 | setup(3,8); for (unsigned j=0;j<24;j++) ids[j]=j%4 ? UINT32_MAX : j; |
| 95 | CHECK(plan()==0); CHECK(verify()==0); /* absent routes retain original slots */ |
| 96 | setup(128,8); for (unsigned j=0;j<1024;j++) ids[j]=UINT32_MAX; |
| 97 | CHECK(plan()==0); CHECK(verify()==0); CHECK(meta[0]==128 && meta[2]==0 && meta[1]==1); |
| 98 | setup(5,0); CHECK(plan()==0); CHECK(verify()==0); CHECK(meta[0]==5 && meta[2]==0); |
| 99 | setup(128,8); cfg[5]=1151; refusal(3); cfg[5]=1152; cfg[7]=1159; refusal(3); |
| 100 | setup(1,8); cfg[6]=1; refusal(3); |
| 101 | setup(1,8); ids[3]=256; refusal(2); ids[3]=UINT32_MAX-1; refusal(2); |
| 102 | setup(1,8); cfg[0]=2; refusal(1); cfg[0]=1; cfg[1]=129; refusal(1); |
| 103 | setup(1,8); cfg[2]=9; refusal(1); setup(1,8); cfg[4]=255; refusal(1); |
| 104 | setup(1,8); cfg[5]=1153; refusal(1); setup(1,8); cfg[6]=258; refusal(1); |
| 105 | setup(1,8); cfg[7]=1161; refusal(1); setup(1,8); cfg[3]=0; refusal(1); |
| 106 | setup(1,8); cfg[3]=257; refusal(1); |
| 107 | setup(1,8); uint64_t result=77; |
| 108 | CHECK(omega_moe_plan(&result,cfg,8,ids,1024,work,10368,work,1028,maps,2320,meta,8)==OMEGA_BAD_ARG); |
| 109 | CHECK(result==77); CHECK(work[0]==UINT64_C(0x5a5a5a5a5a5a5a5a)); |
| 110 | CHECK(omega_moe_plan(&result,cfg,8,ids,1023,work,10368,seg,1028,maps,2320,meta,8)==OMEGA_BAD_ARG); |
| 111 | CHECK(result==77); |
| 112 | for (unsigned j=0;j<1024;j++) { uint32_t v=j%257; memcpy(idwire+j*4,&v,4); } |
| 113 | CHECK(omega_moe_ids32(&result,idwire,4096,1024,ids,1024)==0 && result==0); |
| 114 | for (unsigned j=0;j<1024;j++) CHECK(ids[j]==j%257); |
| 115 | CHECK(omega_moe_ids32(&result,idwire,4096,1025,ids,1024)==0 && result==1); |
| 116 | setup(1,8); CHECK(plan()==0); |
| 117 | memset(mapwire,90,sizeof mapwire); maps[0]=UINT64_MAX; |
| 118 | CHECK(omega_moe_maps32(&result,meta,8,maps,2320,mapwire,9280)==0 && result==2); |
| 119 | for (unsigned j=0;j<sizeof mapwire;j++) CHECK(mapwire[j]==90); |
| 120 | CHECK(plan()==0); memset(segwire,90,sizeof segwire); dirs[0]=0; |
| 121 | CHECK(omega_moe_segs32(&result,seg,1028,meta,8,dirs,514,segwire,8224)==0 && result==3); |
| 122 | for (unsigned j=0;j<sizeof segwire;j++) CHECK(segwire[j]==90); |
| 123 | dirs[0]=4096; seg[2]=UINT64_MAX; |
| 124 | CHECK(omega_moe_segs32(&result,seg,1028,meta,8,dirs,514,segwire,8224)==0 && result==2); |
| 125 | for (unsigned j=0;j<sizeof segwire;j++) CHECK(segwire[j]==90); |
| 126 | for (unsigned n=0;n<=128;n++) { |
| 127 | uint64_t wc[8]={1,n,2048,512,9,512,128,0}; memcpy(window_cfg,wc,sizeof wc); |
| 128 | CHECK(omega_moe_windows(&result,window_cfg,8,windows,1024,window_meta,4)==0 && result==0); |
| 129 | uint64_t first=0; |
| 130 | for (unsigned z=0;z<window_meta[0];z++) { |
| 131 | uint64_t *v=windows+z*8; |
| 132 | CHECK(v[0]==first && v[1]>0 && v[2]==v[1]*9 && v[2]<=128); |
| 133 | CHECK(v[3]==first*2048*4 && v[4]==first*9*512*2*4); |
| 134 | CHECK(v[5]==first*9*512*4 && v[6]==first*9*2048*4 && v[7]==first*512); |
| 135 | first+=v[1]; |
| 136 | } |
| 137 | CHECK(first==n && window_meta[1]==n && window_meta[2]==n*9 && window_meta[3]==1); |
| 138 | } |
| 139 | uint64_t old_windows[1024],old_wmeta[4]; memcpy(old_windows,windows,sizeof windows); memcpy(old_wmeta,window_meta,sizeof window_meta); |
| 140 | window_cfg[6]=1; |
| 141 | CHECK(omega_moe_windows(&result,window_cfg,8,windows,1024,window_meta,4)==0 && result==2); |
| 142 | CHECK(!memcmp(old_windows,windows,sizeof windows) && !memcmp(old_wmeta,window_meta,sizeof window_meta)); |
| 143 | window_cfg[6]=128; window_cfg[7]=1; |
| 144 | CHECK(omega_moe_windows(&result,window_cfg,8,windows,1024,window_meta,4)==0 && result==1); |
| 145 | CHECK(!memcmp(old_windows,windows,sizeof windows) && !memcmp(old_wmeta,window_meta,sizeof window_meta)); |
| 146 | uint64_t q[16]={4096,8192,12288,65536,131072,196608,262144,512,14,126,134,9,1,1,1,256}; |
| 147 | memcpy(req,q,sizeof q); CHECK(group()==0); |
| 148 | for (unsigned i=0;i<7;i++) { CHECK(words[i*2]==req[i]); CHECK(words[i*2+1]==0); } |
| 149 | CHECK(words[14]==512 && words[15]==14); |
| 150 | uint32_t before[16]; memcpy(before,words,sizeof words); |
| 151 | #define BAD(field,value,rc) do { uint64_t save=req[field]; req[field]=value; CHECK(group()==rc); CHECK(!memcmp(before,words,sizeof words)); req[field]=save; } while(0) |
| 152 | BAD(8,15,1); BAD(8,0,1); BAD(11,8,1); BAD(15,255,1); |
| 153 | BAD(9,125,2); BAD(10,133,2); BAD(7,0,2); BAD(7,513,3); BAD(0,4097,3); |
| 154 | BAD(12,0,4); BAD(13,2,4); BAD(14,0,4); |
| 155 | BAD(3,4096,5); BAD(4,65536,5); BAD(5,65536,5); BAD(6,196608,5); |
| 156 | BAD(0,UINT64_MAX-7,5); BAD(3,UINT64_MAX-7,5); BAD(7,128,2); BAD(8,UINT64_MAX,1); |
| 157 | req[12]=0; req[13]=0; CHECK(group()==0); req[12]=1; req[13]=1; |
| 158 | puts("MOE-INDEX HOST PASS: native ABI, incumbent pf_group, GROUP format, sparse rank, refusals, mutants"); |
| 159 | return 0; |
| 160 | } |