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

129 lines · 5718 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* Native words derive from omega_numeric.c E1_CVT and its sm_121 oracle.
3 * Independent integer references live in omega_numeric.c, not this simulator. */
4#include "omega_blackwell_codegen.h"
5#include "omega_numeric.h"
6#ifdef DNN_HALF_ALT_SIM
7#include "bw_warp_sim.h"
8#else
9#include "bw_warp_sim_k.h"
10#endif
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <fcntl.h>
15#include <unistd.h>
16
17static int words(BlackwellIRProgram *p, unsigned op, unsigned d, unsigned a,
18 int guard, uint8_t *code, size_t *bytes) {
19 omega_bw_ir_init(p);
20 if (omega_bw_ir_alloc_vreg(p) != 0 || omega_bw_ir_alloc_vreg(p) != 1) abort();
21 p->regalloc.vreg_to_phys[0] = (int)d;
22 p->regalloc.vreg_to_phys[1] = (int)a;
23 BlackwellIRInsn in = {.op=(BlackwellIROpcode)op,.dst_vreg=0,.src1_vreg=1,
24 .src2_vreg=-1,.src3_vreg=-1,.ureg=-1,.control=0x010fca00u,
25 .predicate_p0=guard!=0,.predicate_not=guard==2};
26 if (omega_bw_ir_append(p,&in)) abort();
27 return omega_bw_encode_program(p,code,16384,bytes);
28}
29static int golden(BlackwellIRProgram *p, const char *dump) {
30 uint8_t code[16384];size_t bytes=0;
31 unsigned regs[][2]={{9,2},{0,0},{13,27},{254,253}};
32 int fd=dump?open(dump,O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC,0600):-1;
33 FILE *f=fd<0?NULL:fdopen(fd,"wb");
34 if(!f&&fd>=0)close(fd);
35 if(dump&&!f)return 0;
36 for(unsigned op=BW_IR_F16_TO_F32;op<=BW_IR_F32_TO_F16;op++)for(unsigned j=0;j<4;j++)for(int g=0;g<3;g++) {
37 unsigned d=regs[j][0],a=regs[j][1];uint32_t w[4];
38 if(words(p,op,d,a,g,code,&bytes)){if(f)fclose(f);return 0;}
39 memcpy(w,code,sizeof w);
40 uint32_t pred=(g==0?7u:g==1?0u:8u)<<12;
41 uint32_t base=op==BW_IR_F16_TO_F32?0xff007230u:0x7304u;
42 uint32_t want0=(base&~0xf000u)|(d<<16)|pred;
43 uint32_t want1=(op==BW_IR_F16_TO_F32?0x20000000u:0u)|a;
44 if(w[0]!=want0||w[1]!=want1||w[2]!=(op==BW_IR_F16_TO_F32?0x4100u:0x200800u)||w[3]!=0x010fca00u) {
45 if(f)fclose(f);
46 return 0;
47 }
48 if(f&&fwrite(code,1,16,f)!=16){fclose(f);return 0;}
49 }
50 if(f&&fclose(f))return 0;
51 /* Unknown op is rejected, not silently encoded as a neighbouring conversion. */
52 return words(p,BW_IR_LAST+1,9,2,0,code,&bytes)!=0;
53}
54static int simulation(BlackwellIRProgram *p) {
55 WsSim s={0};WsLane lane={0};s.p=p;
56 p->regalloc.vreg_to_phys[0]=0;p->regalloc.vreg_to_phys[1]=1;
57 BlackwellIRInsn in={.op=BW_IR_F16_TO_F32,.dst_vreg=0,.src1_vreg=1,
58 .src2_vreg=-1,.src3_vreg=-1,.ureg=-1};
59 for(unsigned h=0;h<65536;h++) {
60 lane.r[1]=h;s.err=0;ws_lane_exec(&s,&lane,&in,0,0,0,0);
61 float want=omega_ref_f16_to_f32((uint16_t)h),got=ws_u2f(lane.r[0]);
62 if(s.err||(!isnan(want)&&ws_f2u(want)!=ws_f2u(got))||(isnan(want)&&!isnan(got)))return 0;
63 if((h&0x7c00u)!=0x7c00u) {
64 in.op=BW_IR_F32_TO_F16;lane.r[1]=ws_f2u(want);s.err=0;
65 ws_lane_exec(&s,&lane,&in,0,0,0,0);
66 if(s.err||lane.r[0]!=h)return 0;
67 in.op=BW_IR_F16_TO_F32;
68 }
69 }
70 in.op=BW_IR_F32_TO_F16;
71 uint32_t seed=1;
72 for(unsigned i=0;i<65536;i++) {
73 seed=seed*1664525u+1013904223u;lane.r[1]=seed;s.err=0;
74 ws_lane_exec(&s,&lane,&in,0,0,0,0);
75 uint16_t want=omega_ref_f32_to_f16(ws_u2f(seed));
76 if(s.err||((want&0x7fffu)>0x7c00u?((lane.r[0]&0x7fffu)<=0x7c00u):lane.r[0]!=want))return 0;
77 }
78 omega_bw_ir_init(p);
79 in.op=BW_IR_EXIT;
80 in.dst_vreg=in.src1_vreg=in.src2_vreg=in.src3_vreg=-1;
81 if(omega_bw_ir_append(p,&in))abort();
82 s.threads=32;
83 return ws_run(&s,1,1)==0;
84}
85/* Exercise the warp scheduler, not only its unguarded per-lane ALU helper.
86 * Both simulator headers must preserve destinations in predicated-off lanes. */
87static int predicates(BlackwellIRProgram *p) {
88 omega_bw_ir_init(p);
89 p->count=2;
90 p->regalloc.vreg_to_phys[0]=0;
91 p->regalloc.vreg_to_phys[1]=1;
92 p->insns[1]=(BlackwellIRInsn){.op=BW_IR_EXIT};
93 for(unsigned op=BW_IR_F16_TO_F32;op<=BW_IR_F32_TO_F16;op++)for(unsigned guard=0;guard<3;guard++)
94 for(unsigned pattern=0;pattern<3;pattern++) {
95 uint32_t input=op==BW_IR_F16_TO_F32?1u:ws_f2u(1.f);
96 uint32_t converted=op==BW_IR_F16_TO_F32?ws_f2u(omega_ref_f16_to_f32((uint16_t)input)):
97 omega_ref_f32_to_f16(ws_u2f(input));
98 p->insns[0]=(BlackwellIRInsn){.op=(BlackwellIROpcode)op,
99 .dst_vreg=0,.src1_vreg=1,.src2_vreg=-1,.src3_vreg=-1,
100 .predicate_p0=guard!=0,.predicate_not=guard==2};
101 WsWarp w={0};
102 WsSim s={.p=p};
103#ifdef DNN_HALF_ALT_SIM
104 w.present=UINT32_MAX;
105#endif
106 w.nfrag=1;
107 w.frag[0].mask=UINT32_MAX;
108 for(unsigned lane=0;lane<WS_WARP;lane++) {
109 w.lane[lane].p0=(uint8_t)(pattern==2?lane&1u:pattern);
110 w.lane[lane].r[0]=0xfeedfaceu;
111 w.lane[lane].r[1]=input;
112 }
113 if(ws_run_warp(&s,&w,0,0,0,0)!=0)return 0;
114 for(unsigned lane=0;lane<WS_WARP;lane++) {
115 int active=guard==0||w.lane[lane].p0!=(guard==2);
116 if(w.lane[lane].r[0]!=(active?converted:0xfeedfaceu))return 0;
117 }
118 }
119 return 1;
120}
121int main(int argc,char **argv) {
122 BlackwellIRProgram *p=malloc(sizeof *p);if(!p)return 2;
123 int okay=golden(p,argc==2?argv[1]:NULL);
124 if(okay)okay=simulation(p);
125 if(okay)okay=predicates(p);
126 free(p);
127 puts(okay?"dnn-half: PASS (native words, predicates, exhaustive half reference, narrowing controls)":"dnn-half: FAIL (native encoding or independent reference)");
128 return !okay;
129}