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_tgemm_diff_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include "omega_cubin.h" |
| 5 | #define CUDAPP_MIGRATION_PENDING_C_CODEGEN_TGEMM 1 |
| 6 | #define omega_tgemm_spec_ok oracle_tgemm_spec_ok |
| 7 | #define omega_tgemm_shared_bytes oracle_tgemm_shared_bytes |
| 8 | #define omega_tgemm_span oracle_tgemm_span |
| 9 | #define omega_tgemm_bm oracle_tgemm_bm |
| 10 | #define omega_blackwell_codegen_tgemm_prog oracle_tgemm_prog |
| 11 | #include "../src/omega_blackwell_codegen_tgemm.c" |
| 12 | #undef omega_tgemm_spec_ok |
| 13 | #undef omega_tgemm_shared_bytes |
| 14 | #undef omega_tgemm_span |
| 15 | #undef omega_tgemm_bm |
| 16 | #undef omega_blackwell_codegen_tgemm_prog |
| 17 | extern int omega_tgemm_spec_ok(const OmegaTgemmSpec *); |
| 18 | extern uint32_t omega_tgemm_shared_bytes(const OmegaTgemmSpec *); |
| 19 | extern uint32_t omega_tgemm_span(const OmegaTgemmSpec *); |
| 20 | extern uint32_t omega_tgemm_bm(const OmegaTgemmSpec *); |
| 21 | extern int omega_blackwell_codegen_tgemm_prog(const OmegaTgemmSpec *, BlackwellIRProgram *); |
| 22 | static BlackwellIRProgram a,b; |
| 23 | static unsigned long long cases; |
| 24 | static uint32_t mutant_unit(uint64_t *result,uint64_t k,uint64_t n,uint64_t shape,uint64_t z,void *ins,uint64_t ni,void *meta,uint64_t nm) { |
| 25 | uint32_t rc=omega_cg_tgemm(result,k,n,shape,z,ins,ni,meta,nm); |
| 26 | if (!rc && !*result) ((uint64_t *)ins)[6]^=UINT64_C(1)<<32; |
| 27 | return rc; |
| 28 | } |
| 29 | static int equal_program(int ca,int cb,int pack) { |
| 30 | if ((ca==0)!=(cb==0)) {fprintf(stderr,"TGEMM refusal C=%d Omega=%d\n",ca,cb);return 0;} |
| 31 | if (ca) return 1; |
| 32 | /* Equal complete IR and allocator state are stronger than equal encoded bytes: |
| 33 | * both branches feed the same deterministic encoder and cubin packer. */ |
| 34 | int equal=a.count==b.count && !memcmp(a.insns,b.insns,a.count*sizeof a.insns[0]) && |
| 35 | !memcmp(&a.regalloc,&b.regalloc,sizeof a.regalloc); |
| 36 | if (!equal || pack) { |
| 37 | static uint8_t x[BW_MAX_IR_INSNS*16],y[BW_MAX_IR_INSNS*16]; |
| 38 | size_t nx=0,ny=0; |
| 39 | int ex=omega_bw_encode_program(&a,x,sizeof x,&nx),ey=omega_bw_encode_program(&b,y,sizeof y,&ny); |
| 40 | if (!equal || ex || ey || nx!=ny || memcmp(x,y,nx)) { |
| 41 | fprintf(stderr,"TGEMM mismatch count=%zu/%zu encode=%d/%d bytes=%zu/%zu\n",a.count,b.count,ex,ey,nx,ny); |
| 42 | for(size_t i=0;i<a.count && i<b.count;i++) if(memcmp(&a.insns[i],&b.insns[i],sizeof a.insns[i])) { |
| 43 | const BlackwellIRInsn *u=&a.insns[i],*v=&b.insns[i]; |
| 44 | fprintf(stderr,"first IR %zu op=%d/%d d=%d/%d a=%d/%d b=%d/%d c=%d/%d imm=%x/%x ctrl=%x/%x subs=%u,%u,%u,%u / %u,%u,%u,%u\n",i,u->op,v->op,u->dst_vreg,v->dst_vreg,u->src1_vreg,v->src1_vreg,u->src2_vreg,v->src2_vreg,u->src3_vreg,v->src3_vreg,u->imm,v->imm,u->control,v->control,u->dst_subreg,u->src1_subreg,u->src2_subreg,u->src3_subreg,v->dst_subreg,v->src1_subreg,v->src2_subreg,v->src3_subreg);break; |
| 45 | } |
| 46 | return 0; |
| 47 | } |
| 48 | uint8_t *cx=NULL,*cy=NULL;size_t lx=0,ly=0;char error[128]; |
| 49 | const OmegaCubinParam params[]={{OMEGA_CUBIN_PARAM_BASE,8,1},{OMEGA_CUBIN_PARAM_BASE+8,8,1},{OMEGA_CUBIN_PARAM_BASE+16,8,1}}; |
| 50 | ex=omega_cubin_pack("tgemm",x,nx,params,3,OMEGA_CUBIN_PARAM_BASE+24,omega_bw_gpr_declared(a.regalloc.peak_gpr_usage),0,&cx,&lx,error,sizeof error); |
| 51 | ey=omega_cubin_pack("tgemm",y,ny,params,3,OMEGA_CUBIN_PARAM_BASE+24,omega_bw_gpr_declared(b.regalloc.peak_gpr_usage),0,&cy,&ly,error,sizeof error); |
| 52 | equal=!ex && !ey && lx==ly && !memcmp(cx,cy,lx); |
| 53 | omega_cubin_free(cx);omega_cubin_free(cy); |
| 54 | if(!equal){fprintf(stderr,"TGEMM cubin mismatch %d/%d\n",ex,ey);return 0;} |
| 55 | } |
| 56 | cases++;return 1; |
| 57 | } |
| 58 | static int check(OmegaTgemmSpec *s,int pack) { |
| 59 | if (oracle_tgemm_spec_ok(s)!=omega_tgemm_spec_ok(s) || |
| 60 | oracle_tgemm_shared_bytes(s)!=omega_tgemm_shared_bytes(s) || |
| 61 | oracle_tgemm_span(s)!=omega_tgemm_span(s) || oracle_tgemm_bm(s)!=omega_tgemm_bm(s)) { |
| 62 | fprintf(stderr,"TGEMM spec/query mismatch K=%u N=%u RT=%u CT=%u W=%u KC=%u AQN=%u layout=%u epi=%u mutant=%d ok=%d/%d shared=%u/%u span=%u/%u bm=%u/%u\n",s->k,s->n,s->rt,s->ct,s->warps,s->kc,s->aqn,s->layout,s->epi,s->mutant,oracle_tgemm_spec_ok(s),omega_tgemm_spec_ok(s),oracle_tgemm_shared_bytes(s),omega_tgemm_shared_bytes(s),oracle_tgemm_span(s),omega_tgemm_span(s),oracle_tgemm_bm(s),omega_tgemm_bm(s));return 0; |
| 63 | } |
| 64 | int ca=oracle_tgemm_prog(s,&a),cb=omega_blackwell_codegen_tgemm_prog(s,&b); |
| 65 | if (!equal_program(ca,cb,pack)) { |
| 66 | fprintf(stderr,"K=%u N=%u RT=%u W=%u AQN=%u mutant=%d\n",s->k,s->n,s->rt,s->warps,s->aqn,s->mutant);return 0; |
| 67 | } |
| 68 | return 1; |
| 69 | } |
| 70 | static int refusals(void) { |
| 71 | const OmegaTgemmSpec good={128,128,2,2,8,32,1,0,0,0}; |
| 72 | if (omega_tgemm_spec_ok(NULL) || oracle_tgemm_spec_ok(NULL) || |
| 73 | omega_tgemm_shared_bytes(NULL) || omega_tgemm_span(NULL) || omega_tgemm_bm(NULL) || |
| 74 | !omega_blackwell_codegen_tgemm_prog(&good,NULL) || !oracle_tgemm_prog(&good,NULL) || |
| 75 | !omega_blackwell_codegen_tgemm_prog(NULL,&b) || !oracle_tgemm_prog(NULL,&a)) return 0; |
| 76 | for(unsigned field=0;field<10;field++) for(unsigned choice=0;choice<4;choice++) { |
| 77 | OmegaTgemmSpec s=good; |
| 78 | const uint32_t values[]={0,1,UINT32_MAX,65537}; |
| 79 | switch(field) { |
| 80 | case 0:s.k=values[choice];break;case 1:s.n=values[choice];break; |
| 81 | case 2:s.rt=values[choice];break;case 3:s.ct=values[choice];break; |
| 82 | case 4:s.warps=values[choice];break;case 5:s.kc=values[choice];break; |
| 83 | case 6:s.aqn=values[choice];break;case 7:s.layout=values[choice];break; |
| 84 | case 8:s.epi=values[choice];break;case 9:s.mutant=(int)values[choice];break; |
| 85 | } |
| 86 | if(!check(&s,0))return 0; |
| 87 | } |
| 88 | return 1; |
| 89 | } |
| 90 | int main(int argc,char **argv) { |
| 91 | int exhaustive=argc==2 && !strcmp(argv[1],"--exhaustive"); |
| 92 | if(argc>2 || (argc==2 && !exhaustive))return 2; |
| 93 | if(!refusals()){fprintf(stderr,"TGEMM negative controls failed\n");return 1;} |
| 94 | for(unsigned rt=2;rt<=8;rt*=2) for(unsigned w=8;w<=16;w*=2) |
| 95 | for(unsigned aqn=1;aqn<=2;aqn++) for(int mut=0;mut<=5;mut++) { |
| 96 | OmegaTgemmSpec s={128,w*16,rt,2,w,32,aqn,0,0,mut}; |
| 97 | if(!check(&s,1))return 1; |
| 98 | for(unsigned k=128;k<=65536;k+=128) { |
| 99 | s.k=k; |
| 100 | if(exhaustive) { |
| 101 | for(unsigned n=w*16;n<131072;n+=w*16) {s.n=n;if(!check(&s,0))return 1;} |
| 102 | } else {s.n=w*16;if(!check(&s,0))return 1;} |
| 103 | } |
| 104 | if(!exhaustive)for(unsigned n=w*16;n<131072;n+=w*16) {s.k=128;s.n=n;if(!check(&s,0))return 1;} |
| 105 | } |
| 106 | OmegaTgemmSpec s={128,128,2,2,8,32,1,0,0,0}; |
| 107 | uint64_t shape=s.rt|((uint64_t)s.warps<<8)|((uint64_t)s.aqn<<16); |
| 108 | if(oracle_tgemm_prog(&s,&a) || omega_gpu_om_ir_replay(mutant_unit,s.k,s.n,shape,0,&b,NULL))return 1; |
| 109 | if(equal_program(0,0,0)){fprintf(stderr,"TGEMM mutant escaped\n");return 1;} |
| 110 | printf("CODEGEN TGEMM MUTANT CAUGHT\n"); |
| 111 | printf("CODEGEN TGEMM %s DIFF PASS (%llu accepted cases)\n",exhaustive?"EXHAUSTIVE":"AXES",cases); |
| 112 | return 0; |
| 113 | } |