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_qmma_diff_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #include <stdio.h> |
| 3 | #include "omega_cubin.h" |
| 4 | #include <string.h> |
| 5 | #define CUDAPP_MIGRATION_PENDING_C_CODEGEN_QMMA 1 |
| 6 | #define CUDAPP_MIGRATION_PENDING_C_QMMA_PROBE_REST 1 |
| 7 | #define omega_qmma_is_mark oracle_qmma_is_mark |
| 8 | #define omega_qmma_patch_code oracle_qmma_patch_code |
| 9 | #define omega_qmma_patch_selftest oracle_qmma_patch_selftest |
| 10 | #define omega_blackwell_codegen_qmma_probe_prog oracle_qmma_probe |
| 11 | #define omega_blackwell_codegen_qmma_peak_prog oracle_qmma_peak |
| 12 | #define omega_blackwell_codegen_f2e4_probe_prog oracle_f2e4_probe |
| 13 | #include "../src/omega_blackwell_codegen_qmma.c" |
| 14 | #undef omega_blackwell_codegen_qmma_probe_prog |
| 15 | #undef omega_blackwell_codegen_qmma_peak_prog |
| 16 | #undef omega_blackwell_codegen_f2e4_probe_prog |
| 17 | extern int omega_blackwell_codegen_qmma_probe_prog(int, uint32_t, BlackwellIRProgram *); |
| 18 | extern int omega_blackwell_codegen_qmma_peak_prog(uint32_t, uint32_t, uint32_t, uint32_t, BlackwellIRProgram *); |
| 19 | extern int omega_blackwell_codegen_f2e4_probe_prog(BlackwellIRProgram *); |
| 20 | #undef omega_qmma_patch_code |
| 21 | extern int omega_qmma_patch_code(const BlackwellIRProgram *, uint8_t *, size_t); |
| 22 | static uint32_t mutant_unit(uint64_t *result,uint64_t k,uint64_t x,uint64_t y,uint64_t z,void *ins,uint64_t ni,void *meta,uint64_t nm) { |
| 23 | uint32_t rc=omega_cg_qmma(result,k,x,y,z,ins,ni,meta,nm); |
| 24 | if(!rc && !*result) ((uint64_t *)ins)[6]^=UINT64_C(1)<<32; |
| 25 | return rc; |
| 26 | } |
| 27 | static BlackwellIRProgram a,b; |
| 28 | static uint8_t x[BW_MAX_IR_INSNS*16],y[BW_MAX_IR_INSNS*16]; |
| 29 | static size_t nx,ny; |
| 30 | static unsigned cases; |
| 31 | static int same(int ca,int cb,const char *label) { |
| 32 | nx=ny=0; |
| 33 | if ((ca==0)!=(cb==0)) {fprintf(stderr,"%s refusal C=%d Omega=%d\n",label,ca,cb);return 0;} |
| 34 | if(ca) return 1; |
| 35 | 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)) { |
| 36 | fprintf(stderr,"%s differs: count %zu/%zu bytes %zu/%zu\n",label,a.count,b.count,nx,ny); |
| 37 | for(size_t i=0;i<a.count && i<b.count;i++) if(memcmp(x+i*16,y+i*16,16)){fprintf(stderr,"first instruction %zu op %d/%d ctrl %x/%x\n",i,a.insns[i].op,b.insns[i].op,a.insns[i].control,b.insns[i].control);break;} |
| 38 | return 0; |
| 39 | } |
| 40 | int px=oracle_qmma_patch_code(&a,x,nx),py=omega_qmma_patch_code(&b,y,ny); |
| 41 | if(px!=py || memcmp(x,y,nx)){fprintf(stderr,"%s patch differs C=%d Omega=%d\n",label,px,py);return 0;} |
| 42 | uint8_t *cx=NULL,*cy=NULL;size_t lx=0,ly=0;char error[128]; |
| 43 | const OmegaCubinParam params[]={{OMEGA_CUBIN_PARAM_BASE,8,1},{OMEGA_CUBIN_PARAM_BASE+8,8,1}}; |
| 44 | int ex=omega_cubin_pack("qmma",x,nx,params,2,OMEGA_CUBIN_PARAM_BASE+16,omega_bw_gpr_declared(a.regalloc.peak_gpr_usage),0,&cx,&lx,error,sizeof error); |
| 45 | int ey=omega_cubin_pack("qmma",y,ny,params,2,OMEGA_CUBIN_PARAM_BASE+16,omega_bw_gpr_declared(b.regalloc.peak_gpr_usage),0,&cy,&ly,error,sizeof error); |
| 46 | int equal=!ex && !ey && lx==ly && !memcmp(cx,cy,lx); |
| 47 | omega_cubin_free(cx);omega_cubin_free(cy); |
| 48 | if(!equal){fprintf(stderr,"%s cubin differs (%d/%d)\n",label,ex,ey);return 0;} |
| 49 | cases++; return 1; |
| 50 | } |
| 51 | static int refusals(void) { |
| 52 | static const uint32_t invalid[][4] = { |
| 53 | {0,1,1,32}, {1,0,1,32}, {1,17,1,32}, |
| 54 | {1,1,0,32}, {1,1,16,32}, {1,1,1,0}, |
| 55 | {1,1,1,31}, {1,1,1,33}, {1,1,1,1056}, |
| 56 | {1,UINT32_MAX,1,32}, {1,1,UINT32_MAX,32}, |
| 57 | {1,1,1,UINT32_MAX} |
| 58 | }; |
| 59 | for (size_t i=0;i<sizeof invalid/sizeof invalid[0];i++) { |
| 60 | const uint32_t *v=invalid[i]; |
| 61 | if (!oracle_qmma_peak(v[0],v[1],v[2],v[3],&a) || |
| 62 | !omega_blackwell_codegen_qmma_peak_prog(v[0],v[1],v[2],v[3],&b)) return 0; |
| 63 | } |
| 64 | if (!oracle_qmma_probe(-1,0,&a) || !omega_blackwell_codegen_qmma_probe_prog(-1,0,&b) || |
| 65 | !oracle_qmma_probe(2,0,&a) || !omega_blackwell_codegen_qmma_probe_prog(2,0,&b) || |
| 66 | !oracle_qmma_probe(0,17,&a) || !omega_blackwell_codegen_qmma_probe_prog(0,17,&b) || |
| 67 | !oracle_qmma_probe(0,0,NULL) || !omega_blackwell_codegen_qmma_probe_prog(0,0,NULL) || |
| 68 | !oracle_qmma_peak(1,1,1,32,NULL) || !omega_blackwell_codegen_qmma_peak_prog(1,1,1,32,NULL) || |
| 69 | !oracle_f2e4_probe(NULL) || !omega_blackwell_codegen_f2e4_probe_prog(NULL)) return 0; |
| 70 | return 1; |
| 71 | } |
| 72 | static int patch_refusals(void) { |
| 73 | static uint8_t x[BW_MAX_IR_INSNS*16],y[BW_MAX_IR_INSNS*16]; |
| 74 | const unsigned corrupt[]={0,5,11}; |
| 75 | for (int kind=0;kind<2;kind++) { |
| 76 | if (kind ? oracle_f2e4_probe(&a) : oracle_qmma_probe(0,0,&a)) return 0; |
| 77 | size_t len=0,mark=a.count; |
| 78 | if (omega_bw_encode_program(&a,x,sizeof x,&len)) return 0; |
| 79 | for(size_t i=0;i<a.count;i++) |
| 80 | if (a.insns[i].imm==(kind ? OMEGA_QMMA_MARK_F2E4 : OMEGA_QMMA_MARK)) {mark=i;break;} |
| 81 | if(mark==a.count) return 0; |
| 82 | for(size_t q=0;q<sizeof corrupt/sizeof corrupt[0];q++) { |
| 83 | memcpy(y,x,len); |
| 84 | y[mark*16+corrupt[q]]^=1; |
| 85 | memcpy(x,y,len); |
| 86 | if (oracle_qmma_patch_code(&a,x,len)!=-1 || omega_qmma_patch_code(&a,y,len)!=-1) return 0; |
| 87 | if (omega_bw_encode_program(&a,x,sizeof x,&len)) return 0; |
| 88 | } |
| 89 | if (oracle_qmma_patch_code(&a,x,a.count*16-1)!=-1 || omega_qmma_patch_code(&a,x,a.count*16-1)!=-1) return 0; |
| 90 | } |
| 91 | return oracle_qmma_patch_code(NULL,x,0)==-1 && omega_qmma_patch_code(NULL,x,0)==-1 && |
| 92 | oracle_qmma_patch_code(&a,NULL,0)==-1 && omega_qmma_patch_code(&a,NULL,0)==-1; |
| 93 | } |
| 94 | /* MOV_IMM stores its imm32 in word 1; see omega_blackwell_codegen.c. |
| 95 | * Locate the niter=7 MOVI, rather than assuming its instruction index. */ |
| 96 | static int iteration_field(const BlackwellIRProgram *p,size_t len,size_t *field) { |
| 97 | size_t found=p->count; |
| 98 | for(size_t i=0;i<p->count;i++) if(p->insns[i].op==BW_IR_MOV_IMM && p->insns[i].imm==7) { |
| 99 | if(found!=p->count) return 0; |
| 100 | found=i; |
| 101 | } |
| 102 | if(found==p->count || found*16+2*sizeof(uint32_t)>len) return 0; |
| 103 | *field=found*16+sizeof(uint32_t); |
| 104 | return 1; |
| 105 | } |
| 106 | static int iteration_only(const uint8_t *code,size_t len,const uint8_t *base,size_t base_len, |
| 107 | size_t field,uint32_t iterations) { |
| 108 | if(len!=base_len || field+sizeof(uint32_t)>len) return 0; |
| 109 | uint32_t decoded=0; |
| 110 | for(unsigned i=0;i<sizeof(uint32_t);i++) decoded|=(uint32_t)code[field+i]<<(8*i); |
| 111 | return decoded==iterations && !memcmp(code,base,field) && |
| 112 | !memcmp(code+field+sizeof(uint32_t),base+field+sizeof(uint32_t),len-field-sizeof(uint32_t)); |
| 113 | } |
| 114 | static void add_iterations(uint32_t *values,size_t *count,uint32_t value) { |
| 115 | for(size_t i=0;i<*count;i++) if(values[i]==value) return; |
| 116 | values[(*count)++]=value; |
| 117 | } |
| 118 | static int peak_structure(void) { |
| 119 | uint32_t values[3*32+3];size_t count=0; |
| 120 | add_iterations(values,&count,1); |
| 121 | add_iterations(values,&count,UINT32_MAX); |
| 122 | add_iterations(values,&count,7); |
| 123 | for(unsigned bit=0;bit<32;bit++) { |
| 124 | uint32_t value=UINT32_C(1)<<bit; |
| 125 | add_iterations(values,&count,value); |
| 126 | add_iterations(values,&count,value-1); |
| 127 | add_iterations(values,&count,value+1); |
| 128 | } |
| 129 | static uint8_t base_c[BW_MAX_IR_INSNS*16],base_om[BW_MAX_IR_INSNS*16]; |
| 130 | for(unsigned chains=1;chains<=16;chains++) for(unsigned stall=1;stall<=15;stall++) |
| 131 | for(unsigned threads=32;threads<=1024;threads+=32) { |
| 132 | int ca=oracle_qmma_peak(7,chains,stall,threads,&a); |
| 133 | int cb=omega_blackwell_codegen_qmma_peak_prog(7,chains,stall,threads,&b); |
| 134 | size_t field_c=0,field_om=0,base_len=0; |
| 135 | if(!same(ca,cb,"peak baseline") || ca || !iteration_field(&a,nx,&field_c) || |
| 136 | !iteration_field(&b,ny,&field_om) || field_c!=field_om) return 0; |
| 137 | base_len=nx;memcpy(base_c,x,nx);memcpy(base_om,y,ny); |
| 138 | for(size_t v=0;v<count;v++) { |
| 139 | uint32_t iterations=values[v]; |
| 140 | if(iterations!=7) { |
| 141 | ca=oracle_qmma_peak(iterations,chains,stall,threads,&a); |
| 142 | cb=omega_blackwell_codegen_qmma_peak_prog(iterations,chains,stall,threads,&b); |
| 143 | if(!same(ca,cb,"peak iteration")) return 0; |
| 144 | } |
| 145 | if(!iterations) { |
| 146 | if(!ca || !cb) return 0; |
| 147 | continue; |
| 148 | } |
| 149 | /* The baseline is already checked, and the zero case has no output. */ |
| 150 | const uint8_t *cx=iterations==7?base_c:x,*cy=iterations==7?base_om:y; |
| 151 | size_t lx=iterations==7?base_len:nx,ly=iterations==7?base_len:ny; |
| 152 | if(!iteration_only(cx,lx,base_c,base_len,field_c,iterations) || |
| 153 | !iteration_only(cy,ly,base_om,base_len,field_om,iterations)) { |
| 154 | fprintf(stderr,"QMMA niter confinement failed: chains=%u stall=%u threads=%u niter=%u\n", |
| 155 | chains,stall,threads,iterations); |
| 156 | return 0; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | printf("CODEGEN QMMA STRUCTURE EXHAUSTIVE NITER BIT-BASIS PASS (%zu values, zero refused)\n",count); |
| 161 | return 1; |
| 162 | } |
| 163 | static uint32_t dependency_mutant_unit(uint64_t *result,uint64_t k,uint64_t iterations,uint64_t chains, |
| 164 | uint64_t shape,void *ins,uint64_t ni,void *meta,uint64_t nm) { |
| 165 | uint32_t rc=omega_cg_qmma(result,k,iterations,chains,shape,ins,ni,meta,nm); |
| 166 | /* A valid but wrong extra dependence: niter parity changes a prologue stall. */ |
| 167 | if(!rc && !*result && k==1 && (iterations&1)) ((uint64_t *)ins)[6]^=UINT64_C(1)<<32; |
| 168 | return rc; |
| 169 | } |
| 170 | static int dependency_mutant(void) { |
| 171 | static uint8_t base[BW_MAX_IR_INSNS*16],changed[BW_MAX_IR_INSNS*16]; |
| 172 | const uint64_t shape=UINT64_C(1)|(UINT64_C(32)<<32); |
| 173 | size_t base_len=0,changed_len=0,field=0; |
| 174 | if(omega_gpu_om_ir_replay(dependency_mutant_unit,1,7,1,shape,&b,NULL) || |
| 175 | omega_bw_encode_program(&b,base,sizeof base,&base_len) || |
| 176 | omega_qmma_patch_code(&b,base,base_len)<0 || !iteration_field(&b,base_len,&field) || |
| 177 | omega_gpu_om_ir_replay(dependency_mutant_unit,1,2,1,shape,&b,NULL) || |
| 178 | omega_bw_encode_program(&b,changed,sizeof changed,&changed_len) || |
| 179 | omega_qmma_patch_code(&b,changed,changed_len)<0) return 0; |
| 180 | if(iteration_only(changed,changed_len,base,base_len,field,2)) { |
| 181 | fprintf(stderr,"QMMA niter-dependent non-immediate mutant escaped confinement check\n"); |
| 182 | return 0; |
| 183 | } |
| 184 | printf("CODEGEN QMMA NITER DEPENDENCE MUTANT CAUGHT\n"); |
| 185 | return 1; |
| 186 | } |
| 187 | int main(void) { |
| 188 | if(!refusals() || !patch_refusals()){fprintf(stderr,"QMMA refusal differential failed\n");return 1;} |
| 189 | for(int mut=0;mut<2;mut++) for(unsigned wait=0;wait<=16;wait++) { |
| 190 | int ca=oracle_qmma_probe(mut,wait,&a),cb=omega_blackwell_codegen_qmma_probe_prog(mut,wait,&b); |
| 191 | if(!same(ca,cb,"probe"))return 1; |
| 192 | } |
| 193 | if(!peak_structure() || !dependency_mutant()) return 1; |
| 194 | if(!same(oracle_f2e4_probe(&a),omega_blackwell_codegen_f2e4_probe_prog(&b),"f2e4"))return 1; |
| 195 | if(oracle_qmma_probe(0,0,&a) || omega_gpu_om_ir_replay(mutant_unit,0,0,0,0,&b,NULL))return 1; |
| 196 | if(same(0,0,"deliberate mutant")){fprintf(stderr,"QMMA mutant escaped\n");return 1;} |
| 197 | printf("CODEGEN QMMA MUTANT CAUGHT\n"); |
| 198 | printf("CODEGEN QMMA DIFF PASS (%u cases)\n",cases); |
| 199 | return 0; |
| 200 | } |