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_q38_graph.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* |
| 3 | * omega_gpu_q38_graph.c: one Qwen3.8-27B decode token as one command list (see the header). Records the FP8 / bf16 GEMV, |
| 4 | * GDN and hd256 attention APIs' *_record launches and the omega_blackwell_codegen_dec glue kernels at width 5120 into an |
| 5 | * omega_gpu_cmdlist. Buffers: buf from omega_gpu_session_alloc (GPU-uncached, nvos.h:1117) holds every region the host |
| 6 | * touches: x (before), token and logits (after), the tables; dbuf (GPU-cached, omega_gpu_mem_cached.h) holds the chip-only |
| 7 | * intermediates (see Layout). |
| 8 | */ |
| 9 | #include "omega_gpu_q38_graph.h" |
| 10 | #include "omega_gpu_cmdlist.h" |
| 11 | #include "omega_blackwell_codegen_dec.h" |
| 12 | #include "omega_blackwell_qmd.h" |
| 13 | #include "omega_gpu_session.h" |
| 14 | #include "omega_gpu_fmt.h" |
| 15 | #include "omega_gpu_mem_cached.h" |
| 16 | #include "cudapp_native.h" |
| 17 | #include "omega_gpu_graph_om.h" |
| 18 | #include "omega_gpu_graph_residue.h" |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | #define TIMEOUT_MS 20000ull |
| 23 | #define SCALE_2P64 18446744073709551616.0f |
| 24 | #define NEG_PAD (-1e30f) |
| 25 | #define TOKEN_POISON 0xffffffffu |
| 26 | #define H (g->hd) /* the model shape (config; 27B defaults): every use has the graph g in scope */ |
| 27 | #define I (g->in) |
| 28 | |
| 29 | /* glue kernels */ |
| 30 | enum { K_NORM, K_ADDNORM, K_SW, K_GATHER, K_GATE, K_AM0, K_AM1, K_SW_M1, K_GATHER_M1, K_GATE_M1, K_ADDNORM_M1, K_COPY, K_ADD, K_COUNT }; |
| 31 | #define COPY_ROW 256u /* K_COPY: GATHER rows of 256 floats through the identity table */ |
| 32 | |
| 33 | typedef struct { OmegaGpuMem code; uint32_t gpr; } Kern; |
| 34 | |
| 35 | /* byte offsets into two buffers, every region 256-byte aligned (RES loads 16-byte vectors). [0, split): buf, GPU-uncached, |
| 36 | * every region the host writes or reads. [split, bytes): dbuf, the chip-only intermediates, GPU-cached unless |
| 37 | * AIEN_OMEGA_Q38_BUF_CACHED=0 (measured 2026-10-19: every FP8 GEMV CTA reads the whole x' row, and uncached each of those |
| 38 | * reads goes to DRAM; cached, down 2100 -> 1735 us and mix 1596 -> 1458 us over 4 layers, runs/probe-buf). read_buf copies |
| 39 | * a dbuf region into stage with a chip launch (K_COPY) before the host reads it. */ |
| 40 | typedef struct { |
| 41 | size_t x, ab, hidden, logits, am_v, am_i, token, norms, fnorm, tabs, gtab, idtab, stage, split; |
| 42 | size_t xq, mix, agg, att, o, gu, act, bytes; |
| 43 | } Layout; |
| 44 | |
| 45 | struct OmegaGpuQ38Graph { |
| 46 | OmegaGpuQ38GraphConfig cfg; |
| 47 | /* the shape, resolved: hidden, inter, attention q / kv heads, GDN value heads; GDN qkv and qkv|z widths, a|b width, |
| 48 | * attention mix (q_proj|k_proj|v_proj) and kernel-row widths, mixer output width (see the header) */ |
| 49 | uint32_t hd, in, qh, kvh, gh, gqkv, gmix, ab, amix, aqkv, mout; |
| 50 | uint32_t am_ctas; /* stage-1 ARGMAX CTAs = ceil(vocab / 2048) */ |
| 51 | Layout lo; |
| 52 | OmegaGpuMem buf, dbuf; /* the host-visible regions; the chip-only regions (see Layout) */ |
| 53 | Kern k[K_COUNT]; |
| 54 | OmegaGpuCmdList *cl; |
| 55 | /* bf16 set (set_layer_bf16): mix / o / gu / dn are bf16 tensors (bmix ...) recorded with the bf16 GEMV, else FP8; |
| 56 | * bdn2: the down projection's second K half (bdn then holds the first) */ |
| 57 | struct { const OmegaGpuFp8Tensor *mix, *o, *gu, *dn; const OmegaGpuBf16Tensor *ab, *bmix, *bo, *bgu, *bdn, *bdn2; uint32_t kind_index; int attn, set, bf16; } layer[OMEGA_GPU_Q38_MAX_LAYERS]; |
| 58 | int final_set; |
| 59 | const OmegaGpuFp8Tensor *head8; /* set_lm_head_fp8: FP8 lm_head (hidden is then written x 2^64, read() undoes it) */ |
| 60 | const OmegaGpuFp8Tensor *exact8; |
| 61 | OmegaGpuMem exact_mem; |
| 62 | Kern exact_scale, exact_bound; |
| 63 | uint64_t exact_info[192]; |
| 64 | int exact_on; |
| 65 | /* token state */ |
| 66 | int in_token, failed; |
| 67 | int xq_layer; /* layer whose input-norm x' sits in xq, -1 none */ |
| 68 | int hidden_ready; |
| 69 | uint64_t t_begin; |
| 70 | OmegaGpuDecodeGraphStats acc; |
| 71 | int flush_each, mutant; |
| 72 | int stamps; |
| 73 | uint32_t n_kind, tagged_in_list, n_stamp; |
| 74 | uint8_t kind[OMEGA_GPU_CMDLIST_MAX_LAUNCHES]; |
| 75 | uint64_t stamp[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1]; |
| 76 | }; |
| 77 | |
| 78 | #ifdef CUDAPP_MIGRATION_PENDING_C_GRAPHS |
| 79 | static size_t al(size_t v) { return omega_graph_addr(0,v,0,0,0,0,0); } |
| 80 | #endif |
| 81 | static uint64_t va_of(const OmegaGpuQ38Graph *g, size_t off) { return omega_graph_addr(1,g->buf.va,g->dbuf.va,g->lo.split,off,0,0); } |
| 82 | static void *cpu_of(const OmegaGpuQ38Graph *g, size_t off) { return (uint8_t *)g->buf.cpu + off; } /* host regions only (off < split) */ |
| 83 | static void put64(uint32_t *a, unsigned off, uint64_t v) { a[(off - 0x380) / 4] = (uint32_t)v; a[(off - 0x380) / 4 + 1] = (uint32_t)(v >> 32); } |
| 84 | #ifdef CUDAPP_MIGRATION_PENDING_C_GRAPHS |
| 85 | static void putf(uint32_t *a, unsigned off, float f) { memcpy(&a[(off - 0x380) / 4], &f, 4); } |
| 86 | #endif |
| 87 | static int fail(OmegaGpuQ38Graph *g, const char *what, int rc) { |
| 88 | if (g) g->failed = 1; |
| 89 | if (what) omega_gpu_session_set_error(what); |
| 90 | return rc; |
| 91 | } |
| 92 | |
| 93 | int omega_q38_gather_table(uint32_t q_heads, uint32_t kv_heads, uint32_t *tab) { |
| 94 | if (!tab || q_heads == 0 || q_heads > 24u || kv_heads == 0 || q_heads % kv_heads != 0) return -1; |
| 95 | #if CUDAPP_MIGRATION_PENDING_GRAPH_RESIDUE |
| 96 | const uint32_t R = OMEGA_GPU_Q38_ROTARY, QP = q_heads * 512u, KP = QP + kv_heads * 256u; |
| 97 | const uint32_t qn = q_heads * 256u, kn = kv_heads * 256u, qkv = qn + 2u * kn, mix = (q_heads + kv_heads) * 512u; |
| 98 | for (uint32_t n = 0; n < mix; n++) { |
| 99 | uint32_t s; |
| 100 | if (n < qn) s = (n / 256u) * 512u + omega_dattn256_perm_slot(n % 256u, R); /* q, permuted */ |
| 101 | else if (n < qn + kn) s = QP + ((n - qn) / 256u) * 256u + omega_dattn256_perm_slot(n % 256u, R); /* k, permuted */ |
| 102 | else if (n < qkv) s = KP + (n - qn - kn); /* v */ |
| 103 | else s = ((n - qkv) / 256u) * 512u + 256u + (n - qkv) % 256u; /* gate */ |
| 104 | tab[n] = s; |
| 105 | } |
| 106 | #else |
| 107 | omega_graph_table(1, q_heads, kv_heads, tab, (q_heads + kv_heads) * 512u); |
| 108 | #endif |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | /* lock held */ |
| 113 | static int kern_build(Kern *k, const OmegaDecSpec *s) { |
| 114 | BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); |
| 115 | if (!prog) return OMEGA_GPU_DGRAPH_CODEGEN_FAIL; |
| 116 | int rc = OMEGA_GPU_DGRAPH_OK; |
| 117 | size_t need = 0; |
| 118 | if (omega_blackwell_codegen_dec_prog(s, prog) != 0 || (need = omega_bw_program_code_bytes(prog)) == 0) rc = OMEGA_GPU_DGRAPH_CODEGEN_FAIL; |
| 119 | else if (omega_gpu_session_alloc_code(need, &k->code) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 120 | else { |
| 121 | OmegaBlackwellKernel kk; |
| 122 | if (omega_blackwell_kernel_encode_into(prog, k->code.cpu, need, &kk) != 0 || kk.code_size != need) rc = OMEGA_GPU_DGRAPH_CODEGEN_FAIL; |
| 123 | else if (omega_gpu_session_sync_to_gpu(&k->code) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 124 | else { k->gpr = omega_bw_gpr_declared(kk.gpr_count); if (k->gpr < 64u) k->gpr = 64u; } /* as the decode graph */ |
| 125 | if (rc != OMEGA_GPU_DGRAPH_OK) { omega_gpu_session_free(&k->code); memset(k, 0, sizeof *k); } |
| 126 | } |
| 127 | omega_gpu_host_free(prog); |
| 128 | return rc; |
| 129 | } |
| 130 | |
| 131 | void omega_gpu_q38_graph_free(OmegaGpuQ38Graph *g) { |
| 132 | if (!g) return; |
| 133 | if (g->cl) omega_gpu_cmdlist_free(g->cl); |
| 134 | omega_gpu_session_lock(); |
| 135 | for (unsigned i = 0; i < K_COUNT; i++) if (g->k[i].code.cpu) omega_gpu_session_free(&g->k[i].code); |
| 136 | if (g->buf.cpu) omega_gpu_session_free(&g->buf); |
| 137 | if (g->dbuf.cpu) omega_gpu_session_free(&g->dbuf); |
| 138 | if (g->exact_mem.cpu) omega_gpu_session_free(&g->exact_mem); |
| 139 | if (g->exact_scale.code.cpu) omega_gpu_session_free(&g->exact_scale.code); |
| 140 | if (g->exact_bound.code.cpu) omega_gpu_session_free(&g->exact_bound.code); |
| 141 | omega_gpu_session_unlock(); |
| 142 | omega_gpu_host_free(g); |
| 143 | } |
| 144 | |
| 145 | int omega_gpu_q38_graph_create(const OmegaGpuQ38GraphConfig *cfg, OmegaGpuQ38Graph **out) { |
| 146 | if (out) *out = NULL; |
| 147 | if (!cfg || !out || !cfg->gdn || !cfg->attn || !cfg->lm_head) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 148 | const uint32_t ctas = omega_graph_config(cfg->layers, OMEGA_GPU_Q38_MAX_LAYERS, cfg->vocab, |
| 149 | OMEGA_DEC_ARGMAX_SPAN, 1, 1, cfg->attn_mask, 1); |
| 150 | if (!ctas) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 151 | /* the shape: zero fields take the 27B values; the widths are capped at the 27B ones (the RES kernel's 1024-lane rows, |
| 152 | * the SWIGLU / GATE 256-lane grids, the attention kernel's q / kv group) */ |
| 153 | const uint64_t sc[16] = {cfg->hidden, cfg->inter, cfg->q_heads, cfg->kv_heads, cfg->gdn_heads}; |
| 154 | uint64_t sd[16]; |
| 155 | if (!omega_graph_q38(sc, sd)) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 156 | const uint32_t hd=sd[0], in=sd[1], qh=sd[2], kvh=sd[3], gh=sd[4]; |
| 157 | OmegaGpuDecodeAttn256CacheView av; |
| 158 | if (omega_gpu_decode_attn256_cache_view(cfg->attn, 0, &av) != 0 || av.q_heads != qh || av.kv_heads != kvh) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 159 | uint32_t n = 0, k = 0; |
| 160 | omega_gpu_bf16_tensor_shape(cfg->lm_head, &n, &k); |
| 161 | if (!omega_graph_shape(1, n, k, 1, cfg->vocab, hd)) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 162 | OmegaGpuQ38Graph *g = omega_gpu_host_calloc(1, sizeof *g); |
| 163 | if (!g) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 164 | g->cfg = *cfg; |
| 165 | g->hd = hd; g->in = in; g->qh = qh; g->kvh = kvh; g->gh = gh; |
| 166 | g->gqkv=sd[5]; g->gmix=sd[6]; g->ab=sd[7]; |
| 167 | g->amix=sd[8]; g->aqkv=sd[9]; g->mout=sd[10]; |
| 168 | g->am_ctas = ctas; |
| 169 | g->xq_layer = -1; |
| 170 | uint64_t layer_counts[2] = {0}; |
| 171 | for (uint32_t l = 0; l < cfg->layers; l++) |
| 172 | omega_graph_layer(cfg->attn_mask, l, layer_counts, &g->layer[l].attn, &g->layer[l].kind_index); |
| 173 | Layout *L = &g->lo; |
| 174 | #ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS |
| 175 | const uint64_t lc[32]={2,1,cfg->layers,g->am_ctas,H,I,1,g->gqkv,g->mout,g->gmix,g->ab,g->amix,g->mout,0,0,OMEGA_DEC_ARGMAX_SPAN,0,0,COPY_ROW}; |
| 176 | uint64_t ld[64],lr=0; |
| 177 | if(omega_mg_layout(&lr,lc,32,ld,64)!=OMEGA_OK||lr!=1){omega_gpu_host_free(g);return OMEGA_GPU_DGRAPH_BAD_ARGS;} |
| 178 | #define FIELD(f,i) L->f=ld[i] |
| 179 | FIELD(x,0);FIELD(ab,1);FIELD(hidden,2);FIELD(logits,3);FIELD(am_v,4);FIELD(am_i,5);FIELD(token,6);FIELD(norms,7);FIELD(fnorm,8);FIELD(tabs,9); |
| 180 | FIELD(gtab,10);FIELD(idtab,11);FIELD(stage,12);FIELD(xq,13);FIELD(mix,14);FIELD(agg,15);FIELD(att,16);FIELD(o,17);FIELD(gu,18);FIELD(act,19);FIELD(split,60);FIELD(bytes,61); |
| 181 | #undef FIELD |
| 182 | #else |
| 183 | size_t o = 0; |
| 184 | #define REG(f, bytes) do { L->f = o; o = al(o + (bytes)); } while (0) |
| 185 | REG(x, H * 4); REG(ab, g->ab * 4); REG(hidden, H * 4); |
| 186 | REG(logits, (size_t)g->am_ctas * OMEGA_DEC_ARGMAX_SPAN * 4); REG(am_v, OMEGA_DEC_ARGMAX_SPAN * 4); REG(am_i, OMEGA_DEC_ARGMAX_SPAN * 4); |
| 187 | REG(token, 16); REG(norms, (size_t)cfg->layers * 2 * H * 4); REG(fnorm, H * 4); REG(tabs, (size_t)cfg->layers * 4 * 16 + 16); /* + the FP8 lm_head entry */ |
| 188 | REG(gtab, g->amix * 4); REG(idtab, COPY_ROW * 4); REG(stage, 2 * I * 4); |
| 189 | L->split = o; |
| 190 | REG(xq, H * 4); REG(mix, g->gmix * 4); REG(agg, g->amix * 4); |
| 191 | REG(att, g->mout * 4); REG(o, H * 4); REG(gu, 2 * I * 4); REG(act, I * 4); |
| 192 | #undef REG |
| 193 | L->bytes = o; |
| 194 | #endif |
| 195 | int rc = OMEGA_GPU_DGRAPH_OK; |
| 196 | if (omega_gpu_cmdlist_create(OMEGA_GPU_CMDLIST_MAX_LAUNCHES, &g->cl) != 0) { omega_gpu_q38_graph_free(g); return OMEGA_GPU_DGRAPH_CHIP_FAIL; } |
| 197 | omega_gpu_session_lock(); |
| 198 | const char *pbc = getenv("AIEN_OMEGA_Q38_BUF_CACHED"); |
| 199 | int (*const dalloc)(size_t, OmegaGpuMem *) = (pbc && pbc[0] == '0' && pbc[1] == 0) ? omega_gpu_session_alloc : omega_gpu_session_alloc_cached; |
| 200 | if (!omega_gpu_session_open() || omega_gpu_session_alloc(L->split, &g->buf) != 0 || dalloc(L->bytes - L->split, &g->dbuf) != 0) |
| 201 | rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 202 | const OmegaDecSpec spec[K_COUNT] = { |
| 203 | [K_NORM] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_NORM, .width = H }, |
| 204 | [K_ADDNORM] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM, .width = H }, |
| 205 | [K_SW] = { .kind = OMEGA_DEC_SWIGLU, .width = I }, |
| 206 | [K_GATHER] = { .kind = OMEGA_DEC_GATHER, .width = g->amix, .stride = g->amix }, |
| 207 | [K_GATE] = { .kind = OMEGA_DEC_GATE }, |
| 208 | [K_AM0] = { .kind = OMEGA_DEC_ARGMAX, .mode = 0 }, |
| 209 | [K_AM1] = { .kind = OMEGA_DEC_ARGMAX, .mode = 1 }, |
| 210 | [K_SW_M1] = { .kind = OMEGA_DEC_SWIGLU, .width = I, .mutant = 1 }, |
| 211 | [K_GATHER_M1] = { .kind = OMEGA_DEC_GATHER, .width = g->amix, .stride = g->amix, .mutant = 1 }, |
| 212 | [K_GATE_M1] = { .kind = OMEGA_DEC_GATE, .mutant = 1 }, |
| 213 | [K_ADDNORM_M1] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM, .width = H, .mutant = 1 }, |
| 214 | [K_COPY] = { .kind = OMEGA_DEC_GATHER, .width = COPY_ROW, .stride = COPY_ROW }, |
| 215 | [K_ADD] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_ADD, .width = H }, /* x += a: the first half of a split bf16 down */ |
| 216 | }; |
| 217 | for (unsigned i = 0; i < K_COUNT && rc == OMEGA_GPU_DGRAPH_OK; i++) rc = kern_build(&g->k[i], &spec[i]); |
| 218 | if (rc == OMEGA_GPU_DGRAPH_OK) { |
| 219 | memset(g->buf.cpu, 0, L->split); |
| 220 | memset(g->dbuf.cpu, 0, L->bytes - L->split); /* fresh; every region is written by the chip before a launch reads it */ |
| 221 | uint32_t *id = cpu_of(g, L->idtab); |
| 222 | for (uint32_t i = 0; i < COPY_ROW; i++) id[i] = i; |
| 223 | float *lg = cpu_of(g, L->logits), *amv = cpu_of(g, L->am_v); |
| 224 | for (size_t i = cfg->vocab; i < (size_t)g->am_ctas * OMEGA_DEC_ARGMAX_SPAN; i++) lg[i] = NEG_PAD; /* lm_head writes [0, vocab) only */ |
| 225 | for (size_t i = 0; i < OMEGA_DEC_ARGMAX_SPAN; i++) amv[i] = NEG_PAD; /* stage 1 writes [0, ctas) only */ |
| 226 | (void)omega_q38_gather_table(qh, kvh, cpu_of(g, L->gtab)); /* the shape was checked above */ |
| 227 | if (omega_gpu_session_sync_to_gpu(&g->buf) != 0 || omega_gpu_session_sync_to_gpu(&g->dbuf) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 228 | } |
| 229 | omega_gpu_session_unlock(); |
| 230 | if (rc != OMEGA_GPU_DGRAPH_OK) { omega_gpu_q38_graph_free(g); return rc; } |
| 231 | *out = g; |
| 232 | return OMEGA_GPU_DGRAPH_OK; |
| 233 | } |
| 234 | |
| 235 | static int shape_is(const OmegaGpuFp8Tensor *t, uint32_t n, uint32_t k) { |
| 236 | uint32_t te = 0, tn = 0, tk = 0; if (t) omega_gpu_fp8_tensor_shape(t, &te, &tn, &tk); |
| 237 | return t && omega_graph_shape(te, tn, tk, 1, n, k); |
| 238 | } |
| 239 | |
| 240 | /* the norms and the layer's table row (t: the FP8 entries, or zeros for a bf16 layer) */ |
| 241 | static int layer_common(OmegaGpuQ38Graph *g, uint32_t layer, const uint64_t t[8], const float *in_norm, const float *post_norm) { |
| 242 | omega_gpu_session_lock(); |
| 243 | memcpy((uint8_t *)cpu_of(g, g->lo.tabs) + (size_t)layer * 64u, t, 8 * sizeof *t); |
| 244 | memcpy((float *)cpu_of(g, g->lo.norms) + (size_t)layer * 2 * H, in_norm, H * 4u); |
| 245 | memcpy((float *)cpu_of(g, g->lo.norms) + (size_t)layer * 2 * H + H, post_norm, H * 4u); |
| 246 | int rc = omega_gpu_session_sync_to_gpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 247 | omega_gpu_session_unlock(); |
| 248 | return rc; |
| 249 | } |
| 250 | |
| 251 | static int ab_ok(const OmegaGpuQ38Graph *g, int attn, const OmegaGpuBf16Tensor *ab) { |
| 252 | if (attn) return ab == NULL; |
| 253 | uint32_t n = 0, k = 0; omega_gpu_bf16_tensor_shape(ab, &n, &k); |
| 254 | return ab != NULL && n == g->ab && k == H; |
| 255 | } |
| 256 | |
| 257 | int omega_gpu_q38_graph_set_layer(OmegaGpuQ38Graph *g, uint32_t layer, const OmegaGpuFp8Tensor *mix, const OmegaGpuBf16Tensor *ab, |
| 258 | const OmegaGpuFp8Tensor *o, const OmegaGpuFp8Tensor *gate_up, const OmegaGpuFp8Tensor *down, |
| 259 | const float *in_norm, const float *post_norm) { |
| 260 | if (!g || layer >= g->cfg.layers || !in_norm || !post_norm || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 261 | const int attn = g->layer[layer].attn; |
| 262 | if (!shape_is(mix, attn ? g->amix : g->gmix, H) || !shape_is(o, H, g->mout) || |
| 263 | !shape_is(gate_up, 2 * I, H) || !shape_is(down, H, I)) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 264 | if (!ab_ok(g, attn, ab)) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 265 | uint64_t t[8]; |
| 266 | if (omega_gpu_fp8_tensor_entry(mix, 0, &t[0], &t[1]) != 0 || omega_gpu_fp8_tensor_entry(o, 0, &t[2], &t[3]) != 0 || |
| 267 | omega_gpu_fp8_tensor_entry(gate_up, 0, &t[4], &t[5]) != 0 || omega_gpu_fp8_tensor_entry(down, 0, &t[6], &t[7]) != 0) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 268 | int rc = layer_common(g, layer, t, in_norm, post_norm); |
| 269 | if (rc == OMEGA_GPU_DGRAPH_OK) { |
| 270 | g->layer[layer].mix = mix; g->layer[layer].ab = ab; g->layer[layer].o = o; g->layer[layer].gu = gate_up; g->layer[layer].dn = down; |
| 271 | g->layer[layer].bmix = g->layer[layer].bo = g->layer[layer].bgu = g->layer[layer].bdn = g->layer[layer].bdn2 = NULL; |
| 272 | g->layer[layer].bf16 = 0; g->layer[layer].set = 1; |
| 273 | } |
| 274 | return rc; |
| 275 | } |
| 276 | |
| 277 | static int bf_shape_is(const OmegaGpuBf16Tensor *t, uint32_t n, uint32_t k) { |
| 278 | uint32_t tn = 0, tk = 0; if (t) omega_gpu_bf16_tensor_shape(t, &tn, &tk); |
| 279 | return t && omega_graph_shape(1, tn, tk, 1, n, k); |
| 280 | } |
| 281 | |
| 282 | /* the bf16 GEMV stages its whole f32 x row in shared memory, which the launch descriptor caps at 32 KiB: k <= 8192 */ |
| 283 | #define BF16_X_MAX_K (OMEGA_BW_QMD_MAX_SHARED_BYTES / 4u) |
| 284 | |
| 285 | int omega_gpu_q38_graph_set_layer_bf16(OmegaGpuQ38Graph *g, uint32_t layer, const OmegaGpuBf16Tensor *mix, const OmegaGpuBf16Tensor *ab, |
| 286 | const OmegaGpuBf16Tensor *o, const OmegaGpuBf16Tensor *gate_up, const OmegaGpuBf16Tensor *down, |
| 287 | const OmegaGpuBf16Tensor *down2, const float *in_norm, const float *post_norm) { |
| 288 | if (!g || layer >= g->cfg.layers || !in_norm || !post_norm || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 289 | const int attn = g->layer[layer].attn; |
| 290 | /* down: whole [H][I] when I fits one x stage, else split in two K halves (down = columns 0..I/2, down2 = I/2..I) */ |
| 291 | const uint32_t kd = down2 ? I / 2u : I; |
| 292 | if (!bf_shape_is(mix, attn ? g->amix : g->gmix, H) || !bf_shape_is(o, H, g->mout) || !bf_shape_is(gate_up, 2 * I, H) || |
| 293 | !bf_shape_is(down, H, kd) || (down2 && !bf_shape_is(down2, H, kd)) || !ab_ok(g, attn, ab)) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 294 | if (H > BF16_X_MAX_K || g->mout > BF16_X_MAX_K || kd > BF16_X_MAX_K || (down2 && I % 512u)) { |
| 295 | omega_gpu_session_set_error("q38 graph: a bf16 projection's K exceeds the GEMV x stage (8192; split down in two halves)"); |
| 296 | return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 297 | } |
| 298 | /* pin the plain kernels the record calls use (widths hidden, mixer out, down K): a record must not compile into an |
| 299 | * occupied slot */ |
| 300 | if (omega_gpu_bf16_prepare(H) != 0 || omega_gpu_bf16_prepare(g->mout) != 0 || omega_gpu_bf16_prepare(kd) != 0) { |
| 301 | omega_gpu_session_set_error("q38 graph: bf16 GEMV kernels could not be prepared"); |
| 302 | return OMEGA_GPU_DGRAPH_CODEGEN_FAIL; |
| 303 | } |
| 304 | const uint64_t t[8] = { 0 }; |
| 305 | int rc = layer_common(g, layer, t, in_norm, post_norm); |
| 306 | if (rc == OMEGA_GPU_DGRAPH_OK) { |
| 307 | g->layer[layer].mix = g->layer[layer].o = g->layer[layer].gu = g->layer[layer].dn = NULL; |
| 308 | g->layer[layer].ab = ab; g->layer[layer].bmix = mix; g->layer[layer].bo = o; g->layer[layer].bgu = gate_up; g->layer[layer].bdn = down; |
| 309 | g->layer[layer].bdn2 = down2; |
| 310 | g->layer[layer].bf16 = 1; g->layer[layer].set = 1; |
| 311 | } |
| 312 | return rc; |
| 313 | } |
| 314 | |
| 315 | int omega_gpu_q38_graph_set_final_norm(OmegaGpuQ38Graph *g, const float *final_norm) { |
| 316 | if (!g || !final_norm || g->in_token || g->exact8) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 317 | omega_gpu_session_lock(); |
| 318 | memcpy(cpu_of(g, g->lo.fnorm), final_norm, H * 4u); |
| 319 | int rc = omega_gpu_session_sync_to_gpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 320 | omega_gpu_session_unlock(); |
| 321 | if (rc == OMEGA_GPU_DGRAPH_OK) g->final_set = 1; |
| 322 | return rc; |
| 323 | } |
| 324 | |
| 325 | int omega_gpu_q38_graph_set_lm_head_fp8(OmegaGpuQ38Graph *g, const OmegaGpuFp8Tensor *head) { |
| 326 | if (!g || g->in_token || g->exact8) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 327 | if (!head) { g->head8 = NULL; return OMEGA_GPU_DGRAPH_OK; } |
| 328 | uint64_t t[2]; |
| 329 | if (!shape_is(head, g->cfg.vocab, H) || omega_gpu_fp8_tensor_entry(head, 0, &t[0], &t[1]) != 0) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 330 | omega_gpu_session_lock(); |
| 331 | memcpy((uint8_t *)cpu_of(g, g->lo.tabs) + (size_t)g->cfg.layers * 64u, t, sizeof t); |
| 332 | int rc = omega_gpu_session_sync_to_gpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 333 | omega_gpu_session_unlock(); |
| 334 | if (rc == OMEGA_GPU_DGRAPH_OK) g->head8 = head; |
| 335 | return rc; |
| 336 | } |
| 337 | |
| 338 | /* Thin resource adapter. Omega owns the envelope, scratch layout, code and schedule. */ |
| 339 | uint64_t omega_gpu_q38_exact_extra_bytes(void) { |
| 340 | /* H is the graph's hidden width. This query has no graph, so it uses the 27B maximum the exact head was measured at. */ |
| 341 | const uint8_t unused[OMEGA_GPU_Q38_HIDDEN * 4]={0};uint64_t m[16]={3,248320},q[192]={0},result=0; |
| 342 | return !omega_q38_runtime(&result,m,16,unused,sizeof unused,q,192) && result==1 ? q[10] : 0; |
| 343 | } |
| 344 | int omega_gpu_q38_graph_set_exact_head(OmegaGpuQ38Graph *g, const OmegaGpuFp8Tensor *head, |
| 345 | const float *coef, uint32_t roundings) { |
| 346 | if (!g || !head || !coef || g->in_token || !g->final_set || g->exact8 || g->head8 || |
| 347 | !shape_is(head, g->cfg.vocab, H) || g->cfg.eps != 1e-6f) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 348 | uint64_t m[16]={1,g->cfg.vocab},result=0,entry[2]; |
| 349 | if (omega_q38_runtime(&result,m,16,cpu_of(g,g->lo.fnorm),H*4,g->exact_info,192) || result!=1 || |
| 350 | roundings!=g->exact_info[2] || omega_gpu_fp8_tensor_entry(head,0,&entry[0],&entry[1])) |
| 351 | return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 352 | uint8_t code[16384];uint64_t bytes=0; |
| 353 | if (omega_q38_emit(&bytes,code,sizeof code) || !bytes || bytes>sizeof code) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 354 | const OmegaDecSpec scale={.kind=OMEGA_DEC_XBOUND,.mode=1}; |
| 355 | omega_gpu_session_lock(); |
| 356 | int rc=kern_build(&g->exact_scale,&scale); |
| 357 | if (!rc && omega_gpu_session_alloc_code(bytes,&g->exact_bound.code)) rc=OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 358 | if (!rc) { |
| 359 | memcpy(g->exact_bound.code.cpu,code,bytes);g->exact_bound.gpr=64; |
| 360 | if(omega_gpu_session_sync_to_gpu(&g->exact_bound.code) || |
| 361 | omega_gpu_session_alloc_cached(g->exact_info[1],&g->exact_mem)) rc=OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 362 | } |
| 363 | if (!rc) { |
| 364 | uint8_t *b=g->exact_mem.cpu;memset(b,0,g->exact_info[1]); |
| 365 | memcpy(b+g->exact_info[5],coef,(size_t)g->cfg.vocab*4); |
| 366 | float *lo=(float *)(b+g->exact_info[6]); |
| 367 | for(size_t i=g->cfg.vocab;i<g->exact_info[9];i++)lo[i]=NEG_PAD; |
| 368 | memcpy((uint8_t *)cpu_of(g,g->lo.tabs)+(size_t)g->cfg.layers*64,entry,sizeof entry); |
| 369 | if(omega_gpu_session_sync_to_gpu(&g->exact_mem) || omega_gpu_session_sync_to_gpu(&g->buf)) rc=OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 370 | } |
| 371 | if(rc) { |
| 372 | if(g->exact_mem.cpu)omega_gpu_session_free(&g->exact_mem); |
| 373 | if(g->exact_scale.code.cpu)omega_gpu_session_free(&g->exact_scale.code); |
| 374 | if(g->exact_bound.code.cpu)omega_gpu_session_free(&g->exact_bound.code); |
| 375 | memset(&g->exact_mem,0,sizeof g->exact_mem); |
| 376 | memset(&g->exact_scale,0,sizeof g->exact_scale);memset(&g->exact_bound,0,sizeof g->exact_bound); |
| 377 | } else {g->exact8=head;g->exact_on=1;} |
| 378 | omega_gpu_session_unlock();return rc; |
| 379 | } |
| 380 | void omega_gpu_q38_graph_set_exact_head_on(OmegaGpuQ38Graph *g,int on) { |
| 381 | if(g && !g->in_token)g->exact_on=on && g->exact8; |
| 382 | } |
| 383 | int omega_gpu_q38_graph_exact_head_active(const OmegaGpuQ38Graph *g) {return g && g->exact8 && g->exact_on;} |
| 384 | |
| 385 | void omega_gpu_q38_graph_set_flush_each(OmegaGpuQ38Graph *g, int on) { if (g) g->flush_each = on ? 1 : 0; } |
| 386 | void omega_gpu_q38_graph_test_set_mutant(OmegaGpuQ38Graph *g, int mutant) { if (g) g->mutant = mutant; } |
| 387 | |
| 388 | /* ---- the list ---- */ |
| 389 | |
| 390 | static OmegaGpuCmdListOpts list_opts(const OmegaGpuQ38Graph *g) { |
| 391 | OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); |
| 392 | o.mode = OMEGA_GPU_CMDLIST_PCAS; o.barrier = (OmegaGpuCmdListBarrier)omega_gpu_decode_graph_barrier(); o.timeout_ms = TIMEOUT_MS; o.stamps = g->stamps; |
| 393 | return o; |
| 394 | } |
| 395 | |
| 396 | static int submit_now(OmegaGpuQ38Graph *g) { |
| 397 | if (omega_gpu_cmdlist_count(g->cl) == 0) return OMEGA_GPU_DGRAPH_OK; |
| 398 | OmegaGpuCmdListOpts o = list_opts(g); |
| 399 | OmegaGpuCmdListStats st; memset(&st, 0, sizeof st); |
| 400 | int rc = omega_gpu_cmdlist_submit(g->cl, &o, &st); |
| 401 | g->acc.launches += st.launches; g->acc.submits += st.submits; g->acc.chip_ns += st.elapsed_ns; g->acc.sems_done += st.sems_done; |
| 402 | if (rc == 0 && g->stamps) { |
| 403 | uint64_t tmp[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1]; |
| 404 | int cnt = omega_gpu_cmdlist_stamps(g->cl, tmp, OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1); |
| 405 | if (cnt < 0) rc = -1; |
| 406 | else { int from = 0; |
| 407 | if (!omega_graph_timeline(g->n_stamp, (uint32_t)cnt, OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1u, &from)) { |
| 408 | omega_gpu_session_set_error("q38 graph: timeline full"); rc = -1; |
| 409 | } else for (int i = from; i < cnt; i++) g->stamp[g->n_stamp++] = tmp[i]; |
| 410 | } |
| 411 | } |
| 412 | g->tagged_in_list = 0; |
| 413 | omega_gpu_cmdlist_reset(g->cl); |
| 414 | if (rc != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); |
| 415 | if (st.sems_done != st.launches) { |
| 416 | char e[128]; omega_gpu_fmt(e, sizeof e, "q38 graph: %u of %u launch semaphores DONE (first missing %d)", st.sems_done, st.launches, st.first_missing); |
| 417 | return fail(g, e, OMEGA_GPU_DGRAPH_CHIP_FAIL); |
| 418 | } |
| 419 | return OMEGA_GPU_DGRAPH_OK; |
| 420 | } |
| 421 | |
| 422 | static void tag(OmegaGpuQ38Graph *g, uint8_t kind) { |
| 423 | const uint32_t n = omega_gpu_cmdlist_count(g->cl); |
| 424 | for (; g->tagged_in_list < n && g->n_kind < OMEGA_GPU_CMDLIST_MAX_LAUNCHES; g->tagged_in_list++) g->kind[g->n_kind++] = kind; |
| 425 | } |
| 426 | |
| 427 | static int after_op(OmegaGpuQ38Graph *g, uint8_t kind, int api_rc) { |
| 428 | if (api_rc != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); |
| 429 | tag(g, kind); |
| 430 | return g->flush_each ? submit_now(g) : OMEGA_GPU_DGRAPH_OK; |
| 431 | } |
| 432 | |
| 433 | static int add_glue(OmegaGpuQ38Graph *g, uint8_t kind, int ki, uint32_t gx, uint32_t gy, size_t elems, const uint32_t *args, uint32_t n_args) { |
| 434 | const Kern *k = &g->k[ki]; |
| 435 | OmegaGpuLaunch L; memset(&L, 0, sizeof L); |
| 436 | L.code_va = k->code.va; L.gpr_count = k->gpr; L.threads_x = OMEGA_DEC_THREADS; L.threads_y = 1; L.grid_x = gx; L.grid_y = gy; |
| 437 | L.num_elements = elems; L.args = args; L.n_args = n_args; L.timeout_ms = TIMEOUT_MS; |
| 438 | return after_op(g, kind, omega_gpu_cmdlist_add(g->cl, &L) < 0 ? -1 : 0); |
| 439 | } |
| 440 | |
| 441 | #ifdef CUDAPP_MIGRATION_PENDING_C_GRAPHS |
| 442 | /* RES at the hidden width: NORM (x -> y) or ADD|NORM (x += a; y = norm(x)) */ |
| 443 | static int rec_res(OmegaGpuQ38Graph *g, int ki, uint64_t x, uint64_t a, uint64_t nw, uint64_t y, float scale) { |
| 444 | uint32_t w[OMEGA_DEC_RES_WORDS]; memset(w, 0, sizeof w); |
| 445 | put64(w, OMEGA_DEC_RES_X, x); put64(w, OMEGA_DEC_RES_A, a); put64(w, OMEGA_DEC_RES_NW, nw); put64(w, OMEGA_DEC_RES_Y, y); |
| 446 | putf(w, OMEGA_DEC_RES_EPS, g->cfg.eps); putf(w, OMEGA_DEC_RES_SCALE, scale); |
| 447 | return add_glue(g, ki == K_NORM ? OMEGA_GPU_Q38_K_NORM : OMEGA_GPU_Q38_K_RES, ki, 1, 1, H, w, OMEGA_DEC_RES_WORDS); |
| 448 | } |
| 449 | #endif |
| 450 | |
| 451 | static int g_overlap_ab = -1; |
| 452 | void omega_gpu_q38_graph_set_overlap_ab(int on) { g_overlap_ab = on ? 1 : 0; } |
| 453 | static int q38_overlap_ab(void) { |
| 454 | if (g_overlap_ab < 0) { const char *e = getenv("AIEN_OMEGA_Q38_OVERLAP_AB"); g_overlap_ab = (e && e[0] == '0' && e[1] == 0) ? 0 : 1; } |
| 455 | return g_overlap_ab; |
| 456 | } |
| 457 | |
| 458 | static int rec_fp8(OmegaGpuQ38Graph *g, uint8_t kind, const OmegaGpuFp8Tensor *t, uint64_t tab, uint64_t x, uint64_t y) { |
| 459 | return after_op(g, kind, omega_gpu_fp8_gemv_grouped_record(g->cl, t, 1, tab, x, 1, y)); |
| 460 | } |
| 461 | /* one projection of a layer: FP8 (table row entry tab) or, for a set_layer_bf16 layer, the bf16 GEMV (weights 2^-64 scaled) */ |
| 462 | static int rec_proj(OmegaGpuQ38Graph *g, uint8_t kind, const OmegaGpuFp8Tensor *t8, const OmegaGpuBf16Tensor *tb, uint64_t tab, uint64_t x, uint64_t y) { |
| 463 | return tb ? after_op(g, kind, omega_gpu_bf16_gemv_record(g->cl, tb, x, y)) : rec_fp8(g, kind, t8, tab, x, y); |
| 464 | } |
| 465 | |
| 466 | int omega_gpu_q38_graph_begin_token(OmegaGpuQ38Graph *g, const float *x_embed) { |
| 467 | if (!g || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 468 | omega_gpu_session_lock(); |
| 469 | g->t_begin = omega_gpu_session_now_ns(); |
| 470 | g->in_token = 1; g->failed = 0; g->xq_layer = -1; g->hidden_ready = 0; |
| 471 | memset(&g->acc, 0, sizeof g->acc); |
| 472 | g->n_kind = 0; g->tagged_in_list = 0; g->n_stamp = 0; |
| 473 | omega_gpu_cmdlist_reset(g->cl); |
| 474 | if (!omega_gpu_session_open()) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); |
| 475 | uint32_t *tok = cpu_of(g, g->lo.token); |
| 476 | tok[0] = TOKEN_POISON; tok[1] = TOKEN_POISON; |
| 477 | if (x_embed) memcpy(cpu_of(g, g->lo.x), x_embed, H * 4u); |
| 478 | if (omega_gpu_session_sync_to_gpu(&g->buf) != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); |
| 479 | return OMEGA_GPU_DGRAPH_OK; |
| 480 | } |
| 481 | |
| 482 | int omega_gpu_q38_graph_record_layer(OmegaGpuQ38Graph *g, uint32_t l, uint32_t pos) { |
| 483 | if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 484 | if (g->failed) return OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 485 | if (l >= g->cfg.layers || !g->layer[l].set || g->hidden_ready || (l + 1 == g->cfg.layers && !g->final_set)) |
| 486 | return fail(g, "q38 graph: layer not set, out of order, or final norm missing", OMEGA_GPU_DGRAPH_BAD_ARGS); |
| 487 | const uint64_t x = va_of(g, g->lo.x), xq = va_of(g, g->lo.xq), mix = va_of(g, g->lo.mix), agg = va_of(g, g->lo.agg); |
| 488 | const uint64_t att = va_of(g, g->lo.att), o = va_of(g, g->lo.o), gu = va_of(g, g->lo.gu), act = va_of(g, g->lo.act); |
| 489 | const uint64_t in_norm = va_of(g, g->lo.norms) + (uint64_t)l * 2 * H * 4u, post_norm = in_norm + H * 4u, tab = va_of(g, g->lo.tabs) + (uint64_t)l * 64u; |
| 490 | const uint32_t ki = g->layer[l].kind_index; |
| 491 | #ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS |
| 492 | const uint64_t cfg[16] = {2, g->xq_layer != (int)l, g->layer[l].attn, q38_overlap_ab(), |
| 493 | g->layer[l].bdn2 != NULL, 0, l, g->cfg.layers, pos, g->mutant, g->head8 != NULL, |
| 494 | g->flush_each, H, I, 1, g->am_ctas}; |
| 495 | uint64_t values[64] = {x,xq,mix,agg,att,o,gu,act,0,in_norm,va_of(g,g->lo.fnorm), |
| 496 | tab,va_of(g,g->lo.ab),va_of(g,g->lo.gtab),va_of(g,g->lo.hidden),att}; |
| 497 | values[20]=post_norm; values[23]=(uint64_t)g->gqkv*4u; values[24]=(uint64_t)g->aqkv*4u; |
| 498 | values[25]=g->amix; values[26]=g->mout; values[27]=omega_graph_fbits(g->cfg.eps); |
| 499 | values[28]=omega_graph_fbits(SCALE_2P64); values[29]=omega_graph_fbits(1.0f); |
| 500 | static const int kernels[25] = {K_NORM,K_ADDNORM,K_ADDNORM,K_SW,K_GATHER,K_GATE, |
| 501 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,K_SW_M1,K_GATHER_M1,K_GATE_M1,K_ADDNORM_M1,K_ADD}; |
| 502 | int rc=OMEGA_GPU_DGRAPH_OK; |
| 503 | for (uint64_t phase=0; rc==OMEGA_GPU_DGRAPH_OK; phase++) { |
| 504 | uint64_t node[64]; |
| 505 | if (!omega_graph_next(cfg,values,node,phase)) return fail(g,NULL,OMEGA_GPU_DGRAPH_CHIP_FAIL); |
| 506 | if (node[0]==255) break; |
| 507 | uint32_t words[32]; omega_graph_args(node,words); |
| 508 | switch(node[0]) { |
| 509 | case 0: break; |
| 510 | case 1: case 5: case 7: case 9: case 12: case 14: { |
| 511 | static const uint8_t kinds[15] = {[1]=OMEGA_GPU_Q38_K_NORM,[5]=OMEGA_GPU_Q38_K_GATHER, |
| 512 | [7]=OMEGA_GPU_Q38_K_GATE,[9]=OMEGA_GPU_Q38_K_RES,[12]=OMEGA_GPU_Q38_K_SWIGLU, |
| 513 | [14]=OMEGA_GPU_Q38_K_RES}; |
| 514 | rc=add_glue(g,kinds[node[0]],kernels[node[1]],node[2],node[3],node[4],words,node[5]); break; |
| 515 | } |
| 516 | case 2: { |
| 517 | int mrc=g->layer[l].bf16 ? omega_gpu_bf16_gemv_record(g->cl,g->layer[l].bmix,node[11],node[12]) |
| 518 | : omega_gpu_fp8_gemv_grouped_record(g->cl,g->layer[l].mix,node[7],node[10],node[11],node[8],node[12]); |
| 519 | if (!mrc && node[6] && omega_gpu_cmdlist_mark_independent(g->cl)) mrc=-1; |
| 520 | rc=after_op(g,OMEGA_GPU_Q38_K_MIX,mrc); break; |
| 521 | } |
| 522 | case 3: rc=after_op(g,OMEGA_GPU_Q38_K_AB,omega_gpu_bf16_gemv_record(g->cl,g->layer[l].ab,node[11],node[12])); break; |
| 523 | case 4: rc=after_op(g,OMEGA_GPU_Q38_K_GDN,omega_gpu_gdn_record(g->cl,g->cfg.gdn,ki,node[11],node[12],node[13], |
| 524 | g->cfg.eps_l2,g->cfg.eps,1.0f,SCALE_2P64,&values[15])); break; |
| 525 | case 6: rc=after_op(g,OMEGA_GPU_Q38_K_ATTN,omega_gpu_decode_attn256_record(g->cl,g->cfg.attn,ki,node[13],node[11], |
| 526 | g->cfg.eps,SCALE_2P64,node[12])); break; |
| 527 | case 8: rc=rec_proj(g,OMEGA_GPU_Q38_K_O,g->layer[l].o,g->layer[l].bo,node[10],node[11],node[12]); break; |
| 528 | case 11: rc=rec_proj(g,OMEGA_GPU_Q38_K_GATE_UP,g->layer[l].gu,g->layer[l].bgu,node[10],node[11],node[12]); break; |
| 529 | case 13: rc=rec_proj(g,OMEGA_GPU_Q38_K_DOWN,g->layer[l].dn,g->layer[l].bdn,node[10],node[11],node[12]); break; |
| 530 | case 16: rc=rec_proj(g,OMEGA_GPU_Q38_K_DOWN,NULL,g->layer[l].bdn2,node[10],node[11],node[12]); break; |
| 531 | default: return fail(g,NULL,OMEGA_GPU_DGRAPH_CHIP_FAIL); |
| 532 | } |
| 533 | } |
| 534 | if (rc==OMEGA_GPU_DGRAPH_OK) { if(l+1==g->cfg.layers) g->hidden_ready=1; else g->xq_layer=(int)l+1; } |
| 535 | #else |
| 536 | int rc = OMEGA_GPU_DGRAPH_OK; |
| 537 | if (g->xq_layer != (int)l) rc = rec_res(g, K_NORM, x, 0, in_norm, xq, SCALE_2P64); |
| 538 | /* GDN layers: the a|b GEMV (latency-bound, ~18 us) is recorded first and the mix GEMV marked independent of it: both only |
| 539 | * read xq and write their own buffers, so the two grids overlap; the GDN step after them waits for both (normal barrier). |
| 540 | * AIEN_OMEGA_Q38_OVERLAP_AB=0 or set_overlap_ab(0): the old order, mix then a|b, each behind a barrier. */ |
| 541 | const int overlap = !g->layer[l].attn && q38_overlap_ab(); |
| 542 | if (overlap && rc == OMEGA_GPU_DGRAPH_OK) rc = after_op(g, OMEGA_GPU_Q38_K_AB, omega_gpu_bf16_gemv_record(g->cl, g->layer[l].ab, xq, va_of(g, g->lo.ab))); |
| 543 | if (rc == OMEGA_GPU_DGRAPH_OK) { |
| 544 | int mrc = g->layer[l].bf16 ? omega_gpu_bf16_gemv_record(g->cl, g->layer[l].bmix, xq, mix) |
| 545 | : omega_gpu_fp8_gemv_grouped_record(g->cl, g->layer[l].mix, 1, tab, xq, 1, mix); |
| 546 | if (mrc == 0 && overlap && !g->flush_each && omega_gpu_cmdlist_mark_independent(g->cl) != 0) mrc = -1; |
| 547 | rc = after_op(g, OMEGA_GPU_Q38_K_MIX, mrc); |
| 548 | } |
| 549 | uint64_t mixer_out = att; |
| 550 | if (!g->layer[l].attn) { |
| 551 | const uint64_t ab = va_of(g, g->lo.ab); |
| 552 | if (!overlap && rc == OMEGA_GPU_DGRAPH_OK) rc = after_op(g, OMEGA_GPU_Q38_K_AB, omega_gpu_bf16_gemv_record(g->cl, g->layer[l].ab, xq, ab)); |
| 553 | if (rc == OMEGA_GPU_DGRAPH_OK) |
| 554 | rc = after_op(g, OMEGA_GPU_Q38_K_GDN, omega_gpu_gdn_record(g->cl, g->cfg.gdn, ki, mix, mix + g->gqkv * 4u, ab, |
| 555 | g->cfg.eps_l2, g->cfg.eps, 1.0f, SCALE_2P64, &mixer_out)); |
| 556 | } else { |
| 557 | uint32_t w[OMEGA_DEC_GA_WORDS]; memset(w, 0, sizeof w); |
| 558 | put64(w, OMEGA_DEC_GA_IN, mix); put64(w, OMEGA_DEC_GA_TAB, va_of(g, g->lo.gtab)); put64(w, OMEGA_DEC_GA_OUT, agg); |
| 559 | if (rc == OMEGA_GPU_DGRAPH_OK) |
| 560 | rc = add_glue(g, OMEGA_GPU_Q38_K_GATHER, g->mutant == 2 ? K_GATHER_M1 : K_GATHER, (g->amix + OMEGA_DEC_THREADS - 1) / OMEGA_DEC_THREADS, 1, |
| 561 | g->amix, w, OMEGA_DEC_GA_WORDS); |
| 562 | if (rc == OMEGA_GPU_DGRAPH_OK) |
| 563 | rc = after_op(g, OMEGA_GPU_Q38_K_ATTN, omega_gpu_decode_attn256_record(g->cl, g->cfg.attn, ki, pos, agg, g->cfg.eps, SCALE_2P64, att)); |
| 564 | uint32_t w2[OMEGA_DEC_GT_WORDS]; memset(w2, 0, sizeof w2); |
| 565 | put64(w2, OMEGA_DEC_GT_A, att); put64(w2, OMEGA_DEC_GT_G, agg + g->aqkv * 4u); put64(w2, OMEGA_DEC_GT_OUT, att); |
| 566 | w2[(OMEGA_DEC_GT_N - 0x380) / 4] = g->mout; |
| 567 | if (rc == OMEGA_GPU_DGRAPH_OK) |
| 568 | rc = add_glue(g, OMEGA_GPU_Q38_K_GATE, g->mutant == 3 ? K_GATE_M1 : K_GATE, g->mout / OMEGA_DEC_THREADS, 1, |
| 569 | g->mout, w2, OMEGA_DEC_GT_WORDS); |
| 570 | } |
| 571 | if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_proj(g, OMEGA_GPU_Q38_K_O, g->layer[l].o, g->layer[l].bo, tab + 16u, mixer_out, o); |
| 572 | if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_res(g, g->mutant == 4 ? K_ADDNORM_M1 : K_ADDNORM, x, o, post_norm, xq, SCALE_2P64); |
| 573 | if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_proj(g, OMEGA_GPU_Q38_K_GATE_UP, g->layer[l].gu, g->layer[l].bgu, tab + 32u, xq, gu); |
| 574 | uint32_t w3[OMEGA_DEC_SW_WORDS]; memset(w3, 0, sizeof w3); |
| 575 | put64(w3, OMEGA_DEC_SW_GU, gu); put64(w3, OMEGA_DEC_SW_ACT, act); putf(w3, OMEGA_DEC_SW_SCALE, SCALE_2P64); |
| 576 | if (rc == OMEGA_GPU_DGRAPH_OK) rc = add_glue(g, OMEGA_GPU_Q38_K_SWIGLU, g->mutant == 1 ? K_SW_M1 : K_SW, I / OMEGA_DEC_THREADS, 1, I, w3, OMEGA_DEC_SW_WORDS); |
| 577 | if (rc == OMEGA_GPU_DGRAPH_OK && g->layer[l].bdn2) { |
| 578 | /* split bf16 down: x += down(act[0 .. I/2]), then the second half lands in o for the ADDNORM below */ |
| 579 | rc = rec_proj(g, OMEGA_GPU_Q38_K_DOWN, NULL, g->layer[l].bdn, 0, act, o); |
| 580 | if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_res(g, K_ADD, x, o, 0, 0, 1.0f); |
| 581 | if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_proj(g, OMEGA_GPU_Q38_K_DOWN, NULL, g->layer[l].bdn2, 0, act + (uint64_t)(I / 2u) * 4u, o); |
| 582 | } else if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_proj(g, OMEGA_GPU_Q38_K_DOWN, g->layer[l].dn, g->layer[l].bdn, tab + 48u, act, o); |
| 583 | if (rc == OMEGA_GPU_DGRAPH_OK) { |
| 584 | const int last = l + 1 == g->cfg.layers; |
| 585 | const uint64_t nw = last ? va_of(g, g->lo.fnorm) : in_norm + 2 * H * 4u; |
| 586 | rc = rec_res(g, K_ADDNORM, x, o, nw, last ? va_of(g, g->lo.hidden) : xq, last && !g->head8 ? 1.0f : SCALE_2P64); |
| 587 | if (rc == OMEGA_GPU_DGRAPH_OK) { if (last) g->hidden_ready = 1; else g->xq_layer = (int)l + 1; } |
| 588 | } |
| 589 | #endif |
| 590 | return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc); |
| 591 | } |
| 592 | |
| 593 | static int rec_exact_head(OmegaGpuQ38Graph *g,uint64_t hidden,uint64_t logits) { |
| 594 | uint64_t m[16]={2,g->cfg.vocab,g->exact_info[0],g->exact_mem.va,hidden,logits, |
| 595 | va_of(g,g->lo.am_v),va_of(g,g->lo.am_i),va_of(g,g->lo.tabs)+(uint64_t)g->cfg.layers*64}; |
| 596 | uint64_t q[192]={0},count=0; |
| 597 | if(omega_q38_runtime(&count,m,16,cpu_of(g,g->lo.fnorm),H*4,q,192) || count!=6) |
| 598 | return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 599 | for(uint64_t i=0;i<count;i++) { |
| 600 | const uint64_t *d=q+i*16;uint32_t args[OMEGA_DEC_RES_WORDS]={0}; |
| 601 | int rc=0;const Kern *k=NULL; |
| 602 | switch(d[0]) { |
| 603 | case 1: |
| 604 | put64(args,OMEGA_DEC_XB_S,d[3]);put64(args,OMEGA_DEC_XB_LO,d[5]); |
| 605 | args[(OMEGA_DEC_XB_N-0x380)/4]=(uint32_t)d[8];k=&g->exact_scale;break; |
| 606 | case 2: |
| 607 | rc=omega_gpu_fp8_gemv_grouped_record(g->cl,g->exact8,1,d[3],d[4],1,d[5]);break; |
| 608 | case 3: |
| 609 | put64(args,896,d[3]);put64(args,904,d[4]);put64(args,912,d[5]);put64(args,920,d[6]); |
| 610 | args[8]=(uint32_t)d[7];args[9]=(uint32_t)d[8];k=&g->exact_bound;break; |
| 611 | case 4: case 5: |
| 612 | put64(args,OMEGA_DEC_AM_V,d[3]);put64(args,OMEGA_DEC_AM_I,d[4]); |
| 613 | put64(args,OMEGA_DEC_AM_OV,d[5]);put64(args,OMEGA_DEC_AM_OI,d[6]);k=&g->k[d[0]==4?K_AM0:K_AM1];break; |
| 614 | case 6: |
| 615 | rc=omega_gpu_bf16_gemv_mask_record(g->cl,g->cfg.lm_head,d[3],d[5],d[4],d[6]);break; |
| 616 | default: return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 617 | } |
| 618 | if(k) { |
| 619 | OmegaGpuLaunch launch={0};launch.code_va=k->code.va;launch.gpr_count=k->gpr; |
| 620 | launch.threads_x=OMEGA_DEC_THREADS;launch.threads_y=1;launch.grid_x=(uint32_t)d[1];launch.grid_y=1; |
| 621 | launch.num_elements=d[2];launch.args=args; |
| 622 | launch.n_args=d[0]==1?OMEGA_DEC_XB_WORDS:d[0]==3?10:OMEGA_DEC_AM_WORDS;launch.timeout_ms=TIMEOUT_MS; |
| 623 | rc=omega_gpu_cmdlist_add(g->cl,&launch)<0?-1:0; |
| 624 | } |
| 625 | rc=after_op(g,OMEGA_GPU_Q38_K_LM_HEAD,rc); |
| 626 | if(rc)return rc; |
| 627 | } |
| 628 | return OMEGA_GPU_DGRAPH_OK; |
| 629 | } |
| 630 | |
| 631 | int omega_gpu_q38_graph_record_final(OmegaGpuQ38Graph *g) { |
| 632 | if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 633 | if (g->failed) return OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 634 | if (!g->final_set) return fail(g, "q38 graph: final norm not set", OMEGA_GPU_DGRAPH_BAD_ARGS); |
| 635 | const uint64_t x = va_of(g, g->lo.x), hidden = va_of(g, g->lo.hidden), logits = va_of(g, g->lo.logits), token = va_of(g, g->lo.token); |
| 636 | int rc = OMEGA_GPU_DGRAPH_OK; |
| 637 | #ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS |
| 638 | const uint64_t cfg[16]={g->hidden_ready,g->head8!=NULL,0,g->mutant,g->cfg.vocab,g->am_ctas,H, |
| 639 | omega_graph_fbits(g->cfg.eps),omega_graph_fbits(SCALE_2P64),omega_graph_fbits(1.0f),0,g->exact_on}; |
| 640 | const uint64_t values[16]={x,hidden,logits,token,va_of(g,g->lo.fnorm), |
| 641 | va_of(g,g->lo.tabs)+(uint64_t)g->cfg.layers*64u,va_of(g,g->lo.am_v),va_of(g,g->lo.am_i)}; |
| 642 | static const int kernels[7]={K_NORM,0,0,0,0,K_AM0,K_AM1}; |
| 643 | for(uint64_t phase=0;rc==OMEGA_GPU_DGRAPH_OK;phase++){ |
| 644 | uint64_t node[64],result=0;uint32_t words[32]; |
| 645 | if(omega_mg_final(&result,cfg,16,values,16,node,64,phase)!=OMEGA_OK||result!=1)return fail(g,NULL,OMEGA_GPU_DGRAPH_CHIP_FAIL); |
| 646 | if(node[0]==255)break; |
| 647 | omega_graph_args(node,words); |
| 648 | switch(node[0]){ |
| 649 | case 0:break; |
| 650 | case 1:case 8:rc=add_glue(g,node[0]==1?OMEGA_GPU_Q38_K_NORM:OMEGA_GPU_Q38_K_ARGMAX, |
| 651 | kernels[node[1]],node[2],node[3],node[4],words,node[5]);break; |
| 652 | case 7:rc=g->head8?rec_fp8(g,OMEGA_GPU_Q38_K_LM_HEAD,g->head8,node[10],node[11],node[12]): |
| 653 | after_op(g,OMEGA_GPU_Q38_K_LM_HEAD,omega_gpu_bf16_gemv_record(g->cl,g->cfg.lm_head,node[11],node[12]));break; |
| 654 | case 10:rc=rec_exact_head(g,node[11],node[12]);break; |
| 655 | default:return fail(g,NULL,OMEGA_GPU_DGRAPH_CHIP_FAIL); |
| 656 | } |
| 657 | } |
| 658 | #else |
| 659 | if (!g->hidden_ready) rc = rec_res(g, K_NORM, x, 0, va_of(g, g->lo.fnorm), hidden, g->head8 ? SCALE_2P64 : 1.0f); |
| 660 | if (rc == OMEGA_GPU_DGRAPH_OK && g->exact_on) |
| 661 | rc = rec_exact_head(g,hidden,logits); |
| 662 | else if (rc == OMEGA_GPU_DGRAPH_OK) |
| 663 | rc = g->head8 ? rec_fp8(g, OMEGA_GPU_Q38_K_LM_HEAD, g->head8, va_of(g, g->lo.tabs) + (uint64_t)g->cfg.layers * 64u, hidden, logits) |
| 664 | : after_op(g, OMEGA_GPU_Q38_K_LM_HEAD, omega_gpu_bf16_gemv_record(g->cl, g->cfg.lm_head, hidden, logits)); |
| 665 | uint32_t a0[OMEGA_DEC_AM_WORDS], a1[OMEGA_DEC_AM_WORDS]; memset(a0, 0, sizeof a0); memset(a1, 0, sizeof a1); |
| 666 | const uint64_t amv = va_of(g, g->lo.am_v), ami = va_of(g, g->lo.am_i); |
| 667 | put64(a0, OMEGA_DEC_AM_V, logits); put64(a0, OMEGA_DEC_AM_I, 0); put64(a0, OMEGA_DEC_AM_OV, amv); put64(a0, OMEGA_DEC_AM_OI, ami); |
| 668 | put64(a1, OMEGA_DEC_AM_V, amv); put64(a1, OMEGA_DEC_AM_I, ami); put64(a1, OMEGA_DEC_AM_OV, token + 4u); put64(a1, OMEGA_DEC_AM_OI, token); |
| 669 | if (rc == OMEGA_GPU_DGRAPH_OK) rc = add_glue(g, OMEGA_GPU_Q38_K_ARGMAX, K_AM0, g->am_ctas, 1, (size_t)g->am_ctas * OMEGA_DEC_ARGMAX_SPAN, a0, OMEGA_DEC_AM_WORDS); |
| 670 | if (rc == OMEGA_GPU_DGRAPH_OK) rc = add_glue(g, OMEGA_GPU_Q38_K_ARGMAX, K_AM1, 1, 1, OMEGA_DEC_ARGMAX_SPAN, a1, OMEGA_DEC_AM_WORDS); |
| 671 | #endif |
| 672 | return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc); |
| 673 | } |
| 674 | |
| 675 | int omega_gpu_q38_graph_end_token(OmegaGpuQ38Graph *g, uint32_t *token, OmegaGpuDecodeGraphStats *st) { |
| 676 | if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 677 | int rc = g->failed ? OMEGA_GPU_DGRAPH_CHIP_FAIL : OMEGA_GPU_DGRAPH_OK; |
| 678 | const uint64_t t_rec = omega_gpu_session_now_ns(); |
| 679 | if (rc == OMEGA_GPU_DGRAPH_OK) rc = submit_now(g); |
| 680 | if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_decode_attn256_counters_check(g->cfg.attn) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); |
| 681 | if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_session_sync_to_cpu(&g->buf) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); |
| 682 | if (rc == OMEGA_GPU_DGRAPH_OK) { |
| 683 | const uint32_t t = ((const uint32_t *)cpu_of(g, g->lo.token))[0]; |
| 684 | if (t == TOKEN_POISON || t >= g->cfg.vocab) { |
| 685 | char e[96]; omega_gpu_fmt(e, sizeof e, "q38 graph: token output %u not written or out of range", t); |
| 686 | rc = fail(g, e, OMEGA_GPU_DGRAPH_CHIP_FAIL); |
| 687 | } else if (token) *token = t; |
| 688 | } |
| 689 | omega_gpu_cmdlist_reset(g->cl); |
| 690 | g->in_token = 0; |
| 691 | const uint64_t t_end = omega_gpu_session_now_ns(); |
| 692 | if (st) { *st = g->acc; st->record_ns = (g->flush_each ? t_end : t_rec) - g->t_begin; st->total_ns = t_end - g->t_begin; } |
| 693 | omega_gpu_session_unlock(); |
| 694 | return rc; |
| 695 | } |
| 696 | |
| 697 | int omega_gpu_q38_graph_token(OmegaGpuQ38Graph *g, const float *x_embed, uint32_t pos, uint32_t *token, OmegaGpuDecodeGraphStats *st) { |
| 698 | if (!g) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 699 | if (omega_gpu_q38_graph_begin_token(g, x_embed) == OMEGA_GPU_DGRAPH_BAD_ARGS) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 700 | for (uint32_t l = 0; l < g->cfg.layers && !g->failed; l++) (void)omega_gpu_q38_graph_record_layer(g, l, pos); |
| 701 | if (!g->failed) (void)omega_gpu_q38_graph_record_final(g); |
| 702 | return omega_gpu_q38_graph_end_token(g, token, st); |
| 703 | } |
| 704 | |
| 705 | int omega_gpu_q38_graph_read(OmegaGpuQ38Graph *g, float *x, float *hidden, float *logits) { |
| 706 | if (!g || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 707 | omega_gpu_session_lock(); |
| 708 | int rc = omega_gpu_session_sync_to_cpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 709 | if (rc == OMEGA_GPU_DGRAPH_OK) { |
| 710 | if (x) memcpy(x, cpu_of(g, g->lo.x), H * 4u); |
| 711 | if (hidden) memcpy(hidden, cpu_of(g, g->lo.hidden), H * 4u); |
| 712 | if (hidden && g->head8) { |
| 713 | #ifdef CUDAPP_MIGRATION_PENDING_C_GRAPH_HEAD_SCALE |
| 714 | for (unsigned i = 0; i < H; i++) hidden[i] *= 0x1p-64f; |
| 715 | #else |
| 716 | uint64_t result = 0; |
| 717 | if (omega_ghscale(&result, hidden, H) != OMEGA_OK || result) |
| 718 | rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 719 | #endif |
| 720 | } |
| 721 | if (logits) memcpy(logits, cpu_of(g, g->lo.logits), (size_t)g->cfg.vocab * 4u); |
| 722 | } |
| 723 | omega_gpu_session_unlock(); |
| 724 | return rc; |
| 725 | } |
| 726 | |
| 727 | int omega_gpu_q38_graph_read_buf(OmegaGpuQ38Graph *g, int which, float *out, uint32_t n) { |
| 728 | if (!g || g->in_token || !out || which < 0 || which >= OMEGA_GPU_Q38_BUF_COUNT) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 729 | const struct { size_t off; uint32_t cap; } b[OMEGA_GPU_Q38_BUF_COUNT] = { |
| 730 | [OMEGA_GPU_Q38_BUF_X] = { g->lo.x, H }, [OMEGA_GPU_Q38_BUF_XQ] = { g->lo.xq, H }, [OMEGA_GPU_Q38_BUF_MIX] = { g->lo.mix, g->gmix }, |
| 731 | [OMEGA_GPU_Q38_BUF_AB] = { g->lo.ab, g->ab }, [OMEGA_GPU_Q38_BUF_AGG] = { g->lo.agg, g->amix }, |
| 732 | [OMEGA_GPU_Q38_BUF_ATT] = { g->lo.att, g->mout }, [OMEGA_GPU_Q38_BUF_O] = { g->lo.o, H }, |
| 733 | [OMEGA_GPU_Q38_BUF_GU] = { g->lo.gu, 2 * I }, [OMEGA_GPU_Q38_BUF_ACT] = { g->lo.act, I }, [OMEGA_GPU_Q38_BUF_HIDDEN] = { g->lo.hidden, H }, |
| 734 | }; |
| 735 | if (n > b[which].cap) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 736 | omega_gpu_session_lock(); |
| 737 | int rc = OMEGA_GPU_DGRAPH_OK; |
| 738 | size_t from = b[which].off; |
| 739 | if (from >= g->lo.split && n) { |
| 740 | /* a chip-only region: K_COPY moves ceil(n / 256) rows of 256 floats into stage (every cap is a multiple of 256) */ |
| 741 | uint32_t w[32]; memset(w, 0, sizeof w); |
| 742 | #ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS |
| 743 | const uint64_t c[16]={2,0,0,0,0,0,COPY_ROW,n},v[16]={va_of(g,from),va_of(g,g->lo.idtab),va_of(g,g->lo.stage)}; |
| 744 | uint64_t node[64],result=0; |
| 745 | if(omega_mg_aux(&result,c,16,v,16,node,64)!=OMEGA_OK||result!=1){omega_gpu_session_unlock();return OMEGA_GPU_DGRAPH_CHIP_FAIL;} |
| 746 | omega_graph_args(node,w); |
| 747 | #else |
| 748 | put64(w, OMEGA_DEC_GA_IN, va_of(g, from)); put64(w, OMEGA_DEC_GA_TAB, va_of(g, g->lo.idtab)); put64(w, OMEGA_DEC_GA_OUT, va_of(g, g->lo.stage)); |
| 749 | #endif |
| 750 | const Kern *k = &g->k[K_COPY]; |
| 751 | OmegaGpuLaunch L; memset(&L, 0, sizeof L); |
| 752 | L.code_va = k->code.va; L.gpr_count = k->gpr; L.threads_x = OMEGA_DEC_THREADS; L.threads_y = 1; |
| 753 | #ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS |
| 754 | L.grid_x=node[2];L.grid_y=node[3];L.num_elements=node[4];L.n_args=node[5]; |
| 755 | #else |
| 756 | L.grid_x = 1; L.grid_y = (n + COPY_ROW - 1) / COPY_ROW;L.num_elements = (size_t)L.grid_y * COPY_ROW;L.n_args = OMEGA_DEC_GA_WORDS; |
| 757 | #endif |
| 758 | L.args = w; L.timeout_ms = TIMEOUT_MS; |
| 759 | if (!omega_gpu_session_open() || omega_gpu_session_launch(&L, NULL, NULL) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 760 | from = g->lo.stage; |
| 761 | } |
| 762 | if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_session_sync_to_cpu(&g->buf) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 763 | if (rc == OMEGA_GPU_DGRAPH_OK) memcpy(out, cpu_of(g, from), (size_t)n * 4u); |
| 764 | omega_gpu_session_unlock(); |
| 765 | return rc; |
| 766 | } |
| 767 | |
| 768 | /* ---- timeline ---- */ |
| 769 | void omega_gpu_q38_graph_set_stamps(OmegaGpuQ38Graph *g, int on) { if (g && !g->in_token) g->stamps = on ? 1 : 0; } |
| 770 | |
| 771 | int omega_gpu_q38_graph_stamps(const OmegaGpuQ38Graph *g, uint64_t *stamps, uint8_t *kinds, uint32_t cap) { |
| 772 | if (!g || g->in_token || !stamps || !kinds) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 773 | if (!g->stamps || g->n_stamp != g->n_kind + 1u) { omega_gpu_session_set_error("q38 graph: no timeline for the last token (stamps off or it failed)"); return OMEGA_GPU_DGRAPH_BAD_ARGS; } |
| 774 | if (cap < g->n_stamp) { omega_gpu_session_set_error("q38 graph: timeline buffers too small"); return OMEGA_GPU_DGRAPH_BAD_ARGS; } |
| 775 | memcpy(stamps, g->stamp, (size_t)g->n_stamp * sizeof stamps[0]); |
| 776 | memcpy(kinds, g->kind, (size_t)g->n_kind); |
| 777 | return (int)g->n_kind; |
| 778 | } |
| 779 | |
| 780 | const char *omega_gpu_q38_graph_kind_name(uint8_t kind) { |
| 781 | static const char *const names[OMEGA_GPU_Q38_K_COUNT] = { "norm", "mix", "ab", "gdn", "gather", "attn", "gate", "o_proj", "res", |
| 782 | "gate_up", "swiglu", "down", "lm_head", "argmax" }; |
| 783 | return kind < OMEGA_GPU_Q38_K_COUNT ? names[kind] : "?"; |
| 784 | } |