interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / src / omega_gpu_q36_graph.c

2037 lines · 137185 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/*
3 * omega_gpu_q36_graph.c: one Qwen3.6-35B-A3B decode token as one command list (see the header). Records the FP8 / bf16 GEMV,
4 * GDN, hd256 attention and router APIs' *_record launches and the omega_blackwell_codegen_dec glue kernels at width 2048 into an
5 * omega_gpu_cmdlist. Buffers: buf from omega_gpu_session_alloc (GPU-uncached, nvos.h:1117) holds every region the host
6 * touches: x (before), token and logits (after), the tables; dbuf (GPU-cached, omega_gpu_mem_cached.h) holds the chip-only
7 * intermediates (see Layout).
8 */
9#include "omega_gpu_q36_graph.h"
10#include "omega_gpu_cmdlist.h"
11#include "omega_blackwell_codegen_dec.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_gpu_q36_native.h"
17#include "omega_gpu_cmdlist_native.h"
18#include "omega_gpu_graph_om.h"
19#include "omega_gpu_graph_residue.h"
20#include <stdlib.h>
21#include <string.h>
22
23#define TIMEOUT_MS 20000ull
24#define SCALE_2P64 18446744073709551616.0f
25#define NEG_PAD (-1e30f)
26#define TOKEN_POISON 0xffffffffu
27#define TOKEN_STRIDE 256u /* bytes per head's token slot (two u32, 256-aligned) */
28#define H OMEGA_GPU_Q36_HIDDEN
29#define EI OMEGA_GPU_Q36_EXPERT_INTER
30#define NG OMEGA_GPU_Q36_GROUPS
31
32/* glue kernels */
33enum { 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,
34 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,
35 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) */
36#define SEGD_ROWS 2u /* dense segment entries per layer in the batch: mix, o */
37#define COPY_ROW 256u /* K_COPY: GATHER rows of 256 floats through the identity table */
38
39typedef struct { OmegaGpuMem code; uint32_t gpr; size_t code_bytes; } Kern;
40
41/* byte offsets into two buffers, every region 256-byte aligned (RES loads 16-byte vectors). [0, split): buf, GPU-uncached,
42 * every region the host writes or reads. [split, bytes): dbuf, the chip-only intermediates, GPU-cached unless
43 * AIEN_OMEGA_Q36_BUF_CACHED=0 (measured 2026-10-19: every FP8 GEMV CTA reads the whole x' row, and uncached each of those
44 * reads goes to DRAM; cached, down 2100 -> 1735 us and mix 1596 -> 1458 us over 4 layers, runs/probe-buf). read_buf copies
45 * a dbuf region into stage with a chip launch (K_COPY) before the host reads it. */
46typedef struct {
47 size_t x, ab, hidden, logits, am_v, am_i, token, norms, fnorm, tabs, gtab, idtab, stage, segd, ptab, split;
48 size_t xq, mix, agg, att, o, gu, act, dn;
49 /* the batch (v2, L312): every conversation of a layer in one set of launches. N = heads laid out; R = N * NG expert rows */
50 size_t xp, mixN, abN, aggN, attN, op, oN, rlb, sgu, sdn, xmap, ymap, xp2, guN, actN, actp, dnN, bytes;
51 uint32_t lb_stride; /* bytes per conversation's router buffer (rlb), 256-aligned */
52} Layout;
53
54typedef struct Q36MoeObservation Q36MoeObservation;
55static uint64_t q36_owner_counter;
56/* A lifetime ticket belongs to this owner class, survives device close, and
57 * cannot be reconstructed from a VA, typed handle, epoch or accepted query. */
58static uint64_t q36_owner_next(void) {
59 uint64_t next = 0;
60 if (omega_native_revision(&next, q36_owner_counter) != OMEGA_OK || !next) return 0;
61 q36_owner_counter = next;
62 return next;
63}
64
65struct OmegaGpuQ36Graph {
66 OmegaGpuQ36GraphConfig cfg;
67 uint32_t am_ctas; /* stage-1 ARGMAX CTAs = ceil(vocab / 2048) */
68 Layout lo;
69 OmegaGpuMem buf, dbuf; /* the host-visible regions; the chip-only regions (see Layout) */
70 Kern k[K_COUNT];
71 OmegaGpuCmdList *cl;
72 OmegaGpuQ36NativeGraph *native; /* NULL until exact independent qualification is admitted */
73 uint64_t native_position[3]; /* actual layer, window token0, window tokens */
74 Q36MoeObservation *moe; /* borrowed only during one locked recording */
75 uint64_t owner_generation, pf_generation;
76 uint64_t tensor_pair_generation[OMEGA_GPU_Q36_MAX_LAYERS];
77 struct { const OmegaGpuFp8Tensor *mix, *o, *gu, *dn; const OmegaGpuBf16Tensor *ab; uint32_t kind_index; int attn, set; } layer[OMEGA_GPU_Q36_MAX_LAYERS];
78 int final_set;
79 const OmegaGpuFp8Tensor *head8; /* set_lm_head_fp8: FP8 lm_head (hidden is then written x 2^64, read() undoes it) */
80 /* heads (strokes): slot h of x, hidden, logits and token; head 0's state objects are cfg.gdn / cfg.attn */
81 uint32_t heads; /* slots laid out (create_heads); 1 = the plain graph */
82 OmegaGpuGdn *gdn_h[OMEGA_GPU_Q36_MAX_HEADS];
83 OmegaGpuDecodeAttn256Cache *attn_h[OMEGA_GPU_Q36_MAX_HEADS];
84 struct { /* set_exact_head: the HUM L314 two-pass head (bf16 lm_head only) */
85 const OmegaGpuFp8Tensor *screen; /* fp8b copy of lm_head (128 x 128 block scales) */
86 OmegaGpuMem mem; /* h8 | s | c | hi | lo (am_ctas * 2048, padded) | tau | tab */
87 uint64_t h8, s, c, hi, lo, tau, tab;
88 uint8_t on[OMEGA_GPU_Q36_MAX_HEADS]; /* per head: its next tokens use it (greedy rows); set_exact_head_on sets every head */
89 } xh;
90 /* token / stroke state */
91 int in_token, failed;
92 uint32_t stroke_heads; /* heads in the open stroke */
93 int xq_layer[OMEGA_GPU_Q36_MAX_HEADS]; /* per head: layer whose input-norm x' sits in the head's xq slot, -1 none
94 * (COMBINE|NORM of layer l writes layer l+1's x' there: the one value that
95 * crosses layers, so it is per head; 02:50Z, found by the stroke gate) */
96 int hidden_ready[OMEGA_GPU_Q36_MAX_HEADS];
97 uint64_t t_begin;
98 OmegaGpuDecodeGraphStats acc;
99 int flush_each, mutant;
100 int batch; /* 1: stroke() records every conversation of a layer at once (v2); set_batch */
101 /* the prefill (L369): one conversation's prompt chunk as rows through the batch's GEMMs; GDN and attention per token in order */
102 struct { uint32_t cap; OmegaGpuMem hb, db; size_t x, rlb, sgu, sdn, sgu2, sdn2, xmap, ymap, xmap2, ymap2;
103 size_t xq, xp, mixN, abN, aggN, attN, op, oN, xp2, guN, actN, actp, dnN; int mutant;
104 uint64_t streamed; /* fp8 GEMM weight bytes every prefill call so far streamed (one pass per mt rows) */
105 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 */
106 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;
107 int stamps;
108 uint32_t n_kind, tagged_in_list, n_stamp;
109 uint8_t kind[OMEGA_GPU_CMDLIST_MAX_LAUNCHES];
110 uint64_t stamp[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1];
111};
112
113static size_t al(size_t v) { return omega_graph_addr(0,v,0,0,0,0,0); }
114static uint64_t va_of(const OmegaGpuQ36Graph *g, size_t off) { return omega_graph_addr(1,g->buf.va,g->dbuf.va,g->lo.split,off,0,0); }
115static void *cpu_of(const OmegaGpuQ36Graph *g, size_t off) { return (uint8_t *)g->buf.cpu + off; } /* host regions only (off < split) */
116static size_t x_of(const OmegaGpuQ36Graph *g, uint32_t h) { return omega_graph_addr(2,g->lo.x,0,0,0,h,H*4u); }
117static size_t hidden_of(const OmegaGpuQ36Graph *g, uint32_t h) { return omega_graph_addr(2,g->lo.hidden,0,0,0,h,H*4u); }
118static size_t xq_of(const OmegaGpuQ36Graph *g, uint32_t h) { return omega_graph_addr(2,g->lo.xq,0,0,0,h,H*4u); }
119static size_t logits_of(const OmegaGpuQ36Graph *g, uint32_t h) { return omega_graph_addr(2,g->lo.logits,0,0,0,h,g->am_ctas*OMEGA_DEC_ARGMAX_SPAN*4u); }
120static size_t token_of(const OmegaGpuQ36Graph *g, uint32_t h) { return omega_graph_addr(2,g->lo.token,0,0,0,h,TOKEN_STRIDE); }
121#ifdef CUDAPP_MIGRATION_PENDING_C_GRAPHS
122static 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); }
123static void putf(uint32_t *a, unsigned off, float f) { memcpy(&a[(off - 0x380) / 4], &f, 4); }
124#endif
125static int fail(OmegaGpuQ36Graph *g, const char *what, int rc) {
126 if (g) g->failed = 1;
127 if (what) omega_gpu_session_set_error(what);
128 return rc;
129}
130
131static int q36_unpaired_moe_refused(OmegaGpuQ36Graph *g) {
132 if (!g->native) return 0;
133 for (unsigned r = 0; r < 2; ++r) {
134 const uint64_t actual[4] = {r ? OMEGA_GPU_Q36_K_DOWN : OMEGA_GPU_Q36_K_GATE_UP,
135 g->native_position[0], g->native_position[1], g->native_position[2]};
136 OmegaGpuQ36NativeState *unused = NULL;
137 if (omega_q36_native_context(g->native, actual, &unused) != 1) {
138 omega_gpu_cmdlist_reset(g->cl);
139 return fail(g, "q36 native MoE: unpaired path refused before record", OMEGA_GPU_DGRAPH_CHIP_FAIL);
140 }
141 }
142 return 0;
143}
144
145void omega_q36_gather_table(uint32_t *tab) {
146#if CUDAPP_MIGRATION_PENDING_GRAPH_RESIDUE
147 const uint32_t R = OMEGA_GPU_Q36_ROTARY, QP = 16u * 512u, KP = QP + 2u * 256u;
148 for (uint32_t n = 0; n < OMEGA_GPU_Q36_ATT_MIX; n++) {
149 uint32_t s;
150 if (n < 4096u) s = (n / 256u) * 512u + omega_dattn256_perm_slot(n % 256u, R); /* q, permuted */
151 else if (n < 4608u) s = QP + ((n - 4096u) / 256u) * 256u + omega_dattn256_perm_slot(n % 256u, R); /* k, permuted */
152 else if (n < 5120u) s = KP + (n - 4608u); /* v */
153 else s = ((n - 5120u) / 256u) * 512u + 256u + (n - 5120u) % 256u; /* gate */
154 tab[n] = s;
155 }
156#else
157 omega_graph_table(1, 16, 2, tab, OMEGA_GPU_Q36_ATT_MIX);
158#endif
159}
160
161/* lock held */
162/* GATHER table of the FP8 GEMM's x' lane layout (omega_fp8gemm_permute_x, lane bytes 16: k a multiple of 512): permuted index
163 * i = s*512 + (w*32 + L)*4 + e holds source column c = s*512 + L*16 + w*4 + e, so tab[i] = c. */
164void omega_q36_perm_table(uint32_t *tab, uint32_t k) {
165#if CUDAPP_MIGRATION_PENDING_GRAPH_RESIDUE
166 for (uint32_t s0 = 0; s0 < k; s0 += 512u)
167 for (uint32_t L = 0; L < 32u; L++)
168 for (uint32_t w = 0; w < 4u; w++)
169 for (uint32_t e = 0; e < 4u; e++) tab[s0 + (w * 32u + L) * 4u + e] = s0 + L * 16u + w * 4u + e;
170#else
171 omega_graph_table(0, 0, 0, tab, k);
172#endif
173}
174
175static void write_dense_segments(OmegaGpuQ36Graph *g, uint32_t heads);
176
177static int kern_build(Kern *k, const OmegaDecSpec *s) {
178 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog);
179 if (!prog) return OMEGA_GPU_DGRAPH_CODEGEN_FAIL;
180 int rc = OMEGA_GPU_DGRAPH_OK;
181 size_t need = 0;
182 if (omega_blackwell_codegen_dec_prog(s, prog) != 0 || (need = omega_bw_program_code_bytes(prog)) == 0) rc = OMEGA_GPU_DGRAPH_CODEGEN_FAIL;
183 else if (omega_gpu_session_alloc_code(need, &k->code) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL;
184 else {
185 OmegaBlackwellKernel kk;
186 if (omega_blackwell_kernel_encode_into(prog, k->code.cpu, need, &kk) != 0 || kk.code_size != need) rc = OMEGA_GPU_DGRAPH_CODEGEN_FAIL;
187 else if (omega_gpu_session_sync_to_gpu(&k->code) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL;
188 else { k->code_bytes = need; k->gpr = omega_bw_gpr_declared(kk.gpr_count); if (k->gpr < 64u) k->gpr = 64u; } /* as the decode graph */
189 if (rc != OMEGA_GPU_DGRAPH_OK) { omega_gpu_session_free(&k->code); memset(k, 0, sizeof *k); }
190 }
191 omega_gpu_host_free(prog);
192 return rc;
193}
194
195void omega_gpu_q36_graph_free(OmegaGpuQ36Graph *g) {
196 if (!g) return;
197 if (g->cl) omega_gpu_cmdlist_free(g->cl);
198 omega_gpu_session_lock();
199 omega_q36_native_graph_release(g->native);
200 omega_gpu_host_free(g->native);
201 for (unsigned i = 0; i < K_COUNT; i++) if (g->k[i].code.cpu) omega_gpu_session_free(&g->k[i].code);
202 if (g->buf.cpu) omega_gpu_session_free(&g->buf);
203 if (g->dbuf.cpu) omega_gpu_session_free(&g->dbuf);
204 if (g->pf.hb.cpu) omega_gpu_session_free(&g->pf.hb);
205 if (g->pf.db.cpu) omega_gpu_session_free(&g->pf.db);
206 if (g->xh.mem.cpu) omega_gpu_session_free(&g->xh.mem);
207 omega_gpu_session_unlock();
208 omega_gpu_host_free(g);
209}
210
211int omega_gpu_q36_graph_create(const OmegaGpuQ36GraphConfig *cfg, OmegaGpuQ36Graph **out) { return omega_gpu_q36_graph_create_heads(cfg, 1, out); }
212
213int omega_gpu_q36_graph_create_heads(const OmegaGpuQ36GraphConfig *cfg, uint32_t heads, OmegaGpuQ36Graph **out) {
214 if (out) *out = NULL;
215 if (!cfg || !out || !cfg->gdn || !cfg->attn || !cfg->router || !cfg->lm_head) return OMEGA_GPU_DGRAPH_BAD_ARGS;
216 const uint32_t ctas = omega_graph_config(cfg->layers, OMEGA_GPU_Q36_MAX_LAYERS, cfg->vocab,
217 OMEGA_DEC_ARGMAX_SPAN, heads, OMEGA_GPU_Q36_MAX_HEADS, cfg->attn_mask, 1);
218 if (!ctas) return OMEGA_GPU_DGRAPH_BAD_ARGS;
219 uint32_t n = 0, k = 0;
220 omega_gpu_bf16_tensor_shape(cfg->lm_head, &n, &k);
221 if (!omega_graph_shape(1, n, k, 1, cfg->vocab, H)) return OMEGA_GPU_DGRAPH_BAD_ARGS;
222 OmegaGpuQ36Graph *g = omega_gpu_host_calloc(1, sizeof *g);
223 if (!g) return OMEGA_GPU_DGRAPH_BAD_ARGS;
224 g->cfg = *cfg;
225 g->am_ctas = ctas;
226 g->heads = heads; g->gdn_h[0] = cfg->gdn; g->attn_h[0] = cfg->attn;
227 for (uint32_t h = 0; h < OMEGA_GPU_Q36_MAX_HEADS; h++) g->xq_layer[h] = -1;
228 uint64_t layer_counts[2] = {0};
229 for (uint32_t l = 0; l < cfg->layers; l++)
230 omega_graph_layer(cfg->attn_mask, l, layer_counts, &g->layer[l].attn, &g->layer[l].kind_index);
231 Layout *L = &g->lo;
232#ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS
233 const uint64_t lc[32]={1,heads,cfg->layers,g->am_ctas,H,EI,NG,0,0,OMEGA_GPU_Q36_GDN_MIX,OMEGA_GPU_Q36_AB,OMEGA_GPU_Q36_ATT_MIX,OMEGA_GPU_Q36_MIXER_OUT,omega_gpu_router_layout_bytes(cfg->router),OMEGA_GEMM_SEG_BYTES,OMEGA_DEC_ARGMAX_SPAN,OMEGA_GPU_Q36_MAX_HEADS,OMEGA_DEC_GR_PAD,COPY_ROW,TOKEN_STRIDE};
234 uint64_t ld[64],lr=0;
235 if(omega_mg_layout(&lr,lc,32,ld,64)!=OMEGA_OK||lr!=1){omega_gpu_host_free(g);return OMEGA_GPU_DGRAPH_BAD_ARGS;}
236#define FIELD(f,i) L->f=ld[i]
237 FIELD(x,0);FIELD(ab,1);FIELD(hidden,2);FIELD(logits,3);FIELD(am_v,4);FIELD(am_i,5);FIELD(token,6);FIELD(norms,7);FIELD(fnorm,8);FIELD(tabs,9);
238 FIELD(gtab,10);FIELD(idtab,11);FIELD(stage,12);FIELD(segd,13);FIELD(ptab,14);FIELD(xq,15);FIELD(mix,16);FIELD(agg,17);FIELD(att,18);FIELD(o,19);
239 FIELD(gu,20);FIELD(act,21);FIELD(dn,22);FIELD(xp,23);FIELD(mixN,24);FIELD(abN,25);FIELD(aggN,26);FIELD(attN,27);FIELD(op,28);FIELD(oN,29);
240 FIELD(rlb,30);FIELD(sgu,31);FIELD(sdn,32);FIELD(xmap,33);FIELD(ymap,34);FIELD(xp2,35);FIELD(guN,36);FIELD(actN,37);FIELD(actp,38);FIELD(dnN,39);FIELD(split,60);FIELD(bytes,61);FIELD(lb_stride,62);
241#undef FIELD
242#else
243 size_t o = 0;
244#define REG(f, bytes) do { L->f = o; o = al(o + (bytes)); } while (0)
245 REG(x, (size_t)H * 4 * heads); REG(ab, OMEGA_GPU_Q36_AB * 4); REG(hidden, (size_t)H * 4 * heads);
246 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);
247 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 */
248 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) */
249 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 */
250 L->split = o;
251 REG(xq, (size_t)H * 4 * heads); REG(mix, OMEGA_GPU_Q36_GDN_MIX * 4); REG(agg, OMEGA_GPU_Q36_ATT_MIX * 4);
252 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);
253 { /* batch regions (N rows each, R = N * NG expert rows; x' inputs of the dense GEMMs hold MAX_HEADS rows: 8 rows per pass) */
254 const size_t N = heads, R = (size_t)heads * NG, M8 = OMEGA_GPU_Q36_MAX_HEADS;
255 L->lb_stride = (uint32_t)al(omega_gpu_router_layout_bytes(cfg->router));
256 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);
257 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);
258 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);
259 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);
260 }
261#undef REG
262 L->bytes = o;
263#endif
264 int rc = OMEGA_GPU_DGRAPH_OK;
265 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; }
266 omega_gpu_session_lock();
267 const char *pbc = getenv("AIEN_OMEGA_Q36_BUF_CACHED");
268 int (*const dalloc)(size_t, OmegaGpuMem *) = (pbc && pbc[0] == '0' && pbc[1] == 0) ? omega_gpu_session_alloc : omega_gpu_session_alloc_cached;
269 if (!omega_gpu_session_open() || omega_gpu_session_alloc(L->split, &g->buf) != 0 || dalloc(L->bytes - L->split, &g->dbuf) != 0)
270 rc = OMEGA_GPU_DGRAPH_CHIP_FAIL;
271 OmegaDecSpec spec[K_COUNT] = {
272 [K_NORM] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_NORM },
273 [K_ADD] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_ADD },
274 [K_CN] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM, .groups = NG },
275 [K_SW] = { .kind = OMEGA_DEC_SWIGLU, .width = EI },
276 [K_GATHER] = { .kind = OMEGA_DEC_GATHER, .width = OMEGA_GPU_Q36_ATT_MIX, .stride = OMEGA_GPU_Q36_ATT_MIX },
277 [K_GATE] = { .kind = OMEGA_DEC_GATE },
278 [K_AM0] = { .kind = OMEGA_DEC_ARGMAX, .mode = 0 },
279 [K_AM1] = { .kind = OMEGA_DEC_ARGMAX, .mode = 1 },
280 [K_SW_M1] = { .kind = OMEGA_DEC_SWIGLU, .width = EI, .mutant = 1 },
281 [K_GATHER_M1] = { .kind = OMEGA_DEC_GATHER, .width = OMEGA_GPU_Q36_ATT_MIX, .stride = OMEGA_GPU_Q36_ATT_MIX, .mutant = 1 },
282 [K_GATE_M1] = { .kind = OMEGA_DEC_GATE, .mutant = 1 },
283 [K_CN_M3] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM, .groups = NG, .mutant = 3 },
284 [K_COPY] = { .kind = OMEGA_DEC_GATHER, .width = COPY_ROW, .stride = COPY_ROW },
285 /* the batch (v2): N-row norm and add, the x' lane permutations (GATHER through ptab), the segment-table kernel */
286 [K_NORM_R] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_NORM | OMEGA_DEC_RES_ROWS },
287 [K_ADD_R] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_ROWS },
288 [K_PERM_XQ] = { .kind = OMEGA_DEC_GATHER, .width = H, .stride = H },
289 [K_PERM_O] = { .kind = OMEGA_DEC_GATHER, .width = OMEGA_GPU_Q36_MIXER_OUT, .stride = OMEGA_GPU_Q36_MIXER_OUT },
290 [K_PERM_XO] = { .kind = OMEGA_DEC_GATHER, .width = H, .stride = H }, /* stride = lb_stride / 4, set below */
291 [K_PERM_ACT] = { .kind = OMEGA_DEC_GATHER, .width = EI, .stride = EI },
292 [K_GROUP] = { .kind = OMEGA_DEC_GROUP, .groups = NG },
293 [K_XS] = { .kind = OMEGA_DEC_XBOUND, .mode = 1 },
294 [K_XB] = { .kind = OMEGA_DEC_XBOUND },
295 [K_XB_M1] = { .kind = OMEGA_DEC_XBOUND, .mutant = 1 },
296 [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 */
297 [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 },
298 [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 */
299 };
300 spec[K_PERM_XO].stride = L->lb_stride / 4u;
301 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 */
302 for (unsigned i = 0; i < K_COUNT && rc == OMEGA_GPU_DGRAPH_OK; i++) rc = kern_build(&g->k[i], &spec[i]);
303 if (rc == OMEGA_GPU_DGRAPH_OK && !(g->owner_generation = q36_owner_next())) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL;
304 if (rc == OMEGA_GPU_DGRAPH_OK) {
305 memset(g->buf.cpu, 0, L->split);
306 memset(g->dbuf.cpu, 0, L->bytes - L->split); /* fresh; every region is written by the chip before a launch reads it */
307 uint32_t *id = cpu_of(g, L->idtab);
308 for (uint32_t i = 0; i < COPY_ROW; i++) id[i] = i;
309 float *amv = cpu_of(g, L->am_v);
310 for (uint32_t h = 0; h < heads; h++) {
311 float *lg = cpu_of(g, logits_of(g, h));
312 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 */
313 }
314 for (size_t i = 0; i < OMEGA_DEC_ARGMAX_SPAN; i++) amv[i] = NEG_PAD; /* stage 1 writes [0, ctas) only */
315 omega_q36_gather_table(cpu_of(g, L->gtab));
316 { 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); }
317 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;
318 }
319 omega_gpu_session_unlock();
320 if (rc != OMEGA_GPU_DGRAPH_OK) { omega_gpu_q36_graph_free(g); return rc; }
321 { 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; } }
322 *out = g;
323 return OMEGA_GPU_DGRAPH_OK;
324}
325
326static int shape_e(const OmegaGpuFp8Tensor *t, uint32_t e, uint32_t n, uint32_t k) {
327 uint32_t te = 0, tn = 0, tk = 0; if (t) omega_gpu_fp8_tensor_shape(t, &te, &tn, &tk);
328 return t && omega_graph_shape(te, tn, tk, e, n, k);
329}
330static int shape_is(const OmegaGpuFp8Tensor *t, uint32_t n, uint32_t k) { return shape_e(t, 1, n, k); }
331#define N_EXPERTS 257u /* routed experts + the shared one (the router's directory) */
332
333int omega_gpu_q36_graph_set_head_state(OmegaGpuQ36Graph *g, uint32_t head, OmegaGpuGdn *gdn, OmegaGpuDecodeAttn256Cache *attn) {
334 if (!g || head >= g->heads || !gdn || !attn || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
335 g->gdn_h[head] = gdn; g->attn_h[head] = attn;
336 return OMEGA_GPU_DGRAPH_OK;
337}
338
339int omega_gpu_q36_graph_set_layer(OmegaGpuQ36Graph *g, uint32_t layer, const OmegaGpuFp8Tensor *mix, const OmegaGpuBf16Tensor *ab,
340 const OmegaGpuFp8Tensor *o, const OmegaGpuFp8Tensor *gate_up, const OmegaGpuFp8Tensor *down,
341 const float *in_norm) {
342 if (!g || layer >= g->cfg.layers || !in_norm || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
343 const int attn = g->layer[layer].attn;
344 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) ||
345 !shape_e(gate_up, N_EXPERTS, 2 * EI, H) || !shape_e(down, N_EXPERTS, H, EI)) return OMEGA_GPU_DGRAPH_BAD_ARGS;
346 if (attn ? ab != NULL : ab == NULL) return OMEGA_GPU_DGRAPH_BAD_ARGS;
347 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; }
348 uint64_t t[8] = { 0 }; /* mix, o; the experts' group tables come from the router */
349 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;
350 omega_gpu_session_lock();
351 g->tensor_pair_generation[layer] = 0;
352 g->layer[layer].set = 0;
353 memcpy((uint8_t *)cpu_of(g, g->lo.tabs) + (size_t)layer * 64u, t, sizeof t);
354 memcpy((float *)cpu_of(g, g->lo.norms) + (size_t)layer * H, in_norm, H * 4u);
355 int rc = omega_gpu_session_sync_to_gpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL;
356 if (rc == OMEGA_GPU_DGRAPH_OK && !(g->tensor_pair_generation[layer] = q36_owner_next())) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL;
357 if (rc == OMEGA_GPU_DGRAPH_OK) {
358 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;
359 g->layer[layer].set = 1;
360 }
361 omega_gpu_session_unlock();
362 return rc;
363}
364
365int omega_gpu_q36_graph_set_final_norm(OmegaGpuQ36Graph *g, const float *final_norm) {
366 if (!g || !final_norm || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
367 omega_gpu_session_lock();
368 memcpy(cpu_of(g, g->lo.fnorm), final_norm, H * 4u);
369 int rc = omega_gpu_session_sync_to_gpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL;
370 omega_gpu_session_unlock();
371 if (rc == OMEGA_GPU_DGRAPH_OK) g->final_set = 1;
372 return rc;
373}
374
375int omega_gpu_q36_graph_set_lm_head_fp8(OmegaGpuQ36Graph *g, const OmegaGpuFp8Tensor *head) {
376 if (!g || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
377 if (!head) { g->head8 = NULL; return OMEGA_GPU_DGRAPH_OK; }
378 uint64_t t[2];
379 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;
380 omega_gpu_session_lock();
381 memcpy((uint8_t *)cpu_of(g, g->lo.tabs) + (size_t)g->cfg.layers * 64u, t, sizeof t);
382 int rc = omega_gpu_session_sync_to_gpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL;
383 omega_gpu_session_unlock();
384 if (rc == OMEGA_GPU_DGRAPH_OK) g->head8 = head;
385 return rc;
386}
387
388int omega_gpu_q36_graph_set_exact_head(OmegaGpuQ36Graph *g, const OmegaGpuFp8Tensor *screen, const float *coef) {
389 if (!g || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
390 if (!screen) {
391 omega_gpu_session_lock();
392 if (g->xh.mem.cpu) omega_gpu_session_free(&g->xh.mem);
393 omega_gpu_session_unlock();
394 memset(&g->xh, 0, sizeof g->xh);
395 return OMEGA_GPU_DGRAPH_OK;
396 }
397 uint64_t t[2];
398 if (!coef || g->xh.screen || !shape_is(screen, g->cfg.vocab, H) || omega_gpu_fp8_tensor_entry(screen, 0, &t[0], &t[1]) != 0)
399 return OMEGA_GPU_DGRAPH_BAD_ARGS;
400 const size_t n = g->cfg.vocab, P = (size_t)g->am_ctas * OMEGA_DEC_ARGMAX_SPAN;
401 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),
402 o_tau = al(o_lo + P * 4), o_tab = al(o_tau + 16), total = al(o_tab + 16);
403 if (omega_gpu_bf16_prepare_mask(H) != 0) return fail(g, "q36 graph: exact head MASK kernel", OMEGA_GPU_DGRAPH_CHIP_FAIL);
404 omega_gpu_session_lock();
405 int rc = omega_gpu_session_alloc(total, &g->xh.mem) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL;
406 if (rc == OMEGA_GPU_DGRAPH_OK) {
407 uint8_t *B = g->xh.mem.cpu;
408 memset(B, 0, total);
409 memcpy(B + o_c, coef, n * 4);
410 for (size_t i = n; i < P; i++) ((float *)(B + o_lo))[i] = NEG_PAD; /* XBOUND writes [0, vocab) only */
411 memcpy(B + o_tab, t, sizeof t);
412 if (omega_gpu_session_sync_to_gpu(&g->xh.mem) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL;
413 }
414 if (rc == OMEGA_GPU_DGRAPH_OK) {
415 const uint64_t V = g->xh.mem.va;
416 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;
417 g->xh.screen = screen; memset(g->xh.on, 1, sizeof g->xh.on);
418 } else if (g->xh.mem.cpu) omega_gpu_session_free(&g->xh.mem);
419 omega_gpu_session_unlock();
420 return rc;
421}
422
423void 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); }
424void omega_gpu_q36_graph_set_head_exact_head_on(OmegaGpuQ36Graph *g, uint32_t head, int on) {
425 if (g && !g->in_token && head < OMEGA_GPU_Q36_MAX_HEADS) g->xh.on[head] = on ? 1 : 0;
426}
427int omega_gpu_q36_graph_exact_head_active_head(const OmegaGpuQ36Graph *g, uint32_t head) {
428 return g && head < OMEGA_GPU_Q36_MAX_HEADS && g->xh.screen && g->xh.on[head] && !g->head8;
429}
430int omega_gpu_q36_graph_exact_head_active(const OmegaGpuQ36Graph *g) { return omega_gpu_q36_graph_exact_head_active_head(g, 0); }
431
432void omega_gpu_q36_graph_set_flush_each(OmegaGpuQ36Graph *g, int on) { if (g) g->flush_each = on ? 1 : 0; }
433void omega_gpu_q36_graph_test_set_mutant(OmegaGpuQ36Graph *g, int mutant) {
434 if (!g) return;
435 g->mutant = mutant;
436 omega_gpu_bf16_test_set_mask_mutant(mutant == 5 ? 4 : 0); /* process-wide, the MASK form only */
437}
438
439/* ---- the list ---- */
440
441static OmegaGpuCmdListOpts list_opts(const OmegaGpuQ36Graph *g) {
442 OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o);
443 o.mode = OMEGA_GPU_CMDLIST_PCAS; o.barrier = (OmegaGpuCmdListBarrier)omega_gpu_decode_graph_barrier(); o.timeout_ms = TIMEOUT_MS; o.stamps = g->stamps;
444 return o;
445}
446
447static int q36_moe_observe(const OmegaGpuCmdList *, const OmegaGpuCmdListConsumed *, void *);
448static int submit_now(OmegaGpuQ36Graph *g) {
449 if (omega_gpu_cmdlist_count(g->cl) == 0) return OMEGA_GPU_DGRAPH_OK;
450 OmegaGpuCmdListOpts o = list_opts(g);
451 OmegaGpuCmdListStats st; memset(&st, 0, sizeof st);
452 int rc = g->moe
453 ? omega_gpu_cmdlist_submit_observed(g->cl, &o, &st, q36_moe_observe, g)
454 : omega_gpu_cmdlist_submit(g->cl, &o, &st);
455 g->acc.launches += st.launches; g->acc.submits += st.submits; g->acc.chip_ns += st.elapsed_ns; g->acc.sems_done += st.sems_done;
456 if (rc == 0 && g->stamps) {
457 uint64_t tmp[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1];
458 int cnt = omega_gpu_cmdlist_stamps(g->cl, tmp, OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1);
459 if (cnt < 0) rc = -1;
460 else { int from = 0;
461 if (!omega_graph_timeline(g->n_stamp, (uint32_t)cnt, OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1u, &from)) {
462 omega_gpu_session_set_error("q36 graph: timeline full"); rc = -1;
463 } else for (int i = from; i < cnt; i++) g->stamp[g->n_stamp++] = tmp[i];
464 }
465 }
466 g->tagged_in_list = 0;
467 omega_gpu_cmdlist_reset(g->cl);
468 if (rc != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL);
469 if (st.sems_done != st.launches) {
470 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);
471 return fail(g, e, OMEGA_GPU_DGRAPH_CHIP_FAIL);
472 }
473 return OMEGA_GPU_DGRAPH_OK;
474}
475
476static void tag(OmegaGpuQ36Graph *g, uint8_t kind) {
477 const uint32_t n = omega_gpu_cmdlist_count(g->cl);
478 for (; g->tagged_in_list < n && g->n_kind < OMEGA_GPU_CMDLIST_MAX_LAUNCHES; g->tagged_in_list++) g->kind[g->n_kind++] = kind;
479}
480
481static int after_op(OmegaGpuQ36Graph *g, uint8_t kind, int api_rc) {
482 if (api_rc != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL);
483 tag(g, kind);
484 return g->flush_each ? submit_now(g) : OMEGA_GPU_DGRAPH_OK;
485}
486
487static 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) {
488 const Kern *k = &g->k[ki];
489 OmegaGpuLaunch L; memset(&L, 0, sizeof L);
490 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;
491 L.num_elements = elems; L.args = args; L.n_args = n_args; L.timeout_ms = TIMEOUT_MS;
492 return after_op(g, kind, omega_gpu_cmdlist_add(g->cl, &L) < 0 ? -1 : 0);
493}
494
495#ifdef CUDAPP_MIGRATION_PENDING_C_GRAPHS
496/* RES at width 2048: NORM (x -> y), ADD (x += a) or COMBINE|NORM (x += sum_g wts[g] * a[g]; y = norm(x)) */
497static 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) {
498 uint32_t w[OMEGA_DEC_RES_WORDS]; memset(w, 0, sizeof w);
499 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);
500 put64(w, OMEGA_DEC_RES_Y, y); putf(w, OMEGA_DEC_RES_EPS, g->cfg.eps); putf(w, OMEGA_DEC_RES_SCALE, scale);
501 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);
502}
503static int rec_res(OmegaGpuQ36Graph *g, int ki, uint64_t x, uint64_t a, uint64_t nw, uint64_t y, float scale) {
504 return rec_res_w(g, ki, x, a, 0, nw, y, scale);
505}
506#endif
507
508/* The a|b GEMV beside the mix GEMV (a speed lever of the 27B, values identical): OFF by default here until the plain graph
509 * has passed its gates (PRIORITY CHANGE 2026-10-20); env AIEN_OMEGA_Q36_OVERLAP_AB=1 turns it on. */
510static int g_overlap_ab = -1;
511static int q36_overlap_ab(void) {
512 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; }
513 return g_overlap_ab;
514}
515
516static int native_record_fp8(OmegaGpuQ36Graph *g, uint8_t kind, const OmegaGpuFp8Tensor *t, uint32_t groups,
517 uint64_t tab, uint64_t x, uint32_t xrows, uint64_t y) {
518 const OmegaGpuNativeArtifact *a = NULL; OmegaGpuNativeLease lease = {0};
519 const uint64_t actual[4] = {kind, g->native_position[0], g->native_position[1], g->native_position[2]};
520 OmegaGpuQ36NativeState *context = NULL;
521 int selected = omega_q36_native_context(g->native, actual, &context);
522 /* The canonical joint-MoE contract is the paired prefill operation, not a
523 * standalone GEMV. Never record a selected expert through this bypass. */
524 if (selected == 0 && (kind == OMEGA_GPU_Q36_K_GATE_UP || kind == OMEGA_GPU_Q36_K_DOWN)) return -1;
525 if (selected == 0) selected = omega_q36_native_begin(context, omega_gpu_fp8_native_cache(), &a, &lease);
526 if (selected < 0) return -1;
527 if (selected == 1) return omega_gpu_fp8_gemv_grouped_record(g->cl, t, groups, tab, x, xrows, y);
528 int rc = omega_gpu_fp8_record_native(g->cl, t, a, &lease, groups, tab, x, xrows, y);
529 omega_q36_native_end(&lease);
530 return omega_q36_native_record_status(rc);
531}
532static int rec_fp8(OmegaGpuQ36Graph *g, uint8_t kind, const OmegaGpuFp8Tensor *t, uint64_t tab, uint64_t x, uint64_t y) {
533 return after_op(g, kind, native_record_fp8(g, kind, t, 1, tab, x, 1, y));
534}
535
536int omega_gpu_q36_graph_begin_token(OmegaGpuQ36Graph *g, const float *x_embed) { return omega_gpu_q36_graph_begin_stroke(g, x_embed, 1); }
537
538int omega_gpu_q36_graph_begin_stroke(OmegaGpuQ36Graph *g, const float *x_embed, uint32_t heads) {
539 if (!g || g->in_token || heads == 0 || heads > g->heads) return OMEGA_GPU_DGRAPH_BAD_ARGS;
540 for (uint32_t h = 1; h < heads; h++) if (!g->gdn_h[h] || !g->attn_h[h]) return OMEGA_GPU_DGRAPH_BAD_ARGS;
541 omega_gpu_session_lock();
542 g->t_begin = omega_gpu_session_now_ns();
543 g->in_token = 1; g->failed = 0; g->stroke_heads = heads;
544 for (uint32_t h = 0; h < heads; h++) { g->xq_layer[h] = -1; g->hidden_ready[h] = 0; }
545 memset(&g->acc, 0, sizeof g->acc);
546 g->n_kind = 0; g->tagged_in_list = 0; g->n_stamp = 0;
547 omega_gpu_cmdlist_reset(g->cl);
548 if (!omega_gpu_session_open()) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL);
549 for (uint32_t h = 0; h < heads; h++) {
550 uint32_t *tok = cpu_of(g, token_of(g, h));
551 tok[0] = TOKEN_POISON; tok[1] = TOKEN_POISON;
552 if (x_embed) memcpy(cpu_of(g, x_of(g, h)), x_embed + (size_t)h * H, H * 4u);
553 }
554 if (g->batch) write_dense_segments(g, heads);
555 if (omega_gpu_session_sync_to_gpu(&g->buf) != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL);
556 return OMEGA_GPU_DGRAPH_OK;
557}
558
559int 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); }
560
561int omega_gpu_q36_graph_record_head_layer(OmegaGpuQ36Graph *g, uint32_t h, uint32_t l, uint32_t pos) {
562 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
563 if (g->failed) return OMEGA_GPU_DGRAPH_CHIP_FAIL;
564 if (h >= g->stroke_heads) return fail(g, "q36 graph: head not in the stroke", OMEGA_GPU_DGRAPH_BAD_ARGS);
565 if (l >= g->cfg.layers || !g->layer[l].set || g->hidden_ready[h] || (l + 1 == g->cfg.layers && !g->final_set))
566 return fail(g, "q36 graph: layer not set, out of order, or final norm missing", OMEGA_GPU_DGRAPH_BAD_ARGS);
567 g->native_position[0] = l; g->native_position[1] = h; g->native_position[2] = 1;
568 if (q36_unpaired_moe_refused(g)) return OMEGA_GPU_DGRAPH_CHIP_FAIL;
569 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);
570 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);
571 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;
572 const uint32_t ki = g->layer[l].kind_index;
573#ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS
574 const uint64_t cfg[16] = {1, g->xq_layer[h] != (int)l, g->layer[l].attn,
575 q36_overlap_ab(), 0, 0, l, g->cfg.layers, pos, g->mutant, g->head8 != NULL,
576 g->flush_each, H, EI, NG, g->am_ctas};
577 uint64_t values[64] = {x, xq, mix, agg, att, o, gu, act, dn, in_norm,
578 va_of(g, g->lo.fnorm), tab, va_of(g, g->lo.ab), va_of(g, g->lo.gtab),
579 va_of(g, hidden_of(g, h)), att};
580 values[23] = OMEGA_GPU_Q36_GDN_QKV * 4u;
581 values[24] = OMEGA_GPU_Q36_ATT_QKV * 4u;
582 values[25] = OMEGA_GPU_Q36_ATT_MIX; values[26] = OMEGA_GPU_Q36_MIXER_OUT;
583 values[27] = omega_graph_fbits(g->cfg.eps); values[28] = omega_graph_fbits(SCALE_2P64);
584 values[29] = omega_graph_fbits(1.0f);
585 static const int kernels[24] = {K_NORM,K_ADD,K_CN,K_SW,K_GATHER,K_GATE,
586 0,0,0,0,0,0,0,0,0,0,0,0,0,0,K_SW_M1,K_GATHER_M1,K_GATE_M1,K_CN_M3};
587 OmegaGpuRouterDevOut router = {0};
588 int rc = OMEGA_GPU_DGRAPH_OK;
589 for (uint64_t phase=0; rc==OMEGA_GPU_DGRAPH_OK; phase++) {
590 uint64_t node[64];
591 if (!omega_graph_next(cfg,values,node,phase)) return fail(g,NULL,OMEGA_GPU_DGRAPH_CHIP_FAIL);
592 if (node[0]==255) break;
593 uint32_t words[32]; omega_graph_args(node,words);
594 switch (node[0]) {
595 case 0: break;
596 case 1: case 5: case 7: case 9: case 12: case 14: {
597 static const uint8_t kinds[15] = {[1]=OMEGA_GPU_Q36_K_NORM,[5]=OMEGA_GPU_Q36_K_GATHER,
598 [7]=OMEGA_GPU_Q36_K_GATE,[9]=OMEGA_GPU_Q36_K_RES,[12]=OMEGA_GPU_Q36_K_SWIGLU,
599 [14]=OMEGA_GPU_Q36_K_RES};
600 rc=add_glue(g,kinds[node[0]],kernels[node[1]],node[2],node[3],node[4],words,node[5]);
601 break;
602 }
603 case 2: {
604 int mrc=native_record_fp8(g,OMEGA_GPU_Q36_K_MIX,g->layer[l].mix,node[7],node[10],node[11],node[8],node[12]);
605 if (!mrc && node[6] && omega_gpu_cmdlist_mark_independent(g->cl)) mrc=-1;
606 rc=after_op(g,OMEGA_GPU_Q36_K_MIX,mrc); break;
607 }
608 case 3: rc=after_op(g,OMEGA_GPU_Q36_K_AB,omega_gpu_bf16_gemv_record(g->cl,g->layer[l].ab,node[11],node[12])); break;
609 case 4: rc=after_op(g,OMEGA_GPU_Q36_K_GDN,omega_gpu_gdn_record(g->cl,g->gdn_h[h],ki,node[11],node[12],node[13],
610 g->cfg.eps_l2,g->cfg.eps,1.0f,SCALE_2P64,&values[15])); break;
611 case 6: rc=after_op(g,OMEGA_GPU_Q36_K_ATTN,omega_gpu_decode_attn256_record(g->cl,g->attn_h[h],ki,node[13],node[11],
612 g->cfg.eps,SCALE_2P64,node[12])); break;
613 case 8: rc=rec_fp8(g,OMEGA_GPU_Q36_K_O,g->layer[l].o,node[10],node[11],node[12]); break;
614 case 10:
615 rc=after_op(g,OMEGA_GPU_Q36_K_ROUTER,omega_gpu_router_record(g->cl,g->cfg.router,l,node[11],g->cfg.eps,1,SCALE_2P64,&router));
616 values[16]=router.tab_gu_va; values[17]=router.tab_dn_va; values[18]=router.xout_va; values[19]=router.wts_va;
617 break;
618 case 11: rc=after_op(g,OMEGA_GPU_Q36_K_GATE_UP,native_record_fp8(g,OMEGA_GPU_Q36_K_GATE_UP,g->layer[l].gu,
619 node[7],node[10],node[11],node[8],node[12])); break;
620 case 13: rc=after_op(g,OMEGA_GPU_Q36_K_DOWN,native_record_fp8(g,OMEGA_GPU_Q36_K_DOWN,g->layer[l].dn,
621 node[7],node[10],node[11],node[8],node[12])); break;
622 default: return fail(g,NULL,OMEGA_GPU_DGRAPH_CHIP_FAIL);
623 }
624 }
625 if (rc==OMEGA_GPU_DGRAPH_OK) { if (l+1==g->cfg.layers) g->hidden_ready[h]=1; else g->xq_layer[h]=(int)l+1; }
626#else
627 int rc = OMEGA_GPU_DGRAPH_OK;
628 if (g->xq_layer[h] != (int)l) rc = rec_res(g, K_NORM, x, 0, in_norm, xq, SCALE_2P64);
629 /* GDN layers: the a|b GEMV (latency-bound, ~18 us) is recorded first and the mix GEMV marked independent of it: both only
630 * read xq and write their own buffers, so the two grids overlap; the GDN step after them waits for both (normal barrier).
631 * AIEN_OMEGA_Q36_OVERLAP_AB=0 or set_overlap_ab(0): the old order, mix then a|b, each behind a barrier. */
632 const int overlap = !g->layer[l].attn && q36_overlap_ab();
633 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)));
634 if (rc == OMEGA_GPU_DGRAPH_OK) {
635 int mrc = native_record_fp8(g, OMEGA_GPU_Q36_K_MIX, g->layer[l].mix, 1, tab, xq, 1, mix);
636 if (mrc == 0 && overlap && !g->flush_each && omega_gpu_cmdlist_mark_independent(g->cl) != 0) mrc = -1;
637 rc = after_op(g, OMEGA_GPU_Q36_K_MIX, mrc);
638 }
639 uint64_t mixer_out = att;
640 if (!g->layer[l].attn) {
641 const uint64_t ab = va_of(g, g->lo.ab);
642 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));
643 if (rc == OMEGA_GPU_DGRAPH_OK)
644 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,
645 g->cfg.eps_l2, g->cfg.eps, 1.0f, SCALE_2P64, &mixer_out));
646 } else {
647 uint32_t w[OMEGA_DEC_GA_WORDS]; memset(w, 0, sizeof w);
648 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);
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, 1,
651 OMEGA_GPU_Q36_ATT_MIX, w, OMEGA_DEC_GA_WORDS);
652 if (rc == OMEGA_GPU_DGRAPH_OK)
653 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));
654 uint32_t w2[OMEGA_DEC_GT_WORDS]; memset(w2, 0, sizeof w2);
655 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);
656 w2[(OMEGA_DEC_GT_N - 0x380) / 4] = OMEGA_GPU_Q36_MIXER_OUT;
657 if (rc == OMEGA_GPU_DGRAPH_OK)
658 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,
659 OMEGA_GPU_Q36_MIXER_OUT, w2, OMEGA_DEC_GT_WORDS);
660 }
661 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_fp8(g, OMEGA_GPU_Q36_K_O, g->layer[l].o, tab + 16u, mixer_out, o);
662 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_res_w(g, K_ADD, x, o, 0, 0, 0, 1.0f);
663 /* the sparse block: router (post-attention norm, top-8 + the shared slot), 9 expert groups, COMBINE|NORM */
664 OmegaGpuRouterDevOut r; memset(&r, 0, sizeof r);
665 if (rc == OMEGA_GPU_DGRAPH_OK)
666 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));
667 if (rc == OMEGA_GPU_DGRAPH_OK)
668 rc = after_op(g, OMEGA_GPU_Q36_K_GATE_UP, native_record_fp8(g, OMEGA_GPU_Q36_K_GATE_UP, g->layer[l].gu, NG, r.tab_gu_va, r.xout_va, 1, gu));
669 uint32_t w3[OMEGA_DEC_SW_WORDS]; memset(w3, 0, sizeof w3);
670 put64(w3, OMEGA_DEC_SW_GU, gu); put64(w3, OMEGA_DEC_SW_ACT, act); putf(w3, OMEGA_DEC_SW_SCALE, SCALE_2P64);
671 if (rc == OMEGA_GPU_DGRAPH_OK)
672 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);
673 if (rc == OMEGA_GPU_DGRAPH_OK)
674 rc = after_op(g, OMEGA_GPU_Q36_K_DOWN, native_record_fp8(g, OMEGA_GPU_Q36_K_DOWN, g->layer[l].dn, NG, r.tab_dn_va, act, NG, dn));
675 if (rc == OMEGA_GPU_DGRAPH_OK) {
676 const int last = l + 1 == g->cfg.layers;
677 const uint64_t nw = last ? va_of(g, g->lo.fnorm) : in_norm + H * 4u;
678 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,
679 last && !g->head8 ? 1.0f : SCALE_2P64);
680 if (rc == OMEGA_GPU_DGRAPH_OK) { if (last) g->hidden_ready[h] = 1; else g->xq_layer[h] = (int)l + 1; }
681 }
682#endif
683 return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc);
684}
685
686/* ---- the batch (v2, L312): every conversation of a layer in one set of launches, each weight read once per layer ---- */
687typedef struct { uint64_t w_va, s_va; uint32_t row0, cnt, pad[2]; } Seg32;
688_Static_assert(sizeof(Seg32) == OMEGA_GEMM_SEG_BYTES, "segment entry layout");
689
690int omega_gpu_q36_graph_set_batch(OmegaGpuQ36Graph *g, int on) {
691 if (!g || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
692 if (!on) { g->batch = 0; return OMEGA_GPU_DGRAPH_OK; }
693 /* pin the four GEMM shapes the batch records (a kernel cache miss while recording is refused): the dense trunk at 8 rows
694 * per pass without maps, the expert blocks at 4 rows per pass with the row maps */
695 const uint32_t R = g->heads * NG;
696 int rc = omega_gpu_fp8gemm_prepare_shape(H, 1, 0, 0);
697 if (rc == 0) rc = omega_gpu_fp8gemm_prepare_shape(OMEGA_GPU_Q36_MIXER_OUT, 1, 0, 0);
698 if (rc == 0) rc = omega_gpu_fp8gemm_prepare_shape(H, R, 0, 1);
699 if (rc == 0) rc = omega_gpu_fp8gemm_prepare_shape(EI, R, 0, 1);
700 if (rc != 0) return OMEGA_GPU_DGRAPH_CHIP_FAIL;
701 /* lever 2: the router once per layer for every head (record_layer_heads; the ROWS kernel has its own cache slot) */
702 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;
703 /* lever 1: the bf16 lm_head read once per group of up to OMEGA_BF16_MAX_XBATCH heads (record_final_heads) */
704 if (!g->head8) {
705 uint32_t n = 0, k = 0; omega_gpu_bf16_tensor_shape(g->cfg.lm_head, &n, &k);
706 for (uint32_t xb = 2; xb <= OMEGA_BF16_MAX_XBATCH && xb <= g->heads; xb++)
707 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;
708 }
709 g->batch = 1;
710 return OMEGA_GPU_DGRAPH_OK;
711}
712int omega_gpu_q36_graph_batch(const OmegaGpuQ36Graph *g) { return g ? g->batch : 0; }
713
714/* 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) */
715static void write_dense_segments(OmegaGpuQ36Graph *g, uint32_t heads) {
716 Seg32 *s = cpu_of(g, g->lo.segd);
717 for (uint32_t l = 0; l < g->cfg.layers; l++) {
718 memset(&s[l * SEGD_ROWS], 0, SEGD_ROWS * sizeof *s);
719 if (!g->layer[l].set) continue;
720 (void)omega_gpu_fp8_tensor_entry(g->layer[l].mix, 0, &s[l * SEGD_ROWS].w_va, &s[l * SEGD_ROWS].s_va);
721 (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);
722 s[l * SEGD_ROWS].cnt = heads; s[l * SEGD_ROWS + 1].cnt = heads;
723 }
724}
725
726static int native_record_gemm(OmegaGpuQ36Graph *g, uint8_t kind, const OmegaGpuFp8Tensor *t, uint32_t rows, uint32_t segs, uint64_t seg, uint64_t xp,
727 uint64_t y, uint64_t xm, uint64_t ym, uint32_t incumbent_mt) {
728 const OmegaGpuNativeArtifact *a = NULL; OmegaGpuNativeLease lease = {0};
729 const uint64_t actual[4] = {kind, g->native_position[0], g->native_position[1], g->native_position[2]};
730 OmegaGpuQ36NativeState *context = NULL;
731 int selected = omega_q36_native_context(g->native, actual, &context);
732 if (selected == 0 && (kind == OMEGA_GPU_Q36_K_GATE_UP || kind == OMEGA_GPU_Q36_K_DOWN))
733 return fail(g, "native MoE requires the complete paired observed operation", OMEGA_GPU_DGRAPH_CHIP_FAIL);
734 if (selected == 0) selected = omega_q36_native_begin(context, omega_gpu_fp8gemm_native_cache(), &a, &lease);
735 if (selected < 0) return fail(g, "native GEMM artifact/qualification/session refused", OMEGA_GPU_DGRAPH_CHIP_FAIL);
736 if (selected == 1) return after_op(g, kind, omega_gpu_fp8gemm_record(g->cl, t, rows, segs, seg, xp, y, xm, ym, incumbent_mt, 0, NULL));
737 int rc = omega_gpu_fp8gemm_record_native(g->cl, t, a, &lease, rows, segs, seg, xp, y, xm, ym);
738 omega_q36_native_end(&lease);
739 return after_op(g, kind, omega_q36_native_record_status(rc));
740}
741#ifdef CUDAPP_MIGRATION_PENDING_C_GRAPHS
742/* rows of width floats permuted through tab into out (the GEMM's x' lane layout); the input row stride is the kernel's */
743static int rec_perm(OmegaGpuQ36Graph *g, int ki, uint64_t in, uint64_t tab, uint64_t out, uint32_t width, uint32_t rows) {
744 uint32_t w[OMEGA_DEC_GA_WORDS]; memset(w, 0, sizeof w);
745 put64(w, OMEGA_DEC_GA_IN, in); put64(w, OMEGA_DEC_GA_TAB, tab); put64(w, OMEGA_DEC_GA_OUT, out);
746 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);
747}
748#endif
749/* COMBINE|NORM of every head in one launch (L356): head h's residual x + h * 2048, down outputs a + h * 9 * 2048, weights
750 * w + h * lb_stride, output y + h * 2048 (the xq or hidden slots). Each row runs the one-head program. ON unless
751 * AIEN_OMEGA_Q36_CN_ROWS=0 (the per-head launches). */
752static int g_cn_rows = -1;
753static int q36_cn_rows(void) {
754 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; }
755 return g_cn_rows;
756}
757#ifdef CUDAPP_MIGRATION_PENDING_C_GRAPHS
758static 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) {
759 uint32_t w[OMEGA_DEC_RES_WORDS]; memset(w, 0, sizeof w);
760 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);
761 put64(w, OMEGA_DEC_RES_Y, y); putf(w, OMEGA_DEC_RES_EPS, g->cfg.eps); putf(w, OMEGA_DEC_RES_SCALE, scale);
762 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);
763}
764/* RES NORM|ROWS or ADD|ROWS over rows of 2048 (x, a, y move by the width per row) */
765static 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) {
766 uint32_t w[OMEGA_DEC_RES_WORDS]; memset(w, 0, sizeof w);
767 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);
768 putf(w, OMEGA_DEC_RES_EPS, g->cfg.eps); putf(w, OMEGA_DEC_RES_SCALE, scale);
769 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);
770}
771#endif
772
773int omega_gpu_q36_graph_record_layer_heads(OmegaGpuQ36Graph *g, uint32_t l, const uint32_t *pos) {
774 if (!g || !g->in_token || !pos) return OMEGA_GPU_DGRAPH_BAD_ARGS;
775 if (g->failed) return OMEGA_GPU_DGRAPH_CHIP_FAIL;
776 if (!g->batch) return fail(g, "q36 graph: record_layer_heads needs set_batch", OMEGA_GPU_DGRAPH_BAD_ARGS);
777 if (l >= g->cfg.layers || !g->layer[l].set || (l + 1 == g->cfg.layers && !g->final_set))
778 return fail(g, "q36 graph: layer not set, out of order, or final norm missing", OMEGA_GPU_DGRAPH_BAD_ARGS);
779 const uint32_t N = g->stroke_heads, R = N * NG, ki = g->layer[l].kind_index;
780 g->native_position[0] = l; g->native_position[1] = 0; g->native_position[2] = N;
781 if (q36_unpaired_moe_refused(g)) return OMEGA_GPU_DGRAPH_CHIP_FAIL;
782 for (uint32_t h = 0; h < N; h++)
783 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);
784 const Layout *L = &g->lo;
785 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);
786 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);
787 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);
788 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);
789 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;
790 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;
791 const uint64_t XS = (uint64_t)H * 4u, MOS = (uint64_t)OMEGA_GPU_Q36_MIXER_OUT * 4u;
792#ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS
793 const uint64_t cfg[16]={1,g->xq_layer[0]!=(int)l,g->layer[l].attn,0,0,0,l,g->cfg.layers,
794 0,g->mutant,g->head8!=NULL,g->flush_each,H,EI,NG,g->am_ctas};
795 uint64_t values[64]={x0,xq0,0,0,0,0,0,0,0,in_norm,va_of(g,L->fnorm),0,0,va_of(g,L->gtab),va_of(g,hidden_of(g,0))};
796 values[23]=OMEGA_GPU_Q36_GDN_QKV*4u; values[24]=OMEGA_GPU_Q36_ATT_QKV*4u;
797 values[25]=OMEGA_GPU_Q36_ATT_MIX; values[26]=MOS/4u;
798 values[27]=omega_graph_fbits(g->cfg.eps);values[28]=omega_graph_fbits(SCALE_2P64);values[29]=omega_graph_fbits(1.0f);
799 values[30]=N;values[31]=L->lb_stride;values[32]=xp;values[33]=mixN;values[34]=abN;
800 values[35]=aggN;values[36]=attN;values[37]=op;values[38]=oN;values[39]=rlb;
801 values[40]=sgu;values[41]=sdn;values[42]=xmap;values[43]=ymap;values[44]=xp2;
802 values[45]=guN;values[46]=actN;values[47]=actp;values[48]=dnN;
803 values[49]=pt2048;values[50]=pt4096;values[51]=pt512;values[52]=segd;
804 values[58]=q36_cn_rows();values[59]=OMEGA_GPU_Q36_GDN_MIX;values[60]=OMEGA_GPU_Q36_AB;values[61]=R;
805 static const int kernels[26]={K_NORM,K_ADD,K_CN,K_SW,K_GATHER,K_GATE,K_NORM_R,K_ADD_R,
806 K_CN_R,K_PERM_XQ,K_PERM_O,K_PERM_XO,K_PERM_ACT,K_GROUP,0,0,0,0,0,0,
807 K_SW_M1,K_GATHER_M1,K_GATE_M1,K_CN_M3,0,K_CN_R_M3};
808 OmegaGpuRouterDevOut router={0};
809 int rc=OMEGA_GPU_DGRAPH_OK;
810 for(uint64_t phase=0;rc==OMEGA_GPU_DGRAPH_OK;phase++) {
811 uint64_t node[64],result=0;
812 if(omega_mg_batch(&result,cfg,16,values,64,node,64,phase)!=OMEGA_OK||result!=1)
813 return fail(g,NULL,OMEGA_GPU_DGRAPH_CHIP_FAIL);
814 if(node[0]==255)break;
815 uint32_t words[32];omega_graph_args(node,words);
816 switch(node[0]){
817 case 0:break;
818 case 20:case 21:case 25:case 26:case 28:case 31:case 33:case 34:case 36:case 37:case 39:case 40:case 41:{
819 static const uint8_t kinds[42]={[20]=OMEGA_GPU_Q36_K_NORM,[21]=OMEGA_GPU_Q36_K_PERM,
820 [25]=OMEGA_GPU_Q36_K_PERM,[26]=OMEGA_GPU_Q36_K_GATHER,[28]=OMEGA_GPU_Q36_K_GATE,
821 [31]=OMEGA_GPU_Q36_K_RES,[33]=OMEGA_GPU_Q36_K_GROUP,[34]=OMEGA_GPU_Q36_K_PERM,
822 [36]=OMEGA_GPU_Q36_K_SWIGLU,[37]=OMEGA_GPU_Q36_K_PERM,[39]=OMEGA_GPU_Q36_K_RES,
823 [40]=OMEGA_GPU_Q36_K_RES,[41]=OMEGA_GPU_Q36_K_PERM};
824 rc=add_glue(g,kinds[node[0]],kernels[node[1]],node[2],node[3],node[4],words,node[5]);break;
825 }
826 case 22:rc=after_op(g,OMEGA_GPU_Q36_K_AB,omega_gpu_bf16_gemv_rows_record(g->cl,g->layer[l].ab,node[7],node[11],node[12]));break;
827 case 23:rc=native_record_gemm(g,OMEGA_GPU_Q36_K_MIX,g->layer[l].mix,node[7],node[8],node[10],node[11],node[12],0,0,0);break;
828 case 24:rc=after_op(g,OMEGA_GPU_Q36_K_GDN,omega_gpu_gdn_record(g->cl,g->gdn_h[node[20]],ki,node[11],node[12],node[13],
829 g->cfg.eps_l2,g->cfg.eps,1.0f,SCALE_2P64,&values[15]));break;
830 case 27:rc=after_op(g,OMEGA_GPU_Q36_K_ATTN,omega_gpu_decode_attn256_record(g->cl,g->attn_h[node[20]],ki,pos[node[20]],
831 node[11],g->cfg.eps,SCALE_2P64,node[12]));break;
832 case 30:rc=native_record_gemm(g,OMEGA_GPU_Q36_K_O,g->layer[l].o,node[7],node[8],node[10],node[11],node[12],0,0,0);break;
833 case 32:
834 rc=after_op(g,OMEGA_GPU_Q36_K_ROUTER,omega_gpu_router_record_rows(g->cl,g->cfg.router,l,node[11],XS,g->cfg.eps,1,SCALE_2P64,
835 node[12],L->lb_stride,node[7],&router));
836 values[53]=router.ids_va;values[54]=router.tab_gu_va;values[55]=router.tab_dn_va;
837 values[56]=router.wts_va;values[57]=router.xout_va;break;
838 case 35:rc=native_record_gemm(g,OMEGA_GPU_Q36_K_GATE_UP,g->layer[l].gu,node[7],node[8],node[10],node[11],node[12],node[13],node[14],0);break;
839 case 38:rc=native_record_gemm(g,OMEGA_GPU_Q36_K_DOWN,g->layer[l].dn,node[7],node[8],node[10],node[11],node[12],node[13],node[14],0);break;
840 default:return fail(g,NULL,OMEGA_GPU_DGRAPH_CHIP_FAIL);
841 }
842 }
843 for(uint32_t h=0;h<N&&rc==OMEGA_GPU_DGRAPH_OK;h++){if(l+1==g->cfg.layers)g->hidden_ready[h]=1;else g->xq_layer[h]=(int)l+1;}
844#else
845 int rc = OMEGA_GPU_DGRAPH_OK;
846 /* input norm of every head (layer 0; after that the previous layer's COMBINE|NORM left layer l's x' in the xq slots) */
847 if (g->xq_layer[0] != (int)l) rc = rec_res_rows(g, K_NORM_R, x0, 0, in_norm, xq0, SCALE_2P64, N);
848 /* x' of the dense trunk: the N xq rows in the GEMM's lane layout */
849 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_XQ, xq0, pt2048, xp, H, N);
850 if (!g->layer[l].attn) {
851 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));
852 if (rc == OMEGA_GPU_DGRAPH_OK) rc = native_record_gemm(g, OMEGA_GPU_Q36_K_MIX, g->layer[l].mix, N, 1, segd, xp, mixN, 0, 0, 0);
853 for (uint32_t h = 0; h < N && rc == OMEGA_GPU_DGRAPH_OK; h++) {
854 const uint64_t mix_h = mixN + (uint64_t)h * OMEGA_GPU_Q36_GDN_MIX * 4u;
855 uint64_t mo = 0;
856 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,
857 abN + (uint64_t)h * OMEGA_GPU_Q36_AB * 4u, g->cfg.eps_l2, g->cfg.eps, 1.0f, SCALE_2P64, &mo));
858 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_O, mo, pt4096, op + h * MOS, OMEGA_GPU_Q36_MIXER_OUT, 1);
859 }
860 } else {
861 if (rc == OMEGA_GPU_DGRAPH_OK) rc = native_record_gemm(g, OMEGA_GPU_Q36_K_MIX, g->layer[l].mix, N, 1, segd, xp, mixN, 0, 0, 0); /* n 9216: the rows are 9216 apart */
862 uint32_t w[OMEGA_DEC_GA_WORDS]; memset(w, 0, sizeof w);
863 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);
864 if (rc == OMEGA_GPU_DGRAPH_OK)
865 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,
866 (size_t)OMEGA_GPU_Q36_ATT_MIX * N, w, OMEGA_DEC_GA_WORDS);
867 for (uint32_t h = 0; h < N && rc == OMEGA_GPU_DGRAPH_OK; h++) {
868 const uint64_t agg_h = aggN + (uint64_t)h * OMEGA_GPU_Q36_ATT_MIX * 4u, att_h = attN + h * MOS;
869 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));
870 uint32_t w2[OMEGA_DEC_GT_WORDS]; memset(w2, 0, sizeof w2);
871 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);
872 w2[(OMEGA_DEC_GT_N - 0x380) / 4] = OMEGA_GPU_Q36_MIXER_OUT;
873 if (rc == OMEGA_GPU_DGRAPH_OK)
874 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,
875 OMEGA_GPU_Q36_MIXER_OUT, w2, OMEGA_DEC_GT_WORDS);
876 }
877 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_O, attN, pt4096, op, OMEGA_GPU_Q36_MIXER_OUT, N);
878 }
879 if (rc == OMEGA_GPU_DGRAPH_OK) rc = native_record_gemm(g, OMEGA_GPU_Q36_K_O, g->layer[l].o, N, 1, segd + OMEGA_GEMM_SEG_BYTES, op, oN, 0, 0, 0);
880 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_res_rows(g, K_ADD_R, x0, oN, 0, 0, 1.0f, N);
881 /* 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),
882 * GROUP sorts their picks into one segment table, two grouped GEMMs */
883 OmegaGpuRouterDevOut r0; memset(&r0, 0, sizeof r0);
884 if (rc == OMEGA_GPU_DGRAPH_OK)
885 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,
886 rlb, L->lb_stride, N, &r0));
887 uint32_t wg[OMEGA_DEC_GR_WORDS]; memset(wg, 0, sizeof wg);
888 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);
889 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);
890 wg[(OMEGA_DEC_GR_STRIDE - 0x380) / 4] = L->lb_stride; wg[(OMEGA_DEC_GR_HEADS - 0x380) / 4] = N;
891 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);
892 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_XO, r0.xout_va, pt2048, xp2, H, N);
893 if (rc == OMEGA_GPU_DGRAPH_OK) rc = native_record_gemm(g, OMEGA_GPU_Q36_K_GATE_UP, g->layer[l].gu, R, R, sgu, xp2, guN, xmap, ymap, 0);
894 uint32_t w3[OMEGA_DEC_SW_WORDS]; memset(w3, 0, sizeof w3);
895 put64(w3, OMEGA_DEC_SW_GU, guN); put64(w3, OMEGA_DEC_SW_ACT, actN); putf(w3, OMEGA_DEC_SW_SCALE, SCALE_2P64);
896 if (rc == OMEGA_GPU_DGRAPH_OK)
897 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);
898 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_ACT, actN, pt512, actp, EI, R);
899 if (rc == OMEGA_GPU_DGRAPH_OK) rc = native_record_gemm(g, OMEGA_GPU_Q36_K_DOWN, g->layer[l].dn, R, R, sdn, actp, dnN, ymap, ymap, 0);
900 const int last = l + 1 == g->cfg.layers;
901 const uint64_t nw = last ? va_of(g, L->fnorm) : in_norm + H * 4u;
902 if (q36_cn_rows()) {
903 if (rc == OMEGA_GPU_DGRAPH_OK)
904 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);
905 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; }
906 } else for (uint32_t h = 0; h < N && rc == OMEGA_GPU_DGRAPH_OK; h++) {
907 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,
908 last ? va_of(g, hidden_of(g, h)) : xq0 + h * XS, last && !g->head8 ? 1.0f : SCALE_2P64);
909 if (rc == OMEGA_GPU_DGRAPH_OK) { if (last) g->hidden_ready[h] = 1; else g->xq_layer[h] = (int)l + 1; }
910 }
911#endif
912 return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc);
913}
914
915#ifdef CUDAPP_MIGRATION_PENDING_C_GRAPHS
916/* the two-stage argmax of one head's logits slot into its token slot (token: index, token + 4: value) */
917static int rec_argmax(OmegaGpuQ36Graph *g, uint64_t logits, uint64_t token) {
918 uint32_t a0[OMEGA_DEC_AM_WORDS], a1[OMEGA_DEC_AM_WORDS]; memset(a0, 0, sizeof a0); memset(a1, 0, sizeof a1);
919 const uint64_t amv = va_of(g, g->lo.am_v), ami = va_of(g, g->lo.am_i);
920 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);
921 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);
922 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);
923 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);
924 return rc;
925}
926#endif
927
928int omega_gpu_q36_graph_record_final(OmegaGpuQ36Graph *g) { return omega_gpu_q36_graph_record_head_final(g, 0); }
929
930int omega_gpu_q36_graph_record_head_final(OmegaGpuQ36Graph *g, uint32_t h) {
931 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
932 if (g->failed) return OMEGA_GPU_DGRAPH_CHIP_FAIL;
933 if (h >= g->stroke_heads) return fail(g, "q36 graph: head not in the stroke", OMEGA_GPU_DGRAPH_BAD_ARGS);
934 if (!g->final_set) return fail(g, "q36 graph: final norm not set", OMEGA_GPU_DGRAPH_BAD_ARGS);
935 g->native_position[0] = g->cfg.layers; g->native_position[1] = h; g->native_position[2] = 1;
936 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));
937 int rc = OMEGA_GPU_DGRAPH_OK;
938#ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS
939 const uint64_t cfg[16]={g->hidden_ready[h],g->head8!=NULL,omega_gpu_q36_graph_exact_head_active_head(g,h),
940 g->mutant,g->cfg.vocab,g->am_ctas,H,omega_graph_fbits(g->cfg.eps),omega_graph_fbits(SCALE_2P64),omega_graph_fbits(1.0f)};
941 const uint64_t values[16]={x,hidden,logits,token,va_of(g,g->lo.fnorm),
942 va_of(g,g->lo.tabs),va_of(g,g->lo.am_v),va_of(g,g->lo.am_i),g->xh.h8,g->xh.s,g->xh.c,g->xh.hi,g->xh.lo,g->xh.tau,g->xh.tab};
943 uint64_t input[16];memcpy(input,values,sizeof input);
944 input[5]+= (uint64_t)g->cfg.layers*64u;
945 static const int kernels[7]={K_NORM,0,K_XS,K_XB,K_XB_M1,K_AM0,K_AM1};
946 for(uint64_t phase=0;rc==OMEGA_GPU_DGRAPH_OK;phase++){
947 uint64_t node[64],result=0;
948 if(omega_mg_final(&result,cfg,16,input,16,node,64,phase)!=OMEGA_OK||result!=1)return fail(g,NULL,OMEGA_GPU_DGRAPH_CHIP_FAIL);
949 if(node[0]==255)break;
950 uint32_t words[32];omega_graph_args(node,words);
951 switch(node[0]){
952 case 0:break;
953 case 1:case 3:case 8:
954 rc=add_glue(g,node[0]==8?OMEGA_GPU_Q36_K_ARGMAX:node[1]==0?OMEGA_GPU_Q36_K_NORM:OMEGA_GPU_Q36_K_LM_HEAD,
955 kernels[node[1]],node[2],node[3],node[4],words,node[5]);break;
956 case 2:rc=rec_fp8(g,OMEGA_GPU_Q36_K_LM_HEAD,g->xh.screen,node[10],node[11],node[12]);break;
957 case 6:rc=after_op(g,OMEGA_GPU_Q36_K_LM_HEAD,omega_gpu_bf16_gemv_mask_record(g->cl,g->cfg.lm_head,node[11],node[12],node[13],node[14]));break;
958 case 7:rc=g->head8?rec_fp8(g,OMEGA_GPU_Q36_K_LM_HEAD,g->head8,node[10],node[11],node[12]):
959 after_op(g,OMEGA_GPU_Q36_K_LM_HEAD,omega_gpu_bf16_gemv_record(g->cl,g->cfg.lm_head,node[11],node[12]));break;
960 default:return fail(g,NULL,OMEGA_GPU_DGRAPH_CHIP_FAIL);
961 }
962 }
963#else
964 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);
965 const uint64_t amv = va_of(g, g->lo.am_v), ami = va_of(g, g->lo.am_i);
966 if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_q36_graph_exact_head_active_head(g, h)) {
967 /* HUM L314 two-pass head: h8 = hidden * 2^64 (exact); s = screen . h8; lo / hi = s -/+ c|hidden|; tau = max lo;
968 * logits = lm_head . hidden on the rows with hi >= tau (today's bits), -inf elsewhere. The argmax below is then
969 * today's token: the full argmax row has hi >= its logit >= tau (omega_blackwell_codegen_dec.h XBOUND). */
970 uint32_t xs[OMEGA_DEC_XB_WORDS], xb[OMEGA_DEC_XB_WORDS], t0[OMEGA_DEC_AM_WORDS], t1[OMEGA_DEC_AM_WORDS];
971 memset(xs, 0, sizeof xs); memset(xb, 0, sizeof xb); memset(t0, 0, sizeof t0); memset(t1, 0, sizeof t1);
972 put64(xs, OMEGA_DEC_XB_S, hidden); put64(xs, OMEGA_DEC_XB_LO, g->xh.h8); xs[(OMEGA_DEC_XB_N - 0x380) / 4] = H;
973 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);
974 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;
975 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);
976 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);
977 rc = add_glue(g, OMEGA_GPU_Q36_K_LM_HEAD, K_XS, H / OMEGA_DEC_THREADS, 1, H, xs, OMEGA_DEC_XB_WORDS);
978 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);
979 if (rc == OMEGA_GPU_DGRAPH_OK)
980 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,
981 g->cfg.vocab, xb, OMEGA_DEC_XB_WORDS);
982 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);
983 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);
984 if (rc == OMEGA_GPU_DGRAPH_OK)
985 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));
986 } else if (rc == OMEGA_GPU_DGRAPH_OK)
987 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)
988 : after_op(g, OMEGA_GPU_Q36_K_LM_HEAD, omega_gpu_bf16_gemv_record(g->cl, g->cfg.lm_head, hidden, logits));
989 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_argmax(g, logits, token);
990#endif
991 return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc);
992}
993
994/* The batch's final head (v2 lever 1, L312): every head's hidden is ready after the last layer (record_layer_heads), so
995 * the bf16 lm_head runs once per group of up to OMEGA_BF16_MAX_XBATCH heads (the XBATCH form: x rows hidden_of(h0..),
996 * y rows logits_of(h0..), ystride the logits slot; each row bit-identical to its own one-head launch), then the argmax
997 * per head. The FP8 head keeps the per-head path (stroke() calls record_head_final). */
998static int record_final_heads(OmegaGpuQ36Graph *g) {
999 const uint32_t N = g->stroke_heads;
1000 if (!g->final_set) return fail(g, "q36 graph: final norm not set", OMEGA_GPU_DGRAPH_BAD_ARGS);
1001 for (uint32_t h = 0; h < N; h++)
1002 if (!g->hidden_ready[h]) return fail(g, "q36 graph: batch final before the last layer", OMEGA_GPU_DGRAPH_BAD_ARGS);
1003 int rc = OMEGA_GPU_DGRAPH_OK;
1004#ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS
1005 const uint64_t cfg[16]={N,OMEGA_BF16_MAX_XBATCH,g->am_ctas,H};
1006 const uint64_t values[16]={va_of(g,g->lo.hidden),va_of(g,g->lo.logits),va_of(g,g->lo.token),
1007 va_of(g,g->lo.am_v),va_of(g,g->lo.am_i)};
1008 for(uint64_t phase=0;rc==OMEGA_GPU_DGRAPH_OK;phase++){
1009 uint64_t node[64],result=0;uint32_t words[32];
1010 if(omega_mg_finb(&result,cfg,16,values,16,node,64,phase)!=OMEGA_OK||result!=1)return fail(g,NULL,OMEGA_GPU_DGRAPH_CHIP_FAIL);
1011 if(node[0]==255)break;
1012 omega_graph_args(node,words);
1013 if(node[0]==7)rc=after_op(g,OMEGA_GPU_Q36_K_LM_HEAD,
1014 omega_gpu_bf16_gemv_xbatch_record(g->cl,g->cfg.lm_head,node[7],node[11],node[12],node[13]));
1015 else rc=add_glue(g,OMEGA_GPU_Q36_K_ARGMAX,node[1]==5?K_AM0:K_AM1,node[2],node[3],node[4],words,node[5]);
1016 }
1017#else
1018 for (uint32_t h0 = 0; h0 < N && rc == OMEGA_GPU_DGRAPH_OK; h0 += OMEGA_BF16_MAX_XBATCH) {
1019 const uint32_t xb = N - h0 < OMEGA_BF16_MAX_XBATCH ? N - h0 : OMEGA_BF16_MAX_XBATCH;
1020 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)),
1021 va_of(g, logits_of(g, h0)), g->am_ctas * OMEGA_DEC_ARGMAX_SPAN));
1022 }
1023 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)));
1024#endif
1025 return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc);
1026}
1027
1028int omega_gpu_q36_graph_end_token(OmegaGpuQ36Graph *g, uint32_t *token, OmegaGpuDecodeGraphStats *st) { return omega_gpu_q36_graph_end_stroke(g, token, st); }
1029
1030int omega_gpu_q36_graph_end_stroke(OmegaGpuQ36Graph *g, uint32_t *tokens, OmegaGpuDecodeGraphStats *st) {
1031 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
1032 int rc = g->failed ? OMEGA_GPU_DGRAPH_CHIP_FAIL : OMEGA_GPU_DGRAPH_OK;
1033 const uint64_t t_rec = omega_gpu_session_now_ns();
1034 if (rc == OMEGA_GPU_DGRAPH_OK) rc = submit_now(g);
1035 for (uint32_t h = 0; h < g->stroke_heads && rc == OMEGA_GPU_DGRAPH_OK; h++)
1036 if (omega_gpu_decode_attn256_counters_check(g->attn_h[h]) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL);
1037 if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_session_sync_to_cpu(&g->buf) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL);
1038 for (uint32_t h = 0; h < g->stroke_heads && rc == OMEGA_GPU_DGRAPH_OK; h++) {
1039 const uint32_t t = ((const uint32_t *)cpu_of(g, token_of(g, h)))[0];
1040 if (t == TOKEN_POISON || t >= g->cfg.vocab) {
1041 char e[96]; omega_gpu_fmt(e, sizeof e, "q36 graph: head %u token output %u not written or out of range", h, t);
1042 rc = fail(g, e, OMEGA_GPU_DGRAPH_CHIP_FAIL);
1043 } else if (tokens) tokens[h] = t;
1044 }
1045 omega_gpu_cmdlist_reset(g->cl);
1046 g->in_token = 0;
1047 const uint64_t t_end = omega_gpu_session_now_ns();
1048 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; }
1049 omega_gpu_session_unlock();
1050 return rc;
1051}
1052
1053int omega_gpu_q36_graph_token(OmegaGpuQ36Graph *g, const float *x_embed, uint32_t pos, uint32_t *token, OmegaGpuDecodeGraphStats *st) {
1054 if (!g) return OMEGA_GPU_DGRAPH_BAD_ARGS;
1055 if (omega_gpu_q36_graph_begin_token(g, x_embed) == OMEGA_GPU_DGRAPH_BAD_ARGS) return OMEGA_GPU_DGRAPH_BAD_ARGS;
1056 for (uint32_t l = 0; l < g->cfg.layers && !g->failed; l++) (void)omega_gpu_q36_graph_record_layer(g, l, pos);
1057 if (!g->failed) (void)omega_gpu_q36_graph_record_final(g);
1058 return omega_gpu_q36_graph_end_token(g, token, st);
1059}
1060
1061int omega_gpu_q36_graph_stroke(OmegaGpuQ36Graph *g, const float *x_embed, const uint32_t *pos, uint32_t heads, uint32_t *tokens,
1062 OmegaGpuDecodeGraphStats *st) {
1063 if (!g || !pos) return OMEGA_GPU_DGRAPH_BAD_ARGS;
1064 if (omega_gpu_q36_graph_begin_stroke(g, x_embed, heads) == OMEGA_GPU_DGRAPH_BAD_ARGS) return OMEGA_GPU_DGRAPH_BAD_ARGS;
1065 /* layer-major: every head's layer l before layer l + 1, so one layer's expert rows are warm across the heads; the batch
1066 * (set_batch) records the layer for every head in one set of launches, each weight read once */
1067 for (uint32_t l = 0; l < g->cfg.layers && !g->failed; l++) {
1068 if (g->batch) (void)omega_gpu_q36_graph_record_layer_heads(g, l, pos);
1069 else for (uint32_t h = 0; h < heads && !g->failed; h++) (void)omega_gpu_q36_graph_record_head_layer(g, h, l, pos[h]);
1070 }
1071 if (g->batch && !g->head8) { if (!g->failed) (void)record_final_heads(g); }
1072 else for (uint32_t h = 0; h < heads && !g->failed; h++) (void)omega_gpu_q36_graph_record_head_final(g, h);
1073 return omega_gpu_q36_graph_end_stroke(g, tokens, st);
1074}
1075
1076int 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); }
1077
1078int omega_gpu_q36_graph_read_head(OmegaGpuQ36Graph *g, uint32_t h, float *x, float *hidden, float *logits) {
1079 if (!g || g->in_token || h >= g->heads) return OMEGA_GPU_DGRAPH_BAD_ARGS;
1080 omega_gpu_session_lock();
1081 int rc = omega_gpu_session_sync_to_cpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL;
1082 if (rc == OMEGA_GPU_DGRAPH_OK) {
1083 if (x) memcpy(x, cpu_of(g, x_of(g, h)), H * 4u);
1084 if (hidden) memcpy(hidden, cpu_of(g, hidden_of(g, h)), H * 4u);
1085 if (hidden && g->head8) {
1086#ifdef CUDAPP_MIGRATION_PENDING_C_GRAPH_HEAD_SCALE
1087 for (unsigned i = 0; i < H; i++) hidden[i] *= 0x1p-64f;
1088#else
1089 uint64_t result = 0;
1090 if (omega_ghscale(&result, hidden, H) != OMEGA_OK || result)
1091 rc = OMEGA_GPU_DGRAPH_CHIP_FAIL;
1092#endif
1093 }
1094 if (logits) memcpy(logits, cpu_of(g, logits_of(g, h)), (size_t)g->cfg.vocab * 4u);
1095 }
1096 omega_gpu_session_unlock();
1097 return rc;
1098}
1099
1100int omega_gpu_q36_graph_read_buf(OmegaGpuQ36Graph *g, int which, float *out, uint32_t n) {
1101 if (!g || g->in_token || !out || which < 0 || which >= OMEGA_GPU_Q36_BUF_COUNT) return OMEGA_GPU_DGRAPH_BAD_ARGS;
1102 const struct { size_t off; uint32_t cap; } b[OMEGA_GPU_Q36_BUF_COUNT] = {
1103 [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 },
1104 [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 },
1105 [OMEGA_GPU_Q36_BUF_ATT] = { g->lo.att, OMEGA_GPU_Q36_MIXER_OUT }, [OMEGA_GPU_Q36_BUF_O] = { g->lo.o, H },
1106 [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 },
1107 [OMEGA_GPU_Q36_BUF_DN] = { g->lo.dn, NG * H },
1108 };
1109 if (n > b[which].cap) return OMEGA_GPU_DGRAPH_BAD_ARGS;
1110 omega_gpu_session_lock();
1111 int rc = OMEGA_GPU_DGRAPH_OK;
1112 size_t from = b[which].off;
1113 if (from >= g->lo.split && n) {
1114 /* a chip-only region: K_COPY moves ceil(n / 256) rows of 256 floats into stage (every cap is a multiple of 256) */
1115 uint32_t w[32]; memset(w, 0, sizeof w);
1116#ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS
1117 const uint64_t c[16]={2,0,0,0,0,0,COPY_ROW,n},v[16]={va_of(g,from),va_of(g,g->lo.idtab),va_of(g,g->lo.stage)};
1118 uint64_t node[64],result=0;
1119 if(omega_mg_aux(&result,c,16,v,16,node,64)!=OMEGA_OK||result!=1){omega_gpu_session_unlock();return OMEGA_GPU_DGRAPH_CHIP_FAIL;}
1120 omega_graph_args(node,w);
1121#else
1122 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));
1123#endif
1124 const Kern *k = &g->k[K_COPY];
1125 OmegaGpuLaunch L; memset(&L, 0, sizeof L);
1126 L.code_va = k->code.va; L.gpr_count = k->gpr; L.threads_x = OMEGA_DEC_THREADS; L.threads_y = 1;
1127#ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS
1128 L.grid_x=node[2];L.grid_y=node[3];L.num_elements=node[4];L.n_args=node[5];
1129#else
1130 L.grid_x = 1; L.grid_y = (n + COPY_ROW - 1) / COPY_ROW;L.num_elements = (size_t)L.grid_y * COPY_ROW;L.n_args = OMEGA_DEC_GA_WORDS;
1131#endif
1132 L.args = w; L.timeout_ms = TIMEOUT_MS;
1133 if (!omega_gpu_session_open() || omega_gpu_session_launch(&L, NULL, NULL) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL;
1134 from = g->lo.stage;
1135 }
1136 if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_session_sync_to_cpu(&g->buf) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL;
1137 if (rc == OMEGA_GPU_DGRAPH_OK) memcpy(out, cpu_of(g, from), (size_t)n * 4u);
1138 omega_gpu_session_unlock();
1139 return rc;
1140}
1141
1142/* ---- timeline ---- */
1143void omega_gpu_q36_graph_set_stamps(OmegaGpuQ36Graph *g, int on) { if (g && !g->in_token) g->stamps = on ? 1 : 0; }
1144
1145int omega_gpu_q36_graph_stamps(const OmegaGpuQ36Graph *g, uint64_t *stamps, uint8_t *kinds, uint32_t cap) {
1146 if (!g || g->in_token || !stamps || !kinds) return OMEGA_GPU_DGRAPH_BAD_ARGS;
1147 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; }
1148 if (cap < g->n_stamp) { omega_gpu_session_set_error("q36 graph: timeline buffers too small"); return OMEGA_GPU_DGRAPH_BAD_ARGS; }
1149 memcpy(stamps, g->stamp, (size_t)g->n_stamp * sizeof stamps[0]);
1150 memcpy(kinds, g->kind, (size_t)g->n_kind);
1151 return (int)g->n_kind;
1152}
1153
1154const char *omega_gpu_q36_graph_kind_name(uint8_t kind) {
1155 static const char *const names[OMEGA_GPU_Q36_K_COUNT] = { "norm", "mix", "ab", "gdn", "gather", "attn", "gate", "o_proj", "res", "router",
1156 "gate_up", "swiglu", "down", "lm_head", "argmax", "perm", "group" };
1157 return kind < OMEGA_GPU_Q36_K_COUNT ? names[kind] : "?";
1158}
1159
1160/* ---- the prefill (L369): one conversation's prompt chunk through the chip as rows ----
1161 * Every arithmetic step of a prompt token is the decode step's: the dense trunk and the experts through the fp8 gemm record
1162 * 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
1163 * and the attention launched once per token, in token order, on the conversation's own state at pos0 + t. The expert rows are
1164 * grouped on the host between two lists per layer (the GROUP kernel holds 128 rows; a chunk holds up to 128 x 8). */
1165#define PF_PAD 8u
1166#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) */
1167static int pf_fail(OmegaGpuQ36Graph *g, const char *what) { return fail(g, what, OMEGA_GPU_DGRAPH_BAD_ARGS); }
1168
1169int omega_gpu_q36_graph_prefill_alloc(OmegaGpuQ36Graph *g, uint32_t cap) {
1170 if (!g || g->in_token || cap == 0 || cap > OMEGA_GPU_Q36_PREFILL_MAX_ROWS) return OMEGA_GPU_DGRAPH_BAD_ARGS;
1171 if (g->pf.cap >= cap) return OMEGA_GPU_DGRAPH_OK;
1172 size_t ho = 0, dof = 0;
1173#ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS
1174 const uint64_t lc[32]={3,g->heads,g->cfg.layers,g->am_ctas,H,EI,NG,0,0,OMEGA_GPU_Q36_GDN_MIX,OMEGA_GPU_Q36_AB,OMEGA_GPU_Q36_ATT_MIX,OMEGA_GPU_Q36_MIXER_OUT,g->lo.lb_stride,OMEGA_GEMM_SEG_BYTES,OMEGA_DEC_ARGMAX_SPAN,OMEGA_GPU_Q36_MAX_HEADS,PF_PAD,COPY_ROW,TOKEN_STRIDE,cap};
1175 uint64_t ld[64],lr=0;
1176 if(omega_mg_layout(&lr,lc,32,ld,64)!=OMEGA_OK||lr!=1)return OMEGA_GPU_DGRAPH_BAD_ARGS;
1177#define FIELD(f,i) g->pf.f=ld[i]
1178 FIELD(x,0);FIELD(rlb,1);FIELD(sgu,2);FIELD(sdn,3);FIELD(sgu2,4);FIELD(sdn2,5);FIELD(xmap,6);FIELD(ymap,7);FIELD(xmap2,8);FIELD(ymap2,9);
1179 FIELD(xq,10);FIELD(xp,11);FIELD(mixN,12);FIELD(abN,13);FIELD(aggN,14);FIELD(attN,15);FIELD(op,16);FIELD(oN,17);FIELD(xp2,18);FIELD(guN,19);FIELD(actN,20);FIELD(actp,21);FIELD(dnN,22);
1180#undef FIELD
1181 ho=ld[61];dof=ld[63];
1182#else
1183 const size_t P = cap, R = P * NG, lbs = g->lo.lb_stride;
1184#define HREG(f, b) do { g->pf.f = ho; ho += al(b); } while (0)
1185#define DREG(f, b) do { g->pf.f = dof; dof += al(b); } while (0)
1186 HREG(x, P * H * 4); HREG(rlb, P * lbs); HREG(sgu, 256u * OMEGA_GEMM_SEG_BYTES); HREG(sdn, 256u * OMEGA_GEMM_SEG_BYTES);
1187 HREG(sgu2, OMEGA_GEMM_SEG_BYTES); HREG(sdn2, OMEGA_GEMM_SEG_BYTES); HREG(xmap, (R + PF_PAD) * 4); HREG(ymap, (R + PF_PAD) * 4);
1188 HREG(xmap2, (P + PF_PAD) * 4); HREG(ymap2, (P + PF_PAD) * 4);
1189 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);
1190 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);
1191 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);
1192 DREG(dnN, R * H * 4);
1193#undef HREG
1194#undef DREG
1195#endif
1196 omega_gpu_session_lock();
1197 if (g->pf.hb.cpu) omega_gpu_session_free(&g->pf.hb);
1198 if (g->pf.db.cpu) omega_gpu_session_free(&g->pf.db);
1199 g->pf.cap = 0;
1200 const char *pbc = getenv("AIEN_OMEGA_Q36_BUF_CACHED");
1201 int (*const dalloc)(size_t, OmegaGpuMem *) = (pbc && pbc[0] == '0' && pbc[1] == 0) ? omega_gpu_session_alloc : omega_gpu_session_alloc_cached;
1202 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
1203 : OMEGA_GPU_DGRAPH_OK;
1204 if (rc == OMEGA_GPU_DGRAPH_OK) memset(g->pf.hb.cpu, 0, ho);
1205 if (rc == OMEGA_GPU_DGRAPH_OK && !(g->pf_generation = q36_owner_next())) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL;
1206 omega_gpu_session_unlock();
1207 /* pin the shapes the prefill records (a kernel cache miss while recording is refused): the dense trunk without maps (8 rows
1208 * per pass), the expert blocks with the row maps at 4 rows per pass, the router's ROWS kernel */
1209 if (rc == OMEGA_GPU_DGRAPH_OK) {
1210 int r = omega_gpu_fp8gemm_prepare_shape(H, 1, 0, 0);
1211 if (r == 0) r = omega_gpu_fp8gemm_prepare_shape(OMEGA_GPU_Q36_MIXER_OUT, 1, 0, 0);
1212 if (r == 0) r = omega_gpu_fp8gemm_prepare_shape(H, 64, 4, 1);
1213 if (r == 0) r = omega_gpu_fp8gemm_prepare_shape(EI, 64, 4, 1);
1214 if (r == 0 && omega_gpu_router_prepare_rows(g->cfg.router, H * 4u, g->lo.lb_stride) != OMEGA_GPU_ROUTER_OK) r = -1;
1215 if (r != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL;
1216 }
1217 if (rc != OMEGA_GPU_DGRAPH_OK) return rc;
1218 const char *m = getenv("AIEN_OMEGA_Q36_PF_MUTANT");
1219 g->pf.mutant = m ? atoi(m) : 0;
1220 const char *gm = getenv("AIEN_OMEGA_Q36_PF_GDN");
1221 g->pf.gdn_multi = !(gm && gm[0] == '0' && gm[1] == 0);
1222 for (uint32_t h = 0; h < g->heads && g->pf.gdn_multi; h++)
1223 if (g->gdn_h[h] && omega_gpu_gdn_multi_prepare(g->gdn_h[h]) != OMEGA_GPU_GDN_OK) return OMEGA_GPU_DGRAPH_CHIP_FAIL;
1224 const char *am = getenv("AIEN_OMEGA_Q36_PF_ATTN");
1225 g->pf.attn_multi = !(am && am[0] == '0' && am[1] == 0);
1226 for (uint32_t h = 0; h < g->heads && g->pf.attn_multi; h++)
1227 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;
1228 omega_gpu_decode_attn256_test_set_multi_mutant(g->pf.mutant == 6 ? 1 : 0); /* TEST mutant 6: the attention prep launch dropped */
1229 g->pf.cap = cap;
1230 return OMEGA_GPU_DGRAPH_OK;
1231}
1232uint32_t omega_gpu_q36_graph_prefill_cap(const OmegaGpuQ36Graph *g) { return g ? g->pf.cap : 0; }
1233uint64_t omega_gpu_q36_graph_prefill_streamed(const OmegaGpuQ36Graph *g) { return g ? g->pf.streamed : 0; }
1234
1235static uint64_t pf_h(const OmegaGpuQ36Graph *g, size_t off) { return g->pf.hb.va + off; }
1236static void *pf_hc(const OmegaGpuQ36Graph *g, size_t off) { return (uint8_t *)g->pf.hb.cpu + off; }
1237static uint64_t pf_d(const OmegaGpuQ36Graph *g, size_t off) { return g->pf.db.va + off; }
1238
1239/* part (a) of layer l: the mixer of every row and the router; ends with the router outputs in pf.rlb */
1240static 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,
1241 OmegaGpuRouterDevOut *first_router) {
1242 const uint32_t ki = g->layer[l].kind_index;
1243 g->native_position[0] = l; g->native_position[1] = 0; g->native_position[2] = n;
1244 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);
1245 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);
1246 const uint64_t pt2048 = va_of(g, g->lo.ptab), pt4096 = pt2048 + (uint64_t)H * 4u;
1247 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;
1248 const uint64_t XS = (uint64_t)H * 4u, MOS = (uint64_t)OMEGA_GPU_Q36_MIXER_OUT * 4u;
1249 int rc = OMEGA_GPU_DGRAPH_OK;
1250#ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS
1251 const uint64_t cfg[16]={3,l==0,g->layer[l].attn,g->pf.gdn_multi,g->pf.attn_multi,g->pf.mutant,l,g->cfg.layers,
1252 pos0,g->mutant,g->head8!=NULL,g->flush_each,H,EI,NG,g->am_ctas};
1253 uint64_t values[64]={x0,xq0,0,0,0,0,0,0,0,in_norm,va_of(g,g->lo.fnorm),0,0,va_of(g,g->lo.gtab)};
1254 values[23]=OMEGA_GPU_Q36_GDN_QKV*4u;values[24]=OMEGA_GPU_Q36_ATT_QKV*4u;
1255 values[25]=OMEGA_GPU_Q36_ATT_MIX;values[26]=MOS/4u;
1256 values[27]=omega_graph_fbits(g->cfg.eps);values[28]=omega_graph_fbits(SCALE_2P64);values[29]=omega_graph_fbits(1.0f);
1257 values[30]=n;values[31]=g->lo.lb_stride;values[32]=xp;values[33]=mixN;values[34]=abN;
1258 values[35]=aggN;values[36]=attN;values[37]=op;values[38]=oN;values[39]=rlb;
1259 values[49]=pt2048;values[50]=pt4096;values[52]=segd;
1260 values[59]=OMEGA_GPU_Q36_GDN_MIX;values[60]=OMEGA_GPU_Q36_AB;values[62]=OMEGA_GPU_ROUTER_MAX_ROWS;
1261 static const int kernels[27]={K_NORM,K_ADD,K_CN,K_SW,K_GATHER,K_GATE,K_NORM_R,K_ADD_R,K_CN_R,
1262 K_PERM_XQ,K_PERM_O,K_PERM_XO,K_PERM_ACT,K_GROUP,0,0,0,0,0,0,K_SW_M1,K_GATHER_M1,K_GATE_M1,K_CN_M3,0,K_CN_R_M3,K_GATE_R};
1263 for(uint64_t phase=0;rc==OMEGA_GPU_DGRAPH_OK;phase++){
1264 uint64_t node[64],result=0;
1265 if(omega_mg_batch(&result,cfg,16,values,64,node,64,phase)!=OMEGA_OK||result!=1)return fail(g,NULL,OMEGA_GPU_DGRAPH_CHIP_FAIL);
1266 if(node[0]==255)break;
1267 uint32_t words[32];omega_graph_args(node,words);
1268 switch(node[0]){
1269 case 0:break;
1270 case 20:case 21:case 25:case 26:case 28:case 31:case 41:case 43:case 45:{
1271 static const uint8_t kinds[46]={[20]=OMEGA_GPU_Q36_K_NORM,[21]=OMEGA_GPU_Q36_K_PERM,[25]=OMEGA_GPU_Q36_K_PERM,
1272 [26]=OMEGA_GPU_Q36_K_GATHER,[28]=OMEGA_GPU_Q36_K_GATE,[31]=OMEGA_GPU_Q36_K_RES,
1273 [41]=OMEGA_GPU_Q36_K_PERM,[43]=OMEGA_GPU_Q36_K_PERM,[45]=OMEGA_GPU_Q36_K_GATE};
1274 rc=add_glue(g,kinds[node[0]],kernels[node[1]],node[2],node[3],node[4],words,node[5]);break;
1275 }
1276 case 22:rc=after_op(g,OMEGA_GPU_Q36_K_AB,omega_gpu_bf16_gemv_rows_record(g->cl,g->layer[l].ab,node[7],node[11],node[12]));break;
1277 case 23:rc=native_record_gemm(g,OMEGA_GPU_Q36_K_MIX,g->layer[l].mix,node[7],node[8],node[10],node[11],node[12],0,0,0);break;
1278 case 24:rc=after_op(g,OMEGA_GPU_Q36_K_GDN,omega_gpu_gdn_record(g->cl,g->gdn_h[head],ki,node[11],node[12],node[13],
1279 g->cfg.eps_l2,g->cfg.eps,1.0f,SCALE_2P64,&values[15]));break;
1280 case 27:rc=after_op(g,OMEGA_GPU_Q36_K_ATTN,omega_gpu_decode_attn256_record(g->cl,g->attn_h[head],ki,node[18],
1281 node[11],g->cfg.eps,SCALE_2P64,node[12]));break;
1282 case 30:rc=native_record_gemm(g,OMEGA_GPU_Q36_K_O,g->layer[l].o,node[7],node[8],node[10],node[11],node[12],0,0,0);break;
1283 case 32:{
1284 OmegaGpuRouterDevOut r={0};
1285 rc=after_op(g,OMEGA_GPU_Q36_K_ROUTER,omega_gpu_router_record_rows(g->cl,g->cfg.router,l,node[11],XS,g->cfg.eps,1,SCALE_2P64,
1286 node[12],g->lo.lb_stride,node[7],&r));
1287 if(rc==OMEGA_GPU_DGRAPH_OK&&node[20]==0){*ids_off=(uint32_t)(r.ids_va-node[12]);*wts_off=(uint32_t)(r.wts_va-node[12]);
1288 *xo_off=(uint32_t)(r.xout_va-node[12]);if(first_router)*first_router=r;}break;
1289 }
1290 case 42:rc=after_op(g,OMEGA_GPU_Q36_K_GDN,omega_gpu_gdn_multi_record(g->cl,g->gdn_h[head],ki,node[7],node[11],node[12],node[15],
1291 node[13],node[16],node[14],node[17],g->cfg.eps_l2,g->cfg.eps,1.0f,SCALE_2P64));break;
1292 case 44:rc=after_op(g,OMEGA_GPU_Q36_K_ATTN,omega_gpu_decode_attn256_multi_record(g->cl,g->attn_h[head],ki,node[18],node[7],
1293 node[11],node[15],g->cfg.eps,SCALE_2P64,node[12],node[17]));break;
1294 default:return fail(g,NULL,OMEGA_GPU_DGRAPH_CHIP_FAIL);
1295 }
1296 }
1297#else
1298 /* layer 0 normalizes the embeddings; after that the previous layer's per-token COMBINE|NORM left layer l's x' in xq */
1299 if (l == 0) rc = rec_res_rows(g, K_NORM_R, x0, 0, in_norm, xq0, SCALE_2P64, n);
1300 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_XQ, xq0, pt2048, xp, H, n);
1301 if (!g->layer[l].attn) {
1302 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));
1303 if (rc == OMEGA_GPU_DGRAPH_OK) rc = native_record_gemm(g, OMEGA_GPU_Q36_K_MIX, g->layer[l].mix, n, 1, segd, xp, mixN, 0, 0, 0);
1304 if (g->pf.gdn_multi && g->pf.mutant != 1 && g->pf.mutant != 3) {
1305 /* every token's recurrence step in one launch (the decode step T times, bit for bit: omega_gpu_gdn_api.h), the rows
1306 * into attN (unused on a GDN layer), then one PERM over the rows. TEST mutant 5: every token reads token 0's a|b. */
1307 if (rc == OMEGA_GPU_DGRAPH_OK)
1308 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,
1309 OMEGA_GPU_Q36_GDN_MIX * 4u, abN, g->pf.mutant == 5 ? 0u : OMEGA_GPU_Q36_AB * 4u,
1310 attN, (uint32_t)MOS, g->cfg.eps_l2, g->cfg.eps, 1.0f, SCALE_2P64));
1311 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_O, attN, pt4096, op, OMEGA_GPU_Q36_MIXER_OUT, n);
1312 } else
1313 for (uint32_t i = 0; i < n && rc == OMEGA_GPU_DGRAPH_OK; i++) {
1314 /* TEST mutant 1: the last two tokens' recurrence steps in swapped order */
1315 const uint32_t t = (g->pf.mutant == 1 && n >= 2 && i + 2 >= n) ? (i + 2 == n ? n - 1 : n - 2) : i;
1316 const uint64_t mix_t = mixN + (uint64_t)t * OMEGA_GPU_Q36_GDN_MIX * 4u;
1317 uint64_t mo = 0;
1318 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,
1319 abN + (uint64_t)t * OMEGA_GPU_Q36_AB * 4u, g->cfg.eps_l2, g->cfg.eps, 1.0f, SCALE_2P64, &mo));
1320 /* TEST mutant 3: the copy out of the GDN launch buffer deferred past the next token's step (the buffer is the
1321 * state object's own, one per conversation: every row would read a later token's output) */
1322 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);
1323 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);
1324 }
1325 } else {
1326 if (rc == OMEGA_GPU_DGRAPH_OK) rc = native_record_gemm(g, OMEGA_GPU_Q36_K_MIX, g->layer[l].mix, n, 1, segd, xp, mixN, 0, 0, 0);
1327 uint32_t w[OMEGA_DEC_GA_WORDS]; memset(w, 0, sizeof w);
1328 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);
1329 if (rc == OMEGA_GPU_DGRAPH_OK)
1330 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,
1331 (size_t)OMEGA_GPU_Q36_ATT_MIX * n, w, OMEGA_DEC_GA_WORDS);
1332 const int attn_multi = g->pf.attn_multi;
1333 if (attn_multi && rc == OMEGA_GPU_DGRAPH_OK) /* every token's attention in one prep + attend pair, bit-identical to the steps */
1334 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,
1335 g->cfg.eps, SCALE_2P64, attN, (uint32_t)MOS));
1336 const int gate_rows = attn_multi && g->mutant != 3; /* every token's gate in one rows launch (the one-row arithmetic per element) */
1337 if (gate_rows && rc == OMEGA_GPU_DGRAPH_OK) {
1338 uint32_t w2[OMEGA_DEC_GT_WORDS]; memset(w2, 0, sizeof w2);
1339 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);
1340 w2[(OMEGA_DEC_GT_N - 0x380) / 4] = OMEGA_GPU_Q36_MIXER_OUT;
1341 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);
1342 }
1343 for (uint32_t t = 0; t < n && rc == OMEGA_GPU_DGRAPH_OK && !gate_rows; t++) {
1344 const uint64_t agg_t = aggN + (uint64_t)t * OMEGA_GPU_Q36_ATT_MIX * 4u, att_t = attN + t * MOS;
1345 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));
1346 uint32_t w2[OMEGA_DEC_GT_WORDS]; memset(w2, 0, sizeof w2);
1347 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);
1348 w2[(OMEGA_DEC_GT_N - 0x380) / 4] = OMEGA_GPU_Q36_MIXER_OUT;
1349 if (rc == OMEGA_GPU_DGRAPH_OK)
1350 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,
1351 OMEGA_GPU_Q36_MIXER_OUT, w2, OMEGA_DEC_GT_WORDS);
1352 }
1353 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_O, attN, pt4096, op, OMEGA_GPU_Q36_MIXER_OUT, n);
1354 }
1355 if (rc == OMEGA_GPU_DGRAPH_OK) rc = native_record_gemm(g, OMEGA_GPU_Q36_K_O, g->layer[l].o, n, 1, segd + OMEGA_GEMM_SEG_BYTES, op, oN, 0, 0, 0);
1356 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_res_rows(g, K_ADD_R, x0, oN, 0, 0, 1.0f, n);
1357 for (uint32_t r0 = 0; r0 < n && rc == OMEGA_GPU_DGRAPH_OK; r0 += OMEGA_GPU_ROUTER_MAX_ROWS) {
1358 const uint32_t rows = n - r0 < OMEGA_GPU_ROUTER_MAX_ROWS ? n - r0 : OMEGA_GPU_ROUTER_MAX_ROWS;
1359 OmegaGpuRouterDevOut r; memset(&r, 0, sizeof r);
1360 const uint64_t lb = rlb + (uint64_t)r0 * g->lo.lb_stride;
1361 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,
1362 lb, g->lo.lb_stride, rows, &r));
1363 if (rc == OMEGA_GPU_DGRAPH_OK && r0 == 0) {
1364 *ids_off = (uint32_t)(r.ids_va - lb); *wts_off = (uint32_t)(r.wts_va - lb); *xo_off = (uint32_t)(r.xout_va - lb);
1365 if (first_router) *first_router = r;
1366 }
1367 }
1368#endif
1369 return rc;
1370}
1371
1372/* the host GROUP: the n * 8 routed picks sorted by expert id (then token, then slot) into segments and row maps, the shared
1373 * expert as its own one-segment table (x' row t, y row t * 9 + 8). Returns the routed segment count. */
1374static uint32_t pf_group(OmegaGpuQ36Graph *g, uint32_t l, uint32_t n, uint32_t ids_off) {
1375#ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS
1376 uint64_t cfg[16]={0,n,0,g->pf.mutant,PF_EXPERT_BYTES,PF_PAD},state[512]={0},out[32],result=0;
1377 const uint8_t zero[32]={0};
1378 for(uint32_t t=0;t<n;t++){
1379 const void *ids=pf_hc(g,g->pf.rlb+(size_t)t*g->lo.lb_stride+ids_off);
1380 if(omega_mg_group(&result,cfg,16,ids,32,state,512,out,32)!=OMEGA_OK||result!=1)return 0;
1381 }
1382 Seg32 *sg=pf_hc(g,g->pf.sgu),*sd=pf_hc(g,g->pf.sdn);
1383 uint32_t segs=0;
1384 cfg[0]=1;
1385 for(;;){
1386 if(omega_mg_group(&result,cfg,16,zero,32,state,512,out,32)!=OMEGA_OK||result!=1)return 0;
1387 if(out[0]==256)break;
1388 memset(&sg[segs],0,sizeof sg[segs]);memset(&sd[segs],0,sizeof sd[segs]);
1389 (void)omega_gpu_fp8_tensor_entry(g->layer[l].gu,out[0],&sg[segs].w_va,&sg[segs].s_va);
1390 (void)omega_gpu_fp8_tensor_entry(g->layer[l].dn,out[0],&sd[segs].w_va,&sd[segs].s_va);
1391 sg[segs].row0=sd[segs].row0=out[1];sg[segs].cnt=sd[segs].cnt=out[3];
1392 g->pf.streamed+=out[4];cfg[6]=out[0]+1;cfg[7]=out[1]+out[2];segs++;
1393 }
1394 uint32_t *xm=pf_hc(g,g->pf.xmap),*ym=pf_hc(g,g->pf.ymap);
1395 cfg[0]=2;
1396 for(uint32_t t=0;t<n;t++){
1397 cfg[2]=t;
1398 const void *ids=pf_hc(g,g->pf.rlb+(size_t)t*g->lo.lb_stride+ids_off);
1399 if(omega_mg_group(&result,cfg,16,ids,32,state,512,out,32)!=OMEGA_OK||result!=1)return 0;
1400 for(unsigned s=0;s<8;s++){xm[out[s*4]]=out[s*4+1];ym[out[s*4+2]]=out[s*4+3];}
1401 }
1402 cfg[0]=4;
1403 if(omega_mg_group(&result,cfg,16,zero,32,state,512,out,32)!=OMEGA_OK||result!=1)return 0;
1404 for(uint64_t p=out[0];p<out[1];p++){xm[p]=0;ym[p]=0;}
1405 Seg32 *g2=pf_hc(g,g->pf.sgu2),*d2=pf_hc(g,g->pf.sdn2);
1406 memset(g2,0,sizeof *g2);memset(d2,0,sizeof *d2);
1407 (void)omega_gpu_fp8_tensor_entry(g->layer[l].gu,256,&g2->w_va,&g2->s_va);
1408 (void)omega_gpu_fp8_tensor_entry(g->layer[l].dn,256,&d2->w_va,&d2->s_va);
1409 g2->cnt=d2->cnt=n;g->pf.streamed+=out[4];
1410 uint32_t *xm2=pf_hc(g,g->pf.xmap2),*ym2=pf_hc(g,g->pf.ymap2);
1411 for(uint64_t p=out[2];p<out[3];p++){xm2[p]=0;ym2[p]=0;}
1412 cfg[0]=3;
1413 for(uint32_t t=0;t<n;t++){
1414 cfg[2]=t;
1415 if(omega_mg_group(&result,cfg,16,zero,32,state,512,out,32)!=OMEGA_OK||result!=1)return 0;
1416 xm2[t]=out[0];ym2[t]=out[1];
1417 }
1418 return segs;
1419#else
1420 const uint32_t S = NG - 1u;
1421 uint32_t cnt[256], start[256];
1422 memset(cnt, 0, sizeof cnt);
1423 for (uint32_t t = 0; t < n; t++) {
1424 const uint32_t *id = pf_hc(g, g->pf.rlb + (size_t)t * g->lo.lb_stride + ids_off);
1425 for (uint32_t s = 0; s < S; s++) cnt[id[s] & 255u]++;
1426 }
1427 Seg32 *sg = pf_hc(g, g->pf.sgu), *sd = pf_hc(g, g->pf.sdn);
1428 uint32_t segs = 0, row = 0;
1429 for (uint32_t e = 0; e < 256u; e++) {
1430 start[e] = row;
1431 if (!cnt[e]) continue;
1432 memset(&sg[segs], 0, sizeof *sg); memset(&sd[segs], 0, sizeof *sd);
1433 (void)omega_gpu_fp8_tensor_entry(g->layer[l].gu, e, &sg[segs].w_va, &sg[segs].s_va);
1434 (void)omega_gpu_fp8_tensor_entry(g->layer[l].dn, e, &sd[segs].w_va, &sd[segs].s_va);
1435 sg[segs].row0 = sd[segs].row0 = row; sg[segs].cnt = sd[segs].cnt = cnt[e];
1436 if (g->pf.mutant == 2 && cnt[e] > 1u) { sg[segs].cnt--; sd[segs].cnt--; } /* TEST mutant 2: every segment one row short */
1437 g->pf.streamed += (uint64_t)((cnt[e] + 3u) / 4u) * PF_EXPERT_BYTES; /* gate|up and down, 4 rows per pass */
1438 row += cnt[e]; segs++;
1439 }
1440 uint32_t *xm = pf_hc(g, g->pf.xmap), *ym = pf_hc(g, g->pf.ymap);
1441 for (uint32_t t = 0; t < n; t++) {
1442 const uint32_t *id = pf_hc(g, g->pf.rlb + (size_t)t * g->lo.lb_stride + ids_off);
1443 for (uint32_t s = 0; s < S; s++) { const uint32_t p = start[id[s] & 255u]++; xm[p] = t; ym[p] = t * NG + s; }
1444 }
1445 for (uint32_t p = n * S; p < n * S + PF_PAD; p++) { xm[p] = 0; ym[p] = 0; }
1446 Seg32 *g2 = pf_hc(g, g->pf.sgu2), *d2 = pf_hc(g, g->pf.sdn2);
1447 memset(g2, 0, sizeof *g2); memset(d2, 0, sizeof *d2);
1448 (void)omega_gpu_fp8_tensor_entry(g->layer[l].gu, 256u, &g2->w_va, &g2->s_va); /* expert 256 = the shared expert */
1449 (void)omega_gpu_fp8_tensor_entry(g->layer[l].dn, 256u, &d2->w_va, &d2->s_va);
1450 g2->cnt = d2->cnt = n;
1451 g->pf.streamed += (uint64_t)((n + 3u) / 4u) * PF_EXPERT_BYTES;
1452 uint32_t *xm2 = pf_hc(g, g->pf.xmap2), *ym2 = pf_hc(g, g->pf.ymap2);
1453 for (uint32_t t = 0; t < n; t++) { xm2[t] = t; ym2[t] = t * NG + S; }
1454 for (uint32_t p = n; p < n + PF_PAD; p++) { xm2[p] = 0; ym2[p] = 0; }
1455 return segs;
1456#endif
1457}
1458
1459/* part (b) of layer l: the experts of every row and the per-token COMBINE|NORM */
1460static 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) {
1461 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);
1462 const uint64_t actp = pf_d(g, g->pf.actp), dnN = pf_d(g, g->pf.dnN), rlb = pf_h(g, g->pf.rlb);
1463 const uint64_t pt2048 = va_of(g, g->lo.ptab), pt512 = pt2048 + (uint64_t)(H + OMEGA_GPU_Q36_MIXER_OUT) * 4u;
1464 const uint64_t in_norm = va_of(g, g->lo.norms) + (uint64_t)l * H * 4u;
1465#ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS
1466 const uint64_t cfg[16]={4,q36_cn_rows(),0,0,0,0,l,g->cfg.layers,0,g->mutant,g->head8!=NULL,g->flush_each,H,EI,NG,segs};
1467 uint64_t values[64]={x0,xq0,0,0,0,0,0,0,0,in_norm,va_of(g,g->lo.fnorm)};
1468 values[14]=va_of(g,hidden_of(g,head));values[27]=omega_graph_fbits(g->cfg.eps);
1469 values[28]=omega_graph_fbits(SCALE_2P64);values[29]=omega_graph_fbits(1.0f);
1470 values[30]=n;values[31]=g->lo.lb_stride;values[40]=pf_h(g,g->pf.sgu);values[41]=pf_h(g,g->pf.sdn);
1471 values[42]=pf_h(g,g->pf.xmap);values[43]=pf_h(g,g->pf.ymap);values[44]=xp2;values[45]=guN;
1472 values[46]=actN;values[47]=actp;values[48]=dnN;values[49]=pt2048;values[51]=pt512;
1473 values[56]=rlb+wts_off;values[57]=rlb+xo_off;values[60]=pf_h(g,g->pf.sgu2);values[61]=pf_h(g,g->pf.sdn2);
1474 values[62]=pf_h(g,g->pf.xmap2);values[63]=pf_h(g,g->pf.ymap2);
1475 static const int kernels[26]={K_NORM,K_ADD,K_CN,K_SW,K_GATHER,K_GATE,K_NORM_R,K_ADD_R,K_CN_R,
1476 K_PERM_XQ,K_PERM_O,K_PERM_XO,K_PERM_ACT,K_GROUP,0,0,0,0,0,0,K_SW_M1,K_GATHER_M1,K_GATE_M1,K_CN_M3,0,K_CN_R_M3};
1477 int rc=OMEGA_GPU_DGRAPH_OK;
1478 for(uint64_t phase=0;rc==OMEGA_GPU_DGRAPH_OK;phase++){
1479 uint64_t node[64],result=0;uint32_t words[32];
1480 if(omega_mg_exp(&result,cfg,16,values,64,node,64,phase)!=OMEGA_OK||result!=1)return fail(g,NULL,OMEGA_GPU_DGRAPH_CHIP_FAIL);
1481 if(node[0]==255)break;
1482 omega_graph_args(node,words);
1483 switch(node[0]){
1484 case 34:case 37:rc=add_glue(g,OMEGA_GPU_Q36_K_PERM,kernels[node[1]],node[2],node[3],node[4],words,node[5]);break;
1485 case 35:rc=native_record_gemm(g,OMEGA_GPU_Q36_K_GATE_UP,g->layer[l].gu,node[7],node[8],node[10],node[11],node[12],node[13],node[14],4);break;
1486 case 36:rc=add_glue(g,OMEGA_GPU_Q36_K_SWIGLU,kernels[node[1]],node[2],node[3],node[4],words,node[5]);break;
1487 case 38:rc=native_record_gemm(g,OMEGA_GPU_Q36_K_DOWN,g->layer[l].dn,node[7],node[8],node[10],node[11],node[12],node[13],node[14],4);break;
1488 case 39:case 40:rc=add_glue(g,OMEGA_GPU_Q36_K_RES,kernels[node[1]],node[2],node[3],node[4],words,node[5]);break;
1489 default:return fail(g,NULL,OMEGA_GPU_DGRAPH_CHIP_FAIL);
1490 }
1491 }
1492#else
1493 const uint64_t XS=(uint64_t)H*4u;
1494 const uint32_t R = n * NG; const uint32_t S = NG - 1u; /* rows each expert GEMM touches: 8 routed per token, 1 shared */
1495 int rc = rec_perm(g, K_PERM_XO, rlb + xo_off, pt2048, xp2, H, n); /* the router's x' rows, one launch buffer apart */
1496 if (rc == OMEGA_GPU_DGRAPH_OK)
1497 rc = native_record_gemm(g, OMEGA_GPU_Q36_K_GATE_UP, g->layer[l].gu, n * S, segs, pf_h(g, g->pf.sgu), xp2, guN, pf_h(g, g->pf.xmap),
1498 pf_h(g, g->pf.ymap), 4);
1499 if (rc == OMEGA_GPU_DGRAPH_OK)
1500 rc = native_record_gemm(g, OMEGA_GPU_Q36_K_GATE_UP, g->layer[l].gu, n, 1, pf_h(g, g->pf.sgu2), xp2, guN, pf_h(g, g->pf.xmap2),
1501 pf_h(g, g->pf.ymap2), 4);
1502 uint32_t w3[OMEGA_DEC_SW_WORDS]; memset(w3, 0, sizeof w3);
1503 put64(w3, OMEGA_DEC_SW_GU, guN); put64(w3, OMEGA_DEC_SW_ACT, actN); putf(w3, OMEGA_DEC_SW_SCALE, SCALE_2P64);
1504 if (rc == OMEGA_GPU_DGRAPH_OK)
1505 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);
1506 if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_ACT, actN, pt512, actp, EI, R);
1507 if (rc == OMEGA_GPU_DGRAPH_OK)
1508 rc = native_record_gemm(g, OMEGA_GPU_Q36_K_DOWN, g->layer[l].dn, n * S, segs, pf_h(g, g->pf.sdn), actp, dnN, pf_h(g, g->pf.ymap),
1509 pf_h(g, g->pf.ymap), 4);
1510 if (rc == OMEGA_GPU_DGRAPH_OK)
1511 rc = native_record_gemm(g, OMEGA_GPU_Q36_K_DOWN, g->layer[l].dn, n, 1, pf_h(g, g->pf.sdn2), actp, dnN, pf_h(g, g->pf.ymap2),
1512 pf_h(g, g->pf.ymap2), 4);
1513 const int last = l + 1 == g->cfg.layers;
1514 const uint64_t nw = last ? va_of(g, g->lo.fnorm) : in_norm + H * 4u;
1515 /* the last layer: only the last token's COMBINE|NORM is needed (its hidden feeds the head; the state is complete) */
1516 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) */
1517 return rc == OMEGA_GPU_DGRAPH_OK ? rec_cn_rows(g, x0, dnN, rlb + wts_off, nw, xq0, SCALE_2P64, n) : rc;
1518 for (uint32_t t = last ? n - 1u : 0u; t < n && rc == OMEGA_GPU_DGRAPH_OK; t++)
1519 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,
1520 last ? va_of(g, hidden_of(g, head)) : xq0 + t * XS, last && !g->head8 ? 1.0f : SCALE_2P64);
1521#endif
1522 return rc;
1523}
1524
1525typedef struct {
1526 OmegaGpuQ36NativeState *state[2];
1527 const OmegaGpuNativeArtifact *artifact[2];
1528 const uint64_t *row[2];
1529 OmegaGpuNativeLease lease[2];
1530 OmegaGpuMem code[2];
1531 uint64_t ordinal[2];
1532} Q36MoePair;
1533struct Q36MoeObservation {
1534 uint64_t windows[1024], meta[4];
1535 Q36MoePair pair[128];
1536 OmegaGpuRouterDevOut router;
1537 OmegaGpuMem router_code[2]; /* actual plain/ROWS owners, not deduplicated launches */
1538 uint64_t router_code_generation[2];
1539 uint32_t router_code_count;
1540 uint32_t layer, tokens, head;
1541};
1542
1543static void q36_moe_release(Q36MoeObservation *m) {
1544 if (!m) return;
1545 for (uint64_t i = 0; i < m->meta[0]; ++i)
1546 for (unsigned r = 0; r < 2; ++r)
1547 if (m->pair[i].lease[r].cache) omega_q36_native_end(&m->pair[i].lease[r]);
1548 omega_gpu_host_free(m);
1549}
1550
1551/* Resolve the entire pair/window set before the first operation record. Absent
1552 * contexts keep the incumbent path; a partial or rejected candidate is an error,
1553 * never permission to change the operation back to its incumbent. */
1554static int q36_moe_select(OmegaGpuQ36Graph *g, uint32_t head, uint32_t l,
1555 uint32_t n, Q36MoeObservation **out) {
1556 uint64_t windows[1024] = {0}, meta[4] = {0}, result = 0;
1557 const uint64_t cfg[8] = {1, n, H, EI, NG, g->lo.lb_stride, 128, 0};
1558 *out = NULL;
1559 if (!g->native) return 1;
1560 if (omega_moe_windows(&result, cfg, 8, windows, 1024, meta, 4) != OMEGA_OK || result) return -1;
1561 /* Legacy whole-operation expert keys cannot bypass the window pairing by
1562 * being discovered only after the incumbent router list has committed. */
1563 if (meta[0] > 1) {
1564 for (unsigned r = 0; r < 2; ++r) {
1565 uint64_t actual[4] = {r ? OMEGA_GPU_Q36_K_DOWN : OMEGA_GPU_Q36_K_GATE_UP, l, 0, n};
1566 OmegaGpuQ36NativeState *unused = NULL;
1567 if (omega_q36_native_context(g->native, actual, &unused) != 1) return -1;
1568 }
1569 }
1570 Q36MoeObservation *m = omega_gpu_host_calloc(1, sizeof *m);
1571 if (!m) return -1;
1572 memcpy(m->windows, windows, sizeof windows); memcpy(m->meta, meta, sizeof meta);
1573 m->layer = l; m->tokens = n; m->head = head;
1574 unsigned found = 0, absent = 0;
1575 for (uint64_t i = 0; i < meta[0]; ++i) {
1576 for (unsigned r = 0; r < 2; ++r) {
1577 uint64_t actual[4] = {r ? OMEGA_GPU_Q36_K_DOWN : OMEGA_GPU_Q36_K_GATE_UP,
1578 l, windows[i * 8], windows[i * 8 + 1]};
1579 Q36MoePair *p = &m->pair[i];
1580 int rc = omega_q36_native_context(g->native, actual, &p->state[r]);
1581 if (rc < 0) goto refuse;
1582 if (rc == 1) { ++absent; continue; }
1583 ++found;
1584 if (omega_q36_native_begin_selected(p->state[r], omega_gpu_fp8gemm_native_cache(),
1585 &p->artifact[r], &p->lease[r], &p->row[r]) != 0) goto refuse;
1586 if (!p->state[r]->expected_closure || !p->state[r]->provenance) goto refuse;
1587 if (omega_gpu_fp8gemm_native_code_observe(&p->lease[r], &p->code[r])) goto refuse;
1588 }
1589 if (m->pair[i].state[0] && m->pair[i].state[1] &&
1590 (memcmp(m->pair[i].state[0]->expected_closure,
1591 m->pair[i].state[1]->expected_closure, 16384) ||
1592 memcmp(m->pair[i].state[0]->provenance,
1593 m->pair[i].state[1]->provenance, sizeof(OmegaGpuQ36NativeProvenance))))
1594 goto refuse;
1595 }
1596 if (!found) { q36_moe_release(m); return 1; }
1597 if (absent || g->flush_each || !g->owner_generation || !g->pf_generation ||
1598 !g->tensor_pair_generation[l]) goto refuse;
1599 *out = m;
1600 return 0;
1601refuse:
1602 q36_moe_release(m);
1603 return -1;
1604}
1605
1606/* Exact selected-pair adapter. No re-selection, host route download, submit,
1607 * fabricated launch256, or replacement executor. Existing barriers order the
1608 * reusable GROUP descriptor/map scratch between windows. */
1609static int pf_record_moe_window(OmegaGpuQ36Graph *g, uint64_t ordinal) {
1610 Q36MoeObservation *m = g->moe;
1611 const uint64_t *w = m->windows + ordinal * 8;
1612 Q36MoePair *p = &m->pair[ordinal];
1613 uint64_t req[16] = {m->router.ids_va + w[7], m->router.tab_gu_va + w[7],
1614 m->router.tab_dn_va + w[7], pf_h(g, g->pf.sgu), pf_h(g, g->pf.sdn),
1615 pf_h(g, g->pf.xmap), pf_h(g, g->pf.ymap), g->lo.lb_stride, w[1],
1616 256, (uint64_t)g->pf.cap * NG + PF_PAD, NG,
1617 omega_gpu_session_epoch(), omega_gpu_session_epoch(), 1, 256};
1618 uint32_t words[OMEGA_DEC_GR_WORDS] = {0};
1619 uint64_t result = 0;
1620 if (omega_moe_group(&result, req, 16, words, 64) != OMEGA_OK || result) return -1;
1621 const uint32_t l = m->layer;
1622#ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS
1623 const uint64_t cfg[16]={w[1],w[2],H,EI,g->mutant,omega_graph_fbits(SCALE_2P64)};
1624 const uint64_t values[16]={pf_d(g,g->pf.xp2),pf_d(g,g->pf.guN),pf_d(g,g->pf.actN),pf_d(g,g->pf.actp),
1625 pf_d(g,g->pf.dnN),pf_h(g,g->pf.xmap),pf_h(g,g->pf.ymap),pf_h(g,g->pf.sgu),pf_h(g,g->pf.sdn),
1626 m->router.xout_va,va_of(g,g->lo.ptab),va_of(g,g->lo.ptab)+(uint64_t)(H+OMEGA_GPU_Q36_MIXER_OUT)*4u};
1627 int rc=0;
1628 for(uint64_t phase=0;!rc;phase++){
1629 uint64_t node[64],out=0;uint32_t args[32];
1630 if(omega_mg_win(&out,cfg,16,values,16,w,8,node,64,phase)!=OMEGA_OK||out!=1)return -1;
1631 if(node[0]==255)break;
1632 omega_graph_args(node,args);
1633 switch(node[0]){
1634 case 46:rc=add_glue(g,OMEGA_GPU_Q36_K_GROUP,K_GROUP,node[2],node[3],node[4],words,OMEGA_DEC_GR_WORDS);break;
1635 case 34:case 37:rc=add_glue(g,OMEGA_GPU_Q36_K_PERM,node[1]==11?K_PERM_XO:K_PERM_ACT,node[2],node[3],node[4],args,node[5]);break;
1636 case 36:rc=add_glue(g,OMEGA_GPU_Q36_K_SWIGLU,node[1]==20?K_SW_M1:K_SW,node[2],node[3],node[4],args,node[5]);break;
1637 case 35:case 38:{
1638 unsigned i=node[15];p->ordinal[i]=omega_gpu_cmdlist_count(g->cl);
1639 rc=after_op(g,i?OMEGA_GPU_Q36_K_DOWN:OMEGA_GPU_Q36_K_GATE_UP,
1640 omega_q36_native_record_status(omega_gpu_fp8gemm_record_native(g->cl,i?g->layer[l].dn:g->layer[l].gu,
1641 p->artifact[i],&p->lease[i],node[7],node[8],node[10],node[11],node[12],node[13],node[14])));break;
1642 }
1643 default:return -1;
1644 }
1645 }
1646#else
1647 const uint32_t n=(uint32_t)w[1],rows=(uint32_t)w[2];
1648 const uint64_t xp = pf_d(g, g->pf.xp2) + w[3], gu = pf_d(g, g->pf.guN) + w[4];
1649 const uint64_t act = pf_d(g, g->pf.actN) + w[5], actp = pf_d(g, g->pf.actp) + w[5];
1650 const uint64_t dn = pf_d(g, g->pf.dnN) + w[6];
1651 const uint64_t xm = pf_h(g, g->pf.xmap), ym = pf_h(g, g->pf.ymap);
1652 const uint64_t pt = va_of(g, g->lo.ptab);
1653 int rc = add_glue(g, OMEGA_GPU_Q36_K_GROUP, K_GROUP, 1, 1, rows, words, OMEGA_DEC_GR_WORDS);
1654 if (!rc) rc = rec_perm(g, K_PERM_XO, m->router.xout_va + w[7], pt, xp, H, n);
1655 if (!rc) p->ordinal[0] = omega_gpu_cmdlist_count(g->cl);
1656 if (!rc) rc = after_op(g, OMEGA_GPU_Q36_K_GATE_UP,
1657 omega_q36_native_record_status(omega_gpu_fp8gemm_record_native(g->cl, g->layer[l].gu, p->artifact[0],
1658 &p->lease[0], rows, rows, pf_h(g, g->pf.sgu), xp, gu, xm, ym)));
1659 uint32_t sw[OMEGA_DEC_SW_WORDS] = {0};
1660 put64(sw, OMEGA_DEC_SW_GU, gu); put64(sw, OMEGA_DEC_SW_ACT, act);
1661 putf(sw, OMEGA_DEC_SW_SCALE, SCALE_2P64);
1662 if (!rc) rc = add_glue(g, OMEGA_GPU_Q36_K_SWIGLU, g->mutant == 1 ? K_SW_M1 : K_SW,
1663 EI / OMEGA_DEC_THREADS, rows, (size_t)rows * EI, sw, OMEGA_DEC_SW_WORDS);
1664 if (!rc) rc = rec_perm(g, K_PERM_ACT, act, pt + (uint64_t)(H + OMEGA_GPU_Q36_MIXER_OUT) * 4u, actp, EI, rows);
1665 if (!rc) p->ordinal[1] = omega_gpu_cmdlist_count(g->cl);
1666 if (!rc) rc = after_op(g, OMEGA_GPU_Q36_K_DOWN,
1667 omega_q36_native_record_status(omega_gpu_fp8gemm_record_native(g->cl, g->layer[l].dn, p->artifact[1],
1668 &p->lease[1], rows, rows, pf_h(g, g->pf.sdn), actp, dn, ym, ym)));
1669#endif
1670 return rc;
1671}
1672
1673static int pf_record_moe_combine(OmegaGpuQ36Graph *g, uint32_t wts_off) {
1674 const Q36MoeObservation *m = g->moe;
1675 const uint32_t l = m->layer, n = m->tokens, head = m->head;
1676 const uint64_t x = pf_h(g, g->pf.x), dn = pf_d(g, g->pf.dnN), xq = pf_d(g, g->pf.xq);
1677 const uint64_t rlb=pf_h(g,g->pf.rlb);
1678#ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS
1679 const uint64_t cfg[16]={4,q36_cn_rows(),0,0,0,0,l,g->cfg.layers,0,g->mutant,g->head8!=NULL,g->flush_each,H,EI,NG};
1680 uint64_t values[64]={x,xq,0,0,0,0,0,0,0,va_of(g,g->lo.norms)+(uint64_t)l*H*4u,va_of(g,g->lo.fnorm)};
1681 values[14]=va_of(g,hidden_of(g,head));values[27]=omega_graph_fbits(g->cfg.eps);
1682 values[28]=omega_graph_fbits(SCALE_2P64);values[29]=omega_graph_fbits(1.0f);
1683 values[30]=n;values[31]=g->lo.lb_stride;values[48]=dn;values[56]=rlb+wts_off;
1684 int rc=0;
1685 for(uint64_t phase=7;!rc;phase++){
1686 uint64_t node[64],result=0;uint32_t words[32];
1687 if(omega_mg_exp(&result,cfg,16,values,64,node,64,phase)!=OMEGA_OK||result!=1)return -1;
1688 if(node[0]==255)break;
1689 omega_graph_args(node,words);
1690 int ki=node[0]==39?(node[1]==25?K_CN_R_M3:K_CN_R):(node[1]==23?K_CN_M3:K_CN);
1691 rc=add_glue(g,OMEGA_GPU_Q36_K_RES,ki,node[2],node[3],node[4],words,node[5]);
1692 }
1693#else
1694 const uint64_t xs=(uint64_t)H*4u;
1695 const int last = l + 1 == g->cfg.layers;
1696 const uint64_t nw = last ? va_of(g, g->lo.fnorm) : va_of(g, g->lo.norms) + (uint64_t)(l + 1) * H * 4u;
1697 if (!last && n > 1 && q36_cn_rows()) return rec_cn_rows(g, x, dn, rlb + wts_off, nw, xq, SCALE_2P64, n);
1698 int rc = 0;
1699 for (uint32_t t = last ? n - 1u : 0u; t < n && !rc; ++t)
1700 rc = rec_res_w(g, g->mutant == 4 ? K_CN_M3 : K_CN, x + t * xs,
1701 dn + (uint64_t)t * NG * H * 4u, rlb + (uint64_t)t * g->lo.lb_stride + wts_off,
1702 nw, last ? va_of(g, hidden_of(g, head)) : xq + t * xs,
1703 last && !g->head8 ? 1.0f : SCALE_2P64);
1704#endif
1705 return rc;
1706}
1707
1708/* Raw observations enter only the canonical Omega serializers. These helpers
1709 * project allocation membership; they do not serialize, hash or qualify it. */
1710static int q36_moe_resource(uint64_t scalars[128], uint64_t allocations[1024],
1711 unsigned role, const OmegaGpuMem *mem, uint64_t va, uint64_t generation, uint64_t stride) {
1712 if (role >= 64 || !mem || !mem->cpu || va < mem->va || va - mem->va >= mem->size) return -1;
1713 uint64_t available = mem->size - (va - mem->va);
1714 uint64_t *a = allocations + role * 16;
1715 a[0] = role; a[1] = mem->handle.index; a[2] = mem->handle.generation;
1716 a[3] = mem->kind; a[4] = mem->va; a[5] = mem->size;
1717 a[6] = va; a[7] = available; a[8] = generation;
1718 if (role < 16) {
1719 unsigned base = role < 8 ? 32 + role * 4 : 80 + (role - 8) * 4;
1720 scalars[base] = va; scalars[base + 1] = available;
1721 scalars[base + 2] = generation; scalars[base + 3] = stride;
1722 }
1723 return 0;
1724}
1725
1726int omega_gpu_q36_moe_project(const OmegaGpuCmdList *cl,
1727 const OmegaGpuCmdListConsumed *consumed, void *opaque) {
1728 OmegaGpuQ36MoeProjection *p = opaque;
1729 OmegaGpuCmdListOwner owner;
1730 uint8_t codes[6][4096] = {{0}}, launches[6][4096] = {{0}};
1731 uint64_t ordinals[6][128] = {{0}}, counts[8] = {0}, roles[4096];
1732 uint8_t code[16384] = {0}, nested[2048] = {0};
1733 uint64_t result = 0;
1734 if (!p) return -1;
1735 memset(p->nested, 0, sizeof p->nested);
1736 if (!consumed || p->code_count > 272 ||
1737 omega_gpu_cmdlist_owner(cl, &owner) || owner.count > 4096) return -1;
1738 for (unsigned i = 0; i < 4096; ++i) roles[i] = 8;
1739 /* One traversal pairs code and mode-final launch leaves for the SAME actual
1740 * entry. Repetition is significant; no deduplication or singleton shortcut. */
1741 for (uint32_t i = 0; i < owner.count; ++i) {
1742 OmegaGpuCmdListEntry e;
1743 const OmegaGpuQ36MoeCode *actual = NULL;
1744 if (omega_gpu_cmdlist_entry(cl, i, &e)) return -1;
1745 for (uint32_t j = 0; j < p->code_count; ++j) {
1746 const OmegaGpuQ36MoeCode *c = &p->code[j];
1747 if (c->code.va == e.code_va && (c->ordinal == UINT64_MAX || c->ordinal == e.ordinal)) {
1748 if (actual) return -1; /* ambiguous owner projection */
1749 actual = c;
1750 }
1751 }
1752 if (!actual) continue;
1753 uint64_t role = actual->role, bytes = actual->code_bytes;
1754 if (role >= 8 || !actual->code.cpu || bytes > sizeof code ||
1755 bytes > actual->code.size) return -1;
1756 roles[i] = role;
1757 uint64_t slot = counts[role]++;
1758 if (slot >= 128) return -1; /* bounded collector storage, policy is .om */
1759 uint64_t meta[32] = {1,159,1,role,e.n_args,bytes,e.ordinal,e.record_generation,
1760 e.independent,e.pending_wait,e.code_va};
1761 uint64_t submit[16] = {1,159,4,owner.record_generation,consumed->options.mode,
1762 consumed->options.barrier,consumed->timeout_ms,consumed->spin_us,
1763 (uint64_t)consumed->options.stamps,(uint64_t)consumed->options.skip_launch,
1764 (uint64_t)consumed->options.first_barrier_none,owner.count};
1765 if (role >= 6) {
1766 if (!e.intent_seal) return -1;
1767 meta[11] = owner.qmds.va + (uint64_t)e.ordinal * OMEGA_GPU_CMDLIST_QMD_BYTES;
1768 if (omega_native_mcintent(&result, meta, 32, e.bank, 1024, e.qmd_slot, 1024,
1769 submit, 16, e.intent_seal, 32) != OMEGA_OK || result) return -1;
1770 continue;
1771 }
1772 memset(code, 0, sizeof code); memcpy(code, actual->code.cpu, bytes);
1773 if (omega_native_mccode(&result, code, 16384, bytes, codes[role] + slot * 32, 32) != OMEGA_OK || result ||
1774 omega_native_mclaunch(&result, meta, 32, e.bank, 1024, e.qmd_slot, 1024,
1775 submit, 16, launches[role] + slot * 32, 32) != OMEGA_OK || result) return -1;
1776 ordinals[role][slot] = e.ordinal;
1777 }
1778 const uint64_t facts[4] = {owner.count, owner.record_generation, owner.capacity, 0};
1779 if (omega_native_mccover(&result, p->context, 16, roles, 4096, counts, 8,
1780 facts, 4) != OMEGA_OK || result) return -1;
1781 for (unsigned role = 0; role < 6; ++role) {
1782 uint64_t meta[8] = {1,159,5,role,0,counts[role],0,0}, zero[128] = {0};
1783 if (omega_native_mcseries(&result, meta, 8, codes[role], 4096, zero, 128,
1784 nested + (10 + role * 2) * 32, 32) != OMEGA_OK || result) return -1;
1785 meta[4] = 1; meta[6] = owner.record_generation; meta[7] = owner.count;
1786 if (omega_native_mcseries(&result, meta, 8, launches[role], 4096, ordinals[role], 128,
1787 nested + (11 + role * 2) * 32, 32) != OMEGA_OK || result) return -1;
1788 }
1789 memcpy(p->nested, nested, sizeof nested);
1790 return 0;
1791}
1792
1793static int q36_moe_nested(OmegaGpuQ36Graph *g, const OmegaGpuCmdList *cl,
1794 const OmegaGpuCmdListConsumed *consumed, uint8_t nested[2048],
1795 OmegaGpuQ36RouterObservation *router_out) {
1796 Q36MoeObservation *m = g ? g->moe : NULL;
1797 OmegaGpuCmdListOwner owner;
1798 OmegaGpuQ36MoeProjection p = {0};
1799 OmegaGpuQ36RouterObservation router = {0};
1800 if (!m || omega_gpu_cmdlist_owner(cl, &owner)) return -1;
1801 const uint64_t context[16] = {1,159,6,m->tokens,m->meta[0],OMEGA_GPU_ROUTER_MAX_ROWS,
1802 m->layer + 1 == g->cfg.layers,(uint64_t)q36_cn_rows(),owner.count,
1803 owner.record_generation,g->lo.lb_stride};
1804 memcpy(p.context, context, sizeof context);
1805 const int ki[7] = {K_GROUP,K_PERM_XO,g->mutant == 1 ? K_SW_M1 : K_SW,
1806 K_PERM_ACT,g->mutant == 4 ? K_CN_M3 : K_CN,K_CN_R,K_CN_R_M3};
1807 const unsigned role[7] = {0,2,3,4,5,5,5};
1808 for (unsigned i = 0; i < 7; ++i)
1809 p.code[p.code_count++] = (OmegaGpuQ36MoeCode){g->k[ki[i]].code,
1810 g->k[ki[i]].code_bytes,role[i],UINT64_MAX};
1811 m->router_code_count = 0;
1812 for (uint32_t i = 0; i < owner.count; ++i) {
1813 OmegaGpuCmdListEntry e;
1814 OmegaGpuQ36RouterObservation r;
1815 if (omega_gpu_cmdlist_entry(cl, i, &e)) return -1;
1816 if (omega_gpu_q36_router_observe(g->cfg.router, m->layer, e.code_va, &r)) continue;
1817 router = r;
1818 unsigned j;
1819 for (j = 0; j < p.code_count; ++j)
1820 if (p.code[j].code.va == r.code.va) break;
1821 if (j == p.code_count) {
1822 if (p.code_count >= 272) return -1;
1823 if (m->router_code_count >= 2) return -1;
1824 m->router_code[m->router_code_count] = r.code;
1825 m->router_code_generation[m->router_code_count++] = r.code_generation;
1826 p.code[p.code_count++] = (OmegaGpuQ36MoeCode){r.code,r.code_bytes,1,UINT64_MAX};
1827 }
1828 }
1829 for (uint64_t w = 0; w < m->meta[0]; ++w)
1830 for (unsigned r = 0; r < 2; ++r) {
1831 if (p.code_count >= 272) return -1;
1832 p.code[p.code_count++] = (OmegaGpuQ36MoeCode){m->pair[w].code[r],
1833 m->pair[w].artifact[r]->sizes[1],6 + r,m->pair[w].ordinal[r]};
1834 }
1835 if (omega_gpu_q36_moe_project(cl, consumed, &p)) return -1;
1836 if (router.entries != N_EXPERTS || !router.gu_directory || !router.dn_directory) return -1;
1837 memcpy(nested, p.nested, sizeof p.nested);
1838 *router_out = router;
1839 return 0;
1840}
1841
1842static int q36_moe_observe(const OmegaGpuCmdList *cl,
1843 const OmegaGpuCmdListConsumed *consumed, void *opaque) {
1844 OmegaGpuQ36Graph *g = opaque;
1845 Q36MoeObservation *m = g ? g->moe : NULL;
1846 OmegaGpuCmdListOwner owner;
1847 OmegaGpuQ36RouterObservation router;
1848 uint8_t nested[2048] = {0};
1849 uint64_t result = 0;
1850 if (!m || omega_gpu_cmdlist_owner(cl, &owner) ||
1851 q36_moe_nested(g, cl, consumed, nested, &router)) return -1;
1852 OmegaGpuNativeTensorObservation tensor[2];
1853 if (omega_gpu_fp8_native_observe(g->layer[m->layer].gu, &tensor[0]) ||
1854 omega_gpu_fp8_native_observe(g->layer[m->layer].dn, &tensor[1])) return -1;
1855 uint64_t directory[2][1024] = {{0}};
1856 for (unsigned r = 0; r < 2; ++r) {
1857 const OmegaGpuFp8Tensor *t = r ? g->layer[m->layer].dn : g->layer[m->layer].gu;
1858 if (tensor[r].experts != N_EXPERTS || tensor[r].allocation_epoch != omega_gpu_session_epoch()) return -1;
1859 for (unsigned i = 0; i < N_EXPERTS; ++i)
1860 if (omega_gpu_fp8_tensor_entry(t, i, &directory[r][i * 2], &directory[r][i * 2 + 1])) return -1;
1861 if (memcmp(directory[r], r ? router.dn_directory : router.gu_directory,
1862 N_EXPERTS * 2u * sizeof(uint64_t))) return -1;
1863 uint64_t meta[8] = {1,159,2,r,N_EXPERTS,g->tensor_pair_generation[m->layer],m->layer,0};
1864 if (omega_native_mcdir(&result, meta, 8, directory[r], 1024,
1865 nested + (45 + r) * 32, 32) != OMEGA_OK || result) return -1;
1866 }
1867 uint64_t s[128] = {2,159,11264,m->meta[0],0,m->tokens,0,0,0,H,EI,256,8,NG,256,128,
1868 g->lo.lb_stride,omega_gpu_session_epoch(),121,g->pf_generation,g->owner_generation,
1869 m->layer,g->tensor_pair_generation[m->layer],0};
1870 for (unsigned i = 24; i < 32; ++i) s[i] = 1;
1871 for (unsigned i = 0; i < NG; ++i) s[64 + i] = i;
1872 s[73] = (uint64_t)m->tokens * NG + PF_PAD; s[74] = 128; s[75] = g->flush_each;
1873 s[76] = g->head8 != NULL; s[77] = g->cfg.layers; s[78] = m->layer + 1 == g->cfg.layers; s[79] = 1;
1874 s[112] = 1; s[113] = 2; s[114] = 2; s[115] = 2; s[116] = PF_PAD;
1875 for (unsigned i = 117; i < 122; ++i) s[i] = 1;
1876 uint64_t alloc[1024] = {0};
1877 const uint64_t va[16] = {m->router.ids_va,m->router.wts_va,m->router.tab_gu_va,m->router.tab_dn_va,
1878 pf_h(g,g->pf.sgu),pf_h(g,g->pf.sdn),pf_h(g,g->pf.xmap),pf_h(g,g->pf.ymap),
1879 pf_d(g,g->pf.xp2),pf_d(g,g->pf.guN),pf_d(g,g->pf.actN),pf_d(g,g->pf.actp),
1880 pf_d(g,g->pf.dnN),pf_h(g,g->pf.x),tensor[0].weights.va,tensor[1].weights.va};
1881 const uint64_t stride[16] = {g->lo.lb_stride,g->lo.lb_stride,g->lo.lb_stride,g->lo.lb_stride,32,32,4,4,
1882 H*4u,NG*EI*8u,NG*EI*4u,NG*EI*4u,NG*H*4u,H*4u,
1883 directory[0][2]-directory[0][0],directory[1][2]-directory[1][0]};
1884 for (unsigned i = 0; i < 16; ++i) {
1885 const OmegaGpuMem *mem = i >= 14 ? &tensor[i - 14].weights :
1886 i >= 8 && i <= 12 ? &g->pf.db : &g->pf.hb;
1887 if (q36_moe_resource(s, alloc, i, mem, va[i],
1888 i >= 14 ? tensor[i - 14].owner_generation : s[19], stride[i])) return -1;
1889 /* Scalar roles14/15 identify the actual graph-owned pair association;
1890 * raw allocation records independently retain each tensor owner's life. */
1891 if (i >= 14) s[80 + (i - 8) * 4 + 2] = s[22];
1892 }
1893 unsigned nr = 16;
1894 const OmegaGpuMem *extra[] = {&tensor[0].scales,&tensor[1].scales,&g->buf,&g->pf.hb,&g->pf.db,
1895 &router.gate,&router.norm,&router.directory,&router.launch_buffer,
1896 &owner.banks,&owner.qmds,&owner.semaphores,&owner.scratch,&owner.stamps};
1897 for (unsigned i = 0; i < sizeof extra / sizeof extra[0]; ++i) {
1898 if (!extra[i]->cpu) {
1899 if (i + 1 == sizeof extra / sizeof extra[0]) continue; /* optional stamps */
1900 return -1;
1901 }
1902 if (q36_moe_resource(s, alloc, nr++, extra[i], extra[i]->va,
1903 i < 2 ? tensor[i].owner_generation : i == 2 ? s[20] :
1904 i >= 5 && i <= 8 ? router.owner_generation :
1905 i >= 9 ? owner.record_generation : s[19], 0)) return -1;
1906 }
1907 const int code_k[7] = {K_GROUP,K_PERM_XO,g->mutant == 1 ? K_SW_M1 : K_SW,
1908 K_PERM_ACT,g->mutant == 4 ? K_CN_M3 : K_CN,K_CN_R,K_CN_R_M3};
1909 for (unsigned i = 0; i < 7; ++i)
1910 if (q36_moe_resource(s, alloc, nr++, &g->k[code_k[i]].code,
1911 g->k[code_k[i]].code.va, g->owner_generation, 0)) return -1;
1912 for (unsigned i = 0; i < m->router_code_count; ++i)
1913 if (q36_moe_resource(s, alloc, nr++, &m->router_code[i], m->router_code[i].va,
1914 m->router_code_generation[i], 0)) return -1;
1915 for (uint64_t w = 0; w < m->meta[0]; ++w)
1916 for (unsigned r = 0; r < 2; ++r)
1917 if (q36_moe_resource(s, alloc, nr++, &m->pair[w].code[r],
1918 m->pair[w].code[r].va, m->pair[w].lease[r].revision, 0)) return -1;
1919 uint64_t ameta[8] = {1,159,3,nr,s[19],s[22],s[20],0};
1920 if (omega_native_mcalloc(&result, ameta, 8, alloc, 1024, nested + 47 * 32, 32) != OMEGA_OK || result) return -1;
1921 for (uint64_t w = 0; w < m->meta[0]; ++w) {
1922 const Q36MoePair *p = &m->pair[w];
1923 uint8_t hashes[2048], wire[16384] = {0}, bound[2][32] = {{0}};
1924 memcpy(hashes, p->state[0]->provenance->hashes, sizeof hashes);
1925 memcpy(hashes + 10 * 32, nested + 10 * 32, 12 * 32);
1926 memcpy(hashes + 45 * 32, nested + 45 * 32, 3 * 32);
1927 s[4] = w; s[6] = m->windows[w * 8]; s[7] = m->windows[w * 8 + 1]; s[8] = m->windows[w * 8 + 2];
1928 s[122] = p->artifact[0]->sizes[1]; s[123] = p->artifact[1]->sizes[1];
1929 if (omega_native_mcpack(&result, s, 128, hashes, 2048, m->windows, 1024,
1930 wire, 16384) != OMEGA_OK || result) return -1;
1931 for (unsigned r = 0; r < 2; ++r) {
1932 const OmegaGpuNativeArtifact *a = p->artifact[r];
1933 if (omega_native_bound(a, &p->lease[r]) ||
1934 omega_native_mcbind(&result, p->state[r]->expected_closure, 16384, wire, 16384,
1935 p->state[r]->query, 64, p->row[r], 64, r, bound[r], 32) != OMEGA_OK || result ||
1936 omega_native_mcblob(&result, wire, 16384, a->canonical, 4096, a->code, 16384,
1937 a->sizes[1], a->launch, 256, r) != OMEGA_OK || result) return -1;
1938 }
1939 if (memcmp(bound[0], bound[1], 32)) return -1;
1940 }
1941 return 0;
1942}
1943
1944int omega_gpu_q36_graph_prefill(OmegaGpuQ36Graph *g, uint32_t head, const float *x_embed, uint32_t n, uint32_t pos0, int want_token,
1945 uint32_t *token, OmegaGpuDecodeGraphStats *st) {
1946 if (!g || g->in_token || !x_embed || n == 0 || head >= g->heads) return OMEGA_GPU_DGRAPH_BAD_ARGS;
1947 if (n > g->pf.cap) return OMEGA_GPU_DGRAPH_BAD_ARGS;
1948 if (!g->gdn_h[head] || !g->attn_h[head]) return OMEGA_GPU_DGRAPH_BAD_ARGS;
1949 for (uint32_t l = 0; l < g->cfg.layers; l++) if (!g->layer[l].set) return OMEGA_GPU_DGRAPH_BAD_ARGS;
1950 if (!g->final_set) return OMEGA_GPU_DGRAPH_BAD_ARGS;
1951 omega_gpu_session_lock();
1952 g->t_begin = omega_gpu_session_now_ns();
1953 g->in_token = 1; g->failed = 0; g->stroke_heads = head + 1u;
1954 for (uint32_t h = 0; h <= head; h++) { g->xq_layer[h] = -1; g->hidden_ready[h] = 0; }
1955 memset(&g->acc, 0, sizeof g->acc);
1956 g->n_kind = 0; g->tagged_in_list = 0; g->n_stamp = 0;
1957 omega_gpu_cmdlist_reset(g->cl);
1958 int rc = OMEGA_GPU_DGRAPH_OK;
1959 if (!omega_gpu_session_open()) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL);
1960 if (rc == OMEGA_GPU_DGRAPH_OK) {
1961 uint32_t *tok = cpu_of(g, token_of(g, head)); tok[0] = TOKEN_POISON; tok[1] = TOKEN_POISON;
1962 memcpy(pf_hc(g, g->pf.x), x_embed, (size_t)n * H * 4u);
1963 write_dense_segments(g, n);
1964 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);
1965 }
1966 for (uint32_t l = 0; l < g->cfg.layers && rc == OMEGA_GPU_DGRAPH_OK; l++) {
1967 uint32_t ids_off = 0, wts_off = 0, xo_off = 0;
1968 Q36MoeObservation *m = NULL;
1969 int selection = q36_moe_select(g, head, l, n, &m);
1970 if (selection < 0) {
1971 rc = fail(g, "q36 native MoE: complete independently retained pair refused", OMEGA_GPU_DGRAPH_CHIP_FAIL);
1972 omega_gpu_cmdlist_reset(g->cl);
1973 break;
1974 }
1975 if (selection == 0) {
1976 g->moe = m;
1977 rc = pf_record_mixer(g, head, l, n, pos0, &ids_off, &wts_off, &xo_off, &m->router);
1978 for (uint64_t w = 0; w < m->meta[0] && !rc; ++w) rc = pf_record_moe_window(g, w);
1979 if (!rc) rc = pf_record_moe_combine(g, wts_off);
1980 if (!rc) rc = submit_now(g);
1981 else omega_gpu_cmdlist_reset(g->cl);
1982 g->moe = NULL;
1983 q36_moe_release(m);
1984 if (rc) rc = fail(g, "q36 native MoE: recorded instance binding refused, no fallback", OMEGA_GPU_DGRAPH_CHIP_FAIL);
1985 continue;
1986 }
1987 rc = pf_record_mixer(g, head, l, n, pos0, &ids_off, &wts_off, &xo_off, NULL);
1988 if (rc == OMEGA_GPU_DGRAPH_OK) rc = submit_now(g);
1989 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);
1990 uint32_t segs = 0;
1991 if (rc == OMEGA_GPU_DGRAPH_OK) {
1992 /* the dense trunk: mix (or q|k|v) and o, 8 rows per pass */
1993 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);
1994 segs = pf_group(g, l, n, ids_off);
1995 if (segs == 0 || segs > 256u) rc = pf_fail(g, "q36 prefill: router picks out of range");
1996 }
1997 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);
1998 if (rc == OMEGA_GPU_DGRAPH_OK) rc = pf_record_experts(g, head, l, n, segs, wts_off, xo_off);
1999 if (rc == OMEGA_GPU_DGRAPH_OK && l + 1 < g->cfg.layers) rc = submit_now(g);
2000 }
2001 if (rc == OMEGA_GPU_DGRAPH_OK) {
2002 g->hidden_ready[head] = 1;
2003 if (want_token) {
2004 rc = omega_gpu_q36_graph_record_head_final(g, head);
2005 }
2006 }
2007 if (rc == OMEGA_GPU_DGRAPH_OK) rc = submit_now(g);
2008 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);
2009 if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_session_sync_to_cpu(&g->buf) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL);
2010 if (rc == OMEGA_GPU_DGRAPH_OK && want_token) {
2011 const uint32_t t = ((const uint32_t *)cpu_of(g, token_of(g, head)))[0];
2012 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);
2013 else if (token) *token = t;
2014 }
2015 omega_gpu_cmdlist_reset(g->cl);
2016 g->in_token = 0;
2017 const uint64_t t_end = omega_gpu_session_now_ns();
2018 if (st) { *st = g->acc; st->record_ns = t_end - g->t_begin; st->total_ns = t_end - g->t_begin; }
2019 omega_gpu_session_unlock();
2020 return rc;
2021}
2022
2023#ifdef CUDAPP_NATIVE_RUNTIME_TEST
2024/* Existing fake typed-session oracle only: run the actual static callers.
2025 * No setter, native admission or test symbol exists in production builds. */
2026int omega_gpu_q36_test_submit_incumbent(OmegaGpuCmdList *cl) {
2027 OmegaGpuQ36Graph g = {0}; g.cl = cl;
2028 return submit_now(&g);
2029}
2030int omega_gpu_q36_test_record_fp8(OmegaGpuCmdList *cl, OmegaGpuQ36NativeState *state,
2031 const OmegaGpuFp8Tensor *tensor, uint64_t tab, uint64_t x, uint64_t y) {
2032 OmegaGpuQ36NativeGraph native = {.keys = {OMEGA_GPU_Q36_K_MIX,0,0,0},
2033 .count = 1, .context = state};
2034 OmegaGpuQ36Graph g = {0}; g.cl = cl; g.native = &native;
2035 return rec_fp8(&g, OMEGA_GPU_Q36_K_MIX, tensor, tab, x, y);
2036}
2037#endif