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 / src / omega_gpu_conv2d_api.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* Memory/session shell. Geometry, grouping, addressing, arithmetic and launch |
| 3 | * decisions are checked seeded Omega. No host convolution or im2col. */ |
| 4 | #include "omega_gpu_conv2d_api.h" |
| 5 | #include "omega_gpu_om_ir.h" |
| 6 | #include "omega_gpu_session.h" |
| 7 | #include "cudapp_native.h" |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | static void words(const OmegaGpuConv2dShape *s, uint64_t w[18]) { |
| 11 | const uint32_t v[18]={s->batch,s->in_channels,s->height,s->width,s->out_channels, |
| 12 | s->kernel_h,s->kernel_w,s->stride_h,s->stride_w,s->pad_top,s->pad_bottom, |
| 13 | s->pad_left,s->pad_right,s->dilation_h,s->dilation_w,s->groups,s->layout,s->dtype}; |
| 14 | for(unsigned j=0;j<18;j++) w[j]=v[j]; |
| 15 | } |
| 16 | int omega_gpu_conv2d_plan(const OmegaGpuConv2dShape *s,uint64_t ix,uint64_t p[32]) { |
| 17 | uint64_t w[18],r; |
| 18 | if(!s||!p) return OMEGA_GPU_CONV2D_BAD_ARGS; |
| 19 | words(s,w); |
| 20 | if(omega_conv_plan(&r,w,18,ix,p,32)) return OMEGA_GPU_CONV2D_CODEGEN; |
| 21 | return r?OMEGA_GPU_CONV2D_REFUSED:0; |
| 22 | } |
| 23 | /* Replay's synchronous callback only needs a view of the shape words. TLS keeps |
| 24 | * simultaneous callers independent; restoring the previous view permits nesting. */ |
| 25 | static _Thread_local const uint64_t *active_shape; |
| 26 | static uint32_t unit(uint64_t *result,uint64_t k,uint64_t a,uint64_t b,uint64_t c, |
| 27 | void *ins,uint64_t ni,void *meta,uint64_t nm) { |
| 28 | (void)k;(void)a;(void)b;(void)c; |
| 29 | return omega_conv_ir(result,active_shape,18,ins,ni,meta,nm); |
| 30 | } |
| 31 | int omega_gpu_conv2d_codegen(const OmegaGpuConv2dShape *s,BlackwellIRProgram *program, |
| 32 | uint8_t *code,size_t capacity,OmegaBlackwellKernel *kernel) { |
| 33 | uint64_t p[32],w[18]; |
| 34 | if(!s||!program||!code||!kernel) return OMEGA_GPU_CONV2D_BAD_ARGS; |
| 35 | memset(kernel,0,sizeof *kernel); |
| 36 | int rc=omega_gpu_conv2d_plan(s,0,p); if(rc) return rc; |
| 37 | words(s,w); |
| 38 | const uint64_t *prior=active_shape; active_shape=w; |
| 39 | rc=omega_gpu_om_ir_replay(unit,0,0,0,0,program,NULL); |
| 40 | active_shape=prior; |
| 41 | if(rc||omega_blackwell_kernel_encode_into(program,code,capacity,kernel)) return OMEGA_GPU_CONV2D_CODEGEN; |
| 42 | return 0; |
| 43 | } |
| 44 | static void ptr(uint32_t *a,uint64_t p) {a[0]=(uint32_t)p;a[1]=(uint32_t)(p>>32);} |
| 45 | int omega_gpu_conv2d(const OmegaGpuConv2dShape *s,const void *image,const void *filter,float *out) { |
| 46 | uint64_t p[32]; |
| 47 | if(!s||!image||!filter||!out) return OMEGA_GPU_CONV2D_BAD_ARGS; |
| 48 | int rc=omega_gpu_conv2d_plan(s,0,p); if(rc) return rc; |
| 49 | BlackwellIRProgram *program=malloc(sizeof *program); uint8_t *code=malloc(65536); |
| 50 | OmegaBlackwellKernel kernel={0}; OmegaGpuMem mem[4]; memset(mem,0,sizeof mem); |
| 51 | if(!program||!code) {free(program);free(code);return OMEGA_GPU_CONV2D_CODEGEN;} |
| 52 | rc=omega_gpu_conv2d_codegen(s,program,code,65536,&kernel); |
| 53 | if(rc) {free(program);free(code);return rc;} |
| 54 | omega_gpu_session_lock(); |
| 55 | if(!omega_gpu_session_open()||omega_gpu_session_alloc(p[2],mem)||omega_gpu_session_alloc(p[3],mem+1)|| |
| 56 | omega_gpu_session_alloc(p[4],mem+2)||omega_gpu_session_alloc_code(kernel.code_size,mem+3)) { |
| 57 | rc=OMEGA_GPU_CONV2D_CHIP;goto done; |
| 58 | } |
| 59 | memcpy(mem[0].cpu,image,p[2]);memcpy(mem[1].cpu,filter,p[3]);memset(mem[2].cpu,0,p[4]); |
| 60 | memcpy(mem[3].cpu,code,kernel.code_size); |
| 61 | for(unsigned j=0;j<4;j++) if(omega_gpu_session_sync_to_gpu(mem+j)) {rc=OMEGA_GPU_CONV2D_CHIP;goto done;} |
| 62 | for(uint64_t ix=0;ix<p[5];ix++) { |
| 63 | rc=omega_gpu_conv2d_plan(s,ix,p);if(rc) goto done; |
| 64 | uint32_t args[7];ptr(args,mem[0].va+p[8]);ptr(args+2,mem[1].va+p[9]);ptr(args+4,mem[2].va+p[10]);args[6]=(uint32_t)p[11]; |
| 65 | OmegaGpuLaunch launch={.code_va=mem[3].va,.gpr_count=omega_bw_gpr_declared(program->regalloc.peak_gpr_usage), |
| 66 | .threads_x=(uint32_t)p[7],.threads_y=1,.grid_x=(uint32_t)p[6],.grid_y=1,.num_elements=p[1], |
| 67 | .args=args,.n_args=7,.timeout_ms=30000}; |
| 68 | if(omega_gpu_session_launch(&launch,NULL,NULL)) {rc=OMEGA_GPU_CONV2D_CHIP;goto done;} |
| 69 | } |
| 70 | if(omega_gpu_session_sync_to_cpu(mem+2)) {rc=OMEGA_GPU_CONV2D_CHIP;goto done;} |
| 71 | memcpy(out,mem[2].cpu,p[4]); |
| 72 | done: |
| 73 | for(unsigned j=0;j<4;j++) omega_gpu_session_free(mem+j); |
| 74 | omega_gpu_session_unlock();free(program);free(code);return rc; |
| 75 | } |