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 / gpu_dnn_rows_ref.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #ifndef GPU_DNN_ROWS_REF_H |
| 3 | #define GPU_DNN_ROWS_REF_H |
| 4 | #include <math.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | static double dr_input(const void *p, size_t i, unsigned dtype) { |
| 8 | if (dtype == 0) return ((const float*)p)[i]; |
| 9 | uint16_t h=((const uint16_t*)p)[i]; |
| 10 | if (dtype == 1) { uint32_t b=(uint32_t)h<<16; float f; memcpy(&f,&b,4); return f; } |
| 11 | _Float16 f; memcpy(&f,&h,2); return (double)f; |
| 12 | } |
| 13 | static void dr_reference(const OmegaGpuDnnRowsShape *s,const void *p,double *o) { |
| 14 | for (unsigned r=0;r<s->rows;++r) { |
| 15 | size_t base=(size_t)r*s->length; |
| 16 | double mean=0, v=0, mx=-INFINITY; |
| 17 | for (unsigned j=0;j<s->length;++j) { double x=dr_input(p,base+j,s->dtype); mean+=x; if(x>mx) mx=x; } |
| 18 | mean/=s->length; |
| 19 | for (unsigned j=0;j<s->length;++j) { |
| 20 | double x=dr_input(p,base+j,s->dtype); |
| 21 | if(s->op==0) v+=exp(x-mx); |
| 22 | else { double d=s->op==1?x-mean:x; v+=d*d; } |
| 23 | } |
| 24 | if(s->op) v=sqrt(v/s->length+(double)s->epsilon); |
| 25 | for(unsigned j=0;j<s->length;++j) { double x=dr_input(p,base+j,s->dtype); o[base+j]=s->op==0?exp(x-mx)/v:(s->op==1?x-mean:x)/v; } |
| 26 | } |
| 27 | } |
| 28 | static int dr_equal(const float *o,const double *r,size_t n) { |
| 29 | /* MODELED 16 gamma_2053 envelope; README records Wolfram derivation. */ |
| 30 | const double tol=16.0*(2053.0/16775163.0); |
| 31 | for(size_t i=0;i<n;++i) if(!isfinite(o[i]) || fabs((double)o[i]-r[i])>tol*(1+fabs(r[i]))) return 0; |
| 32 | return 1; |
| 33 | } |
| 34 | #endif |