Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/omega_gpu_q38_graph.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / omega_gpu_q38_graph.c` 1042 lines ยท 67433 bytes Raw Blame History 1 /* 2 * omega_gpu_q38_graph.c: Qwen3.8-27B (and 9B) dense decode graph as one command list (see the header). Records the FP8 / 3 * bf16 GEMV (and grouped FP8 GEMM for batched multi-head strokes), GDN and hd256 attention APIs' *_record launches and the 4 * omega_blackwell_codegen_dec glue kernels at width H into an omega_gpu_cmdlist. Buffers: buf from omega_gpu_session_alloc 5 * (GPU-uncached, nvos.h:1117) holds every region the host touches: x (before), token and logits (after), the tables; dbuf 6 * (GPU-cached, omega_gpu_mem_cached.h) holds the chip-only intermediates (see Layout). 7 */ 8 #include "omega_gpu_q38_graph.h" 9 #include "omega_gpu_cmdlist.h" 10 #include "omega_blackwell_codegen_dec.h" 11 #include "omega_blackwell_qmd.h" 12 #include "omega_gpu_session.h" 13 #include "omega_gpu_fmt.h" 14 #include "omega_gpu_mem_cached.h" 15 #include "omega_gpu_fp8gemm_api.h" 16 #include "omega_native.h" 17 #include 18 #include 19 20 #define TIMEOUT_MS 20000ull 21 #define SCALE_2P64 `18446744073709551616`.0f 22 #define NEG_PAD (-1e30f) 23 #define TOKEN_POISON 0xffffffffu 24 #define TOKEN_STRIDE 256u /* bytes per head's token slot (two u32, 256-aligned) */ 25 #define SEGD_ROWS 4u /* dense segment entries per layer in the batch: mix, o, gate_up, down */ 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 { 31 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, 32 K_NORM_R, K_ADDNORM_R, K_ADDNORM_R_M1, K_PERM_XQ, K_PERM_O, K_PERM_ACT, K_XS, K_XB, K_XB_M1, K_COUNT 33 }; 34 #define COPY_ROW 256u /* K_COPY: GATHER rows of 256 floats through the identity table */ 35 36 typedef struct { OmegaGpuMem code; uint32_t gpr; } Kern; 37 38 /* byte offsets into two buffers, every region 256-byte aligned (RES loads 16-byte vectors). [0, split): buf, GPU-uncached, 39 * every region the host writes or reads. [split, bytes): dbuf, the chip-only intermediates, GPU-cached unless 40 * AIEN_OMEGA_Q38_BUF_CACHED=0. read_buf copies a dbuf region into stage with a chip launch (K_COPY) before the host reads it. */ 41 typedef struct { 42 size_t x, ab, hidden, logits, am_v, am_i, token, norms, fnorm, tabs, gtab, idtab, stage, segd, xh_seg, ptab, split; 43 size_t xq, mix, agg, att, o, gu, act; 44 /* batched dense decode (v2): N = heads laid out */ 45 size_t xp, mixN, abN, aggN, attN, op, oN, guN, actN, actp, bytes; 46 } Layout; 47 48 struct OmegaGpuQ38Graph { 49 OmegaGpuQ38GraphConfig cfg; 50 /* the shape, resolved: hidden, inter, attention q / kv heads, GDN value heads; GDN qkv and qkv|z widths, a|b width, 51 * attention mix (q_proj|k_proj|v_proj) and kernel-row widths, mixer output width (see the header) */ 52 uint32_t hd, in, qh, kvh, gh, gqkv, gmix, ab, amix, aqkv, mout; 53 uint32_t am_ctas; /* stage-1 ARGMAX CTAs = ceil(vocab / 2048) */ 54 Layout lo; 55 OmegaGpuMem buf, dbuf; /* the host-visible regions; the chip-only regions (see Layout) */ 56 Kern k[K_COUNT]; 57 OmegaGpuCmdList *cl; 58 /* bf16 set (set_layer_bf16): mix / o / gu / dn are bf16 tensors (bmix ...) recorded with the bf16 GEMV, else FP8; 59 * bdn2: the down projection's second K half (bdn then holds the first) */ 60 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]; 61 int final_set; 62 const OmegaGpuFp8Tensor *head8; /* set_lm_head_fp8: FP8 lm_head (hidden is then written x 2^64, read() undoes it) */ 63 /* heads (strokes): slot h of x, hidden, xq, logits and token; head 0's state objects are cfg.gdn / cfg.attn */ 64 uint32_t heads; /* slots laid out (create_heads); 1 = the plain graph */ 65 OmegaGpuGdn *gdn_h[OMEGA_GPU_Q38_MAX_HEADS]; 66 OmegaGpuDecodeAttn256Cache *attn_h[OMEGA_GPU_Q38_MAX_HEADS]; 67 struct { /* set_exact_head: the HUM L314 two-pass head (bf16 lm_head only) */ 68 const OmegaGpuFp8Tensor *screen; /* fp8b copy of lm_head (128 x 128 block scales) */ 69 OmegaGpuMem mem; /* h8 | s | c | hi | lo (am_ctas * 2048, padded) | tau | tab */ 70 uint64_t h8, s, c, hi, lo, tau, tab; 71 uint32_t roundings; 72 uint8_t on[OMEGA_GPU_Q38_MAX_HEADS]; 73 } xh; 74 /* token / stroke state */ 75 int in_token, failed; 76 uint32_t stroke_heads; /* heads in the open stroke */ 77 int xq_layer[OMEGA_GPU_Q38_MAX_HEADS]; /* per head: layer whose input-norm x' sits in head h's xq slot, -1 none */ 78 int hidden_ready[OMEGA_GPU_Q38_MAX_HEADS]; 79 uint64_t t_begin; 80 OmegaGpuDecodeGraphStats acc; 81 int flush_each, mutant; 82 int batch; /* 1: stroke() records each FP8 layer for every head at once (v2); set_batch */ 83 int stamps; 84 uint32_t n_kind, tagged_in_list, n_stamp; 85 uint8_t kind[OMEGA_GPU_CMDLIST_MAX_LAUNCHES]; 86 uint64_t stamp[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1]; 87 }; 88 89 static size_t al(size_t v) { return (v + 255u) & ~(size_t)255u; } 90 static uint64_t va_of(const OmegaGpuQ38Graph *g, size_t off) { return off < g->lo.split ? g->buf.va + off : g->dbuf.va + (off - g->lo.split); } 91 static void *cpu_of(const OmegaGpuQ38Graph *g, size_t off) { return (uint8_t *)g->buf.cpu + off; } /* host regions only (off < split) */ 92 static size_t x_of(const OmegaGpuQ38Graph *g, uint32_t h) { return g->lo.x + (size_t)h * H * 4u; } 93 static size_t hidden_of(const OmegaGpuQ38Graph *g, uint32_t h) { return g->lo.hidden + (size_t)h * H * 4u; } 94 static size_t xq_of(const OmegaGpuQ38Graph *g, uint32_t h) { return g->lo.xq + (size_t)h * H * 4u; } 95 static size_t logits_of(const OmegaGpuQ38Graph *g, uint32_t h) { return g->lo.logits + (size_t)h * g->am_ctas * OMEGA_DEC_ARGMAX_SPAN * 4u; } 96 static size_t amv_of(const OmegaGpuQ38Graph *g, uint32_t h) { return g->lo.am_v + (size_t)h * OMEGA_DEC_ARGMAX_SPAN * 4u; } 97 static size_t ami_of(const OmegaGpuQ38Graph *g, uint32_t h) { return g->lo.am_i + (size_t)h * OMEGA_DEC_ARGMAX_SPAN * 4u; } 98 static size_t token_of(const OmegaGpuQ38Graph *g, uint32_t h) { return g->lo.token + (size_t)h * TOKEN_STRIDE; } 99 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); } 100 static void putf(uint32_t *a, unsigned off, float f) { memcpy(&a[(off - 0x380) / 4], &f, 4); } 101 static int fail(OmegaGpuQ38Graph *g, const char *what, int rc) { 102 if (g) g->failed = 1; 103 if (what) omega_gpu_session_set_error(what); 104 return rc; 105 } 106 107 int omega_q38_gather_table(uint32_t q_heads, uint32_t kv_heads, uint32_t *tab) { 108 if (!tab || q_heads == 0 || q_heads > 24u || kv_heads == 0 || q_heads % kv_heads != 0) return -1; 109 const uint32_t R = OMEGA_GPU_Q38_ROTARY, QP = q_heads * 512u, KP = QP + kv_heads * 256u; 110 const uint32_t qn = q_heads * 256u, kn = kv_heads * 256u, qkv = qn + 2u * kn, mix = (q_heads + kv_heads) * 512u; 111 for (uint32_t n = 0; n < mix; n++) { 112 uint32_t s; 113 if (n < qn) s = (n / 256u) * 512u + omega_dattn256_perm_slot(n % 256u, R); /* q, permuted */ 114 else if (n < qn + kn) s = QP + ((n - qn) / 256u) * 256u + omega_dattn256_perm_slot(n % 256u, R); /* k, permuted */ 115 else if (n < qkv) s = KP + (n - qn - kn); /* v */ 116 else s = ((n - qkv) / 256u) * 512u + 256u + (n - qkv) % 256u; /* gate */ 117 tab[n] = s; 118 } 119 return 0; 120 } 121 122 /* GATHER table of the FP8 GEMM's x' lane layout (omega_fp8gemm_permute_x: lane_bytes 16 when k % 512 == 0 else 8): 123 * permuted index i = s0 + (w*32 + L)*4 + e holds source column c = s0 + L*lb + w*4 + e, so tab[i] = c. */ 124 void omega_q38_perm_table(uint32_t *tab, uint32_t k) { 125 if (!tab || k == 0 || k % 256u != 0) return; 126 const uint32_t lb = (k % 512u == 0) ? 16u : 8u; 127 const uint32_t step = 32u * lb, words = lb / 4u; 128 for (uint32_t s0 = 0; s0 < k; s0 += step) 129 for (uint32_t L = 0; L < 32u; L++) 130 for (uint32_t w = 0; w < words; w++) 131 for (uint32_t e = 0; e < 4u; e++) tab[s0 + (w * 32u + L) * 4u + e] = s0 + L * lb + w * 4u + e; 132 } 133 134 static void write_dense_segments(OmegaGpuQ38Graph *g, uint32_t heads); 135 136 /* lock held */ 137 static int kern_build(Kern *k, const OmegaDecSpec *s) { 138 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); 139 if (!prog) return OMEGA_GPU_DGRAPH_CODEGEN_FAIL; 140 int rc = OMEGA_GPU_DGRAPH_OK; 141 size_t need = 0; 142 if (omega_blackwell_codegen_dec_prog(s, prog) != 0 || (need = omega_bw_program_code_bytes(prog)) == 0) rc = OMEGA_GPU_DGRAPH_CODEGEN_FAIL; 143 else if (omega_gpu_session_alloc_code(need, &k->code) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; 144 else { 145 OmegaBlackwellKernel kk; 146 if (omega_blackwell_kernel_encode_into(prog, k->code.cpu, need, &kk) != 0 || kk.code_size != need) rc = OMEGA_GPU_DGRAPH_CODEGEN_FAIL; 147 else if (omega_gpu_session_sync_to_gpu(&k->code) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; 148 else { k->gpr = omega_bw_gpr_declared(kk.gpr_count); if (k->gpr < 64u) k->gpr = 64u; } /* as the decode graph */ 149 if (rc != OMEGA_GPU_DGRAPH_OK) { omega_gpu_session_free(&k->code); memset(k, 0, sizeof *k); } 150 } 151 omega_gpu_host_free(prog); 152 return rc; 153 } 154 155 void omega_gpu_q38_graph_free(OmegaGpuQ38Graph *g) { 156 if (!g) return; 157 if (g->cl) omega_gpu_cmdlist_free(g->cl); 158 omega_gpu_session_lock(); 159 for (unsigned i = 0; i < K_COUNT; i++) if (g->k[i].code.cpu) omega_gpu_session_free(&g->k[i].code); 160 if (g->buf.cpu) omega_gpu_session_free(&g->buf); 161 if (g->dbuf.cpu) omega_gpu_session_free(&g->dbuf); 162 if (g->xh.mem.cpu) omega_gpu_session_free(&g->xh.mem); 163 omega_gpu_session_unlock(); 164 omega_gpu_host_free(g); 165 } 166 167 int omega_gpu_q38_graph_create(const OmegaGpuQ38GraphConfig *cfg, OmegaGpuQ38Graph **out) { 168 return omega_gpu_q38_graph_create_heads(cfg, 1, out); 169 } 170 171 int omega_gpu_q38_graph_create_heads(const OmegaGpuQ38GraphConfig *cfg, uint32_t heads, OmegaGpuQ38Graph **out) { 172 if (out) *out = NULL; 173 if (heads == 0 || heads > OMEGA_GPU_Q38_MAX_HEADS) return OMEGA_GPU_DGRAPH_BAD_ARGS; 174 if (!cfg || !out || cfg->layers == 0 || cfg->layers > OMEGA_GPU_Q38_MAX_LAYERS || !cfg->gdn || !cfg->attn || !cfg->lm_head || 175 cfg->vocab == 0 || cfg->vocab > OMEGA_DEC_ARGMAX_SPAN * OMEGA_DEC_ARGMAX_SPAN) return OMEGA_GPU_DGRAPH_BAD_ARGS; 176 if (cfg->layers < 64u && (cfg->attn_mask >> cfg->layers) != 0) return OMEGA_GPU_DGRAPH_BAD_ARGS; 177 /* the shape: zero fields take the 27B values; the widths are capped at the 27B ones (the RES kernel's 1024-lane rows, 178 * the SWIGLU / GATE 256-lane grids, the attention kernel's q / kv group) */ 179 const uint32_t hd = cfg->hidden ? cfg->hidden : OMEGA_GPU_Q38_HIDDEN, in = cfg->inter ? cfg->inter : OMEGA_GPU_Q38_INTER; 180 const uint32_t qh = cfg->q_heads ? cfg->q_heads : 24u, kvh = cfg->kv_heads ? cfg->kv_heads : 4u, gh = cfg->gdn_heads ? cfg->gdn_heads : 48u; 181 if (hd % 1024u || hd > OMEGA_GPU_Q38_HIDDEN || in == 0 || in % 256u || in > OMEGA_GPU_Q38_INTER || qh > 24u || qh % kvh || 182 qh / kvh > 8u || (gh != 32u && gh != 48u) || qh * 256u != gh * 128u) return OMEGA_GPU_DGRAPH_BAD_ARGS; 183 OmegaGpuDecodeAttn256CacheView av; 184 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; 185 uint32_t n = 0, k = 0; 186 omega_gpu_bf16_tensor_shape(cfg->lm_head, &n, &k); 187 if (n != cfg->vocab || k != hd) return OMEGA_GPU_DGRAPH_BAD_ARGS; 188 OmegaGpuQ38Graph *g = omega_gpu_host_calloc(1, sizeof *g); 189 if (!g) return OMEGA_GPU_DGRAPH_BAD_ARGS; 190 g->cfg = *cfg; 191 g->hd = hd; g->in = in; g->qh = qh; g->kvh = kvh; g->gh = gh; 192 g->gqkv = (32u + gh) * 128u; g->gmix = g->gqkv + gh * 128u; g->ab = 2u * gh; 193 g->amix = (qh + kvh) * 512u; g->aqkv = qh * 256u + kvh * 512u; g->mout = qh * 256u; 194 g->am_ctas = (cfg->vocab + OMEGA_DEC_ARGMAX_SPAN - 1) / OMEGA_DEC_ARGMAX_SPAN; 195 g->heads = heads; g->gdn_h[0] = cfg->gdn; g->attn_h[0] = cfg->attn; 196 for (uint32_t h = 0; h < OMEGA_GPU_Q38_MAX_HEADS; h++) g->xq_layer[h] = -1; 197 for (uint32_t l = 0, na = 0, ng = 0; l < cfg->layers; l++) { 198 g->layer[l].attn = (int)((cfg->attn_mask >> l) & 1u); 199 g->layer[l].kind_index = g->layer[l].attn ? na++ : ng++; 200 } 201 Layout *L = &g->lo; size_t o = 0; 202 #define REG(f, bytes) do { L->f = o; o = al(o + (bytes)); } while (0) 203 REG(x, (size_t)H * 4 * heads); REG(ab, g->ab * 4); REG(hidden, (size_t)H * 4 * heads); 204 REG(logits, (size_t)g->am_ctas * OMEGA_DEC_ARGMAX_SPAN * 4 * heads); 205 REG(am_v, (size_t)OMEGA_DEC_ARGMAX_SPAN * 4 * heads); REG(am_i, (size_t)OMEGA_DEC_ARGMAX_SPAN * 4 * heads); 206 REG(token, (size_t)TOKEN_STRIDE * heads); REG(norms, (size_t)cfg->layers * 2 * H * 4); REG(fnorm, H * 4); 207 REG(tabs, (size_t)cfg->layers * 4 * 16 + 16); /* + the FP8 lm_head entry */ 208 REG(gtab, g->amix * 4); REG(idtab, COPY_ROW * 4); REG(stage, (size_t)2 * I * 4); 209 REG(segd, (size_t)cfg->layers * SEGD_ROWS * OMEGA_GEMM_SEG_BYTES); 210 REG(xh_seg, OMEGA_GEMM_SEG_BYTES); 211 REG(ptab, (size_t)(H + g->mout + I) * 4); 212 L->split = o; 213 REG(xq, (size_t)H * 4 * heads); REG(mix, g->gmix * 4); REG(agg, g->amix * 4); 214 REG(att, g->mout * 4); REG(o, H * 4); REG(gu, (size_t)2 * I * 4); REG(act, (size_t)I * 4); 215 { /* batch regions: N rows each, plus M8 padding rows on fp8gemm inputs */ 216 const size_t N = heads, NP = (size_t)heads + OMEGA_GPU_Q38_MAX_HEADS; 217 REG(xp, NP * H * 4); REG(mixN, N * g->gmix * 4); REG(abN, N * g->ab * 4); REG(aggN, N * g->amix * 4); 218 REG(attN, N * g->mout * 4); REG(op, NP * g->mout * 4); REG(oN, N * H * 4); 219 REG(guN, N * 2 * I * 4); REG(actN, N * I * 4); REG(actp, NP * I * 4); 220 } 221 #undef REG 222 L->bytes = o; 223 int rc = OMEGA_GPU_DGRAPH_OK; 224 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; } 225 omega_gpu_session_lock(); 226 const char *pbc = getenv("AIEN_OMEGA_Q38_BUF_CACHED"); 227 int (*const dalloc)(size_t, OmegaGpuMem *) = (pbc && pbc[0] == '0' && pbc[1] == 0) ? omega_gpu_session_alloc : omega_gpu_session_alloc_cached; 228 if (!omega_gpu_session_open() || omega_gpu_session_alloc(L->split, &g->buf) != 0 || dalloc(L->bytes - L->split, &g->dbuf) != 0) 229 rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; 230 const OmegaDecSpec spec[K_COUNT] = { 231 [K_NORM] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_NORM, .width = H }, 232 [K_ADDNORM] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM, .width = H }, 233 [K_SW] = { .kind = OMEGA_DEC_SWIGLU, .width = I }, 234 [K_GATHER] = { .kind = OMEGA_DEC_GATHER, .width = g->amix, .stride = g->amix }, 235 [K_GATE] = { .kind = OMEGA_DEC_GATE }, 236 [K_AM0] = { .kind = OMEGA_DEC_ARGMAX, .mode = 0 }, 237 [K_AM1] = { .kind = OMEGA_DEC_ARGMAX, .mode = 1 }, 238 [K_SW_M1] = { .kind = OMEGA_DEC_SWIGLU, .width = I, .mutant = 1 }, 239 [K_GATHER_M1] = { .kind = OMEGA_DEC_GATHER, .width = g->amix, .stride = g->amix, .mutant = 1 }, 240 [K_GATE_M1] = { .kind = OMEGA_DEC_GATE, .mutant = 1 }, 241 [K_ADDNORM_M1] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM, .width = H, .mutant = 1 }, 242 [K_COPY] = { .kind = OMEGA_DEC_GATHER, .width = COPY_ROW, .stride = COPY_ROW }, 243 [K_ADD] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_ADD, .width = H }, 244 [K_NORM_R] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_NORM | OMEGA_DEC_RES_ROWS, .width = H }, 245 [K_ADDNORM_R] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM | OMEGA_DEC_RES_ROWS, .width = H }, 246 [K_ADDNORM_R_M1] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM | OMEGA_DEC_RES_ROWS, .width = H, .mutant = 1 }, 247 [K_PERM_XQ] = { .kind = OMEGA_DEC_GATHER, .width = H, .stride = H }, 248 [K_PERM_O] = { .kind = OMEGA_DEC_GATHER, .width = g->mout, .stride = g->mout }, 249 [K_PERM_ACT] = { .kind = OMEGA_DEC_GATHER, .width = I, .stride = I }, 250 [K_XS] = { .kind = OMEGA_DEC_XBOUND, .mode = 1 }, 251 [K_XB] = { .kind = OMEGA_DEC_XBOUND, .width = H }, 252 [K_XB_M1] = { .kind = OMEGA_DEC_XBOUND, .width = H, .mutant = 1 }, 253 }; 254 for (unsigned i = 0; i < K_COUNT && rc == OMEGA_GPU_DGRAPH_OK; i++) rc = kern_build(&g->k[i], &spec[i]); 255 if (rc == OMEGA_GPU_DGRAPH_OK) { 256 memset(g->buf.cpu, 0, L->split); 257 memset(g->dbuf.cpu, 0, L->bytes - L->split); /* fresh; every region is written by the chip before a launch reads it */ 258 uint32_t *id = cpu_of(g, L->idtab); 259 for (uint32_t i = 0; i < COPY_ROW; i++) id[i] = i; 260 for (uint32_t h = 0; h < heads; h++) { 261 float *lg = cpu_of(g, logits_of(g, h)); 262 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 */ 263 float *av = cpu_of(g, amv_of(g, h)); 264 for (size_t i = 0; i < OMEGA_DEC_ARGMAX_SPAN; i++) av[i] = NEG_PAD; /* stage 1 writes [0, ctas) only */ 265 } 266 (void)omega_q38_gather_table(qh, kvh, cpu_of(g, L->gtab)); /* the shape was checked above */ 267 { 268 uint32_t *pt = cpu_of(g, L->ptab); 269 omega_q38_perm_table(pt, H); 270 omega_q38_perm_table(pt + H, g->mout); 271 omega_q38_perm_table(pt + H + g->mout, I); 272 } 273 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; 274 } 275 omega_gpu_session_unlock(); 276 if (rc != OMEGA_GPU_DGRAPH_OK) { omega_gpu_q38_graph_free(g); return rc; } 277 { 278 const char *e = getenv("AIEN_OMEGA_Q38_BATCH"); 279 if (e && e[0] == '1' && e[1] == 0 && omega_gpu_q38_graph_set_batch(g, 1) != OMEGA_GPU_DGRAPH_OK) { 280 omega_gpu_q38_graph_free(g); 281 return OMEGA_GPU_DGRAPH_CHIP_FAIL; 282 } 283 } 284 *out = g; 285 return OMEGA_GPU_DGRAPH_OK; 286 } 287 288 int omega_gpu_q38_graph_set_head_state(OmegaGpuQ38Graph *g, uint32_t head, OmegaGpuGdn *gdn, OmegaGpuDecodeAttn256Cache *attn) { 289 if (!g || head >= g->heads || !gdn || !attn || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; 290 OmegaGpuDecodeAttn256CacheView av; 291 if (omega_gpu_decode_attn256_cache_view(attn, 0, &av) != 0 || av.q_heads != g->qh || av.kv_heads != g->kvh) 292 return OMEGA_GPU_DGRAPH_BAD_ARGS; 293 g->gdn_h[head] = gdn; g->attn_h[head] = attn; 294 return OMEGA_GPU_DGRAPH_OK; 295 } 296 297 static int shape_is(const OmegaGpuFp8Tensor *t, uint32_t n, uint32_t k) { 298 uint32_t te = 0, tn = 0, tk = 0; if (t) omega_gpu_fp8_tensor_shape(t, &te, &tn, &tk); 299 return t && te == 1 && tn == n && tk == k; 300 } 301 302 /* the norms and the layer's table row (t: the FP8 entries, or zeros for a bf16 layer) */ 303 static int layer_common(OmegaGpuQ38Graph *g, uint32_t layer, const uint64_t t[8], const float *in_norm, const float *post_norm) { 304 omega_gpu_session_lock(); 305 memcpy((uint8_t *)cpu_of(g, g->lo.tabs) + (size_t)layer * 64u, t, 8 * sizeof *t); 306 memcpy((float *)cpu_of(g, g->lo.norms) + (size_t)layer * 2 * H, in_norm, H * 4u); 307 memcpy((float *)cpu_of(g, g->lo.norms) + (size_t)layer * 2 * H + H, post_norm, H * 4u); 308 int rc = omega_gpu_session_sync_to_gpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL; 309 omega_gpu_session_unlock(); 310 return rc; 311 } 312 313 static int ab_ok(const OmegaGpuQ38Graph *g, int attn, const OmegaGpuBf16Tensor *ab) { 314 if (attn) return ab == NULL; 315 uint32_t n = 0, k = 0; omega_gpu_bf16_tensor_shape(ab, &n, &k); 316 return ab != NULL && n == g->ab && k == H; 317 } 318 319 int omega_gpu_q38_graph_set_layer(OmegaGpuQ38Graph *g, uint32_t layer, const OmegaGpuFp8Tensor *mix, const OmegaGpuBf16Tensor *ab, 320 const OmegaGpuFp8Tensor *o, const OmegaGpuFp8Tensor *gate_up, const OmegaGpuFp8Tensor *down, 321 const float *in_norm, const float *post_norm) { 322 if (!g || layer >= g->cfg.layers || !in_norm || !post_norm || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; 323 const int attn = g->layer[layer].attn; 324 if (!shape_is(mix, attn ? g->amix : g->gmix, H) || !shape_is(o, H, g->mout) || 325 !shape_is(gate_up, 2 * I, H) || !shape_is(down, H, I)) return OMEGA_GPU_DGRAPH_BAD_ARGS; 326 if (!ab_ok(g, attn, ab)) return OMEGA_GPU_DGRAPH_BAD_ARGS; 327 uint64_t t[8]; 328 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 || 329 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; 330 int rc = layer_common(g, layer, t, in_norm, post_norm); 331 if (rc == OMEGA_GPU_DGRAPH_OK) { 332 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; 333 g->layer[layer].bmix = g->layer[layer].bo = g->layer[layer].bgu = g->layer[layer].bdn = g->layer[layer].bdn2 = NULL; 334 g->layer[layer].bf16 = 0; g->layer[layer].set = 1; 335 } 336 return rc; 337 } 338 339 static int bf_shape_is(const OmegaGpuBf16Tensor *t, uint32_t n, uint32_t k) { 340 uint32_t tn = 0, tk = 0; if (t) omega_gpu_bf16_tensor_shape(t, &tn, &tk); 341 return t && tn == n && tk == k; 342 } 343 344 /* the bf16 GEMV stages its whole f32 x row in shared memory, which the launch descriptor caps at 32 KiB: k <= 8192 */ 345 #define BF16_X_MAX_K (OMEGA_BW_QMD_MAX_SHARED_BYTES / 4u) 346 347 int omega_gpu_q38_graph_set_layer_bf16(OmegaGpuQ38Graph *g, uint32_t layer, const OmegaGpuBf16Tensor *mix, const OmegaGpuBf16Tensor *ab, 348 const OmegaGpuBf16Tensor *o, const OmegaGpuBf16Tensor *gate_up, const OmegaGpuBf16Tensor *down, 349 const OmegaGpuBf16Tensor *down2, const float *in_norm, const float *post_norm) { 350 if (!g || layer >= g->cfg.layers || !in_norm || !post_norm || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; 351 const int attn = g->layer[layer].attn; 352 /* 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) */ 353 const uint32_t kd = down2 ? I / 2u : I; 354 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) || 355 !bf_shape_is(down, H, kd) || (down2 && !bf_shape_is(down2, H, kd)) || !ab_ok(g, attn, ab)) return OMEGA_GPU_DGRAPH_BAD_ARGS; 356 if (H > BF16_X_MAX_K || g->mout > BF16_X_MAX_K || kd > BF16_X_MAX_K || (down2 && I % 512u)) { 357 omega_gpu_session_set_error("q38 graph: a bf16 projection's K exceeds the GEMV x stage (8192; split down in two halves)"); 358 return OMEGA_GPU_DGRAPH_BAD_ARGS; 359 } 360 if (omega_gpu_bf16_prepare(H) != 0 || omega_gpu_bf16_prepare(g->mout) != 0 || omega_gpu_bf16_prepare(kd) != 0) { 361 omega_gpu_session_set_error("q38 graph: bf16 GEMV kernels could not be prepared"); 362 return OMEGA_GPU_DGRAPH_CODEGEN_FAIL; 363 } 364 const uint64_t t[8] = { 0 }; 365 int rc = layer_common(g, layer, t, in_norm, post_norm); 366 if (rc == OMEGA_GPU_DGRAPH_OK) { 367 g->layer[layer].mix = g->layer[layer].o = g->layer[layer].gu = g->layer[layer].dn = NULL; 368 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; 369 g->layer[layer].bdn2 = down2; 370 g->layer[layer].bf16 = 1; g->layer[layer].set = 1; 371 } 372 return rc; 373 } 374 375 int omega_gpu_q38_graph_set_final_norm(OmegaGpuQ38Graph *g, const float *final_norm) { 376 if (!g || !final_norm || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; 377 omega_gpu_session_lock(); 378 memcpy(cpu_of(g, g->lo.fnorm), final_norm, H * 4u); 379 int rc = omega_gpu_session_sync_to_gpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL; 380 omega_gpu_session_unlock(); 381 if (rc == OMEGA_GPU_DGRAPH_OK) g->final_set = 1; 382 return rc; 383 } 384 385 int omega_gpu_q38_graph_set_lm_head_fp8(OmegaGpuQ38Graph *g, const OmegaGpuFp8Tensor *head) { 386 if (!g || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; 387 if (!head) { g->head8 = NULL; return OMEGA_GPU_DGRAPH_OK; } 388 uint64_t t[2]; 389 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; 390 omega_gpu_session_lock(); 391 memcpy((uint8_t *)cpu_of(g, g->lo.tabs) + (size_t)g->cfg.layers * 64u, t, sizeof t); 392 int rc = omega_gpu_session_sync_to_gpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL; 393 omega_gpu_session_unlock(); 394 if (rc == OMEGA_GPU_DGRAPH_OK) g->head8 = head; 395 return rc; 396 } 397 398 uint64_t omega_gpu_q38_exact_extra_bytes(void) { 399 /* 27B exact-head admission reserve: FP8 screen tensor (248320 x 5120 + 128x128 bf16 scales) + device scratch + host coef */ 400 const uint64_t V = 248320ull, HD = OMEGA_GPU_Q38_HIDDEN; 401 const uint64_t screen_w = V * HD; 402 const uint64_t screen_s = ((V + 127ull) / 128ull) * ((HD + 127ull) / 128ull) * 2ull; 403 const uint64_t P = ((V + OMEGA_DEC_ARGMAX_SPAN - 1ull) / OMEGA_DEC_ARGMAX_SPAN) * OMEGA_DEC_ARGMAX_SPAN; 404 const uint64_t scratch = al((size_t)(HD * 4u)) + al((size_t)(V * 4u)) * 3ull + al((size_t)(P * 4u)) + al(16u) * 2ull; 405 const uint64_t host_coef = V * 4ull; 406 return screen_w + screen_s + scratch + host_coef; 407 } 408 409 int omega_gpu_q38_graph_set_exact_head(OmegaGpuQ38Graph *g, const OmegaGpuFp8Tensor *screen, const float *coef, uint32_t roundings) { 410 if (!g || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; 411 if (!screen) { 412 omega_gpu_session_lock(); 413 if (g->xh.mem.cpu) omega_gpu_session_free(&g->xh.mem); 414 omega_gpu_session_unlock(); 415 memset(&g->xh, 0, sizeof g->xh); 416 return OMEGA_GPU_DGRAPH_OK; 417 } 418 uint64_t t[2]; 419 if (!coef || roundings == 0 || g->xh.screen || !shape_is(screen, g->cfg.vocab, H) || 420 omega_gpu_fp8_tensor_entry(screen, 0, &t[0], &t[1]) != 0) 421 return OMEGA_GPU_DGRAPH_BAD_ARGS; 422 const size_t n = g->cfg.vocab, P = (size_t)g->am_ctas * OMEGA_DEC_ARGMAX_SPAN, nh = g->heads; 423 const size_t o_h8 = 0, o_s = al(o_h8 + nh * H * 4), o_c = al(o_s + nh * n * 4), o_hi = al(o_c + n * 4), 424 o_lo = al(o_hi + nh * n * 4), o_tau = al(o_lo + nh * P * 4), o_tab = al(o_tau + nh * 16), total = al(o_tab + 16); 425 if (omega_gpu_bf16_prepare_mask(H) != 0) return fail(g, "q38 graph: exact head MASK kernel", OMEGA_GPU_DGRAPH_CHIP_FAIL); 426 omega_gpu_session_lock(); 427 int rc = omega_gpu_session_alloc(total, &g->xh.mem) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL; 428 if (rc == OMEGA_GPU_DGRAPH_OK) { 429 uint8_t *B = g->xh.mem.cpu; 430 memset(B, 0, total); 431 memcpy(B + o_c, coef, n * 4); 432 for (size_t h = 0; h < nh; h++) { 433 float *lo_h = (float *)(B + o_lo + h * P * 4); 434 for (size_t i = n; i < P; i++) lo_h[i] = NEG_PAD; /* XBOUND writes [0, vocab) only */ 435 } 436 memcpy(B + o_tab, t, sizeof t); 437 if (omega_gpu_session_sync_to_gpu(&g->xh.mem) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; 438 } 439 if (rc == OMEGA_GPU_DGRAPH_OK) { 440 const uint64_t V = g->xh.mem.va; 441 g->xh.h8 = V + o_h8; g->xh.s = V + o_s; g->xh.c = V + o_c; g->xh.hi = V + o_hi; g->xh.lo = V + o_lo; g->xh.tau = V + o_tau; g->xh.tab = V + o_tab; 442 g->xh.screen = screen; g->xh.roundings = roundings; 443 memset(g->xh.on, 1, sizeof g->xh.on); 444 } else if (g->xh.mem.cpu) omega_gpu_session_free(&g->xh.mem); 445 omega_gpu_session_unlock(); 446 return rc; 447 } 448 449 void omega_gpu_q38_graph_set_exact_head_on(OmegaGpuQ38Graph *g, int on) { if (g && !g->in_token) memset(g->xh.on, on ? 1 : 0, sizeof g->xh.on); } 450 void omega_gpu_q38_graph_set_head_exact_head_on(OmegaGpuQ38Graph *g, uint32_t head, int on) { 451 if (g && !g->in_token && head < OMEGA_GPU_Q38_MAX_HEADS) g->xh.on[head] = on ? 1 : 0; 452 } 453 int omega_gpu_q38_graph_exact_head_active_head(const OmegaGpuQ38Graph *g, uint32_t head) { 454 return g && head < OMEGA_GPU_Q38_MAX_HEADS && g->xh.screen && g->xh.on[head] && !g->head8; 455 } 456 int omega_gpu_q38_graph_exact_head_active(const OmegaGpuQ38Graph *g) { return omega_gpu_q38_graph_exact_head_active_head(g, 0); } 457 458 void omega_gpu_q38_graph_set_flush_each(OmegaGpuQ38Graph *g, int on) { if (g) g->flush_each = on ? 1 : 0; } 459 void omega_gpu_q38_graph_test_set_mutant(OmegaGpuQ38Graph *g, int mutant) { 460 if (!g) return; 461 g->mutant = mutant; 462 omega_gpu_bf16_test_set_mask_mutant(mutant == 5 ? 4 : 0); /* process-wide, the MASK form only */ 463 } 464 465 /* ---- the list ---- */ 466 467 static OmegaGpuCmdListOpts list_opts(const OmegaGpuQ38Graph *g) { 468 OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); 469 o.mode = OMEGA_GPU_CMDLIST_PCAS; o.barrier = (OmegaGpuCmdListBarrier)omega_gpu_decode_graph_barrier(); o.timeout_ms = TIMEOUT_MS; o.stamps = g->stamps; 470 return o; 471 } 472 473 static int submit_now(OmegaGpuQ38Graph *g) { 474 if (omega_gpu_cmdlist_count(g->cl) == 0) return OMEGA_GPU_DGRAPH_OK; 475 OmegaGpuCmdListOpts o = list_opts(g); 476 OmegaGpuCmdListStats st; memset(&st, 0, sizeof st); 477 int rc = omega_gpu_cmdlist_submit(g->cl, &o, &st); 478 g->acc.launches += st.launches; g->acc.submits += st.submits; g->acc.chip_ns += st.elapsed_ns; g->acc.sems_done += st.sems_done; 479 if (rc == 0 && g->stamps) { 480 uint64_t tmp[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1]; 481 int cnt = omega_gpu_cmdlist_stamps(g->cl, tmp, OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1); 482 if (cnt < 0) rc = -1; 483 else { const int from = g->n_stamp == 0 ? 0 : 1; for (int i = from; i < cnt && g->n_stamp <= OMEGA_GPU_CMDLIST_MAX_LAUNCHES; i++) g->stamp[g->n_stamp++] = tmp[i]; } 484 } 485 g->tagged_in_list = 0; 486 omega_gpu_cmdlist_reset(g->cl); 487 if (rc != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); 488 if (st.sems_done != st.launches) { 489 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); 490 return fail(g, e, OMEGA_GPU_DGRAPH_CHIP_FAIL); 491 } 492 return OMEGA_GPU_DGRAPH_OK; 493 } 494 495 static int ensure_room(OmegaGpuQ38Graph *g, uint32_t need) { 496 if (omega_gpu_cmdlist_count(g->cl) + need >= OMEGA_GPU_CMDLIST_MAX_LAUNCHES) return submit_now(g); 497 return OMEGA_GPU_DGRAPH_OK; 498 } 499 500 static void tag(OmegaGpuQ38Graph *g, uint8_t kind) { 501 const uint32_t n = omega_gpu_cmdlist_count(g->cl); 502 for (; g->tagged_in_list < n && g->n_kind < OMEGA_GPU_CMDLIST_MAX_LAUNCHES; g->tagged_in_list++) g->kind[g->n_kind++] = kind; 503 } 504 505 static int after_op(OmegaGpuQ38Graph *g, uint8_t kind, int api_rc) { 506 if (api_rc != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); 507 tag(g, kind); 508 return g->flush_each ? submit_now(g) : OMEGA_GPU_DGRAPH_OK; 509 } 510 511 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) { 512 const Kern *k = &g->k[ki]; 513 OmegaGpuLaunch L; memset(&L, 0, sizeof L); 514 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; 515 L.num_elements = elems; L.args = args; L.n_args = n_args; L.timeout_ms = TIMEOUT_MS; 516 return after_op(g, kind, omega_gpu_cmdlist_add(g->cl, &L) < 0 ? -1 : 0); 517 } 518 519 /* RES at the hidden width: NORM (x -> y) or ADD|NORM (x += a; y = norm(x)) */ 520 static int rec_res(OmegaGpuQ38Graph *g, int ki, uint64_t x, uint64_t a, uint64_t nw, uint64_t y, float scale) { 521 uint32_t w[OMEGA_DEC_RES_WORDS]; memset(w, 0, sizeof w); 522 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); 523 putf(w, OMEGA_DEC_RES_EPS, g->cfg.eps); putf(w, OMEGA_DEC_RES_SCALE, scale); 524 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); 525 } 526 527 static int rec_res_rows(OmegaGpuQ38Graph *g, int ki, uint64_t x, uint64_t a, uint64_t nw, uint64_t y, float scale, uint32_t rows) { 528 uint32_t w[OMEGA_DEC_RES_WORDS]; memset(w, 0, sizeof w); 529 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); 530 putf(w, OMEGA_DEC_RES_EPS, g->cfg.eps); putf(w, OMEGA_DEC_RES_SCALE, scale); 531 return add_glue(g, ki == K_NORM_R ? OMEGA_GPU_Q38_K_NORM : OMEGA_GPU_Q38_K_RES, ki, 1, rows, (size_t)H * rows, w, OMEGA_DEC_RES_WORDS); 532 } 533 534 static int g_overlap_ab = -1; 535 void omega_gpu_q38_graph_set_overlap_ab(int on) { g_overlap_ab = on ? 1 : 0; } 536 static int q38_overlap_ab(void) { 537 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; } 538 return g_overlap_ab; 539 } 540 541 static int rec_fp8(OmegaGpuQ38Graph *g, uint8_t kind, const OmegaGpuFp8Tensor *t, uint64_t tab, uint64_t x, uint64_t y) { 542 return after_op(g, kind, omega_gpu_fp8_gemv_grouped_record(g->cl, t, 1, tab, x, 1, y)); 543 } 544 /* one projection of a layer: FP8 (table row entry tab) or, for a set_layer_bf16 layer, the bf16 GEMV (weights 2^-64 scaled) */ 545 static int rec_proj(OmegaGpuQ38Graph *g, uint8_t kind, const OmegaGpuFp8Tensor *t8, const OmegaGpuBf16Tensor *tb, uint64_t tab, uint64_t x, uint64_t y) { 546 return tb ? after_op(g, kind, omega_gpu_bf16_gemv_record(g->cl, tb, x, y)) : rec_fp8(g, kind, t8, tab, x, y); 547 } 548 549 /* ---- strokes (multi-head) & batch (v2) ---- */ 550 551 typedef struct { uint64_t w_va, s_va; uint32_t row0, cnt, pad[2]; } Seg32; 552 _Static_assert(sizeof(Seg32) == OMEGA_GEMM_SEG_BYTES, "segment entry layout"); 553 554 static void write_dense_segments(OmegaGpuQ38Graph *g, uint32_t heads) { 555 Seg32 *s = cpu_of(g, g->lo.segd); 556 for (uint32_t l = 0; l < g->cfg.layers; l++) { 557 Seg32 *row = &s[l * SEGD_ROWS]; 558 memset(row, 0, SEGD_ROWS * sizeof *row); 559 if (!g->layer[l].set || g->layer[l].bf16) continue; 560 (void)omega_gpu_fp8_tensor_entry(g->layer[l].mix, 0, &row[0].w_va, &row[0].s_va); 561 (void)omega_gpu_fp8_tensor_entry(g->layer[l].o, 0, &row[1].w_va, &row[1].s_va); 562 (void)omega_gpu_fp8_tensor_entry(g->layer[l].gu, 0, &row[2].w_va, &row[2].s_va); 563 (void)omega_gpu_fp8_tensor_entry(g->layer[l].dn, 0, &row[3].w_va, &row[3].s_va); 564 for (uint32_t j = 0; j < SEGD_ROWS; j++) row[j].cnt = heads; 565 } 566 Seg32 *xs = cpu_of(g, g->lo.xh_seg); 567 memset(xs, 0, sizeof *xs); 568 if (g->xh.screen) { 569 (void)omega_gpu_fp8_tensor_entry(g->xh.screen, 0, &xs->w_va, &xs->s_va); 570 xs->cnt = heads; 571 } 572 } 573 574 int omega_gpu_q38_graph_set_batch(OmegaGpuQ38Graph *g, int on) { 575 if (!g || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; 576 if (!on) { g->batch = 0; return OMEGA_GPU_DGRAPH_OK; } 577 int rc = omega_gpu_fp8gemm_prepare_shape(H, 1, 0, 0); 578 if (rc == 0) rc = omega_gpu_fp8gemm_prepare_shape(g->mout, 1, 0, 0); 579 if (rc == 0) rc = omega_gpu_fp8gemm_prepare_shape(I, 1, 0, 0); 580 if (rc != 0) return OMEGA_GPU_DGRAPH_CHIP_FAIL; 581 if (!g->head8) { 582 uint64_t max_xb = 0; 583 if (omega2_q38_bf16_xbatch_max(&max_xb, H, OMEGA_BW_QMD_MAX_SHARED_BYTES) != OMEGA_OK) return OMEGA_GPU_DGRAPH_CHIP_FAIL; 584 for (uint32_t xb = 2; xb <= max_xb && xb <= g->heads; xb++) 585 if (omega_gpu_bf16_prepare_xbatch(H, xb, g->am_ctas * OMEGA_DEC_ARGMAX_SPAN) != OMEGA_GPU_BF16_OK) 586 return OMEGA_GPU_DGRAPH_CHIP_FAIL; 587 } 588 g->batch = 1; 589 return OMEGA_GPU_DGRAPH_OK; 590 } 591 int omega_gpu_q38_graph_batch(const OmegaGpuQ38Graph *g) { return g ? g->batch : 0; } 592 593 static int rec_gemm(OmegaGpuQ38Graph *g, uint8_t kind, const OmegaGpuFp8Tensor *t, uint32_t rows, uint64_t seg, uint64_t xp, uint64_t y) { 594 return after_op(g, kind, omega_gpu_fp8gemm_record(g->cl, t, rows, 1, seg, xp, y, 0, 0, 0, 0, NULL)); 595 } 596 static int rec_perm(OmegaGpuQ38Graph *g, int ki, uint64_t in, uint64_t tab, uint64_t out, uint32_t width, uint32_t rows) { 597 uint32_t w[OMEGA_DEC_GA_WORDS]; memset(w, 0, sizeof w); 598 put64(w, OMEGA_DEC_GA_IN, in); put64(w, OMEGA_DEC_GA_TAB, tab); put64(w, OMEGA_DEC_GA_OUT, out); 599 return add_glue(g, OMEGA_GPU_Q38_K_PERM, ki, (width + OMEGA_DEC_THREADS - 1) / OMEGA_DEC_THREADS, rows, (size_t)width * rows, w, OMEGA_DEC_GA_WORDS); 600 } 601 602 int omega_gpu_q38_graph_begin_token(OmegaGpuQ38Graph *g, const float *x_embed) { 603 return omega_gpu_q38_graph_begin_stroke(g, x_embed, 1); 604 } 605 606 int omega_gpu_q38_graph_begin_stroke(OmegaGpuQ38Graph *g, const float *x_embed, uint32_t heads) { 607 if (!g || g->in_token || heads == 0 || heads > g->heads) return OMEGA_GPU_DGRAPH_BAD_ARGS; 608 for (uint32_t h = 1; h < heads; h++) if (!g->gdn_h[h] || !g->attn_h[h]) return OMEGA_GPU_DGRAPH_BAD_ARGS; 609 omega_gpu_session_lock(); 610 g->t_begin = omega_gpu_session_now_ns(); 611 g->in_token = 1; g->failed = 0; g->stroke_heads = heads; 612 for (uint32_t h = 0; h < heads; h++) { g->xq_layer[h] = -1; g->hidden_ready[h] = 0; } 613 memset(&g->acc, 0, sizeof g->acc); 614 g->n_kind = 0; g->tagged_in_list = 0; g->n_stamp = 0; 615 omega_gpu_cmdlist_reset(g->cl); 616 if (!omega_gpu_session_open()) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); 617 for (uint32_t h = 0; h < heads; h++) { 618 uint32_t *tok = cpu_of(g, token_of(g, h)); 619 tok[0] = TOKEN_POISON; tok[1] = TOKEN_POISON; 620 if (x_embed) memcpy(cpu_of(g, x_of(g, h)), x_embed + (size_t)h * H, H * 4u); 621 } 622 if (g->batch) write_dense_segments(g, heads); 623 if (omega_gpu_session_sync_to_gpu(&g->buf) != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); 624 return OMEGA_GPU_DGRAPH_OK; 625 } 626 627 int omega_gpu_q38_graph_record_layer(OmegaGpuQ38Graph *g, uint32_t l, uint32_t pos) { 628 return omega_gpu_q38_graph_record_head_layer(g, 0, l, pos); 629 } 630 631 int omega_gpu_q38_graph_record_head_layer(OmegaGpuQ38Graph *g, uint32_t h, uint32_t l, uint32_t pos) { 632 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; 633 if (g->failed) return OMEGA_GPU_DGRAPH_CHIP_FAIL; 634 if (h >= g->stroke_heads) return fail(g, "q38 graph: head not in the stroke", OMEGA_GPU_DGRAPH_BAD_ARGS); 635 if (l >= g->cfg.layers || !g->layer[l].set || g->hidden_ready[h] || (l + 1 == g->cfg.layers && !g->final_set)) 636 return fail(g, "q38 graph: layer not set, out of order, or final norm missing", OMEGA_GPU_DGRAPH_BAD_ARGS); 637 int rc = ensure_room(g, 20u); 638 if (rc != OMEGA_GPU_DGRAPH_OK) return rc; 639 const uint64_t x = va_of(g, x_of(g, h)), xq = va_of(g, xq_of(g, h)), mix = va_of(g, g->lo.mix), agg = va_of(g, g->lo.agg); 640 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); 641 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; 642 const uint32_t ki = g->layer[l].kind_index; 643 if (g->xq_layer[h] != (int)l) rc = rec_res(g, K_NORM, x, 0, in_norm, xq, SCALE_2P64); 644 const int overlap = !g->layer[l].attn && q38_overlap_ab(); 645 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))); 646 if (rc == OMEGA_GPU_DGRAPH_OK) { 647 int mrc = g->layer[l].bf16 ? omega_gpu_bf16_gemv_record(g->cl, g->layer[l].bmix, xq, mix) 648 : omega_gpu_fp8_gemv_grouped_record(g->cl, g->layer[l].mix, 1, tab, xq, 1, mix); 649 if (mrc == 0 && overlap && !g->flush_each && omega_gpu_cmdlist_mark_independent(g->cl) != 0) mrc = -1; 650 rc = after_op(g, OMEGA_GPU_Q38_K_MIX, mrc); 651 } 652 uint64_t mixer_out = att; 653 if (!g->layer[l].attn) { 654 const uint64_t ab = va_of(g, g->lo.ab); 655 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)); 656 if (rc == OMEGA_GPU_DGRAPH_OK) 657 rc = after_op(g, OMEGA_GPU_Q38_K_GDN, omega_gpu_gdn_record(g->cl, g->gdn_h[h], ki, mix, mix + g->gqkv * 4u, ab, 658 g->cfg.eps_l2, g->cfg.eps, 1.0f, SCALE_2P64, &mixer_out)); 659 } else { 660 uint32_t w[OMEGA_DEC_GA_WORDS]; memset(w, 0, sizeof w); 661 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); 662 if (rc == OMEGA_GPU_DGRAPH_OK) 663 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, 664 g->amix, w, OMEGA_DEC_GA_WORDS); 665 if (rc == OMEGA_GPU_DGRAPH_OK) 666 rc = after_op(g, OMEGA_GPU_Q38_K_ATTN, omega_gpu_decode_attn256_record(g->cl, g->attn_h[h], ki, pos, agg, g->cfg.eps, SCALE_2P64, att)); 667 uint32_t w2[OMEGA_DEC_GT_WORDS]; memset(w2, 0, sizeof w2); 668 put64(w2, OMEGA_DEC_GT_A, att); put64(w2, OMEGA_DEC_GT_G, agg + g->aqkv * 4u); put64(w2, OMEGA_DEC_GT_OUT, att); 669 w2[(OMEGA_DEC_GT_N - 0x380) / 4] = g->mout; 670 if (rc == OMEGA_GPU_DGRAPH_OK) 671 rc = add_glue(g, OMEGA_GPU_Q38_K_GATE, g->mutant == 3 ? K_GATE_M1 : K_GATE, g->mout / OMEGA_DEC_THREADS, 1, 672 g->mout, w2, OMEGA_DEC_GT_WORDS); 673 } 674 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); 675 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); 676 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); 677 uint32_t w3[OMEGA_DEC_SW_WORDS]; memset(w3, 0, sizeof w3); 678 put64(w3, OMEGA_DEC_SW_GU, gu); put64(w3, OMEGA_DEC_SW_ACT, act); putf(w3, OMEGA_DEC_SW_SCALE, SCALE_2P64); 679 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); 680 if (rc == OMEGA_GPU_DGRAPH_OK && g->layer[l].bdn2) { 681 rc = rec_proj(g, OMEGA_GPU_Q38_K_DOWN, NULL, g->layer[l].bdn, 0, act, o); 682 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_res(g, K_ADD, x, o, 0, 0, 1.0f); 683 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); 684 } 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); 685 if (rc == OMEGA_GPU_DGRAPH_OK) { 686 const int last = l + 1 == g->cfg.layers; 687 const uint64_t nw = last ? va_of(g, g->lo.fnorm) : in_norm + 2 * H * 4u; 688 rc = rec_res(g, K_ADDNORM, x, o, nw, last ? va_of(g, hidden_of(g, h)) : xq, last && !g->head8 ? 1.0f : SCALE_2P64); 689 if (rc == OMEGA_GPU_DGRAPH_OK) { if (last) g->hidden_ready[h] = 1; else g->xq_layer[h] = (int)l + 1; } 690 } 691 return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc); 692 } 693 694 static int mark_indep_if(OmegaGpuQ38Graph *g, uint32_t ind) { 695 if (!ind || g->flush_each) return OMEGA_GPU_DGRAPH_OK; 696 return omega_gpu_cmdlist_mark_independent(g->cl) == 0 ? OMEGA_GPU_DGRAPH_OK : fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); 697 } 698 699 int omega_gpu_q38_graph_record_layer_heads(OmegaGpuQ38Graph *g, uint32_t l, const uint32_t *pos) { 700 if (!g || !g->in_token || !pos) return OMEGA_GPU_DGRAPH_BAD_ARGS; 701 if (g->failed) return OMEGA_GPU_DGRAPH_CHIP_FAIL; 702 if (!g->batch) return fail(g, "q38 graph: record_layer_heads needs set_batch", OMEGA_GPU_DGRAPH_BAD_ARGS); 703 if (l >= g->cfg.layers || !g->layer[l].set || (l + 1 == g->cfg.layers && !g->final_set)) 704 return fail(g, "q38 graph: layer not set, out of order, or final norm missing", OMEGA_GPU_DGRAPH_BAD_ARGS); 705 const uint32_t N = g->stroke_heads, ki = g->layer[l].kind_index; 706 if (g->layer[l].bf16) { 707 int rc = OMEGA_GPU_DGRAPH_OK; 708 for (uint32_t h = 0; h < N && rc == OMEGA_GPU_DGRAPH_OK; h++) rc = omega_gpu_q38_graph_record_head_layer(g, h, l, pos[h]); 709 return rc; 710 } 711 for (uint32_t h = 0; h < N; h++) 712 if (g->hidden_ready[h] || g->xq_layer[h] != g->xq_layer[0]) return fail(g, "q38 graph: heads out of step", OMEGA_GPU_DGRAPH_BAD_ARGS); 713 const uint64_t pcfg[8] = { N, (uint64_t)(g->layer[l].attn ? 1u : 0u), (uint64_t)q38_overlap_ab(), (uint64_t)(g->xq_layer[0] != (int)l), 0, 0, 0, 0 }; 714 uint32_t plan[384]; uint64_t nops = 0; 715 if (omega2_q38_plan_layer(&nops, pcfg, 8, plan, 384) != OMEGA_OK || nops == 0) 716 return fail(g, "q38 graph: om/gpu/q38_plan layer failed", OMEGA_GPU_DGRAPH_CHIP_FAIL); 717 int rc = ensure_room(g, (uint32_t)nops + 4u); 718 if (rc != OMEGA_GPU_DGRAPH_OK) return rc; 719 const Layout *L = &g->lo; 720 const uint64_t x0 = va_of(g, x_of(g, 0)), xq0 = va_of(g, xq_of(g, 0)), xp = va_of(g, L->xp), mixN = va_of(g, L->mixN), abN = va_of(g, L->abN); 721 const uint64_t aggN = va_of(g, L->aggN), attN = va_of(g, L->attN), op = va_of(g, L->op), oN = va_of(g, L->oN); 722 const uint64_t guN = va_of(g, L->guN), actN = va_of(g, L->actN), actp = va_of(g, L->actp); 723 const uint64_t ptH = va_of(g, L->ptab), ptM = ptH + (uint64_t)H * 4u, ptI = ptM + (uint64_t)g->mout * 4u; 724 const uint64_t in_norm = va_of(g, L->norms) + (uint64_t)l * 2 * H * 4u, post_norm = in_norm + H * 4u; 725 const uint64_t segd = va_of(g, L->segd) + (uint64_t)l * SEGD_ROWS * OMEGA_GEMM_SEG_BYTES; 726 const uint64_t MOS = (uint64_t)g->mout * 4u; 727 uint64_t gdn_mo[OMEGA_GPU_Q38_MAX_HEADS] = { 0 }; 728 for (uint64_t k = 0; k < nops && rc == OMEGA_GPU_DGRAPH_OK; k++) { 729 const uint32_t op_id = plan[k * 4 + 0], ind = plan[k * 4 + 1], a0 = plan[k * 4 + 2]; 730 switch (op_id) { 731 case 1: rc = rec_res_rows(g, K_NORM_R, x0, 0, in_norm, xq0, SCALE_2P64, N); break; 732 case 2: rc = rec_perm(g, K_PERM_XQ, xq0, ptH, xp, H, N); break; 733 case 3: rc = after_op(g, OMEGA_GPU_Q38_K_AB, omega_gpu_bf16_gemv_rows_record(g->cl, g->layer[l].ab, N, xq0, abN)); break; 734 case 4: rc = rec_gemm(g, OMEGA_GPU_Q38_K_MIX, g->layer[l].mix, N, segd, xp, mixN); break; 735 case 5: { 736 const uint64_t mix_h = mixN + (uint64_t)a0 * g->gmix * 4u; 737 rc = after_op(g, OMEGA_GPU_Q38_K_GDN, omega_gpu_gdn_record(g->cl, g->gdn_h[a0], ki, mix_h, mix_h + g->gqkv * 4u, 738 abN + (uint64_t)a0 * g->ab * 4u, g->cfg.eps_l2, g->cfg.eps, 1.0f, SCALE_2P64, &gdn_mo[a0])); 739 break; 740 } 741 case 6: rc = rec_perm(g, K_PERM_O, gdn_mo[a0], ptM, op + (uint64_t)a0 * MOS, g->mout, 1); break; 742 case 7: { 743 uint32_t w[OMEGA_DEC_GA_WORDS]; memset(w, 0, sizeof w); 744 put64(w, OMEGA_DEC_GA_IN, mixN); put64(w, OMEGA_DEC_GA_TAB, va_of(g, L->gtab)); put64(w, OMEGA_DEC_GA_OUT, aggN); 745 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, N, 746 (size_t)g->amix * N, w, OMEGA_DEC_GA_WORDS); 747 break; 748 } 749 case 8: { 750 const uint64_t agg_h = aggN + (uint64_t)a0 * g->amix * 4u, att_h = attN + (uint64_t)a0 * MOS; 751 rc = after_op(g, OMEGA_GPU_Q38_K_ATTN, omega_gpu_decode_attn256_record(g->cl, g->attn_h[a0], ki, pos[a0], agg_h, g->cfg.eps, SCALE_2P64, att_h)); 752 break; 753 } 754 case 9: { 755 const uint64_t agg_h = aggN + (uint64_t)a0 * g->amix * 4u, att_h = attN + (uint64_t)a0 * MOS; 756 uint32_t w2[OMEGA_DEC_GT_WORDS]; memset(w2, 0, sizeof w2); 757 put64(w2, OMEGA_DEC_GT_A, att_h); put64(w2, OMEGA_DEC_GT_G, agg_h + g->aqkv * 4u); put64(w2, OMEGA_DEC_GT_OUT, att_h); 758 w2[(OMEGA_DEC_GT_N - 0x380) / 4] = g->mout; 759 rc = add_glue(g, OMEGA_GPU_Q38_K_GATE, g->mutant == 3 ? K_GATE_M1 : K_GATE, g->mout / OMEGA_DEC_THREADS, 1, g->mout, w2, OMEGA_DEC_GT_WORDS); 760 break; 761 } 762 case 10: rc = rec_perm(g, K_PERM_O, attN, ptM, op, g->mout, N); break; 763 case 11: rc = rec_gemm(g, OMEGA_GPU_Q38_K_O, g->layer[l].o, N, segd + OMEGA_GEMM_SEG_BYTES, op, oN); break; 764 case 12: rc = rec_res_rows(g, g->mutant == 4 ? K_ADDNORM_R_M1 : K_ADDNORM_R, x0, oN, post_norm, xq0, SCALE_2P64, N); break; 765 case 13: rc = rec_gemm(g, OMEGA_GPU_Q38_K_GATE_UP, g->layer[l].gu, N, segd + 2u * OMEGA_GEMM_SEG_BYTES, xp, guN); break; 766 case 14: { 767 uint32_t w3[OMEGA_DEC_SW_WORDS]; memset(w3, 0, sizeof w3); 768 put64(w3, OMEGA_DEC_SW_GU, guN); put64(w3, OMEGA_DEC_SW_ACT, actN); putf(w3, OMEGA_DEC_SW_SCALE, SCALE_2P64); 769 rc = add_glue(g, OMEGA_GPU_Q38_K_SWIGLU, g->mutant == 1 ? K_SW_M1 : K_SW, I / OMEGA_DEC_THREADS, N, (size_t)N * I, w3, OMEGA_DEC_SW_WORDS); 770 break; 771 } 772 case 15: rc = rec_perm(g, K_PERM_ACT, actN, ptI, actp, I, N); break; 773 case 16: rc = rec_gemm(g, OMEGA_GPU_Q38_K_DOWN, g->layer[l].dn, N, segd + 3u * OMEGA_GEMM_SEG_BYTES, actp, oN); break; 774 case 17: { 775 const int last = l + 1 == g->cfg.layers; 776 const uint64_t nw = last ? va_of(g, L->fnorm) : in_norm + 2 * H * 4u; 777 rc = rec_res_rows(g, K_ADDNORM_R, x0, oN, nw, last ? va_of(g, hidden_of(g, 0)) : xq0, last && !g->head8 ? 1.0f : SCALE_2P64, N); 778 for (uint32_t h = 0; h < N && rc == OMEGA_GPU_DGRAPH_OK; h++) { if (last) g->hidden_ready[h] = 1; else g->xq_layer[h] = (int)l + 1; } 779 break; 780 } 781 default: rc = fail(g, "q38 graph: unknown om plan op", OMEGA_GPU_DGRAPH_CHIP_FAIL); break; 782 } 783 if (rc == OMEGA_GPU_DGRAPH_OK) rc = mark_indep_if(g, ind); 784 } 785 return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc); 786 } 787 788 static int rec_argmax(OmegaGpuQ38Graph *g, uint64_t logits, uint64_t token) { 789 uint32_t a0[OMEGA_DEC_AM_WORDS], a1[OMEGA_DEC_AM_WORDS]; memset(a0, 0, sizeof a0); memset(a1, 0, sizeof a1); 790 const uint64_t amv = va_of(g, g->lo.am_v), ami = va_of(g, g->lo.am_i); 791 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); 792 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); 793 int 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); 794 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); 795 return rc; 796 } 797 798 int omega_gpu_q38_graph_record_final(OmegaGpuQ38Graph *g) { 799 return omega_gpu_q38_graph_record_head_final(g, 0); 800 } 801 802 int omega_gpu_q38_graph_record_head_final(OmegaGpuQ38Graph *g, uint32_t h) { 803 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; 804 if (g->failed) return OMEGA_GPU_DGRAPH_CHIP_FAIL; 805 if (h >= g->stroke_heads) return fail(g, "q38 graph: head not in the stroke", OMEGA_GPU_DGRAPH_BAD_ARGS); 806 if (!g->final_set) return fail(g, "q38 graph: final norm not set", OMEGA_GPU_DGRAPH_BAD_ARGS); 807 int rc = ensure_room(g, 12u); 808 if (rc != OMEGA_GPU_DGRAPH_OK) return rc; 809 const uint64_t x = va_of(g, x_of(g, h)), hidden = va_of(g, hidden_of(g, h)), logits = va_of(g, logits_of(g, h)), token = va_of(g, token_of(g, h)); 810 if (!g->hidden_ready[h]) rc = rec_res(g, K_NORM, x, 0, va_of(g, g->lo.fnorm), hidden, g->head8 ? SCALE_2P64 : 1.0f); 811 const uint64_t amv = va_of(g, g->lo.am_v), ami = va_of(g, g->lo.am_i); 812 if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_q38_graph_exact_head_active_head(g, h)) { 813 /* HUM L314 two-pass head at hidden width H: h8 = hidden * 2^64; s = screen . h8; lo/hi = s -/+ c|hidden|; 814 * tau = max lo; logits = lm_head . hidden where hi >= tau, -inf elsewhere. */ 815 uint32_t xs[OMEGA_DEC_XB_WORDS], xb[OMEGA_DEC_XB_WORDS], t0[OMEGA_DEC_AM_WORDS], t1[OMEGA_DEC_AM_WORDS]; 816 memset(xs, 0, sizeof xs); memset(xb, 0, sizeof xb); memset(t0, 0, sizeof t0); memset(t1, 0, sizeof t1); 817 put64(xs, OMEGA_DEC_XB_S, hidden); put64(xs, OMEGA_DEC_XB_LO, g->xh.h8); xs[(OMEGA_DEC_XB_N - 0x380) / 4] = H; 818 put64(xb, OMEGA_DEC_XB_S, g->xh.s); put64(xb, OMEGA_DEC_XB_C, g->xh.c); put64(xb, OMEGA_DEC_XB_H, hidden); 819 put64(xb, OMEGA_DEC_XB_LO, g->xh.lo); put64(xb, OMEGA_DEC_XB_HI, g->xh.hi); xb[(OMEGA_DEC_XB_N - 0x380) / 4] = g->cfg.vocab; 820 put64(t0, OMEGA_DEC_AM_V, g->xh.lo); put64(t0, OMEGA_DEC_AM_I, 0); put64(t0, OMEGA_DEC_AM_OV, amv); put64(t0, OMEGA_DEC_AM_OI, ami); 821 put64(t1, OMEGA_DEC_AM_V, amv); put64(t1, OMEGA_DEC_AM_I, ami); put64(t1, OMEGA_DEC_AM_OV, g->xh.tau); put64(t1, OMEGA_DEC_AM_OI, g->xh.tau + 4u); 822 rc = add_glue(g, OMEGA_GPU_Q38_K_LM_HEAD, K_XS, H / OMEGA_DEC_THREADS, 1, H, xs, OMEGA_DEC_XB_WORDS); 823 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_fp8(g, OMEGA_GPU_Q38_K_LM_HEAD, g->xh.screen, g->xh.tab, g->xh.h8, g->xh.s); 824 if (rc == OMEGA_GPU_DGRAPH_OK) 825 rc = add_glue(g, OMEGA_GPU_Q38_K_LM_HEAD, g->mutant == 6 ? K_XB_M1 : K_XB, (g->cfg.vocab + OMEGA_DEC_THREADS - 1) / OMEGA_DEC_THREADS, 1, 826 g->cfg.vocab, xb, OMEGA_DEC_XB_WORDS); 827 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, t0, OMEGA_DEC_AM_WORDS); 828 if (rc == OMEGA_GPU_DGRAPH_OK) rc = add_glue(g, OMEGA_GPU_Q38_K_ARGMAX, K_AM1, 1, 1, OMEGA_DEC_ARGMAX_SPAN, t1, OMEGA_DEC_AM_WORDS); 829 if (rc == OMEGA_GPU_DGRAPH_OK) 830 rc = after_op(g, OMEGA_GPU_Q38_K_LM_HEAD, omega_gpu_bf16_gemv_mask_record(g->cl, g->cfg.lm_head, hidden, logits, g->xh.hi, g->xh.tau)); 831 } else if (rc == OMEGA_GPU_DGRAPH_OK) 832 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) 833 : after_op(g, OMEGA_GPU_Q38_K_LM_HEAD, omega_gpu_bf16_gemv_record(g->cl, g->cfg.lm_head, hidden, logits)); 834 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_argmax(g, logits, token); 835 return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc); 836 } 837 838 static int record_final_heads(OmegaGpuQ38Graph *g) { 839 const uint32_t N = g->stroke_heads; 840 if (!g->final_set) return fail(g, "q38 graph: final norm not set", OMEGA_GPU_DGRAPH_BAD_ARGS); 841 for (uint32_t h = 0; h < N; h++) 842 if (!g->hidden_ready[h]) return fail(g, "q38 graph: batch final before the last layer", OMEGA_GPU_DGRAPH_BAD_ARGS); 843 int all_exact = 1, any_exact = 0; 844 for (uint32_t h = 0; h < N; h++) { 845 const int ex = omega_gpu_q38_graph_exact_head_active_head(g, h); 846 all_exact &= ex; any_exact |= ex; 847 } 848 const uint64_t pcfg[8] = { N, H, OMEGA_BW_QMD_MAX_SHARED_BYTES, (uint64_t)all_exact, (uint64_t)any_exact, (uint64_t)(g->head8 != NULL), 0, 0 }; 849 uint32_t plan[384]; uint64_t nops = 0; 850 if (omega2_q38_plan_final(&nops, pcfg, 8, plan, 384) != OMEGA_OK || nops == 0) 851 return fail(g, "q38 graph: om/gpu/q38_plan final failed", OMEGA_GPU_DGRAPH_CHIP_FAIL); 852 int rc = ensure_room(g, (uint32_t)nops + 4u); 853 const size_t n = g->cfg.vocab, P = (size_t)g->am_ctas * OMEGA_DEC_ARGMAX_SPAN; 854 const uint64_t ptH = va_of(g, g->lo.ptab), xp = va_of(g, g->lo.xp); 855 for (uint64_t k = 0; k < nops && rc == OMEGA_GPU_DGRAPH_OK; k++) { 856 const uint32_t op_id = plan[k * 4 + 0], ind = plan[k * 4 + 1], a0 = plan[k * 4 + 2], a1 = plan[k * 4 + 3]; 857 switch (op_id) { 858 case 101: { /* FOP_XS_ALL: scale hidden[0..N-1] * 2^64 -> xh.h8[0..N-1] */ 859 uint32_t xs[OMEGA_DEC_XB_WORDS]; memset(xs, 0, sizeof xs); 860 put64(xs, OMEGA_DEC_XB_S, va_of(g, hidden_of(g, 0))); put64(xs, OMEGA_DEC_XB_LO, g->xh.h8); 861 xs[(OMEGA_DEC_XB_N - 0x380) / 4] = N * H; 862 rc = add_glue(g, OMEGA_GPU_Q38_K_LM_HEAD, K_XS, (N * H) / OMEGA_DEC_THREADS, 1, (size_t)N * H, xs, OMEGA_DEC_XB_WORDS); 863 break; 864 } 865 case 102: /* FOP_PERM_XH: permute xh.h8[0..N-1] -> xp */ 866 rc = rec_perm(g, K_PERM_XQ, g->xh.h8, ptH, xp, H, N); 867 break; 868 case 103: /* FOP_GEMM_SCREEN: single batched FP8 GEMM over xh.screen for all N heads */ 869 rc = rec_gemm(g, OMEGA_GPU_Q38_K_LM_HEAD, g->xh.screen, N, va_of(g, g->lo.xh_seg), xp, g->xh.s); 870 break; 871 case 104: { /* FOP_XB: bounds lo/hi for head a0 */ 872 uint32_t xb[OMEGA_DEC_XB_WORDS]; memset(xb, 0, sizeof xb); 873 put64(xb, OMEGA_DEC_XB_S, g->xh.s + (uint64_t)a0 * n * 4u); 874 put64(xb, OMEGA_DEC_XB_C, g->xh.c); 875 put64(xb, OMEGA_DEC_XB_H, va_of(g, hidden_of(g, a0))); 876 put64(xb, OMEGA_DEC_XB_LO, g->xh.lo + (uint64_t)a0 * P * 4u); 877 put64(xb, OMEGA_DEC_XB_HI, g->xh.hi + (uint64_t)a0 * n * 4u); 878 xb[(OMEGA_DEC_XB_N - 0x380) / 4] = g->cfg.vocab; 879 rc = add_glue(g, OMEGA_GPU_Q38_K_LM_HEAD, g->mutant == 6 ? K_XB_M1 : K_XB, (g->cfg.vocab + OMEGA_DEC_THREADS - 1) / OMEGA_DEC_THREADS, 1, 880 g->cfg.vocab, xb, OMEGA_DEC_XB_WORDS); 881 break; 882 } 883 case 105: { /* FOP_TAU_AM0: stage-1 argmax on xh.lo[a0] -> amv[a0]/ami[a0] */ 884 uint32_t t0[OMEGA_DEC_AM_WORDS]; memset(t0, 0, sizeof t0); 885 put64(t0, OMEGA_DEC_AM_V, g->xh.lo + (uint64_t)a0 * P * 4u); put64(t0, OMEGA_DEC_AM_I, 0); 886 put64(t0, OMEGA_DEC_AM_OV, va_of(g, amv_of(g, a0))); put64(t0, OMEGA_DEC_AM_OI, va_of(g, ami_of(g, a0))); 887 rc = add_glue(g, OMEGA_GPU_Q38_K_ARGMAX, K_AM0, g->am_ctas, 1, P, t0, OMEGA_DEC_AM_WORDS); 888 break; 889 } 890 case 106: { /* FOP_TAU_AM1: stage-2 argmax -> xh.tau[a0] */ 891 uint32_t t1[OMEGA_DEC_AM_WORDS]; memset(t1, 0, sizeof t1); 892 const uint64_t tau_h = g->xh.tau + (uint64_t)a0 * 16u; 893 put64(t1, OMEGA_DEC_AM_V, va_of(g, amv_of(g, a0))); put64(t1, OMEGA_DEC_AM_I, va_of(g, ami_of(g, a0))); 894 put64(t1, OMEGA_DEC_AM_OV, tau_h); put64(t1, OMEGA_DEC_AM_OI, tau_h + 4u); 895 rc = add_glue(g, OMEGA_GPU_Q38_K_ARGMAX, K_AM1, 1, 1, OMEGA_DEC_ARGMAX_SPAN, t1, OMEGA_DEC_AM_WORDS); 896 break; 897 } 898 case 107: /* FOP_BF16_MASK: masked bf16 lm_head for head a0 */ 899 rc = after_op(g, OMEGA_GPU_Q38_K_LM_HEAD, 900 omega_gpu_bf16_gemv_mask_record(g->cl, g->cfg.lm_head, va_of(g, hidden_of(g, a0)), va_of(g, logits_of(g, a0)), 901 g->xh.hi + (uint64_t)a0 * n * 4u, g->xh.tau + (uint64_t)a0 * 16u)); 902 break; 903 case 108: /* FOP_BF16_XBATCH: xbatch bf16 lm_head for heads [a0, a0 + a1) */ 904 rc = after_op(g, OMEGA_GPU_Q38_K_LM_HEAD, 905 omega_gpu_bf16_gemv_xbatch_record(g->cl, g->cfg.lm_head, a1, va_of(g, hidden_of(g, a0)), 906 va_of(g, logits_of(g, a0)), g->am_ctas * OMEGA_DEC_ARGMAX_SPAN)); 907 break; 908 case 109: { /* FOP_LOG_AM0: stage-1 argmax on logits[a0] -> amv[a0]/ami[a0] */ 909 uint32_t l0[OMEGA_DEC_AM_WORDS]; memset(l0, 0, sizeof l0); 910 put64(l0, OMEGA_DEC_AM_V, va_of(g, logits_of(g, a0))); put64(l0, OMEGA_DEC_AM_I, 0); 911 put64(l0, OMEGA_DEC_AM_OV, va_of(g, amv_of(g, a0))); put64(l0, OMEGA_DEC_AM_OI, va_of(g, ami_of(g, a0))); 912 rc = add_glue(g, OMEGA_GPU_Q38_K_ARGMAX, K_AM0, g->am_ctas, 1, P, l0, OMEGA_DEC_AM_WORDS); 913 break; 914 } 915 case 110: { /* FOP_LOG_AM1: stage-2 argmax -> token[a0] */ 916 uint32_t l1[OMEGA_DEC_AM_WORDS]; memset(l1, 0, sizeof l1); 917 const uint64_t tok_h = va_of(g, token_of(g, a0)); 918 put64(l1, OMEGA_DEC_AM_V, va_of(g, amv_of(g, a0))); put64(l1, OMEGA_DEC_AM_I, va_of(g, ami_of(g, a0))); 919 put64(l1, OMEGA_DEC_AM_OV, tok_h + 4u); put64(l1, OMEGA_DEC_AM_OI, tok_h); 920 rc = add_glue(g, OMEGA_GPU_Q38_K_ARGMAX, K_AM1, 1, 1, OMEGA_DEC_ARGMAX_SPAN, l1, OMEGA_DEC_AM_WORDS); 921 break; 922 } 923 case 111: rc = omega_gpu_q38_graph_record_head_final(g, a0); break; 924 default: rc = fail(g, "q38 graph: unknown om final op", OMEGA_GPU_DGRAPH_CHIP_FAIL); break; 925 } 926 if (rc == OMEGA_GPU_DGRAPH_OK) rc = mark_indep_if(g, ind); 927 } 928 return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc); 929 } 930 931 int omega_gpu_q38_graph_end_token(OmegaGpuQ38Graph *g, uint32_t *token, OmegaGpuDecodeGraphStats *st) { 932 return omega_gpu_q38_graph_end_stroke(g, token, st); 933 } 934 935 int omega_gpu_q38_graph_end_stroke(OmegaGpuQ38Graph *g, uint32_t *tokens, OmegaGpuDecodeGraphStats *st) { 936 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; 937 int rc = g->failed ? OMEGA_GPU_DGRAPH_CHIP_FAIL : OMEGA_GPU_DGRAPH_OK; 938 const uint64_t t_rec = omega_gpu_session_now_ns(); 939 if (rc == OMEGA_GPU_DGRAPH_OK) rc = submit_now(g); 940 for (uint32_t h = 0; h < g->stroke_heads && rc == OMEGA_GPU_DGRAPH_OK; h++) 941 if (omega_gpu_decode_attn256_counters_check(g->attn_h[h]) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); 942 if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_session_sync_to_cpu(&g->buf) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); 943 for (uint32_t h = 0; h < g->stroke_heads && rc == OMEGA_GPU_DGRAPH_OK; h++) { 944 const uint32_t t = ((const uint32_t *)cpu_of(g, token_of(g, h)))[0]; 945 if (t == TOKEN_POISON || t >= g->cfg.vocab) { 946 char e[96]; omega_gpu_fmt(e, sizeof e, "q38 graph: head %u token output %u not written or out of range", h, t); 947 rc = fail(g, e, OMEGA_GPU_DGRAPH_CHIP_FAIL); 948 } else if (tokens) tokens[h] = t; 949 } 950 omega_gpu_cmdlist_reset(g->cl); 951 g->in_token = 0; 952 const uint64_t t_end = omega_gpu_session_now_ns(); 953 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; } 954 omega_gpu_session_unlock(); 955 return rc; 956 } 957 958 int omega_gpu_q38_graph_token(OmegaGpuQ38Graph *g, const float *x_embed, uint32_t pos, uint32_t *token, OmegaGpuDecodeGraphStats *st) { 959 if (!g) return OMEGA_GPU_DGRAPH_BAD_ARGS; 960 if (omega_gpu_q38_graph_begin_token(g, x_embed) == OMEGA_GPU_DGRAPH_BAD_ARGS) return OMEGA_GPU_DGRAPH_BAD_ARGS; 961 for (uint32_t l = 0; l < g->cfg.layers && !g->failed; l++) (void)omega_gpu_q38_graph_record_layer(g, l, pos); 962 if (!g->failed) (void)omega_gpu_q38_graph_record_final(g); 963 return omega_gpu_q38_graph_end_token(g, token, st); 964 } 965 966 int omega_gpu_q38_graph_stroke(OmegaGpuQ38Graph *g, const float *x_embed, const uint32_t *pos, uint32_t heads, uint32_t *tokens, 967 OmegaGpuDecodeGraphStats *st) { 968 if (!g || !pos) return OMEGA_GPU_DGRAPH_BAD_ARGS; 969 if (omega_gpu_q38_graph_begin_stroke(g, x_embed, heads) == OMEGA_GPU_DGRAPH_BAD_ARGS) return OMEGA_GPU_DGRAPH_BAD_ARGS; 970 for (uint32_t l = 0; l < g->cfg.layers && !g->failed; l++) { 971 if (g->batch) (void)omega_gpu_q38_graph_record_layer_heads(g, l, pos); 972 else for (uint32_t h = 0; h < heads && !g->failed; h++) (void)omega_gpu_q38_graph_record_head_layer(g, h, l, pos[h]); 973 } 974 if (g->batch && !g->head8) { if (!g->failed) (void)record_final_heads(g); } 975 else for (uint32_t h = 0; h < heads && !g->failed; h++) (void)omega_gpu_q38_graph_record_head_final(g, h); 976 return omega_gpu_q38_graph_end_stroke(g, tokens, st); 977 } 978 979 int omega_gpu_q38_graph_read(OmegaGpuQ38Graph *g, float *x, float *hidden, float *logits) { 980 return omega_gpu_q38_graph_read_head(g, 0, x, hidden, logits); 981 } 982 983 int omega_gpu_q38_graph_read_head(OmegaGpuQ38Graph *g, uint32_t h, float *x, float *hidden, float *logits) { 984 if (!g || g->in_token || h >= g->heads) return OMEGA_GPU_DGRAPH_BAD_ARGS; 985 omega_gpu_session_lock(); 986 int rc = omega_gpu_session_sync_to_cpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL; 987 if (rc == OMEGA_GPU_DGRAPH_OK) { 988 if (x) memcpy(x, cpu_of(g, x_of(g, h)), H * 4u); 989 if (hidden) memcpy(hidden, cpu_of(g, hidden_of(g, h)), H * 4u); 990 if (hidden && g->head8) for (unsigned i = 0; i < H; i++) hidden[i] *= 0x1p-64f; /* exact: a power of two */ 991 if (logits) memcpy(logits, cpu_of(g, logits_of(g, h)), (size_t)g->cfg.vocab * 4u); 992 } 993 omega_gpu_session_unlock(); 994 return rc; 995 } 996 997 int omega_gpu_q38_graph_read_buf(OmegaGpuQ38Graph *g, int which, float *out, uint32_t n) { 998 if (!g || g->in_token || !out || which < 0 || which >= OMEGA_GPU_Q38_BUF_COUNT) return OMEGA_GPU_DGRAPH_BAD_ARGS; 999 const struct { size_t off; uint32_t cap; } b[OMEGA_GPU_Q38_BUF_COUNT] = { 1000 [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 }, 1001 [OMEGA_GPU_Q38_BUF_AB] = { g->lo.ab, g->ab }, [OMEGA_GPU_Q38_BUF_AGG] = { g->lo.agg, g->amix }, 1002 [OMEGA_GPU_Q38_BUF_ATT] = { g->lo.att, g->mout }, [OMEGA_GPU_Q38_BUF_O] = { g->lo.o, H }, 1003 [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 }, 1004 }; 1005 if (n > b[which].cap) return OMEGA_GPU_DGRAPH_BAD_ARGS; 1006 omega_gpu_session_lock(); 1007 int rc = OMEGA_GPU_DGRAPH_OK; 1008 size_t from = b[which].off; 1009 if (from >= g->lo.split && n) { 1010 /* a chip-only region: K_COPY moves ceil(n / 256) rows of 256 floats into stage (every cap is a multiple of 256) */ 1011 uint32_t w[OMEGA_DEC_GA_WORDS]; memset(w, 0, sizeof w); 1012 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)); 1013 const Kern *k = &g->k[K_COPY]; 1014 OmegaGpuLaunch L; memset(&L, 0, sizeof L); 1015 L.code_va = k->code.va; L.gpr_count = k->gpr; L.threads_x = OMEGA_DEC_THREADS; L.threads_y = 1; L.grid_x = 1; L.grid_y = (n + COPY_ROW - 1) / COPY_ROW; 1016 L.num_elements = (size_t)L.grid_y * COPY_ROW; L.args = w; L.n_args = OMEGA_DEC_GA_WORDS; L.timeout_ms = TIMEOUT_MS; 1017 if (!omega_gpu_session_open() || omega_gpu_session_launch(&L, NULL, NULL) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; 1018 from = g->lo.stage; 1019 } 1020 if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_session_sync_to_cpu(&g->buf) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; 1021 if (rc == OMEGA_GPU_DGRAPH_OK) memcpy(out, cpu_of(g, from), (size_t)n * 4u); 1022 omega_gpu_session_unlock(); 1023 return rc; 1024 } 1025 1026 /* ---- timeline ---- */ 1027 void omega_gpu_q38_graph_set_stamps(OmegaGpuQ38Graph *g, int on) { if (g && !g->in_token) g->stamps = on ? 1 : 0; } 1028 1029 int omega_gpu_q38_graph_stamps(const OmegaGpuQ38Graph *g, uint64_t *stamps, uint8_t *kinds, uint32_t cap) { 1030 if (!g || g->in_token || !stamps || !kinds) return OMEGA_GPU_DGRAPH_BAD_ARGS; 1031 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; } 1032 if (cap < g->n_stamp) { omega_gpu_session_set_error("q38 graph: timeline buffers too small"); return OMEGA_GPU_DGRAPH_BAD_ARGS; } 1033 memcpy(stamps, g->stamp, (size_t)g->n_stamp * sizeof stamps[0]); 1034 memcpy(kinds, g->kind, (size_t)g->n_kind); 1035 return (int)g->n_kind; 1036 } 1037 1038 const char *omega_gpu_q38_graph_kind_name(uint8_t kind) { 1039 static const char *const names[OMEGA_GPU_Q38_K_COUNT] = { "norm", "mix", "ab", "gdn", "gather", "attn", "gate", "o_proj", "res", 1040 "gate_up", "swiglu", "down", "lm_head", "argmax", "perm" }; 1041 return kind < OMEGA_GPU_Q38_K_COUNT ? names[kind] : "?"; 1042 }