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 / gpu_attn_any_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #include "omega_gpu_attn_any_api.h" |
| 3 | #include "omega_gpu_session.h" |
| 4 | #include "cudapp_native.h" |
| 5 | #include "bw_warp_sim_k.h" |
| 6 | #include "gpu_attn_any_ref.h" |
| 7 | #include <stdio.h> |
| 8 | static int failures; |
| 9 | #define CHECK(x) do { if (!(x)) { fprintf(stderr,"attn-any failure line %d: %s\n",__LINE__,#x); ++failures; } } while (0) |
| 10 | static int chip_mode, shell_mode, open_success; |
| 11 | static unsigned open_calls, alloc_calls, lock_calls, unlock_calls, free_calls; |
| 12 | static max_align_t fake_session; |
| 13 | OmegaGpuSession *__real_omega_gpu_session_open(void); |
| 14 | void __real_omega_gpu_session_lock(void); |
| 15 | void __real_omega_gpu_session_unlock(void); |
| 16 | void __real_omega_gpu_session_free(OmegaGpuMem *); |
| 17 | int __real_omega_gpu_session_alloc(size_t, OmegaGpuMem *); |
| 18 | static _Noreturn void forbidden(const char *name) { |
| 19 | fprintf(stderr,"attn-any host guard: forbidden %s\n",name); |
| 20 | abort(); |
| 21 | } |
| 22 | OmegaGpuSession *__wrap_omega_gpu_session_open(void) { |
| 23 | if (chip_mode) return __real_omega_gpu_session_open(); |
| 24 | if (!shell_mode) forbidden("session_open"); |
| 25 | ++open_calls; |
| 26 | return open_success ? (OmegaGpuSession *)&fake_session : NULL; |
| 27 | } |
| 28 | void __wrap_omega_gpu_session_lock(void) { |
| 29 | if (chip_mode) { __real_omega_gpu_session_lock(); return; } |
| 30 | if (!shell_mode) forbidden("session_lock"); |
| 31 | ++lock_calls; |
| 32 | } |
| 33 | void __wrap_omega_gpu_session_unlock(void) { |
| 34 | if (chip_mode) { __real_omega_gpu_session_unlock(); return; } |
| 35 | if (!shell_mode) forbidden("session_unlock"); |
| 36 | ++unlock_calls; |
| 37 | } |
| 38 | void __wrap_omega_gpu_session_free(OmegaGpuMem *mem) { |
| 39 | if (chip_mode) { __real_omega_gpu_session_free(mem); return; } |
| 40 | if (!shell_mode) forbidden("session_free"); |
| 41 | ++free_calls; |
| 42 | } |
| 43 | int __wrap_omega_gpu_session_alloc(size_t bytes, OmegaGpuMem *mem) { |
| 44 | if (chip_mode) return __real_omega_gpu_session_alloc(bytes,mem); |
| 45 | if (!shell_mode) forbidden("session_alloc"); |
| 46 | (void)bytes; (void)mem; ++alloc_calls; |
| 47 | return -1; |
| 48 | } |
| 49 | static void put64(uint32_t *p, uintptr_t x) { p[0]=(uint32_t)x; p[1]=(uint32_t)((uint64_t)x>>32); } |
| 50 | static void *input(size_t n, unsigned dtype, unsigned salt) { |
| 51 | void *p=malloc(n*(dtype ? 2u:4u)); if (!p) abort(); |
| 52 | for (size_t i=0;i<n;++i) { |
| 53 | /* Exact dyadic inputs, differing heads and rows, bounded by 1/8. */ |
| 54 | float f=(float)((int)((i*17+salt*13)%9)-4)*0.03125f; |
| 55 | if (dtype==2) { _Float16 h=(_Float16)f; memcpy(&((uint16_t*)p)[i],&h,sizeof h); } |
| 56 | else if (dtype==1) { uint32_t u; memcpy(&u,&f,4); ((uint16_t*)p)[i]=(uint16_t)(u>>16); } |
| 57 | else ((float*)p)[i]=f; |
| 58 | } |
| 59 | return p; |
| 60 | } |
| 61 | static int run_shape(OmegaGpuAttnAnyShape s, int chip, int wrong_mask) { |
| 62 | size_t nq=(size_t)s.seq_q*s.q_heads*s.head_dim, nk=(size_t)s.seq_kv*s.kv_heads*s.head_dim; |
| 63 | void *q=input(nq,s.dtype,1), *k=input(nk,s.dtype,3), *v=input(nk,s.dtype,7); |
| 64 | float *o=malloc(nq*sizeof *o); double *ref=malloc(nq*sizeof *ref); |
| 65 | BlackwellIRProgram *p=malloc(sizeof *p); uint8_t *code=malloc(OMEGA_BW_CODE_CAP_MAX); |
| 66 | if (!o || !ref || !p || !code) abort(); |
| 67 | memset(o,0xa5,nq*sizeof *o); |
| 68 | OmegaBlackwellKernel kernel; |
| 69 | int rc=omega_gpu_attn_any_codegen(&s,p,code,OMEGA_BW_CODE_CAP_MAX,&kernel); |
| 70 | if (rc) fprintf(stderr,"attn-any codegen rc=%d dim=%u type=%u\n",rc,s.head_dim,s.dtype); |
| 71 | CHECK(rc==OMEGA_GPU_ATTN_ANY_OK); |
| 72 | if (!rc && chip) rc=omega_gpu_attn_any(&s,q,k,v,o); |
| 73 | if (!rc && !chip) { |
| 74 | for (uint32_t h=0;h<s.q_heads;++h) { |
| 75 | uint64_t shape[8]={s.seq_q,s.seq_kv,s.head_dim,s.q_heads,s.kv_heads,s.causal,s.dtype,0}, plan[16]={0}, result=0; |
| 76 | uint32_t bits; memcpy(&bits,&s.scale,4); shape[7]=bits; |
| 77 | CHECK(omega_aa_plan(&result,shape,8,h,plan,16)==0 && result==0); |
| 78 | CHECK(plan[4]==(uint64_t)h*s.seq_q*s.head_dim*(s.dtype?2u:4u)); |
| 79 | CHECK(plan[5]==(uint64_t)(h/(s.q_heads/s.kv_heads))*s.seq_kv*s.head_dim*(s.dtype?2u:4u)); |
| 80 | WsSim sim={0}; sim.p=p; sim.threads=32; sim.order=h&1; sim.warp_sched=h&1; |
| 81 | uint32_t *cb=&sim.cbank[0x380/4]; |
| 82 | put64(cb,(uintptr_t)q+plan[4]); put64(cb+2,(uintptr_t)k+plan[5]); |
| 83 | put64(cb+4,(uintptr_t)v+plan[5]); put64(cb+6,(uintptr_t)o+plan[6]); |
| 84 | cb[8]=s.seq_kv; cb[9]=bits; |
| 85 | sim.ranges[0]=(WsRange){(uintptr_t)q,(uintptr_t)q+nq*(s.dtype?2u:4u)}; |
| 86 | sim.ranges[1]=(WsRange){(uintptr_t)k,(uintptr_t)k+nk*(s.dtype?2u:4u)}; |
| 87 | sim.ranges[2]=(WsRange){(uintptr_t)v,(uintptr_t)v+nk*(s.dtype?2u:4u)}; |
| 88 | sim.ranges[3]=(WsRange){(uintptr_t)o,(uintptr_t)o+nq*4}; sim.nranges=4; |
| 89 | rc=ws_run(&sim,s.seq_q,1); |
| 90 | if (rc) fprintf(stderr,"attn-any simulator: %s\n",sim.msg); |
| 91 | CHECK(rc==0); if (rc) break; |
| 92 | } |
| 93 | } |
| 94 | aa_reference(&s,q,k,v,ref,wrong_mask); |
| 95 | int equal=!rc && aa_equal(o,ref,nq); |
| 96 | if (!wrong_mask) CHECK(equal); |
| 97 | free(q);free(k);free(v);free(o);free(ref);free(p);free(code); |
| 98 | return equal; |
| 99 | } |
| 100 | static void refusals(void) { |
| 101 | OmegaGpuAttnAnyShape s={1,1,257,4,1,0,0,0.125f}; |
| 102 | BlackwellIRProgram *p=malloc(sizeof *p); uint8_t *code=malloc(OMEGA_BW_CODE_CAP_MAX); OmegaBlackwellKernel k; |
| 103 | CHECK(omega_gpu_attn_any_codegen(&s,p,code,OMEGA_BW_CODE_CAP_MAX,&k)==OMEGA_GPU_ATTN_ANY_REFUSED); |
| 104 | float dummy=0; /* Admission must refuse before any session/device access. */ |
| 105 | CHECK(omega_gpu_attn_any(&s,&dummy,&dummy,&dummy,&dummy)==OMEGA_GPU_ATTN_ANY_REFUSED); |
| 106 | s.head_dim=80;s.q_heads=3;s.kv_heads=2; |
| 107 | CHECK(omega_gpu_attn_any_codegen(&s,p,code,OMEGA_BW_CODE_CAP_MAX,&k)==OMEGA_GPU_ATTN_ANY_REFUSED); |
| 108 | s.q_heads=4;s.kv_heads=1;s.seq_kv=0; |
| 109 | CHECK(omega_gpu_attn_any_codegen(&s,p,code,OMEGA_BW_CODE_CAP_MAX,&k)==OMEGA_GPU_ATTN_ANY_REFUSED); |
| 110 | s.seq_kv=1;s.dtype=3; |
| 111 | CHECK(omega_gpu_attn_any_codegen(&s,p,code,OMEGA_BW_CODE_CAP_MAX,&k)==OMEGA_GPU_ATTN_ANY_REFUSED); |
| 112 | s.dtype=0;s.causal=2; |
| 113 | CHECK(omega_gpu_attn_any_codegen(&s,p,code,OMEGA_BW_CODE_CAP_MAX,&k)==OMEGA_GPU_ATTN_ANY_REFUSED); |
| 114 | s.causal=0;s.scale=INFINITY; |
| 115 | CHECK(omega_gpu_attn_any_codegen(&s,p,code,OMEGA_BW_CODE_CAP_MAX,&k)==OMEGA_GPU_ATTN_ANY_REFUSED); |
| 116 | s.scale=NAN; |
| 117 | CHECK(omega_gpu_attn_any_codegen(&s,p,code,OMEGA_BW_CODE_CAP_MAX,&k)==OMEGA_GPU_ATTN_ANY_REFUSED); |
| 118 | s.scale=0.125f;s.seq_q=65536; |
| 119 | CHECK(omega_gpu_attn_any_codegen(&s,p,code,OMEGA_BW_CODE_CAP_MAX,&k)==OMEGA_GPU_ATTN_ANY_REFUSED); |
| 120 | s.seq_q=65535;s.head_dim=256;s.q_heads=65535; |
| 121 | CHECK(omega_gpu_attn_any_codegen(&s,p,code,OMEGA_BW_CODE_CAP_MAX,&k)==OMEGA_GPU_ATTN_ANY_REFUSED); |
| 122 | s.seq_q=1;s.q_heads=0; |
| 123 | CHECK(omega_gpu_attn_any_codegen(&s,p,code,OMEGA_BW_CODE_CAP_MAX,&k)==OMEGA_GPU_ATTN_ANY_REFUSED); |
| 124 | CHECK(omega_gpu_attn_any(NULL,NULL,NULL,NULL,NULL)==OMEGA_GPU_ATTN_ANY_BAD_ARGS); |
| 125 | free(p);free(code); |
| 126 | } |
| 127 | static int session_contract(void) { |
| 128 | OmegaGpuAttnAnyShape shape={1,1,1,1,1,0,0,0.125f}; |
| 129 | float value=0; |
| 130 | shell_mode=1; |
| 131 | for (open_success=0;open_success<2;++open_success) { |
| 132 | open_calls=alloc_calls=lock_calls=unlock_calls=free_calls=0; |
| 133 | CHECK(omega_gpu_attn_any(&shape,&value,&value,&value,&value)==OMEGA_GPU_ATTN_ANY_CHIP); |
| 134 | CHECK(open_calls==1 && alloc_calls==(unsigned)open_success); |
| 135 | CHECK(lock_calls==1 && unlock_calls==1 && free_calls==5); |
| 136 | } |
| 137 | puts(failures?"attn-any shell contract: FAIL":"attn-any shell contract: PASS (NULL refusal; non-NULL reaches allocation)"); |
| 138 | return failures?1:0; |
| 139 | } |
| 140 | int main(int argc,char **argv) { |
| 141 | int chip=argc==2 && !strcmp(argv[1],"--chip"); |
| 142 | int quick=(argc==2 && !strcmp(argv[1],"--quick")) || |
| 143 | (argc==3 && !strcmp(argv[1],"--sim") && !strcmp(argv[2],"--quick")); |
| 144 | if (argc==2 && !strcmp(argv[1],"--shell-contract")) return session_contract(); |
| 145 | if (!chip && !quick && !(argc==2 && !strcmp(argv[1],"--sim"))) return 2; |
| 146 | chip_mode=chip; |
| 147 | refusals(); |
| 148 | OmegaGpuAttnAnyShape control={7,65,80,4,1,1,0,0.125f}; |
| 149 | CHECK(run_shape(control,chip,0)); |
| 150 | CHECK(!run_shape(control,chip,1)); /* The independent wrong-mask reference must disagree. */ |
| 151 | OmegaGpuAttnAnyShape grouped={2,7,96,8,2,0,1,0.125f}; |
| 152 | CHECK(run_shape(grouped,chip,0)); |
| 153 | grouped.scale=0; CHECK(run_shape(grouped,chip,0)); /* Uniform weights, including bf16 input. */ |
| 154 | static const uint32_t half_dims[]={1,33,64,80,96,128,255,256}; |
| 155 | for (unsigned i=0;i<8;++i) { |
| 156 | OmegaGpuAttnAnyShape half={2,7,half_dims[i],4,1,i%2,2,0.125f}; |
| 157 | CHECK(run_shape(half,chip,0)); |
| 158 | } |
| 159 | if (!quick) { |
| 160 | static const uint32_t seq[]={1,2,7,64,65,513}, dim[]={64,80,96,128,256}, groups[]={1,4,8}; |
| 161 | for (unsigned i=0;i<30;++i) { |
| 162 | OmegaGpuAttnAnyShape s={seq[i%6],seq[(i/6+i+1)%6],dim[i%5],2*groups[i%3],2,(i/3)%2,(i/3)%3,0.125f}; |
| 163 | CHECK(run_shape(s,chip,0)); |
| 164 | } |
| 165 | static const uint32_t tails[]={1,3,31,33,255}; |
| 166 | for (unsigned i=0;i<5;++i) { |
| 167 | OmegaGpuAttnAnyShape s={2,7,tails[i],1,1,i%2,i%3,-0.25f}; |
| 168 | CHECK(run_shape(s,chip,0)); |
| 169 | } |
| 170 | OmegaGpuAttnAnyShape square={513,513,64,1,1,1,0,0.125f}; CHECK(run_shape(square,chip,0)); |
| 171 | } |
| 172 | if (failures) { printf("attn-any %s: FAIL (%d checks)\n",chip?"chip":"host+warp-sim",failures);return 1; } |
| 173 | printf("attn-any %s: PASS (reference, shape sweep, refusals, wrong-mask control)\n",chip?"chip":"host+warp-sim");return 0; |
| 174 | } |