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 / om / q38-head / norm-check.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* External verification instrument for the existing 5120-wide final RMSNorm. */ |
| 3 | #include "omega_blackwell_codegen_dec.h" |
| 4 | #include "omega_gpu_session.h" |
| 5 | #include <assert.h> |
| 6 | #include <float.h> |
| 7 | #include <math.h> |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | #include "../standalone-oracles/oracle.h" |
| 12 | |
| 13 | #define H 5120u |
| 14 | static void demand(int rc) { |
| 15 | if (rc) { fprintf(stderr, "GPU: %s\n", omega_gpu_session_last_error()); exit(4); } |
| 16 | } |
| 17 | static void pointer(uint32_t *a, unsigned off, uint64_t p) { |
| 18 | a[(off-0x380)/4]=(uint32_t)p; a[(off-0x380)/4+1]=(uint32_t)(p>>32); |
| 19 | } |
| 20 | static void scalar(uint32_t *a, unsigned off, float x) { memcpy(a+(off-0x380)/4,&x,4); } |
| 21 | static uint32_t rng=710; |
| 22 | static float random_value(void) { |
| 23 | rng=(uint32_t)so_call(22,rng,0,0,0); |
| 24 | return so_float(so_call(23,rng,0,0,0)); |
| 25 | } |
| 26 | int main(int argc,char **argv) { |
| 27 | if(argc!=4 || strcmp(argv[1],"--chip")) return 65; |
| 28 | char *end=NULL; float envelope=strtof(argv[3],&end); |
| 29 | if(!end || *end || !so_all(so_call(24,so_bits(envelope),0,0,0),so_call(37,so_bits(envelope),so_bits(0.0f),0,0),1)) return 65; |
| 30 | float weights[H]; FILE *f=fopen(argv[2],"rb"); assert(f); |
| 31 | assert(fread(weights,4,H,f)==H && fgetc(f)==EOF); fclose(f); |
| 32 | float wm=0; for(unsigned j=0;j<H;j++) {assert(so_call(24,so_bits(weights[j]),0,0,0));wm=so_float(so_call(25,so_bits(wm),so_bits(weights[j]),0,0));} |
| 33 | OmegaDecSpec spec={0};spec.kind=OMEGA_DEC_RES;spec.mode=OMEGA_DEC_RES_NORM;spec.width=H; |
| 34 | static BlackwellIRProgram program; |
| 35 | assert(!omega_blackwell_codegen_dec_prog(&spec,&program)); |
| 36 | size_t size=omega_bw_program_code_bytes(&program); assert(size); |
| 37 | OmegaGpuMem code={0},data={0};OmegaBlackwellKernel kernel; |
| 38 | omega_gpu_session_lock(); demand(omega_gpu_session_open()?0:-1); |
| 39 | demand(omega_gpu_session_alloc_code(size,&code)); |
| 40 | assert(!omega_blackwell_kernel_encode_into(&program,code.cpu,size,&kernel)); |
| 41 | demand(omega_gpu_session_sync_to_gpu(&code)); |
| 42 | demand(omega_gpu_session_alloc(so_bytes(H,12,0),&data)); |
| 43 | float *x=data.cpu,*w=x+H,*y=w+H; |
| 44 | uint32_t args[OMEGA_DEC_RES_WORDS]={0}; |
| 45 | pointer(args,OMEGA_DEC_RES_X,data.va);pointer(args,OMEGA_DEC_RES_NW,so_call(5,data.va,so_bytes(H,4,0),0,0)); |
| 46 | pointer(args,OMEGA_DEC_RES_Y,so_call(5,data.va,so_bytes(H,8,0),0,0)); |
| 47 | scalar(args,OMEGA_DEC_RES_EPS,1e-6f);scalar(args,OMEGA_DEC_RES_SCALE,1.0f); |
| 48 | OmegaGpuLaunch launch={0};launch.code_va=code.va; |
| 49 | launch.gpr_count=omega_bw_gpr_declared(kernel.gpr_count); |
| 50 | if(launch.gpr_count<64)launch.gpr_count=64; |
| 51 | launch.threads_x=256;launch.threads_y=1;launch.grid_x=launch.grid_y=1; |
| 52 | launch.num_elements=H;launch.args=args;launch.n_args=OMEGA_DEC_RES_WORDS;launch.timeout_ms=20000; |
| 53 | int exponents[]={-149,-126,-80,-20,-10,0,20,60,120}; |
| 54 | unsigned checks=0,failed=0;long double largest=0; |
| 55 | for(unsigned wi=0;wi<2;wi++) for(unsigned e=0;e<sizeof exponents/sizeof *exponents;e++) |
| 56 | for(unsigned pattern=0;pattern<5;pattern++) { |
| 57 | for(unsigned j=0;j<H;j++) { |
| 58 | float z=so_call(34,pattern,3,0,0)?so_float(so_call(27,j,pattern,0,0)): |
| 59 | so_eq(pattern,3)?random_value():ldexpf(random_value(),(int)(int64_t)so_call(40,j,0,0,0)); |
| 60 | x[j]=ldexpf(z,exponents[e]);w[j]=wi?so_float(so_call(39,j,so_bits(wm),0,0)):weights[j];y[j]=NAN; |
| 61 | } |
| 62 | demand(omega_gpu_session_sync_to_gpu(&data)); |
| 63 | demand(omega_gpu_session_launch(&launch,NULL,NULL)); |
| 64 | demand(omega_gpu_session_sync_to_cpu(&data)); |
| 65 | /* Independent binary128 oracle: seeded Omega has no f128 type. |
| 66 | * Keep square, sqrtl and reference comparison at their original precision. */ |
| 67 | long double square=0; |
| 68 | for(unsigned j=0;j<H;j++) {failed=so_bad(failed,(int)so_call(24,so_bits(y[j]),0,0,0));square+=(long double)y[j]*y[j];} |
| 69 | long double norm=sqrtl(square);if(norm>largest)largest=norm; |
| 70 | int oracle_ok=norm<=envelope; |
| 71 | failed=so_bad(failed,oracle_ok); |
| 72 | if(!oracle_ok) printf("FAIL weight=%u exponent=%d pattern=%u norm=%.12Lg bound=%.12g\n",wi,exponents[e],pattern,norm,envelope); |
| 73 | checks=(unsigned)so_call(5,checks,1,0,0); |
| 74 | } |
| 75 | omega_gpu_session_free(&data);omega_gpu_session_free(&code);omega_gpu_session_unlock(); |
| 76 | printf("RMSNorm envelope: %u cases, %u failures, largest norm %.12Lg, bound %.12g: %s\n",checks,failed,largest,envelope,failed?"FAIL":"PASS"); |
| 77 | return failed?1:0; |
| 78 | } |