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