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_fp8gemm_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_fp8gemm_api.c: FP8 grouped GEMM for prefill on the shared session (OWN-30B builder F item 26). |
| 18 | * See the header. Submission path and buffer policy are the FP8 GEMV API's: every buffer both sides touch is |
| 19 | * GPU-uncached (nvos.h NVOS32_ATTR2_GPU_CACHEABLE), one launch = omega_gpu_session_launch. */ |
| 20 | #include "omega_gpu_fp8gemm_api.h" |
| 21 | #include "omega_gpu_session.h" |
| 22 | #include "omega_gpu_cmdlist.h" |
| 23 | #include "omega_gpu_fmt.h" |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | #define CACHE_SLOTS 8 |
| 28 | #define MARKER_TIMEOUT_MS 20000ull |
| 29 | #define POISON_F32 (-999.0f) |
| 30 | |
| 31 | typedef struct { |
| 32 | bool valid, pinned; |
| 33 | OmegaFp8GemmSpec spec; |
| 34 | OmegaBlackwellKernel kernel; |
| 35 | OmegaGpuMem code; |
| 36 | } Slot; |
| 37 | |
| 38 | static struct { |
| 39 | OmegaGpuScratch x_buf, y_buf, seg_buf; |
| 40 | uint32_t mt, warps, ctas; |
| 41 | int mutant; |
| 42 | bool hooked; |
| 43 | Slot cache[CACHE_SLOTS]; |
| 44 | unsigned next; |
| 45 | } g; |
| 46 | |
| 47 | const char *omega_gpu_fp8gemm_last_error(void) { return omega_gpu_session_last_error(); } |
| 48 | const char *omega_gpu_fp8gemm_rc_name(int rc) { |
| 49 | switch (rc) { |
| 50 | case OMEGA_GPU_FP8GEMM_OK: return "OK"; |
| 51 | case OMEGA_GPU_FP8GEMM_BAD_ARGS: return "BAD_ARGS"; |
| 52 | case OMEGA_GPU_FP8GEMM_TOO_LARGE: return "TOO_LARGE"; |
| 53 | case OMEGA_GPU_FP8GEMM_CODEGEN_FAIL: return "CODEGEN_FAIL"; |
| 54 | case OMEGA_GPU_FP8GEMM_CHIP_FAIL: return "CHIP_FAIL"; |
| 55 | default: return "UNKNOWN"; |
| 56 | } |
| 57 | } |
| 58 | static void cache_clear_locked(void) { |
| 59 | for (unsigned i = 0; i < CACHE_SLOTS; i++) { |
| 60 | if (!g.cache[i].valid) continue; |
| 61 | omega_gpu_session_free(&g.cache[i].code); |
| 62 | memset(&g.cache[i], 0, sizeof g.cache[i]); |
| 63 | } |
| 64 | g.next = 0; |
| 65 | } |
| 66 | static void on_session_close(void) { |
| 67 | cache_clear_locked(); |
| 68 | omega_gpu_session_scratch_free(&g.x_buf); |
| 69 | omega_gpu_session_scratch_free(&g.y_buf); |
| 70 | omega_gpu_session_scratch_free(&g.seg_buf); |
| 71 | } |
| 72 | static OmegaGpuSession *dev_open_locked(void) { |
| 73 | if (!g.hooked) { (void)omega_gpu_session_on_close(on_session_close); g.hooked = true; } |
| 74 | return omega_gpu_session_open(); |
| 75 | } |
| 76 | void omega_gpu_fp8gemm_cache_clear(void) { omega_gpu_session_lock(); cache_clear_locked(); omega_gpu_session_unlock(); } |
| 77 | void omega_gpu_fp8gemm_set_shape(uint32_t mt, uint32_t warps) { omega_gpu_session_lock(); g.mt = mt; g.warps = warps; cache_clear_locked(); omega_gpu_session_unlock(); } |
| 78 | void omega_gpu_fp8gemm_set_cta_budget(uint32_t c) { omega_gpu_session_lock(); g.ctas = c; omega_gpu_session_unlock(); } |
| 79 | void omega_gpu_fp8gemm_test_set_mutant(int m) { omega_gpu_session_lock(); g.mutant = m; cache_clear_locked(); omega_gpu_session_unlock(); } |
| 80 | |
| 81 | static uint32_t gpr_declared(uint32_t peak) { return omega_bw_gpr_declared(peak); } /* peak + 2 rounded up to 8: the chip hung at peak + 1 (omega_blackwell_codegen.h) */ |
| 82 | static bool register_file_fits(uint32_t warps, uint32_t peak) { |
| 83 | uint32_t decl = gpr_declared(peak); |
| 84 | if ((uint64_t)32u * warps * decl <= 65536u) return true; |
| 85 | char e[160]; omega_gpu_fmt(e, sizeof e, "fp8 gemm kernel: %u warps x %u declared registers exceed the 65536-register file", warps, decl); |
| 86 | omega_gpu_session_set_error(e); |
| 87 | return false; |
| 88 | } |
| 89 | int omega_gpu_fp8gemm_kernel_fits(const OmegaFp8GemmSpec *spec, uint32_t *declared) { |
| 90 | uint32_t peak = 0; |
| 91 | if (!spec || omega_fp8gemm_prog_stats(spec, NULL, &peak) != 0) { omega_gpu_session_set_error("fp8 gemm codegen refused the shape"); return OMEGA_GPU_FP8GEMM_CODEGEN_FAIL; } |
| 92 | if (declared) *declared = gpr_declared(peak); |
| 93 | return register_file_fits(spec->warps, peak) ? OMEGA_GPU_FP8GEMM_OK : OMEGA_GPU_FP8GEMM_TOO_LARGE; |
| 94 | } |
| 95 | |
| 96 | void omega_gpu_fp8gemm_shape_for(uint32_t k, uint32_t *mt, uint32_t *warps, uint32_t *lane_bytes) { |
| 97 | uint32_t lb = k % 512u == 0 ? 16u : 8u; |
| 98 | if (lane_bytes) *lane_bytes = lb; |
| 99 | /* default 4 rows per pass: chip sweep 2026-10-13, gate|up 3.5 ms at 4 vs 6.0 ms at 8 (8 rows fill the 16 KB of shared memory and run at half the rate) */ |
| 100 | if (mt) *mt = g.mt ? g.mt : 4u; |
| 101 | if (warps) *warps = g.warps ? g.warps : 16u; |
| 102 | } |
| 103 | /* dense = a few distinct experts with many rows each (qkv, o): 8 rows per pass measured best there */ |
| 104 | static void spec_for(uint32_t k, uint32_t segs, OmegaFp8GemmSpec *spec) { |
| 105 | memset(spec, 0, sizeof *spec); |
| 106 | spec->k = k; |
| 107 | omega_gpu_fp8gemm_shape_for(k, &spec->mt, &spec->warps, &spec->lane_bytes); |
| 108 | if (!g.mt && segs < 8u) spec->mt = 8u; |
| 109 | spec->mutant = g.mutant; |
| 110 | } |
| 111 | |
| 112 | static int g_rec; /* 1 while a record call runs (lock held): kernel_for must not evict */ |
| 113 | static int kernel_for(const OmegaFp8GemmSpec *spec, Slot **out, bool *hit) { |
| 114 | for (unsigned i = 0; i < CACHE_SLOTS; i++) { |
| 115 | Slot *s = &g.cache[i]; |
| 116 | if (s->valid && memcmp(&s->spec, spec, sizeof *spec) == 0) { *out = s; *hit = true; return OMEGA_GPU_FP8GEMM_OK; } |
| 117 | } |
| 118 | Slot *s = NULL; |
| 119 | for (unsigned i = 0; i < CACHE_SLOTS && !s; i++) { |
| 120 | Slot *c = &g.cache[(g.next + i) % CACHE_SLOTS]; |
| 121 | if (!c->valid || !c->pinned) { s = c; g.next = (unsigned)((c - g.cache) + 1) % CACHE_SLOTS; } |
| 122 | } |
| 123 | if (!s) { omega_gpu_session_set_error("fp8 gemm kernel cache: every slot holds a prepared kernel"); return OMEGA_GPU_FP8GEMM_TOO_LARGE; } |
| 124 | if (g_rec && s->valid) { omega_gpu_session_set_error("fp8 gemm record: kernel cache miss would evict a resident kernel (prepare the shape before recording)"); return OMEGA_GPU_FP8GEMM_TOO_LARGE; } |
| 125 | BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); |
| 126 | if (!prog) { omega_gpu_session_set_error("fp8 gemm codegen: host memory"); return OMEGA_GPU_FP8GEMM_CODEGEN_FAIL; } |
| 127 | int cg = omega_blackwell_codegen_fp8gemm_prog(spec, prog); |
| 128 | size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0; |
| 129 | if (cg != 0 || need == 0) { omega_gpu_host_free(prog); omega_gpu_session_set_error("fp8 gemm codegen refused the shape"); return OMEGA_GPU_FP8GEMM_CODEGEN_FAIL; } |
| 130 | if (!register_file_fits(spec->warps, prog->regalloc.peak_gpr_usage)) { omega_gpu_host_free(prog); return OMEGA_GPU_FP8GEMM_TOO_LARGE; } |
| 131 | OmegaGpuMem code; |
| 132 | if (omega_gpu_session_alloc_code(need, &code) != 0) { omega_gpu_host_free(prog); return OMEGA_GPU_FP8GEMM_CHIP_FAIL; } |
| 133 | OmegaBlackwellKernel kernel; |
| 134 | cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel); |
| 135 | omega_gpu_host_free(prog); |
| 136 | if (cg != 0 || !kernel.code || kernel.code_size != need) { omega_gpu_session_free(&code); return OMEGA_GPU_FP8GEMM_CODEGEN_FAIL; } |
| 137 | if (omega_gpu_session_sync_to_gpu(&code) != 0) { omega_gpu_session_free(&code); return OMEGA_GPU_FP8GEMM_CHIP_FAIL; } |
| 138 | if (s->valid) omega_gpu_session_free(&s->code); |
| 139 | memset(s, 0, sizeof *s); |
| 140 | s->valid = true; s->spec = *spec; s->kernel = kernel; s->code = code; |
| 141 | *out = s; *hit = false; |
| 142 | return OMEGA_GPU_FP8GEMM_OK; |
| 143 | } |
| 144 | |
| 145 | int omega_gpu_fp8gemm_prepare(uint32_t k) { |
| 146 | /* both variants: many experts (4 rows per pass) and dense (8 rows per pass) */ |
| 147 | OmegaFp8GemmSpec sp[2]; spec_for(k, 64u, &sp[0]); spec_for(k, 1u, &sp[1]); |
| 148 | omega_gpu_session_lock(); |
| 149 | int rc = OMEGA_GPU_FP8GEMM_OK; |
| 150 | if (!dev_open_locked()) rc = OMEGA_GPU_FP8GEMM_CHIP_FAIL; |
| 151 | else for (int v = 0; v < 2 && rc == OMEGA_GPU_FP8GEMM_OK; v++) { Slot *s = NULL; bool hit = false; rc = kernel_for(&sp[v], &s, &hit); if (rc == OMEGA_GPU_FP8GEMM_OK) s->pinned = true; } |
| 152 | omega_gpu_session_unlock(); |
| 153 | return rc; |
| 154 | } |
| 155 | |
| 156 | /* ---- the stroke's record path (L312 v2): one launch appended to a command list, every buffer the caller's ---- */ |
| 157 | static void spec_for_stroke(uint32_t k, uint32_t segs, uint32_t mt, int rowmap, OmegaFp8GemmSpec *spec) { |
| 158 | spec_for(k, segs, spec); |
| 159 | if (mt) spec->mt = mt; |
| 160 | spec->rowmap = rowmap ? 1u : 0u; |
| 161 | } |
| 162 | int omega_gpu_fp8gemm_prepare_shape(uint32_t k, uint32_t segs, uint32_t mt, int rowmap) { |
| 163 | OmegaFp8GemmSpec spec; spec_for_stroke(k, segs, mt, rowmap, &spec); |
| 164 | if (omega_fp8gemm_shared_bytes(&spec) == 0) { omega_gpu_session_set_error("fp8 gemm prepare: shape refused (rowmap needs mt <= 4)"); return OMEGA_GPU_FP8GEMM_BAD_ARGS; } |
| 165 | omega_gpu_session_lock(); |
| 166 | int rc = OMEGA_GPU_FP8GEMM_OK; |
| 167 | if (!dev_open_locked()) rc = OMEGA_GPU_FP8GEMM_CHIP_FAIL; |
| 168 | else { Slot *s = NULL; bool hit = false; rc = kernel_for(&spec, &s, &hit); if (rc == OMEGA_GPU_FP8GEMM_OK) s->pinned = true; } |
| 169 | omega_gpu_session_unlock(); |
| 170 | return rc; |
| 171 | } |
| 172 | uint32_t omega_gpu_fp8gemm_cta_budget_for(uint32_t segs) { return g.ctas ? g.ctas : (segs >= 64u ? 2u : segs >= 8u ? 12u : 96u); } |
| 173 | |
| 174 | int omega_gpu_fp8gemm_record(OmegaGpuCmdList *cl, const OmegaGpuFp8Tensor *t, uint32_t rows, uint32_t segs, uint64_t seg_va, uint64_t xp_va, |
| 175 | uint64_t y_va, uint64_t xmap_va, uint64_t ymap_va, uint32_t mt, uint32_t cta_budget, OmegaGpuFp8GemmInfo *info) { |
| 176 | if (info) memset(info, 0, sizeof *info); |
| 177 | uint32_t experts = 0, n = 0, k = 0; |
| 178 | omega_gpu_fp8_tensor_shape(t, &experts, &n, &k); |
| 179 | if (!cl || !t || !experts || !seg_va || !xp_va || !y_va || rows == 0 || segs == 0 || (!xmap_va) != (!ymap_va)) return OMEGA_GPU_FP8GEMM_BAD_ARGS; |
| 180 | if (rows > OMEGA_GPU_FP8GEMM_MAX_ROWS || segs > OMEGA_GPU_FP8GEMM_MAX_SEGMENTS) return OMEGA_GPU_FP8GEMM_TOO_LARGE; |
| 181 | OmegaFp8GemmSpec spec; spec_for_stroke(k, segs, mt, xmap_va != 0, &spec); |
| 182 | if (omega_fp8gemm_shared_bytes(&spec) == 0) { omega_gpu_session_set_error("fp8 gemm record: shape refused (rowmap needs mt <= 4)"); return OMEGA_GPU_FP8GEMM_BAD_ARGS; } |
| 183 | const uint32_t W = spec.warps; |
| 184 | uint32_t grid_x = (n + W - 1) / W; |
| 185 | const uint32_t budget = cta_budget ? cta_budget : omega_gpu_fp8gemm_cta_budget_for(segs); |
| 186 | if (grid_x > budget) grid_x = budget; |
| 187 | if (grid_x == 0) grid_x = 1; |
| 188 | /* caller holds the session lock */ |
| 189 | if (!dev_open_locked()) return OMEGA_GPU_FP8GEMM_CHIP_FAIL; |
| 190 | Slot *ks = NULL; bool hit = false; |
| 191 | g_rec = 1; |
| 192 | int rc = kernel_for(&spec, &ks, &hit); |
| 193 | g_rec = 0; |
| 194 | if (rc != OMEGA_GPU_FP8GEMM_OK) return rc; |
| 195 | uint32_t args[OMEGA_GEMM_ARGS_WORDS_MAP]; memset(args, 0, sizeof args); |
| 196 | args[0] = (uint32_t)seg_va; args[1] = (uint32_t)(seg_va >> 32); |
| 197 | args[2] = (uint32_t)xp_va; args[3] = (uint32_t)(xp_va >> 32); |
| 198 | args[4] = (uint32_t)y_va; args[5] = (uint32_t)(y_va >> 32); |
| 199 | args[6] = n; args[7] = grid_x * W; |
| 200 | args[8] = (uint32_t)xmap_va; args[9] = (uint32_t)(xmap_va >> 32); |
| 201 | args[10] = (uint32_t)ymap_va; args[11] = (uint32_t)(ymap_va >> 32); |
| 202 | if (info) { |
| 203 | info->rows = rows; info->segments = segs; info->n = n; info->k = k; info->mt = spec.mt; info->warps_per_cta = W; info->lane_bytes = spec.lane_bytes; |
| 204 | info->grid_x = grid_x; info->grid_y = segs; info->kernel_cache_hit = hit; |
| 205 | } |
| 206 | OmegaGpuLaunch L = { |
| 207 | .code_va = ks->code.va, .gpr_count = gpr_declared(ks->kernel.gpr_count), |
| 208 | .threads_x = 32u * W, .threads_y = 1, .grid_x = grid_x, .grid_y = segs, |
| 209 | .num_elements = (size_t)rows * n, |
| 210 | .args = args, .n_args = spec.rowmap ? OMEGA_GEMM_ARGS_WORDS_MAP : OMEGA_GEMM_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS, |
| 211 | .shared_bytes = omega_fp8gemm_shared_bytes(&spec), |
| 212 | }; |
| 213 | 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"); return OMEGA_GPU_FP8GEMM_CHIP_FAIL; } |
| 214 | if (omega_gpu_cmdlist_add(cl, &L) < 0) return OMEGA_GPU_FP8GEMM_CHIP_FAIL; |
| 215 | return OMEGA_GPU_FP8GEMM_OK; |
| 216 | } |
| 217 | |
| 218 | /* x' = x * 2^64 in the kernel's lane layout: column c = s*32*LB + L*LB + w*4 + e -> index s*32*LB + (w*32 + L)*4 + e */ |
| 219 | void omega_fp8gemm_permute_x(const float *x, uint32_t k, uint32_t lb, float *out) { |
| 220 | const uint32_t row = 32u * lb, nw = lb / 4u; |
| 221 | for (uint32_t s0 = 0; s0 + row <= k; s0 += row) |
| 222 | for (uint32_t L = 0; L < 32u; L++) |
| 223 | for (uint32_t w = 0; w < nw; w++) { |
| 224 | const float *src = x + s0 + L * lb + w * 4u; float *dst = out + s0 + (w * 32u + L) * 4u; |
| 225 | dst[0] = src[0] * 18446744073709551616.0f; dst[1] = src[1] * 18446744073709551616.0f; |
| 226 | dst[2] = src[2] * 18446744073709551616.0f; dst[3] = src[3] * 18446744073709551616.0f; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | typedef struct { uint64_t w_va, s_va; uint32_t row0, cnt; uint32_t pad[2]; } SegEntry; |
| 231 | _Static_assert(sizeof(SegEntry) == OMEGA_GEMM_SEG_BYTES, "segment entry layout"); |
| 232 | |
| 233 | static int gemm_core(const OmegaGpuFp8Tensor *t, uint32_t M, const uint32_t *ids, const uint32_t *trow, const float *x32, |
| 234 | const uint16_t *x16, uint32_t x_rows, float *y, OmegaGpuFp8GemmInfo *info) { |
| 235 | uint64_t t0 = omega_gpu_session_now_ns(); |
| 236 | if (info) memset(info, 0, sizeof *info); |
| 237 | uint32_t experts = 0, n = 0, k = 0; |
| 238 | omega_gpu_fp8_tensor_shape(t, &experts, &n, &k); |
| 239 | if (!t || !experts || !ids || !trow || (!x32 && !x16) || !y || M == 0 || x_rows == 0) return OMEGA_GPU_FP8GEMM_BAD_ARGS; |
| 240 | if (M > OMEGA_GPU_FP8GEMM_MAX_ROWS) return OMEGA_GPU_FP8GEMM_TOO_LARGE; |
| 241 | uint32_t segs = 0; |
| 242 | for (uint32_t i = 0; i < M; i++) { |
| 243 | if (ids[i] >= experts || trow[i] >= x_rows || (i && ids[i] < ids[i - 1])) return OMEGA_GPU_FP8GEMM_BAD_ARGS; |
| 244 | if (i == 0 || ids[i] != ids[i - 1]) segs++; |
| 245 | } |
| 246 | if (segs > OMEGA_GPU_FP8GEMM_MAX_SEGMENTS) return OMEGA_GPU_FP8GEMM_TOO_LARGE; |
| 247 | OmegaFp8GemmSpec spec; spec_for(k, segs, &spec); |
| 248 | const uint32_t MT = spec.mt, W = spec.warps; |
| 249 | uint32_t grid_x = (n + W - 1) / W; |
| 250 | uint32_t budget = g.ctas ? g.ctas : (segs >= 64u ? 2u : segs >= 8u ? 12u : 96u); |
| 251 | if (grid_x > budget) grid_x = budget; |
| 252 | if (grid_x == 0) grid_x = 1; |
| 253 | |
| 254 | omega_gpu_session_lock(); |
| 255 | int rc = OMEGA_GPU_FP8GEMM_OK; |
| 256 | if (!dev_open_locked()) { omega_gpu_session_unlock(); return OMEGA_GPU_FP8GEMM_CHIP_FAIL; } |
| 257 | int src = omega_gpu_session_scratch(&g.x_buf, ((size_t)M + MT) * k * 4u); |
| 258 | if (src == 0) src = omega_gpu_session_scratch(&g.y_buf, (size_t)M * n * 4u); |
| 259 | if (src == 0) src = omega_gpu_session_scratch(&g.seg_buf, (size_t)OMEGA_GPU_FP8GEMM_MAX_SEGMENTS * OMEGA_GEMM_SEG_BYTES); |
| 260 | if (src != 0) { omega_gpu_session_unlock(); return src == OMEGA_GPU_SESSION_SCRATCH_RESERVED ? OMEGA_GPU_FP8GEMM_TOO_LARGE : OMEGA_GPU_FP8GEMM_CHIP_FAIL; } |
| 261 | Slot *ks = NULL; bool hit = false; |
| 262 | rc = kernel_for(&spec, &ks, &hit); |
| 263 | if (rc != OMEGA_GPU_FP8GEMM_OK) { omega_gpu_session_unlock(); return rc; } |
| 264 | |
| 265 | /* segment table, permuted x' rows, poisoned y */ |
| 266 | SegEntry *tab = g.seg_buf.mem.cpu; |
| 267 | uint64_t streamed = 0, distinct = 0; uint32_t sg = 0, passes_max = 0; |
| 268 | for (uint32_t i = 0; i < M;) { |
| 269 | uint32_t j = i; while (j < M && ids[j] == ids[i]) j++; |
| 270 | memset(&tab[sg], 0, sizeof tab[sg]); |
| 271 | (void)omega_gpu_fp8_tensor_entry(t, ids[i], &tab[sg].w_va, &tab[sg].s_va); |
| 272 | tab[sg].row0 = i; tab[sg].cnt = j - i; |
| 273 | uint32_t passes = (j - i + MT - 1) / MT; if (passes > passes_max) passes_max = passes; |
| 274 | streamed += (uint64_t)passes * n * k; distinct += (uint64_t)n * k; |
| 275 | sg++; i = j; |
| 276 | } |
| 277 | float *xd = g.x_buf.mem.cpu, *yd = g.y_buf.mem.cpu; |
| 278 | { |
| 279 | float tmp[OMEGA_FP8_MAX_K], tp[OMEGA_FP8_MAX_K]; /* permute in cached memory, then one sequential copy: element-wise writes into the uncached buffer cost 2.8 ms per 4 MB */ |
| 280 | /* a source row routed to several experts (top-k) is permuted once and copied after: the permuted rows are kept in cached memory */ |
| 281 | float *pc = ((size_t)x_rows * k * 4u <= (16u << 20)) ? malloc((size_t)x_rows * k * 4u) : NULL; |
| 282 | unsigned char *have = pc ? calloc(x_rows, 1) : NULL; |
| 283 | for (uint32_t i = 0; i < M; i++) { |
| 284 | const uint32_t r = trow[i]; |
| 285 | if (have && have[r]) { memcpy(xd + (size_t)i * k, pc + (size_t)r * k, (size_t)k * 4u); continue; } |
| 286 | if (x32) memcpy(tmp, x32 + (size_t)r * k, (size_t)k * 4u); |
| 287 | else for (uint32_t c = 0; c < k; c++) { uint32_t b = (uint32_t)x16[(size_t)r * k + c] << 16; memcpy(&tmp[c], &b, 4); } |
| 288 | float *dst = have ? pc + (size_t)r * k : tp; |
| 289 | omega_fp8gemm_permute_x(tmp, k, spec.lane_bytes, dst); |
| 290 | if (have) have[r] = 1; |
| 291 | memcpy(xd + (size_t)i * k, dst, (size_t)k * 4u); |
| 292 | } |
| 293 | free(pc); free(have); |
| 294 | memset(xd + (size_t)M * k, 0, (size_t)MT * k * 4u); |
| 295 | } |
| 296 | for (size_t i = 0; i < (size_t)M * n; i++) yd[i] = POISON_F32; |
| 297 | if (omega_gpu_session_sync_to_gpu(&g.seg_buf.mem) != 0 || 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_FP8GEMM_CHIP_FAIL; |
| 298 | |
| 299 | uint32_t args[OMEGA_GEMM_ARGS_WORDS]; |
| 300 | args[0] = (uint32_t)g.seg_buf.mem.va; args[1] = (uint32_t)(g.seg_buf.mem.va >> 32); |
| 301 | args[2] = (uint32_t)g.x_buf.mem.va; args[3] = (uint32_t)(g.x_buf.mem.va >> 32); |
| 302 | args[4] = (uint32_t)g.y_buf.mem.va; args[5] = (uint32_t)(g.y_buf.mem.va >> 32); |
| 303 | args[6] = n; args[7] = grid_x * W; |
| 304 | if (info) { |
| 305 | info->rows = M; info->segments = sg; info->n = n; info->k = k; info->mt = MT; info->warps_per_cta = W; info->lane_bytes = spec.lane_bytes; |
| 306 | info->grid_x = grid_x; info->grid_y = sg; info->passes_max = passes_max; |
| 307 | info->weight_bytes = distinct; info->streamed_bytes = streamed; info->kernel_cache_hit = hit; |
| 308 | } |
| 309 | if (rc == OMEGA_GPU_FP8GEMM_OK) { |
| 310 | OmegaGpuLaunch L = { |
| 311 | .code_va = ks->code.va, .gpr_count = gpr_declared(ks->kernel.gpr_count), |
| 312 | .threads_x = 32u * W, .threads_y = 1, .grid_x = grid_x, .grid_y = sg, |
| 313 | .num_elements = (size_t)M * n, |
| 314 | .args = args, .n_args = OMEGA_GEMM_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS, |
| 315 | .shared_bytes = omega_fp8gemm_shared_bytes(&spec), |
| 316 | }; |
| 317 | 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_FP8GEMM_CHIP_FAIL; } |
| 318 | else if (omega_gpu_session_launch(&L, info ? &info->elapsed_ns : NULL, info ? &info->completion_marker : NULL) != 0) rc = OMEGA_GPU_FP8GEMM_CHIP_FAIL; |
| 319 | } |
| 320 | if (rc == OMEGA_GPU_FP8GEMM_OK && omega_gpu_session_sync_to_cpu(&g.y_buf.mem) != 0) rc = OMEGA_GPU_FP8GEMM_CHIP_FAIL; |
| 321 | if (rc == OMEGA_GPU_FP8GEMM_OK) { |
| 322 | size_t unchanged = 0; |
| 323 | for (size_t i = 0; i < (size_t)M * n; i++) if (yd[i] == POISON_F32) unchanged++; |
| 324 | if (unchanged) { omega_gpu_session_set_error("output poison survived"); rc = OMEGA_GPU_FP8GEMM_CHIP_FAIL; } |
| 325 | else memcpy(y, yd, (size_t)M * n * sizeof *y); |
| 326 | } |
| 327 | omega_gpu_session_unlock(); |
| 328 | if (info) info->call_ns = omega_gpu_session_now_ns() - t0; |
| 329 | return rc; |
| 330 | } |
| 331 | |
| 332 | int omega_gpu_fp8gemm_f32(const OmegaGpuFp8Tensor *t, uint32_t M, const uint32_t *ids, const uint32_t *trow, const float *x, uint32_t x_rows, float *y, OmegaGpuFp8GemmInfo *info) { |
| 333 | return gemm_core(t, M, ids, trow, x, NULL, x_rows, y, info); |
| 334 | } |
| 335 | int omega_gpu_fp8gemm_bf16(const OmegaGpuFp8Tensor *t, uint32_t M, const uint32_t *ids, const uint32_t *trow, const uint16_t *x, uint32_t x_rows, float *y, OmegaGpuFp8GemmInfo *info) { |
| 336 | return gemm_core(t, M, ids, trow, NULL, x, x_rows, y, info); |
| 337 | } |