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 / om / q38-head / check.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* External verification instrument. Never linked into the runtime. */ |
| 3 | #include "bw_warp_sim_k.h" |
| 4 | #include "components/native-frame.h" |
| 5 | #include "omega_gpu_session.h" |
| 6 | #include <float.h> |
| 7 | #include <assert.h> |
| 8 | #include "../standalone-oracles/oracle.h" |
| 9 | static uint32_t bits(float f) { uint32_t u; memcpy(&u,&f,4); return u; } |
| 10 | static int chip; |
| 11 | static OmegaGpuMem code; |
| 12 | static void demand(int rc) {if(rc) {fprintf(stderr,"GPU: %s\n",omega_gpu_session_last_error());exit(4);}} |
| 13 | static void execute(float **v,uint32_t n,float hn,unsigned order) { |
| 14 | if(chip) { |
| 15 | OmegaGpuMem mem[4]; uint32_t args[10]; |
| 16 | omega_gpu_session_lock(); |
| 17 | for(unsigned j=0;j<4;j++) { |
| 18 | demand(omega_gpu_session_alloc(so_bytes(n,4,32),&mem[j])); |
| 19 | memcpy(mem[j].cpu,v[j],so_bytes(n,4,32));demand(omega_gpu_session_sync_to_gpu(&mem[j])); |
| 20 | args[j*2]=(uint32_t)mem[j].va;args[j*2+1]=(uint32_t)(mem[j].va>>32); |
| 21 | } |
| 22 | args[8]=bits(hn);args[9]=n; |
| 23 | OmegaGpuLaunch l={0};l.code_va=code.va;l.gpr_count=64;l.threads_x=256;l.threads_y=1; |
| 24 | l.grid_x=(uint32_t)so_call(7,n,0,0,0);l.grid_y=1;l.num_elements=n;l.args=args;l.n_args=10;l.timeout_ms=20000; |
| 25 | demand(omega_gpu_session_launch(&l,NULL,NULL)); |
| 26 | for(unsigned j=0;j<4;j++) { |
| 27 | demand(omega_gpu_session_sync_to_cpu(&mem[j]));memcpy(v[j],mem[j].cpu,so_bytes(n,4,32)); |
| 28 | omega_gpu_session_free(&mem[j]); |
| 29 | } |
| 30 | omega_gpu_session_unlock();return; |
| 31 | } |
| 32 | static WsSim s; memset(&s,0,sizeof s);s.p=&native_program;s.threads=256;s.order=order; |
| 33 | s.nranges=4; |
| 34 | for(unsigned j=0;j<4;j++) { |
| 35 | uintptr_t p=(uintptr_t)v[j];s.cbank[(896+j*8)/4]=(uint32_t)p;s.cbank[(896+j*8)/4+1]=(uint32_t)(p>>32); |
| 36 | s.ranges[j].lo=p;s.ranges[j].hi=so_call(5,p,so_bytes(n,4,0),0,0); |
| 37 | } |
| 38 | s.cbank[928/4]=bits(hn);s.cbank[932/4]=n; |
| 39 | int rc=ws_run(&s,(uint32_t)so_call(7,n,0,0,0),1); |
| 40 | if(rc) {fprintf(stderr,"sim n%u order%u: %s\n",n,order,s.msg);exit(2);} |
| 41 | } |
| 42 | static unsigned run(uint32_t n, float hn, unsigned order) { |
| 43 | float *v[4]; |
| 44 | for(unsigned j=0;j<4;j++) {v[j]=calloc(so_bytes(n,1,32),4);assert(v[j]);} |
| 45 | for(uint32_t i=0;i<n;i++) { |
| 46 | v[0][i]=ldexpf(so_float(so_call(26,i,0,0,0)), (int)so_float(so_call(31,i,0,0,0))); |
| 47 | v[1][i]=ldexpf(so_float(so_call(26,i,1,0,0)), (int)so_float(so_call(32,i,0,0,0))); |
| 48 | } |
| 49 | if(so_call(33,n,4,0,0)) {v[0][0]=FLT_MAX; v[1][0]=FLT_MAX; v[0][1]=-FLT_MAX; v[1][1]=FLT_MAX; |
| 50 | v[0][2]=0;v[1][2]=0;v[0][3]=FLT_TRUE_MIN;v[1][3]=FLT_TRUE_MIN;} |
| 51 | for(unsigned j=2;j<4;j++) for(uint32_t i=0;i<so_bytes(n,1,32);i++) v[j][i]=12345.0f; |
| 52 | execute(v,n,hn,order); |
| 53 | unsigned bad=0; |
| 54 | for(uint32_t i=0;i<n;i++) { |
| 55 | /* Independent binary128 oracle. Preserve the original precision: seeded |
| 56 | * Omega has no f128. Do not replace these reference operations by f64. */ |
| 57 | long double radius=(long double)v[1][i]*hn; |
| 58 | long double lo=(long double)v[0][i]-radius,hi=(long double)v[0][i]+radius; |
| 59 | long double slack=radius*0x1p-16L+fabsl(v[0][i])*0x1p-16L+0x1p-96L; |
| 60 | /* A conservatively inflated radius may itself overflow even when one |
| 61 | * endpoint cancels. The contract then explicitly retains this row. */ |
| 62 | int tight=so_any(radius+slack>FLT_MAX, |
| 63 | so_all(so_any(lo-slack < -FLT_MAX,(long double)v[2][i]>=lo-slack,0), |
| 64 | so_any(hi+slack > FLT_MAX,(long double)v[3][i]<=hi+slack,0),1),0); |
| 65 | int ok=so_all((long double)v[2][i]<=lo,(long double)v[3][i]>=hi,tight); |
| 66 | if(!ok) { |
| 67 | if(!bad) fprintf(stderr,"bad row%u s%g c%g hn%g lo%g hi%g\n",i,v[0][i],v[1][i],hn,v[2][i],v[3][i]); |
| 68 | bad=so_bad(bad,ok); |
| 69 | } |
| 70 | } |
| 71 | for(unsigned j=2;j<4;j++)for(uint32_t i=n;i<so_bytes(n,1,32);i++)assert(so_eq(so_bits(v[j][i]),so_bits(12345.0f))); |
| 72 | for(unsigned j=0;j<4;j++)free(v[j]); |
| 73 | return bad; |
| 74 | } |
| 75 | static void read_floats(const char *dir,const char *name,float *v,size_t n) { |
| 76 | char path[4096];assert(snprintf(path,sizeof path,"%s/%s",dir,name)<(int)sizeof path); |
| 77 | FILE *f=fopen(path,"rb");assert(f);assert(fread(v,4,n,f)==n);assert(fgetc(f)==EOF);fclose(f); |
| 78 | } |
| 79 | static void replay(const char *screens,const char *captured) { |
| 80 | const uint32_t n=248320;unsigned positions[]={1,16,32,64,96,97,98,127}; |
| 81 | float *v[4],*full=calloc(n,4),h[5120];assert(full); |
| 82 | for(unsigned j=0;j<4;j++){v[j]=calloc(so_bytes(n,1,32),4);assert(v[j]);} |
| 83 | read_floats(screens,"coef.f32",v[1],n); |
| 84 | for(unsigned p=0;p<sizeof positions/sizeof *positions;p++) { |
| 85 | char name[64];snprintf(name,sizeof name,"%u-screen.f32",positions[p]);read_floats(screens,name,v[0],n); |
| 86 | snprintf(name,sizeof name,"%u-hidden.f32",positions[p]);read_floats(captured,name,h,5120); |
| 87 | snprintf(name,sizeof name,"%u-logits.f32",positions[p]);read_floats(captured,name,full,n); |
| 88 | /* Independent binary128 oracle, including directed-rounding libm. */ |
| 89 | long double sum=0; |
| 90 | for(unsigned j=0;j<5120;j++)sum=nextafterl(sum+(long double)h[j]*h[j],INFINITY); |
| 91 | float hn=nextafterf((float)nextafterl(sqrtl(sum),INFINITY),INFINITY);assert(so_call(24,bits(hn),0,0,0)); |
| 92 | for(unsigned j=2;j<4;j++)for(uint32_t i=0;i<n+32;i++)v[j][i]=12345.0f; |
| 93 | execute(v,n,hn,0); |
| 94 | float tau=-INFINITY;unsigned winner=0,candidates=0,violations=0; |
| 95 | for(uint32_t i=0;i<n;i++) { |
| 96 | assert(so_all(so_call(24,bits(full[i]),0,0,0),so_call(24,bits(v[0][i]),0,0,0),so_all(so_call(24,bits(v[1][i]),0,0,0),so_call(29,bits(v[1][i]),bits(0.0f),0,0),1))); |
| 97 | if(so_call(37,bits(full[i]),bits(full[winner]),0,0))winner=i; |
| 98 | tau=so_float(so_call(30,bits(v[2][i]),bits(tau),0,0)); |
| 99 | violations=so_bad(violations,so_all(so_call(29,bits(full[i]),bits(v[2][i]),0,0),so_call(29,bits(v[3][i]),bits(full[i]),0,0),1)); |
| 100 | } |
| 101 | for(uint32_t i=0;i<n;i++)candidates=(unsigned)so_call(5,candidates,so_call(29,bits(v[3][i]),bits(tau),0,0),0,0); |
| 102 | for(unsigned j=2;j<4;j++)for(uint32_t i=n;i<so_bytes(n,1,32);i++)assert(so_eq(bits(v[j][i]),bits(12345.0f))); |
| 103 | int retained=(int)so_call(29,bits(v[3][winner]),bits(tau),0,0); |
| 104 | printf("replay position=%u candidates=%u violations=%u winner_retained=%d\n",positions[p],candidates,violations,retained); |
| 105 | assert(so_all(so_eq(violations,0),retained,1)); |
| 106 | } |
| 107 | for(unsigned j=0;j<4;j++)free(v[j]); |
| 108 | free(full); |
| 109 | } |
| 110 | int main(int argc,char **argv) { |
| 111 | if((argc!=2&&argc!=3&&argc!=5)||!frame_load(argv[1],0))return 65; |
| 112 | chip=argc>=3&&!strcmp(argv[2],"--chip"); |
| 113 | if(argc>=3&&!chip)return 65; |
| 114 | if(chip) { |
| 115 | unsigned char bytes[16384]; FILE *f=fopen(argv[1],"rb");assert(f); |
| 116 | size_t off=48+16*(native_program.regalloc.num_vregs+native_program.regalloc.num_uvregs)+64*native_program.count; |
| 117 | assert(!fseek(f,(long)off,SEEK_SET));size_t nb=fread(bytes,1,sizeof bytes,f);assert(!ferror(f));fclose(f); |
| 118 | omega_gpu_session_lock();demand(omega_gpu_session_open()?0:-1); |
| 119 | demand(omega_gpu_session_alloc_code(nb,&code)); |
| 120 | memcpy(code.cpu,bytes,nb);demand(omega_gpu_session_sync_to_gpu(&code));omega_gpu_session_unlock(); |
| 121 | } |
| 122 | uint32_t counts[]={1,31,32,33,255,256,257,1027}; |
| 123 | float norms[]={FLT_TRUE_MIN,0x1p-60f,1,123.25f,0x1p60f,FLT_MAX}; |
| 124 | for(unsigned a=0;a<sizeof counts/sizeof *counts;a++) |
| 125 | for(unsigned b=0;b<sizeof norms/sizeof *norms;b++) |
| 126 | for(unsigned o=0;o<2;o++) if(run(counts[a],norms[b],o))return 1; |
| 127 | puts("PASS: bounds enclose long-double reference with bounded slack; tails untouched; both orders/repetitions"); |
| 128 | if(argc==5)replay(argv[3],argv[4]); |
| 129 | if(chip) { |
| 130 | omega_gpu_session_lock();omega_gpu_session_free(&code);omega_gpu_session_unlock();omega_gpu_session_close(); |
| 131 | puts("PASS: generated machine code executed on GPU");return 0; |
| 132 | } |
| 133 | if(!frame_wrong_result())return 66; |
| 134 | if(!run(1027,123.25f,0))return 3; |
| 135 | puts("PASS: arithmetic mutant caught"); |
| 136 | assert(frame_load(argv[1],0)); |
| 137 | int changed=0; |
| 138 | for(size_t i=0;i<native_program.count;i++)if(native_program.insns[i].op==BW_IR_FMUL) { |
| 139 | BlackwellIRInsn *x=&native_program.insns[i]; |
| 140 | x->op=BW_IR_MOV_IMM;x->imm=bits(INFINITY); |
| 141 | x->src1_vreg=x->src2_vreg=x->src3_vreg=-1;changed=1;break; |
| 142 | } |
| 143 | if(!changed||!run(1027,123.25f,0))return 3; |
| 144 | puts("PASS: all-infinite radius mutant caught by bounded slack");return 0; |
| 145 | } |