omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / omega_gpu_prefill.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* omega_gpu_prefill.c: device-resident prefill buffers, kernel cache and launch helper (OWN-30B T3b). See the header. */ |
| 18 | #include "omega_gpu_prefill.h" |
| 19 | #include "omega_blackwell_codegen_pf.h" |
| 20 | #include "omega_blackwell_codegen_tc.h" |
| 21 | #include "omega_blackwell_codegen_qmma.h" |
| 22 | #include "omega_gpu_platform.h" |
| 23 | #include "omega_gpu_fmt.h" |
| 24 | #include "omega_gpu_cmdlist.h" |
| 25 | #include <string.h> |
| 26 | |
| 27 | #define PF_SLOTS 64 |
| 28 | #define PF_STAGE_BYTES (64ull << 20) |
| 29 | #define PF_TIMEOUT_MS 20000ull |
| 30 | #define PF_SPIN_US 200000u /* spin on the marker: prefill launches are back to back, a sleep-poll costs ~100 us each */ |
| 31 | |
| 32 | typedef struct { bool valid; OmegaPfKey key; OmegaPfKernel k; OmegaGpuMem code; } Slot; |
| 33 | static struct { |
| 34 | bool hooked; |
| 35 | Slot slot[PF_SLOTS]; |
| 36 | OmegaGpuScratch stage; |
| 37 | uint64_t chip_ns, launches; |
| 38 | } g; |
| 39 | |
| 40 | const char *omega_gpu_pf_rc_name(int rc) { |
| 41 | switch (rc) { |
| 42 | case OMEGA_GPU_PF_OK: return "OK"; |
| 43 | case OMEGA_GPU_PF_BAD_ARGS: return "BAD_ARGS"; |
| 44 | case OMEGA_GPU_PF_TOO_LARGE: return "TOO_LARGE"; |
| 45 | case OMEGA_GPU_PF_CODEGEN_FAIL: return "CODEGEN_FAIL"; |
| 46 | case OMEGA_GPU_PF_CHIP_FAIL: return "CHIP_FAIL"; |
| 47 | default: return "UNKNOWN"; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static void on_close(void) { |
| 52 | 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]); } |
| 53 | omega_gpu_session_scratch_free(&g.stage); |
| 54 | } |
| 55 | int omega_gpu_pf_open_locked(void) { |
| 56 | 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; } |
| 57 | return omega_gpu_session_open() ? OMEGA_GPU_PF_OK : OMEGA_GPU_PF_CHIP_FAIL; |
| 58 | } |
| 59 | |
| 60 | 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; } |
| 61 | void omega_gpu_pf_stats_reset(void) { g.chip_ns = 0; g.launches = 0; } |
| 62 | |
| 63 | int omega_gpu_pf_alloc_dev(size_t bytes, OmegaGpuMem *out) { |
| 64 | if (!out || bytes == 0) return OMEGA_GPU_PF_BAD_ARGS; |
| 65 | memset(out, 0, sizeof *out); |
| 66 | omega_gpu_session_lock(); |
| 67 | int rc = omega_gpu_pf_open_locked(); |
| 68 | if (rc == OMEGA_GPU_PF_OK) { |
| 69 | OmegaGpuSession *s = omega_gpu_session_open(); |
| 70 | const OmegaGpuPlatform *p = omega_gpu_session_platform(); |
| 71 | const size_t want = (bytes + 0xfffu) & ~(size_t)0xfffu; |
| 72 | OmegaGpuBufferDesc d = { .kind = OMEGA_GPU_HANDLE_BUF_DATA, .rights = OMEGA_GPU_RIGHT_GPU_READ | OMEGA_GPU_RIGHT_GPU_WRITE, |
| 73 | .gpu_cache = OMEGA_GPU_CACHE_GPU_CACHED, .align = 0x1000u, .bytes = want }; |
| 74 | OmegaCpuView cpu; OmegaGpuVa va; OmegaGpuBusAddr bus; |
| 75 | int prc = (s && p) ? p->dma_alloc(p->ctx, s->dev, &d, &out->handle, &cpu, &va, &bus) : OMEGA_GPU_PLAT_REFUSED_ARGS; |
| 76 | if (prc != OMEGA_GPU_PLAT_OK || !cpu.ptr || cpu.len < want) { |
| 77 | if (prc == OMEGA_GPU_PLAT_OK) (void)p->dma_free(p->ctx, out->handle); |
| 78 | char e[96]; omega_gpu_fmt(e, sizeof e, "prefill: cached alloc of %zu bytes refused (%d)", want, prc); |
| 79 | omega_gpu_session_set_error(e); memset(out, 0, sizeof *out); rc = OMEGA_GPU_PF_CHIP_FAIL; |
| 80 | } else { out->kind = OMEGA_GPU_HANDLE_BUF_DATA; out->cpu = cpu.ptr; out->va = va.va; out->size = cpu.len; } |
| 81 | } |
| 82 | omega_gpu_session_unlock(); |
| 83 | return rc; |
| 84 | } |
| 85 | int omega_gpu_pf_alloc_host(size_t bytes, OmegaGpuMem *out) { |
| 86 | if (!out || bytes == 0) return OMEGA_GPU_PF_BAD_ARGS; |
| 87 | omega_gpu_session_lock(); |
| 88 | int rc = omega_gpu_pf_open_locked(); |
| 89 | if (rc == OMEGA_GPU_PF_OK && omega_gpu_session_alloc(bytes, out) != 0) rc = OMEGA_GPU_PF_CHIP_FAIL; |
| 90 | omega_gpu_session_unlock(); |
| 91 | return rc; |
| 92 | } |
| 93 | void omega_gpu_pf_free(OmegaGpuMem *m) { omega_gpu_session_lock(); omega_gpu_session_free(m); omega_gpu_session_unlock(); } |
| 94 | |
| 95 | int omega_gpu_pf_kernel_locked(const OmegaPfKey *key, OmegaPfGen gen, OmegaPfKernel *out) { |
| 96 | 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; } |
| 97 | Slot *s = NULL; |
| 98 | for (unsigned i = 0; i < PF_SLOTS && !s; i++) if (!g.slot[i].valid) s = &g.slot[i]; |
| 99 | if (!s) { omega_gpu_session_set_error("prefill kernel cache full"); return OMEGA_GPU_PF_TOO_LARGE; } |
| 100 | BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); |
| 101 | if (!prog) { omega_gpu_session_set_error("prefill codegen: host memory"); return OMEGA_GPU_PF_CODEGEN_FAIL; } |
| 102 | int cg = gen(key, prog); |
| 103 | size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0; |
| 104 | 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; } |
| 105 | const uint32_t peak = prog->regalloc.peak_gpr_usage, decl = omega_bw_gpr_declared(peak); |
| 106 | if (decl > 128u) { omega_gpu_host_free(prog); omega_gpu_session_set_error("prefill kernel: more than 128 registers"); return OMEGA_GPU_PF_TOO_LARGE; } |
| 107 | OmegaGpuMem code; |
| 108 | if (omega_gpu_session_alloc_code(need, &code) != 0) { omega_gpu_host_free(prog); return OMEGA_GPU_PF_CHIP_FAIL; } |
| 109 | OmegaBlackwellKernel kernel; |
| 110 | cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel); |
| 111 | if (cg == 0 && omega_qmma_patch_code(prog, code.cpu, need) < 0) cg = -1; |
| 112 | omega_gpu_host_free(prog); |
| 113 | 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; } |
| 114 | if (omega_gpu_session_sync_to_gpu(&code) != 0) { omega_gpu_session_free(&code); return OMEGA_GPU_PF_CHIP_FAIL; } |
| 115 | memset(s, 0, sizeof *s); |
| 116 | s->valid = true; s->key = *key; s->code = code; |
| 117 | s->k.code_va = code.va; s->k.gpr_peak = kernel.gpr_count; s->k.gpr_declared = omega_bw_gpr_declared(kernel.gpr_count); |
| 118 | *out = s->k; |
| 119 | return OMEGA_GPU_PF_OK; |
| 120 | } |
| 121 | |
| 122 | int omega_gpu_pf_launch_locked(const OmegaPfKernel *k, uint32_t threads, uint32_t grid_x, uint32_t grid_y, uint32_t shared_bytes, |
| 123 | const uint32_t *args, uint32_t n_args, uint64_t *ns) { |
| 124 | if (!k || !threads || !grid_x || !grid_y) return OMEGA_GPU_PF_BAD_ARGS; |
| 125 | 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; } |
| 126 | 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; } |
| 127 | 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, |
| 128 | .num_elements = (size_t)threads * grid_x * grid_y, .args = args, .n_args = n_args, .timeout_ms = PF_TIMEOUT_MS, |
| 129 | .shared_bytes = shared_bytes, .spin_us = PF_SPIN_US }; |
| 130 | uint64_t t = 0; |
| 131 | if (omega_gpu_session_launch(&L, &t, NULL) != 0) return OMEGA_GPU_PF_CHIP_FAIL; |
| 132 | g.chip_ns += t; g.launches++; |
| 133 | if (ns) *ns += t; |
| 134 | return OMEGA_GPU_PF_OK; |
| 135 | } |
| 136 | |
| 137 | int omega_gpu_pf_record_locked(OmegaGpuCmdList *cl, const OmegaPfKernel *k, uint32_t threads, uint32_t grid_x, uint32_t grid_y, |
| 138 | uint32_t shared_bytes, const uint32_t *args, uint32_t n_args) { |
| 139 | if (!cl || !k || !threads || !grid_x || !grid_y) return OMEGA_GPU_PF_BAD_ARGS; |
| 140 | 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; } |
| 141 | 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; } |
| 142 | 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, |
| 143 | .num_elements = (size_t)threads * grid_x * grid_y, .args = args, .n_args = n_args, .timeout_ms = PF_TIMEOUT_MS, |
| 144 | .shared_bytes = shared_bytes, .spin_us = PF_SPIN_US }; |
| 145 | return omega_gpu_cmdlist_add(cl, &L) < 0 ? OMEGA_GPU_PF_CHIP_FAIL : OMEGA_GPU_PF_OK; |
| 146 | } |
| 147 | |
| 148 | static int gen_copy(const OmegaPfKey *key, void *prog) { (void)key; return omega_blackwell_codegen_pf_copy_prog(prog); } |
| 149 | #define KIND_COPY 1u |
| 150 | static int copy_locked(uint64_t dst, uint64_t src, size_t bytes) { |
| 151 | if ((bytes & 15u) || (dst & 15u) || (src & 15u)) return OMEGA_GPU_PF_BAD_ARGS; |
| 152 | if (bytes == 0) return OMEGA_GPU_PF_OK; |
| 153 | OmegaPfKey key; memset(&key, 0, sizeof key); key.kind = KIND_COPY; |
| 154 | OmegaPfKernel k; int rc = omega_gpu_pf_kernel_locked(&key, gen_copy, &k); |
| 155 | for (size_t done = 0; rc == OMEGA_GPU_PF_OK && done < bytes;) { |
| 156 | size_t part = bytes - done; if (part > (1ull << 30)) part = 1ull << 30; |
| 157 | const uint32_t units = (uint32_t)(part / 16u), grid = (units + OMEGA_PF_COPY_THREADS - 1u) / OMEGA_PF_COPY_THREADS; |
| 158 | const uint64_t d = dst + done, s = src + done; |
| 159 | uint32_t args[5] = { (uint32_t)d, (uint32_t)(d >> 32), (uint32_t)s, (uint32_t)(s >> 32), units }; |
| 160 | rc = omega_gpu_pf_launch_locked(&k, OMEGA_PF_COPY_THREADS, grid, 1, 0, args, 5, NULL); |
| 161 | done += part; |
| 162 | } |
| 163 | return rc; |
| 164 | } |
| 165 | int omega_gpu_pf_copy_va(uint64_t dst_va, uint64_t src_va, size_t bytes) { |
| 166 | omega_gpu_session_lock(); |
| 167 | int rc = omega_gpu_pf_open_locked(); |
| 168 | if (rc == OMEGA_GPU_PF_OK) rc = copy_locked(dst_va, src_va, bytes); |
| 169 | omega_gpu_session_unlock(); |
| 170 | return rc; |
| 171 | } |
| 172 | |
| 173 | static int xfer(const OmegaGpuMem *dev, size_t off, void *host, size_t bytes, int up) { |
| 174 | if (!dev || !host || (off & 15u) || (bytes & 15u) || off + bytes > dev->size) return OMEGA_GPU_PF_BAD_ARGS; |
| 175 | omega_gpu_session_lock(); |
| 176 | int rc = omega_gpu_pf_open_locked(); |
| 177 | if (rc == OMEGA_GPU_PF_OK && omega_gpu_session_scratch(&g.stage, PF_STAGE_BYTES) != 0) rc = OMEGA_GPU_PF_CHIP_FAIL; |
| 178 | for (size_t done = 0; rc == OMEGA_GPU_PF_OK && done < bytes;) { |
| 179 | size_t part = bytes - done; if (part > PF_STAGE_BYTES) part = PF_STAGE_BYTES; |
| 180 | if (up) { |
| 181 | memcpy(g.stage.mem.cpu, (const uint8_t *)host + done, part); |
| 182 | if (omega_gpu_session_sync_to_gpu(&g.stage.mem) != 0) rc = OMEGA_GPU_PF_CHIP_FAIL; |
| 183 | else rc = copy_locked(dev->va + off + done, g.stage.mem.va, part); |
| 184 | } else { |
| 185 | rc = copy_locked(g.stage.mem.va, dev->va + off + done, part); |
| 186 | if (rc == OMEGA_GPU_PF_OK && omega_gpu_session_sync_to_cpu(&g.stage.mem) != 0) rc = OMEGA_GPU_PF_CHIP_FAIL; |
| 187 | if (rc == OMEGA_GPU_PF_OK) memcpy((uint8_t *)host + done, g.stage.mem.cpu, part); |
| 188 | } |
| 189 | done += part; |
| 190 | } |
| 191 | omega_gpu_session_unlock(); |
| 192 | return rc; |
| 193 | } |
| 194 | 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); } |
| 195 | int omega_gpu_pf_download(const OmegaGpuMem *src, size_t off, void *dst, size_t bytes) { return xfer(src, off, dst, bytes, 0); } |