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 / codegen_moe_diff_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include "omega_blackwell_codegen_moe.h" |
| 6 | #include "omega_blackwell_codegen_moe2.h" |
| 7 | #include "omega_cubin.h" |
| 8 | #include "cudapp_native.h" |
| 9 | #include "omega_gpu_om_ir.h" |
| 10 | extern int omega_blackwell_codegen_fp8_gemv_prog_oracle(const OmegaFp8GemvSpec *, BlackwellIRProgram *); |
| 11 | extern int omega_blackwell_codegen_fp8_gemv2_prog_oracle(const OmegaFp8GemvSpec *, BlackwellIRProgram *); |
| 12 | extern uint32_t omega_fp8_gemv_steps_oracle(const OmegaFp8GemvSpec *); |
| 13 | extern uint32_t omega_fp8_gemv_shared_bytes_oracle(const OmegaFp8GemvSpec *); |
| 14 | extern uint32_t omega_fp8_gemv2_shared_bytes_oracle(const OmegaFp8GemvSpec *); |
| 15 | extern int omega_fp8_gemv2_spec_ok_oracle(const OmegaFp8GemvSpec *); |
| 16 | static BlackwellIRProgram a,b; |
| 17 | static unsigned cases, refused; |
| 18 | static int equal(unsigned version, const OmegaFp8GemvSpec *s) { |
| 19 | int ra=version==1?omega_blackwell_codegen_fp8_gemv_prog_oracle(s,&a):omega_blackwell_codegen_fp8_gemv2_prog_oracle(s,&a); |
| 20 | int rb=version==1?omega_blackwell_codegen_fp8_gemv_prog(s,&b):omega_blackwell_codegen_fp8_gemv2_prog(s,&b); |
| 21 | if(ra!=rb)goto fail; |
| 22 | if(ra){refused++;return 1;} |
| 23 | if(a.count!=b.count||memcmp(a.insns,b.insns,a.count*sizeof a.insns[0])||memcmp(&a.regalloc,&b.regalloc,sizeof a.regalloc))goto fail; |
| 24 | static uint8_t x[BW_MAX_IR_INSNS*16],y[BW_MAX_IR_INSNS*16];size_t nx=0,ny=0; |
| 25 | if(omega_bw_encode_program(&a,x,sizeof x,&nx)||omega_bw_encode_program(&b,y,sizeof y,&ny)||nx!=ny||memcmp(x,y,nx))goto fail; |
| 26 | uint8_t *cx=NULL,*cy=NULL;size_t lx=0,ly=0;char error[256]; |
| 27 | const OmegaCubinParam params[]={{OMEGA_FP8_ARG_TAB,8,1},{OMEGA_FP8_ARG_X,8,1},{OMEGA_FP8_ARG_Y,8,1},{OMEGA_FP8_ARG_RG,4,0},{OMEGA_FP8_ARG_XSTRIDE,4,0},{OMEGA_FP8_ARG_YSTRIDE,4,0},{OMEGA_FP8_ARG_STRIDE,4,0}}; |
| 28 | uint32_t shared=version==1?omega_fp8_gemv_shared_bytes(s):omega_fp8_gemv2_shared_bytes(s); |
| 29 | int ex=omega_cubin_pack("moe",x,nx,params,7,OMEGA_FP8_ARG_TAB+40,omega_bw_gpr_declared(a.regalloc.peak_gpr_usage),shared,&cx,&lx,error,sizeof error); |
| 30 | int ey=omega_cubin_pack("moe",y,ny,params,7,OMEGA_FP8_ARG_TAB+40,omega_bw_gpr_declared(b.regalloc.peak_gpr_usage),shared,&cy,&ly,error,sizeof error); |
| 31 | int ceq=!ex&&!ey&&lx==ly&&!memcmp(cx,cy,lx);omega_cubin_free(cx);omega_cubin_free(cy); |
| 32 | if(!ceq)goto fail; |
| 33 | cases++;return 1; |
| 34 | fail: |
| 35 | fprintf(stderr,"MOE%u DIFF k=%u lb=%u rows=%u warps=%u depth=%u mutant=%d sched=%u coal=%u wfirst=%u C=%d Omega=%d count=%zu/%zu\n",version,s->k,s->lane_bytes,s->rows,s->warps,s->depth,s->mutant,s->sched,s->coal,s->wfirst,ra,rb,a.count,b.count); |
| 36 | for(size_t i=0;i<a.count&&i<b.count;i++)if(memcmp(&a.insns[i],&b.insns[i],sizeof a.insns[i])) { |
| 37 | fprintf(stderr,"IR %zu op=%d/%d ctrl=%x/%x dst=%d/%d src=%d,%d,%d/%d,%d,%d imm=%x/%x u=%d/%d\n",i,a.insns[i].op,b.insns[i].op,a.insns[i].control,b.insns[i].control,a.insns[i].dst_vreg,b.insns[i].dst_vreg,a.insns[i].src1_vreg,a.insns[i].src2_vreg,a.insns[i].src3_vreg,b.insns[i].src1_vreg,b.insns[i].src2_vreg,b.insns[i].src3_vreg,a.insns[i].imm,b.insns[i].imm,a.insns[i].ureg,b.insns[i].ureg);break; |
| 38 | } |
| 39 | return 0; |
| 40 | } |
| 41 | static void set_axis(OmegaFp8GemvSpec *s,unsigned axis,uint32_t value){ |
| 42 | switch(axis){ |
| 43 | case 0:s->k=value;break;case 1:s->lane_bytes=value;break; |
| 44 | case 2:s->rows=value;break;case 3:s->warps=value;break; |
| 45 | case 4:s->depth=value;break;case 5:s->mutant=(int32_t)value;break; |
| 46 | case 6:s->sched=value;break;case 7:s->coal=value;break;case 8:s->wfirst=value;break; |
| 47 | } |
| 48 | } |
| 49 | static uint32_t corrupt(uint64_t *r,uint64_t k,uint64_t a0,uint64_t q,uint64_t c,void *records,uint64_t nr,void *meta,uint64_t nm){ |
| 50 | const uint64_t fields[16]={256,8,1,1}; |
| 51 | (void)k;(void)a0;(void)q;(void)c; |
| 52 | uint32_t rc=omega_cg_moe(r,fields,16,records,nr,meta,nm); |
| 53 | if(!rc&&!*r)((uint64_t*)records)[6]^=1; |
| 54 | return rc; |
| 55 | } |
| 56 | static int negative(void){ |
| 57 | OmegaFp8GemvSpec s={256,8,1,1,0,0,0,0,0}; |
| 58 | if(omega_blackwell_codegen_fp8_gemv_prog_oracle(&s,&a)||omega_gpu_om_ir_replay(corrupt,0,0,0,0,&b,NULL))return 0; |
| 59 | if(a.count==b.count&&!memcmp(a.insns,b.insns,a.count*sizeof a.insns[0]))return 0; |
| 60 | puts("CODEGEN MOE RECORD NEGATIVE CONTROL CAUGHT");return 1; |
| 61 | } |
| 62 | int main(int argc,char **argv) { |
| 63 | if(argc==11){OmegaFp8GemvSpec s={0};unsigned v=(unsigned)strtoul(argv[1],0,0);s.k=(uint32_t)strtoul(argv[2],0,0);s.lane_bytes=(uint32_t)strtoul(argv[3],0,0);s.rows=(uint32_t)strtoul(argv[4],0,0);s.warps=(uint32_t)strtoul(argv[5],0,0);s.depth=(uint32_t)strtoul(argv[6],0,0);s.mutant=atoi(argv[7]);s.sched=(uint32_t)strtoul(argv[8],0,0);s.coal=(uint32_t)strtoul(argv[9],0,0);s.wfirst=(uint32_t)strtoul(argv[10],0,0);return equal(v,&s)?0:1;} |
| 64 | for(unsigned v=1;v<=2;v++)for(unsigned k=256;k<=32768;k+=256)for(unsigned lb=8;lb<=16;lb*=2)for(unsigned r=1;r<=8;r*=2)for(unsigned w=1;w<=32;w++)for(unsigned d=0;d<=(v==1?16:0);d++)for(int m=0;m<=(v==1?5:3);m++)for(unsigned sc=0;sc<=(v==1?1:0);sc++)for(unsigned co=0;co<=(v==1?1:0);co++)for(unsigned wf=0;wf<=(v==1?1:0);wf++){ |
| 65 | OmegaFp8GemvSpec s={k,lb,r,w,d,m,sc,co,wf}; |
| 66 | if(v==1){if(omega_fp8_gemv_steps(&s)!=omega_fp8_gemv_steps_oracle(&s)||omega_fp8_gemv_shared_bytes(&s)!=omega_fp8_gemv_shared_bytes_oracle(&s))return 1;if(!omega_fp8_gemv_steps_oracle(&s))continue;} |
| 67 | else{if(omega_fp8_gemv2_spec_ok(&s)!=omega_fp8_gemv2_spec_ok_oracle(&s)||omega_fp8_gemv2_shared_bytes(&s)!=omega_fp8_gemv2_shared_bytes_oracle(&s))return 1;if(!omega_fp8_gemv2_spec_ok_oracle(&s))continue;} |
| 68 | if(!equal(v,&s))return 1; |
| 69 | /* Variant 2 never reads these four scalar axes in the C oracle. Exercise |
| 70 | * every bit and all-ones independently for every accepted structural tuple. */ |
| 71 | if(v==2)for(unsigned axis=4;axis<=8;axis++){ |
| 72 | if(axis==5)continue; |
| 73 | for(unsigned bit=0;bit<=32;bit++){ |
| 74 | OmegaFp8GemvSpec scalar=s;set_axis(&scalar,axis,bit==32?UINT32_MAX:UINT32_C(1)<<bit); |
| 75 | if(!equal(v,&scalar))return 1; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | const uint32_t edges[]={0,1,2,3,4,7,8,15,16,17,31,32,33,127,128,255,256,257,511,512,513,32767,32768,32769,UINT32_MAX}; |
| 80 | for(unsigned v=1;v<=2;v++)for(unsigned axis=0;axis<9;axis++){ |
| 81 | for(unsigned i=0;i<sizeof edges/sizeof edges[0];i++){ |
| 82 | OmegaFp8GemvSpec s={512,8,1,3,2,0,1,1,1};set_axis(&s,axis,edges[i]);if(!equal(v,&s))return 1; |
| 83 | } |
| 84 | for(unsigned bit=0;bit<32;bit++){ |
| 85 | OmegaFp8GemvSpec s={512,8,1,3,2,0,1,1,1};set_axis(&s,axis,UINT32_C(1)<<bit);if(!equal(v,&s))return 1; |
| 86 | } |
| 87 | } |
| 88 | OmegaFp8GemvSpec valid={512,8,1,3,2,0,1,1,1}; |
| 89 | if(omega_blackwell_codegen_fp8_gemv_prog(NULL,&b)!=-1||omega_blackwell_codegen_fp8_gemv_prog(&valid,NULL)!=-1|| |
| 90 | omega_blackwell_codegen_fp8_gemv2_prog(NULL,&b)!=-1||omega_blackwell_codegen_fp8_gemv2_prog(&valid,NULL)!=-1||!negative())return 1; |
| 91 | printf("CODEGEN MOE DIFF PASS (%u programs, %u refusals)\n",cases,refused);return 0; |
| 92 | } |