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_attn_any_api.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* Memory shell only. Shape, grouping, row ownership, tails and all arithmetic |
| 3 | * decisions are seeded Omega in om/attn-any; generic replay is shared. */ |
| 4 | #include "omega_gpu_attn_any_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 | |
| 11 | static int plan(const OmegaGpuAttnAnyShape *s, uint64_t head, uint64_t words[8], uint64_t p[16]) { |
| 12 | uint32_t bits; |
| 13 | uint64_t result = 0; |
| 14 | memcpy(&bits, &s->scale, sizeof bits); |
| 15 | words[0] = s->seq_q; words[1] = s->seq_kv; words[2] = s->head_dim; |
| 16 | words[3] = s->q_heads; words[4] = s->kv_heads; words[5] = s->causal; |
| 17 | words[6] = s->dtype; words[7] = bits; |
| 18 | if (omega_aa_plan(&result, words, 8, head, p, 16) != 0) return OMEGA_GPU_ATTN_ANY_CODEGEN; |
| 19 | return result ? OMEGA_GPU_ATTN_ANY_REFUSED : OMEGA_GPU_ATTN_ANY_OK; |
| 20 | } |
| 21 | int omega_gpu_attn_any_codegen(const OmegaGpuAttnAnyShape *s, BlackwellIRProgram *program, uint8_t *code, size_t capacity, OmegaBlackwellKernel *kernel) { |
| 22 | uint64_t words[8], p[16]; |
| 23 | if (!s || !program || !code || !kernel) return OMEGA_GPU_ATTN_ANY_BAD_ARGS; |
| 24 | memset(kernel, 0, sizeof *kernel); |
| 25 | int rc = plan(s, 0, words, p); |
| 26 | if (rc) return rc; |
| 27 | rc = omega_gpu_om_ir_replay(omega_attn_any, 0, words[2], p[9], 0, program, NULL); |
| 28 | if (rc || omega_blackwell_kernel_encode_into(program, code, capacity, kernel)) return OMEGA_GPU_ATTN_ANY_CODEGEN; |
| 29 | return OMEGA_GPU_ATTN_ANY_OK; |
| 30 | } |
| 31 | static void putptr(uint32_t *args, uint64_t ptr) { args[0] = (uint32_t)ptr; args[1] = (uint32_t)(ptr >> 32); } |
| 32 | int omega_gpu_attn_any(const OmegaGpuAttnAnyShape *s, const void *q, const void *k, const void *v, float *out) { |
| 33 | uint64_t words[8], p[16]; |
| 34 | if (!s || !q || !k || !v || !out) return OMEGA_GPU_ATTN_ANY_BAD_ARGS; |
| 35 | int rc = plan(s, 0, words, p); |
| 36 | if (rc) return rc; /* No device access for a refused shape. */ |
| 37 | BlackwellIRProgram *program = malloc(sizeof *program); |
| 38 | uint8_t *code = malloc(65536); |
| 39 | OmegaBlackwellKernel kernel = {0}; |
| 40 | if (!program || !code) { free(code); free(program); return OMEGA_GPU_ATTN_ANY_CODEGEN; } |
| 41 | rc = omega_gpu_attn_any_codegen(s, program, code, 65536, &kernel); |
| 42 | if (rc) { free(code); free(program); return rc; } |
| 43 | OmegaGpuMem buffers[5] = {0}; |
| 44 | omega_gpu_session_lock(); |
| 45 | if (omega_gpu_session_open() == NULL || omega_gpu_session_alloc(p[0], &buffers[0]) != 0 || |
| 46 | omega_gpu_session_alloc(p[1], &buffers[1]) != 0 || omega_gpu_session_alloc(p[1], &buffers[2]) != 0 || |
| 47 | omega_gpu_session_alloc(p[2], &buffers[3]) != 0 || omega_gpu_session_alloc_code(kernel.code_size, &buffers[4]) != 0) { |
| 48 | rc = OMEGA_GPU_ATTN_ANY_CHIP; |
| 49 | goto cleanup; |
| 50 | } |
| 51 | memcpy(buffers[0].cpu, q, p[0]); memcpy(buffers[1].cpu, k, p[1]); memcpy(buffers[2].cpu, v, p[1]); |
| 52 | memset(buffers[3].cpu, 0, p[2]); memcpy(buffers[4].cpu, code, kernel.code_size); |
| 53 | for (unsigned i = 0; i < 5; i++) if (omega_gpu_session_sync_to_gpu(&buffers[i]) != 0) { rc = OMEGA_GPU_ATTN_ANY_CHIP; goto cleanup; } |
| 54 | for (uint64_t head = 0; head < words[3]; head++) { |
| 55 | rc = plan(s, head, words, p); |
| 56 | if (rc) goto cleanup; |
| 57 | uint32_t args[10]; |
| 58 | putptr(args, buffers[0].va + p[4]); putptr(args + 2, buffers[1].va + p[5]); |
| 59 | putptr(args + 4, buffers[2].va + p[5]); putptr(args + 6, buffers[3].va + p[6]); |
| 60 | args[8] = (uint32_t)words[1]; args[9] = (uint32_t)words[7]; |
| 61 | OmegaGpuLaunch launch = { .code_va = buffers[4].va, .gpr_count = omega_bw_gpr_declared(program->regalloc.peak_gpr_usage), |
| 62 | .threads_x = (uint32_t)p[8], .threads_y = 1, .grid_x = (uint32_t)p[7], .grid_y = 1, |
| 63 | .num_elements = p[7], .args = args, .n_args = 10, .timeout_ms = 30000 }; |
| 64 | if (omega_gpu_session_launch(&launch, NULL, NULL) != 0) { rc = OMEGA_GPU_ATTN_ANY_CHIP; goto cleanup; } |
| 65 | } |
| 66 | if (omega_gpu_session_sync_to_cpu(&buffers[3]) != 0) { rc = OMEGA_GPU_ATTN_ANY_CHIP; goto cleanup; } |
| 67 | memcpy(out, buffers[3].cpu, p[2]); |
| 68 | cleanup: |
| 69 | /* Session parks rather than releases buffers after uncertain completion. */ |
| 70 | for (unsigned i = 0; i < 5; i++) omega_gpu_session_free(&buffers[i]); |
| 71 | omega_gpu_session_unlock(); |
| 72 | free(code); free(program); |
| 73 | return rc; |
| 74 | } |