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_prefill.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* omega_gpu_prefill.c: device-resident prefill buffers, kernel cache and launch helper (OWN-30B T3b). See the header. */ |
| 3 | #include "omega_gpu_prefill.h" |
| 4 | #include "omega_blackwell_codegen_pf.h" |
| 5 | #include "omega_blackwell_codegen_tc.h" |
| 6 | #include "omega_blackwell_codegen_qmma.h" |
| 7 | #include "omega_gpu_platform.h" |
| 8 | #include "omega_gpu_fmt.h" |
| 9 | #include "omega_gpu_cmdlist.h" |
| 10 | #include "omega_gpu_graph_om.h" |
| 11 | #include "omega_gpu_graph_residue.h" |
| 12 | #include <string.h> |
| 13 | |
| 14 | #define PF_SLOTS 64 |
| 15 | #define PF_STAGE_BYTES (64ull << 20) |
| 16 | #define PF_TIMEOUT_MS 20000ull |
| 17 | #define PF_SPIN_US 200000u /* spin on the marker: prefill launches are back to back, a sleep-poll costs ~100 us each */ |
| 18 | |
| 19 | typedef struct { bool valid; OmegaPfKey key; OmegaPfKernel k; OmegaGpuMem code; } Slot; |
| 20 | static struct { |
| 21 | bool hooked; |
| 22 | Slot slot[PF_SLOTS]; |
| 23 | OmegaGpuScratch stage; |
| 24 | uint64_t chip_ns, launches; |
| 25 | } g; |
| 26 | |
| 27 | const char *omega_gpu_pf_rc_name(int rc) { |
| 28 | switch (rc) { |
| 29 | case OMEGA_GPU_PF_OK: return "OK"; |
| 30 | case OMEGA_GPU_PF_BAD_ARGS: return "BAD_ARGS"; |
| 31 | case OMEGA_GPU_PF_TOO_LARGE: return "TOO_LARGE"; |
| 32 | case OMEGA_GPU_PF_CODEGEN_FAIL: return "CODEGEN_FAIL"; |
| 33 | case OMEGA_GPU_PF_CHIP_FAIL: return "CHIP_FAIL"; |
| 34 | default: return "UNKNOWN"; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | static void on_close(void) { |
| 39 | for (unsigned i = 0; i < PF_SLOTS; i++) if (g.slot[i].valid) { omega_gpu_session_free(&g.slot[i].code); memset(&g.slot[i], 0, sizeof g.slot[i]); } |
| 40 | omega_gpu_session_scratch_free(&g.stage); |
| 41 | } |
| 42 | int omega_gpu_pf_open_locked(void) { |
| 43 | if (!g.hooked) { if (omega_gpu_session_on_close(on_close) != 0) { omega_gpu_session_set_error("prefill: no session hook slot left"); return OMEGA_GPU_PF_CHIP_FAIL; } g.hooked = true; } |
| 44 | return omega_gpu_session_open() ? OMEGA_GPU_PF_OK : OMEGA_GPU_PF_CHIP_FAIL; |
| 45 | } |
| 46 | |
| 47 | void omega_gpu_pf_stats(uint64_t *chip_ns, uint64_t *launches) { if (chip_ns) *chip_ns = g.chip_ns; if (launches) *launches = g.launches; } |
| 48 | void omega_gpu_pf_stats_reset(void) { g.chip_ns = 0; g.launches = 0; } |
| 49 | |
| 50 | int omega_gpu_pf_alloc_dev_locked(size_t bytes, OmegaGpuMem *out) { |
| 51 | if (!out || bytes == 0) return OMEGA_GPU_PF_BAD_ARGS; |
| 52 | memset(out, 0, sizeof *out); |
| 53 | int rc = omega_gpu_pf_open_locked(); |
| 54 | if (rc == OMEGA_GPU_PF_OK) { |
| 55 | OmegaGpuSession *s = omega_gpu_session_open(); |
| 56 | const OmegaGpuPlatform *p = omega_gpu_session_platform(); |
| 57 | #if CUDAPP_MIGRATION_PENDING_GRAPH_RESIDUE |
| 58 | const size_t want = (bytes + 0xfffu) & ~(size_t)0xfffu; |
| 59 | #else |
| 60 | const uint64_t rcfg[16] = {bytes}; uint64_t rout[16]; |
| 61 | const size_t want = omega_graph_meta(4, rcfg, rout); |
| 62 | #endif |
| 63 | OmegaGpuBufferDesc d = { .kind = OMEGA_GPU_HANDLE_BUF_DATA, .rights = OMEGA_GPU_RIGHT_GPU_READ | OMEGA_GPU_RIGHT_GPU_WRITE, |
| 64 | .gpu_cache = OMEGA_GPU_CACHE_GPU_CACHED, .align = 0x1000u, .bytes = want }; |
| 65 | OmegaCpuView cpu; OmegaGpuVa va; OmegaGpuBusAddr bus; |
| 66 | int prc = (s && p) ? p->dma_alloc(p->ctx, s->dev, &d, &out->handle, &cpu, &va, &bus) : OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 67 | if (prc != OMEGA_GPU_PLAT_OK || !cpu.ptr || cpu.len < want) { |
| 68 | if (prc == OMEGA_GPU_PLAT_OK) (void)p->dma_free(p->ctx, out->handle); |
| 69 | char e[96]; omega_gpu_fmt(e, sizeof e, "prefill: cached alloc of %zu bytes refused (%d)", want, prc); |
| 70 | omega_gpu_session_set_error(e); memset(out, 0, sizeof *out); rc = OMEGA_GPU_PF_CHIP_FAIL; |
| 71 | } else { out->kind = OMEGA_GPU_HANDLE_BUF_DATA; out->cpu = cpu.ptr; out->va = va.va; out->size = cpu.len; } |
| 72 | } |
| 73 | return rc; |
| 74 | } |
| 75 | int omega_gpu_pf_alloc_dev(size_t bytes, OmegaGpuMem *out) { |
| 76 | omega_gpu_session_lock(); |
| 77 | int rc = omega_gpu_pf_alloc_dev_locked(bytes, out); |
| 78 | omega_gpu_session_unlock(); |
| 79 | return rc; |
| 80 | } |
| 81 | int omega_gpu_pf_alloc_host(size_t bytes, OmegaGpuMem *out) { |
| 82 | if (!out || bytes == 0) return OMEGA_GPU_PF_BAD_ARGS; |
| 83 | omega_gpu_session_lock(); |
| 84 | int rc = omega_gpu_pf_open_locked(); |
| 85 | if (rc == OMEGA_GPU_PF_OK && omega_gpu_session_alloc(bytes, out) != 0) rc = OMEGA_GPU_PF_CHIP_FAIL; |
| 86 | omega_gpu_session_unlock(); |
| 87 | return rc; |
| 88 | } |
| 89 | void omega_gpu_pf_free(OmegaGpuMem *m) { omega_gpu_session_lock(); omega_gpu_session_free(m); omega_gpu_session_unlock(); } |
| 90 | |
| 91 | int omega_gpu_pf_kernel_locked(const OmegaPfKey *key, OmegaPfGen gen, OmegaPfKernel *out) { |
| 92 | for (unsigned i = 0; i < PF_SLOTS; i++) if (g.slot[i].valid && !memcmp(&g.slot[i].key, key, sizeof *key)) { *out = g.slot[i].k; return OMEGA_GPU_PF_OK; } |
| 93 | Slot *s = NULL; |
| 94 | for (unsigned i = 0; i < PF_SLOTS && !s; i++) if (!g.slot[i].valid) s = &g.slot[i]; |
| 95 | if (!s) { omega_gpu_session_set_error("prefill kernel cache full"); return OMEGA_GPU_PF_TOO_LARGE; } |
| 96 | BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); |
| 97 | if (!prog) { omega_gpu_session_set_error("prefill codegen: host memory"); return OMEGA_GPU_PF_CODEGEN_FAIL; } |
| 98 | int cg = gen(key, prog); |
| 99 | size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0; |
| 100 | if (cg != 0 || need == 0) { omega_gpu_host_free(prog); omega_gpu_session_set_error("prefill codegen refused the shape"); return OMEGA_GPU_PF_CODEGEN_FAIL; } |
| 101 | const uint32_t peak = prog->regalloc.peak_gpr_usage, decl = omega_bw_gpr_declared(peak); |
| 102 | /* Preserve the old 128-register declaration unless codegen explicitly opted |
| 103 | * into more. The allocator's hardware limit leaves declaration headroom; |
| 104 | * launch/record below independently enforce registers times threads. |
| 105 | * Blackwell Tuning Guide, physical page 13: 255/thread, 64K per SM. */ |
| 106 | if (!omega_graph_budget(peak, prog->regalloc.gpr_limit, BW_PHYS_GPR_MAX, BW_PHYS_GPR_HW_LIMIT, |
| 107 | omega_bw_gpr_declared_ok(decl, peak))) { |
| 108 | omega_gpu_host_free(prog); omega_gpu_session_set_error("prefill kernel: register budget exceeded"); return OMEGA_GPU_PF_TOO_LARGE; |
| 109 | } |
| 110 | OmegaGpuMem code; |
| 111 | if (omega_gpu_session_alloc_code(need, &code) != 0) { omega_gpu_host_free(prog); return OMEGA_GPU_PF_CHIP_FAIL; } |
| 112 | OmegaBlackwellKernel kernel; |
| 113 | cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel); |
| 114 | if (cg == 0 && omega_qmma_patch_code(prog, code.cpu, need) < 0) cg = -1; |
| 115 | omega_gpu_host_free(prog); |
| 116 | if (cg != 0 || kernel.code_size != need) { omega_gpu_session_free(&code); omega_gpu_session_set_error("prefill encode failed"); return OMEGA_GPU_PF_CODEGEN_FAIL; } |
| 117 | if (omega_gpu_session_sync_to_gpu(&code) != 0) { omega_gpu_session_free(&code); return OMEGA_GPU_PF_CHIP_FAIL; } |
| 118 | memset(s, 0, sizeof *s); |
| 119 | s->valid = true; s->key = *key; s->code = code; |
| 120 | s->k.code_va = code.va; s->k.gpr_peak = kernel.gpr_count; s->k.gpr_declared = omega_bw_gpr_declared(kernel.gpr_count); |
| 121 | *out = s->k; |
| 122 | return OMEGA_GPU_PF_OK; |
| 123 | } |
| 124 | |
| 125 | #ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS |
| 126 | static int pf_launch_plan(const OmegaPfKernel *k, uint32_t threads, uint32_t gx, uint32_t gy, |
| 127 | uint32_t shared, const uint32_t *args, uint32_t nargs, OmegaGpuLaunch *launch, |
| 128 | const char *budget, const char *registers) { |
| 129 | if (!k) return OMEGA_GPU_PF_BAD_ARGS; |
| 130 | const uint64_t q[16]={k->code_va,k->gpr_declared,threads,gx,gy,shared,nargs,k->gpr_peak}; |
| 131 | uint64_t d[16],result=0; |
| 132 | if(omega_mg_lauch(&result,q,16,d,16)!=OMEGA_OK)return OMEGA_GPU_PF_CHIP_FAIL; |
| 133 | if(result==0)return OMEGA_GPU_PF_BAD_ARGS; |
| 134 | if(result==2){omega_gpu_session_set_error(budget);return OMEGA_GPU_PF_TOO_LARGE;} |
| 135 | if(result==3){omega_gpu_session_set_error(registers);return OMEGA_GPU_PF_CHIP_FAIL;} |
| 136 | *launch=(OmegaGpuLaunch){.code_va=d[0],.gpr_count=d[1],.threads_x=d[2],.threads_y=1, |
| 137 | .grid_x=d[3],.grid_y=d[4],.shared_bytes=d[5],.n_args=d[6],.num_elements=d[8], |
| 138 | .timeout_ms=d[9],.spin_us=d[10],.args=args}; |
| 139 | return OMEGA_GPU_PF_OK; |
| 140 | } |
| 141 | #endif |
| 142 | int omega_gpu_pf_launch_locked(const OmegaPfKernel *k, uint32_t threads, uint32_t grid_x, uint32_t grid_y, uint32_t shared_bytes, |
| 143 | const uint32_t *args, uint32_t n_args, uint64_t *ns) { |
| 144 | #ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS |
| 145 | OmegaGpuLaunch L; |
| 146 | int rc=pf_launch_plan(k,threads,grid_x,grid_y,shared_bytes,args,n_args,&L, |
| 147 | "prefill launch: registers x threads exceed the file","declared registers below peak + 2: refusing to launch"); |
| 148 | if(rc!=OMEGA_GPU_PF_OK)return rc; |
| 149 | #else |
| 150 | if (!k || !threads || !grid_x || !grid_y) return OMEGA_GPU_PF_BAD_ARGS; |
| 151 | if ((uint64_t)threads * k->gpr_declared > 65536u) { omega_gpu_session_set_error("prefill launch: registers x threads exceed the file"); return OMEGA_GPU_PF_TOO_LARGE; } |
| 152 | if (!omega_bw_gpr_declared_ok(k->gpr_declared, k->gpr_peak)) { omega_gpu_session_set_error("declared registers below peak + 2: refusing to launch"); return OMEGA_GPU_PF_CHIP_FAIL; } |
| 153 | OmegaGpuLaunch L = { .code_va = k->code_va, .gpr_count = k->gpr_declared, .threads_x = threads, .threads_y = 1, .grid_x = grid_x, .grid_y = grid_y, |
| 154 | .num_elements = (size_t)threads * grid_x * grid_y, .args = args, .n_args = n_args, .timeout_ms = PF_TIMEOUT_MS, |
| 155 | .shared_bytes = shared_bytes, .spin_us = PF_SPIN_US }; |
| 156 | #endif |
| 157 | uint64_t t = 0; |
| 158 | if (omega_gpu_session_launch(&L, &t, NULL) != 0) return OMEGA_GPU_PF_CHIP_FAIL; |
| 159 | g.chip_ns += t; g.launches++; |
| 160 | if (ns) *ns += t; |
| 161 | return OMEGA_GPU_PF_OK; |
| 162 | } |
| 163 | |
| 164 | int omega_gpu_pf_record_locked(OmegaGpuCmdList *cl, const OmegaPfKernel *k, uint32_t threads, uint32_t grid_x, uint32_t grid_y, |
| 165 | uint32_t shared_bytes, const uint32_t *args, uint32_t n_args) { |
| 166 | #ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS |
| 167 | if(!cl)return OMEGA_GPU_PF_BAD_ARGS; |
| 168 | OmegaGpuLaunch L; |
| 169 | int rc=pf_launch_plan(k,threads,grid_x,grid_y,shared_bytes,args,n_args,&L, |
| 170 | "prefill record: registers x threads exceed the file","declared registers below peak + 2: refusing to record"); |
| 171 | if(rc!=OMEGA_GPU_PF_OK)return rc; |
| 172 | #else |
| 173 | if (!cl || !k || !threads || !grid_x || !grid_y) return OMEGA_GPU_PF_BAD_ARGS; |
| 174 | if ((uint64_t)threads * k->gpr_declared > 65536u) { omega_gpu_session_set_error("prefill record: registers x threads exceed the file"); return OMEGA_GPU_PF_TOO_LARGE; } |
| 175 | if (!omega_bw_gpr_declared_ok(k->gpr_declared, k->gpr_peak)) { omega_gpu_session_set_error("declared registers below peak + 2: refusing to record"); return OMEGA_GPU_PF_CHIP_FAIL; } |
| 176 | OmegaGpuLaunch L = { .code_va = k->code_va, .gpr_count = k->gpr_declared, .threads_x = threads, .threads_y = 1, .grid_x = grid_x, .grid_y = grid_y, |
| 177 | .num_elements = (size_t)threads * grid_x * grid_y, .args = args, .n_args = n_args, .timeout_ms = PF_TIMEOUT_MS, |
| 178 | .shared_bytes = shared_bytes, .spin_us = PF_SPIN_US }; |
| 179 | #endif |
| 180 | return omega_gpu_cmdlist_add(cl, &L) < 0 ? OMEGA_GPU_PF_CHIP_FAIL : OMEGA_GPU_PF_OK; |
| 181 | } |
| 182 | |
| 183 | static int gen_copy(const OmegaPfKey *key, void *prog) { (void)key; return omega_blackwell_codegen_pf_copy_prog(prog); } |
| 184 | #define KIND_COPY 1u |
| 185 | static int copy_locked(uint64_t dst, uint64_t src, size_t bytes) { |
| 186 | #ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS |
| 187 | uint64_t q[16]={dst,src,bytes,0,1ull<<30,OMEGA_PF_COPY_THREADS},plan[16],result=0; |
| 188 | if(omega_mg_copy(&result,q,16,plan,16)!=OMEGA_OK||result==0)return OMEGA_GPU_PF_BAD_ARGS; |
| 189 | if(result==2)return OMEGA_GPU_PF_OK; |
| 190 | #else |
| 191 | if ((bytes & 15u) || (dst & 15u) || (src & 15u)) return OMEGA_GPU_PF_BAD_ARGS; |
| 192 | if (bytes == 0) return OMEGA_GPU_PF_OK; |
| 193 | #endif |
| 194 | OmegaPfKey key; memset(&key, 0, sizeof key); key.kind = KIND_COPY; |
| 195 | OmegaPfKernel k; int rc = omega_gpu_pf_kernel_locked(&key, gen_copy, &k); |
| 196 | for (size_t done = 0; rc == OMEGA_GPU_PF_OK && done < bytes;) { |
| 197 | #ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS |
| 198 | q[3]=done; |
| 199 | if(omega_mg_copy(&result,q,16,plan,16)!=OMEGA_OK||result!=1)return OMEGA_GPU_PF_CHIP_FAIL; |
| 200 | const size_t part=plan[2]; |
| 201 | const uint32_t units=plan[3],grid=plan[4]; |
| 202 | const uint64_t d=plan[0],s=plan[1]; |
| 203 | #else |
| 204 | size_t part = bytes - done; if (part > (1ull << 30)) part = 1ull << 30; |
| 205 | const uint32_t units = (uint32_t)(part / 16u), grid = (units + OMEGA_PF_COPY_THREADS - 1u) / OMEGA_PF_COPY_THREADS; |
| 206 | const uint64_t d = dst + done, s = src + done; |
| 207 | #endif |
| 208 | uint32_t args[5] = { (uint32_t)d, (uint32_t)(d >> 32), (uint32_t)s, (uint32_t)(s >> 32), units }; |
| 209 | rc = omega_gpu_pf_launch_locked(&k, OMEGA_PF_COPY_THREADS, grid, 1, 0, args, 5, NULL); |
| 210 | done += part; |
| 211 | } |
| 212 | return rc; |
| 213 | } |
| 214 | int omega_gpu_pf_copy_va_locked(uint64_t dst_va, uint64_t src_va, size_t bytes, uint64_t *chip_ns) { |
| 215 | const uint64_t before = g.chip_ns; |
| 216 | int rc = omega_gpu_pf_open_locked(); |
| 217 | if (rc == OMEGA_GPU_PF_OK) rc = copy_locked(dst_va, src_va, bytes); |
| 218 | if (chip_ns) *chip_ns = g.chip_ns - before; |
| 219 | return rc; |
| 220 | } |
| 221 | int omega_gpu_pf_copy_va(uint64_t dst_va, uint64_t src_va, size_t bytes) { |
| 222 | omega_gpu_session_lock(); |
| 223 | int rc = omega_gpu_pf_copy_va_locked(dst_va, src_va, bytes, NULL); |
| 224 | omega_gpu_session_unlock(); |
| 225 | return rc; |
| 226 | } |
| 227 | |
| 228 | static int xfer(const OmegaGpuMem *dev, size_t off, void *host, size_t bytes, int up) { |
| 229 | #ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS |
| 230 | if(!dev||!host)return OMEGA_GPU_PF_BAD_ARGS; |
| 231 | uint64_t q[16]={dev->va,off,bytes,dev->size,0,0,up!=0},plan[16],result=0; |
| 232 | if(omega_mg_xfer(&result,q,16,plan,16)!=OMEGA_OK||result==0)return OMEGA_GPU_PF_BAD_ARGS; |
| 233 | #else |
| 234 | if (!dev || !host || (off & 15u) || (bytes & 15u) || off + bytes > dev->size) return OMEGA_GPU_PF_BAD_ARGS; |
| 235 | #endif |
| 236 | omega_gpu_session_lock(); |
| 237 | int rc = omega_gpu_pf_open_locked(); |
| 238 | if (rc == OMEGA_GPU_PF_OK && omega_gpu_session_scratch(&g.stage, PF_STAGE_BYTES) != 0) rc = OMEGA_GPU_PF_CHIP_FAIL; |
| 239 | for (size_t done = 0; rc == OMEGA_GPU_PF_OK && done < bytes;) { |
| 240 | #ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS |
| 241 | q[4]=done;q[5]=g.stage.mem.va; |
| 242 | if(omega_mg_xfer(&result,q,16,plan,16)!=OMEGA_OK||result!=1){rc=OMEGA_GPU_PF_CHIP_FAIL;break;} |
| 243 | const size_t part=plan[2]; |
| 244 | const uint64_t dst=plan[0],src=plan[1]; |
| 245 | #else |
| 246 | size_t part = bytes - done; if (part > PF_STAGE_BYTES) part = PF_STAGE_BYTES; |
| 247 | const uint64_t dst=up?dev->va+off+done:g.stage.mem.va,src=up?g.stage.mem.va:dev->va+off+done; |
| 248 | #endif |
| 249 | if (up) { |
| 250 | memcpy(g.stage.mem.cpu, (const uint8_t *)host + done, part); |
| 251 | if (omega_gpu_session_sync_to_gpu(&g.stage.mem) != 0) rc = OMEGA_GPU_PF_CHIP_FAIL; |
| 252 | else rc = copy_locked(dst, src, part); |
| 253 | } else { |
| 254 | rc = copy_locked(dst, src, part); |
| 255 | if (rc == OMEGA_GPU_PF_OK && omega_gpu_session_sync_to_cpu(&g.stage.mem) != 0) rc = OMEGA_GPU_PF_CHIP_FAIL; |
| 256 | if (rc == OMEGA_GPU_PF_OK) memcpy((uint8_t *)host + done, g.stage.mem.cpu, part); |
| 257 | } |
| 258 | done += part; |
| 259 | } |
| 260 | omega_gpu_session_unlock(); |
| 261 | return rc; |
| 262 | } |
| 263 | int omega_gpu_pf_upload(const OmegaGpuMem *dst, size_t off, const void *src, size_t bytes) { return xfer(dst, off, (void *)src, bytes, 1); } |
| 264 | int omega_gpu_pf_download(const OmegaGpuMem *src, size_t off, void *dst, size_t bytes) { return xfer(src, off, dst, bytes, 0); } |