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 / codegen_tern_diff_test.c

91 lines · 6021 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#define CUDAPP_MIGRATION_PENDING_C_CODEGEN_TERN 1
6#define omega_tern_gemv_chunks oracle_chunks
7#define omega_tern_gemv_spec_ok oracle_spec_ok
8#define omega_tern_gemv_steps oracle_steps
9#define omega_tern_gemv_coalesced_out oracle_coalesced
10#define omega_tern_gemv_out_slot_bytes oracle_slot
11#define omega_tern_gemv_shared_bytes oracle_shared
12#define omega_tern_word_offset oracle_offset
13#define omega_tern_i2fp_violations oracle_violations
14#define omega_blackwell_codegen_tern_gemv_prog oracle_prog
15#define omega_tern_gemv_prog_stats oracle_stats
16#include "../src/omega_blackwell_codegen_tern.c"
17#undef omega_tern_gemv_chunks
18#undef omega_tern_gemv_spec_ok
19#undef omega_tern_gemv_steps
20#undef omega_tern_gemv_coalesced_out
21#undef omega_tern_gemv_out_slot_bytes
22#undef omega_tern_gemv_shared_bytes
23#undef omega_tern_word_offset
24#undef omega_tern_i2fp_violations
25#undef omega_blackwell_codegen_tern_gemv_prog
26#undef omega_tern_gemv_prog_stats
27#include "omega_gpu_om_ir.h"
28#include "cudapp_native.h"
29extern uint32_t omega_tern_gemv_chunks(const OmegaTernGemvSpec *);
30extern int omega_tern_gemv_spec_ok(const OmegaTernGemvSpec *);
31extern uint32_t omega_tern_gemv_steps(const OmegaTernGemvSpec *);
32extern int omega_tern_gemv_coalesced_out(const OmegaTernGemvSpec *);
33extern uint32_t omega_tern_gemv_out_slot_bytes(const OmegaTernGemvSpec *);
34extern uint32_t omega_tern_gemv_shared_bytes(const OmegaTernGemvSpec *);
35extern uint64_t omega_tern_word_offset(uint32_t,uint32_t,uint32_t,uint32_t,uint32_t);
36extern long omega_tern_i2fp_violations(const BlackwellIRProgram *);
37extern int omega_blackwell_codegen_tern_gemv_prog(const OmegaTernGemvSpec *,BlackwellIRProgram *);
38static BlackwellIRProgram a,b;
39static unsigned cases,refused;
40static int equal(const OmegaTernGemvSpec *s) {
41 int ra=oracle_prog(s,&a),rb=omega_blackwell_codegen_tern_gemv_prog(s,&b);
42 if((ra==0)!=(rb==0)) goto fail;
43 /* A refused program has no specified IR output; compare the refusal, not stale scratch state. */
44 if(ra){refused++;return 1;}
45 if(a.count!=b.count||memcmp(a.insns,b.insns,a.count*sizeof a.insns[0])||memcmp(&a.regalloc,&b.regalloc,sizeof a.regalloc)) goto fail;
46 static uint8_t x[BW_MAX_IR_INSNS*16],y[BW_MAX_IR_INSNS*16];size_t nx=0,ny=0;
47 if(omega_bw_encode_program(&a,x,sizeof x,&nx)||omega_bw_encode_program(&b,y,sizeof y,&ny)||nx!=ny||memcmp(x,y,nx))goto fail;
48 if(omega_tern_i2fp_violations(&b)!=oracle_violations(&a))goto fail;
49 cases++;return 1;
50fail:
51 fprintf(stderr,"TERN DIFF k=%u layout=%u rows=%u warps=%u depth=%u epi=%u mutant=%d C=%d Omega=%d count=%zu/%zu\n",s->k,s->layout,s->rows,s->warps,s->depth,s->epi,s->mutant,ra,rb,a.count,b.count);
52 for(size_t i=0;i<a.count&&i<b.count;i++)if(memcmp(&a.insns[i],&b.insns[i],sizeof a.insns[i])){
53 fprintf(stderr,"first IR difference %zu op=%d/%d ctrl=%x/%x dst=%d/%d src=%d,%d,%d/%d,%d,%d imm=%x/%x u=%d/%d\n",i,a.insns[i].op,b.insns[i].op,a.insns[i].control,b.insns[i].control,a.insns[i].dst_vreg,b.insns[i].dst_vreg,a.insns[i].src1_vreg,a.insns[i].src2_vreg,a.insns[i].src3_vreg,b.insns[i].src1_vreg,b.insns[i].src2_vreg,b.insns[i].src3_vreg,a.insns[i].imm,b.insns[i].imm,a.insns[i].ureg,b.insns[i].ureg);
54 break;
55 }
56 return 0;
57}
58static uint32_t corrupt(uint64_t *r,uint64_t k,uint64_t x,uint64_t y,uint64_t z,void *b0,uint64_t nb,void *m,uint64_t nm){
59 uint32_t rc=omega_cg_tern(r,k,x,y,z,b0,nb,m,nm);if(!rc&&!*r)((uint64_t*)b0)[6]^=1;return rc;
60}
61int main(int argc,char **argv){
62 if(argc==8){OmegaTernGemvSpec s={(uint32_t)strtoul(argv[1],0,0),(uint32_t)strtoul(argv[2],0,0),(uint32_t)strtoul(argv[3],0,0),(uint32_t)strtoul(argv[4],0,0),(uint32_t)strtoul(argv[5],0,0),(uint32_t)strtoul(argv[6],0,0),atoi(argv[7])};return equal(&s)?0:1;}
63 /* Exhaustive finite accepted domain, including all shipped mutation selectors. The spec guard is independently C. */
64 for(unsigned k=256;k<=OMEGA_TERN_MAX_K;k+=256)for(unsigned l=0;l<2;l++)for(unsigned r=1;r<=4;r*=2)for(unsigned w=1;w<=32;w++)for(unsigned d=1;d<=16;d++)for(unsigned e=0;e<2;e++)for(int m=0;m<=6;m++){
65 OmegaTernGemvSpec s={k,l,r,w,d,e,m};
66 if(oracle_spec_ok(&s)!=omega_tern_gemv_spec_ok(&s))return 1;
67 if(!oracle_spec_ok(&s))continue;
68 if(m==0 && e==0 && w==1 && d==1){
69 const uint32_t rr[]={0,1,2*r-1,2*r,UINT32_MAX};
70 const uint32_t cc[]={0,1,k/16-1,k/16,UINT32_MAX};
71 for(unsigned i=0;i<sizeof rr/sizeof rr[0];i++)for(unsigned j=0;j<sizeof cc/sizeof cc[0];j++)
72 if(oracle_offset(l,r,k,rr[i],cc[j])!=omega_tern_word_offset(l,r,k,rr[i],cc[j]))return 1;
73 }
74 if(oracle_chunks(&s)!=(uint32_t)omega_tern_gemv_chunks(&s)||oracle_steps(&s)!=omega_tern_gemv_steps(&s)||oracle_coalesced(&s)!=omega_tern_gemv_coalesced_out(&s)||oracle_slot(&s)!=omega_tern_gemv_out_slot_bytes(&s)||oracle_shared(&s)!=omega_tern_gemv_shared_bytes(&s)||!equal(&s))return 1;
75 }
76 const uint32_t edge[]={0,1,255,257,8193,16385,UINT32_MAX};
77 for(unsigned i=0;i<sizeof edge/sizeof edge[0];i++){OmegaTernGemvSpec s={edge[i],0,1,1,1,0,0};if(!equal(&s))return 1;}
78 const OmegaTernGemvSpec invalid[]={
79 {256,2,1,1,1,0,0},{256,0,0,1,1,0,0},{256,0,3,1,1,0,0},{256,0,8,1,1,0,0},
80 {256,0,1,0,1,0,0},{256,0,1,33,1,0,0},{256,0,1,1,0,0,0},{256,0,1,1,17,0,0},
81 {256,0,1,1,1,2,0},{256,0,1,1,1,0,-1},{256,0,1,1,1,0,7}
82 };
83 for(unsigned i=0;i<sizeof invalid/sizeof invalid[0];i++)
84 if(oracle_spec_ok(&invalid[i])||omega_tern_gemv_spec_ok(&invalid[i])||!equal(&invalid[i]))return 1;
85 OmegaTernGemvSpec s={256,0,1,1,1,0,0};
86 if(omega_blackwell_codegen_tern_gemv_prog(NULL,&b)!=-1||omega_blackwell_codegen_tern_gemv_prog(&s,NULL)!=-1)return 1;
87 if(oracle_prog(&s,&a)||omega_gpu_om_ir_replay(corrupt,256,UINT64_C(1)<<32,(UINT64_C(1)<<32)|1,0,&b,NULL))return 1;
88 if(a.count==b.count&&!memcmp(a.insns,b.insns,a.count*sizeof a.insns[0])){fprintf(stderr,"record negative control escaped\n");return 1;}
89 puts("CODEGEN TERN RECORD NEGATIVE CONTROL CAUGHT");
90 printf("CODEGEN TERN DIFF PASS (%u programs, %u refusals)\n",cases,refused);return 0;
91}