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