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_q36_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_q36_graph.c` 1160 lines ยท 85469 bytes Raw Blame History 1 /* 2 * omega_gpu_q36_graph.c: one Qwen3.6-35B-A3B decode token as one command list (see the header). Records the FP8 / bf16 GEMV, 3 * GDN, hd256 attention and router APIs' *_record launches and the omega_blackwell_codegen_dec glue kernels at width 2048 into an 4 * omega_gpu_cmdlist. Buffers: buf from omega_gpu_session_alloc (GPU-uncached, nvos.h:1117) holds every region the host 5 * touches: x (before), token and logits (after), the tables; dbuf (GPU-cached, omega_gpu_mem_cached.h) holds the chip-only 6 * intermediates (see Layout). 7 */ 8 #include "omega_gpu_q36_graph.h" 9 #include "omega_gpu_cmdlist.h" 10 #include "omega_blackwell_codegen_dec.h" 11 #include "omega_gpu_session.h" 12 #include "omega_gpu_fmt.h" 13 #include "omega_gpu_mem_cached.h" 14 #include "omega_gpu_fp8gemm_api.h" 15 #include 16 #include 17 18 #define TIMEOUT_MS 20000ull 19 #define SCALE_2P64 `18446744073709551616`.0f 20 #define NEG_PAD (-1e30f) 21 #define TOKEN_POISON 0xffffffffu 22 #define TOKEN_STRIDE 256u /* bytes per head's token slot (two u32, 256-aligned) */ 23 #define H OMEGA_GPU_Q36_HIDDEN 24 #define EI OMEGA_GPU_Q36_EXPERT_INTER 25 #define NG OMEGA_GPU_Q36_GROUPS 26 27 /* glue kernels */ 28 enum { K_NORM, K_ADD, K_CN, K_SW, K_GATHER, K_GATE, K_AM0, K_AM1, K_SW_M1, K_GATHER_M1, K_GATE_M1, K_CN_M3, K_COPY, 29 K_NORM_R, K_ADD_R, K_PERM_XQ, K_PERM_O, K_PERM_XO, K_PERM_ACT, K_GROUP, K_XS, K_XB, K_XB_M1, 30 K_CN_R, K_CN_R_M3, K_GATE_R, K_COUNT }; /* K_NORM_R .. K_GROUP: the batch (v2) kernels; K_XS .. K_XB_M1: the exact head; K_CN_R: COMBINE|NORM of every head in one launch (L356) */ 31 #define SEGD_ROWS 2u /* dense segment entries per layer in the batch: mix, o */ 32 #define COPY_ROW 256u /* K_COPY: GATHER rows of 256 floats through the identity table */ 33 34 typedef struct { OmegaGpuMem code; uint32_t gpr; } Kern; 35 36 /* byte offsets into two buffers, every region 256-byte aligned (RES loads 16-byte vectors). [0, split): buf, GPU-uncached, 37 * every region the host writes or reads. [split, bytes): dbuf, the chip-only intermediates, GPU-cached unless 38 * AIEN_OMEGA_Q36_BUF_CACHED=0 (measured 2026-10-19: every FP8 GEMV CTA reads the whole x' row, and uncached each of those 39 * reads goes to DRAM; cached, down 2100 -> 1735 us and mix 1596 -> 1458 us over 4 layers, runs/probe-buf). read_buf copies 40 * 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, ptab, split; 43 size_t xq, mix, agg, att, o, gu, act, dn; 44 /* the batch (v2, L312): every conversation of a layer in one set of launches. N = heads laid out; R = N * NG expert rows */ 45 size_t xp, mixN, abN, aggN, attN, op, oN, rlb, sgu, sdn, xmap, ymap, xp2, guN, actN, actp, dnN, bytes; 46 uint32_t lb_stride; /* bytes per conversation's router buffer (rlb), 256-aligned */ 47 } Layout; 48 49 struct OmegaGpuQ36Graph { 50 OmegaGpuQ36GraphConfig cfg; 51 uint32_t am_ctas; /* stage-1 ARGMAX CTAs = ceil(vocab / 2048) */ 52 Layout lo; 53 OmegaGpuMem buf, dbuf; /* the host-visible regions; the chip-only regions (see Layout) */ 54 Kern k[K_COUNT]; 55 OmegaGpuCmdList *cl; 56 struct { const OmegaGpuFp8Tensor *mix, *o, *gu, *dn; const OmegaGpuBf16Tensor *ab; uint32_t kind_index; int attn, set; } layer[OMEGA_GPU_Q36_MAX_LAYERS]; 57 int final_set; 58 const OmegaGpuFp8Tensor *head8; /* set_lm_head_fp8: FP8 lm_head (hidden is then written x 2^64, read() undoes it) */ 59 /* heads (strokes): slot h of x, hidden, logits and token; head 0's state objects are cfg.gdn / cfg.attn */ 60 uint32_t heads; /* slots laid out (create_heads); 1 = the plain graph */ 61 OmegaGpuGdn *gdn_h[OMEGA_GPU_Q36_MAX_HEADS]; 62 OmegaGpuDecodeAttn256Cache *attn_h[OMEGA_GPU_Q36_MAX_HEADS]; 63 struct { /* set_exact_head: the HUM L314 two-pass head (bf16 lm_head only) */ 64 const OmegaGpuFp8Tensor *screen; /* fp8b copy of lm_head (128 x 128 block scales) */ 65 OmegaGpuMem mem; /* h8 | s | c | hi | lo (am_ctas * 2048, padded) | tau | tab */ 66 uint64_t h8, s, c, hi, lo, tau, tab; 67 uint8_t on[OMEGA_GPU_Q36_MAX_HEADS]; /* per head: its next tokens use it (greedy rows); set_exact_head_on sets every head */ 68 } xh; 69 /* token / stroke state */ 70 int in_token, failed; 71 uint32_t stroke_heads; /* heads in the open stroke */ 72 int xq_layer[OMEGA_GPU_Q36_MAX_HEADS]; /* per head: layer whose input-norm x' sits in the head's xq slot, -1 none 73 * (COMBINE|NORM of layer l writes layer l+1's x' there: the one value that 74 * crosses layers, so it is per head; 02:50Z, found by the stroke gate) */ 75 int hidden_ready[OMEGA_GPU_Q36_MAX_HEADS]; 76 uint64_t t_begin; 77 OmegaGpuDecodeGraphStats acc; 78 int flush_each, mutant; 79 int batch; /* 1: stroke() records every conversation of a layer at once (v2); set_batch */ 80 /* the prefill (L369): one conversation's prompt chunk as rows through the batch's GEMMs; GDN and attention per token in order */ 81 struct { uint32_t cap; OmegaGpuMem hb, db; size_t x, rlb, sgu, sdn, sgu2, sdn2, xmap, ymap, xmap2, ymap2; 82 size_t xq, xp, mixN, abN, aggN, attN, op, oN, xp2, guN, actN, actp, dnN; int mutant; 83 uint64_t streamed; /* fp8 GEMM weight bytes every prefill call so far streamed (one pass per mt rows) */ 84 int attn_multi; /* 1 (default): each attention layer in one prep + attend launch pair; AIEN_OMEGA_Q36_PF_ATTN=0: one decode step per token */ 85 int gdn_multi; /* 1 (default): the GDN layers in one multi-token launch; AIEN_OMEGA_Q36_PF_GDN=0: one decode step per token */ } pf; 86 int stamps; 87 uint32_t n_kind, tagged_in_list, n_stamp; 88 uint8_t kind[OMEGA_GPU_CMDLIST_MAX_LAUNCHES]; 89 uint64_t stamp[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1]; 90 }; 91 92 static size_t al(size_t v) { return (v + 255u) & ~(size_t)255u; } 93 static uint64_t va_of(const OmegaGpuQ36Graph *g, size_t off) { return off < g->lo.split ? g->buf.va + off : g->dbuf.va + (off - g->lo.split); } 94 static void *cpu_of(const OmegaGpuQ36Graph *g, size_t off) { return (uint8_t *)g->buf.cpu + off; } /* host regions only (off < split) */ 95 static size_t x_of(const OmegaGpuQ36Graph *g, uint32_t h) { return g->lo.x + (size_t)h * H * 4u; } 96 static size_t hidden_of(const OmegaGpuQ36Graph *g, uint32_t h) { return g->lo.hidden + (size_t)h * H * 4u; } 97 static size_t xq_of(const OmegaGpuQ36Graph *g, uint32_t h) { return g->lo.xq + (size_t)h * H * 4u; } 98 static size_t logits_of(const OmegaGpuQ36Graph *g, uint32_t h) { return g->lo.logits + (size_t)h * g->am_ctas * OMEGA_DEC_ARGMAX_SPAN * 4u; } 99 static size_t token_of(const OmegaGpuQ36Graph *g, uint32_t h) { return g->lo.token + (size_t)h * TOKEN_STRIDE; } 100 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); } 101 static void putf(uint32_t *a, unsigned off, float f) { memcpy(&a[(off - 0x380) / 4], &f, 4); } 102 static int fail(OmegaGpuQ36Graph *g, const char *what, int rc) { 103 if (g) g->failed = 1; 104 if (what) omega_gpu_session_set_error(what); 105 return rc; 106 } 107 108 void omega_q36_gather_table(uint32_t *tab) { 109 const uint32_t R = OMEGA_GPU_Q36_ROTARY, QP = 16u * 512u, KP = QP + 2u * 256u; 110 for (uint32_t n = 0; n < OMEGA_GPU_Q36_ATT_MIX; n++) { 111 uint32_t s; 112 if (n < 4096u) s = (n / 256u) * 512u + omega_dattn256_perm_slot(n % 256u, R); /* q, permuted */ 113 else if (n < 4608u) s = QP + ((n - 4096u) / 256u) * 256u + omega_dattn256_perm_slot(n % 256u, R); /* k, permuted */ 114 else if (n < 5120u) s = KP + (n - 4608u); /* v */ 115 else s = ((n - 5120u) / 256u) * 512u + 256u + (n - 5120u) % 256u; /* gate */ 116 tab[n] = s; 117 } 118 } 119 120 /* lock held */ 121 /* GATHER table of the FP8 GEMM's x' lane layout (omega_fp8gemm_permute_x, lane bytes 16: k a multiple of 512): permuted index 122 * i = s*512 + (w*32 + L)*4 + e holds source column c = s*512 + L*16 + w*4 + e, so tab[i] = c. */ 123 void omega_q36_perm_table(uint32_t *tab, uint32_t k) { 124 for (uint32_t s0 = 0; s0 < k; s0 += 512u) 125 for (uint32_t L = 0; L < 32u; L++) 126 for (uint32_t w = 0; w < 4u; w++) 127 for (uint32_t e = 0; e < 4u; e++) tab[s0 + (w * 32u + L) * 4u + e] = s0 + L * 16u + w * 4u + e; 128 } 129 130 static void write_dense_segments(OmegaGpuQ36Graph *g, uint32_t heads); 131 132 static int kern_build(Kern *k, const OmegaDecSpec *s) { 133 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); 134 if (!prog) return OMEGA_GPU_DGRAPH_CODEGEN_FAIL; 135 int rc = OMEGA_GPU_DGRAPH_OK; 136 size_t need = 0; 137 if (omega_blackwell_codegen_dec_prog(s, prog) != 0 || (need = omega_bw_program_code_bytes(prog)) == 0) rc = OMEGA_GPU_DGRAPH_CODEGEN_FAIL; 138 else if (omega_gpu_session_alloc_code(need, &k->code) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; 139 else { 140 OmegaBlackwellKernel kk; 141 if (omega_blackwell_kernel_encode_into(prog, k->code.cpu, need, &kk) != 0 || kk.code_size != need) rc = OMEGA_GPU_DGRAPH_CODEGEN_FAIL; 142 else if (omega_gpu_session_sync_to_gpu(&k->code) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; 143 else { k->gpr = omega_bw_gpr_declared(kk.gpr_count); if (k->gpr < 64u) k->gpr = 64u; } /* as the decode graph */ 144 if (rc != OMEGA_GPU_DGRAPH_OK) { omega_gpu_session_free(&k->code); memset(k, 0, sizeof *k); } 145 } 146 omega_gpu_host_free(prog); 147 return rc; 148 } 149 150 void omega_gpu_q36_graph_free(OmegaGpuQ36Graph *g) { 151 if (!g) return; 152 if (g->cl) omega_gpu_cmdlist_free(g->cl); 153 omega_gpu_session_lock(); 154 for (unsigned i = 0; i < K_COUNT; i++) if (g->k[i].code.cpu) omega_gpu_session_free(&g->k[i].code); 155 if (g->buf.cpu) omega_gpu_session_free(&g->buf); 156 if (g->dbuf.cpu) omega_gpu_session_free(&g->dbuf); 157 if (g->pf.hb.cpu) omega_gpu_session_free(&g->pf.hb); 158 if (g->pf.db.cpu) omega_gpu_session_free(&g->pf.db); 159 if (g->xh.mem.cpu) omega_gpu_session_free(&g->xh.mem); 160 omega_gpu_session_unlock(); 161 omega_gpu_host_free(g); 162 } 163 164 int omega_gpu_q36_graph_create(const OmegaGpuQ36GraphConfig *cfg, OmegaGpuQ36Graph **out) { return omega_gpu_q36_graph_create_heads(cfg, 1, out); } 165 166 int omega_gpu_q36_graph_create_heads(const OmegaGpuQ36GraphConfig *cfg, uint32_t heads, OmegaGpuQ36Graph **out) { 167 if (out) *out = NULL; 168 if (heads == 0 || heads > OMEGA_GPU_Q36_MAX_HEADS) return OMEGA_GPU_DGRAPH_BAD_ARGS; 169 if (!cfg || !out || cfg->layers == 0 || cfg->layers > OMEGA_GPU_Q36_MAX_LAYERS || !cfg->gdn || !cfg->attn || !cfg->router || !cfg->lm_head || 170 cfg->vocab == 0 || cfg->vocab > OMEGA_DEC_ARGMAX_SPAN * OMEGA_DEC_ARGMAX_SPAN) return OMEGA_GPU_DGRAPH_BAD_ARGS; 171 if (cfg->layers < 64u && (cfg->attn_mask >> cfg->layers) != 0) return OMEGA_GPU_DGRAPH_BAD_ARGS; 172 uint32_t n = 0, k = 0; 173 omega_gpu_bf16_tensor_shape(cfg->lm_head, &n, &k); 174 if (n != cfg->vocab || k != H) return OMEGA_GPU_DGRAPH_BAD_ARGS; 175 OmegaGpuQ36Graph *g = omega_gpu_host_calloc(1, sizeof *g); 176 if (!g) return OMEGA_GPU_DGRAPH_BAD_ARGS; 177 g->cfg = *cfg; 178 g->am_ctas = (cfg->vocab + OMEGA_DEC_ARGMAX_SPAN - 1) / OMEGA_DEC_ARGMAX_SPAN; 179 g->heads = heads; g->gdn_h[0] = cfg->gdn; g->attn_h[0] = cfg->attn; 180 for (uint32_t h = 0; h < OMEGA_GPU_Q36_MAX_HEADS; h++) g->xq_layer[h] = -1; 181 for (uint32_t l = 0, na = 0, ng = 0; l < cfg->layers; l++) { 182 g->layer[l].attn = (int)((cfg->attn_mask >> l) & 1u); 183 g->layer[l].kind_index = g->layer[l].attn ? na++ : ng++; 184 } 185 Layout *L = &g->lo; size_t o = 0; 186 #define REG(f, bytes) do { L->f = o; o = al(o + (bytes)); } while (0) 187 REG(x, (size_t)H * 4 * heads); REG(ab, OMEGA_GPU_Q36_AB * 4); REG(hidden, (size_t)H * 4 * heads); 188 REG(logits, (size_t)g->am_ctas * OMEGA_DEC_ARGMAX_SPAN * 4 * heads); REG(am_v, OMEGA_DEC_ARGMAX_SPAN * 4); REG(am_i, OMEGA_DEC_ARGMAX_SPAN * 4); 189 REG(token, (size_t)TOKEN_STRIDE * heads); REG(norms, (size_t)cfg->layers * H * 4); REG(fnorm, H * 4); REG(tabs, (size_t)cfg->layers * 4 * 16 + 16); /* + the FP8 lm_head entry */ 190 REG(gtab, OMEGA_GPU_Q36_ATT_MIX * 4); REG(idtab, COPY_ROW * 4); REG(stage, (size_t)NG * H * 4); /* the largest read_buf region (dn) */ 191 REG(segd, (size_t)cfg->layers * SEGD_ROWS * OMEGA_GEMM_SEG_BYTES); REG(ptab, (size_t)(H + OMEGA_GPU_Q36_MIXER_OUT + EI) * 4); /* batch: dense segment entries, the x' lane permutations */ 192 L->split = o; 193 REG(xq, (size_t)H * 4 * heads); REG(mix, OMEGA_GPU_Q36_GDN_MIX * 4); REG(agg, OMEGA_GPU_Q36_ATT_MIX * 4); 194 REG(att, OMEGA_GPU_Q36_MIXER_OUT * 4); REG(o, H * 4); REG(gu, (size_t)NG * 2 * EI * 4); REG(act, (size_t)NG * EI * 4); REG(dn, (size_t)NG * H * 4); 195 { /* batch regions (N rows each, R = N * NG expert rows; x' inputs of the dense GEMMs hold MAX_HEADS rows: 8 rows per pass) */ 196 const size_t N = heads, R = (size_t)heads * NG, M8 = OMEGA_GPU_Q36_MAX_HEADS; 197 L->lb_stride = (uint32_t)al(omega_gpu_router_layout_bytes(cfg->router)); 198 REG(xp, M8 * H * 4); REG(mixN, N * OMEGA_GPU_Q36_GDN_MIX * 4); REG(abN, N * OMEGA_GPU_Q36_AB * 4); REG(aggN, N * OMEGA_GPU_Q36_ATT_MIX * 4); 199 REG(attN, N * OMEGA_GPU_Q36_MIXER_OUT * 4); REG(op, M8 * OMEGA_GPU_Q36_MIXER_OUT * 4); REG(oN, N * H * 4); REG(rlb, N * L->lb_stride); 200 REG(sgu, R * OMEGA_GEMM_SEG_BYTES); REG(sdn, R * OMEGA_GEMM_SEG_BYTES); REG(xmap, (R + OMEGA_DEC_GR_PAD) * 4); REG(ymap, (R + OMEGA_DEC_GR_PAD) * 4); 201 REG(xp2, M8 * H * 4); REG(guN, R * 2 * EI * 4); REG(actN, R * EI * 4); REG(actp, R * EI * 4); REG(dnN, R * H * 4); 202 } 203 #undef REG 204 L->bytes = o; 205 int rc = OMEGA_GPU_DGRAPH_OK; 206 if (omega_gpu_cmdlist_create(OMEGA_GPU_CMDLIST_MAX_LAUNCHES, &g->cl) != 0) { omega_gpu_q36_graph_free(g); return OMEGA_GPU_DGRAPH_CHIP_FAIL; } 207 omega_gpu_session_lock(); 208 const char *pbc = getenv("AIEN_OMEGA_Q36_BUF_CACHED"); 209 int (*const dalloc)(size_t, OmegaGpuMem *) = (pbc && pbc[0] == '0' && pbc[1] == 0) ? omega_gpu_session_alloc : omega_gpu_session_alloc_cached; 210 if (!omega_gpu_session_open() || omega_gpu_session_alloc(L->split, &g->buf) != 0 || dalloc(L->bytes - L->split, &g->dbuf) != 0) 211 rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; 212 OmegaDecSpec spec[K_COUNT] = { 213 [K_NORM] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_NORM }, 214 [K_ADD] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_ADD }, 215 [K_CN] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM, .groups = NG }, 216 [K_SW] = { .kind = OMEGA_DEC_SWIGLU, .width = EI }, 217 [K_GATHER] = { .kind = OMEGA_DEC_GATHER, .width = OMEGA_GPU_Q36_ATT_MIX, .stride = OMEGA_GPU_Q36_ATT_MIX }, 218 [K_GATE] = { .kind = OMEGA_DEC_GATE }, 219 [K_AM0] = { .kind = OMEGA_DEC_ARGMAX, .mode = 0 }, 220 [K_AM1] = { .kind = OMEGA_DEC_ARGMAX, .mode = 1 }, 221 [K_SW_M1] = { .kind = OMEGA_DEC_SWIGLU, .width = EI, .mutant = 1 }, 222 [K_GATHER_M1] = { .kind = OMEGA_DEC_GATHER, .width = OMEGA_GPU_Q36_ATT_MIX, .stride = OMEGA_GPU_Q36_ATT_MIX, .mutant = 1 }, 223 [K_GATE_M1] = { .kind = OMEGA_DEC_GATE, .mutant = 1 }, 224 [K_CN_M3] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM, .groups = NG, .mutant = 3 }, 225 [K_COPY] = { .kind = OMEGA_DEC_GATHER, .width = COPY_ROW, .stride = COPY_ROW }, 226 /* the batch (v2): N-row norm and add, the x' lane permutations (GATHER through ptab), the segment-table kernel */ 227 [K_NORM_R] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_NORM | OMEGA_DEC_RES_ROWS }, 228 [K_ADD_R] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_ROWS }, 229 [K_PERM_XQ] = { .kind = OMEGA_DEC_GATHER, .width = H, .stride = H }, 230 [K_PERM_O] = { .kind = OMEGA_DEC_GATHER, .width = OMEGA_GPU_Q36_MIXER_OUT, .stride = OMEGA_GPU_Q36_MIXER_OUT }, 231 [K_PERM_XO] = { .kind = OMEGA_DEC_GATHER, .width = H, .stride = H }, /* stride = lb_stride / 4, set below */ 232 [K_PERM_ACT] = { .kind = OMEGA_DEC_GATHER, .width = EI, .stride = EI }, 233 [K_GROUP] = { .kind = OMEGA_DEC_GROUP, .groups = NG }, 234 [K_XS] = { .kind = OMEGA_DEC_XBOUND, .mode = 1 }, 235 [K_XB] = { .kind = OMEGA_DEC_XBOUND }, 236 [K_XB_M1] = { .kind = OMEGA_DEC_XBOUND, .mutant = 1 }, 237 [K_CN_R] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM | OMEGA_DEC_RES_ROWS, .groups = NG }, /* stride set below */ 238 [K_CN_R_M3] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM | OMEGA_DEC_RES_ROWS, .groups = NG, .mutant = 3 }, 239 [K_GATE_R] = { .kind = OMEGA_DEC_GATE, .mode = 1, .width = OMEGA_GPU_Q36_MIXER_OUT, .stride = OMEGA_GPU_Q36_ATT_MIX }, /* L369 prefill: every token's gate */ 240 }; 241 spec[K_PERM_XO].stride = L->lb_stride / 4u; 242 spec[K_CN_R].stride = L->lb_stride; spec[K_CN_R_M3].stride = L->lb_stride; /* head h's routing weights: wts_va + h * lb_stride */ 243 for (unsigned i = 0; i < K_COUNT && rc == OMEGA_GPU_DGRAPH_OK; i++) rc = kern_build(&g->k[i], &spec[i]); 244 if (rc == OMEGA_GPU_DGRAPH_OK) { 245 memset(g->buf.cpu, 0, L->split); 246 memset(g->dbuf.cpu, 0, L->bytes - L->split); /* fresh; every region is written by the chip before a launch reads it */ 247 uint32_t *id = cpu_of(g, L->idtab); 248 for (uint32_t i = 0; i < COPY_ROW; i++) id[i] = i; 249 float *amv = cpu_of(g, L->am_v); 250 for (uint32_t h = 0; h < heads; h++) { 251 float *lg = cpu_of(g, logits_of(g, h)); 252 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 */ 253 } 254 for (size_t i = 0; i < OMEGA_DEC_ARGMAX_SPAN; i++) amv[i] = NEG_PAD; /* stage 1 writes [0, ctas) only */ 255 omega_q36_gather_table(cpu_of(g, L->gtab)); 256 { uint32_t *pt = cpu_of(g, L->ptab); omega_q36_perm_table(pt, H); omega_q36_perm_table(pt + H, OMEGA_GPU_Q36_MIXER_OUT); omega_q36_perm_table(pt + H + OMEGA_GPU_Q36_MIXER_OUT, EI); } 257 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; 258 } 259 omega_gpu_session_unlock(); 260 if (rc != OMEGA_GPU_DGRAPH_OK) { omega_gpu_q36_graph_free(g); return rc; } 261 { const char *e = getenv("AIEN_OMEGA_Q36_BATCH"); if (e && e[0] == '1' && e[1] == 0 && omega_gpu_q36_graph_set_batch(g, 1) != OMEGA_GPU_DGRAPH_OK) { omega_gpu_q36_graph_free(g); return OMEGA_GPU_DGRAPH_CHIP_FAIL; } } 262 *out = g; 263 return OMEGA_GPU_DGRAPH_OK; 264 } 265 266 static int shape_e(const OmegaGpuFp8Tensor *t, uint32_t e, uint32_t n, uint32_t k) { 267 uint32_t te = 0, tn = 0, tk = 0; if (t) omega_gpu_fp8_tensor_shape(t, &te, &tn, &tk); 268 return t && te == e && tn == n && tk == k; 269 } 270 static int shape_is(const OmegaGpuFp8Tensor *t, uint32_t n, uint32_t k) { return shape_e(t, 1, n, k); } 271 #define N_EXPERTS 257u /* routed experts + the shared one (the router's directory) */ 272 273 int omega_gpu_q36_graph_set_head_state(OmegaGpuQ36Graph *g, uint32_t head, OmegaGpuGdn *gdn, OmegaGpuDecodeAttn256Cache *attn) { 274 if (!g || head >= g->heads || !gdn || !attn || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; 275 g->gdn_h[head] = gdn; g->attn_h[head] = attn; 276 return OMEGA_GPU_DGRAPH_OK; 277 } 278 279 int omega_gpu_q36_graph_set_layer(OmegaGpuQ36Graph *g, uint32_t layer, const OmegaGpuFp8Tensor *mix, const OmegaGpuBf16Tensor *ab, 280 const OmegaGpuFp8Tensor *o, const OmegaGpuFp8Tensor *gate_up, const OmegaGpuFp8Tensor *down, 281 const float *in_norm) { 282 if (!g || layer >= g->cfg.layers || !in_norm || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; 283 const int attn = g->layer[layer].attn; 284 if (!shape_is(mix, attn ? OMEGA_GPU_Q36_ATT_MIX : OMEGA_GPU_Q36_GDN_MIX, H) || !shape_is(o, H, OMEGA_GPU_Q36_MIXER_OUT) || 285 !shape_e(gate_up, N_EXPERTS, 2 * EI, H) || !shape_e(down, N_EXPERTS, H, EI)) return OMEGA_GPU_DGRAPH_BAD_ARGS; 286 if (attn ? ab != NULL : ab == NULL) return OMEGA_GPU_DGRAPH_BAD_ARGS; 287 if (!attn) { uint32_t n = 0, k = 0; omega_gpu_bf16_tensor_shape(ab, &n, &k); if (n != OMEGA_GPU_Q36_AB || k != H) return OMEGA_GPU_DGRAPH_BAD_ARGS; } 288 uint64_t t[8] = { 0 }; /* mix, o; the experts' group tables come from the router */ 289 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) return OMEGA_GPU_DGRAPH_BAD_ARGS; 290 omega_gpu_session_lock(); 291 memcpy((uint8_t *)cpu_of(g, g->lo.tabs) + (size_t)layer * 64u, t, sizeof t); 292 memcpy((float *)cpu_of(g, g->lo.norms) + (size_t)layer * H, in_norm, H * 4u); 293 int rc = omega_gpu_session_sync_to_gpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL; 294 omega_gpu_session_unlock(); 295 if (rc == OMEGA_GPU_DGRAPH_OK) { 296 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; 297 g->layer[layer].set = 1; 298 } 299 return rc; 300 } 301 302 int omega_gpu_q36_graph_set_final_norm(OmegaGpuQ36Graph *g, const float *final_norm) { 303 if (!g || !final_norm || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; 304 omega_gpu_session_lock(); 305 memcpy(cpu_of(g, g->lo.fnorm), final_norm, H * 4u); 306 int rc = omega_gpu_session_sync_to_gpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL; 307 omega_gpu_session_unlock(); 308 if (rc == OMEGA_GPU_DGRAPH_OK) g->final_set = 1; 309 return rc; 310 } 311 312 int omega_gpu_q36_graph_set_lm_head_fp8(OmegaGpuQ36Graph *g, const OmegaGpuFp8Tensor *head) { 313 if (!g || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; 314 if (!head) { g->head8 = NULL; return OMEGA_GPU_DGRAPH_OK; } 315 uint64_t t[2]; 316 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; 317 omega_gpu_session_lock(); 318 memcpy((uint8_t *)cpu_of(g, g->lo.tabs) + (size_t)g->cfg.layers * 64u, t, sizeof t); 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->head8 = head; 322 return rc; 323 } 324 325 int omega_gpu_q36_graph_set_exact_head(OmegaGpuQ36Graph *g, const OmegaGpuFp8Tensor *screen, const float *coef) { 326 if (!g || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; 327 if (!screen) { 328 omega_gpu_session_lock(); 329 if (g->xh.mem.cpu) omega_gpu_session_free(&g->xh.mem); 330 omega_gpu_session_unlock(); 331 memset(&g->xh, 0, sizeof g->xh); 332 return OMEGA_GPU_DGRAPH_OK; 333 } 334 uint64_t t[2]; 335 if (!coef || g->xh.screen || !shape_is(screen, g->cfg.vocab, H) || omega_gpu_fp8_tensor_entry(screen, 0, &t[0], &t[1]) != 0) 336 return OMEGA_GPU_DGRAPH_BAD_ARGS; 337 const size_t n = g->cfg.vocab, P = (size_t)g->am_ctas * OMEGA_DEC_ARGMAX_SPAN; 338 const size_t o_h8 = 0, o_s = al(o_h8 + H * 4), o_c = al(o_s + n * 4), o_hi = al(o_c + n * 4), o_lo = al(o_hi + n * 4), 339 o_tau = al(o_lo + P * 4), o_tab = al(o_tau + 16), total = al(o_tab + 16); 340 if (omega_gpu_bf16_prepare_mask(H) != 0) return fail(g, "q36 graph: exact head MASK kernel", OMEGA_GPU_DGRAPH_CHIP_FAIL); 341 omega_gpu_session_lock(); 342 int rc = omega_gpu_session_alloc(total, &g->xh.mem) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL; 343 if (rc == OMEGA_GPU_DGRAPH_OK) { 344 uint8_t *B = g->xh.mem.cpu; 345 memset(B, 0, total); 346 memcpy(B + o_c, coef, n * 4); 347 for (size_t i = n; i < P; i++) ((float *)(B + o_lo))[i] = NEG_PAD; /* XBOUND writes [0, vocab) only */ 348 memcpy(B + o_tab, t, sizeof t); 349 if (omega_gpu_session_sync_to_gpu(&g->xh.mem) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; 350 } 351 if (rc == OMEGA_GPU_DGRAPH_OK) { 352 const uint64_t V = g->xh.mem.va; 353 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; 354 g->xh.screen = screen; memset(g->xh.on, 1, sizeof g->xh.on); 355 } else if (g->xh.mem.cpu) omega_gpu_session_free(&g->xh.mem); 356 omega_gpu_session_unlock(); 357 return rc; 358 } 359 360 void omega_gpu_q36_graph_set_exact_head_on(OmegaGpuQ36Graph *g, int on) { if (g && !g->in_token) memset(g->xh.on, on ? 1 : 0, sizeof g->xh.on); } 361 void omega_gpu_q36_graph_set_head_exact_head_on(OmegaGpuQ36Graph *g, uint32_t head, int on) { 362 if (g && !g->in_token && head < OMEGA_GPU_Q36_MAX_HEADS) g->xh.on[head] = on ? 1 : 0; 363 } 364 int omega_gpu_q36_graph_exact_head_active_head(const OmegaGpuQ36Graph *g, uint32_t head) { 365 return g && head < OMEGA_GPU_Q36_MAX_HEADS && g->xh.screen && g->xh.on[head] && !g->head8; 366 } 367 int omega_gpu_q36_graph_exact_head_active(const OmegaGpuQ36Graph *g) { return omega_gpu_q36_graph_exact_head_active_head(g, 0); } 368 369 void omega_gpu_q36_graph_set_flush_each(OmegaGpuQ36Graph *g, int on) { if (g) g->flush_each = on ? 1 : 0; } 370 void omega_gpu_q36_graph_test_set_mutant(OmegaGpuQ36Graph *g, int mutant) { 371 if (!g) return; 372 g->mutant = mutant; 373 omega_gpu_bf16_test_set_mask_mutant(mutant == 5 ? 4 : 0); /* process-wide, the MASK form only */ 374 } 375 376 /* ---- the list ---- */ 377 378 static OmegaGpuCmdListOpts list_opts(const OmegaGpuQ36Graph *g) { 379 OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); 380 o.mode = OMEGA_GPU_CMDLIST_PCAS; o.barrier = (OmegaGpuCmdListBarrier)omega_gpu_decode_graph_barrier(); o.timeout_ms = TIMEOUT_MS; o.stamps = g->stamps; 381 return o; 382 } 383 384 static int submit_now(OmegaGpuQ36Graph *g) { 385 if (omega_gpu_cmdlist_count(g->cl) == 0) return OMEGA_GPU_DGRAPH_OK; 386 OmegaGpuCmdListOpts o = list_opts(g); 387 OmegaGpuCmdListStats st; memset(&st, 0, sizeof st); 388 int rc = omega_gpu_cmdlist_submit(g->cl, &o, &st); 389 g->acc.launches += st.launches; g->acc.submits += st.submits; g->acc.chip_ns += st.elapsed_ns; g->acc.sems_done += st.sems_done; 390 if (rc == 0 && g->stamps) { 391 uint64_t tmp[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1]; 392 int cnt = omega_gpu_cmdlist_stamps(g->cl, tmp, OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1); 393 if (cnt < 0) rc = -1; 394 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]; } 395 } 396 g->tagged_in_list = 0; 397 omega_gpu_cmdlist_reset(g->cl); 398 if (rc != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); 399 if (st.sems_done != st.launches) { 400 char e[128]; omega_gpu_fmt(e, sizeof e, "q36 graph: %u of %u launch semaphores DONE (first missing %d)", st.sems_done, st.launches, st.first_missing); 401 return fail(g, e, OMEGA_GPU_DGRAPH_CHIP_FAIL); 402 } 403 return OMEGA_GPU_DGRAPH_OK; 404 } 405 406 static void tag(OmegaGpuQ36Graph *g, uint8_t kind) { 407 const uint32_t n = omega_gpu_cmdlist_count(g->cl); 408 for (; g->tagged_in_list < n && g->n_kind < OMEGA_GPU_CMDLIST_MAX_LAUNCHES; g->tagged_in_list++) g->kind[g->n_kind++] = kind; 409 } 410 411 static int after_op(OmegaGpuQ36Graph *g, uint8_t kind, int api_rc) { 412 if (api_rc != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); 413 tag(g, kind); 414 return g->flush_each ? submit_now(g) : OMEGA_GPU_DGRAPH_OK; 415 } 416 417 static int add_glue(OmegaGpuQ36Graph *g, uint8_t kind, int ki, uint32_t gx, uint32_t gy, size_t elems, const uint32_t *args, uint32_t n_args) { 418 const Kern *k = &g->k[ki]; 419 OmegaGpuLaunch L; memset(&L, 0, sizeof L); 420 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; 421 L.num_elements = elems; L.args = args; L.n_args = n_args; L.timeout_ms = TIMEOUT_MS; 422 return after_op(g, kind, omega_gpu_cmdlist_add(g->cl, &L) < 0 ? -1 : 0); 423 } 424 425 /* RES at width 2048: NORM (x -> y), ADD (x += a) or COMBINE|NORM (x += sum_g wts[g] * a[g]; y = norm(x)) */ 426 static int rec_res_w(OmegaGpuQ36Graph *g, int ki, uint64_t x, uint64_t a, uint64_t wts, uint64_t nw, uint64_t y, float scale) { 427 uint32_t w[OMEGA_DEC_RES_WORDS]; memset(w, 0, sizeof w); 428 put64(w, OMEGA_DEC_RES_X, x); put64(w, OMEGA_DEC_RES_A, a); put64(w, OMEGA_DEC_RES_W, wts); put64(w, OMEGA_DEC_RES_NW, nw); 429 put64(w, OMEGA_DEC_RES_Y, y); putf(w, OMEGA_DEC_RES_EPS, g->cfg.eps); putf(w, OMEGA_DEC_RES_SCALE, scale); 430 return add_glue(g, ki == K_NORM ? OMEGA_GPU_Q36_K_NORM : OMEGA_GPU_Q36_K_RES, ki, 1, 1, H, w, OMEGA_DEC_RES_WORDS); 431 } 432 static int rec_res(OmegaGpuQ36Graph *g, int ki, uint64_t x, uint64_t a, uint64_t nw, uint64_t y, float scale) { 433 return rec_res_w(g, ki, x, a, 0, nw, y, scale); 434 } 435 436 /* The a|b GEMV beside the mix GEMV (a speed lever of the 27B, values identical): OFF by default here until the plain graph 437 * has passed its gates (PRIORITY CHANGE 2026-10-20); env AIEN_OMEGA_Q36_OVERLAP_AB=1 turns it on. */ 438 static int g_overlap_ab = -1; 439 static int q36_overlap_ab(void) { 440 if (g_overlap_ab < 0) { const char *e = getenv("AIEN_OMEGA_Q36_OVERLAP_AB"); g_overlap_ab = (e && e[0] == '1' && e[1] == 0) ? 1 : 0; } 441 return g_overlap_ab; 442 } 443 444 static int rec_fp8(OmegaGpuQ36Graph *g, uint8_t kind, const OmegaGpuFp8Tensor *t, uint64_t tab, uint64_t x, uint64_t y) { 445 return after_op(g, kind, omega_gpu_fp8_gemv_grouped_record(g->cl, t, 1, tab, x, 1, y)); 446 } 447 448 int omega_gpu_q36_graph_begin_token(OmegaGpuQ36Graph *g, const float *x_embed) { return omega_gpu_q36_graph_begin_stroke(g, x_embed, 1); } 449 450 int omega_gpu_q36_graph_begin_stroke(OmegaGpuQ36Graph *g, const float *x_embed, uint32_t heads) { 451 if (!g || g->in_token || heads == 0 || heads > g->heads) return OMEGA_GPU_DGRAPH_BAD_ARGS; 452 for (uint32_t h = 1; h < heads; h++) if (!g->gdn_h[h] || !g->attn_h[h]) return OMEGA_GPU_DGRAPH_BAD_ARGS; 453 omega_gpu_session_lock(); 454 g->t_begin = omega_gpu_session_now_ns(); 455 g->in_token = 1; g->failed = 0; g->stroke_heads = heads; 456 for (uint32_t h = 0; h < heads; h++) { g->xq_layer[h] = -1; g->hidden_ready[h] = 0; } 457 memset(&g->acc, 0, sizeof g->acc); 458 g->n_kind = 0; g->tagged_in_list = 0; g->n_stamp = 0; 459 omega_gpu_cmdlist_reset(g->cl); 460 if (!omega_gpu_session_open()) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); 461 for (uint32_t h = 0; h < heads; h++) { 462 uint32_t *tok = cpu_of(g, token_of(g, h)); 463 tok[0] = TOKEN_POISON; tok[1] = TOKEN_POISON; 464 if (x_embed) memcpy(cpu_of(g, x_of(g, h)), x_embed + (size_t)h * H, H * 4u); 465 } 466 if (g->batch) write_dense_segments(g, heads); 467 if (omega_gpu_session_sync_to_gpu(&g->buf) != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); 468 return OMEGA_GPU_DGRAPH_OK; 469 } 470 471 int omega_gpu_q36_graph_record_layer(OmegaGpuQ36Graph *g, uint32_t l, uint32_t pos) { return omega_gpu_q36_graph_record_head_layer(g, 0, l, pos); } 472 473 int omega_gpu_q36_graph_record_head_layer(OmegaGpuQ36Graph *g, uint32_t h, uint32_t l, uint32_t pos) { 474 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; 475 if (g->failed) return OMEGA_GPU_DGRAPH_CHIP_FAIL; 476 if (h >= g->stroke_heads) return fail(g, "q36 graph: head not in the stroke", OMEGA_GPU_DGRAPH_BAD_ARGS); 477 if (l >= g->cfg.layers || !g->layer[l].set || g->hidden_ready[h] || (l + 1 == g->cfg.layers && !g->final_set)) 478 return fail(g, "q36 graph: layer not set, out of order, or final norm missing", OMEGA_GPU_DGRAPH_BAD_ARGS); 479 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); 480 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), dn = va_of(g, g->lo.dn); 481 const uint64_t in_norm = va_of(g, g->lo.norms) + (uint64_t)l * H * 4u, tab = va_of(g, g->lo.tabs) + (uint64_t)l * 64u; 482 const uint32_t ki = g->layer[l].kind_index; 483 int rc = OMEGA_GPU_DGRAPH_OK; 484 if (g->xq_layer[h] != (int)l) rc = rec_res(g, K_NORM, x, 0, in_norm, xq, SCALE_2P64); 485 /* GDN layers: the a|b GEMV (latency-bound, ~18 us) is recorded first and the mix GEMV marked independent of it: both only 486 * read xq and write their own buffers, so the two grids overlap; the GDN step after them waits for both (normal barrier). 487 * AIEN_OMEGA_Q36_OVERLAP_AB=0 or set_overlap_ab(0): the old order, mix then a|b, each behind a barrier. */ 488 const int overlap = !g->layer[l].attn && q36_overlap_ab(); 489 if (overlap && rc == OMEGA_GPU_DGRAPH_OK) rc = after_op(g, OMEGA_GPU_Q36_K_AB, omega_gpu_bf16_gemv_record(g->cl, g->layer[l].ab, xq, va_of(g, g->lo.ab))); 490 if (rc == OMEGA_GPU_DGRAPH_OK) { 491 int mrc = omega_gpu_fp8_gemv_grouped_record(g->cl, g->layer[l].mix, 1, tab, xq, 1, mix); 492 if (mrc == 0 && overlap && !g->flush_each && omega_gpu_cmdlist_mark_independent(g->cl) != 0) mrc = -1; 493 rc = after_op(g, OMEGA_GPU_Q36_K_MIX, mrc); 494 } 495 uint64_t mixer_out = att; 496 if (!g->layer[l].attn) { 497 const uint64_t ab = va_of(g, g->lo.ab); 498 if (!overlap && rc == OMEGA_GPU_DGRAPH_OK) rc = after_op(g, OMEGA_GPU_Q36_K_AB, omega_gpu_bf16_gemv_record(g->cl, g->layer[l].ab, xq, ab)); 499 if (rc == OMEGA_GPU_DGRAPH_OK) 500 rc = after_op(g, OMEGA_GPU_Q36_K_GDN, omega_gpu_gdn_record(g->cl, g->gdn_h[h], ki, mix, mix + OMEGA_GPU_Q36_GDN_QKV * 4u, ab, 501 g->cfg.eps_l2, g->cfg.eps, 1.0f, SCALE_2P64, &mixer_out)); 502 } else { 503 uint32_t w[OMEGA_DEC_GA_WORDS]; memset(w, 0, sizeof w); 504 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); 505 if (rc == OMEGA_GPU_DGRAPH_OK) 506 rc = add_glue(g, OMEGA_GPU_Q36_K_GATHER, g->mutant == 2 ? K_GATHER_M1 : K_GATHER, (OMEGA_GPU_Q36_ATT_MIX + OMEGA_DEC_THREADS - 1) / OMEGA_DEC_THREADS, 1, 507 OMEGA_GPU_Q36_ATT_MIX, w, OMEGA_DEC_GA_WORDS); 508 if (rc == OMEGA_GPU_DGRAPH_OK) 509 rc = after_op(g, OMEGA_GPU_Q36_K_ATTN, omega_gpu_decode_attn256_record(g->cl, g->attn_h[h], ki, pos, agg, g->cfg.eps, SCALE_2P64, att)); 510 uint32_t w2[OMEGA_DEC_GT_WORDS]; memset(w2, 0, sizeof w2); 511 put64(w2, OMEGA_DEC_GT_A, att); put64(w2, OMEGA_DEC_GT_G, agg + OMEGA_GPU_Q36_ATT_QKV * 4u); put64(w2, OMEGA_DEC_GT_OUT, att); 512 w2[(OMEGA_DEC_GT_N - 0x380) / 4] = OMEGA_GPU_Q36_MIXER_OUT; 513 if (rc == OMEGA_GPU_DGRAPH_OK) 514 rc = add_glue(g, OMEGA_GPU_Q36_K_GATE, g->mutant == 3 ? K_GATE_M1 : K_GATE, OMEGA_GPU_Q36_MIXER_OUT / OMEGA_DEC_THREADS, 1, 515 OMEGA_GPU_Q36_MIXER_OUT, w2, OMEGA_DEC_GT_WORDS); 516 } 517 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_fp8(g, OMEGA_GPU_Q36_K_O, g->layer[l].o, tab + 16u, mixer_out, o); 518 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_res_w(g, K_ADD, x, o, 0, 0, 0, 1.0f); 519 /* the sparse block: router (post-attention norm, top-8 + the shared slot), 9 expert groups, COMBINE|NORM */ 520 OmegaGpuRouterDevOut r; memset(&r, 0, sizeof r); 521 if (rc == OMEGA_GPU_DGRAPH_OK) 522 rc = after_op(g, OMEGA_GPU_Q36_K_ROUTER, omega_gpu_router_record(g->cl, g->cfg.router, l, x, g->cfg.eps, 1, SCALE_2P64, &r)); 523 if (rc == OMEGA_GPU_DGRAPH_OK) 524 rc = after_op(g, OMEGA_GPU_Q36_K_GATE_UP, omega_gpu_fp8_gemv_grouped_record(g->cl, g->layer[l].gu, NG, r.tab_gu_va, r.xout_va, 1, gu)); 525 uint32_t w3[OMEGA_DEC_SW_WORDS]; memset(w3, 0, sizeof w3); 526 put64(w3, OMEGA_DEC_SW_GU, gu); put64(w3, OMEGA_DEC_SW_ACT, act); putf(w3, OMEGA_DEC_SW_SCALE, SCALE_2P64); 527 if (rc == OMEGA_GPU_DGRAPH_OK) 528 rc = add_glue(g, OMEGA_GPU_Q36_K_SWIGLU, g->mutant == 1 ? K_SW_M1 : K_SW, EI / OMEGA_DEC_THREADS, NG, (size_t)NG * EI, w3, OMEGA_DEC_SW_WORDS); 529 if (rc == OMEGA_GPU_DGRAPH_OK) 530 rc = after_op(g, OMEGA_GPU_Q36_K_DOWN, omega_gpu_fp8_gemv_grouped_record(g->cl, g->layer[l].dn, NG, r.tab_dn_va, act, NG, dn)); 531 if (rc == OMEGA_GPU_DGRAPH_OK) { 532 const int last = l + 1 == g->cfg.layers; 533 const uint64_t nw = last ? va_of(g, g->lo.fnorm) : in_norm + H * 4u; 534 rc = rec_res_w(g, g->mutant == 4 ? K_CN_M3 : K_CN, x, dn, r.wts_va, nw, last ? va_of(g, hidden_of(g, h)) : xq, 535 last && !g->head8 ? 1.0f : SCALE_2P64); 536 if (rc == OMEGA_GPU_DGRAPH_OK) { if (last) g->hidden_ready[h] = 1; else g->xq_layer[h] = (int)l + 1; } 537 } 538 return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc); 539 } 540 541 /* ---- the batch (v2, L312): every conversation of a layer in one set of launches, each weight read once per layer ---- */ 542 typedef struct { uint64_t w_va, s_va; uint32_t row0, cnt, pad[2]; } Seg32; 543 _Static_assert(sizeof(Seg32) == OMEGA_GEMM_SEG_BYTES, "segment entry layout"); 544 545 int omega_gpu_q36_graph_set_batch(OmegaGpuQ36Graph *g, int on) { 546 if (!g || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; 547 if (!on) { g->batch = 0; return OMEGA_GPU_DGRAPH_OK; } 548 /* pin the four GEMM shapes the batch records (a kernel cache miss while recording is refused): the dense trunk at 8 rows 549 * per pass without maps, the expert blocks at 4 rows per pass with the row maps */ 550 const uint32_t R = g->heads * NG; 551 int rc = omega_gpu_fp8gemm_prepare_shape(H, 1, 0, 0); 552 if (rc == 0) rc = omega_gpu_fp8gemm_prepare_shape(OMEGA_GPU_Q36_MIXER_OUT, 1, 0, 0); 553 if (rc == 0) rc = omega_gpu_fp8gemm_prepare_shape(H, R, 0, 1); 554 if (rc == 0) rc = omega_gpu_fp8gemm_prepare_shape(EI, R, 0, 1); 555 if (rc != 0) return OMEGA_GPU_DGRAPH_CHIP_FAIL; 556 /* lever 2: the router once per layer for every head (record_layer_heads; the ROWS kernel has its own cache slot) */ 557 if (g->heads > 1u && omega_gpu_router_prepare_rows(g->cfg.router, H * 4u, g->lo.lb_stride) != OMEGA_GPU_ROUTER_OK) return OMEGA_GPU_DGRAPH_CHIP_FAIL; 558 /* lever 1: the bf16 lm_head read once per group of up to OMEGA_BF16_MAX_XBATCH heads (record_final_heads) */ 559 if (!g->head8) { 560 uint32_t n = 0, k = 0; omega_gpu_bf16_tensor_shape(g->cfg.lm_head, &n, &k); 561 for (uint32_t xb = 2; xb <= OMEGA_BF16_MAX_XBATCH && xb <= g->heads; xb++) 562 if (omega_gpu_bf16_prepare_xbatch(k, xb, g->am_ctas * OMEGA_DEC_ARGMAX_SPAN) != OMEGA_GPU_BF16_OK) return OMEGA_GPU_DGRAPH_CHIP_FAIL; 563 } 564 g->batch = 1; 565 return OMEGA_GPU_DGRAPH_OK; 566 } 567 int omega_gpu_q36_graph_batch(const OmegaGpuQ36Graph *g) { return g ? g->batch : 0; } 568 569 /* the dense segment entries of the open stroke: {mix, o} per layer, row0 0, cnt = the stroke's heads (host region, synced by begin_stroke) */ 570 static void write_dense_segments(OmegaGpuQ36Graph *g, uint32_t heads) { 571 Seg32 *s = cpu_of(g, g->lo.segd); 572 for (uint32_t l = 0; l < g->cfg.layers; l++) { 573 memset(&s[l * SEGD_ROWS], 0, SEGD_ROWS * sizeof *s); 574 if (!g->layer[l].set) continue; 575 (void)omega_gpu_fp8_tensor_entry(g->layer[l].mix, 0, &s[l * SEGD_ROWS].w_va, &s[l * SEGD_ROWS].s_va); 576 (void)omega_gpu_fp8_tensor_entry(g->layer[l].o, 0, &s[l * SEGD_ROWS + 1].w_va, &s[l * SEGD_ROWS + 1].s_va); 577 s[l * SEGD_ROWS].cnt = heads; s[l * SEGD_ROWS + 1].cnt = heads; 578 } 579 } 580 581 static int rec_gemm(OmegaGpuQ36Graph *g, uint8_t kind, const OmegaGpuFp8Tensor *t, uint32_t rows, uint32_t segs, uint64_t seg, uint64_t xp, 582 uint64_t y, uint64_t xm, uint64_t ym) { 583 return after_op(g, kind, omega_gpu_fp8gemm_record(g->cl, t, rows, segs, seg, xp, y, xm, ym, 0, 0, NULL)); 584 } 585 /* rows of width floats permuted through tab into out (the GEMM's x' lane layout); the input row stride is the kernel's */ 586 static int rec_perm(OmegaGpuQ36Graph *g, int ki, uint64_t in, uint64_t tab, uint64_t out, uint32_t width, uint32_t rows) { 587 uint32_t w[OMEGA_DEC_GA_WORDS]; memset(w, 0, sizeof w); 588 put64(w, OMEGA_DEC_GA_IN, in); put64(w, OMEGA_DEC_GA_TAB, tab); put64(w, OMEGA_DEC_GA_OUT, out); 589 return add_glue(g, OMEGA_GPU_Q36_K_PERM, ki, (width + OMEGA_DEC_THREADS - 1) / OMEGA_DEC_THREADS, rows, (size_t)width * rows, w, OMEGA_DEC_GA_WORDS); 590 } 591 /* COMBINE|NORM of every head in one launch (L356): head h's residual x + h * 2048, down outputs a + h * 9 * 2048, weights 592 * w + h * lb_stride, output y + h * 2048 (the xq or hidden slots). Each row runs the one-head program. ON unless 593 * AIEN_OMEGA_Q36_CN_ROWS=0 (the per-head launches). */ 594 static int g_cn_rows = -1; 595 static int q36_cn_rows(void) { 596 if (g_cn_rows < 0) { const char *e = getenv("AIEN_OMEGA_Q36_CN_ROWS"); g_cn_rows = (e && e[0] == '0' && e[1] == 0) ? 0 : 1; } 597 return g_cn_rows; 598 } 599 static int rec_cn_rows(OmegaGpuQ36Graph *g, uint64_t x, uint64_t a, uint64_t wts, uint64_t nw, uint64_t y, float scale, uint32_t rows) { 600 uint32_t w[OMEGA_DEC_RES_WORDS]; memset(w, 0, sizeof w); 601 put64(w, OMEGA_DEC_RES_X, x); put64(w, OMEGA_DEC_RES_A, a); put64(w, OMEGA_DEC_RES_W, wts); put64(w, OMEGA_DEC_RES_NW, nw); 602 put64(w, OMEGA_DEC_RES_Y, y); putf(w, OMEGA_DEC_RES_EPS, g->cfg.eps); putf(w, OMEGA_DEC_RES_SCALE, scale); 603 return add_glue(g, OMEGA_GPU_Q36_K_RES, g->mutant == 4 ? K_CN_R_M3 : K_CN_R, 1, rows, (size_t)H * rows, w, OMEGA_DEC_RES_WORDS); 604 } 605 /* RES NORM|ROWS or ADD|ROWS over rows of 2048 (x, a, y move by the width per row) */ 606 static int rec_res_rows(OmegaGpuQ36Graph *g, int ki, uint64_t x, uint64_t a, uint64_t nw, uint64_t y, float scale, uint32_t rows) { 607 uint32_t w[OMEGA_DEC_RES_WORDS]; memset(w, 0, sizeof w); 608 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); 609 putf(w, OMEGA_DEC_RES_EPS, g->cfg.eps); putf(w, OMEGA_DEC_RES_SCALE, scale); 610 return add_glue(g, ki == K_NORM_R ? OMEGA_GPU_Q36_K_NORM : OMEGA_GPU_Q36_K_RES, ki, 1, rows, (size_t)H * rows, w, OMEGA_DEC_RES_WORDS); 611 } 612 613 int omega_gpu_q36_graph_record_layer_heads(OmegaGpuQ36Graph *g, uint32_t l, const uint32_t *pos) { 614 if (!g || !g->in_token || !pos) return OMEGA_GPU_DGRAPH_BAD_ARGS; 615 if (g->failed) return OMEGA_GPU_DGRAPH_CHIP_FAIL; 616 if (!g->batch) return fail(g, "q36 graph: record_layer_heads needs set_batch", OMEGA_GPU_DGRAPH_BAD_ARGS); 617 if (l >= g->cfg.layers || !g->layer[l].set || (l + 1 == g->cfg.layers && !g->final_set)) 618 return fail(g, "q36 graph: layer not set, out of order, or final norm missing", OMEGA_GPU_DGRAPH_BAD_ARGS); 619 const uint32_t N = g->stroke_heads, R = N * NG, ki = g->layer[l].kind_index; 620 for (uint32_t h = 0; h < N; h++) 621 if (g->hidden_ready[h] || g->xq_layer[h] != g->xq_layer[0]) return fail(g, "q36 graph: heads out of step", OMEGA_GPU_DGRAPH_BAD_ARGS); 622 const Layout *L = &g->lo; 623 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); 624 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), rlb = va_of(g, L->rlb); 625 const uint64_t sgu = va_of(g, L->sgu), sdn = va_of(g, L->sdn), xmap = va_of(g, L->xmap), ymap = va_of(g, L->ymap), xp2 = va_of(g, L->xp2); 626 const uint64_t guN = va_of(g, L->guN), actN = va_of(g, L->actN), actp = va_of(g, L->actp), dnN = va_of(g, L->dnN); 627 const uint64_t pt2048 = va_of(g, L->ptab), pt4096 = pt2048 + (uint64_t)H * 4u, pt512 = pt4096 + (uint64_t)OMEGA_GPU_Q36_MIXER_OUT * 4u; 628 const uint64_t in_norm = va_of(g, L->norms) + (uint64_t)l * H * 4u, segd = va_of(g, L->segd) + (uint64_t)l * SEGD_ROWS * OMEGA_GEMM_SEG_BYTES; 629 const uint64_t XS = (uint64_t)H * 4u, MOS = (uint64_t)OMEGA_GPU_Q36_MIXER_OUT * 4u; 630 int rc = OMEGA_GPU_DGRAPH_OK; 631 /* input norm of every head (layer 0; after that the previous layer's COMBINE|NORM left layer l's x' in the xq slots) */ 632 if (g->xq_layer[0] != (int)l) rc = rec_res_rows(g, K_NORM_R, x0, 0, in_norm, xq0, SCALE_2P64, N); 633 /* x' of the dense trunk: the N xq rows in the GEMM's lane layout */ 634 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_XQ, xq0, pt2048, xp, H, N); 635 if (!g->layer[l].attn) { 636 if (rc == OMEGA_GPU_DGRAPH_OK) rc = after_op(g, OMEGA_GPU_Q36_K_AB, omega_gpu_bf16_gemv_rows_record(g->cl, g->layer[l].ab, N, xq0, abN)); 637 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_gemm(g, OMEGA_GPU_Q36_K_MIX, g->layer[l].mix, N, 1, segd, xp, mixN, 0, 0); 638 for (uint32_t h = 0; h < N && rc == OMEGA_GPU_DGRAPH_OK; h++) { 639 const uint64_t mix_h = mixN + (uint64_t)h * OMEGA_GPU_Q36_GDN_MIX * 4u; 640 uint64_t mo = 0; 641 rc = after_op(g, OMEGA_GPU_Q36_K_GDN, omega_gpu_gdn_record(g->cl, g->gdn_h[h], ki, mix_h, mix_h + OMEGA_GPU_Q36_GDN_QKV * 4u, 642 abN + (uint64_t)h * OMEGA_GPU_Q36_AB * 4u, g->cfg.eps_l2, g->cfg.eps, 1.0f, SCALE_2P64, &mo)); 643 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_O, mo, pt4096, op + h * MOS, OMEGA_GPU_Q36_MIXER_OUT, 1); 644 } 645 } else { 646 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_gemm(g, OMEGA_GPU_Q36_K_MIX, g->layer[l].mix, N, 1, segd, xp, mixN, 0, 0); /* n 9216: the rows are 9216 apart */ 647 uint32_t w[OMEGA_DEC_GA_WORDS]; memset(w, 0, sizeof w); 648 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); 649 if (rc == OMEGA_GPU_DGRAPH_OK) 650 rc = add_glue(g, OMEGA_GPU_Q36_K_GATHER, g->mutant == 2 ? K_GATHER_M1 : K_GATHER, (OMEGA_GPU_Q36_ATT_MIX + OMEGA_DEC_THREADS - 1) / OMEGA_DEC_THREADS, N, 651 (size_t)OMEGA_GPU_Q36_ATT_MIX * N, w, OMEGA_DEC_GA_WORDS); 652 for (uint32_t h = 0; h < N && rc == OMEGA_GPU_DGRAPH_OK; h++) { 653 const uint64_t agg_h = aggN + (uint64_t)h * OMEGA_GPU_Q36_ATT_MIX * 4u, att_h = attN + h * MOS; 654 rc = after_op(g, OMEGA_GPU_Q36_K_ATTN, omega_gpu_decode_attn256_record(g->cl, g->attn_h[h], ki, pos[h], agg_h, g->cfg.eps, SCALE_2P64, att_h)); 655 uint32_t w2[OMEGA_DEC_GT_WORDS]; memset(w2, 0, sizeof w2); 656 put64(w2, OMEGA_DEC_GT_A, att_h); put64(w2, OMEGA_DEC_GT_G, agg_h + OMEGA_GPU_Q36_ATT_QKV * 4u); put64(w2, OMEGA_DEC_GT_OUT, att_h); 657 w2[(OMEGA_DEC_GT_N - 0x380) / 4] = OMEGA_GPU_Q36_MIXER_OUT; 658 if (rc == OMEGA_GPU_DGRAPH_OK) 659 rc = add_glue(g, OMEGA_GPU_Q36_K_GATE, g->mutant == 3 ? K_GATE_M1 : K_GATE, OMEGA_GPU_Q36_MIXER_OUT / OMEGA_DEC_THREADS, 1, 660 OMEGA_GPU_Q36_MIXER_OUT, w2, OMEGA_DEC_GT_WORDS); 661 } 662 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_O, attN, pt4096, op, OMEGA_GPU_Q36_MIXER_OUT, N); 663 } 664 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_gemm(g, OMEGA_GPU_Q36_K_O, g->layer[l].o, N, 1, segd + OMEGA_GEMM_SEG_BYTES, op, oN, 0, 0); 665 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_res_rows(g, K_ADD_R, x0, oN, 0, 0, 1.0f, N); 666 /* the sparse block: one router launch for the N heads (lever 2: the gate table read once; head h's buffer at rlb + h * lb_stride), 667 * GROUP sorts their picks into one segment table, two grouped GEMMs */ 668 OmegaGpuRouterDevOut r0; memset(&r0, 0, sizeof r0); 669 if (rc == OMEGA_GPU_DGRAPH_OK) 670 rc = after_op(g, OMEGA_GPU_Q36_K_ROUTER, omega_gpu_router_record_rows(g->cl, g->cfg.router, l, x0, (uint32_t)XS, g->cfg.eps, 1, SCALE_2P64, 671 rlb, L->lb_stride, N, &r0)); 672 uint32_t wg[OMEGA_DEC_GR_WORDS]; memset(wg, 0, sizeof wg); 673 put64(wg, OMEGA_DEC_GR_IDS, r0.ids_va); put64(wg, OMEGA_DEC_GR_TGU, r0.tab_gu_va); put64(wg, OMEGA_DEC_GR_TDN, r0.tab_dn_va); 674 put64(wg, OMEGA_DEC_GR_SGU, sgu); put64(wg, OMEGA_DEC_GR_SDN, sdn); put64(wg, OMEGA_DEC_GR_XMAP, xmap); put64(wg, OMEGA_DEC_GR_YMAP, ymap); 675 wg[(OMEGA_DEC_GR_STRIDE - 0x380) / 4] = L->lb_stride; wg[(OMEGA_DEC_GR_HEADS - 0x380) / 4] = N; 676 if (rc == OMEGA_GPU_DGRAPH_OK) rc = add_glue(g, OMEGA_GPU_Q36_K_GROUP, K_GROUP, 1, 1, R, wg, OMEGA_DEC_GR_WORDS); 677 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_XO, r0.xout_va, pt2048, xp2, H, N); 678 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_gemm(g, OMEGA_GPU_Q36_K_GATE_UP, g->layer[l].gu, R, R, sgu, xp2, guN, xmap, ymap); 679 uint32_t w3[OMEGA_DEC_SW_WORDS]; memset(w3, 0, sizeof w3); 680 put64(w3, OMEGA_DEC_SW_GU, guN); put64(w3, OMEGA_DEC_SW_ACT, actN); putf(w3, OMEGA_DEC_SW_SCALE, SCALE_2P64); 681 if (rc == OMEGA_GPU_DGRAPH_OK) 682 rc = add_glue(g, OMEGA_GPU_Q36_K_SWIGLU, g->mutant == 1 ? K_SW_M1 : K_SW, EI / OMEGA_DEC_THREADS, R, (size_t)R * EI, w3, OMEGA_DEC_SW_WORDS); 683 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_ACT, actN, pt512, actp, EI, R); 684 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_gemm(g, OMEGA_GPU_Q36_K_DOWN, g->layer[l].dn, R, R, sdn, actp, dnN, ymap, ymap); 685 const int last = l + 1 == g->cfg.layers; 686 const uint64_t nw = last ? va_of(g, L->fnorm) : in_norm + H * 4u; 687 if (q36_cn_rows()) { 688 if (rc == OMEGA_GPU_DGRAPH_OK) 689 rc = rec_cn_rows(g, x0, dnN, r0.wts_va, nw, last ? va_of(g, hidden_of(g, 0)) : xq0, last && !g->head8 ? 1.0f : SCALE_2P64, N); 690 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; } 691 } else for (uint32_t h = 0; h < N && rc == OMEGA_GPU_DGRAPH_OK; h++) { 692 rc = rec_res_w(g, g->mutant == 4 ? K_CN_M3 : K_CN, x0 + h * XS, dnN + (uint64_t)h * NG * H * 4u, r0.wts_va + (uint64_t)h * L->lb_stride, nw, 693 last ? va_of(g, hidden_of(g, h)) : xq0 + h * XS, last && !g->head8 ? 1.0f : SCALE_2P64); 694 if (rc == OMEGA_GPU_DGRAPH_OK) { if (last) g->hidden_ready[h] = 1; else g->xq_layer[h] = (int)l + 1; } 695 } 696 return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc); 697 } 698 699 /* the two-stage argmax of one head's logits slot into its token slot (token: index, token + 4: value) */ 700 static int rec_argmax(OmegaGpuQ36Graph *g, uint64_t logits, uint64_t token) { 701 uint32_t a0[OMEGA_DEC_AM_WORDS], a1[OMEGA_DEC_AM_WORDS]; memset(a0, 0, sizeof a0); memset(a1, 0, sizeof a1); 702 const uint64_t amv = va_of(g, g->lo.am_v), ami = va_of(g, g->lo.am_i); 703 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); 704 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); 705 int rc = add_glue(g, OMEGA_GPU_Q36_K_ARGMAX, K_AM0, g->am_ctas, 1, (size_t)g->am_ctas * OMEGA_DEC_ARGMAX_SPAN, a0, OMEGA_DEC_AM_WORDS); 706 if (rc == OMEGA_GPU_DGRAPH_OK) rc = add_glue(g, OMEGA_GPU_Q36_K_ARGMAX, K_AM1, 1, 1, OMEGA_DEC_ARGMAX_SPAN, a1, OMEGA_DEC_AM_WORDS); 707 return rc; 708 } 709 710 int omega_gpu_q36_graph_record_final(OmegaGpuQ36Graph *g) { return omega_gpu_q36_graph_record_head_final(g, 0); } 711 712 int omega_gpu_q36_graph_record_head_final(OmegaGpuQ36Graph *g, uint32_t h) { 713 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; 714 if (g->failed) return OMEGA_GPU_DGRAPH_CHIP_FAIL; 715 if (h >= g->stroke_heads) return fail(g, "q36 graph: head not in the stroke", OMEGA_GPU_DGRAPH_BAD_ARGS); 716 if (!g->final_set) return fail(g, "q36 graph: final norm not set", OMEGA_GPU_DGRAPH_BAD_ARGS); 717 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)); 718 int rc = OMEGA_GPU_DGRAPH_OK; 719 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); 720 const uint64_t amv = va_of(g, g->lo.am_v), ami = va_of(g, g->lo.am_i); 721 if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_q36_graph_exact_head_active_head(g, h)) { 722 /* HUM L314 two-pass head: h8 = hidden * 2^64 (exact); s = screen . h8; lo / hi = s -/+ c|hidden|; tau = max lo; 723 * logits = lm_head . hidden on the rows with hi >= tau (today's bits), -inf elsewhere. The argmax below is then 724 * today's token: the full argmax row has hi >= its logit >= tau (omega_blackwell_codegen_dec.h XBOUND). */ 725 uint32_t xs[OMEGA_DEC_XB_WORDS], xb[OMEGA_DEC_XB_WORDS], t0[OMEGA_DEC_AM_WORDS], t1[OMEGA_DEC_AM_WORDS]; 726 memset(xs, 0, sizeof xs); memset(xb, 0, sizeof xb); memset(t0, 0, sizeof t0); memset(t1, 0, sizeof t1); 727 put64(xs, OMEGA_DEC_XB_S, hidden); put64(xs, OMEGA_DEC_XB_LO, g->xh.h8); xs[(OMEGA_DEC_XB_N - 0x380) / 4] = H; 728 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); 729 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; 730 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); 731 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); 732 rc = add_glue(g, OMEGA_GPU_Q36_K_LM_HEAD, K_XS, H / OMEGA_DEC_THREADS, 1, H, xs, OMEGA_DEC_XB_WORDS); 733 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_fp8(g, OMEGA_GPU_Q36_K_LM_HEAD, g->xh.screen, g->xh.tab, g->xh.h8, g->xh.s); 734 if (rc == OMEGA_GPU_DGRAPH_OK) 735 rc = add_glue(g, OMEGA_GPU_Q36_K_LM_HEAD, g->mutant == 6 ? K_XB_M1 : K_XB, (g->cfg.vocab + OMEGA_DEC_THREADS - 1) / OMEGA_DEC_THREADS, 1, 736 g->cfg.vocab, xb, OMEGA_DEC_XB_WORDS); 737 if (rc == OMEGA_GPU_DGRAPH_OK) rc = add_glue(g, OMEGA_GPU_Q36_K_ARGMAX, K_AM0, g->am_ctas, 1, (size_t)g->am_ctas * OMEGA_DEC_ARGMAX_SPAN, t0, OMEGA_DEC_AM_WORDS); 738 if (rc == OMEGA_GPU_DGRAPH_OK) rc = add_glue(g, OMEGA_GPU_Q36_K_ARGMAX, K_AM1, 1, 1, OMEGA_DEC_ARGMAX_SPAN, t1, OMEGA_DEC_AM_WORDS); 739 if (rc == OMEGA_GPU_DGRAPH_OK) 740 rc = after_op(g, OMEGA_GPU_Q36_K_LM_HEAD, omega_gpu_bf16_gemv_mask_record(g->cl, g->cfg.lm_head, hidden, logits, g->xh.hi, g->xh.tau)); 741 } else if (rc == OMEGA_GPU_DGRAPH_OK) 742 rc = g->head8 ? rec_fp8(g, OMEGA_GPU_Q36_K_LM_HEAD, g->head8, va_of(g, g->lo.tabs) + (uint64_t)g->cfg.layers * 64u, hidden, logits) 743 : after_op(g, OMEGA_GPU_Q36_K_LM_HEAD, omega_gpu_bf16_gemv_record(g->cl, g->cfg.lm_head, hidden, logits)); 744 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_argmax(g, logits, token); 745 return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc); 746 } 747 748 /* The batch's final head (v2 lever 1, L312): every head's hidden is ready after the last layer (record_layer_heads), so 749 * the bf16 lm_head runs once per group of up to OMEGA_BF16_MAX_XBATCH heads (the XBATCH form: x rows hidden_of(h0..), 750 * y rows logits_of(h0..), ystride the logits slot; each row bit-identical to its own one-head launch), then the argmax 751 * per head. The FP8 head keeps the per-head path (stroke() calls record_head_final). */ 752 static int record_final_heads(OmegaGpuQ36Graph *g) { 753 const uint32_t N = g->stroke_heads; 754 if (!g->final_set) return fail(g, "q36 graph: final norm not set", OMEGA_GPU_DGRAPH_BAD_ARGS); 755 for (uint32_t h = 0; h < N; h++) 756 if (!g->hidden_ready[h]) return fail(g, "q36 graph: batch final before the last layer", OMEGA_GPU_DGRAPH_BAD_ARGS); 757 int rc = OMEGA_GPU_DGRAPH_OK; 758 for (uint32_t h0 = 0; h0 < N && rc == OMEGA_GPU_DGRAPH_OK; h0 += OMEGA_BF16_MAX_XBATCH) { 759 const uint32_t xb = N - h0 < OMEGA_BF16_MAX_XBATCH ? N - h0 : OMEGA_BF16_MAX_XBATCH; 760 rc = after_op(g, OMEGA_GPU_Q36_K_LM_HEAD, omega_gpu_bf16_gemv_xbatch_record(g->cl, g->cfg.lm_head, xb, va_of(g, hidden_of(g, h0)), 761 va_of(g, logits_of(g, h0)), g->am_ctas * OMEGA_DEC_ARGMAX_SPAN)); 762 } 763 for (uint32_t h = 0; h < N && rc == OMEGA_GPU_DGRAPH_OK; h++) rc = rec_argmax(g, va_of(g, logits_of(g, h)), va_of(g, token_of(g, h))); 764 return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc); 765 } 766 767 int omega_gpu_q36_graph_end_token(OmegaGpuQ36Graph *g, uint32_t *token, OmegaGpuDecodeGraphStats *st) { return omega_gpu_q36_graph_end_stroke(g, token, st); } 768 769 int omega_gpu_q36_graph_end_stroke(OmegaGpuQ36Graph *g, uint32_t *tokens, OmegaGpuDecodeGraphStats *st) { 770 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; 771 int rc = g->failed ? OMEGA_GPU_DGRAPH_CHIP_FAIL : OMEGA_GPU_DGRAPH_OK; 772 const uint64_t t_rec = omega_gpu_session_now_ns(); 773 if (rc == OMEGA_GPU_DGRAPH_OK) rc = submit_now(g); 774 for (uint32_t h = 0; h < g->stroke_heads && rc == OMEGA_GPU_DGRAPH_OK; h++) 775 if (omega_gpu_decode_attn256_counters_check(g->attn_h[h]) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); 776 if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_session_sync_to_cpu(&g->buf) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); 777 for (uint32_t h = 0; h < g->stroke_heads && rc == OMEGA_GPU_DGRAPH_OK; h++) { 778 const uint32_t t = ((const uint32_t *)cpu_of(g, token_of(g, h)))[0]; 779 if (t == TOKEN_POISON || t >= g->cfg.vocab) { 780 char e[96]; omega_gpu_fmt(e, sizeof e, "q36 graph: head %u token output %u not written or out of range", h, t); 781 rc = fail(g, e, OMEGA_GPU_DGRAPH_CHIP_FAIL); 782 } else if (tokens) tokens[h] = t; 783 } 784 omega_gpu_cmdlist_reset(g->cl); 785 g->in_token = 0; 786 const uint64_t t_end = omega_gpu_session_now_ns(); 787 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; } 788 omega_gpu_session_unlock(); 789 return rc; 790 } 791 792 int omega_gpu_q36_graph_token(OmegaGpuQ36Graph *g, const float *x_embed, uint32_t pos, uint32_t *token, OmegaGpuDecodeGraphStats *st) { 793 if (!g) return OMEGA_GPU_DGRAPH_BAD_ARGS; 794 if (omega_gpu_q36_graph_begin_token(g, x_embed) == OMEGA_GPU_DGRAPH_BAD_ARGS) return OMEGA_GPU_DGRAPH_BAD_ARGS; 795 for (uint32_t l = 0; l < g->cfg.layers && !g->failed; l++) (void)omega_gpu_q36_graph_record_layer(g, l, pos); 796 if (!g->failed) (void)omega_gpu_q36_graph_record_final(g); 797 return omega_gpu_q36_graph_end_token(g, token, st); 798 } 799 800 int omega_gpu_q36_graph_stroke(OmegaGpuQ36Graph *g, const float *x_embed, const uint32_t *pos, uint32_t heads, uint32_t *tokens, 801 OmegaGpuDecodeGraphStats *st) { 802 if (!g || !pos) return OMEGA_GPU_DGRAPH_BAD_ARGS; 803 if (omega_gpu_q36_graph_begin_stroke(g, x_embed, heads) == OMEGA_GPU_DGRAPH_BAD_ARGS) return OMEGA_GPU_DGRAPH_BAD_ARGS; 804 /* layer-major: every head's layer l before layer l + 1, so one layer's expert rows are warm across the heads; the batch 805 * (set_batch) records the layer for every head in one set of launches, each weight read once */ 806 for (uint32_t l = 0; l < g->cfg.layers && !g->failed; l++) { 807 if (g->batch) (void)omega_gpu_q36_graph_record_layer_heads(g, l, pos); 808 else for (uint32_t h = 0; h < heads && !g->failed; h++) (void)omega_gpu_q36_graph_record_head_layer(g, h, l, pos[h]); 809 } 810 if (g->batch && !g->head8) { if (!g->failed) (void)record_final_heads(g); } 811 else for (uint32_t h = 0; h < heads && !g->failed; h++) (void)omega_gpu_q36_graph_record_head_final(g, h); 812 return omega_gpu_q36_graph_end_stroke(g, tokens, st); 813 } 814 815 int omega_gpu_q36_graph_read(OmegaGpuQ36Graph *g, float *x, float *hidden, float *logits) { return omega_gpu_q36_graph_read_head(g, 0, x, hidden, logits); } 816 817 int omega_gpu_q36_graph_read_head(OmegaGpuQ36Graph *g, uint32_t h, float *x, float *hidden, float *logits) { 818 if (!g || g->in_token || h >= g->heads) return OMEGA_GPU_DGRAPH_BAD_ARGS; 819 omega_gpu_session_lock(); 820 int rc = omega_gpu_session_sync_to_cpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL; 821 if (rc == OMEGA_GPU_DGRAPH_OK) { 822 if (x) memcpy(x, cpu_of(g, x_of(g, h)), H * 4u); 823 if (hidden) memcpy(hidden, cpu_of(g, hidden_of(g, h)), H * 4u); 824 if (hidden && g->head8) for (unsigned i = 0; i < H; i++) hidden[i] *= 0x1p-64f; /* exact: a power of two */ 825 if (logits) memcpy(logits, cpu_of(g, logits_of(g, h)), (size_t)g->cfg.vocab * 4u); 826 } 827 omega_gpu_session_unlock(); 828 return rc; 829 } 830 831 int omega_gpu_q36_graph_read_buf(OmegaGpuQ36Graph *g, int which, float *out, uint32_t n) { 832 if (!g || g->in_token || !out || which < 0 || which >= OMEGA_GPU_Q36_BUF_COUNT) return OMEGA_GPU_DGRAPH_BAD_ARGS; 833 const struct { size_t off; uint32_t cap; } b[OMEGA_GPU_Q36_BUF_COUNT] = { 834 [OMEGA_GPU_Q36_BUF_X] = { g->lo.x, H }, [OMEGA_GPU_Q36_BUF_XQ] = { g->lo.xq, H }, [OMEGA_GPU_Q36_BUF_MIX] = { g->lo.mix, OMEGA_GPU_Q36_GDN_MIX }, 835 [OMEGA_GPU_Q36_BUF_AB] = { g->lo.ab, OMEGA_GPU_Q36_AB }, [OMEGA_GPU_Q36_BUF_AGG] = { g->lo.agg, OMEGA_GPU_Q36_ATT_MIX }, 836 [OMEGA_GPU_Q36_BUF_ATT] = { g->lo.att, OMEGA_GPU_Q36_MIXER_OUT }, [OMEGA_GPU_Q36_BUF_O] = { g->lo.o, H }, 837 [OMEGA_GPU_Q36_BUF_GU] = { g->lo.gu, NG * 2 * EI }, [OMEGA_GPU_Q36_BUF_ACT] = { g->lo.act, NG * EI }, [OMEGA_GPU_Q36_BUF_HIDDEN] = { g->lo.hidden, H }, 838 [OMEGA_GPU_Q36_BUF_DN] = { g->lo.dn, NG * H }, 839 }; 840 if (n > b[which].cap) return OMEGA_GPU_DGRAPH_BAD_ARGS; 841 omega_gpu_session_lock(); 842 int rc = OMEGA_GPU_DGRAPH_OK; 843 size_t from = b[which].off; 844 if (from >= g->lo.split && n) { 845 /* a chip-only region: K_COPY moves ceil(n / 256) rows of 256 floats into stage (every cap is a multiple of 256) */ 846 uint32_t w[OMEGA_DEC_GA_WORDS]; memset(w, 0, sizeof w); 847 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)); 848 const Kern *k = &g->k[K_COPY]; 849 OmegaGpuLaunch L; memset(&L, 0, sizeof L); 850 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; 851 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; 852 if (!omega_gpu_session_open() || omega_gpu_session_launch(&L, NULL, NULL) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; 853 from = g->lo.stage; 854 } 855 if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_session_sync_to_cpu(&g->buf) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; 856 if (rc == OMEGA_GPU_DGRAPH_OK) memcpy(out, cpu_of(g, from), (size_t)n * 4u); 857 omega_gpu_session_unlock(); 858 return rc; 859 } 860 861 /* ---- timeline ---- */ 862 void omega_gpu_q36_graph_set_stamps(OmegaGpuQ36Graph *g, int on) { if (g && !g->in_token) g->stamps = on ? 1 : 0; } 863 864 int omega_gpu_q36_graph_stamps(const OmegaGpuQ36Graph *g, uint64_t *stamps, uint8_t *kinds, uint32_t cap) { 865 if (!g || g->in_token || !stamps || !kinds) return OMEGA_GPU_DGRAPH_BAD_ARGS; 866 if (!g->stamps || g->n_stamp != g->n_kind + 1u) { omega_gpu_session_set_error("q36 graph: no timeline for the last token (stamps off or it failed)"); return OMEGA_GPU_DGRAPH_BAD_ARGS; } 867 if (cap < g->n_stamp) { omega_gpu_session_set_error("q36 graph: timeline buffers too small"); return OMEGA_GPU_DGRAPH_BAD_ARGS; } 868 memcpy(stamps, g->stamp, (size_t)g->n_stamp * sizeof stamps[0]); 869 memcpy(kinds, g->kind, (size_t)g->n_kind); 870 return (int)g->n_kind; 871 } 872 873 const char *omega_gpu_q36_graph_kind_name(uint8_t kind) { 874 static const char *const names[OMEGA_GPU_Q36_K_COUNT] = { "norm", "mix", "ab", "gdn", "gather", "attn", "gate", "o_proj", "res", "router", 875 "gate_up", "swiglu", "down", "lm_head", "argmax", "perm", "group" }; 876 return kind < OMEGA_GPU_Q36_K_COUNT ? names[kind] : "?"; 877 } 878 879 /* ---- the prefill (L369): one conversation's prompt chunk through the chip as rows ---- 880 * Every arithmetic step of a prompt token is the decode step's: the dense trunk and the experts through the fp8 gemm record 881 * path (bit-identical to the GEMV per row), a|b through the bf16 ROWS form, the router through its ROWS form, and the GDN step 882 * and the attention launched once per token, in token order, on the conversation's own state at pos0 + t. The expert rows are 883 * grouped on the host between two lists per layer (the GROUP kernel holds 128 rows; a chunk holds up to 128 x 8). */ 884 #define PF_PAD 8u 885 #define PF_EXPERT_BYTES ((uint64_t)3u * EI * H) /* one expert's e4m3 gate|up (2 x EI x H) and down (H x EI) */ 886 static int pf_fail(OmegaGpuQ36Graph *g, const char *what) { return fail(g, what, OMEGA_GPU_DGRAPH_BAD_ARGS); } 887 888 int omega_gpu_q36_graph_prefill_alloc(OmegaGpuQ36Graph *g, uint32_t cap) { 889 if (!g || g->in_token || cap == 0 || cap > OMEGA_GPU_Q36_PREFILL_MAX_ROWS) return OMEGA_GPU_DGRAPH_BAD_ARGS; 890 if (g->pf.cap >= cap) return OMEGA_GPU_DGRAPH_OK; 891 const size_t P = cap, R = P * NG, RR = P * (NG - 1u), lbs = g->lo.lb_stride; 892 size_t ho = 0, dof = 0; 893 #define HREG(f, b) do { g->pf.f = ho; ho += al(b); } while (0) 894 #define DREG(f, b) do { g->pf.f = dof; dof += al(b); } while (0) 895 HREG(x, P * H * 4); HREG(rlb, P * lbs); HREG(sgu, 256u * OMEGA_GEMM_SEG_BYTES); HREG(sdn, 256u * OMEGA_GEMM_SEG_BYTES); 896 HREG(sgu2, OMEGA_GEMM_SEG_BYTES); HREG(sdn2, OMEGA_GEMM_SEG_BYTES); HREG(xmap, (RR + PF_PAD) * 4); HREG(ymap, (RR + PF_PAD) * 4); 897 HREG(xmap2, (P + PF_PAD) * 4); HREG(ymap2, (P + PF_PAD) * 4); 898 DREG(xq, P * H * 4); DREG(xp, (P + PF_PAD) * H * 4); DREG(mixN, P * OMEGA_GPU_Q36_GDN_MIX * 4); DREG(abN, P * OMEGA_GPU_Q36_AB * 4); 899 DREG(aggN, P * OMEGA_GPU_Q36_ATT_MIX * 4); DREG(attN, P * OMEGA_GPU_Q36_MIXER_OUT * 4); DREG(op, (P + PF_PAD) * OMEGA_GPU_Q36_MIXER_OUT * 4); 900 DREG(oN, P * H * 4); DREG(xp2, (P + PF_PAD) * H * 4); DREG(guN, R * 2 * EI * 4); DREG(actN, R * EI * 4); DREG(actp, (R + PF_PAD) * EI * 4); 901 DREG(dnN, R * H * 4); 902 #undef HREG 903 #undef DREG 904 omega_gpu_session_lock(); 905 if (g->pf.hb.cpu) omega_gpu_session_free(&g->pf.hb); 906 if (g->pf.db.cpu) omega_gpu_session_free(&g->pf.db); 907 g->pf.cap = 0; 908 const char *pbc = getenv("AIEN_OMEGA_Q36_BUF_CACHED"); 909 int (*const dalloc)(size_t, OmegaGpuMem *) = (pbc && pbc[0] == '0' && pbc[1] == 0) ? omega_gpu_session_alloc : omega_gpu_session_alloc_cached; 910 int rc = (!omega_gpu_session_open() || omega_gpu_session_alloc(ho, &g->pf.hb) != 0 || dalloc(dof, &g->pf.db) != 0) ? OMEGA_GPU_DGRAPH_CHIP_FAIL 911 : OMEGA_GPU_DGRAPH_OK; 912 if (rc == OMEGA_GPU_DGRAPH_OK) memset(g->pf.hb.cpu, 0, ho); 913 omega_gpu_session_unlock(); 914 /* pin the shapes the prefill records (a kernel cache miss while recording is refused): the dense trunk without maps (8 rows 915 * per pass), the expert blocks with the row maps at 4 rows per pass, the router's ROWS kernel */ 916 if (rc == OMEGA_GPU_DGRAPH_OK) { 917 int r = omega_gpu_fp8gemm_prepare_shape(H, 1, 0, 0); 918 if (r == 0) r = omega_gpu_fp8gemm_prepare_shape(OMEGA_GPU_Q36_MIXER_OUT, 1, 0, 0); 919 if (r == 0) r = omega_gpu_fp8gemm_prepare_shape(H, 64, 4, 1); 920 if (r == 0) r = omega_gpu_fp8gemm_prepare_shape(EI, 64, 4, 1); 921 if (r == 0 && omega_gpu_router_prepare_rows(g->cfg.router, H * 4u, g->lo.lb_stride) != OMEGA_GPU_ROUTER_OK) r = -1; 922 if (r != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; 923 } 924 if (rc != OMEGA_GPU_DGRAPH_OK) return rc; 925 const char *m = getenv("AIEN_OMEGA_Q36_PF_MUTANT"); 926 g->pf.mutant = m ? atoi(m) : 0; 927 const char *gm = getenv("AIEN_OMEGA_Q36_PF_GDN"); 928 g->pf.gdn_multi = !(gm && gm[0] == '0' && gm[1] == 0); 929 for (uint32_t h = 0; h < g->heads && g->pf.gdn_multi; h++) 930 if (g->gdn_h[h] && omega_gpu_gdn_multi_prepare(g->gdn_h[h]) != OMEGA_GPU_GDN_OK) return OMEGA_GPU_DGRAPH_CHIP_FAIL; 931 const char *am = getenv("AIEN_OMEGA_Q36_PF_ATTN"); 932 g->pf.attn_multi = !(am && am[0] == '0' && am[1] == 0); 933 for (uint32_t h = 0; h < g->heads && g->pf.attn_multi; h++) 934 if (g->attn_h[h] && omega_gpu_decode_attn256_multi_prepare(g->attn_h[h]) != OMEGA_GPU_DATTN_OK) return OMEGA_GPU_DGRAPH_CHIP_FAIL; 935 omega_gpu_decode_attn256_test_set_multi_mutant(g->pf.mutant == 6 ? 1 : 0); /* TEST mutant 6: the attention prep launch dropped */ 936 g->pf.cap = cap; 937 return OMEGA_GPU_DGRAPH_OK; 938 } 939 uint32_t omega_gpu_q36_graph_prefill_cap(const OmegaGpuQ36Graph *g) { return g ? g->pf.cap : 0; } 940 uint64_t omega_gpu_q36_graph_prefill_streamed(const OmegaGpuQ36Graph *g) { return g ? g->pf.streamed : 0; } 941 942 static uint64_t pf_h(const OmegaGpuQ36Graph *g, size_t off) { return g->pf.hb.va + off; } 943 static void *pf_hc(const OmegaGpuQ36Graph *g, size_t off) { return (uint8_t *)g->pf.hb.cpu + off; } 944 static uint64_t pf_d(const OmegaGpuQ36Graph *g, size_t off) { return g->pf.db.va + off; } 945 946 /* part (a) of layer l: the mixer of every row and the router; ends with the router outputs in pf.rlb */ 947 static int pf_record_mixer(OmegaGpuQ36Graph *g, uint32_t head, uint32_t l, uint32_t n, uint32_t pos0, uint32_t *ids_off, uint32_t *wts_off, uint32_t *xo_off) { 948 const uint32_t ki = g->layer[l].kind_index; 949 const uint64_t x0 = pf_h(g, g->pf.x), xq0 = pf_d(g, g->pf.xq), xp = pf_d(g, g->pf.xp), mixN = pf_d(g, g->pf.mixN), abN = pf_d(g, g->pf.abN); 950 const uint64_t aggN = pf_d(g, g->pf.aggN), attN = pf_d(g, g->pf.attN), op = pf_d(g, g->pf.op), oN = pf_d(g, g->pf.oN), rlb = pf_h(g, g->pf.rlb); 951 const uint64_t pt2048 = va_of(g, g->lo.ptab), pt4096 = pt2048 + (uint64_t)H * 4u; 952 const uint64_t in_norm = va_of(g, g->lo.norms) + (uint64_t)l * H * 4u, segd = va_of(g, g->lo.segd) + (uint64_t)l * SEGD_ROWS * OMEGA_GEMM_SEG_BYTES; 953 const uint64_t XS = (uint64_t)H * 4u, MOS = (uint64_t)OMEGA_GPU_Q36_MIXER_OUT * 4u; 954 int rc = OMEGA_GPU_DGRAPH_OK; 955 /* layer 0 normalizes the embeddings; after that the previous layer's per-token COMBINE|NORM left layer l's x' in xq */ 956 if (l == 0) rc = rec_res_rows(g, K_NORM_R, x0, 0, in_norm, xq0, SCALE_2P64, n); 957 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_XQ, xq0, pt2048, xp, H, n); 958 if (!g->layer[l].attn) { 959 if (rc == OMEGA_GPU_DGRAPH_OK) rc = after_op(g, OMEGA_GPU_Q36_K_AB, omega_gpu_bf16_gemv_rows_record(g->cl, g->layer[l].ab, n, xq0, abN)); 960 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_gemm(g, OMEGA_GPU_Q36_K_MIX, g->layer[l].mix, n, 1, segd, xp, mixN, 0, 0); 961 if (g->pf.gdn_multi && g->pf.mutant != 1 && g->pf.mutant != 3) { 962 /* every token's recurrence step in one launch (the decode step T times, bit for bit: omega_gpu_gdn_api.h), the rows 963 * into attN (unused on a GDN layer), then one PERM over the rows. TEST mutant 5: every token reads token 0's a|b. */ 964 if (rc == OMEGA_GPU_DGRAPH_OK) 965 rc = after_op(g, OMEGA_GPU_Q36_K_GDN, omega_gpu_gdn_multi_record(g->cl, g->gdn_h[head], ki, n, mixN, mixN + OMEGA_GPU_Q36_GDN_QKV * 4u, 966 OMEGA_GPU_Q36_GDN_MIX * 4u, abN, g->pf.mutant == 5 ? 0u : OMEGA_GPU_Q36_AB * 4u, 967 attN, (uint32_t)MOS, g->cfg.eps_l2, g->cfg.eps, 1.0f, SCALE_2P64)); 968 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_O, attN, pt4096, op, OMEGA_GPU_Q36_MIXER_OUT, n); 969 } else 970 for (uint32_t i = 0; i < n && rc == OMEGA_GPU_DGRAPH_OK; i++) { 971 /* TEST mutant 1: the last two tokens' recurrence steps in swapped order */ 972 const uint32_t t = (g->pf.mutant == 1 && n >= 2 && i + 2 >= n) ? (i + 2 == n ? n - 1 : n - 2) : i; 973 const uint64_t mix_t = mixN + (uint64_t)t * OMEGA_GPU_Q36_GDN_MIX * 4u; 974 uint64_t mo = 0; 975 rc = after_op(g, OMEGA_GPU_Q36_K_GDN, omega_gpu_gdn_record(g->cl, g->gdn_h[head], ki, mix_t, mix_t + OMEGA_GPU_Q36_GDN_QKV * 4u, 976 abN + (uint64_t)t * OMEGA_GPU_Q36_AB * 4u, g->cfg.eps_l2, g->cfg.eps, 1.0f, SCALE_2P64, &mo)); 977 /* TEST mutant 3: the copy out of the GDN launch buffer deferred past the next token's step (the buffer is the 978 * state object's own, one per conversation: every row would read a later token's output) */ 979 if (rc == OMEGA_GPU_DGRAPH_OK && !(g->pf.mutant == 3 && t + 1 < n)) rc = rec_perm(g, K_PERM_O, mo, pt4096, op + t * MOS, OMEGA_GPU_Q36_MIXER_OUT, 1); 980 if (rc == OMEGA_GPU_DGRAPH_OK && g->pf.mutant == 3 && t > 0) rc = rec_perm(g, K_PERM_O, mo, pt4096, op + (t - 1) * MOS, OMEGA_GPU_Q36_MIXER_OUT, 1); 981 } 982 } else { 983 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_gemm(g, OMEGA_GPU_Q36_K_MIX, g->layer[l].mix, n, 1, segd, xp, mixN, 0, 0); 984 uint32_t w[OMEGA_DEC_GA_WORDS]; memset(w, 0, sizeof w); 985 put64(w, OMEGA_DEC_GA_IN, mixN); put64(w, OMEGA_DEC_GA_TAB, va_of(g, g->lo.gtab)); put64(w, OMEGA_DEC_GA_OUT, aggN); 986 if (rc == OMEGA_GPU_DGRAPH_OK) 987 rc = add_glue(g, OMEGA_GPU_Q36_K_GATHER, g->mutant == 2 ? K_GATHER_M1 : K_GATHER, (OMEGA_GPU_Q36_ATT_MIX + OMEGA_DEC_THREADS - 1) / OMEGA_DEC_THREADS, n, 988 (size_t)OMEGA_GPU_Q36_ATT_MIX * n, w, OMEGA_DEC_GA_WORDS); 989 const int attn_multi = g->pf.attn_multi; 990 if (attn_multi && rc == OMEGA_GPU_DGRAPH_OK) /* every token's attention in one prep + attend pair, bit-identical to the steps */ 991 rc = after_op(g, OMEGA_GPU_Q36_K_ATTN, omega_gpu_decode_attn256_multi_record(g->cl, g->attn_h[head], ki, pos0, n, aggN, OMEGA_GPU_Q36_ATT_MIX * 4u, 992 g->cfg.eps, SCALE_2P64, attN, (uint32_t)MOS)); 993 const int gate_rows = attn_multi && g->mutant != 3; /* every token's gate in one rows launch (the one-row arithmetic per element) */ 994 if (gate_rows && rc == OMEGA_GPU_DGRAPH_OK) { 995 uint32_t w2[OMEGA_DEC_GT_WORDS]; memset(w2, 0, sizeof w2); 996 put64(w2, OMEGA_DEC_GT_A, attN); put64(w2, OMEGA_DEC_GT_G, aggN + OMEGA_GPU_Q36_ATT_QKV * 4u); put64(w2, OMEGA_DEC_GT_OUT, attN); 997 w2[(OMEGA_DEC_GT_N - 0x380) / 4] = OMEGA_GPU_Q36_MIXER_OUT; 998 rc = add_glue(g, OMEGA_GPU_Q36_K_GATE, K_GATE_R, OMEGA_GPU_Q36_MIXER_OUT / OMEGA_DEC_THREADS, n, (size_t)OMEGA_GPU_Q36_MIXER_OUT * n, w2, OMEGA_DEC_GT_WORDS); 999 } 1000 for (uint32_t t = 0; t < n && rc == OMEGA_GPU_DGRAPH_OK && !gate_rows; t++) { 1001 const uint64_t agg_t = aggN + (uint64_t)t * OMEGA_GPU_Q36_ATT_MIX * 4u, att_t = attN + t * MOS; 1002 if (!attn_multi) rc = after_op(g, OMEGA_GPU_Q36_K_ATTN, omega_gpu_decode_attn256_record(g->cl, g->attn_h[head], ki, pos0 + t, agg_t, g->cfg.eps, SCALE_2P64, att_t)); 1003 uint32_t w2[OMEGA_DEC_GT_WORDS]; memset(w2, 0, sizeof w2); 1004 put64(w2, OMEGA_DEC_GT_A, att_t); put64(w2, OMEGA_DEC_GT_G, agg_t + OMEGA_GPU_Q36_ATT_QKV * 4u); put64(w2, OMEGA_DEC_GT_OUT, att_t); 1005 w2[(OMEGA_DEC_GT_N - 0x380) / 4] = OMEGA_GPU_Q36_MIXER_OUT; 1006 if (rc == OMEGA_GPU_DGRAPH_OK) 1007 rc = add_glue(g, OMEGA_GPU_Q36_K_GATE, g->mutant == 3 ? K_GATE_M1 : K_GATE, OMEGA_GPU_Q36_MIXER_OUT / OMEGA_DEC_THREADS, 1, 1008 OMEGA_GPU_Q36_MIXER_OUT, w2, OMEGA_DEC_GT_WORDS); 1009 } 1010 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_O, attN, pt4096, op, OMEGA_GPU_Q36_MIXER_OUT, n); 1011 } 1012 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_gemm(g, OMEGA_GPU_Q36_K_O, g->layer[l].o, n, 1, segd + OMEGA_GEMM_SEG_BYTES, op, oN, 0, 0); 1013 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_res_rows(g, K_ADD_R, x0, oN, 0, 0, 1.0f, n); 1014 for (uint32_t r0 = 0; r0 < n && rc == OMEGA_GPU_DGRAPH_OK; r0 += OMEGA_GPU_ROUTER_MAX_ROWS) { 1015 const uint32_t rows = n - r0 < OMEGA_GPU_ROUTER_MAX_ROWS ? n - r0 : OMEGA_GPU_ROUTER_MAX_ROWS; 1016 OmegaGpuRouterDevOut r; memset(&r, 0, sizeof r); 1017 const uint64_t lb = rlb + (uint64_t)r0 * g->lo.lb_stride; 1018 rc = after_op(g, OMEGA_GPU_Q36_K_ROUTER, omega_gpu_router_record_rows(g->cl, g->cfg.router, l, x0 + r0 * XS, (uint32_t)XS, g->cfg.eps, 1, SCALE_2P64, 1019 lb, g->lo.lb_stride, rows, &r)); 1020 if (rc == OMEGA_GPU_DGRAPH_OK && r0 == 0) { *ids_off = (uint32_t)(r.ids_va - lb); *wts_off = (uint32_t)(r.wts_va - lb); *xo_off = (uint32_t)(r.xout_va - lb); } 1021 } 1022 return rc; 1023 } 1024 1025 /* the host GROUP: the n * 8 routed picks sorted by expert id (then token, then slot) into segments and row maps, the shared 1026 * expert as its own one-segment table (x' row t, y row t * 9 + 8). Returns the routed segment count. */ 1027 static uint32_t pf_group(OmegaGpuQ36Graph *g, uint32_t l, uint32_t n, uint32_t ids_off) { 1028 const uint32_t S = NG - 1u; 1029 uint32_t cnt[256], start[256]; 1030 memset(cnt, 0, sizeof cnt); 1031 for (uint32_t t = 0; t < n; t++) { 1032 const uint32_t *id = pf_hc(g, g->pf.rlb + (size_t)t * g->lo.lb_stride + ids_off); 1033 for (uint32_t s = 0; s < S; s++) cnt[id[s] & 255u]++; 1034 } 1035 Seg32 *sg = pf_hc(g, g->pf.sgu), *sd = pf_hc(g, g->pf.sdn); 1036 uint32_t segs = 0, row = 0; 1037 for (uint32_t e = 0; e < 256u; e++) { 1038 start[e] = row; 1039 if (!cnt[e]) continue; 1040 memset(&sg[segs], 0, sizeof *sg); memset(&sd[segs], 0, sizeof *sd); 1041 (void)omega_gpu_fp8_tensor_entry(g->layer[l].gu, e, &sg[segs].w_va, &sg[segs].s_va); 1042 (void)omega_gpu_fp8_tensor_entry(g->layer[l].dn, e, &sd[segs].w_va, &sd[segs].s_va); 1043 sg[segs].row0 = sd[segs].row0 = row; sg[segs].cnt = sd[segs].cnt = cnt[e]; 1044 if (g->pf.mutant == 2 && cnt[e] > 1u) { sg[segs].cnt--; sd[segs].cnt--; } /* TEST mutant 2: every segment one row short */ 1045 g->pf.streamed += (uint64_t)((cnt[e] + 3u) / 4u) * PF_EXPERT_BYTES; /* gate|up and down, 4 rows per pass */ 1046 row += cnt[e]; segs++; 1047 } 1048 uint32_t *xm = pf_hc(g, g->pf.xmap), *ym = pf_hc(g, g->pf.ymap); 1049 for (uint32_t t = 0; t < n; t++) { 1050 const uint32_t *id = pf_hc(g, g->pf.rlb + (size_t)t * g->lo.lb_stride + ids_off); 1051 for (uint32_t s = 0; s < S; s++) { const uint32_t p = start[id[s] & 255u]++; xm[p] = t; ym[p] = t * NG + s; } 1052 } 1053 for (uint32_t p = n * S; p < n * S + PF_PAD; p++) { xm[p] = 0; ym[p] = 0; } 1054 Seg32 *g2 = pf_hc(g, g->pf.sgu2), *d2 = pf_hc(g, g->pf.sdn2); 1055 memset(g2, 0, sizeof *g2); memset(d2, 0, sizeof *d2); 1056 (void)omega_gpu_fp8_tensor_entry(g->layer[l].gu, 256u, &g2->w_va, &g2->s_va); /* expert 256 = the shared expert */ 1057 (void)omega_gpu_fp8_tensor_entry(g->layer[l].dn, 256u, &d2->w_va, &d2->s_va); 1058 g2->cnt = d2->cnt = n; 1059 g->pf.streamed += (uint64_t)((n + 3u) / 4u) * PF_EXPERT_BYTES; 1060 uint32_t *xm2 = pf_hc(g, g->pf.xmap2), *ym2 = pf_hc(g, g->pf.ymap2); 1061 for (uint32_t t = 0; t < n; t++) { xm2[t] = t; ym2[t] = t * NG + S; } 1062 for (uint32_t p = n; p < n + PF_PAD; p++) { xm2[p] = 0; ym2[p] = 0; } 1063 return segs; 1064 } 1065 1066 /* part (b) of layer l: the experts of every row and the per-token COMBINE|NORM */ 1067 static int pf_record_experts(OmegaGpuQ36Graph *g, uint32_t head, uint32_t l, uint32_t n, uint32_t segs, uint32_t wts_off, uint32_t xo_off) { 1068 const uint64_t x0 = pf_h(g, g->pf.x), xq0 = pf_d(g, g->pf.xq), xp2 = pf_d(g, g->pf.xp2), guN = pf_d(g, g->pf.guN), actN = pf_d(g, g->pf.actN); 1069 const uint64_t actp = pf_d(g, g->pf.actp), dnN = pf_d(g, g->pf.dnN), rlb = pf_h(g, g->pf.rlb); 1070 const uint64_t pt2048 = va_of(g, g->lo.ptab), pt512 = pt2048 + (uint64_t)(H + OMEGA_GPU_Q36_MIXER_OUT) * 4u; 1071 const uint64_t in_norm = va_of(g, g->lo.norms) + (uint64_t)l * H * 4u, XS = (uint64_t)H * 4u; 1072 const uint32_t R = n * NG; const uint32_t S = NG - 1u; /* rows each expert GEMM touches: 8 routed per token, 1 shared */ 1073 int rc = rec_perm(g, K_PERM_XO, rlb + xo_off, pt2048, xp2, H, n); /* the router's x' rows, one launch buffer apart */ 1074 if (rc == OMEGA_GPU_DGRAPH_OK) 1075 rc = after_op(g, OMEGA_GPU_Q36_K_GATE_UP, omega_gpu_fp8gemm_record(g->cl, g->layer[l].gu, n * S, segs, pf_h(g, g->pf.sgu), xp2, guN, pf_h(g, g->pf.xmap), 1076 pf_h(g, g->pf.ymap), 4, 0, NULL)); 1077 if (rc == OMEGA_GPU_DGRAPH_OK) 1078 rc = after_op(g, OMEGA_GPU_Q36_K_GATE_UP, omega_gpu_fp8gemm_record(g->cl, g->layer[l].gu, n, 1, pf_h(g, g->pf.sgu2), xp2, guN, pf_h(g, g->pf.xmap2), 1079 pf_h(g, g->pf.ymap2), 4, 0, NULL)); 1080 uint32_t w3[OMEGA_DEC_SW_WORDS]; memset(w3, 0, sizeof w3); 1081 put64(w3, OMEGA_DEC_SW_GU, guN); put64(w3, OMEGA_DEC_SW_ACT, actN); putf(w3, OMEGA_DEC_SW_SCALE, SCALE_2P64); 1082 if (rc == OMEGA_GPU_DGRAPH_OK) 1083 rc = add_glue(g, OMEGA_GPU_Q36_K_SWIGLU, g->mutant == 1 ? K_SW_M1 : K_SW, EI / OMEGA_DEC_THREADS, R, (size_t)R * EI, w3, OMEGA_DEC_SW_WORDS); 1084 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_ACT, actN, pt512, actp, EI, R); 1085 if (rc == OMEGA_GPU_DGRAPH_OK) 1086 rc = after_op(g, OMEGA_GPU_Q36_K_DOWN, omega_gpu_fp8gemm_record(g->cl, g->layer[l].dn, n * S, segs, pf_h(g, g->pf.sdn), actp, dnN, pf_h(g, g->pf.ymap), 1087 pf_h(g, g->pf.ymap), 4, 0, NULL)); 1088 if (rc == OMEGA_GPU_DGRAPH_OK) 1089 rc = after_op(g, OMEGA_GPU_Q36_K_DOWN, omega_gpu_fp8gemm_record(g->cl, g->layer[l].dn, n, 1, pf_h(g, g->pf.sdn2), actp, dnN, pf_h(g, g->pf.ymap2), 1090 pf_h(g, g->pf.ymap2), 4, 0, NULL)); 1091 const int last = l + 1 == g->cfg.layers; 1092 const uint64_t nw = last ? va_of(g, g->lo.fnorm) : in_norm + H * 4u; 1093 /* the last layer: only the last token's COMBINE|NORM is needed (its hidden feeds the head; the state is complete) */ 1094 if (!last && n > 1u && q36_cn_rows()) /* every token's COMBINE|NORM in one rows launch (L356's rows form, each row the one-row program) */ 1095 return rc == OMEGA_GPU_DGRAPH_OK ? rec_cn_rows(g, x0, dnN, rlb + wts_off, nw, xq0, SCALE_2P64, n) : rc; 1096 for (uint32_t t = last ? n - 1u : 0u; t < n && rc == OMEGA_GPU_DGRAPH_OK; t++) 1097 rc = rec_res_w(g, g->mutant == 4 ? K_CN_M3 : K_CN, x0 + t * XS, dnN + (uint64_t)t * NG * H * 4u, rlb + (uint64_t)t * g->lo.lb_stride + wts_off, nw, 1098 last ? va_of(g, hidden_of(g, head)) : xq0 + t * XS, last && !g->head8 ? 1.0f : SCALE_2P64); 1099 return rc; 1100 } 1101 1102 int omega_gpu_q36_graph_prefill(OmegaGpuQ36Graph *g, uint32_t head, const float *x_embed, uint32_t n, uint32_t pos0, int want_token, 1103 uint32_t *token, OmegaGpuDecodeGraphStats *st) { 1104 if (!g || g->in_token || !x_embed || n == 0 || head >= g->heads) return OMEGA_GPU_DGRAPH_BAD_ARGS; 1105 if (n > g->pf.cap) return OMEGA_GPU_DGRAPH_BAD_ARGS; 1106 if (!g->gdn_h[head] || !g->attn_h[head]) return OMEGA_GPU_DGRAPH_BAD_ARGS; 1107 for (uint32_t l = 0; l < g->cfg.layers; l++) if (!g->layer[l].set) return OMEGA_GPU_DGRAPH_BAD_ARGS; 1108 if (!g->final_set) return OMEGA_GPU_DGRAPH_BAD_ARGS; 1109 omega_gpu_session_lock(); 1110 g->t_begin = omega_gpu_session_now_ns(); 1111 g->in_token = 1; g->failed = 0; g->stroke_heads = head + 1u; 1112 for (uint32_t h = 0; h <= head; h++) { g->xq_layer[h] = -1; g->hidden_ready[h] = 0; } 1113 memset(&g->acc, 0, sizeof g->acc); 1114 g->n_kind = 0; g->tagged_in_list = 0; g->n_stamp = 0; 1115 omega_gpu_cmdlist_reset(g->cl); 1116 int rc = OMEGA_GPU_DGRAPH_OK; 1117 if (!omega_gpu_session_open()) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); 1118 if (rc == OMEGA_GPU_DGRAPH_OK) { 1119 uint32_t *tok = cpu_of(g, token_of(g, head)); tok[0] = TOKEN_POISON; tok[1] = TOKEN_POISON; 1120 memcpy(pf_hc(g, g->pf.x), x_embed, (size_t)n * H * 4u); 1121 write_dense_segments(g, n); 1122 if (omega_gpu_session_sync_to_gpu(&g->buf) != 0 || omega_gpu_session_sync_to_gpu(&g->pf.hb) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); 1123 } 1124 for (uint32_t l = 0; l < g->cfg.layers && rc == OMEGA_GPU_DGRAPH_OK; l++) { 1125 uint32_t ids_off = 0, wts_off = 0, xo_off = 0; 1126 rc = pf_record_mixer(g, head, l, n, pos0, &ids_off, &wts_off, &xo_off); 1127 if (rc == OMEGA_GPU_DGRAPH_OK) rc = submit_now(g); 1128 if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_session_sync_to_cpu(&g->pf.hb) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); 1129 uint32_t segs = 0; 1130 if (rc == OMEGA_GPU_DGRAPH_OK) { 1131 /* the dense trunk: mix (or q|k|v) and o, 8 rows per pass */ 1132 g->pf.streamed += (uint64_t)((n + 7u) / 8u) * H * ((g->layer[l].attn ? OMEGA_GPU_Q36_ATT_MIX : OMEGA_GPU_Q36_GDN_MIX) + OMEGA_GPU_Q36_MIXER_OUT); 1133 segs = pf_group(g, l, n, ids_off); 1134 if (segs == 0 || segs > 256u) rc = pf_fail(g, "q36 prefill: router picks out of range"); 1135 } 1136 if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_session_sync_to_gpu(&g->pf.hb) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); 1137 if (rc == OMEGA_GPU_DGRAPH_OK) rc = pf_record_experts(g, head, l, n, segs, wts_off, xo_off); 1138 if (rc == OMEGA_GPU_DGRAPH_OK && l + 1 < g->cfg.layers) rc = submit_now(g); 1139 } 1140 if (rc == OMEGA_GPU_DGRAPH_OK) { 1141 g->hidden_ready[head] = 1; 1142 if (want_token) { 1143 rc = omega_gpu_q36_graph_record_head_final(g, head); 1144 } 1145 } 1146 if (rc == OMEGA_GPU_DGRAPH_OK) rc = submit_now(g); 1147 if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_decode_attn256_counters_check(g->attn_h[head]) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); 1148 if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_session_sync_to_cpu(&g->buf) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); 1149 if (rc == OMEGA_GPU_DGRAPH_OK && want_token) { 1150 const uint32_t t = ((const uint32_t *)cpu_of(g, token_of(g, head)))[0]; 1151 if (t == TOKEN_POISON || t >= g->cfg.vocab) rc = fail(g, "q36 prefill: token output not written or out of range", OMEGA_GPU_DGRAPH_CHIP_FAIL); 1152 else if (token) *token = t; 1153 } 1154 omega_gpu_cmdlist_reset(g->cl); 1155 g->in_token = 0; 1156 const uint64_t t_end = omega_gpu_session_now_ns(); 1157 if (st) { *st = g->acc; st->record_ns = t_end - g->t_begin; st->total_ns = t_end - g->t_begin; } 1158 omega_gpu_session_unlock(); 1159 return rc; 1160 }