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_router_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 | /* |
| 18 | * omega_gpu_router_api.c: resident router weights, directories, launch buffers and the one-launch router call |
| 19 | * (see the header). Session use mirrors omega_gpu_decode_attn_api.c: every buffer from omega_gpu_session_alloc |
| 20 | * (GPU-uncached, host-visible), sync_to_gpu after host writes, sync_to_cpu before host reads, one |
| 21 | * omega_gpu_session_launch per call. |
| 22 | */ |
| 23 | #include "omega_gpu_router_api.h" |
| 24 | #include "omega_gpu_session.h" |
| 25 | #include <stdbool.h> |
| 26 | #include <string.h> |
| 27 | #include "omega_gpu_cmdlist.h" |
| 28 | |
| 29 | #define MARKER_TIMEOUT_MS 20000ull |
| 30 | #define POISON_F32 (-999.0f) |
| 31 | #define POISON_ID 0xffffffffu |
| 32 | |
| 33 | /* the launch buffer: one allocation, fixed offsets (bytes) */ |
| 34 | #define LB_X 0u |
| 35 | #define LB_XOUT 8192u |
| 36 | #define LB_IDS 16384u |
| 37 | #define LB_WTS 16448u |
| 38 | #define LB_TAB_GU 16512u |
| 39 | #define LB_TAB_DN 16640u /* 8 slots; with the shared slot (9) the down table is at LB_TAB_DN9 */ |
| 40 | #define LB_BYTES 16768u |
| 41 | #define LB_TAB_DN9 16656u |
| 42 | #define LB_BYTES9 16800u |
| 43 | |
| 44 | struct OmegaGpuRouter { |
| 45 | uint32_t layers; |
| 46 | OmegaGpuMem gate; /* [layers][128][2048] bf16 */ |
| 47 | OmegaGpuMem norm; /* [layers][2048] f32 */ |
| 48 | OmegaGpuMem dir; /* [layers][2][128][2] u64 */ |
| 49 | OmegaGpuMem lb; /* launch buffer */ |
| 50 | uint32_t experts, shared; /* routed experts (128 or 256) and the shared slot (0 or 1) */ |
| 51 | uint32_t rows, slots; /* gate rows = experts + shared; output slots = 8 + shared */ |
| 52 | uint32_t lb_tab_dn, lb_bytes; |
| 53 | }; |
| 54 | #define ROWB(r) ((size_t)(r)->rows * OMEGA_ROUTER_ROW_BYTES) /* gate bytes per layer */ |
| 55 | #define DIRB(r) ((size_t)(r)->rows * 16u) /* one directory per layer */ |
| 56 | |
| 57 | typedef struct { |
| 58 | bool valid; |
| 59 | OmegaRouterSpec spec; |
| 60 | OmegaBlackwellKernel kernel; |
| 61 | OmegaGpuMem code; |
| 62 | } Slot; |
| 63 | |
| 64 | static struct { |
| 65 | bool hooked; |
| 66 | Slot slot; /* the plain kernel (grid 1 x 1) */ |
| 67 | Slot rows; /* the ROWS form (lever 2: one launch per layer for every conversation); omega_gpu_router_prepare_rows */ |
| 68 | uint32_t warps, depth; |
| 69 | int mutant; |
| 70 | } g; |
| 71 | static int g_rec; /* 1 while a record call runs (lock held): kernel_for must not evict */ |
| 72 | |
| 73 | static void slot_free(Slot *s) { if (s->valid) omega_gpu_session_free(&s->code); memset(s, 0, sizeof *s); } |
| 74 | static void slot_clear_locked(void) { slot_free(&g.slot); slot_free(&g.rows); } |
| 75 | static void on_session_close(void) { memset(&g.slot, 0, sizeof g.slot); memset(&g.rows, 0, sizeof g.rows); } |
| 76 | static OmegaGpuSession *dev_open_locked(void) { |
| 77 | if (!g.hooked) { (void)omega_gpu_session_on_close(on_session_close); g.hooked = true; } |
| 78 | return omega_gpu_session_open(); |
| 79 | } |
| 80 | 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) */ |
| 81 | |
| 82 | void omega_gpu_router_set_shape(uint32_t warps, uint32_t depth) { omega_gpu_session_lock(); g.warps = warps; g.depth = depth; slot_clear_locked(); omega_gpu_session_unlock(); } |
| 83 | void omega_gpu_router_test_set_mutant(int mutant) { omega_gpu_session_lock(); g.mutant = mutant; slot_clear_locked(); omega_gpu_session_unlock(); } |
| 84 | const char *omega_gpu_router_last_error(void) { return omega_gpu_session_last_error(); } |
| 85 | const char *omega_gpu_router_rc_name(int rc) { |
| 86 | switch (rc) { |
| 87 | case OMEGA_GPU_ROUTER_OK: return "OK"; |
| 88 | case OMEGA_GPU_ROUTER_BAD_ARGS: return "BAD_ARGS"; |
| 89 | case OMEGA_GPU_ROUTER_TOO_LARGE: return "TOO_LARGE"; |
| 90 | case OMEGA_GPU_ROUTER_CODEGEN_FAIL: return "CODEGEN_FAIL"; |
| 91 | case OMEGA_GPU_ROUTER_CHIP_FAIL: return "CHIP_FAIL"; |
| 92 | default: return "?"; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /* ---- buffers ---- */ |
| 97 | int omega_gpu_router_alloc(uint32_t layers, OmegaGpuRouter **out) { return omega_gpu_router_alloc_shape(layers, 0, 0, out); } |
| 98 | int omega_gpu_router_alloc_shape(uint32_t layers, uint32_t experts, uint32_t shared, OmegaGpuRouter **out) { |
| 99 | if (!out || layers == 0 || layers > OMEGA_GPU_ROUTER_MAX_LAYERS) return OMEGA_GPU_ROUTER_BAD_ARGS; |
| 100 | *out = NULL; |
| 101 | if (!experts) experts = OMEGA_ROUTER_EXPERTS; |
| 102 | if ((experts != 128u && experts != 256u) || shared > 1u) return OMEGA_GPU_ROUTER_BAD_ARGS; |
| 103 | OmegaGpuRouter *r = omega_gpu_host_calloc(1, sizeof *r); |
| 104 | if (!r) return OMEGA_GPU_ROUTER_BAD_ARGS; |
| 105 | r->layers = layers; r->experts = experts; r->shared = shared; r->rows = experts + shared; r->slots = OMEGA_ROUTER_TOPK + shared; |
| 106 | r->lb_tab_dn = shared ? LB_TAB_DN9 : LB_TAB_DN; r->lb_bytes = shared ? LB_BYTES9 : LB_BYTES; |
| 107 | omega_gpu_session_lock(); |
| 108 | int rc = OMEGA_GPU_ROUTER_OK; |
| 109 | if (!dev_open_locked()) rc = OMEGA_GPU_ROUTER_CHIP_FAIL; |
| 110 | if (rc == OMEGA_GPU_ROUTER_OK && omega_gpu_session_alloc((size_t)layers * ROWB(r), &r->gate) != 0) rc = OMEGA_GPU_ROUTER_CHIP_FAIL; |
| 111 | if (rc == OMEGA_GPU_ROUTER_OK && omega_gpu_session_alloc((size_t)layers * OMEGA_ROUTER_HIDDEN * 4u, &r->norm) != 0) rc = OMEGA_GPU_ROUTER_CHIP_FAIL; |
| 112 | if (rc == OMEGA_GPU_ROUTER_OK && omega_gpu_session_alloc((size_t)layers * 2u * DIRB(r), &r->dir) != 0) rc = OMEGA_GPU_ROUTER_CHIP_FAIL; |
| 113 | if (rc == OMEGA_GPU_ROUTER_OK && omega_gpu_session_alloc(r->lb_bytes, &r->lb) != 0) rc = OMEGA_GPU_ROUTER_CHIP_FAIL; |
| 114 | if (rc == OMEGA_GPU_ROUTER_OK) { |
| 115 | memset(r->dir.cpu, 0, (size_t)layers * 2u * DIRB(r)); |
| 116 | memset(r->lb.cpu, 0, r->lb_bytes); |
| 117 | if (omega_gpu_session_sync_to_gpu(&r->dir) != 0 || omega_gpu_session_sync_to_gpu(&r->lb) != 0) rc = OMEGA_GPU_ROUTER_CHIP_FAIL; |
| 118 | } |
| 119 | omega_gpu_session_unlock(); |
| 120 | if (rc != OMEGA_GPU_ROUTER_OK) { omega_gpu_router_free(r); return rc; } |
| 121 | *out = r; |
| 122 | return OMEGA_GPU_ROUTER_OK; |
| 123 | } |
| 124 | |
| 125 | void omega_gpu_router_free(OmegaGpuRouter *r) { |
| 126 | if (!r) return; |
| 127 | omega_gpu_session_lock(); |
| 128 | if (r->gate.cpu) omega_gpu_session_free(&r->gate); |
| 129 | if (r->norm.cpu) omega_gpu_session_free(&r->norm); |
| 130 | if (r->dir.cpu) omega_gpu_session_free(&r->dir); |
| 131 | if (r->lb.cpu) omega_gpu_session_free(&r->lb); |
| 132 | omega_gpu_session_unlock(); |
| 133 | omega_gpu_host_free(r); |
| 134 | } |
| 135 | |
| 136 | int omega_gpu_router_set_layer(OmegaGpuRouter *r, uint32_t layer, const uint16_t *gate_bf16, const float *norm_w) { |
| 137 | if (!r || layer >= r->layers || !gate_bf16 || !norm_w) return OMEGA_GPU_ROUTER_BAD_ARGS; |
| 138 | omega_gpu_session_lock(); |
| 139 | memcpy((uint8_t *)r->gate.cpu + (size_t)layer * ROWB(r), gate_bf16, ROWB(r)); |
| 140 | memcpy((uint8_t *)r->norm.cpu + (size_t)layer * OMEGA_ROUTER_HIDDEN * 4u, norm_w, (size_t)OMEGA_ROUTER_HIDDEN * 4u); |
| 141 | int rc = (omega_gpu_session_sync_to_gpu(&r->gate) == 0 && omega_gpu_session_sync_to_gpu(&r->norm) == 0) ? OMEGA_GPU_ROUTER_OK : OMEGA_GPU_ROUTER_CHIP_FAIL; |
| 142 | omega_gpu_session_unlock(); |
| 143 | return rc; |
| 144 | } |
| 145 | |
| 146 | int omega_gpu_router_set_directory(OmegaGpuRouter *r, uint32_t layer, const uint64_t *gu, const uint64_t *dn) { |
| 147 | if (!r || layer >= r->layers || !gu || !dn) return OMEGA_GPU_ROUTER_BAD_ARGS; |
| 148 | omega_gpu_session_lock(); |
| 149 | uint8_t *d = (uint8_t *)r->dir.cpu + (size_t)layer * 2u * DIRB(r); |
| 150 | memcpy(d, gu, DIRB(r)); |
| 151 | memcpy(d + DIRB(r), dn, DIRB(r)); |
| 152 | int rc = omega_gpu_session_sync_to_gpu(&r->dir) == 0 ? OMEGA_GPU_ROUTER_OK : OMEGA_GPU_ROUTER_CHIP_FAIL; |
| 153 | omega_gpu_session_unlock(); |
| 154 | return rc; |
| 155 | } |
| 156 | |
| 157 | int omega_gpu_router_set_directory_fp8(OmegaGpuRouter *r, uint32_t layer, const OmegaGpuFp8Tensor *gate_up, const OmegaGpuFp8Tensor *down) { |
| 158 | if (!r || !gate_up || !down) return OMEGA_GPU_ROUTER_BAD_ARGS; |
| 159 | uint64_t gu[(256u + 1u) * 2], dn[(256u + 1u) * 2]; |
| 160 | for (uint32_t x = 0; x < r->rows; x++) { |
| 161 | if (omega_gpu_fp8_tensor_entry(gate_up, x, &gu[2 * x], &gu[2 * x + 1]) != 0) return OMEGA_GPU_ROUTER_BAD_ARGS; |
| 162 | if (omega_gpu_fp8_tensor_entry(down, x, &dn[2 * x], &dn[2 * x + 1]) != 0) return OMEGA_GPU_ROUTER_BAD_ARGS; |
| 163 | } |
| 164 | return omega_gpu_router_set_directory(r, layer, gu, dn); |
| 165 | } |
| 166 | |
| 167 | void omega_gpu_router_device_outputs(const OmegaGpuRouter *r, uint64_t *xout_va, uint64_t *tab_gu_va, uint64_t *tab_dn_va) { |
| 168 | if (xout_va) *xout_va = r ? r->lb.va + LB_XOUT : 0; |
| 169 | if (tab_gu_va) *tab_gu_va = r ? r->lb.va + LB_TAB_GU : 0; |
| 170 | if (tab_dn_va) *tab_dn_va = r ? r->lb.va + r->lb_tab_dn : 0; |
| 171 | } |
| 172 | |
| 173 | int omega_gpu_router_read_back(OmegaGpuRouter *r, float *xout, uint64_t *tab_gu, uint64_t *tab_dn) { |
| 174 | if (!r) return OMEGA_GPU_ROUTER_BAD_ARGS; |
| 175 | omega_gpu_session_lock(); |
| 176 | int rc = omega_gpu_session_sync_to_cpu(&r->lb) == 0 ? OMEGA_GPU_ROUTER_OK : OMEGA_GPU_ROUTER_CHIP_FAIL; |
| 177 | if (rc == OMEGA_GPU_ROUTER_OK) { |
| 178 | const uint8_t *b = r->lb.cpu; |
| 179 | if (xout) memcpy(xout, b + LB_XOUT, OMEGA_ROUTER_HIDDEN * 4u); |
| 180 | if (tab_gu) memcpy(tab_gu, b + LB_TAB_GU, (size_t)r->slots * 16u); |
| 181 | if (tab_dn) memcpy(tab_dn, b + r->lb_tab_dn, (size_t)r->slots * 16u); |
| 182 | } |
| 183 | omega_gpu_session_unlock(); |
| 184 | return rc; |
| 185 | } |
| 186 | |
| 187 | /* ---- kernel ---- */ |
| 188 | static int kernel_for(const OmegaRouterSpec *spec, Slot **out, bool *hit) { |
| 189 | Slot *s = spec->xstride ? &g.rows : &g.slot; |
| 190 | if (s->valid && memcmp(&s->spec, spec, sizeof *spec) == 0) { *out = s; *hit = true; return OMEGA_GPU_ROUTER_OK; } |
| 191 | if (g_rec && s->valid) { omega_gpu_session_set_error("router record: kernel cache miss would evict the resident kernel (warm up before recording)"); return OMEGA_GPU_ROUTER_TOO_LARGE; } |
| 192 | slot_free(s); |
| 193 | BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); |
| 194 | if (!prog) { omega_gpu_session_set_error("router codegen: host memory"); return OMEGA_GPU_ROUTER_CODEGEN_FAIL; } |
| 195 | int cg = omega_blackwell_codegen_router_prog(spec, prog); |
| 196 | size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0; |
| 197 | if (cg != 0 || need == 0) { omega_gpu_host_free(prog); omega_gpu_session_set_error("router codegen refused the spec"); return OMEGA_GPU_ROUTER_CODEGEN_FAIL; } |
| 198 | if ((uint64_t)32u * spec->warps * gpr_declared(prog->regalloc.peak_gpr_usage) > 65536u) { omega_gpu_host_free(prog); omega_gpu_session_set_error("router: register file"); return OMEGA_GPU_ROUTER_TOO_LARGE; } |
| 199 | OmegaGpuMem code; |
| 200 | if (omega_gpu_session_alloc_code(need, &code) != 0) { omega_gpu_host_free(prog); return OMEGA_GPU_ROUTER_CHIP_FAIL; } |
| 201 | OmegaBlackwellKernel kernel; |
| 202 | cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel); |
| 203 | omega_gpu_host_free(prog); |
| 204 | if (cg != 0 || !kernel.code || kernel.code_size != need) { omega_gpu_session_free(&code); omega_gpu_session_set_error("router encode"); return OMEGA_GPU_ROUTER_CODEGEN_FAIL; } |
| 205 | if (omega_gpu_session_sync_to_gpu(&code) != 0) { omega_gpu_session_free(&code); return OMEGA_GPU_ROUTER_CHIP_FAIL; } |
| 206 | memset(s, 0, sizeof *s); |
| 207 | s->valid = true; s->spec = *spec; s->kernel = kernel; s->code = code; |
| 208 | *out = s; *hit = false; |
| 209 | return OMEGA_GPU_ROUTER_OK; |
| 210 | } |
| 211 | |
| 212 | static void spec_for(const OmegaGpuRouter *r, OmegaRouterSpec *spec, uint32_t xstride, uint32_t lbstride) { |
| 213 | memset(spec, 0, sizeof *spec); |
| 214 | spec->warps = g.warps ? g.warps : OMEGA_GPU_ROUTER_WARPS; spec->depth = g.depth ? g.depth : OMEGA_GPU_ROUTER_DEPTH; spec->mutant = g.mutant; |
| 215 | spec->experts = r->experts == OMEGA_ROUTER_EXPERTS ? 0u : r->experts; spec->shared = r->shared; |
| 216 | spec->xstride = xstride; spec->lbstride = lbstride; |
| 217 | } |
| 218 | |
| 219 | /* rec non-NULL (record path, caller holds the session lock): x is the device row dev_x, the launch is appended to rec |
| 220 | * instead of submitted, nothing is staged, poisoned or read back (outputs stay at the launch-buffer addresses). |
| 221 | * rows >= 2 (record path only, lever 2): the ROWS kernel, grid_y rows, row i at dev_x + i * xstride and rec_lb + i * lbstride. */ |
| 222 | static int router_core(OmegaGpuRouter *r, uint32_t layer, const float *x, float eps, int norm_topk, float out_scale, |
| 223 | uint32_t *ids, float *weights, OmegaGpuRouterInfo *info, OmegaGpuCmdList *rec, uint64_t dev_x, uint64_t rec_lb, |
| 224 | uint32_t xstride, uint32_t lbstride, uint32_t rows) { |
| 225 | uint64_t t0 = omega_gpu_session_now_ns(); |
| 226 | if (info) memset(info, 0, sizeof *info); |
| 227 | if (!r || layer >= r->layers || (rec ? !dev_x : (!x || !ids || !weights))) return OMEGA_GPU_ROUTER_BAD_ARGS; |
| 228 | if (rows > 1u && (!rec || !rec_lb)) return OMEGA_GPU_ROUTER_BAD_ARGS; |
| 229 | OmegaRouterSpec spec; spec_for(r, &spec, rows > 1u ? xstride : 0u, rows > 1u ? lbstride : 0u); |
| 230 | OmegaRouterSmem sm; |
| 231 | if (omega_router_smem(&spec, &sm) != 0) return OMEGA_GPU_ROUTER_BAD_ARGS; |
| 232 | |
| 233 | if (!rec) omega_gpu_session_lock(); |
| 234 | #define UNLOCK() do { if (!rec) omega_gpu_session_unlock(); } while (0) |
| 235 | int rc = OMEGA_GPU_ROUTER_OK; |
| 236 | if (!dev_open_locked()) { UNLOCK(); return OMEGA_GPU_ROUTER_CHIP_FAIL; } |
| 237 | Slot *ks = NULL; bool hit = false; |
| 238 | g_rec = rec != NULL; |
| 239 | rc = kernel_for(&spec, &ks, &hit); |
| 240 | g_rec = 0; |
| 241 | if (rc != OMEGA_GPU_ROUTER_OK) { UNLOCK(); return rc; } |
| 242 | uint8_t *b = r->lb.cpu; |
| 243 | float *xo = (float *)(b + LB_XOUT), *wd = (float *)(b + LB_WTS); |
| 244 | uint32_t *idd = (uint32_t *)(b + LB_IDS); |
| 245 | if (!rec) { |
| 246 | memcpy(b + LB_X, x, OMEGA_ROUTER_HIDDEN * 4u); |
| 247 | for (size_t i = 0; i < OMEGA_ROUTER_HIDDEN; i++) xo[i] = POISON_F32; |
| 248 | for (size_t i = 0; i < r->slots; i++) { wd[i] = POISON_F32; idd[i] = POISON_ID; } |
| 249 | memset(b + LB_TAB_GU, 0, (size_t)r->slots * 16u); memset(b + r->lb_tab_dn, 0, (size_t)r->slots * 16u); |
| 250 | if (omega_gpu_session_sync_to_gpu(&r->lb) != 0) rc = OMEGA_GPU_ROUTER_CHIP_FAIL; |
| 251 | } |
| 252 | |
| 253 | uint32_t args[OMEGA_ROUTER_ARGS_WORDS]; |
| 254 | const uint64_t gate = r->gate.va + (uint64_t)layer * ROWB(r); |
| 255 | const uint64_t norm = r->norm.va + (uint64_t)layer * OMEGA_ROUTER_HIDDEN * 4u; |
| 256 | const uint64_t dgu = r->dir.va + (uint64_t)layer * 2u * DIRB(r), ddn = dgu + DIRB(r); |
| 257 | float fe = eps, fo = out_scale; uint32_t ue, uo; memcpy(&ue, &fe, 4); memcpy(&uo, &fo, 4); |
| 258 | #define A64(i, v) do { args[i] = (uint32_t)(v); args[(i) + 1] = (uint32_t)((v) >> 32); } while (0) |
| 259 | const uint64_t lb = rec_lb ? rec_lb : r->lb.va; /* record_to: the outputs land in the caller's buffer (one per conversation in the stroke) */ |
| 260 | A64(0, rec ? dev_x : r->lb.va + LB_X); A64(2, gate); A64(4, norm); A64(6, lb + LB_XOUT); A64(8, lb + LB_IDS); A64(10, lb + LB_WTS); |
| 261 | A64(12, dgu); A64(14, ddn); A64(16, lb + LB_TAB_GU); A64(18, lb + r->lb_tab_dn); |
| 262 | #undef A64 |
| 263 | args[20] = ue; args[21] = uo; args[22] = norm_topk ? 1u : 0u; |
| 264 | if (info) { |
| 265 | info->layer = layer; info->warps = spec.warps; info->depth = spec.depth; info->threads = 32u * spec.warps; |
| 266 | info->weight_bytes = ROWB(r); |
| 267 | info->kernel_cache_hit = hit; |
| 268 | } |
| 269 | if (rc == OMEGA_GPU_ROUTER_OK) { |
| 270 | OmegaGpuLaunch L = { |
| 271 | .code_va = ks->code.va, .gpr_count = gpr_declared(ks->kernel.gpr_count), |
| 272 | .threads_x = 32u * spec.warps, .threads_y = 1, .grid_x = 1, .grid_y = rows > 1u ? rows : 1u, |
| 273 | .num_elements = (size_t)OMEGA_ROUTER_HIDDEN * (rows > 1u ? rows : 1u), |
| 274 | .args = args, .n_args = OMEGA_ROUTER_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS, |
| 275 | .shared_bytes = sm.total, |
| 276 | }; |
| 277 | 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_ROUTER_CHIP_FAIL; } |
| 278 | else if (rec) { if (omega_gpu_cmdlist_add(rec, &L) < 0) rc = OMEGA_GPU_ROUTER_CHIP_FAIL; } |
| 279 | else if (omega_gpu_session_launch(&L, info ? &info->elapsed_ns : NULL, info ? &info->completion_marker : NULL) != 0) rc = OMEGA_GPU_ROUTER_CHIP_FAIL; |
| 280 | } |
| 281 | if (rc == OMEGA_GPU_ROUTER_OK && !rec && omega_gpu_session_sync_to_cpu(&r->lb) != 0) rc = OMEGA_GPU_ROUTER_CHIP_FAIL; |
| 282 | if (rc == OMEGA_GPU_ROUTER_OK && !rec) { |
| 283 | size_t unchanged = 0; |
| 284 | for (size_t i = 0; i < OMEGA_ROUTER_HIDDEN; i++) if (xo[i] == POISON_F32) unchanged++; |
| 285 | for (size_t i = 0; i < r->slots; i++) if (wd[i] == POISON_F32 || idd[i] == POISON_ID) unchanged++; |
| 286 | const uint64_t *tg = (const uint64_t *)(b + LB_TAB_GU), *td = (const uint64_t *)(b + r->lb_tab_dn); |
| 287 | size_t tab_zero = 0; |
| 288 | for (size_t i = 0; i < 2u * r->slots; i++) { if (tg[i] == 0) tab_zero++; if (td[i] == 0) tab_zero++; } |
| 289 | size_t bad_id = 0; |
| 290 | for (size_t i = 0; i < OMEGA_ROUTER_TOPK; i++) if (idd[i] >= r->experts) bad_id++; |
| 291 | if (r->shared && idd[OMEGA_ROUTER_TOPK] != r->experts) bad_id++; |
| 292 | if (unchanged) { omega_gpu_session_set_error("router: output poison survived"); rc = OMEGA_GPU_ROUTER_CHIP_FAIL; } |
| 293 | else if (bad_id) { omega_gpu_session_set_error("router: an expert id is out of range"); rc = OMEGA_GPU_ROUTER_CHIP_FAIL; } |
| 294 | else if (tab_zero) { omega_gpu_session_set_error("router: a group table word stayed zero (directory not set?)"); rc = OMEGA_GPU_ROUTER_CHIP_FAIL; } |
| 295 | else { memcpy(ids, idd, (size_t)r->slots * 4u); memcpy(weights, wd, (size_t)r->slots * 4u); } |
| 296 | } |
| 297 | UNLOCK(); |
| 298 | #undef UNLOCK |
| 299 | if (info) info->call_ns = omega_gpu_session_now_ns() - t0; |
| 300 | return rc; |
| 301 | } |
| 302 | |
| 303 | int omega_gpu_router(OmegaGpuRouter *r, uint32_t layer, const float *x, float eps, int norm_topk, float out_scale, |
| 304 | uint32_t *ids, float *weights, OmegaGpuRouterInfo *info) { |
| 305 | return router_core(r, layer, x, eps, norm_topk, out_scale, ids, weights, info, NULL, 0, 0, 0, 0, 0); |
| 306 | } |
| 307 | |
| 308 | /* Record path (OWN-30B W-L2a decode graph): see the header. Caller holds omega_gpu_session_lock(). */ |
| 309 | int omega_gpu_router_record(OmegaGpuCmdList *cl, OmegaGpuRouter *r, uint32_t layer, uint64_t x_va, float eps, int norm_topk, |
| 310 | float out_scale, OmegaGpuRouterDevOut *out) { |
| 311 | if (!cl) return OMEGA_GPU_ROUTER_BAD_ARGS; |
| 312 | int rc = router_core(r, layer, NULL, eps, norm_topk, out_scale, NULL, NULL, NULL, cl, x_va, 0, 0, 0, 0); |
| 313 | if (rc == OMEGA_GPU_ROUTER_OK && out) { |
| 314 | out->xout_va = r->lb.va + LB_XOUT; out->ids_va = r->lb.va + LB_IDS; out->wts_va = r->lb.va + LB_WTS; |
| 315 | out->tab_gu_va = r->lb.va + LB_TAB_GU; out->tab_dn_va = r->lb.va + r->lb_tab_dn; |
| 316 | } |
| 317 | return rc; |
| 318 | } |
| 319 | /* Record path with the outputs in the caller's buffer (L312 v2, the stroke: one buffer per conversation, the layout of |
| 320 | * omega_gpu_router_layout_bytes). Caller holds omega_gpu_session_lock(). */ |
| 321 | int omega_gpu_router_record_to(OmegaGpuCmdList *cl, OmegaGpuRouter *r, uint32_t layer, uint64_t x_va, float eps, int norm_topk, |
| 322 | float out_scale, uint64_t lb_va, OmegaGpuRouterDevOut *out) { |
| 323 | if (!cl || !r || !lb_va || (lb_va & 255u)) return OMEGA_GPU_ROUTER_BAD_ARGS; |
| 324 | int rc = router_core(r, layer, NULL, eps, norm_topk, out_scale, NULL, NULL, NULL, cl, x_va, lb_va, 0, 0, 0); |
| 325 | if (rc == OMEGA_GPU_ROUTER_OK && out) { |
| 326 | out->xout_va = lb_va + LB_XOUT; out->ids_va = lb_va + LB_IDS; out->wts_va = lb_va + LB_WTS; |
| 327 | out->tab_gu_va = lb_va + LB_TAB_GU; out->tab_dn_va = lb_va + r->lb_tab_dn; |
| 328 | } |
| 329 | return rc; |
| 330 | } |
| 331 | /* Lever 2 (L312): one launch for `rows` conversations of one layer (the ROWS form of the kernel, grid_y rows). Caller holds |
| 332 | * omega_gpu_session_lock(). rows 1 is record_to. The rows kernel must be resident (omega_gpu_router_prepare_rows). */ |
| 333 | int omega_gpu_router_record_rows(OmegaGpuCmdList *cl, OmegaGpuRouter *r, uint32_t layer, uint64_t x_va, uint32_t xstride, float eps, |
| 334 | int norm_topk, float out_scale, uint64_t lb_va, uint32_t lbstride, uint32_t rows, OmegaGpuRouterDevOut *out) { |
| 335 | if (!cl || !r || !lb_va || (lb_va & 255u) || rows == 0 || rows > OMEGA_GPU_ROUTER_MAX_ROWS) return OMEGA_GPU_ROUTER_BAD_ARGS; |
| 336 | if (rows == 1u) return omega_gpu_router_record_to(cl, r, layer, x_va, eps, norm_topk, out_scale, lb_va, out); |
| 337 | if (xstride < OMEGA_ROUTER_HIDDEN * 4u || (xstride & 15u) || lbstride < r->lb_bytes || (lbstride & 255u)) return OMEGA_GPU_ROUTER_BAD_ARGS; |
| 338 | int rc = router_core(r, layer, NULL, eps, norm_topk, out_scale, NULL, NULL, NULL, cl, x_va, lb_va, xstride, lbstride, rows); |
| 339 | if (rc == OMEGA_GPU_ROUTER_OK && out) { |
| 340 | out->xout_va = lb_va + LB_XOUT; out->ids_va = lb_va + LB_IDS; out->wts_va = lb_va + LB_WTS; |
| 341 | out->tab_gu_va = lb_va + LB_TAB_GU; out->tab_dn_va = lb_va + r->lb_tab_dn; |
| 342 | } |
| 343 | return rc; |
| 344 | } |
| 345 | /* Builds (or finds) the ROWS kernel for these strides and keeps it resident beside the plain one (its own cache slot). */ |
| 346 | int omega_gpu_router_prepare_rows(OmegaGpuRouter *r, uint32_t xstride, uint32_t lbstride) { |
| 347 | if (!r || xstride < OMEGA_ROUTER_HIDDEN * 4u || (xstride & 15u) || lbstride < r->lb_bytes || (lbstride & 255u)) return OMEGA_GPU_ROUTER_BAD_ARGS; |
| 348 | OmegaRouterSpec spec; spec_for(r, &spec, xstride, lbstride); |
| 349 | omega_gpu_session_lock(); |
| 350 | int rc = OMEGA_GPU_ROUTER_CHIP_FAIL; |
| 351 | if (dev_open_locked()) { Slot *ks = NULL; bool hit = false; g_rec = 0; rc = kernel_for(&spec, &ks, &hit); } |
| 352 | omega_gpu_session_unlock(); |
| 353 | return rc; |
| 354 | } |
| 355 | uint32_t omega_gpu_router_layout_bytes(const OmegaGpuRouter *r) { return r ? r->lb_bytes : 0u; } |