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_tcgemm_api.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_tcgemm_api.c: tensor-core FP8 grouped GEMM on the shared session (OWN-30B T3 cut 1). See the header. |
| 18 | * Submission path and buffer policy follow omega_gpu_fp8gemm_api.c (GPU-uncached buffers, one omega_gpu_session_launch). */ |
| 19 | #include "omega_gpu_tcgemm_api.h" |
| 20 | #include "omega_gpu_session.h" |
| 21 | #include "omega_gpu_fmt.h" |
| 22 | #include <string.h> |
| 23 | #include <stdlib.h> |
| 24 | |
| 25 | #define CACHE_SLOTS 8 |
| 26 | #define MARKER_TIMEOUT_MS 20000ull |
| 27 | #define POISON_F32 (-999.0f) |
| 28 | |
| 29 | typedef struct { bool valid, pinned; OmegaTcGemmSpec spec; OmegaBlackwellKernel kernel; OmegaGpuMem code; } Slot; |
| 30 | typedef struct { uint64_t w_va, s_va; uint32_t row0, cnt, pad[2]; } TileEntry; |
| 31 | _Static_assert(sizeof(TileEntry) == OMEGA_TCGEMM_TILE_BYTES, "tile entry layout"); |
| 32 | |
| 33 | static struct { |
| 34 | OmegaGpuScratch x_buf, y_buf, tile_buf, ridx_buf; |
| 35 | uint32_t rt, ct, warps; |
| 36 | int mutant; |
| 37 | bool hooked; |
| 38 | Slot cache[CACHE_SLOTS]; |
| 39 | unsigned next; |
| 40 | } g; |
| 41 | |
| 42 | const char *omega_gpu_tcgemm_last_error(void) { return omega_gpu_session_last_error(); } |
| 43 | const char *omega_gpu_tcgemm_rc_name(int rc) { |
| 44 | switch (rc) { |
| 45 | case OMEGA_GPU_TCGEMM_OK: return "OK"; |
| 46 | case OMEGA_GPU_TCGEMM_BAD_ARGS: return "BAD_ARGS"; |
| 47 | case OMEGA_GPU_TCGEMM_TOO_LARGE: return "TOO_LARGE"; |
| 48 | case OMEGA_GPU_TCGEMM_CODEGEN_FAIL: return "CODEGEN_FAIL"; |
| 49 | case OMEGA_GPU_TCGEMM_CHIP_FAIL: return "CHIP_FAIL"; |
| 50 | default: return "UNKNOWN"; |
| 51 | } |
| 52 | } |
| 53 | static void cache_clear_locked(void) { |
| 54 | for (unsigned i = 0; i < CACHE_SLOTS; i++) { if (!g.cache[i].valid) continue; omega_gpu_session_free(&g.cache[i].code); memset(&g.cache[i], 0, sizeof g.cache[i]); } |
| 55 | g.next = 0; |
| 56 | } |
| 57 | static void on_session_close(void) { |
| 58 | cache_clear_locked(); |
| 59 | omega_gpu_session_scratch_free(&g.x_buf); omega_gpu_session_scratch_free(&g.y_buf); |
| 60 | omega_gpu_session_scratch_free(&g.tile_buf); omega_gpu_session_scratch_free(&g.ridx_buf); |
| 61 | } |
| 62 | static OmegaGpuSession *dev_open_locked(void) { |
| 63 | if (!g.hooked) { (void)omega_gpu_session_on_close(on_session_close); g.hooked = true; } |
| 64 | return omega_gpu_session_open(); |
| 65 | } |
| 66 | void omega_gpu_tcgemm_cache_clear(void) { omega_gpu_session_lock(); cache_clear_locked(); omega_gpu_session_unlock(); } |
| 67 | void omega_gpu_tcgemm_set_shape(uint32_t rt, uint32_t ct, uint32_t w) { omega_gpu_session_lock(); g.rt = rt; g.ct = ct; g.warps = w; cache_clear_locked(); omega_gpu_session_unlock(); } |
| 68 | void omega_gpu_tcgemm_test_set_mutant(int m) { omega_gpu_session_lock(); g.mutant = m; cache_clear_locked(); omega_gpu_session_unlock(); } |
| 69 | |
| 70 | void omega_gpu_tcgemm_spec_for(uint32_t k, uint32_t n, OmegaTcGemmSpec *spec) { |
| 71 | memset(spec, 0, sizeof *spec); |
| 72 | spec->k = k; |
| 73 | /* 64 rows x 128 columns per CTA, 8 warps (peak 124 registers, declared 128); 64 columns when n is not a multiple of 128 */ |
| 74 | spec->rt = g.rt ? g.rt : 4u; spec->ct = g.ct ? g.ct : (n % 128u == 0 ? 2u : 1u); spec->warps = g.warps ? g.warps : 8u; |
| 75 | spec->mutant = g.mutant; |
| 76 | } |
| 77 | |
| 78 | static int kernel_for(const OmegaTcGemmSpec *spec, Slot **out, bool *hit) { |
| 79 | for (unsigned i = 0; i < CACHE_SLOTS; i++) { Slot *s = &g.cache[i]; if (s->valid && memcmp(&s->spec, spec, sizeof *spec) == 0) { *out = s; *hit = true; return OMEGA_GPU_TCGEMM_OK; } } |
| 80 | Slot *s = NULL; |
| 81 | for (unsigned i = 0; i < CACHE_SLOTS && !s; i++) { Slot *c = &g.cache[(g.next + i) % CACHE_SLOTS]; if (!c->valid || !c->pinned) { s = c; g.next = (unsigned)((c - g.cache) + 1) % CACHE_SLOTS; } } |
| 82 | if (!s) { omega_gpu_session_set_error("tc gemm kernel cache: every slot holds a prepared kernel"); return OMEGA_GPU_TCGEMM_TOO_LARGE; } |
| 83 | BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); |
| 84 | if (!prog) { omega_gpu_session_set_error("tc gemm codegen: host memory"); return OMEGA_GPU_TCGEMM_CODEGEN_FAIL; } |
| 85 | int cg = omega_blackwell_codegen_tcgemm_prog(spec, prog); |
| 86 | size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0; |
| 87 | if (cg != 0 || need == 0) { omega_gpu_host_free(prog); omega_gpu_session_set_error("tc gemm codegen refused the shape"); return OMEGA_GPU_TCGEMM_CODEGEN_FAIL; } |
| 88 | const uint32_t decl = omega_bw_gpr_declared(prog->regalloc.peak_gpr_usage); |
| 89 | if (decl > 128u || (uint64_t)32u * spec->warps * decl > 65536u) { |
| 90 | char e[160]; omega_gpu_fmt(e, sizeof e, "tc gemm kernel: %u warps x %u declared registers do not fit", spec->warps, decl); |
| 91 | omega_gpu_session_set_error(e); omega_gpu_host_free(prog); return OMEGA_GPU_TCGEMM_TOO_LARGE; |
| 92 | } |
| 93 | OmegaGpuMem code; |
| 94 | if (omega_gpu_session_alloc_code(need, &code) != 0) { omega_gpu_host_free(prog); return OMEGA_GPU_TCGEMM_CHIP_FAIL; } |
| 95 | OmegaBlackwellKernel kernel; |
| 96 | cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel); |
| 97 | omega_gpu_host_free(prog); |
| 98 | if (cg != 0 || !kernel.code || kernel.code_size != need) { omega_gpu_session_free(&code); return OMEGA_GPU_TCGEMM_CODEGEN_FAIL; } |
| 99 | if (omega_gpu_session_sync_to_gpu(&code) != 0) { omega_gpu_session_free(&code); return OMEGA_GPU_TCGEMM_CHIP_FAIL; } |
| 100 | if (s->valid) omega_gpu_session_free(&s->code); |
| 101 | memset(s, 0, sizeof *s); |
| 102 | s->valid = true; s->spec = *spec; s->kernel = kernel; s->code = code; |
| 103 | *out = s; *hit = false; |
| 104 | return OMEGA_GPU_TCGEMM_OK; |
| 105 | } |
| 106 | |
| 107 | int omega_gpu_tcgemm_prepare(uint32_t k) { |
| 108 | OmegaTcGemmSpec sp; omega_gpu_tcgemm_spec_for(k, 128u, &sp); |
| 109 | omega_gpu_session_lock(); |
| 110 | int rc = OMEGA_GPU_TCGEMM_OK; Slot *s = NULL; bool hit = false; |
| 111 | if (!dev_open_locked()) rc = OMEGA_GPU_TCGEMM_CHIP_FAIL; |
| 112 | else if ((rc = kernel_for(&sp, &s, &hit)) == OMEGA_GPU_TCGEMM_OK) s->pinned = true; |
| 113 | omega_gpu_session_unlock(); |
| 114 | return rc; |
| 115 | } |
| 116 | |
| 117 | int omega_gpu_tcgemm_f32(const OmegaGpuFp8Tensor *t, uint32_t M, const uint32_t *ids, const uint32_t *trow, const float *x, uint32_t x_rows, float *y, OmegaGpuTcGemmInfo *info) { |
| 118 | uint64_t t0 = omega_gpu_session_now_ns(); |
| 119 | if (info) memset(info, 0, sizeof *info); |
| 120 | uint32_t experts = 0, n = 0, k = 0; |
| 121 | if (!t) return OMEGA_GPU_TCGEMM_BAD_ARGS; |
| 122 | omega_gpu_fp8_tensor_shape(t, &experts, &n, &k); |
| 123 | if (!experts || !ids || !trow || !x || !y || M == 0 || x_rows == 0) return OMEGA_GPU_TCGEMM_BAD_ARGS; |
| 124 | OmegaTcGemmSpec spec; omega_gpu_tcgemm_spec_for(k, n, &spec); |
| 125 | if (!omega_tcgemm_spec_ok(&spec)) return OMEGA_GPU_TCGEMM_BAD_ARGS; |
| 126 | const uint32_t BM = 16u * spec.rt, span = omega_tcgemm_span(&spec); |
| 127 | if (n % span || n % 128u) return OMEGA_GPU_TCGEMM_BAD_ARGS; |
| 128 | uint32_t segs = 0, ntiles = 0; |
| 129 | for (uint32_t i = 0; i < M;) { |
| 130 | if (ids[i] >= experts) return OMEGA_GPU_TCGEMM_BAD_ARGS; |
| 131 | uint32_t j = i; while (j < M && ids[j] == ids[i]) { if (trow[j] >= x_rows) return OMEGA_GPU_TCGEMM_BAD_ARGS; j++; } |
| 132 | if (j < M && ids[j] < ids[i]) return OMEGA_GPU_TCGEMM_BAD_ARGS; |
| 133 | segs++; ntiles += (j - i + BM - 1) / BM; i = j; |
| 134 | } |
| 135 | if (ntiles > 65535u) return OMEGA_GPU_TCGEMM_TOO_LARGE; |
| 136 | const uint32_t grid_x = n / span; |
| 137 | |
| 138 | omega_gpu_session_lock(); |
| 139 | int rc = OMEGA_GPU_TCGEMM_OK; |
| 140 | if (!dev_open_locked()) { omega_gpu_session_unlock(); return OMEGA_GPU_TCGEMM_CHIP_FAIL; } |
| 141 | int src = omega_gpu_session_scratch(&g.x_buf, (size_t)x_rows * k * 2u); |
| 142 | if (src == 0) src = omega_gpu_session_scratch(&g.y_buf, (size_t)M * n * 4u); |
| 143 | if (src == 0) src = omega_gpu_session_scratch(&g.tile_buf, (size_t)ntiles * OMEGA_TCGEMM_TILE_BYTES); |
| 144 | if (src == 0) src = omega_gpu_session_scratch(&g.ridx_buf, ((size_t)M + BM) * 4u); |
| 145 | if (src != 0) { omega_gpu_session_unlock(); return src == OMEGA_GPU_SESSION_SCRATCH_RESERVED ? OMEGA_GPU_TCGEMM_TOO_LARGE : OMEGA_GPU_TCGEMM_CHIP_FAIL; } |
| 146 | Slot *ks = NULL; bool hit = false; |
| 147 | rc = kernel_for(&spec, &ks, &hit); |
| 148 | if (rc != OMEGA_GPU_TCGEMM_OK) { omega_gpu_session_unlock(); return rc; } |
| 149 | |
| 150 | TileEntry *tab = g.tile_buf.mem.cpu; uint32_t *ridx = g.ridx_buf.mem.cpu; uint32_t nt = 0; |
| 151 | uint64_t distinct = 0; |
| 152 | for (uint32_t i = 0; i < M;) { |
| 153 | uint32_t j = i; while (j < M && ids[j] == ids[i]) j++; |
| 154 | uint64_t w_va = 0, s_va = 0; (void)omega_gpu_fp8_tensor_entry(t, ids[i], &w_va, &s_va); |
| 155 | for (uint32_t r = i; r < j; r += BM) { memset(&tab[nt], 0, sizeof tab[nt]); tab[nt].w_va = w_va; tab[nt].s_va = s_va; tab[nt].row0 = r; tab[nt].cnt = j - r < BM ? j - r : BM; nt++; } |
| 156 | distinct += (uint64_t)n * k; i = j; |
| 157 | } |
| 158 | { uint32_t *tmp = malloc(((size_t)M + BM) * 4u); if (!tmp) { omega_gpu_session_unlock(); return OMEGA_GPU_TCGEMM_CHIP_FAIL; } |
| 159 | for (uint32_t i = 0; i < M + BM; i++) tmp[i] = i < M ? trow[i] : 0u; |
| 160 | memcpy(ridx, tmp, ((size_t)M + BM) * 4u); free(tmp); } |
| 161 | { /* permute in cached memory, one sequential copy per row into the uncached buffer */ |
| 162 | uint16_t *row = malloc((size_t)k * 2u); uint16_t *xd = g.x_buf.mem.cpu; |
| 163 | if (!row) { omega_gpu_session_unlock(); return OMEGA_GPU_TCGEMM_CHIP_FAIL; } |
| 164 | for (uint32_t r = 0; r < x_rows; r++) { omega_tcgemm_permute_x(x + (size_t)r * k, k, row); memcpy(xd + (size_t)r * k, row, (size_t)k * 2u); } |
| 165 | free(row); |
| 166 | } |
| 167 | float *yd = g.y_buf.mem.cpu; |
| 168 | for (size_t i = 0; i < (size_t)M * n; i++) yd[i] = POISON_F32; |
| 169 | if (omega_gpu_session_sync_to_gpu(&g.tile_buf.mem) != 0 || omega_gpu_session_sync_to_gpu(&g.ridx_buf.mem) != 0 || |
| 170 | omega_gpu_session_sync_to_gpu(&g.x_buf.mem) != 0 || omega_gpu_session_sync_to_gpu(&g.y_buf.mem) != 0) rc = OMEGA_GPU_TCGEMM_CHIP_FAIL; |
| 171 | |
| 172 | uint32_t args[OMEGA_TCGEMM_ARGS_WORDS]; |
| 173 | const uint64_t va[4] = { g.tile_buf.mem.va, g.ridx_buf.mem.va, g.x_buf.mem.va, g.y_buf.mem.va }; |
| 174 | for (int i = 0; i < 4; i++) { args[2 * i] = (uint32_t)va[i]; args[2 * i + 1] = (uint32_t)(va[i] >> 32); } |
| 175 | args[8] = n; |
| 176 | const uint32_t decl = omega_bw_gpr_declared(ks->kernel.gpr_count); |
| 177 | if (info) { |
| 178 | info->rows = M; info->segments = segs; info->tiles = nt; info->n = n; info->k = k; info->rt = spec.rt; info->ct = spec.ct; info->warps = spec.warps; |
| 179 | info->grid_x = grid_x; info->grid_y = nt; info->gpr_declared = decl; |
| 180 | info->weight_bytes = distinct; info->streamed_bytes = (uint64_t)nt * n * k; |
| 181 | info->act_bytes = (uint64_t)nt * grid_x * BM * k * 2u; info->kernel_cache_hit = hit; |
| 182 | } |
| 183 | if (rc == OMEGA_GPU_TCGEMM_OK) { |
| 184 | OmegaGpuLaunch L = { |
| 185 | .code_va = ks->code.va, .gpr_count = decl, |
| 186 | .threads_x = 32u * spec.warps, .threads_y = 1, .grid_x = grid_x, .grid_y = nt, |
| 187 | .num_elements = (size_t)M * n, |
| 188 | .args = args, .n_args = OMEGA_TCGEMM_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS, |
| 189 | .shared_bytes = omega_tcgemm_shared_bytes(&spec), |
| 190 | }; |
| 191 | if (!omega_bw_gpr_declared_ok(L.gpr_count, ks->kernel.gpr_count)) { omega_gpu_session_set_error("declared registers below peak + 2: refusing to launch"); rc = OMEGA_GPU_TCGEMM_CHIP_FAIL; } |
| 192 | else if (omega_gpu_session_launch(&L, info ? &info->elapsed_ns : NULL, info ? &info->completion_marker : NULL) != 0) rc = OMEGA_GPU_TCGEMM_CHIP_FAIL; |
| 193 | } |
| 194 | if (rc == OMEGA_GPU_TCGEMM_OK && omega_gpu_session_sync_to_cpu(&g.y_buf.mem) != 0) rc = OMEGA_GPU_TCGEMM_CHIP_FAIL; |
| 195 | if (rc == OMEGA_GPU_TCGEMM_OK) { |
| 196 | size_t unchanged = 0; |
| 197 | for (size_t i = 0; i < (size_t)M * n; i++) if (yd[i] == POISON_F32) unchanged++; |
| 198 | if (unchanged) { omega_gpu_session_set_error("output poison survived"); rc = OMEGA_GPU_TCGEMM_CHIP_FAIL; } |
| 199 | else memcpy(y, yd, (size_t)M * n * sizeof *y); |
| 200 | } |
| 201 | omega_gpu_session_unlock(); |
| 202 | if (info) info->call_ns = omega_gpu_session_now_ns() - t0; |
| 203 | return rc; |
| 204 | } |