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_activations_test.c

138 lines · 7237 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2#include "omega_gpu_dnn_activations_api.h"
3#include "omega_gpu_session.h"
4#include "cudapp_native.h"
5#include "bw_warp_sim_k.h"
6#include "gpu_dnn_activations_ref.h"
7#include <float.h>
8#include <stdio.h>
9static int failures,opens,locks,unlocks,frees,allocs,open_success,chip_mode,shell_mode;
10static OmegaGpuSession fake_session;
11OmegaGpuSession *__real_omega_gpu_session_open(void);
12void __real_omega_gpu_session_lock(void);
13void __real_omega_gpu_session_unlock(void);
14void __real_omega_gpu_session_free(OmegaGpuMem *);
15int __real_omega_gpu_session_alloc(size_t,OmegaGpuMem *);
16OmegaGpuSession *__wrap_omega_gpu_session_open(void) {
17 if(chip_mode) return __real_omega_gpu_session_open();
18 if(!shell_mode) {fputs("DNN-HOST-GUARD session_open\n",stderr);abort();}
19 ++opens;return open_success ? &fake_session : NULL;
20}
21void __wrap_omega_gpu_session_lock(void) {
22 if(chip_mode) {__real_omega_gpu_session_lock();return;}
23 if(!shell_mode) {fputs("DNN-HOST-GUARD session_lock\n",stderr);abort();}
24 ++locks;
25}
26void __wrap_omega_gpu_session_unlock(void) {
27 if(chip_mode) {__real_omega_gpu_session_unlock();return;}
28 if(!shell_mode) {fputs("DNN-HOST-GUARD session_unlock\n",stderr);abort();}
29 ++unlocks;
30}
31void __wrap_omega_gpu_session_free(OmegaGpuMem *m) {
32 if(chip_mode) {__real_omega_gpu_session_free(m);return;}
33 if(!shell_mode) {fputs("DNN-HOST-GUARD session_free\n",stderr);abort();}
34 ++frees;
35}
36int __wrap_omega_gpu_session_alloc(size_t bytes,OmegaGpuMem *m) {
37 if(chip_mode) return __real_omega_gpu_session_alloc(bytes,m);
38 if(!shell_mode) {fputs("DNN-HOST-GUARD session_alloc\n",stderr);abort();}
39 ++allocs;return -1;
40}
41#define CHECK(v) do { if (!(v)) { fprintf(stderr,"dnn-activations line %d: %s\n",__LINE__,#v);++failures; } } while(0)
42static void put64(uint32_t *p,uintptr_t v) { p[0]=(uint32_t)v;p[1]=(uint32_t)((uint64_t)v>>32); }
43static unsigned input_offset;
44static void *inputs(size_t n,unsigned dtype) {
45 static const float values[]={0.0f,-0.0f,0.000001f,-0.000001f,0.125f,-0.125f,0.5f,-0.5f,1,-1,2,-2,3,-3,5,-5,8,-8,10,-10,20,-20,80,-80,100,-100,65504,-65504};
46 void *p=malloc(n*(dtype?2:4));if (!p) abort();
47 for(size_t i=0;i<n;++i) {
48 float x=values[(i+input_offset)%(sizeof values/sizeof *values)];
49 if (i%113==111 && dtype!=2) x=FLT_MAX;
50 if (i%113==112 && dtype!=2) x=-FLT_MAX;
51 if (dtype==0) ((float*)p)[i]=x;
52 else if (dtype==1) { uint32_t b;memcpy(&b,&x,4);((uint16_t*)p)[i]=(uint16_t)(b>>16); }
53 else { _Float16 h=(_Float16)x;memcpy((uint16_t*)p+i,&h,2); }
54 }
55 return p;
56}
57static int run_case(uint64_t n,unsigned op,unsigned dtype,int wrong) {
58 OmegaGpuDnnActivationsShape shape={n,op,dtype};
59 void *in=inputs(n,dtype);float *out=malloc((n+1)*4);BlackwellIRProgram *p=malloc(sizeof *p);
60 uint8_t *code=malloc(OMEGA_BW_CODE_CAP_MAX);OmegaBlackwellKernel k;
61 if (!out||!p||!code) abort();
62 for(size_t i=0;i<=n;++i) out[i]=12345.0f;
63 int rc=omega_gpu_dnn_activations_codegen(&shape,p,code,OMEGA_BW_CODE_CAP_MAX,&k);
64 CHECK(rc==0);
65 if (!rc && chip_mode) { rc=omega_gpu_dnn_activations(&shape,in,out);CHECK(rc==0); }
66 if (!rc && !chip_mode) {
67 WsSim sim={0};sim.p=p;sim.threads=32;sim.order=op&1;sim.warp_sched=dtype&1;
68 uint32_t *cb=&sim.cbank[0x380/4];put64(cb,(uintptr_t)in);put64(cb+2,(uintptr_t)out);cb[4]=(uint32_t)n;
69 sim.ranges[0]=(WsRange){(uintptr_t)in,(uintptr_t)in+n*(dtype?2:4)};
70 sim.ranges[1]=(WsRange){(uintptr_t)out,(uintptr_t)out+n*4};sim.nranges=2;
71 rc=ws_run(&sim,(uint32_t)((n+31)/32),1);
72 if(rc) fprintf(stderr,"activation simulator: %s\n",sim.msg);
73 CHECK(rc==0);
74 }
75 int equal=!rc;
76 for(size_t i=0;i<n&&!rc;++i) {
77 double x=da_input(in,i,dtype),want=da_reference(x,op);
78 if(wrong) out[i]+=0.25f;
79 if(!da_equal(out[i],want)) { equal=0;if(!wrong) {fprintf(stderr,"activation n=%llu op=%u type=%u i=%zu x=%.9g got=%.9g want=%.17g\n",(unsigned long long)n,op,dtype,i,x,out[i],want);break;} }
80 if(x==0.0 && signbit(x) && !wrong) CHECK(signbit(out[i]));
81 }
82 CHECK(out[n]==12345.0f);
83 if(!wrong) CHECK(equal);
84 free(in);free(out);free(p);free(code);return equal;
85}
86static void refusals(void) {
87 OmegaGpuDnnActivationsShape s={0,0,0};float f=0;
88 CHECK(omega_gpu_dnn_activations(&s,&f,&f)==OMEGA_GPU_DNN_ACTIVATIONS_REFUSED);
89 s.elements=4294967296ULL;
90 CHECK(omega_gpu_dnn_activations(&s,&f,&f)==OMEGA_GPU_DNN_ACTIVATIONS_REFUSED);
91 s.elements=1;s.operation=4;
92 CHECK(omega_gpu_dnn_activations(&s,&f,&f)==OMEGA_GPU_DNN_ACTIVATIONS_REFUSED);
93 s.operation=0;s.dtype=3;
94 CHECK(omega_gpu_dnn_activations(&s,&f,&f)==OMEGA_GPU_DNN_ACTIVATIONS_REFUSED);
95 CHECK(opens==0 && locks==0 && unlocks==0 && frees==0 && allocs==0);
96 CHECK(omega_gpu_dnn_activations(NULL,&f,&f)==OMEGA_GPU_DNN_ACTIVATIONS_BAD_ARGS);
97 uint64_t result=99,plan[8]={0};
98 CHECK(omega_da_plan(&result,65,3,2,plan,8)==0 && result==0);
99 CHECK(plan[0]==130 && plan[1]==260 && plan[2]==3 && plan[3]==32);
100 CHECK(omega_da_plan(&result,4294967295ULL,3,0,plan,8)==0 && result==0);
101 CHECK(plan[0]==17179869180ULL && plan[2]==134217728ULL);
102 if(shell_mode) {
103 s.elements=1;s.operation=0;s.dtype=0;
104 CHECK(omega_gpu_dnn_activations(&s,&f,&f)==OMEGA_GPU_DNN_ACTIVATIONS_CHIP);
105 CHECK(opens==1 && locks==1 && unlocks==1 && frees==3 && allocs==0);
106 open_success=1;
107 CHECK(omega_gpu_dnn_activations(&s,&f,&f)==OMEGA_GPU_DNN_ACTIVATIONS_CHIP);
108 CHECK(opens==2 && locks==2 && unlocks==2 && frees==6 && allocs==1);
109 open_success=0;
110 }
111}
112int main(int argc,char **argv) {
113 if(argc!=2 || (strcmp(argv[1],"--sim")&&strcmp(argv[1],"--quick")&&strcmp(argv[1],"--chip")&&strcmp(argv[1],"--shell-contract"))) return 2;
114 chip_mode=!strcmp(argv[1],"--chip");
115 shell_mode=!strcmp(argv[1],"--shell-contract");
116#ifndef DA_CHIP_RUNNER
117 if(chip_mode) {fputs("NOT_RUN: build the separate operator-only chip runner\n",stderr);return 2;}
118#else
119 if(!chip_mode) return 2;
120#endif
121#ifdef DA_GUARD_MUTANT
122 if(!chip_mode && !shell_mode) (void)omega_gpu_session_open();
123#endif
124 int quick=!strcmp(argv[1],"--quick");refusals();
125 if(shell_mode) {
126 if(failures) {puts("dnn-activations shell-contract: FAIL");return 1;}
127 puts("dnn-activations shell-contract: PASS (NULL open: 0 allocs; non-NULL open: 1 failed alloc; isolated fake backend)");return 0;
128 }
129 CHECK(!run_case(65,1,0,1));
130 static const uint64_t counts[]={1,2,7,31,32,33,64,65,513,65535};
131 for(unsigned op=0;op<4;++op) for(unsigned dtype=0;dtype<3;++dtype)
132 for(unsigned j=0;j<(quick?8u:10u);++j) {
133 unsigned rotations=counts[j]<31 ? 28u : 1u;
134 for(input_offset=0;input_offset<rotations;++input_offset) CHECK(run_case(counts[j],op,dtype,0));
135 }
136 if(failures) {printf("dnn-activations %s: FAIL (%d checks)\n",chip_mode?"chip":"host+warp-sim",failures);return 1;}
137 printf("dnn-activations %s: PASS (4 operators, 3 types, tails, independent double references, refusal-before-open, wrong-result control)\n",chip_mode?"chip":"host+warp-sim");return 0;
138}