Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/src/omega_gpu_gdn_api.c # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / src / omega_gpu_gdn_api.c` 617 lines · 35193 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* 3 * omega_gpu_gdn_api.c: resident Gated DeltaNet parameters, tails, states and the one-launch decode step (see the 4 * header). Session use mirrors omega_gpu_router_api.c: buffers from omega_gpu_session_alloc (GPU-uncached, host-visible), 5 * sync_to_gpu after host writes, sync_to_cpu before host reads, one omega_gpu_session_launch per call; the tails and states 6 * are the exception (GPU-cached, reached by the host only through the copy / zero launches). 7 */ 8 #include "omega_gpu_gdn_api.h" 9 #include "omega_gpu_gdn_admit.h" 10 #include "omega_gpu_hostmath.h" 11 #include "omega_gpu_session.h" 12 #include "omega_gpu_cmdlist.h" 13 #include "omega_blackwell_codegen_tc.h" 14 #include 15 #include 16 #include 17 #include 18 #include "omega_gpu_mem_cached.h" 19 #include "omega_gpu_apiviews.h" 20 #include "omega_gpu_api_hostrest.h" 21 22 #define MARKER_TIMEOUT_MS 20000ull 23 #define POISON_F32 (-999.0f) 24 25 /* the launch buffer: one allocation, fixed offsets (bytes) */ 26 #define LB_QKV 0u 27 #define LB_Z (LB_QKV + OMEGA_GDN_CONV_DIM * 4u) 28 #define LB_AB (LB_Z + OMEGA_GDN_V_DIM * 4u) 29 #define LB_OUT (LB_AB + OMEGA_GDN_VALUE_HEADS * 8u) 30 #define LB_BYTES (LB_OUT + OMEGA_GDN_V_DIM * 4u) 31 32 struct OmegaGpuGdn { 33 uint32_t layers; 34 uint32_t ratio; /* OmegaGdnSpec.ratio (0 = 3, the 27B) */ 35 OmegaGdnShape sh; 36 OmegaGpuMem par; /* [layers][OMEGA_GDN_PAR_BYTES] */ 37 OmegaGpuMem tail; /* [layers][OMEGA_GDN_TAIL_BYTES] */ 38 OmegaGpuMem state; /* [layers][OMEGA_GDN_STATE_BYTES] */ 39 OmegaGpuMem lb; /* launch buffer */ 40 int cached; /* tail and state GPU-cached: the host reaches them only through the chip (reset, stage) */ 41 int bf16; /* L313: the state is stored bf16 (AIEN_OMEGA_GDN_STATE_BF16=1 at alloc); set/get_state keep the f32 layout */ 42 OmegaGpuMem stage; /* cached: uncached staging for set_state / get_state, [state of one layer][tail of one layer] */ 43 OmegaGpuScratch pf; /* prefill scratch (see pf_layout) */ 44 }; 45 46 typedef struct { 47 bool valid; 48 OmegaGdnSpec spec; 49 OmegaBlackwellKernel kernel; 50 OmegaGpuMem code; 51 uint32_t insns; 52 } Slot; 53 54 /* the resident kernels: decode, prefill prep, scan, post; the state copy and zero */ 55 enum { KK_DECODE, KK_PREP, KK_SCAN, KK_POST, KK_COPY, KK_ZERO, KK_MULTI, KK_N }; 56 static const uint32_t kk_threads[KK_N] = { OMEGA_GDN_THREADS, OMEGA_GDN_PREP_THREADS, OMEGA_GDN_THREADS, OMEGA_GDN_THREADS, 57 OMEGA_GDN_COPY_THREADS, OMEGA_GDN_COPY_THREADS, OMEGA_GDN_THREADS }; 58 static struct { 59 bool hooked; 60 Slot slot[KK_N]; 61 int mutant; 62 } g_k; 63 static int g_rec; /* 1 while a record call runs (lock held): kernel_for must not evict */ 64 65 static void slot_clear_locked(void) { 66 for (unsigned k = 0; k < KK_N; k++) { if (g_k.slot[k].valid) omega_gpu_session_free(&g_k.slot[k].code); memset(&g_k.slot[k], 0, sizeof g_k.slot[k]); } 67 } 68 static void on_session_close(void) { memset(g_k.slot, 0, sizeof g_k.slot); } 69 static OmegaGpuSession *dev_open_locked(void) { 70 if (!g_k.hooked) { (void)omega_gpu_session_on_close(on_session_close); g_k.hooked = true; } 71 return omega_gpu_session_open(); 72 } 73 74 void omega_gpu_gdn_test_set_mutant(int mutant) { omega_gpu_session_lock(); g_k.mutant = mutant; slot_clear_locked(); omega_gpu_session_unlock(); } 75 const char *omega_gpu_gdn_last_error(void) { return omega_gpu_session_last_error(); } 76 const char *omega_gpu_gdn_rc_name(int rc) { 77 switch (rc) { 78 case OMEGA_GPU_GDN_OK: return "OK"; 79 case OMEGA_GPU_GDN_BAD_ARGS: return "BAD_ARGS"; 80 case OMEGA_GPU_GDN_TOO_LARGE: return "TOO_LARGE"; 81 case OMEGA_GPU_GDN_CODEGEN_FAIL: return "CODEGEN_FAIL"; 82 case OMEGA_GPU_GDN_CHIP_FAIL: return "CHIP_FAIL"; 83 default: return "?"; 84 } 85 } 86 87 /* ---- host layout ---- */ 88 /* channel indexing lives in av_gdn_channel and the pack units */ 89 90 static int shape_of(uint32_t ratio, OmegaGdnShape *s) { return omega_gdn_admit_shape(ratio, 0, s); } 91 92 int omega_gdn_pack_params(const float *conv_w, const float *a_log, const float *dt_bias, const float *norm_w, uint8_t *block) { 93 return omega_gdn_pack_params_r(0, conv_w, a_log, dt_bias, norm_w, block); 94 } 95 96 int omega_gdn_pack_params_r(uint32_t ratio, const float *conv_w, const float *a_log, const float *dt_bias, const float *norm_w, uint8_t *block) { 97 OmegaGdnShape S; const OmegaGdnShape *shp = &S; 98 if (shape_of(ratio, &S) || !conv_w || !a_log || !dt_bias || !norm_w || !block) return -1; 99 memcpy(block + OMEGA_GDN_PAR_CONV, conv_w, (size_t)shp->conv * OMEGA_GDN_TAPS * 4u); 100 float *sc = (float *)(block + shp->par_scal); 101 float *ng = malloc((size_t)shp->vh * sizeof *ng); 102 if (!ng) return -1; 103 if (av_gdn_negexp(shp->vh, a_log, ng) != 0) { free(ng); return -1; } 104 av_gdn_scales(shp->vh, ng, dt_bias, sc); 105 free(ng); 106 memcpy(block + shp->par_norm, norm_w, OMEGA_GDN_HEAD_DIM * 4u); 107 return 0; 108 } 109 110 void omega_gdn_pack_tail(const float *tail, float *dev) { (void)omega_gdn_pack_tail_r(0, tail, dev); } 111 112 int omega_gdn_pack_tail_r(uint32_t ratio, const float *tail, float *dev) { 113 OmegaGdnShape S; 114 if (shape_of(ratio, &S)) return -1; 115 return av_gdn_pack_tail(S.vh, S.conv, tail, dev); 116 } 117 118 int omega_gdn_unpack_tail(const float *dev, float *tail) { return omega_gdn_unpack_tail_r(0, dev, tail); } 119 120 int omega_gdn_unpack_tail_r(uint32_t ratio, const float *dev, float *tail) { 121 OmegaGdnShape S; 122 if (shape_of(ratio, &S)) return -1; 123 return av_gdn_unpack_tail(S.vh, S.conv, tail, dev); 124 } 125 126 /* ---- buffers ---- */ 127 static int zero_ts_locked(OmegaGpuGdn *g); 128 static int move_locked(unsigned kind, uint64_t src, uint64_t dst, size_t bytes); 129 int omega_gpu_gdn_alloc(uint32_t layers, OmegaGpuGdn **out) { return omega_gpu_gdn_alloc_ratio(layers, 0, out); } 130 131 int omega_gpu_gdn_alloc_ratio(uint32_t layers, uint32_t ratio, OmegaGpuGdn **out) { 132 OmegaGdnShape S; 133 if (!out || omega_gdn_admit_alloc(layers, ratio, 0, &S)) return OMEGA_GPU_GDN_BAD_ARGS; 134 /* L313: AIEN_OMEGA_GDN_STATE_BF16=1 stores the state bf16 (half the bytes per conversation; compute stays f32) */ 135 const char *pb = getenv("AIEN_OMEGA_GDN_STATE_BF16"); 136 const int bf16 = pb && pb[0] == '1' && pb[1] == 0; 137 if (bf16 && omega_gdn_admit_shape(ratio, 1, &S)) return OMEGA_GPU_GDN_BAD_ARGS; 138 *out = NULL; 139 OmegaGpuGdn *g = omega_gpu_host_calloc(1, sizeof *g); 140 if (!g) return OMEGA_GPU_GDN_BAD_ARGS; 141 g->bf16 = bf16; 142 g->layers = layers; 143 g->ratio = ratio; 144 g->sh = S; 145 omega_gpu_session_lock(); 146 int rc = OMEGA_GPU_GDN_OK; 147 if (!dev_open_locked()) rc = OMEGA_GPU_GDN_CHIP_FAIL; 148 if (rc == OMEGA_GPU_GDN_OK && omega_gpu_session_alloc((size_t)layers * g->sh.par_bytes, &g->par) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; 149 /* tail and state GPU-cached unless AIEN_OMEGA_GDN_STATE_CACHED=0 (measured: the decode step 129 -> 43 us per layer, 150 * runs/probe-gdn): the step's 64 scalar state stores per thread merge in L2 instead of going to DRAM one by one */ 151 const char *pc = getenv("AIEN_OMEGA_GDN_STATE_CACHED"); 152 g->cached = !(pc && pc[0] == '0' && pc[1] == 0); 153 int (*const alloc_ts)(size_t, OmegaGpuMem *) = g->cached ? omega_gpu_session_alloc_cached : omega_gpu_session_alloc; 154 if (rc == OMEGA_GPU_GDN_OK && alloc_ts((size_t)layers * g->sh.tail_bytes, &g->tail) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; 155 if (rc == OMEGA_GPU_GDN_OK && alloc_ts((size_t)layers * g->sh.state_bytes, &g->state) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; 156 if (rc == OMEGA_GPU_GDN_OK && omega_gpu_session_alloc(LB_BYTES, &g->lb) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; 157 if (rc == OMEGA_GPU_GDN_OK) { 158 memset(g->par.cpu, 0, (size_t)layers * g->sh.par_bytes); 159 memset(g->lb.cpu, 0, LB_BYTES); 160 if (omega_gpu_session_sync_to_gpu(&g->par) != 0 || omega_gpu_session_sync_to_gpu(&g->lb) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; 161 } 162 /* tail and state start at zero (cached: zeroed by the chip, since a recycled page may still have lines in L2 that a 163 * CPU write would not reach) */ 164 if (rc == OMEGA_GPU_GDN_OK) rc = zero_ts_locked(g); 165 omega_gpu_session_unlock(); 166 if (rc != OMEGA_GPU_GDN_OK) { omega_gpu_gdn_free(g); return rc; } 167 *out = g; 168 return OMEGA_GPU_GDN_OK; 169 } 170 171 void omega_gpu_gdn_free(OmegaGpuGdn *g) { 172 if (!g) return; 173 omega_gpu_session_lock(); 174 if (g->par.cpu) omega_gpu_session_free(&g->par); 175 if (g->tail.cpu) omega_gpu_session_free(&g->tail); 176 if (g->state.cpu) omega_gpu_session_free(&g->state); 177 if (g->lb.cpu) omega_gpu_session_free(&g->lb); 178 if (g->stage.cpu) omega_gpu_session_free(&g->stage); 179 omega_gpu_session_scratch_free(&g->pf); 180 omega_gpu_session_unlock(); 181 omega_gpu_host_free(g); 182 } 183 184 int omega_gpu_gdn_set_layer(OmegaGpuGdn *g, uint32_t layer, const float *conv_w, const float *a_log, const float *dt_bias, 185 const float *norm_w) { 186 if (!g || omega_gdn_admit_layer(layer, g->layers)) return OMEGA_GPU_GDN_BAD_ARGS; 187 omega_gpu_session_lock(); 188 int rc = omega_gdn_pack_params_r(g->ratio, conv_w, a_log, dt_bias, norm_w, (uint8_t *)g->par.cpu + (size_t)layer * g->sh.par_bytes) == 0 189 ? OMEGA_GPU_GDN_OK : OMEGA_GPU_GDN_BAD_ARGS; 190 if (rc == OMEGA_GPU_GDN_OK && omega_gpu_session_sync_to_gpu(&g->par) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; 191 omega_gpu_session_unlock(); 192 return rc; 193 } 194 195 int omega_gpu_gdn_reset(OmegaGpuGdn *g) { 196 if (!g) return OMEGA_GPU_GDN_BAD_ARGS; 197 omega_gpu_session_lock(); 198 int rc = dev_open_locked() ? zero_ts_locked(g) : OMEGA_GPU_GDN_CHIP_FAIL; 199 omega_gpu_session_unlock(); 200 return rc; 201 } 202 203 /* L313: one layer's state between the f32 reference layout and the stored format (bf16: round to nearest even, the chip's 204 * F2FP.BF16.F32 rule; widen exact). */ 205 static void state_put(const OmegaGpuGdn *g, void *dst, const float *src) { 206 if (!g->bf16) { memcpy(dst, src, g->sh.state_bytes); return; } 207 av_bf16_n(1, g->sh.state_bytes / 2u, src, dst); 208 } 209 static void state_get(const OmegaGpuGdn *g, float *dst, const void *src) { 210 if (!g->bf16) { memcpy(dst, src, g->sh.state_bytes); return; } 211 av_widen_n(g->sh.state_bytes / 2u, src, dst); 212 } 213 214 /* cached: the staging buffer [state][tail] of one layer (uncached), made on first use */ 215 static int stage_locked(OmegaGpuGdn *g) { 216 if (g->stage.cpu) return OMEGA_GPU_GDN_OK; 217 return omega_gpu_session_alloc((size_t)g->sh.state_bytes + g->sh.tail_bytes, &g->stage) == 0 ? OMEGA_GPU_GDN_OK : OMEGA_GPU_GDN_CHIP_FAIL; 218 } 219 220 int omega_gpu_gdn_set_state(OmegaGpuGdn *g, uint32_t layer, const float *tail, const float *state) { 221 if (!g || omega_gdn_admit_layer(layer, g->layers) || !tail || !state) return OMEGA_GPU_GDN_BAD_ARGS; 222 omega_gpu_session_lock(); 223 int rc = OMEGA_GPU_GDN_OK; 224 const size_t so = (size_t)layer * g->sh.state_bytes, to = (size_t)layer * g->sh.tail_bytes; 225 if (!g->cached) { 226 omega_gdn_pack_tail_r(g->ratio, tail, (float *)((uint8_t *)g->tail.cpu + to)); 227 state_put(g, (uint8_t *)g->state.cpu + so, state); 228 rc = (omega_gpu_session_sync_to_gpu(&g->tail) == 0 && omega_gpu_session_sync_to_gpu(&g->state) == 0) ? OMEGA_GPU_GDN_OK : OMEGA_GPU_GDN_CHIP_FAIL; 229 } else { 230 rc = dev_open_locked() ? stage_locked(g) : OMEGA_GPU_GDN_CHIP_FAIL; 231 if (rc == OMEGA_GPU_GDN_OK) { 232 state_put(g, g->stage.cpu, state); 233 omega_gdn_pack_tail_r(g->ratio, tail, (float *)((uint8_t *)g->stage.cpu + g->sh.state_bytes)); 234 if (omega_gpu_session_sync_to_gpu(&g->stage) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; 235 } 236 if (rc == OMEGA_GPU_GDN_OK) rc = move_locked(KK_COPY, g->stage.va, g->state.va + so, g->sh.state_bytes); 237 if (rc == OMEGA_GPU_GDN_OK) rc = move_locked(KK_COPY, g->stage.va + g->sh.state_bytes, g->tail.va + to, g->sh.tail_bytes); 238 } 239 omega_gpu_session_unlock(); 240 return rc; 241 } 242 243 int omega_gpu_gdn_get_state(OmegaGpuGdn *g, uint32_t layer, float *tail, float *state) { 244 if (!g || omega_gdn_admit_layer(layer, g->layers) || (!tail && !state)) return OMEGA_GPU_GDN_BAD_ARGS; 245 omega_gpu_session_lock(); 246 int rc = OMEGA_GPU_GDN_OK; 247 const size_t so = (size_t)layer * g->sh.state_bytes, to = (size_t)layer * g->sh.tail_bytes; 248 const uint8_t *sp, *tp; 249 if (!g->cached) { 250 rc = (omega_gpu_session_sync_to_cpu(&g->tail) == 0 && omega_gpu_session_sync_to_cpu(&g->state) == 0) ? OMEGA_GPU_GDN_OK : OMEGA_GPU_GDN_CHIP_FAIL; 251 sp = (const uint8_t *)g->state.cpu + so; tp = (const uint8_t *)g->tail.cpu + to; 252 } else { 253 rc = dev_open_locked() ? stage_locked(g) : OMEGA_GPU_GDN_CHIP_FAIL; 254 if (rc == OMEGA_GPU_GDN_OK) rc = move_locked(KK_COPY, g->state.va + so, g->stage.va, g->sh.state_bytes); 255 if (rc == OMEGA_GPU_GDN_OK) rc = move_locked(KK_COPY, g->tail.va + to, g->stage.va + g->sh.state_bytes, g->sh.tail_bytes); 256 if (rc == OMEGA_GPU_GDN_OK && omega_gpu_session_sync_to_cpu(&g->stage) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; 257 sp = g->stage.cpu; tp = sp + g->sh.state_bytes; 258 } 259 if (rc == OMEGA_GPU_GDN_OK && tail && omega_gdn_unpack_tail_r(g->ratio, (const float *)tp, tail) != 0) { 260 omega_gpu_session_set_error("gdn: the per-head copies of a key head's conv tail disagree"); 261 rc = OMEGA_GPU_GDN_CHIP_FAIL; 262 } 263 if (rc == OMEGA_GPU_GDN_OK && state) state_get(g, state, sp); 264 omega_gpu_session_unlock(); 265 return rc; 266 } 267 268 /* ---- kernel ---- */ 269 static int kernel_build(unsigned kind, const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { 270 switch (kind) { 271 case KK_DECODE: return omega_blackwell_codegen_gdn_prog(spec, prog); 272 case KK_PREP: return omega_blackwell_codegen_gdn_prep_prog(spec, prog); 273 case KK_SCAN: return omega_blackwell_codegen_gdn_scan_prog(spec, prog); 274 case KK_POST: return omega_blackwell_codegen_gdn_post_prog(spec, prog); 275 case KK_COPY: case KK_ZERO: return omega_blackwell_codegen_gdn_copy_prog(spec, prog); 276 case KK_MULTI: return omega_blackwell_codegen_gdn_multi_prog(spec, prog); 277 default: return -1; 278 } 279 } 280 static int kernel_for(unsigned kind, const OmegaGdnSpec *spec, Slot **out, bool *hit) { 281 Slot *s = &g_k.slot[kind]; 282 const uint64_t policy = OMEGA_AH(11, s->valid, memcmp(&s->spec, spec, sizeof *spec) == 0, g_rec); 283 if (policy == 1) { *out = s; *hit = true; return OMEGA_GPU_GDN_OK; } 284 if (policy == 2) { omega_gpu_session_set_error("gdn record: kernel cache miss would evict the resident kernel (warm up before recording)"); return OMEGA_GPU_GDN_TOO_LARGE; } 285 if (s->valid) omega_gpu_session_free(&s->code); 286 memset(s, 0, sizeof *s); 287 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); 288 if (!prog) { omega_gpu_session_set_error("gdn codegen: host memory"); return OMEGA_GPU_GDN_CODEGEN_FAIL; } 289 int cg = kernel_build(kind, spec, prog); 290 size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0; 291 if (cg != 0 || need == 0) { omega_gpu_host_free(prog); omega_gpu_session_set_error("gdn codegen refused the spec"); return OMEGA_GPU_GDN_CODEGEN_FAIL; } 292 if (!OMEGA_AH(3, kk_threads[kind], omega_bw_gpr_declared(prog->regalloc.peak_gpr_usage))) { omega_gpu_host_free(prog); omega_gpu_session_set_error("gdn: register file"); return OMEGA_GPU_GDN_TOO_LARGE; } 293 const uint32_t insns = (uint32_t)prog->count; 294 OmegaGpuMem code; 295 if (omega_gpu_session_alloc_code(need, &code) != 0) { omega_gpu_host_free(prog); return OMEGA_GPU_GDN_CHIP_FAIL; } 296 OmegaBlackwellKernel kernel; 297 cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel); 298 omega_gpu_host_free(prog); 299 if (cg != 0 || !kernel.code || kernel.code_size != need) { omega_gpu_session_free(&code); omega_gpu_session_set_error("gdn encode"); return OMEGA_GPU_GDN_CODEGEN_FAIL; } 300 if (omega_gpu_session_sync_to_gpu(&code) != 0) { omega_gpu_session_free(&code); return OMEGA_GPU_GDN_CHIP_FAIL; } 301 memset(s, 0, sizeof *s); 302 s->valid = true; s->spec = *spec; s->kernel = kernel; s->code = code; s->insns = insns; 303 *out = s; *hit = false; 304 return OMEGA_GPU_GDN_OK; 305 } 306 307 308 /* One copy (KK_COPY: dst = src) or zero (KK_ZERO: dst = 0, src unused) launch over `bytes` (a multiple of 4096), submitted 309 * and waited for. Caller holds the session lock with the device open. */ 310 static int move_locked(unsigned kind, uint64_t src, uint64_t dst, size_t bytes) { 311 uint32_t grid = 0; 312 if (omega_gdn_admit_move(kind, dst, bytes, &grid)) return OMEGA_GPU_GDN_BAD_ARGS; 313 OmegaGdnSpec spec; memset(&spec, 0, sizeof spec); 314 spec.copy = kind == KK_COPY ? OMEGA_GDN_COPY : OMEGA_GDN_ZERO; 315 Slot *ks = NULL; bool hit = false; 316 int rc = kernel_for(kind, &spec, &ks, &hit); 317 if (rc != OMEGA_GPU_GDN_OK) return rc; 318 uint32_t args[OMEGA_GDN_COPY_ARGS_WORDS] = { (uint32_t)src, (uint32_t)(src >> 32), (uint32_t)dst, (uint32_t)(dst >> 32) }; 319 OmegaGpuLaunch L = { 320 .code_va = ks->code.va, .gpr_count = omega_bw_gpr_declared(ks->kernel.gpr_count), 321 .threads_x = OMEGA_GDN_COPY_THREADS, .threads_y = 1, .grid_x = grid, .grid_y = 1, 322 .num_elements = bytes / 4u, .args = args, .n_args = OMEGA_GDN_COPY_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS, 323 }; 324 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_GDN_CHIP_FAIL; } 325 return omega_gpu_session_launch(&L, NULL, NULL) == 0 ? OMEGA_GPU_GDN_OK : OMEGA_GPU_GDN_CHIP_FAIL; 326 } 327 328 /* Every layer's tail and state to zero: cached on the chip, uncached by the host. Caller holds the lock, device open. */ 329 static int zero_ts_locked(OmegaGpuGdn *g) { 330 const size_t tb = (size_t)g->layers * g->sh.tail_bytes, sb = (size_t)g->layers * g->sh.state_bytes; 331 if (!g->cached) { 332 memset(g->tail.cpu, 0, tb); memset(g->state.cpu, 0, sb); 333 return (omega_gpu_session_sync_to_gpu(&g->tail) == 0 && omega_gpu_session_sync_to_gpu(&g->state) == 0) ? OMEGA_GPU_GDN_OK : OMEGA_GPU_GDN_CHIP_FAIL; 334 } 335 int rc = move_locked(KK_ZERO, 0, g->tail.va, tb); 336 return rc == OMEGA_GPU_GDN_OK ? move_locked(KK_ZERO, 0, g->state.va, sb) : rc; 337 } 338 /* rec non-NULL (record path, caller holds the session lock): the inputs are the device rows dev_*, the launch is 339 * appended to rec instead of submitted, nothing is staged, poisoned or read back. */ 340 static int gdn_core(OmegaGpuGdn *g, uint32_t layer, const float *qkv, const float *z, const float *ab, float eps_l2, 341 float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnInfo *info, 342 OmegaGpuCmdList *rec, uint64_t dev_qkv, uint64_t dev_z, uint64_t dev_ab) { 343 uint64_t t0 = omega_gpu_session_now_ns(); 344 if (info) memset(info, 0, sizeof *info); 345 if (!g || omega_gdn_admit_layer(layer, g->layers) || (rec ? (!dev_qkv || !dev_z || !dev_ab) : (!qkv || !z || !ab || !out))) return OMEGA_GPU_GDN_BAD_ARGS; 346 if (omega_gdn_admit_finite(eps_l2, eps_norm, in_scale, out_scale)) return OMEGA_GPU_GDN_BAD_ARGS; 347 OmegaGdnSpec spec; memset(&spec, 0, sizeof spec); 348 spec.mutant = g_k.mutant; 349 spec.ratio = g->ratio; 350 spec.state_bf16 = (uint32_t)g->bf16; 351 352 if (!rec) omega_gpu_session_lock(); 353 #define UNLOCK() do { if (!rec) omega_gpu_session_unlock(); } while (0) 354 int rc = OMEGA_GPU_GDN_OK; 355 if (!dev_open_locked()) { UNLOCK(); return OMEGA_GPU_GDN_CHIP_FAIL; } 356 Slot *ks = NULL; bool hit = false; 357 g_rec = rec != NULL; 358 rc = kernel_for(KK_DECODE, &spec, &ks, &hit); 359 g_rec = 0; 360 if (rc != OMEGA_GPU_GDN_OK) { UNLOCK(); return rc; } 361 uint8_t *b = g->lb.cpu; 362 float *yo = (float *)(b + LB_OUT); 363 if (!rec) { 364 memcpy(b + LB_QKV, qkv, g->sh.conv * 4u); 365 memcpy(b + LB_Z, z, g->sh.vdim * 4u); 366 memcpy(b + LB_AB, ab, g->sh.vh * 8u); 367 for (size_t i = 0; i < g->sh.vdim; i++) yo[i] = POISON_F32; 368 if (omega_gpu_session_sync_to_gpu(&g->lb) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; 369 } 370 371 uint32_t args[OMEGA_GDN_ARGS_WORDS]; 372 #define A64(i, v) do { args[i] = (uint32_t)(v); args[(i) + 1] = (uint32_t)((v) >> 32); } while (0) 373 A64(0, rec ? dev_qkv : g->lb.va + LB_QKV); A64(2, rec ? dev_z : g->lb.va + LB_Z); A64(4, rec ? dev_ab : g->lb.va + LB_AB); 374 A64(6, g->par.va + (uint64_t)layer * g->sh.par_bytes); A64(8, g->tail.va + (uint64_t)layer * g->sh.tail_bytes); 375 A64(10, g->state.va + (uint64_t)layer * g->sh.state_bytes); A64(12, g->lb.va + LB_OUT); 376 #undef A64 377 memcpy(&args[14], &eps_l2, 4); memcpy(&args[15], &eps_norm, 4); memcpy(&args[16], &in_scale, 4); memcpy(&args[17], &out_scale, 4); 378 if (info) { 379 info->layer = layer; info->threads = OMEGA_GDN_THREADS; info->grid_x = g->sh.r; info->grid_y = OMEGA_GDN_GRID_Y; 380 info->insns = ks->insns; info->gprs = ks->kernel.gpr_count; 381 info->state_bytes = 2ull * g->sh.state_bytes; 382 info->kernel_cache_hit = hit; 383 } 384 if (rc == OMEGA_GPU_GDN_OK) { 385 OmegaGpuLaunch L = { 386 .code_va = ks->code.va, .gpr_count = omega_bw_gpr_declared(ks->kernel.gpr_count), 387 .threads_x = OMEGA_GDN_THREADS, .threads_y = 1, .grid_x = g->sh.r, .grid_y = OMEGA_GDN_GRID_Y, 388 .num_elements = (size_t)g->sh.vdim, 389 .args = args, .n_args = OMEGA_GDN_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS, 390 .shared_bytes = OMEGA_GDN_SHARED_BYTES, 391 }; 392 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_GDN_CHIP_FAIL; } 393 else if (rec) { if (omega_gpu_cmdlist_add(rec, &L) < 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; } 394 else if (omega_gpu_session_launch(&L, info ? &info->elapsed_ns : NULL, info ? &info->completion_marker : NULL) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; 395 } 396 if (rc == OMEGA_GPU_GDN_OK && !rec && omega_gpu_session_sync_to_cpu(&g->lb) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; 397 if (rc == OMEGA_GPU_GDN_OK && !rec) { 398 size_t unchanged = 0; 399 for (size_t i = 0; i < g->sh.vdim; i++) if (yo[i] == POISON_F32) unchanged++; 400 if (unchanged) { omega_gpu_session_set_error("gdn: output poison survived"); rc = OMEGA_GPU_GDN_CHIP_FAIL; } 401 else memcpy(out, yo, g->sh.vdim * 4u); 402 } 403 UNLOCK(); 404 #undef UNLOCK 405 if (info) info->call_ns = omega_gpu_session_now_ns() - t0; 406 return rc; 407 } 408 409 int omega_gpu_gdn_step(OmegaGpuGdn *g, uint32_t layer, const float *qkv, const float *z, const float *ab, float eps_l2, 410 float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnInfo *info) { 411 return gdn_core(g, layer, qkv, z, ab, eps_l2, eps_norm, in_scale, out_scale, out, info, NULL, 0, 0, 0); 412 } 413 414 /* Record path (decode graph): see the header. Caller holds omega_gpu_session_lock(). */ 415 int omega_gpu_gdn_record(OmegaGpuCmdList *cl, OmegaGpuGdn *g, uint32_t layer, uint64_t qkv_va, uint64_t z_va, uint64_t ab_va, 416 float eps_l2, float eps_norm, float in_scale, float out_scale, uint64_t *out_va) { 417 if (!cl) return OMEGA_GPU_GDN_BAD_ARGS; 418 int rc = gdn_core(g, layer, NULL, NULL, NULL, eps_l2, eps_norm, in_scale, out_scale, NULL, NULL, cl, qkv_va, z_va, ab_va); 419 if (rc == OMEGA_GPU_GDN_OK && out_va) *out_va = g->lb.va + LB_OUT; 420 return rc; 421 } 422 423 /* ---- multi-token decode (L369): see the header ---- */ 424 int omega_gpu_gdn_multi_prepare(OmegaGpuGdn *g) { 425 if (!g) return OMEGA_GPU_GDN_BAD_ARGS; 426 OmegaGdnSpec spec; memset(&spec, 0, sizeof spec); 427 spec.mutant = g_k.mutant; spec.ratio = g->ratio; 428 omega_gpu_session_lock(); 429 int rc = dev_open_locked() ? OMEGA_GPU_GDN_OK : OMEGA_GPU_GDN_CHIP_FAIL; 430 Slot *ks = NULL; bool hit = false; 431 if (rc == OMEGA_GPU_GDN_OK) rc = kernel_for(KK_MULTI, &spec, &ks, &hit); 432 omega_gpu_session_unlock(); 433 return rc; 434 } 435 436 int omega_gpu_gdn_multi_record(OmegaGpuCmdList *cl, OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, uint64_t qkv_va, uint64_t z_va, 437 uint32_t xstride, uint64_t ab_va, uint32_t abstride, uint64_t out_va, uint32_t ostride, float eps_l2, 438 float eps_norm, float in_scale, float out_scale) { 439 if (!cl || !g || omega_gdn_admit_multi(layer, g->layers, tokens, qkv_va, z_va, xstride, ab_va, abstride, out_va, ostride)) return OMEGA_GPU_GDN_BAD_ARGS; 440 if (omega_gdn_admit_finite(eps_l2, eps_norm, in_scale, out_scale)) return OMEGA_GPU_GDN_BAD_ARGS; 441 OmegaGdnSpec spec; memset(&spec, 0, sizeof spec); 442 spec.mutant = g_k.mutant; spec.ratio = g->ratio; 443 /* caller holds the session lock */ 444 if (!dev_open_locked()) return OMEGA_GPU_GDN_CHIP_FAIL; 445 Slot *ks = NULL; bool hit = false; 446 g_rec = 1; 447 int rc = kernel_for(KK_MULTI, &spec, &ks, &hit); 448 g_rec = 0; 449 if (rc != OMEGA_GPU_GDN_OK) return rc; 450 uint32_t args[OMEGA_GDN_MULTI_ARGS_WORDS]; 451 #define A64(i, v) do { args[i] = (uint32_t)(v); args[(i) + 1] = (uint32_t)((v) >> 32); } while (0) 452 A64(0, qkv_va); A64(2, z_va); A64(4, ab_va); 453 A64(6, g->par.va + (uint64_t)layer * g->sh.par_bytes); A64(8, g->tail.va + (uint64_t)layer * g->sh.tail_bytes); 454 A64(10, g->state.va + (uint64_t)layer * g->sh.state_bytes); A64(12, out_va); 455 #undef A64 456 memcpy(&args[14], &eps_l2, 4); memcpy(&args[15], &eps_norm, 4); memcpy(&args[16], &in_scale, 4); memcpy(&args[17], &out_scale, 4); 457 args[18] = tokens; args[19] = xstride; args[20] = abstride; args[21] = ostride; 458 OmegaGpuLaunch L = { 459 .code_va = ks->code.va, .gpr_count = omega_bw_gpr_declared(ks->kernel.gpr_count), 460 .threads_x = OMEGA_GDN_THREADS, .threads_y = 1, .grid_x = g->sh.r, .grid_y = OMEGA_GDN_GRID_Y, 461 .num_elements = (size_t)g->sh.vdim, 462 .args = args, .n_args = OMEGA_GDN_MULTI_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS, 463 .shared_bytes = OMEGA_GDN_SHARED_BYTES, 464 }; 465 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_GDN_CHIP_FAIL; } 466 return omega_gpu_cmdlist_add(cl, &L) < 0 ? OMEGA_GPU_GDN_CHIP_FAIL : OMEGA_GPU_GDN_OK; 467 } 468 469 int omega_gpu_gdn_read_out(OmegaGpuGdn *g, float *out) { 470 if (!g || !out) return OMEGA_GPU_GDN_BAD_ARGS; 471 omega_gpu_session_lock(); 472 int rc = omega_gpu_session_sync_to_cpu(&g->lb) == 0 ? OMEGA_GPU_GDN_OK : OMEGA_GPU_GDN_CHIP_FAIL; 473 if (rc == OMEGA_GPU_GDN_OK) memcpy(out, (const uint8_t *)g->lb.cpu + LB_OUT, g->sh.vdim * 4u); 474 omega_gpu_session_unlock(); 475 return rc; 476 } 477 478 /* ---- prefill (omega#543) ---- */ 479 /* scratch sections for T tokens (bytes, 256-aligned): qkv [T][10240] | z [T][6144] | ab [T][48][2] | out [T][6144] | 480 * o [T][6144] | records [T rounded up to 8][48] */ 481 enum { PF_QKV, PF_Z, PF_AB, PF_OUT, PF_O, PF_REC, PF_N }; 482 /* T already admitted (omega_gdn_admit_tokens), g->sh admitted at alloc: the layout cannot refuse. */ 483 static size_t pf_layout(const OmegaGdnShape *shp, uint32_t T, size_t off[PF_N]) { 484 size_t bytes = 0; 485 if (omega_gdn_admit_layout(shp->r, T, off, &bytes)) return SIZE_MAX; 486 return bytes; 487 } 488 489 int omega_gpu_gdn_prefill_reserve(OmegaGpuGdn *g, uint32_t max_tokens) { 490 if (!g || omega_gdn_admit_tokens(max_tokens)) return OMEGA_GPU_GDN_BAD_ARGS; 491 size_t off[PF_N]; 492 const size_t bytes = pf_layout(&g->sh, max_tokens, off); 493 omega_gpu_session_lock(); 494 int rc = OMEGA_GPU_GDN_OK; 495 if (!dev_open_locked()) rc = OMEGA_GPU_GDN_CHIP_FAIL; 496 else { 497 omega_gpu_session_scratch_unreserve(&g->pf); 498 if (omega_gpu_session_scratch_reserve(&g->pf, bytes) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; 499 } 500 omega_gpu_session_unlock(); 501 return rc; 502 } 503 504 /* rec non-NULL (record path, caller holds the lock): the inputs are device rows, the scratch must already hold T 505 * tokens (omega_gpu_gdn_prefill_reserve), the three launches are appended to rec. */ 506 static int pf_core(OmegaGpuGdn *g, uint32_t layer, uint32_t T, const float *qkv, const float *z, const float *ab, float eps_l2, 507 float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnPfInfo *info, 508 OmegaGpuCmdList *rec, uint64_t dev_qkv, uint64_t dev_z, uint64_t dev_ab, uint64_t *out_va) { 509 uint64_t c0 = omega_gpu_session_now_ns(); 510 if (info) memset(info, 0, sizeof *info); 511 if (!g || omega_gdn_admit_layer(layer, g->layers) || omega_gdn_admit_tokens(T)) return OMEGA_GPU_GDN_BAD_ARGS; 512 if (!OMEGA_AH(23, 2, rec != NULL, dev_qkv, dev_z, dev_ab, qkv != NULL, z != NULL, ab != NULL, out != NULL)) return OMEGA_GPU_GDN_BAD_ARGS; 513 if (omega_gdn_admit_finite(eps_l2, eps_norm, in_scale, out_scale)) return OMEGA_GPU_GDN_BAD_ARGS; 514 OmegaGdnSpec spec; memset(&spec, 0, sizeof spec); 515 spec.mutant = g_k.mutant; 516 spec.ratio = g->ratio; 517 spec.state_bf16 = (uint32_t)g->bf16; 518 size_t off[PF_N]; 519 const size_t bytes = pf_layout(&g->sh, T, off); 520 521 if (!rec) omega_gpu_session_lock(); 522 #define UNLOCK() do { if (!rec) omega_gpu_session_unlock(); } while (0) 523 if (!dev_open_locked()) { UNLOCK(); return OMEGA_GPU_GDN_CHIP_FAIL; } 524 if (!OMEGA_AH(14, rec != NULL, g->pf.mem.cpu != NULL, g->pf.cap, bytes)) { 525 omega_gpu_session_set_error("gdn prefill record: scratch below T tokens (call omega_gpu_gdn_prefill_reserve first)"); 526 UNLOCK(); return OMEGA_GPU_GDN_TOO_LARGE; 527 } 528 if (!rec && omega_gpu_session_scratch(&g->pf, bytes) != 0) { UNLOCK(); return OMEGA_GPU_GDN_TOO_LARGE; } 529 Slot *ks[3] = { NULL, NULL, NULL }; 530 int rc = OMEGA_GPU_GDN_OK, hits = 0; 531 g_rec = rec != NULL; 532 for (unsigned k = 0; k < 3 && rc == OMEGA_GPU_GDN_OK; k++) { bool hit = false; rc = kernel_for(KK_PREP + k, &spec, &ks[k], &hit); hits += hit; } 533 g_rec = 0; 534 if (rc != OMEGA_GPU_GDN_OK) { UNLOCK(); return rc; } 535 uint8_t *b = g->pf.mem.cpu; 536 const uint64_t va = g->pf.mem.va; 537 float *yo = (float *)(b + off[PF_OUT]); 538 const size_t nout = (size_t)T * g->sh.vdim; 539 if (!rec) { 540 memcpy(b + off[PF_QKV], qkv, (size_t)T * g->sh.conv * 4u); 541 memcpy(b + off[PF_Z], z, nout * 4u); 542 memcpy(b + off[PF_AB], ab, (size_t)T * g->sh.vh * 8u); 543 for (size_t i = 0; i < nout; i++) yo[i] = POISON_F32; 544 if (omega_gpu_session_sync_to_gpu(&g->pf.mem) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; 545 } 546 const uint64_t a_qkv = rec ? dev_qkv : va + off[PF_QKV], a_z = rec ? dev_z : va + off[PF_Z], a_ab = rec ? dev_ab : va + off[PF_AB]; 547 const uint64_t a_par = g->par.va + (uint64_t)layer * g->sh.par_bytes, a_tail = g->tail.va + (uint64_t)layer * g->sh.tail_bytes; 548 const uint64_t a_state = g->state.va + (uint64_t)layer * g->sh.state_bytes, a_rec = va + off[PF_REC], a_o = va + off[PF_O]; 549 const uint64_t a_out = va + off[PF_OUT]; 550 uint32_t ap[OMEGA_GDN_PREP_ARGS_WORDS], as[OMEGA_GDN_SCAN_ARGS_WORDS], aq[OMEGA_GDN_POST_ARGS_WORDS]; 551 #define A64(arr, i, v) do { (arr)[i] = (uint32_t)(v); (arr)[(i) + 1] = (uint32_t)((v) >> 32); } while (0) 552 A64(ap, 0, a_qkv); A64(ap, 2, a_ab); A64(ap, 4, a_par); A64(ap, 6, a_tail); A64(ap, 8, a_rec); 553 ap[10] = T; memcpy(&ap[11], &eps_l2, 4); memcpy(&ap[12], &in_scale, 4); 554 A64(as, 0, a_rec); A64(as, 2, a_qkv); A64(as, 4, a_tail); A64(as, 6, a_state); A64(as, 8, a_o); 555 as[10] = T; memcpy(&as[11], &in_scale, 4); 556 A64(aq, 0, a_o); A64(aq, 2, a_z); A64(aq, 4, a_par); A64(aq, 6, a_out); 557 memcpy(&aq[8], &eps_norm, 4); memcpy(&aq[9], &in_scale, 4); memcpy(&aq[10], &out_scale, 4); 558 #undef A64 559 const uint32_t thr[3] = { OMEGA_GDN_PREP_THREADS, OMEGA_GDN_THREADS, OMEGA_GDN_THREADS }; 560 const uint32_t gx[3] = { (uint32_t)OMEGA_AH(15, T, OMEGA_GDN_PREP_TOKENS, g->sh.r, 0), (uint32_t)OMEGA_AH(15, T, OMEGA_GDN_PREP_TOKENS, g->sh.r, 1), (uint32_t)OMEGA_AH(15, T, OMEGA_GDN_PREP_TOKENS, g->sh.r, 2) }; 561 const uint32_t gy[3] = { OMEGA_GDN_KEY_HEADS, OMEGA_GDN_GRID_Y, g->sh.post_grid_y }; 562 const uint32_t sh[3] = { OMEGA_GDN_PREP_SHARED_BYTES, OMEGA_GDN_SCAN_SHARED_BYTES, 0 }; 563 const uint32_t *args[3] = { ap, as, aq }; 564 const uint32_t nargs[3] = { OMEGA_GDN_PREP_ARGS_WORDS, OMEGA_GDN_SCAN_ARGS_WORDS, OMEGA_GDN_POST_ARGS_WORDS }; 565 if (info) { 566 info->layer = layer; info->tokens = T; info->kernel_cache_hits = (uint32_t)hits; 567 for (unsigned k = 0; k < 3; k++) { info->insns[k] = ks[k]->insns; info->gprs[k] = ks[k]->kernel.gpr_count; } 568 } 569 for (unsigned k = 0; k < 3 && rc == OMEGA_GPU_GDN_OK; k++) { 570 OmegaGpuLaunch L = { 571 .code_va = ks[k]->code.va, .gpr_count = omega_bw_gpr_declared(ks[k]->kernel.gpr_count), 572 .threads_x = thr[k], .threads_y = 1, .grid_x = gx[k], .grid_y = gy[k], 573 .num_elements = nout, 574 .args = args[k], .n_args = nargs[k], .timeout_ms = MARKER_TIMEOUT_MS, 575 .shared_bytes = sh[k], 576 }; 577 if (!omega_bw_gpr_declared_ok(L.gpr_count, ks[k]->kernel.gpr_count)) { omega_gpu_session_set_error("declared registers below peak + 2: refusing to launch"); rc = OMEGA_GPU_GDN_CHIP_FAIL; } 578 else if (rec) { if (omega_gpu_cmdlist_add(rec, &L) < 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; } 579 else if (omega_gpu_session_launch(&L, info ? &info->elapsed_ns[k] : NULL, NULL) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; 580 } 581 if (rc == OMEGA_GPU_GDN_OK && !rec && omega_gpu_session_sync_to_cpu(&g->pf.mem) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; 582 if (rc == OMEGA_GPU_GDN_OK && !rec) { 583 size_t unchanged = 0; 584 for (size_t i = 0; i < nout; i++) if (yo[i] == POISON_F32) unchanged++; 585 if (unchanged) { omega_gpu_session_set_error("gdn prefill: output poison survived"); rc = OMEGA_GPU_GDN_CHIP_FAIL; } 586 else memcpy(out, yo, nout * 4u); 587 } 588 if (rc == OMEGA_GPU_GDN_OK && out_va) *out_va = a_out; 589 UNLOCK(); 590 #undef UNLOCK 591 if (info) info->call_ns = omega_gpu_session_now_ns() - c0; 592 return rc; 593 } 594 595 int omega_gpu_gdn_prefill(OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, const float *qkv, const float *z, const float *ab, 596 float eps_l2, float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnPfInfo *info) { 597 return pf_core(g, layer, tokens, qkv, z, ab, eps_l2, eps_norm, in_scale, out_scale, out, info, NULL, 0, 0, 0, NULL); 598 } 599 600 int omega_gpu_gdn_prefill_record(OmegaGpuCmdList *cl, OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, uint64_t qkv_va, 601 uint64_t z_va, uint64_t ab_va, float eps_l2, float eps_norm, float in_scale, float out_scale, 602 uint64_t *out_va) { 603 if (!cl) return OMEGA_GPU_GDN_BAD_ARGS; 604 return pf_core(g, layer, tokens, NULL, NULL, NULL, eps_l2, eps_norm, in_scale, out_scale, NULL, NULL, cl, qkv_va, z_va, ab_va, out_va); 605 } 606 607 int omega_gpu_gdn_prefill_read_out(OmegaGpuGdn *g, uint32_t tokens, float *out) { 608 if (!g || !out || omega_gdn_admit_tokens(tokens)) return OMEGA_GPU_GDN_BAD_ARGS; 609 size_t off[PF_N]; 610 const size_t bytes = pf_layout(&g->sh, tokens, off); 611 omega_gpu_session_lock(); 612 int rc = (!g->pf.mem.cpu || g->pf.cap < bytes) ? OMEGA_GPU_GDN_BAD_ARGS 613 : omega_gpu_session_sync_to_cpu(&g->pf.mem) == 0 ? OMEGA_GPU_GDN_OK : OMEGA_GPU_GDN_CHIP_FAIL; 614 if (rc == OMEGA_GPU_GDN_OK) memcpy(out, (const uint8_t *)g->pf.mem.cpu + off[PF_OUT], (size_t)tokens * g->sh.vdim * 4u); 615 omega_gpu_session_unlock(); 616 return rc; 617 }