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 / om_bw_diff_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #include "omega_blackwell_matmul.h" |
| 3 | #include "omega_blackwell_realize.h" |
| 4 | #include "omega_gpu_code_alloc.h" |
| 5 | #include "sha256.h" |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | #include <limits.h> |
| 10 | #ifndef CUDAPP_MIGRATION_PENDING_BW_MATMUL |
| 11 | #error This differential requires the default-OFF independent C oracle |
| 12 | #endif |
| 13 | int oracle_omega_matmul_spec_init(OmegaMatMulSpec *, uint32_t, uint32_t, uint32_t, OmegaMatMulPrecision); |
| 14 | float oracle_omega_fp16_to_fp32(uint16_t); |
| 15 | float oracle_omega_bf16_to_fp32(uint16_t); |
| 16 | uint16_t oracle_omega_fp32_to_fp16(float); |
| 17 | uint16_t oracle_omega_fp32_to_bf16(float); |
| 18 | int oracle_omega_blackwell_bind_matmul_realization(const OmegaMatMulSpec *, const OmegaBlackwellKernel *, OmegaBlackwellRealizationIdentity *); |
| 19 | int oracle_omega_blackwell_get_machine_id(SemanticId *); |
| 20 | int oracle_omega_blackwell_realization_compute_id(OmegaBlackwellRealization *); |
| 21 | int oracle_omega_blackwell_realize_vector(const OmegaVectorSpec *, OmegaBlackwellRealization *); |
| 22 | int oracle_omega_blackwell_codegen_matmul(const OmegaMatMulSpec *, OmegaBlackwellKernel *); |
| 23 | void oracle_omega_blackwell_kernel_free(OmegaBlackwellKernel *); |
| 24 | int oracle_omega_matmul_cpu_oracle_i32(const uint32_t *, const uint32_t *, uint32_t *, uint32_t, uint32_t, uint32_t); |
| 25 | int oracle_omega_matmul_cpu_oracle_f16(const uint16_t *, const uint16_t *, float *, uint32_t, uint32_t, uint32_t); |
| 26 | int oracle_omega_matmul_cpu_oracle_bf16(const uint16_t *, const uint16_t *, float *, uint32_t, uint32_t, uint32_t); |
| 27 | static unsigned checks; |
| 28 | #define CHECK(c) do { ++checks; if (!(c)) { fprintf(stderr,"FAIL line %d: %s\n",__LINE__,#c); return 1; } } while(0) |
| 29 | static uint32_t bits(float f) { uint32_t b; memcpy(&b,&f,4); return b; } |
| 30 | static float value(uint32_t b) { float f; memcpy(&f,&b,4); return f; } |
| 31 | static int nan16(uint16_t h) { return (h&0x7c00)==0x7c00 && (h&0x3ff); } |
| 32 | static int nanbf(uint16_t h) { return (h&0x7f80)==0x7f80 && (h&0x7f); } |
| 33 | static uint32_t rng = 0x6123abcd; |
| 34 | static uint32_t next(void) { rng ^= rng<<13; rng ^= rng>>17; rng ^= rng<<5; return rng; } |
| 35 | static int same_float(float a, float b) { |
| 36 | uint32_t x=bits(a), y=bits(b); |
| 37 | return x==y || ((x&0x7fffffff)>0x7f800000 && (y&0x7fffffff)>0x7f800000); |
| 38 | } |
| 39 | static int cpu_views(void) { |
| 40 | uint32_t a[81], b[81], c[81], expected[81]; |
| 41 | uint16_t h[81], q[81]; |
| 42 | float out[81], want[81]; |
| 43 | uint64_t result=0; |
| 44 | uint8_t name[64]; |
| 45 | uint64_t length=0; |
| 46 | for(unsigned i=0;i<64;i++) { |
| 47 | memset(name,0x61,sizeof name); name[i]=0; |
| 48 | CHECK(omega_bw_name(&result,name,sizeof name,&length,1)==0 && result==0); |
| 49 | CHECK(length==strnlen((const char *)name,sizeof name)); |
| 50 | } |
| 51 | memset(name,0x61,sizeof name); length=123; |
| 52 | CHECK(omega_bw_name(&result,name,sizeof name,&length,1)==0 && result==UINT64_MAX && length==123); |
| 53 | CHECK(omega_bw_name(&result,name,63,&length,1)!=0); |
| 54 | for(unsigned trial=0;trial<100;trial++) { |
| 55 | uint32_t m=1+next()%9,k=1+next()%9,n=1+next()%9; |
| 56 | for(unsigned i=0;i<81;i++) { a[i]=next(); b[i]=next(); h[i]=(uint16_t)next(); q[i]=(uint16_t)next(); } |
| 57 | CHECK(omega_matmul_cpu_oracle_i32(a,b,c,m,k,n)==0); |
| 58 | CHECK(oracle_omega_matmul_cpu_oracle_i32(a,b,expected,m,k,n)==0); |
| 59 | CHECK(memcmp(c,expected,(size_t)m*n*sizeof(*c))==0); |
| 60 | CHECK(omega_matmul_cpu_oracle_f16(h,q,out,m,k,n)==0); |
| 61 | CHECK(oracle_omega_matmul_cpu_oracle_f16(h,q,want,m,k,n)==0); |
| 62 | for(unsigned i=0;i<m*n;i++) CHECK(same_float(out[i],want[i])); |
| 63 | CHECK(omega_matmul_cpu_oracle_bf16(h,q,out,m,k,n)==0); |
| 64 | CHECK(oracle_omega_matmul_cpu_oracle_bf16(h,q,want,m,k,n)==0); |
| 65 | for(unsigned i=0;i<m*n;i++) CHECK(same_float(out[i],want[i])); |
| 66 | m=next()|1; k=next()|1; n=next()|1; |
| 67 | CHECK(omega_bw_shape(&result,(uint64_t)m*k,(uint64_t)k*n,(uint64_t)m*n)==0); |
| 68 | CHECK(result==n); |
| 69 | } |
| 70 | CHECK(omega_bw_shape(&result,(uint64_t)UINT32_MAX*UINT32_MAX,(uint64_t)UINT32_MAX*UINT32_MAX,(uint64_t)UINT32_MAX*UINT32_MAX)==0 && result==UINT32_MAX); |
| 71 | const uint64_t invalid[][3]={{1,2,3},{2,1,8},{3,2,2},{1,UINT64_MAX,2},{UINT64_MAX,UINT64_MAX,1},{UINT64_MAX,1,UINT64_MAX},{0,1,1},{1,0,1},{1,1,0}}; |
| 72 | for(unsigned i=0;i<sizeof invalid/sizeof invalid[0];i++) |
| 73 | CHECK(omega_bw_shape(&result,invalid[i][0],invalid[i][1],invalid[i][2])==0 && result==0); |
| 74 | /* Red controls: a non-square quotient and k not divisible by recovered n. */ |
| 75 | for(unsigned i=0;i<2;i++) { |
| 76 | memset(c,0x5a,sizeof c); memcpy(expected,c,sizeof c); |
| 77 | memset(out,0x5a,sizeof out); memcpy(want,out,sizeof out); |
| 78 | CHECK(omega_bw_cpui(&result,a,invalid[i][0],b,invalid[i][1],c,invalid[i][2])==0 && result==UINT64_MAX); |
| 79 | CHECK(memcmp(c,expected,sizeof c)==0); |
| 80 | CHECK(omega_bw_cpuf16(&result,h,invalid[i][0],q,invalid[i][1],out,invalid[i][2])==0 && result==UINT64_MAX); |
| 81 | CHECK(memcmp(out,want,sizeof out)==0); |
| 82 | CHECK(omega_bw_cpubf16(&result,h,invalid[i][0],q,invalid[i][1],out,invalid[i][2])==0 && result==UINT64_MAX); |
| 83 | CHECK(memcmp(out,want,sizeof out)==0); |
| 84 | } |
| 85 | CHECK(omega_bw_cpui(&result,a,1,b,1,a,1)!=0); |
| 86 | CHECK(omega_matmul_cpu_oracle_i32(a,b,a,1,1,1)!=0); |
| 87 | CHECK(omega_matmul_cpu_oracle_f16((const uint16_t *)a,q,(float *)a,1,1,1)!=0); |
| 88 | CHECK(omega_matmul_cpu_oracle_bf16((const uint16_t *)a,q,(float *)a,1,1,1)!=0); |
| 89 | CHECK(omega_matmul_cpu_oracle_i32(a,b,a+1,1,2,2)!=0); |
| 90 | /* Read-only inputs may alias; only writable output must be disjoint. */ |
| 91 | CHECK(omega_matmul_cpu_oracle_i32(a,a,c,1,2,1)==0); |
| 92 | CHECK(oracle_omega_matmul_cpu_oracle_i32(a,a,expected,1,2,1)==0 && c[0]==expected[0]); |
| 93 | CHECK(omega_matmul_cpu_oracle_f16(h,h,out,1,2,1)==0); |
| 94 | CHECK(oracle_omega_matmul_cpu_oracle_f16(h,h,want,1,2,1)==0 && same_float(out[0],want[0])); |
| 95 | CHECK(omega_matmul_cpu_oracle_bf16(h,h,out,1,2,1)==0); |
| 96 | CHECK(oracle_omega_matmul_cpu_oracle_bf16(h,h,want,1,2,1)==0 && same_float(out[0],want[0])); |
| 97 | /* Exercise a dot beyond the implicit one-million runtime-loop guard. */ |
| 98 | size_t count=1000001; |
| 99 | uint32_t *long_a=calloc(count,sizeof(*long_a)),*long_b=calloc(count,sizeof(*long_b)); |
| 100 | CHECK(long_a && long_b); |
| 101 | long_a[count-1]=7; long_b[count-1]=9; |
| 102 | CHECK(omega_matmul_cpu_oracle_i32(long_a,long_b,c,1,(uint32_t)count,1)==0 && c[0]==63); |
| 103 | free(long_a); free(long_b); |
| 104 | return 0; |
| 105 | } |
| 106 | static uint64_t policy(unsigned op,uint64_t x,uint64_t y,uint64_t z,uint64_t w,uint64_t v) { |
| 107 | switch(op) { |
| 108 | case 4: { |
| 109 | if (x>UINT64_MAX-y-4095) return 0; |
| 110 | uint64_t b=(x+y+4095)&~UINT64_C(4095); |
| 111 | return b<4096?4096:b; |
| 112 | } |
| 113 | case 7: return y==0?x:UINT64_MAX; |
| 114 | case 8: return x==0 && y==0?0:UINT64_MAX; |
| 115 | case 9: { int64_t s; memcpy(&s,&x,8); return s>0?(uint64_t)(s-1):UINT64_MAX; } |
| 116 | case 10: return x?2:3; |
| 117 | case 11: return x?14:13; |
| 118 | case 12: return y==0?x:0; |
| 119 | case 13: return x?1:0; |
| 120 | case 14: return x<=32768 && y<=16384 && z==384 && w==384 && v<=128; |
| 121 | case 15: return x<=4096; |
| 122 | default: return UINT64_MAX; |
| 123 | } |
| 124 | } |
| 125 | int main(void) { |
| 126 | for (uint32_t h=0;h<65536;h++) { |
| 127 | CHECK(bits(omega_fp16_to_fp32(h))==bits(oracle_omega_fp16_to_fp32(h))); |
| 128 | CHECK(bits(omega_bf16_to_fp32(h))==bits(oracle_omega_bf16_to_fp32(h))); |
| 129 | float f=oracle_omega_fp16_to_fp32(h); |
| 130 | uint16_t a=omega_fp32_to_fp16(f),b=oracle_omega_fp32_to_fp16(f); |
| 131 | CHECK(a==b || (nan16(a)&&nan16(b))); |
| 132 | } |
| 133 | const uint32_t edges[]={0,0x80000000,1,0x7fffff,0x800000,0x33000000,0x33800000,0x387fc000,0x38800000,0x477fe000,0x47800000,0x7f800000,0xff800000,0x7f800001,0xffc12345,0x3f808000,0x3f818000}; |
| 134 | for(unsigned i=0;i<100000;i++) { |
| 135 | uint32_t u=i<sizeof edges/sizeof *edges?edges[i]:next(); |
| 136 | float f=value(u); |
| 137 | uint16_t a=omega_fp32_to_fp16(f),b=oracle_omega_fp32_to_fp16(f); |
| 138 | CHECK(a==b || (nan16(a)&&nan16(b))); |
| 139 | a=omega_fp32_to_bf16(f); b=oracle_omega_fp32_to_bf16(f); |
| 140 | CHECK(a==b || (nanbf(a)&&nanbf(b))); |
| 141 | CHECK(a==omega_fp32_to_bf16_rne(f)); |
| 142 | } |
| 143 | const uint64_t limits[]={0,1,127,128,129,383,384,385,2047,2048,2049,4095,4096,4097,16384,16385,32768,32769,UINT64_MAX-6143,UINT64_MAX-6142,INT64_MAX,UINT64_C(1)<<63,UINT64_MAX}; |
| 144 | const unsigned ops[]={4,7,8,9,10,11,12,13,14,15,99}; |
| 145 | for(unsigned o=0;o<sizeof ops/sizeof *ops;o++) for(unsigned i=0;i<sizeof limits/sizeof *limits;i++) { |
| 146 | uint64_t x=limits[i]; |
| 147 | for(unsigned j=0;j<sizeof limits/sizeof *limits;j++) { |
| 148 | uint64_t y=ops[o]==4?(j&1?2048:0):limits[j]; |
| 149 | CHECK(omega_bw_value(ops[o],x,y,384,384,128)==policy(ops[o],x,y,384,384,128)); |
| 150 | } |
| 151 | } |
| 152 | for(unsigned i=0;i<10000;i++) { |
| 153 | uint64_t x=next(),y=next(),z=next(),w=next(),v=next(); |
| 154 | unsigned op=ops[i%(sizeof ops/sizeof *ops)]; |
| 155 | CHECK(omega_bw_value(op,x,y,z,w,v)==policy(op,x,y,z,w,v)); |
| 156 | } |
| 157 | CHECK(omega_bw_value(14,32768,16384,384,384,128)==1); |
| 158 | CHECK(omega_bw_value(14,32768,16384,383,384,128)==0); |
| 159 | CHECK(omega_bw_value(14,32768,16384,384,383,128)==0); |
| 160 | CHECK(omega_bw_value(14,32768,16384,384,384,129)==0); |
| 161 | uint8_t data[256],hash[32],want[32]; |
| 162 | for(unsigned i=0;i<sizeof data;i++) data[i]=(uint8_t)next(); |
| 163 | for(unsigned n=0;n<=256;n++) { |
| 164 | CHECK(omega_bw_id(data,n,hash)==0); |
| 165 | sha256_hash(data,n,want); |
| 166 | CHECK(memcmp(hash,want,32)==0); |
| 167 | } |
| 168 | memset(hash,0xa5,sizeof hash); uint8_t saved[32]; memcpy(saved,hash,32); |
| 169 | uint64_t result=123; |
| 170 | CHECK(omega_bw_identity(&result,data,255,16,hash,32)==32); |
| 171 | CHECK(result==123 && memcmp(saved,hash,32)==0); |
| 172 | CHECK(omega_bw_id(data,257,hash)==-1); |
| 173 | CHECK(omega_bw_identity(&result,data,256,16,data,32)==32); |
| 174 | const uint32_t dims[]={0,1,2,8192,8193,16384,16385,262144,262145,UINT32_MAX}; |
| 175 | for(unsigned i=0;i<sizeof dims/sizeof *dims;i++) for(unsigned j=0;j<sizeof dims/sizeof *dims;j++) for(unsigned k=0;k<sizeof dims/sizeof *dims;k++) { |
| 176 | OmegaMatMulSpec a={0},b={0}; |
| 177 | int x=omega_matmul_spec_init(&a,dims[i],dims[j],dims[k],(OmegaMatMulPrecision)next()); |
| 178 | int y=oracle_omega_matmul_spec_init(&b,dims[i],dims[j],dims[k],a.precision); |
| 179 | /* Refused calls need not initialize the precision field. */ |
| 180 | CHECK(x==y); |
| 181 | if(x==0) CHECK(memcmp(&a,&b,sizeof a)==0); |
| 182 | } |
| 183 | CHECK(omega_matmul_spec_init(NULL,1,1,1,0)==-1); |
| 184 | CHECK(oracle_omega_matmul_spec_init(NULL,1,1,1,0)==-1); |
| 185 | for(unsigned i=0;i<100;i++) { |
| 186 | OmegaBlackwellRealization a={0},b={0}; |
| 187 | uint8_t *p=(uint8_t *)&a; |
| 188 | for(unsigned j=0;j<sizeof a;j++) p[j]=(uint8_t)next(); |
| 189 | b=a; |
| 190 | CHECK(omega_blackwell_realization_compute_id(&a)==oracle_omega_blackwell_realization_compute_id(&b)); |
| 191 | CHECK(memcmp(&a,&b,sizeof a)==0); |
| 192 | OmegaMatMulSpec s; OmegaBlackwellKernel kern={0}; OmegaBlackwellRealizationIdentity ia,ib; |
| 193 | CHECK(omega_matmul_spec_init(&s,1,1,1,0)==0); |
| 194 | for(unsigned j=0;j<32;j++) kern.code_digest[j]=(uint8_t)next(); |
| 195 | CHECK(omega_blackwell_bind_matmul_realization(&s,&kern,&ia)==0); |
| 196 | CHECK(oracle_omega_blackwell_bind_matmul_realization(&s,&kern,&ib)==0); |
| 197 | CHECK(memcmp(&ia,&ib,sizeof ia)==0); |
| 198 | } |
| 199 | SemanticId a,b; |
| 200 | CHECK(omega_blackwell_get_machine_id(&a)==0 && oracle_omega_blackwell_get_machine_id(&b)==0); |
| 201 | CHECK(memcmp(&a,&b,sizeof a)==0); |
| 202 | CHECK(omega_blackwell_get_machine_id(NULL)==-1 && oracle_omega_blackwell_get_machine_id(NULL)==-1); |
| 203 | CHECK(omega_blackwell_realization_compute_id(NULL)==-1 && oracle_omega_blackwell_realization_compute_id(NULL)==-1); |
| 204 | OmegaVectorSpec vs={0}; OmegaBlackwellRealization ra,rb; |
| 205 | const uint32_t counts[]={0,1,63,64,65,UINT32_MAX-63,UINT32_MAX}; |
| 206 | for(unsigned i=0;i<sizeof counts/sizeof *counts;i++) { |
| 207 | vs.element_count=counts[i]; |
| 208 | CHECK(omega_blackwell_realize_vector(&vs,&ra)==oracle_omega_blackwell_realize_vector(&vs,&rb)); |
| 209 | CHECK(memcmp(&ra,&rb,sizeof ra)==0); |
| 210 | } |
| 211 | CHECK(omega_blackwell_realize_vector(NULL,&ra)==-1 && oracle_omega_blackwell_realize_vector(NULL,&rb)==-1); |
| 212 | CHECK(omega_blackwell_realize_vector(&vs,NULL)==-1 && oracle_omega_blackwell_realize_vector(&vs,NULL)==-1); |
| 213 | OmegaMatMulSpec spec; OmegaBlackwellKernel ka={0},kb={0}; |
| 214 | CHECK(omega_matmul_spec_init(&spec,1,1,1,0)==0); |
| 215 | CHECK(omega_blackwell_bind_matmul_realization(NULL,&ka,(OmegaBlackwellRealizationIdentity *)&ra)==-1); |
| 216 | CHECK(omega_blackwell_bind_matmul_realization(&spec,NULL,(OmegaBlackwellRealizationIdentity *)&ra)==-1); |
| 217 | CHECK(omega_blackwell_bind_matmul_realization(&spec,&ka,NULL)==-1); |
| 218 | CHECK(oracle_omega_blackwell_bind_matmul_realization(NULL,&ka,(OmegaBlackwellRealizationIdentity *)&ra)==-1); |
| 219 | CHECK(oracle_omega_blackwell_bind_matmul_realization(&spec,NULL,(OmegaBlackwellRealizationIdentity *)&ra)==-1); |
| 220 | CHECK(oracle_omega_blackwell_bind_matmul_realization(&spec,&ka,NULL)==-1); |
| 221 | for(unsigned p=0;p<4;p++) { |
| 222 | spec.precision=(OmegaMatMulPrecision)p; |
| 223 | int x=omega_blackwell_codegen_matmul(&spec,&ka),y=oracle_omega_blackwell_codegen_matmul(&spec,&kb); |
| 224 | CHECK(x==y); |
| 225 | if(x==0) { CHECK(ka.code_size==kb.code_size); CHECK(memcmp(ka.code,kb.code,ka.code_size)==0); } |
| 226 | omega_blackwell_kernel_free(&ka); oracle_omega_blackwell_kernel_free(&kb); |
| 227 | } |
| 228 | CHECK(omega_blackwell_codegen_matmul(NULL,&ka)==-1 && oracle_omega_blackwell_codegen_matmul(NULL,&kb)==-1); |
| 229 | CHECK(omega_blackwell_codegen_matmul(&spec,NULL)==-1 && oracle_omega_blackwell_codegen_matmul(&spec,NULL)==-1); |
| 230 | omega_blackwell_kernel_free(NULL); oracle_omega_blackwell_kernel_free(NULL); |
| 231 | uint32_t ai[6]={1,2,3,4,UINT32_MAX,6},bi[6]={6,5,4,3,2,1},ci[4],di[4]; |
| 232 | uint16_t ah[6]={0,0x3c00,0xbc00,1,0x4000,0x8000},bh[6]={0x3c00,0x4000,0xbc00,0,0x3c00,1}; float cf[4],df[4]; |
| 233 | CHECK(omega_matmul_cpu_oracle_i32(ai,bi,ci,2,3,2)==oracle_omega_matmul_cpu_oracle_i32(ai,bi,di,2,3,2)); |
| 234 | CHECK(memcmp(ci,di,sizeof ci)==0); |
| 235 | CHECK(omega_matmul_cpu_oracle_f16(ah,bh,cf,2,3,2)==oracle_omega_matmul_cpu_oracle_f16(ah,bh,df,2,3,2)); |
| 236 | CHECK(memcmp(cf,df,sizeof cf)==0); |
| 237 | CHECK(omega_matmul_cpu_oracle_bf16(ah,bh,cf,2,3,2)==oracle_omega_matmul_cpu_oracle_bf16(ah,bh,df,2,3,2)); |
| 238 | CHECK(memcmp(cf,df,sizeof cf)==0); |
| 239 | #define BAD_ORACLE(fn,a,b,c) do { CHECK(fn(NULL,b,c,1,1,1)==-1); CHECK(fn(a,NULL,c,1,1,1)==-1); CHECK(fn(a,b,NULL,1,1,1)==-1); CHECK(fn(a,b,c,0,1,1)==-1); CHECK(fn(a,b,c,1,0,1)==-1); CHECK(fn(a,b,c,1,1,0)==-1); } while(0) |
| 240 | BAD_ORACLE(oracle_omega_matmul_cpu_oracle_i32,ai,bi,ci); |
| 241 | BAD_ORACLE(oracle_omega_matmul_cpu_oracle_f16,ah,bh,cf); |
| 242 | BAD_ORACLE(oracle_omega_matmul_cpu_oracle_bf16,ah,bh,cf); |
| 243 | BAD_ORACLE(omega_matmul_cpu_oracle_i32,ai,bi,ci); |
| 244 | BAD_ORACLE(omega_matmul_cpu_oracle_f16,ah,bh,cf); |
| 245 | BAD_ORACLE(omega_matmul_cpu_oracle_bf16,ah,bh,cf); |
| 246 | CHECK(cpu_views()==0); |
| 247 | #ifdef OMEGA_BW_DIFF_RED |
| 248 | hash[0] ^= 1; |
| 249 | CHECK(memcmp(hash,saved,32)==0); |
| 250 | #endif |
| 251 | printf("PASS bw matmul realization differential checks=%u\n",checks); |
| 252 | return 0; |
| 253 | } |