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 / tests / ampere / math_device.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* Device-only verifier: full binary32 input sweep for the four emitted MUFU functions. |
| 3 | * Independent host oracle: system long-double libm, not another CUDA++ kernel. |
| 4 | * Bounds: NVIDIA PTX ISA 9.4 sections 9.7.3.13,16,20,21. This checks intended |
| 5 | * non-FTZ PTX-level numerical contracts; raw MUFU equivalence is NOT yet qualified. |
| 6 | * No GPU is opened without the explicit --device flag. Shards are never exhaustive PASS. */ |
| 7 | #include "omega_ampere_codegen.h" |
| 8 | #include "omega_gpu_session.h" |
| 9 | #include <errno.h> |
| 10 | #include <inttypes.h> |
| 11 | #include <math.h> |
| 12 | #include <stdint.h> |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | #define BATCH 65536u |
| 18 | #define NOREG (-1) |
| 19 | #define FULL_DOMAIN UINT64_C(0x100000000) |
| 20 | #define CTRL(stall,wbar,rbar,wait) \ |
| 21 | (((uint32_t)(stall)<<9)|((uint32_t)(wbar)<<14)|((uint32_t)(rbar)<<17)|((uint32_t)(wait)<<20)) |
| 22 | #define ALU CTRL(15,7,7,0) |
| 23 | #define LOAD_CTRL (CTRL(4,0,7,0)|0x2000u) |
| 24 | static uint32_t bits(float x) { uint32_t u; memcpy(&u,&x,sizeof u); return u; } |
| 25 | static float number(uint32_t u) { float x; memcpy(&x,&u,sizeof x); return x; } |
| 26 | static int append(BlackwellIRProgram *p, BlackwellIROpcode op, int d, int a, int b, int c, uint32_t imm, uint32_t control, int uniform, int pred) { |
| 27 | BlackwellIRInsn i = {0}; |
| 28 | i.op=op; i.dst_vreg=d; i.src1_vreg=a; i.src2_vreg=b; i.src3_vreg=c; i.ureg=NOREG; |
| 29 | i.imm=imm; i.control=control; i.is_uniform=uniform; i.predicate_p0=pred; |
| 30 | return omega_bw_ir_append(p,&i)<0 ? -1 : 0; |
| 31 | } |
| 32 | static int kernel(BlackwellIRProgram *p, BlackwellIROpcode math) { |
| 33 | omega_bw_ir_init(p); |
| 34 | int src=omega_bw_ir_alloc_vreg64(p), dst=omega_bw_ir_alloc_vreg64(p); |
| 35 | int tid=omega_bw_ir_alloc_vreg(p), cta=omega_bw_ir_alloc_vreg(p), n=omega_bw_ir_alloc_vreg(p); |
| 36 | int i=omega_bw_ir_alloc_vreg(p), address=omega_bw_ir_alloc_vreg64(p); |
| 37 | int x=omega_bw_ir_alloc_vreg(p), y=omega_bw_ir_alloc_vreg(p); |
| 38 | /* Conservative scheduling: no dependent instruction is issued before its load barrier. |
| 39 | * Each producer allocates barrier zero; the next instruction waits on barrier zero. */ |
| 40 | uint32_t pending=0; |
| 41 | #define EMIT(OP,D,A,B,C,IMM,BASE,PRED) do { if(append(p,OP,D,A,B,C,IMM,(BASE)|(pending<<20),0,PRED))return -1; pending=0; } while(0) |
| 42 | #define LOAD(OP,D,A,IMM) do { EMIT(OP,D,A,NOREG,NOREG,IMM,LOAD_CTRL,0); pending=1u; } while(0) |
| 43 | LOAD(BW_IR_LDC64,src,NOREG,0x380); |
| 44 | LOAD(BW_IR_LDC64,dst,NOREG,0x388); |
| 45 | LOAD(BW_IR_LDC,n,NOREG,0x390); |
| 46 | LOAD(BW_IR_S2R,tid,NOREG,BW_SR_TID_X); |
| 47 | LOAD(BW_IR_S2R,cta,NOREG,BW_SR_CTAID_X); |
| 48 | EMIT(BW_IR_IMAD,i,cta,NOREG,tid,256,ALU,0); |
| 49 | EMIT(BW_IR_ISETP_GE_U32,NOREG,i,n,NOREG,0,ALU,0); |
| 50 | EMIT(BW_IR_EXIT,NOREG,NOREG,NOREG,NOREG,0,ALU,1); |
| 51 | EMIT(BW_IR_IMAD_WIDE,address,i,NOREG,src,4,ALU,0); |
| 52 | LOAD(BW_IR_LDG_E,x,address,0); |
| 53 | /* MUFU completes through a scoreboard, not a fixed arithmetic delay. */ |
| 54 | EMIT(math,y,x,NOREG,NOREG,0,LOAD_CTRL,0); pending=1u; |
| 55 | EMIT(BW_IR_IMAD_WIDE,address,i,NOREG,dst,4,ALU,0); |
| 56 | EMIT(BW_IR_STG_E,NOREG,address,y,NOREG,0,CTRL(4,7,1,0)|0x2000u,0); pending=2u; |
| 57 | EMIT(BW_IR_EXIT,NOREG,NOREG,NOREG,NOREG,0,ALU,0); |
| 58 | EMIT(BW_IR_BRA,NOREG,NOREG,NOREG,NOREG,0,0x000fc000u,0); |
| 59 | #undef EMIT |
| 60 | #undef LOAD |
| 61 | return omega_bw_regalloc_solve(p); |
| 62 | } |
| 63 | static int agrees(unsigned op,uint32_t input,uint32_t output) { |
| 64 | float x=number(input), got=number(output); |
| 65 | long double ref; |
| 66 | switch(op) { |
| 67 | case 0: ref=1.0L/(long double)x; break; |
| 68 | case 1: ref=1.0L/sqrtl((long double)x); break; |
| 69 | case 2: ref=exp2l((long double)x); break; |
| 70 | default: ref=log2l((long double)x); break; |
| 71 | } |
| 72 | if (isnan(ref)) return isnan(got); |
| 73 | float rounded=(float)ref; |
| 74 | if (isinf(rounded)||rounded==0.0f) return output==bits(rounded); |
| 75 | if (!isfinite(got)) return 0; |
| 76 | if (op==0||op==2) { |
| 77 | uint32_t want=bits(rounded); |
| 78 | if ((output>>31)!=(want>>31)) return 0; |
| 79 | uint32_t distance=output>want?output-want:want-output; |
| 80 | return distance <= (op==0?1u:2u); |
| 81 | } |
| 82 | long double error=fabsl((long double)got-ref); |
| 83 | if(op==1) return error<=fabsl(ref)*exp2l(-22.9L); |
| 84 | if(x>0.5f && x<2.0f) return error<=0x1p-22L; |
| 85 | return error<=fabsl(ref)*0x1p-22L; |
| 86 | } |
| 87 | static int parse_u64(const char *s,uint64_t *v) { |
| 88 | char *end; errno=0; *v=strtoull(s,&end,0); |
| 89 | return errno||!s[0]||s[0]=='-'||*end ? -1:0; |
| 90 | } |
| 91 | int main(int argc,char **argv) { |
| 92 | uint64_t start=0,count=FULL_DOMAIN; int device=0,inject=0,rc=1,emit=0,selftest=0; |
| 93 | for(int i=1;i<argc;i++) { |
| 94 | if(!strcmp(argv[i],"--device")) device=1; |
| 95 | else if(!strcmp(argv[i],"--inject-output-error")) inject=1; |
| 96 | else if(!strcmp(argv[i],"--emit-only")) emit=1; |
| 97 | else if(!strcmp(argv[i],"--self-test")) selftest=1; |
| 98 | else if(!strcmp(argv[i],"--start")&&i+1<argc) { if(parse_u64(argv[++i],&start))return 64; } |
| 99 | else if(!strcmp(argv[i],"--count")&&i+1<argc) { if(parse_u64(argv[++i],&count))return 64; } |
| 100 | else return 64; |
| 101 | } |
| 102 | if(start>=FULL_DOMAIN||!count||count>FULL_DOMAIN-start)return 64; |
| 103 | static const BlackwellIROpcode ops[]={BW_IR_MUFU_RCP,BW_IR_MUFU_RSQ,BW_IR_MUFU_EX2,BW_IR_MUFU_LG2}; |
| 104 | static const char *names[]={"rcp","rsqrt","exp2","log2"}; |
| 105 | if (selftest) { |
| 106 | for(unsigned op=0;op<4;op++) { |
| 107 | uint32_t correct=op==3?0xff800000u:op==2?0x3f800000u:0x7f800000u; |
| 108 | if(!agrees(op,0,correct)||agrees(op,0,correct^0x80000000u))return 1; |
| 109 | if(!agrees(op,0x7fc00000u,0x7fc12345u)||agrees(op,0x7fc00000u,0x3f800000u))return 1; |
| 110 | } |
| 111 | puts("AMPERE MATH HOST ORACLE CONTROLS PASS; DEVICE NOT_RUN");return 0; |
| 112 | } |
| 113 | if (emit) { |
| 114 | for(unsigned op=0;op<4;op++) { |
| 115 | BlackwellIRProgram program; uint8_t code[16384];size_t written=0; |
| 116 | if(kernel(&program,ops[op])||omega_bw_encode_program(&program,code,sizeof code,&written)|| |
| 117 | omega_gpu_encode_program_target(&program,86,code,sizeof code,&written))return 1; |
| 118 | } |
| 119 | puts("AMPERE MATH KERNEL EMISSION PASS; DEVICE NOT_RUN");return 0; |
| 120 | } |
| 121 | if(!device) { puts("AMPERE MATH DEVICE NOT_RUN (explicit --device required)");return 2; } |
| 122 | if(inject&&(start!=0||count!=BATCH))return 64; |
| 123 | OmegaGpuMem in={0},out={0},code={0}; |
| 124 | omega_gpu_session_lock(); |
| 125 | if(!omega_gpu_session_open()||omega_gpu_session_target_sm()!=86) { |
| 126 | fprintf(stderr,"AMPERE MATH NOT_RUN: target/open: %s\n",omega_gpu_session_last_error());rc=2;goto done; |
| 127 | } |
| 128 | if(omega_gpu_session_alloc(BATCH*sizeof(uint32_t),&in)||omega_gpu_session_alloc(BATCH*sizeof(uint32_t),&out))goto done; |
| 129 | for(unsigned op=0;op<sizeof ops/sizeof ops[0];op++) { |
| 130 | BlackwellIRProgram program; |
| 131 | if(kernel(&program,ops[op]))goto done; |
| 132 | size_t capacity=omega_bw_program_code_bytes(&program),written=0; |
| 133 | if(omega_gpu_session_alloc_code(capacity,&code))goto done; |
| 134 | if(omega_gpu_encode_program_target(&program,86,code.cpu,capacity,&written)||written!=capacity||omega_gpu_session_sync_to_gpu(&code))goto done; |
| 135 | for(uint64_t offset=0;offset<count;) { |
| 136 | uint32_t n=count-offset>BATCH?BATCH:(uint32_t)(count-offset); |
| 137 | for(uint32_t j=0;j<n;j++) ((uint32_t *)in.cpu)[j]=(uint32_t)(start+offset+j); |
| 138 | memset(out.cpu,0xa5,BATCH*sizeof(uint32_t)); |
| 139 | if(omega_gpu_session_sync_to_gpu(&in)||omega_gpu_session_sync_to_gpu(&out))goto done; |
| 140 | uint32_t args[32]={(uint32_t)in.va,(uint32_t)(in.va>>32),(uint32_t)out.va,(uint32_t)(out.va>>32),n}; |
| 141 | OmegaGpuLaunch launch={.code_va=code.va,.gpr_count=64,.threads_x=256,.threads_y=1,.grid_x=(n+255)/256,.grid_y=1, |
| 142 | .num_elements=n,.args=args,.n_args=32,.timeout_ms=10000,.target_sm=86}; |
| 143 | if(omega_gpu_session_launch(&launch,NULL,NULL)||omega_gpu_session_sync_to_cpu(&out))goto done; |
| 144 | if(inject&&offset==0)((uint32_t *)out.cpu)[0]^=0x80000000u; |
| 145 | for(uint32_t j=0;j<n;j++)if(!agrees(op,((uint32_t *)in.cpu)[j],((uint32_t *)out.cpu)[j])) { |
| 146 | fprintf(stderr,"AMPERE MATH FAIL op=%s input=%08"PRIx32" output=%08"PRIx32"\n",names[op],((uint32_t *)in.cpu)[j],((uint32_t *)out.cpu)[j]);goto done; |
| 147 | } |
| 148 | offset+=n; |
| 149 | } |
| 150 | printf("AMPERE MATH %s %s start=%"PRIu64" count=%"PRIu64"\n",names[op],count==FULL_DOMAIN?"EXHAUSTIVE PASS":"SHARD PASS",start,count); |
| 151 | omega_gpu_session_free(&code); memset(&code,0,sizeof code); |
| 152 | } |
| 153 | rc=0; |
| 154 | done: |
| 155 | if(code.cpu)omega_gpu_session_free(&code); |
| 156 | if(out.cpu)omega_gpu_session_free(&out); |
| 157 | if(in.cpu)omega_gpu_session_free(&in); |
| 158 | if(rc==1)fprintf(stderr,"AMPERE MATH FAIL stage=%s\n",omega_gpu_session_last_error()); |
| 159 | omega_gpu_session_unlock();omega_gpu_session_close();return rc; |
| 160 | } |