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 / src / omega_gpu_dnn_activations_api.c

52 lines · 3192 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* Memory/session shell. Admission, launch geometry and all arithmetic are .om. */
3#include "omega_gpu_dnn_activations_api.h"
4#include "omega_gpu_om_ir.h"
5#include "omega_gpu_session.h"
6#include "cudapp_native.h"
7#include <stdlib.h>
8#include <string.h>
9static int plan(const OmegaGpuDnnActivationsShape *s,uint64_t p[8]) {
10 uint64_t result=0;
11 if(omega_da_plan(&result,s->elements,s->operation,s->dtype,p,8)) return OMEGA_GPU_DNN_ACTIVATIONS_CODEGEN;
12 return result ? OMEGA_GPU_DNN_ACTIVATIONS_REFUSED : OMEGA_GPU_DNN_ACTIVATIONS_OK;
13}
14int omega_gpu_dnn_activations_codegen(const OmegaGpuDnnActivationsShape *s,BlackwellIRProgram *program,uint8_t *code,size_t capacity,OmegaBlackwellKernel *kernel) {
15 uint64_t p[8];
16 if(!s||!program||!code||!kernel) return OMEGA_GPU_DNN_ACTIVATIONS_BAD_ARGS;
17 memset(kernel,0,sizeof *kernel);
18 int rc=plan(s,p);if(rc) return rc;
19 if(omega_gpu_om_ir_replay(omega_dnn_activations,s->operation,s->dtype,0,0,program,NULL) ||
20 omega_blackwell_kernel_encode_into(program,code,capacity,kernel)) return OMEGA_GPU_DNN_ACTIVATIONS_CODEGEN;
21 return OMEGA_GPU_DNN_ACTIVATIONS_OK;
22}
23static void putptr(uint32_t *p,uint64_t v) {p[0]=(uint32_t)v;p[1]=(uint32_t)(v>>32);}
24int omega_gpu_dnn_activations(const OmegaGpuDnnActivationsShape *s,const void *input,float *output) {
25 uint64_t p[8];
26 if(!s||!input||!output) return OMEGA_GPU_DNN_ACTIVATIONS_BAD_ARGS;
27 int rc=plan(s,p);if(rc) return rc; /* Refusal must precede session/device access. */
28 BlackwellIRProgram *program=malloc(sizeof *program);uint8_t *code=malloc(65536);
29 OmegaBlackwellKernel kernel={0};
30 if(!program||!code) {free(program);free(code);return OMEGA_GPU_DNN_ACTIVATIONS_CODEGEN;}
31 rc=omega_gpu_dnn_activations_codegen(s,program,code,65536,&kernel);
32 if(rc) {free(program);free(code);return rc;}
33 OmegaGpuMem buffers[3]={0};
34 omega_gpu_session_lock();
35 if(!omega_gpu_session_open() || omega_gpu_session_alloc(p[0],&buffers[0]) ||
36 omega_gpu_session_alloc(p[1],&buffers[1]) || omega_gpu_session_alloc_code(kernel.code_size,&buffers[2])) {
37 rc=OMEGA_GPU_DNN_ACTIVATIONS_CHIP;goto cleanup;
38 }
39 memcpy(buffers[0].cpu,input,p[0]);memset(buffers[1].cpu,0,p[1]);memcpy(buffers[2].cpu,code,kernel.code_size);
40 for(unsigned i=0;i<3;++i) if(omega_gpu_session_sync_to_gpu(&buffers[i])) {rc=OMEGA_GPU_DNN_ACTIVATIONS_CHIP;goto cleanup;}
41 uint32_t args[5];putptr(args,buffers[0].va);putptr(args+2,buffers[1].va);args[4]=(uint32_t)s->elements;
42 OmegaGpuLaunch launch={.code_va=buffers[2].va,.gpr_count=omega_bw_gpr_declared(program->regalloc.peak_gpr_usage),
43 .threads_x=(uint32_t)p[3],.threads_y=1,.grid_x=(uint32_t)p[2],.grid_y=1,.num_elements=s->elements,
44 .args=args,.n_args=5,.timeout_ms=30000};
45 if(omega_gpu_session_launch(&launch,NULL,NULL) || omega_gpu_session_sync_to_cpu(&buffers[1])) {
46 rc=OMEGA_GPU_DNN_ACTIVATIONS_CHIP;goto cleanup;
47 }
48 memcpy(output,buffers[1].cpu,p[1]);
49cleanup:
50 for(unsigned i=0;i<3;++i) omega_gpu_session_free(&buffers[i]);
51 omega_gpu_session_unlock();free(program);free(code);return rc;
52}