interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / tests / gpu_dnn_rows_test.c

110 lines · 6133 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2#include "omega_gpu_dnn_rows_api.h"
3#include "bw_warp_sim_k.h"
4#include "omega_gpu_session.h"
5#include "gpu_dnn_rows_ref.h"
6#include <stdio.h>
7static int failures;
8static int allow_chip, shell_contract, session_attempts, allocation_attempts, fake_open_success;
9OmegaGpuSession *__real_omega_gpu_session_open(void);
10int __real_omega_gpu_session_alloc(size_t,OmegaGpuMem *);
11void __real_omega_gpu_session_lock(void);
12void __real_omega_gpu_session_unlock(void);
13void __real_omega_gpu_session_free(OmegaGpuMem *);
14static void host_guard(const char *name) {
15 if(!allow_chip&&!shell_contract) {
16 fprintf(stderr,"dnn-rows HOST DEVICE GUARD: %s\n",name);
17 abort();
18 }
19}
20OmegaGpuSession *__wrap_omega_gpu_session_open(void) {
21 host_guard("session_open");
22 ++session_attempts;
23 return allow_chip ? __real_omega_gpu_session_open() : (fake_open_success?(OmegaGpuSession *)(uintptr_t)1:NULL);
24}
25int __wrap_omega_gpu_session_alloc(size_t n,OmegaGpuMem *p) {
26 host_guard("session_alloc");
27 ++allocation_attempts;
28 return allow_chip ? __real_omega_gpu_session_alloc(n,p) : -1;
29}
30void __wrap_omega_gpu_session_lock(void) { host_guard("session_lock"); if(allow_chip) __real_omega_gpu_session_lock(); }
31void __wrap_omega_gpu_session_unlock(void) { host_guard("session_unlock"); if(allow_chip) __real_omega_gpu_session_unlock(); }
32void __wrap_omega_gpu_session_free(OmegaGpuMem *p) { host_guard("session_free"); if(allow_chip) __real_omega_gpu_session_free(p); }
33#define CHECK(x) do { if(!(x)) { fprintf(stderr,"rows failure line %d: %s\n",__LINE__,#x); ++failures; } } while(0)
34static void putptr(uint32_t *p,uintptr_t a) {p[0]=(uint32_t)a;p[1]=(uint32_t)((uint64_t)a>>32);}
35static void *input(size_t n,unsigned dt,int constant) {
36 void *p=malloc(n*(dt?2u:4u)); if(!p) abort();
37 for(size_t i=0;i<n;++i) {
38 float f=constant?0.25f:(float)((int)((i*17+i/7)%33)-16)*0.03125f;
39 if(constant==2) f=1024.0f+(float)(i%7);
40 if(dt==0) ((float*)p)[i]=f;
41 else if(dt==1) {uint32_t b;memcpy(&b,&f,4);((uint16_t*)p)[i]=(uint16_t)(b>>16);}
42 else {_Float16 h=(_Float16)f;memcpy((uint16_t*)p+i,&h,2);}
43 }
44 return p;
45}
46static int run(OmegaGpuDnnRowsShape s,int constant,int chip,int wrong) {
47 size_t n=(size_t)s.rows*s.length;void *in=input(n,s.dtype,constant);
48 float *out=malloc(n*4);double *ref=malloc(n*sizeof *ref);
49 BlackwellIRProgram *p=malloc(sizeof *p);uint8_t *code=malloc(65536);OmegaBlackwellKernel k;
50 if(!out||!ref||!p||!code) abort();
51 memset(out,0xa5,n*4);
52 int rc=omega_gpu_dnn_rows_codegen(&s,p,code,65536,&k);
53 if(rc) fprintf(stderr,"codegen rc=%d op=%u n=%u dt=%u\n",rc,s.op,s.length,s.dtype);
54 CHECK(rc==0);
55 if(!rc&&chip) rc=omega_gpu_dnn_rows(&s,in,out);
56 if(!rc&&!chip) {
57 WsSim sim={0};sim.p=p;sim.threads=32;sim.order=constant&1;sim.warp_sched=constant&1;
58 uint32_t *cb=&sim.cbank[896/4];putptr(cb,(uintptr_t)in);putptr(cb+2,(uintptr_t)out);memcpy(cb+4,&s.epsilon,4);
59 sim.ranges[0]=(WsRange){(uintptr_t)in,(uintptr_t)in+n*(s.dtype?2:4)};
60 sim.ranges[1]=(WsRange){(uintptr_t)out,(uintptr_t)out+n*4};sim.nranges=2;
61 rc=ws_run(&sim,s.rows,1);if(rc)fprintf(stderr,"rows sim %s\n",sim.msg);CHECK(rc==0);
62 }
63 dr_reference(&s,in,ref);if(wrong) ref[0]+=1.0;
64 int equal=!rc&&dr_equal(out,ref,n);if(!wrong)CHECK(equal);
65 free(in);free(out);free(ref);free(p);free(code);return equal;
66}
67static void refuse(OmegaGpuDnnRowsShape s) {
68 float dummy=0;BlackwellIRProgram *p=malloc(sizeof *p);uint8_t code[4096];OmegaBlackwellKernel k;
69 CHECK(omega_gpu_dnn_rows_codegen(&s,p,code,sizeof code,&k)==OMEGA_GPU_DNN_ROWS_REFUSED);
70 CHECK(omega_gpu_dnn_rows(&s,&dummy,&dummy)==OMEGA_GPU_DNN_ROWS_REFUSED);free(p);
71}
72int main(int argc,char **argv) {
73 int quick=argc==2&&!strcmp(argv[1],"--quick"),chip=argc==2&&!strcmp(argv[1],"--chip");
74 allow_chip=chip;
75 shell_contract=argc==2&&!strcmp(argv[1],"--shell-contract");
76 if(shell_contract) {
77 float in=1,out=0;
78 OmegaGpuDnnRowsShape s={1,1,0,0,0};
79 CHECK(omega_gpu_dnn_rows(&s,&in,&out)==OMEGA_GPU_DNN_ROWS_CHIP);
80 CHECK(session_attempts==1&&allocation_attempts==0);
81 fake_open_success=1;session_attempts=0;allocation_attempts=0;
82 CHECK(omega_gpu_dnn_rows(&s,&in,&out)==OMEGA_GPU_DNN_ROWS_CHIP);
83 CHECK(session_attempts==1&&allocation_attempts==1);
84 if(failures)return 1;
85 puts("dnn-rows shell contract: PASS (NULL open zero allocs; non-NULL open one blocked alloc; fake backend only)");
86 return 0;
87 }
88 if(!quick&&!chip&&!(argc==2&&!strcmp(argv[1],"--sim")))return 2;
89 OmegaGpuDnnRowsShape s={2,33,0,0,0.03125f};
90 CHECK(run(s,0,chip,0));CHECK(!run(s,0,chip,1));
91 for(unsigned op=0;op<3;++op) {s.op=op;s.dtype=2;CHECK(run(s,0,chip,0));}
92 s=(OmegaGpuDnnRowsShape){2,65,0,0,0};
93 CHECK(run(s,2,chip,0));
94 s=(OmegaGpuDnnRowsShape){0,1,0,0,1};refuse(s);s.rows=1;s.length=0;refuse(s);
95 s.length=65536;refuse(s);s.length=1;s.rows=65536;refuse(s);s.rows=1;s.dtype=3;refuse(s);
96 s.dtype=0;s.op=3;refuse(s);s.op=1;s.epsilon=0;refuse(s);s.epsilon=-1;refuse(s);
97 s.epsilon=NAN;refuse(s);s.epsilon=INFINITY;refuse(s);s.epsilon=0x1p-149f;refuse(s);
98 s.epsilon=1;s.rows=65535;s.length=65535;refuse(s);
99 CHECK(omega_gpu_dnn_rows(NULL,NULL,NULL)==OMEGA_GPU_DNN_ROWS_BAD_ARGS);
100 if(!quick) {
101 const unsigned lens[]={1,2,7,31,32,33,64,65,513,65535};
102 const float eps[]={0.0009765625f,0.03125f,1.0f};
103 for(unsigned op=0;op<3;++op) for(unsigned dt=0;dt<3;++dt)
104 for(unsigned l=0;l<sizeof lens/sizeof *lens;++l) for(unsigned e=0;e<3;++e)
105 for(int c=0;c<2;++c) {s=(OmegaGpuDnnRowsShape){2,lens[l],dt,op,eps[e]};CHECK(run(s,c,chip,0));}
106 }
107 if(!chip) CHECK(session_attempts==0&&allocation_attempts==0);
108 if(failures){printf("dnn-rows: FAIL (%d checks)\n",failures);return 1;}
109 printf("dnn-rows %s: PASS (all lengths/types/epsilon, constant+varied rows, tail ranges, refusals, wrong-result control)\n",chip?"chip":"host+warp-sim");return 0;
110}