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 / api_kernel_gen_attention.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* Independent migration oracle: retain the original generator in this TU only. */ |
| 3 | #define CUDAPP_MIGRATION_PENDING_C_API_KGEN 1 |
| 4 | #include "../src/omega_gpu_attention_api.c" |
| 5 | #include "omega_gpu_om_ir.h" |
| 6 | #include "omega_cubin.h" |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | |
| 10 | extern uint32_t omega_attention_ir(uint64_t *,uint64_t,uint64_t,uint64_t,uint64_t,void *,uint64_t,void *,uint64_t); |
| 11 | static unsigned cases, cubins; |
| 12 | static int cubin_same(const KSpec *k, const BlackwellIRProgram *cp, const BlackwellIRProgram *op, |
| 13 | const uint8_t *ct, const uint8_t *ot, size_t nt, int red) { |
| 14 | static const OmegaCubinParam params[] = { |
| 15 | {ARG_Q,8,1},{ARG_POOL,8,1},{ARG_OUT,8,1},{ARG_TAB,8,1}, |
| 16 | {ARG_MAXB,4,0},{ARG_SCALE,4,0},{ARG_BSTRIDE,4,0},{ARG_TSTRIDE,4,0}, |
| 17 | {ARG_KVP,4,0},{ARG_HSTRIDE,4,0},{ARG_NQH,4,0},{ARG_CTXOFF,4,0} |
| 18 | }; |
| 19 | uint8_t *cc=NULL,*oc=NULL;size_t cn=0,on=0;char ce[128]={0},oe[128]={0}; |
| 20 | uint32_t shared=kspec_shared(k); |
| 21 | int cr=omega_cubin_pack("attention_kgen",ct,nt,params,sizeof params/sizeof *params, |
| 22 | OMEGA_CUBIN_CBANK_MAX,omega_bw_gpr_declared(cp->regalloc.peak_gpr_usage),shared,&cc,&cn,ce,sizeof ce); |
| 23 | int orc=omega_cubin_pack("attention_kgen",ot,nt,params,sizeof params/sizeof *params, |
| 24 | OMEGA_CUBIN_CBANK_MAX,omega_bw_gpr_declared(op->regalloc.peak_gpr_usage),shared,&oc,&on,oe,sizeof oe); |
| 25 | if(red && orc==0 && on)oc[on-1]^=1; |
| 26 | int same=cr==0 && orc==0 && cn==on && memcmp(cc,oc,cn)==0; |
| 27 | if(!same)fprintf(stderr,"cubin mismatch status=%d/%d bytes=%zu/%zu error=%s/%s\n",cr,orc,cn,on,ce,oe); |
| 28 | omega_cubin_free(cc);omega_cubin_free(oc);return same; |
| 29 | } |
| 30 | static int compare(const KSpec *k, int red) { |
| 31 | BlackwellIRProgram *cp=calloc(1,sizeof *cp), *op=calloc(1,sizeof *op); |
| 32 | uint64_t words[8192]={0},meta[512]={0},res=99,err=0; |
| 33 | uint64_t kind=(uint64_t)k->f32|((uint64_t)k->general<<1)|((uint64_t)k->divergent<<2)|((uint64_t)k->mutant<<8)|((uint64_t)(k->split+1)<<40); |
| 34 | if(!cp||!op) exit(2); |
| 35 | omega_bw_ir_init(cp); |
| 36 | Em e={0};e.p=cp;e.rg_mutant=k->divergent==2; |
| 37 | if(k->general)gen_attention_general(&e,k);else gen_attention(&e,k); |
| 38 | uint32_t st=omega_attention_ir(&res,kind,k->hd,k->log2bs,k->log2gqa,words,8192,meta,512); |
| 39 | if(st||res||meta[4]||meta[6]||meta[7]||e.err||e.depth||e.rg.depth||meta[0]!=cp->count||meta[1]!=(uint64_t)cp->regalloc.num_vregs||meta[2]!=(uint64_t)cp->regalloc.num_uvregs||meta[5]!=(uint64_t)e.n_loops)goto bad; |
| 40 | if(red==1)words[6]^=1; |
| 41 | for(size_t i=0;i<cp->count;i++) { |
| 42 | BlackwellIRInsn *in=&cp->insns[i];uint64_t *w=words+8*i; |
| 43 | uint64_t flags=in->is_uniform|((uint64_t)in->predicate_p0<<1)|((uint64_t)in->predicate_not<<2)|((uint64_t)in->dst_subreg<<16)|((uint64_t)in->src1_subreg<<24)|((uint64_t)in->src2_subreg<<32)|((uint64_t)in->src3_subreg<<40)|((uint64_t)in->bar_reg<<48); |
| 44 | if(w[0]!=(uint64_t)in->op||w[1]!=(uint64_t)(int64_t)in->dst_vreg||w[2]!=(uint64_t)(int64_t)in->src1_vreg||w[3]!=(uint64_t)(int64_t)in->src2_vreg||w[4]!=(uint64_t)(int64_t)in->src3_vreg||w[5]!=(uint64_t)(int64_t)in->ureg||w[6]!=((uint64_t)in->imm|((uint64_t)in->control<<32))||w[7]!=flags){fprintf(stderr,"record %zu mismatch op=%u/%llu\n",i,in->op,(unsigned long long)w[0]);goto bad;} |
| 45 | } |
| 46 | for(int v=0;v<cp->regalloc.num_vregs;v++)if(meta[64+v]!=cp->regalloc.intervals[v].bundle_size){fprintf(stderr,"width v%d %llu/%u\n",v,(unsigned long long)meta[64+v],cp->regalloc.intervals[v].bundle_size);goto bad;} |
| 47 | for(int v=0;v<cp->regalloc.num_uvregs;v++)if(meta[320+v]!=(cp->regalloc.uintervals[v].is_pair?2u:1u)){fprintf(stderr,"width u%d %llu/%u\n",v,(unsigned long long)meta[320+v],cp->regalloc.uintervals[v].is_pair?2u:1u);goto bad;} |
| 48 | for(int l=0;l<e.n_loops;l++)if(meta[16+2*l]!=(uint64_t)e.loop_start[l]||meta[17+2*l]!=(uint64_t)e.loop_end[l]){fprintf(stderr,"loop %d %llu,%llu/%d,%d\n",l,(unsigned long long)meta[16+2*l],(unsigned long long)meta[17+2*l],e.loop_start[l],e.loop_end[l]);goto bad;} |
| 49 | int cr=e.err?-1:omega_bw_regalloc_solve(cp); |
| 50 | int orc=omega_gpu_om_ir_replay(omega_attention_ir,kind,k->hd,k->log2bs,k->log2gqa,op,&err); |
| 51 | if((cr==0)!=(orc==0)){fprintf(stderr,"allocation result %d/%d replay_error=%llu\n",cr,orc,(unsigned long long)err);goto bad;} |
| 52 | if(cr==0){ |
| 53 | if(check_loop_invariant(&e)!=0){fprintf(stderr,"C oracle loop invariant failed\n");goto bad;} |
| 54 | if(memcmp(&cp->regalloc,&op->regalloc,sizeof cp->regalloc)){fprintf(stderr,"solved register allocation mismatch\n");goto bad;} |
| 55 | uint8_t cb[BW_MAX_IR_INSNS*16],ob[BW_MAX_IR_INSNS*16];size_t cn=0,on=0; |
| 56 | int ce=omega_bw_encode_program(cp,cb,sizeof cb,&cn),oe=omega_bw_encode_program(op,ob,sizeof ob,&on); |
| 57 | if(ce||oe||cn!=on||memcmp(cb,ob,cn)){fprintf(stderr,"encode %d/%d bytes=%zu/%zu gpr=%u/%u\n",ce,oe,cn,on,cp->regalloc.peak_gpr_usage,op->regalloc.peak_gpr_usage);goto bad;} |
| 58 | if(!cubin_same(k,cp,op,cb,ob,cn,red==2))goto bad; |
| 59 | cubins++; |
| 60 | } |
| 61 | cases++;free(cp);free(op);return 0; |
| 62 | bad: |
| 63 | fprintf(stderr,"attention parity failed f32=%d general=%d hd=%u bs=%u gqa=%u split=%d mutant=%d divergent=%d status=%u result=%llu err=%llu n=%llu/%zu\n",k->f32,k->general,k->hd,k->log2bs,k->log2gqa,k->split,k->mutant,k->divergent,st,(unsigned long long)res,(unsigned long long)meta[4],(unsigned long long)meta[0],cp->count); |
| 64 | fprintf(stderr,"vregs=%llu/%d uvregs=%llu/%d loops=%llu/%d metadata=%llu,%llu Cerror=%d depth=%d region=%d\n",(unsigned long long)meta[1],cp->regalloc.num_vregs,(unsigned long long)meta[2],cp->regalloc.num_uvregs,(unsigned long long)meta[5],e.n_loops,(unsigned long long)meta[6],(unsigned long long)meta[7],e.err,e.depth,e.rg.depth); |
| 65 | free(cp);free(op);return 1; |
| 66 | } |
| 67 | static int refused(uint64_t kind,uint64_t hd,uint64_t bs,uint64_t gqa){ |
| 68 | uint64_t r[8192]={0},m[512],out=99;memset(m,0xa5,sizeof m); |
| 69 | uint32_t st=omega_attention_ir(&out,kind,hd,bs,gqa,r,8192,m,512); |
| 70 | if(st||out!=1||m[6]||m[7]||m[0]){fprintf(stderr,"refusal failed kind=%llu hd=%llu bs=%llu gqa=%llu\n",(unsigned long long)kind,(unsigned long long)hd,(unsigned long long)bs,(unsigned long long)gqa);return 1;} |
| 71 | cases++;return 0; |
| 72 | } |
| 73 | int main(int argc,char **argv){ |
| 74 | KSpec k={.hd=64,.split=-1}; |
| 75 | if(argc>1)return compare(&k,strcmp(argv[1],"--cubin-red")==0?2:1); |
| 76 | /* All hd residues, warp boundaries, D4/DT branches. */ |
| 77 | for(unsigned f=0;f<2;f++)for(unsigned d=1;d<=256;d++){ |
| 78 | k=(KSpec){.f32=f,.hd=d,.general=true,.split=-1,.log2bs=31};if(compare(&k,0))return 1; |
| 79 | } |
| 80 | /* Full source-derived shift/GQA/split partitions, not a sampled Cartesian product. */ |
| 81 | for(unsigned f=0;f<2;f++)for(unsigned h=64;h<=128;h+=64)for(unsigned b=0;b<=31;b++)for(unsigned g=0;g<=31;g++)for(int s=-1;s<=4;s++){ |
| 82 | k=(KSpec){.f32=f,.hd=h,.log2bs=b,.log2gqa=g,.split=s};if(compare(&k,0))return 1; |
| 83 | } |
| 84 | for(unsigned f=0;f<2;f++)for(unsigned h=64;h<=128;h+=64)for(unsigned g=0;g<=3;g++)for(int s=-1;s<=4;s++)for(int m=0;m<=6;m++)for(int dv=0;dv<=2;dv++){ |
| 85 | k=(KSpec){.f32=f,.hd=h,.log2bs=4,.log2gqa=g,.split=s,.mutant=m,.divergent=dv};if(compare(&k,0))return 1; |
| 86 | } |
| 87 | const unsigned general_bs[]={0,1,30,31}; |
| 88 | for(unsigned f=0;f<2;f++)for(unsigned d=1;d<=256;d++)for(int m=0;m<=5;m++)for(size_t b=0;b<sizeof general_bs/sizeof *general_bs;b++){ |
| 89 | k=(KSpec){.f32=f,.hd=d,.general=true,.split=-1,.log2bs=general_bs[b],.mutant=m};if(compare(&k,0))return 1; |
| 90 | } |
| 91 | const uint32_t hd_bad[]={0,1,2,3,4,5,31,32,33,63,65,127,129,255,256,257,UINT32_MAX}; |
| 92 | for(size_t i=0;i<sizeof hd_bad/sizeof *hd_bad;i++)if(refused(0,hd_bad[i],0,0))return 1; |
| 93 | if(refused(2,0,0,0)||refused(2,257,0,0)||refused(2,UINT32_MAX,0,0))return 1; |
| 94 | const uint32_t sh_bad[]={32,33,UINT32_MAX}; |
| 95 | for(size_t i=0;i<sizeof sh_bad/sizeof *sh_bad;i++)if(refused(0,64,sh_bad[i],0)||refused(0,64,0,sh_bad[i]))return 1; |
| 96 | if(refused(UINT32_MAX,64,0,0)||refused(1ull<<44,64,0,0)||refused(6ull<<40,64,0,0)||refused(7ull<<8,64,0,0)||refused(3ull<<2,64,0,0))return 1; |
| 97 | printf("ATTENTION-IR DIFFERENTIAL PASS cases=%u cubins=%u records+allocation+encoded-bytes+cubins identical\n",cases,cubins);return 0; |
| 98 | } |