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_conv2d_ref.h

37 lines · 1914 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2#ifndef GPU_CONV2D_REF_H
3#define GPU_CONV2D_REF_H
4#include <math.h>
5#include <stdint.h>
6#include <string.h>
7/* Independent numeric decoding; no emitted IR, seeded plan, or simulator math. */
8static double cv_value(const void *p,size_t i,unsigned t) {
9 if(!t) return ((const float*)p)[i];
10 uint16_t u=((const uint16_t*)p)[i];
11 if(t==1) {uint32_t x=(uint32_t)u<<16;float f;memcpy(&f,&x,4);return f;}
12 unsigned e=u>>10&31,m=u&1023; double x=e?ldexp(1.0+(double)m/1024,(int)e-15):ldexp((double)m,-24);
13 return u&32768?-x:x;
14}
15static size_t cv_index(unsigned layout,size_t n,size_t c,size_t y,size_t x,size_t C,size_t H,size_t W) {
16 return layout?((n*H+y)*W+x)*C+c:((n*C+c)*H+y)*W+x;
17}
18static void cv_reference(const OmegaGpuConv2dShape *s,const void *a,const void *b,double *out,double *mag,int wrong) {
19 size_t oh=(s->height+s->pad_top+s->pad_bottom-s->dilation_h*(s->kernel_h-1)-1)/s->stride_h+1;
20 size_t ow=(s->width+s->pad_left+s->pad_right-s->dilation_w*(s->kernel_w-1)-1)/s->stride_w+1;
21 size_t ci=s->in_channels/s->groups,co=s->out_channels/s->groups;
22 for(size_t n=0;n<s->batch;n++) for(size_t o=0;o<s->out_channels;o++)
23 for(size_t y=0;y<oh;y++) for(size_t x=0;x<ow;x++) {
24 double sum=0,absum=0;
25 for(size_t c=0;c<ci;c++) for(size_t ky=0;ky<s->kernel_h;ky++) for(size_t kx=0;kx<s->kernel_w;kx++) {
26 int64_t iy=(int64_t)(y*s->stride_h+ky*s->dilation_h)-s->pad_top;
27 int64_t ix=(int64_t)(x*s->stride_w+kx*s->dilation_w)-s->pad_left;
28 if(iy<0||ix<0||iy>=s->height||ix>=s->width) continue;
29 size_t ic=c+(wrong?0:o/co)*ci;
30 double product=cv_value(a,cv_index(s->layout,n,ic,iy,ix,s->in_channels,s->height,s->width),s->dtype)*
31 cv_value(b,((o*ci+c)*s->kernel_h+ky)*s->kernel_w+kx,s->dtype);
32 sum+=product;absum+=fabs(product);
33 }
34 size_t dst=cv_index(s->layout,n,o,y,x,s->out_channels,oh,ow);out[dst]=sum;mag[dst]=absum;
35 }
36}
37#endif