Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/tests/gemm_ex_om_num_test.c # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tests / gemm_ex_om_num_test.c` 86 lines · 4778 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* Exhaustive differential check of om/gemmex/num.om against the C number formats of src/omega_gpu_gemm_ex.c. 3 * Build: cat om/gemmex/num.om om/gemmex/probe.om > t.om; OMEGA_SEEDED_OMC=$OMEGA_OMC sh $OMEGA_BRIDGE/tools/bridge-seeded c t.om > t.c; 4 * gcc -std=gnu11 -O2 -ffp-contract=off -o h tests/gemm_ex_om_num_test.c t.c -lm; GX_STEP=13 ./h */ 5 #include 6 #include 7 #include 8 #include 9 #include 10 #define OMEGA_GEMM_DT_F32 0 11 #define OMEGA_GEMM_DT_F16 2 12 13 static float u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } 14 static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } 15 static int fin(float f) { return (f2u(f) & 0x7f800000u) != 0x7f800000u; } 16 static double dabs(double v) { return v < 0 ? -v : v; } 17 18 static float h2f(uint16_t h) { 19 uint32_t s = (uint32_t)(h >> 15) << 31, e = (h >> 10) & 31u, m = h & 0x3ffu; 20 if (e == 31u) return u2f(s | 0x7f800000u | m << 13); 21 if (e == 0u) { float v = (float)m * (1.0f / `16777216`.0f); return s ? -v : v; } /* m 2^-24, exact */ 22 return u2f(s | (e + 112u) << 23 | m << 13); 23 } 24 static float e4m3f(uint8_t b) { 25 uint32_t e = (b >> 3) & 15u, m = b & 7u; 26 if (e == 15u && m == 7u) return u2f(0x7fc00000u); 27 float v = e == 0u ? (float)m * (1.0f / 512.0f) : u2f((e + 120u) << 23 | m << 20); 28 return b & 0x80u ? -v : v; 29 } 30 static uint16_t f2bf_rne(float f) { 31 uint32_t u = f2u(f); 32 if (!fin(f)) return (uint16_t)(u >> 16 | ((u & 0x7fffffu) ? 0x40u : 0u)); 33 return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16); 34 } 35 static uint16_t f2h_rne(float f) { 36 uint32_t u = f2u(f), s = (u >> 16) & 0x8000u, a = u & 0x7fffffffu; 37 if (a >= 0x7f800000u) return (uint16_t)(s | 0x7c00u | (a > 0x7f800000u ? 0x200u : 0u)); 38 if (a >= 0x477ff000u) return (uint16_t)(s | 0x7c00u); /* >= 65520 rounds past the f16 maximum */ 39 if (a < 0x38800000u) { /* below 2^-14: an f16 subnormal (unit 2^-24) or zero */ 40 if (a < 0x33000000u) return (uint16_t)s; /* below 2^-25 */ 41 uint32_t mant = (a & 0x7fffffu) | 0x800000u, sh = 126u - (a >> 23); 42 uint32_t q = mant >> sh, rem = mant & ((1u << sh) - 1u), half = 1u << (sh - 1u); 43 if (rem > half || (rem == half && (q & 1u))) q++; 44 return (uint16_t)(s | q); 45 } 46 uint32_t r = a + 0xfffu + ((a >> 13) & 1u); 47 return (uint16_t)(s | (((r >> 23) - 112u) << 10 | ((r >> 13) & 0x3ffu))); 48 } 49 /* double to float rounding to odd: one more rounding to bf16 or f16 is then the correct rounding of v */ 50 static float d2f_odd(double v) { 51 float f = (float)v; 52 if (v != v || !fin(f) || (double)f == v) return f; 53 uint32_t u = f2u(f); 54 if (u & 1u) return f; 55 return u2f(dabs(v) > dabs((double)f) ? u + 1u : u - 1u); 56 } 57 /* exact split of a finite f32 into three bf16 planes by truncation: f = hi + mid + lo, except the parts below 2^-133 */ 58 static void split3(float f, uint16_t p[3]) { 59 if (!fin(f)) { p[0] = p[1] = p[2] = 0; return; } 60 uint32_t u = f2u(f); 61 p[0] = (uint16_t)(u >> 16); 62 float r1 = f - u2f(u & 0xffff0000u); 63 uint32_t u1 = f2u(r1); 64 p[1] = (uint16_t)(u1 >> 16); 65 p[2] = (uint16_t)(f2u(r1 - u2f(u1 & 0xffff0000u)) >> 16); 66 } 67 uint32_t omega_gx_probe(uint64_t *result, uint64_t p0, uint64_t p1); 68 static uint64_t P(uint64_t op, uint64_t a){ uint64_t r; if (omega_gx_probe(&r, op, a)) { printf("trap\n"); exit(2);} return r; } 69 static uint64_t bad=0, n=0; 70 #define CK(c,msg,x) do{ n++; if(!(c)){ if(bad++<5) printf("FAIL %s %llx\n",msg,(unsigned long long)(x)); } }while(0) 71 int main(void){ 72 for (uint32_t h=0; h<65536; h++) CK(P(0,h)==f2u(h2f(h)),"h2f",h); 73 for (uint32_t b=0; b<256; b++) CK(P(1,b)==f2u(e4m3f(b)),"e4m3",b); 74 uint64_t step = getenv("GX_STEP") ? strtoull(getenv("GX_STEP"),0,10) : 13; 75 for (uint64_t x=0; x<(1ull<<32); x+=step) { uint32_t u=(uint32_t)x; 76 CK(P(2,u)==f2bf_rne(u2f(u)),"f2bf",u); 77 CK(P(3,u)==f2h_rne(u2f(u)),"f2h",u); 78 uint16_t p[3]; split3(u2f(u),p); CK(P(4,u)==((uint64_t)p[0]|(uint64_t)p[1]<<16|(uint64_t)p[2]<<32),"split",u); } 79 uint64_t s=88172645463325252ull; 80 for (int i=0;i<`20000000`;i++){ s^=s<<13; s^=s>>7; s^=s<<17; uint64_t d=s; 81 if (i&1) { double f=(double)u2f((uint32_t)(s>>32)); memcpy(&d,&f,8); d ^= (s & 0xfff) * (i&2?0:1); } 82 double v; memcpy(&v,&d,8); CK(P(5,d)==f2u(d2f_odd(v)),"d2fo",d); } 83 static const uint32_t m[5]={0x7f800000u,0x7f80u,0x7c00u,0x7fu,0x7cu}; 84 for (uint64_t t=0;t<5;t++) for (uint32_t u=0;u<65536;u++){ uint32_t v= t==0 ? u<<16|u : u; CK(P(6,t<<32|v)==((v&m[t])==m[t]),"bad",t<<32|v);} 85 printf("%s gemmex num: %llu checks, %llu failed\n", bad?"FAIL":"PASS", (unsigned long long)n,(unsigned long long)bad); return bad!=0; 86 }