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_decode_graph.c

551 lines · 33701 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/*
3 * omega_gpu_decode_graph.c: one decode token as one command list (see the header). Records the four decode APIs'
4 * *_record launches and the omega_blackwell_codegen_dec glue kernels into an omega_gpu_cmdlist. Buffers:
5 * omega_gpu_session_alloc only (GPU-uncached, nvos.h:1117); the host touches x (before), token and logits (after).
6 */
7#include "omega_gpu_decode_graph.h"
8#include "omega_gpu_cmdlist.h"
9#include "omega_blackwell_codegen_dec.h"
10#include "omega_gpu_session.h"
11#include "omega_gpu_fmt.h"
12#include "omega_gpu_graph_om.h"
13#include "omega_gpu_graph_residue.h"
14#include <stdlib.h>
15#include <string.h>
16
17#define TIMEOUT_MS 20000ull
18#define SCALE_2P64 18446744073709551616.0f
19#define NEG_PAD (-1e30f)
20#define TOKEN_POISON 0xffffffffu
21
22/* glue kernels */
23enum { K_NORM, K_ADD, K_CN, K_SW, K_AM0, K_AM1, K_SW_M1, K_CN_M2, K_COUNT };
24
25typedef struct { OmegaGpuMem code; uint32_t gpr; } Kern;
26
27/* the one buffer: byte offsets, every region 256-byte aligned (RES/SWIGLU load 16-byte vectors) */
28typedef struct {
29 size_t x, xq, qkv, attn, o, gu, act, dn, hidden, logits, am_v, am_i, token, norms, fnorm, tabs, pf_x, pf_y, bytes;
30} Layout;
31
32struct OmegaGpuDecodeGraph {
33 OmegaGpuDecodeGraphConfig cfg;
34 uint32_t am_ctas; /* stage-1 ARGMAX CTAs = ceil(vocab / 2048) */
35 Layout lo;
36 OmegaGpuMem buf;
37 Kern k[K_COUNT];
38 OmegaGpuCmdList *cl;
39 struct { const OmegaGpuFp8Tensor *qkv, *o, *gu, *dn; int set; } layer[OMEGA_GPU_DGRAPH_MAX_LAYERS];
40 int final_set;
41 /* token state */
42 int in_token, failed;
43 int xq_layer; /* layer whose input-norm x' sits in xq, -1 none */
44 int hidden_ready;
45 uint64_t t_begin;
46 OmegaGpuDecodeGraphStats acc;
47 int flush_each, mutant;
48 /* timeline (set_stamps): one kind per recorded launch and the GPU timestamps around every launch of the last token */
49 int stamps;
50 uint32_t n_kind, tagged_in_list, n_stamp;
51 uint8_t kind[OMEGA_GPU_CMDLIST_MAX_LAUNCHES];
52 uint64_t stamp[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1];
53};
54
55#ifdef CUDAPP_MIGRATION_PENDING_C_GRAPHS
56static size_t al(size_t v) { return omega_graph_addr(0,v,0,0,0,0,0); }
57#endif
58static uint64_t va_of(const OmegaGpuDecodeGraph *g, size_t off) { return omega_graph_addr(2,g->buf.va,0,0,0,off,1); }
59static void *cpu_of(const OmegaGpuDecodeGraph *g, size_t off) { return (uint8_t *)g->buf.cpu + off; }
60#ifdef CUDAPP_MIGRATION_PENDING_C_GRAPHS
61static 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); }
62static void putf(uint32_t *a, unsigned off, float f) { memcpy(&a[(off - 0x380) / 4], &f, 4); }
63#endif
64static int fail(OmegaGpuDecodeGraph *g, const char *what, int rc) {
65 if (g) g->failed = 1;
66 if (what) omega_gpu_session_set_error(what);
67 return rc;
68}
69
70/* lock held */
71static int kern_build(Kern *k, OmegaDecKind kind, uint32_t mode, int mutant) {
72 OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = kind; s.mode = mode; s.mutant = mutant;
73 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog);
74 if (!prog) return OMEGA_GPU_DGRAPH_CODEGEN_FAIL;
75 int rc = OMEGA_GPU_DGRAPH_OK;
76 size_t need = 0;
77 if (omega_blackwell_codegen_dec_prog(&s, prog) != 0 || (need = omega_bw_program_code_bytes(prog)) == 0) rc = OMEGA_GPU_DGRAPH_CODEGEN_FAIL;
78 else if (omega_gpu_session_alloc_code(need, &k->code) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL;
79 else {
80 OmegaBlackwellKernel kk;
81 if (omega_blackwell_kernel_encode_into(prog, k->code.cpu, need, &kk) != 0 || kk.code_size != need) rc = OMEGA_GPU_DGRAPH_CODEGEN_FAIL;
82 else if (omega_gpu_session_sync_to_gpu(&k->code) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL;
83 else { k->gpr = omega_bw_gpr_declared(kk.gpr_count); if (k->gpr < 64u) k->gpr = 64u; } /* as tests/gpu_cmdlist_test.c (chip PASS) */
84 if (rc != OMEGA_GPU_DGRAPH_OK) { omega_gpu_session_free(&k->code); memset(k, 0, sizeof *k); }
85 }
86 omega_gpu_host_free(prog);
87 return rc;
88}
89
90void omega_gpu_decode_graph_free(OmegaGpuDecodeGraph *g) {
91 if (!g) return;
92 if (g->cl) omega_gpu_cmdlist_free(g->cl);
93 omega_gpu_session_lock();
94 for (unsigned i = 0; i < K_COUNT; i++) if (g->k[i].code.cpu) omega_gpu_session_free(&g->k[i].code);
95 if (g->buf.cpu) omega_gpu_session_free(&g->buf);
96 omega_gpu_session_unlock();
97 omega_gpu_host_free(g);
98}
99
100int omega_gpu_decode_graph_create(const OmegaGpuDecodeGraphConfig *cfg, OmegaGpuDecodeGraph **out) {
101 if (out) *out = NULL;
102 if (!cfg || !out || !cfg->router || !cfg->attn || !cfg->lm_head) return OMEGA_GPU_DGRAPH_BAD_ARGS;
103 const uint32_t ctas = omega_graph_config(cfg->layers, OMEGA_GPU_DGRAPH_MAX_LAYERS, cfg->vocab,
104 OMEGA_DEC_ARGMAX_SPAN, 1, 1, 0, 0);
105 if (!ctas) return OMEGA_GPU_DGRAPH_BAD_ARGS;
106 uint32_t n = 0, k = 0;
107 omega_gpu_bf16_tensor_shape(cfg->lm_head, &n, &k);
108 if (!omega_graph_shape(1, n, k, 1, cfg->vocab, OMEGA_GPU_DGRAPH_HIDDEN)) return OMEGA_GPU_DGRAPH_BAD_ARGS;
109 OmegaGpuDecodeGraph *g = omega_gpu_host_calloc(1, sizeof *g);
110 if (!g) return OMEGA_GPU_DGRAPH_BAD_ARGS;
111 g->cfg = *cfg;
112 g->am_ctas = ctas;
113 g->xq_layer = -1;
114 const size_t H = OMEGA_GPU_DGRAPH_HIDDEN, TK = OMEGA_GPU_DGRAPH_TOPK, I = OMEGA_GPU_DGRAPH_INTER;
115 Layout *L = &g->lo;
116#ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS
117 const uint64_t lc[32]={0,1,cfg->layers,g->am_ctas,H,I,TK,OMEGA_GPU_DGRAPH_QKV,OMEGA_GPU_DGRAPH_ATTN,0,0,0,0,0,0,OMEGA_DEC_ARGMAX_SPAN};
118 uint64_t ld[64],lr=0;
119 if(omega_mg_layout(&lr,lc,32,ld,64)!=OMEGA_OK||lr!=1){omega_gpu_host_free(g);return OMEGA_GPU_DGRAPH_BAD_ARGS;}
120#define FIELD(f,i) L->f=ld[i]
121 FIELD(x,0);FIELD(xq,1);FIELD(qkv,2);FIELD(attn,3);FIELD(o,4);FIELD(gu,5);FIELD(act,6);FIELD(dn,7);FIELD(hidden,8);
122 FIELD(logits,9);FIELD(am_v,10);FIELD(am_i,11);FIELD(token,12);FIELD(norms,13);FIELD(fnorm,14);FIELD(tabs,15);FIELD(pf_x,16);FIELD(pf_y,17);FIELD(bytes,61);
123#undef FIELD
124#else
125 size_t o = 0;
126#define REG(f, bytes) do { L->f = o; o = al(o + (bytes)); } while (0)
127 REG(x, H * 4); REG(xq, H * 4); REG(qkv, OMEGA_GPU_DGRAPH_QKV * 4); REG(attn, OMEGA_GPU_DGRAPH_ATTN * 4); REG(o, H * 4);
128 REG(gu, TK * 2 * I * 4); REG(act, TK * I * 4); REG(dn, TK * H * 4); REG(hidden, H * 4);
129 REG(logits, (size_t)g->am_ctas * OMEGA_DEC_ARGMAX_SPAN * 4); REG(am_v, OMEGA_DEC_ARGMAX_SPAN * 4); REG(am_i, OMEGA_DEC_ARGMAX_SPAN * 4);
130 REG(token, 16); REG(norms, (size_t)cfg->layers * H * 4); REG(fnorm, H * 4); REG(tabs, (size_t)cfg->layers * 2 * 16);
131 REG(pf_x, OMEGA_GPU_DGRAPH_ATTN * 4); REG(pf_y, H * 4); /* prefetch launch (W-L2d): x read by nothing else, y read by nothing */
132#undef REG
133 L->bytes = o;
134#endif
135 int rc = OMEGA_GPU_DGRAPH_OK;
136 if (omega_gpu_cmdlist_create(OMEGA_GPU_CMDLIST_MAX_LAUNCHES, &g->cl) != 0) { omega_gpu_decode_graph_free(g); return OMEGA_GPU_DGRAPH_CHIP_FAIL; }
137 omega_gpu_session_lock();
138 if (!omega_gpu_session_open() || omega_gpu_session_alloc(L->bytes, &g->buf) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL;
139 static const struct { OmegaDecKind kind; uint32_t mode; int mutant; } spec[K_COUNT] = {
140 [K_NORM] = { OMEGA_DEC_RES, OMEGA_DEC_RES_NORM, 0 },
141 [K_ADD] = { OMEGA_DEC_RES, OMEGA_DEC_RES_ADD, 0 },
142 [K_CN] = { OMEGA_DEC_RES, OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM, 0 },
143 [K_SW] = { OMEGA_DEC_SWIGLU, 0, 0 },
144 [K_AM0] = { OMEGA_DEC_ARGMAX, 0, 0 },
145 [K_AM1] = { OMEGA_DEC_ARGMAX, 1, 0 },
146 [K_SW_M1] = { OMEGA_DEC_SWIGLU, 0, 1 },
147 [K_CN_M2] = { OMEGA_DEC_RES, OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM, 2 },
148 };
149 for (unsigned i = 0; i < K_COUNT && rc == OMEGA_GPU_DGRAPH_OK; i++) rc = kern_build(&g->k[i], spec[i].kind, spec[i].mode, spec[i].mutant);
150 if (rc == OMEGA_GPU_DGRAPH_OK) {
151 memset(g->buf.cpu, 0, L->bytes);
152 float *lg = cpu_of(g, L->logits), *amv = cpu_of(g, L->am_v);
153 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 */
154 for (size_t i = 0; i < OMEGA_DEC_ARGMAX_SPAN; i++) amv[i] = NEG_PAD; /* stage 1 writes [0, ctas) only */
155 if (omega_gpu_session_sync_to_gpu(&g->buf) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL;
156 }
157 omega_gpu_session_unlock();
158 if (rc != OMEGA_GPU_DGRAPH_OK) { omega_gpu_decode_graph_free(g); return rc; }
159 *out = g;
160 return OMEGA_GPU_DGRAPH_OK;
161}
162
163static int shape_is(const OmegaGpuFp8Tensor *t, uint32_t e, uint32_t n, uint32_t k) {
164 uint32_t te = 0, tn = 0, tk = 0; omega_gpu_fp8_tensor_shape(t, &te, &tn, &tk);
165 return t && omega_graph_shape(te, tn, tk, e, n, k);
166}
167
168int omega_gpu_decode_graph_set_layer(OmegaGpuDecodeGraph *g, uint32_t layer, const OmegaGpuFp8Tensor *qkv, const OmegaGpuFp8Tensor *o,
169 const OmegaGpuFp8Tensor *gate_up, const OmegaGpuFp8Tensor *down, const float *input_norm) {
170 if (!g || layer >= g->cfg.layers || !input_norm || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
171 const uint32_t H = OMEGA_GPU_DGRAPH_HIDDEN, I = OMEGA_GPU_DGRAPH_INTER;
172 if (!shape_is(qkv, 1, OMEGA_GPU_DGRAPH_QKV, H) || !shape_is(o, 1, H, OMEGA_GPU_DGRAPH_ATTN) || !shape_is(gate_up, 128, 2 * I, H) || !shape_is(down, 128, H, I))
173 return OMEGA_GPU_DGRAPH_BAD_ARGS;
174 uint64_t t[4];
175 if (omega_gpu_fp8_tensor_entry(qkv, 0, &t[0], &t[1]) != 0 || omega_gpu_fp8_tensor_entry(o, 0, &t[2], &t[3]) != 0) return OMEGA_GPU_DGRAPH_BAD_ARGS;
176 omega_gpu_session_lock();
177 memcpy((uint8_t *)cpu_of(g, g->lo.tabs) + (size_t)layer * 32u, t, sizeof t);
178 memcpy((float *)cpu_of(g, g->lo.norms) + (size_t)layer * H, input_norm, H * 4u);
179 int rc = omega_gpu_session_sync_to_gpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL;
180 omega_gpu_session_unlock();
181 if (rc == OMEGA_GPU_DGRAPH_OK) { g->layer[layer].qkv = qkv; g->layer[layer].o = o; g->layer[layer].gu = gate_up; g->layer[layer].dn = down; g->layer[layer].set = 1; }
182 return rc;
183}
184
185int omega_gpu_decode_graph_set_final_norm(OmegaGpuDecodeGraph *g, const float *final_norm) {
186 if (!g || !final_norm || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
187 omega_gpu_session_lock();
188 memcpy(cpu_of(g, g->lo.fnorm), final_norm, OMEGA_GPU_DGRAPH_HIDDEN * 4u);
189 int rc = omega_gpu_session_sync_to_gpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL;
190 omega_gpu_session_unlock();
191 if (rc == OMEGA_GPU_DGRAPH_OK) g->final_set = 1;
192 return rc;
193}
194
195OmegaGpuCmdList *omega_gpu_decode_graph_list(OmegaGpuDecodeGraph *g) { return g ? g->cl : NULL; }
196void omega_gpu_decode_graph_set_flush_each(OmegaGpuDecodeGraph *g, int on) { if (g) g->flush_each = on ? 1 : 0; }
197void omega_gpu_decode_graph_test_set_mutant(OmegaGpuDecodeGraph *g, int mutant) { if (g) g->mutant = mutant; }
198
199void omega_gpu_decode_graph_vas(const OmegaGpuDecodeGraph *g, OmegaGpuDecodeGraphVas *v) {
200 if (!v) return;
201 memset(v, 0, sizeof *v);
202 if (!g) return;
203 v->x = va_of(g, g->lo.x); v->xq = va_of(g, g->lo.xq); v->qkv = va_of(g, g->lo.qkv); v->attn = va_of(g, g->lo.attn); v->o = va_of(g, g->lo.o);
204 v->gu = va_of(g, g->lo.gu); v->act = va_of(g, g->lo.act); v->dn = va_of(g, g->lo.dn); v->hidden = va_of(g, g->lo.hidden);
205 v->logits = va_of(g, g->lo.logits); v->token = va_of(g, g->lo.token);
206}
207
208/* ---- the list ---- */
209
210/* barrier between the launches of a token (W-L2c launch-cost study): omega_gpu_decode_graph_set_barrier, else env
211 * AIEN_OMEGA_DGRAPH_BARRIER (0 wfi+inval_all = default, 2 wfi_only, 3 wfi+inval_data, 4 flush_data; 1 none is refused: the
212 * token is a dependency chain). Read once. */
213static int g_barrier = -1;
214void omega_gpu_decode_graph_set_barrier(int b) {
215#if CUDAPP_MIGRATION_PENDING_GRAPH_RESIDUE
216 g_barrier = (b >= 0 && b <= 4 && b != 1) ? b : 0;
217#else
218 const uint64_t c[16] = {(uint64_t)(int64_t)b}; uint64_t d[16];
219 g_barrier = (int)omega_graph_meta(3, c, d);
220#endif
221}
222int omega_gpu_decode_graph_barrier(void) {
223 if (g_barrier < 0) { const char *e = getenv("AIEN_OMEGA_DGRAPH_BARRIER"); omega_gpu_decode_graph_set_barrier(e ? atoi(e) : 0); }
224 return g_barrier;
225}
226void omega_gpu_decode_graph_test_set_barrier_none(void) { g_barrier = 1; }
227/* weight prefetch (W-L2d): 1 = beside each layer's attention, an independent launch (no barrier before it) runs the o_proj GEMV
228 * on a scratch x into a scratch y, so the o_proj weights stream from DRAM while the latency-bound attention runs and the real
229 * o_proj then reads them from L2 (needs GPU-cached weights: an uncached mapping is never kept in L2, gpu_l2_reuse_test r2).
230 * omega_gpu_decode_graph_set_prefetch, else env AIEN_OMEGA_DGRAPH_PREFETCH ("1"). Read once. Default 0. */
231static int g_prefetch = -1, g_prefetch_red;
232void omega_gpu_decode_graph_set_prefetch(int on) { g_prefetch = on ? 1 : 0; }
233int omega_gpu_decode_graph_prefetch(void) {
234 if (g_prefetch < 0) { const char *e = getenv("AIEN_OMEGA_DGRAPH_PREFETCH"); g_prefetch = (e && e[0] == '1' && e[1] == 0) ? 1 : 0; }
235 return g_prefetch;
236}
237void omega_gpu_decode_graph_test_set_prefetch_red(int on) { g_prefetch_red = on ? 1 : 0; }
238static OmegaGpuCmdListBarrier barrier_now(void) { return (OmegaGpuCmdListBarrier)omega_gpu_decode_graph_barrier(); }
239
240static OmegaGpuCmdListOpts list_opts(const OmegaGpuDecodeGraph *g) {
241 OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o);
242 o.mode = OMEGA_GPU_CMDLIST_PCAS; o.barrier = barrier_now(); o.timeout_ms = TIMEOUT_MS; o.stamps = g->stamps;
243 return o;
244}
245
246static int submit_now(OmegaGpuDecodeGraph *g) {
247 if (omega_gpu_cmdlist_count(g->cl) == 0) return OMEGA_GPU_DGRAPH_OK;
248 OmegaGpuCmdListOpts o = list_opts(g);
249 OmegaGpuCmdListStats st; memset(&st, 0, sizeof st);
250 int rc = omega_gpu_cmdlist_submit(g->cl, &o, &st);
251 g->acc.launches += st.launches; g->acc.submits += st.submits; g->acc.chip_ns += st.elapsed_ns; g->acc.sems_done += st.sems_done;
252 if (rc == 0 && g->stamps) { /* before reset: the accessor sizes by the list; later submits (flush_each) drop their start slot */
253 uint64_t tmp[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1];
254 int cnt = omega_gpu_cmdlist_stamps(g->cl, tmp, OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1);
255 if (cnt < 0) rc = -1;
256 else {
257 int from = 0;
258 if (!omega_graph_timeline(g->n_stamp, (uint32_t)cnt, OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1u, &from)) {
259 omega_gpu_session_set_error("decode graph: timeline full (more launches per token than OMEGA_GPU_CMDLIST_MAX_LAUNCHES)");
260 rc = -1;
261 } else {
262 for (int i = from; i < cnt; i++) g->stamp[g->n_stamp++] = tmp[i];
263 }
264 }
265 }
266 g->tagged_in_list = 0;
267 omega_gpu_cmdlist_reset(g->cl);
268 if (rc != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL);
269 if (st.sems_done != st.launches) {
270 char e[128]; omega_gpu_fmt(e, sizeof e, "decode graph: %u of %u launch semaphores DONE (first missing %d)", st.sems_done, st.launches, st.first_missing);
271 return fail(g, e, OMEGA_GPU_DGRAPH_CHIP_FAIL);
272 }
273 return OMEGA_GPU_DGRAPH_OK;
274}
275
276/* kinds for the launches added since the last tag (the timeline) */
277static void tag(OmegaGpuDecodeGraph *g, uint8_t kind) {
278 const uint32_t n = omega_gpu_cmdlist_count(g->cl);
279 for (; g->tagged_in_list < n && g->n_kind < OMEGA_GPU_CMDLIST_MAX_LAUNCHES; g->tagged_in_list++) g->kind[g->n_kind++] = kind;
280}
281
282/* after every recorded launch: flush_each submits it on its own (the comparison path) */
283static int after_op(OmegaGpuDecodeGraph *g, uint8_t kind, int api_rc) {
284 if (api_rc != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL);
285 tag(g, kind);
286 return g->flush_each ? submit_now(g) : OMEGA_GPU_DGRAPH_OK;
287}
288
289static int add_glue(OmegaGpuDecodeGraph *g, uint8_t kind, const Kern *k, uint32_t gx, uint32_t gy, size_t elems, const uint32_t *args, uint32_t n_args) {
290 OmegaGpuLaunch L; memset(&L, 0, sizeof L);
291 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;
292 L.num_elements = elems; L.args = args; L.n_args = n_args; L.timeout_ms = TIMEOUT_MS;
293 return after_op(g, kind, omega_gpu_cmdlist_add(g->cl, &L) < 0 ? -1 : 0);
294}
295
296#ifdef CUDAPP_MIGRATION_PENDING_C_GRAPHS
297static int res_kernel(const OmegaGpuDecodeGraph *g, uint32_t mode) {
298 if (mode == OMEGA_DEC_RES_NORM) return K_NORM;
299 if (mode == OMEGA_DEC_RES_ADD) return K_ADD;
300 if (mode == (OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM)) return g->mutant == 2 ? K_CN_M2 : K_CN;
301 return -1;
302}
303#endif
304
305int omega_gpu_decode_graph_record_res(OmegaGpuDecodeGraph *g, uint32_t mode, uint64_t x_va, uint64_t a_va, uint64_t w_va,
306 uint64_t nw_va, uint64_t y_va, float eps, float scale) {
307 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
308#ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS
309 const uint64_t c[16]={0,mode,g->mutant,OMEGA_GPU_DGRAPH_HIDDEN},v[16]={x_va,a_va,w_va,nw_va,y_va,omega_graph_fbits(eps),omega_graph_fbits(scale)};
310 uint64_t node[64],result=0;uint32_t words[32];
311 if(omega_mg_aux(&result,c,16,v,16,node,64)!=OMEGA_OK||result!=1)return fail(g,"decode graph: RES mode not built (NORM, ADD, COMBINE|NORM)",OMEGA_GPU_DGRAPH_BAD_ARGS);
312 omega_graph_args(node,words);
313 return add_glue(g,node[6],&g->k[node[1]],node[2],node[3],node[4],words,node[5]);
314#else
315 int ki = res_kernel(g, mode);
316 if (ki < 0) return fail(g, "decode graph: RES mode not built (NORM, ADD, COMBINE|NORM)", OMEGA_GPU_DGRAPH_BAD_ARGS);
317 uint32_t a[OMEGA_DEC_RES_WORDS]; memset(a, 0, sizeof a);
318 put64(a, OMEGA_DEC_RES_X, x_va); put64(a, OMEGA_DEC_RES_A, a_va); put64(a, OMEGA_DEC_RES_W, w_va); put64(a, OMEGA_DEC_RES_NW, nw_va);
319 put64(a, OMEGA_DEC_RES_Y, y_va); putf(a, OMEGA_DEC_RES_EPS, eps); putf(a, OMEGA_DEC_RES_SCALE, scale);
320 const uint8_t kind = ki == K_ADD ? OMEGA_GPU_DGRAPH_K_ADD : ki == K_NORM ? OMEGA_GPU_DGRAPH_K_NORM : OMEGA_GPU_DGRAPH_K_COMBINE;
321 return add_glue(g, kind, &g->k[ki], 1, 1, OMEGA_GPU_DGRAPH_HIDDEN, a, OMEGA_DEC_RES_WORDS);
322#endif
323}
324
325int omega_gpu_decode_graph_record_swiglu(OmegaGpuDecodeGraph *g, uint64_t gu_va, uint64_t act_va, float scale) {
326 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
327#ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS
328 const uint64_t c[16]={1,0,g->mutant,0,OMEGA_DEC_INTER,OMEGA_DEC_TOPK},v[16]={gu_va,act_va,omega_graph_fbits(scale)};
329 uint64_t node[64],result=0;uint32_t words[32];
330 if(omega_mg_aux(&result,c,16,v,16,node,64)!=OMEGA_OK||result!=1)return fail(g,NULL,OMEGA_GPU_DGRAPH_BAD_ARGS);
331 omega_graph_args(node,words);
332 return add_glue(g,node[6],&g->k[node[1]],node[2],node[3],node[4],words,node[5]);
333#else
334 uint32_t a[OMEGA_DEC_SW_WORDS]; memset(a, 0, sizeof a);
335 put64(a, OMEGA_DEC_SW_GU, gu_va); put64(a, OMEGA_DEC_SW_ACT, act_va); putf(a, OMEGA_DEC_SW_SCALE, scale);
336 return add_glue(g, OMEGA_GPU_DGRAPH_K_SWIGLU, &g->k[g->mutant == 1 ? K_SW_M1 : K_SW], OMEGA_DEC_INTER / OMEGA_DEC_THREADS, OMEGA_DEC_TOPK, (size_t)OMEGA_DEC_TOPK * OMEGA_DEC_INTER, a, OMEGA_DEC_SW_WORDS);
337#endif
338}
339
340/* A failed begin leaves no token behind: list emptied, out of the token, lock released (no end_token owed, omega #518). */
341static int begin_fail(OmegaGpuDecodeGraph *g) {
342 omega_gpu_cmdlist_reset(g->cl);
343 g->in_token = 0; g->failed = 1;
344 omega_gpu_session_unlock(); /* begin_fail releases */
345 return OMEGA_GPU_DGRAPH_CHIP_FAIL;
346}
347
348int omega_gpu_decode_graph_begin_token(OmegaGpuDecodeGraph *g, const float *x_embed) {
349 if (!g || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
350 omega_gpu_session_lock();
351 g->t_begin = omega_gpu_session_now_ns();
352 g->in_token = 1; g->failed = 0; g->xq_layer = -1; g->hidden_ready = 0;
353 memset(&g->acc, 0, sizeof g->acc);
354 g->n_kind = 0; g->tagged_in_list = 0; g->n_stamp = 0;
355 omega_gpu_cmdlist_reset(g->cl);
356 if (!omega_gpu_session_open()) return begin_fail(g);
357 uint32_t *tok = cpu_of(g, g->lo.token);
358 tok[0] = TOKEN_POISON; tok[1] = TOKEN_POISON;
359 if (x_embed) memcpy(cpu_of(g, g->lo.x), x_embed, OMEGA_GPU_DGRAPH_HIDDEN * 4u);
360 if (omega_gpu_session_sync_to_gpu(&g->buf) != 0) return begin_fail(g);
361 return OMEGA_GPU_DGRAPH_OK;
362}
363
364int omega_gpu_decode_graph_record_layer(OmegaGpuDecodeGraph *g, uint32_t l, uint32_t pos) {
365 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
366 if (g->failed) return OMEGA_GPU_DGRAPH_CHIP_FAIL;
367 if (l >= g->cfg.layers || !g->layer[l].set || g->hidden_ready || (l + 1 == g->cfg.layers && !g->final_set))
368 return fail(g, "decode graph: layer not set, out of order, or final norm missing", OMEGA_GPU_DGRAPH_BAD_ARGS);
369 OmegaGpuDecodeGraphVas v; omega_gpu_decode_graph_vas(g, &v);
370 const float eps = g->cfg.eps;
371 const uint64_t norm_l = va_of(g, g->lo.norms) + (uint64_t)l * OMEGA_GPU_DGRAPH_HIDDEN * 4u, tab = va_of(g, g->lo.tabs) + (uint64_t)l * 32u;
372#ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS
373 const uint64_t cfg[16] = {0,g->xq_layer!=(int)l,1,0,0,omega_gpu_decode_graph_prefetch(),
374 l,g->cfg.layers,pos,g->mutant,0,g->flush_each,OMEGA_GPU_DGRAPH_HIDDEN,
375 OMEGA_DEC_INTER,OMEGA_GPU_DGRAPH_TOPK,g->am_ctas};
376 uint64_t values[64] = {v.x,v.xq,v.qkv,0,v.attn,v.o,v.gu,v.act,v.dn,norm_l,
377 va_of(g,g->lo.fnorm),tab,0,0,v.hidden,v.attn};
378 values[21]=va_of(g,g->lo.pf_x); values[22]=va_of(g,g->lo.pf_y);
379 values[27]=omega_graph_fbits(eps); values[28]=omega_graph_fbits(SCALE_2P64); values[29]=omega_graph_fbits(1.0f);
380 static const int kernels[24] = {K_NORM,K_ADD,K_CN,K_SW,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,K_SW_M1,0,0,K_CN_M2};
381 OmegaGpuRouterDevOut router={0};
382 int rc=OMEGA_GPU_DGRAPH_OK;
383 for(uint64_t phase=0;rc==OMEGA_GPU_DGRAPH_OK;phase++) {
384 uint64_t node[64];
385 if(!omega_graph_next(cfg,values,node,phase))return fail(g,NULL,OMEGA_GPU_DGRAPH_CHIP_FAIL);
386 if(node[0]==255)break;
387 uint32_t words[32];omega_graph_args(node,words);
388 switch(node[0]) {
389 case 0:break;
390 case 1:case 9:case 12:case 14: {
391 static const uint8_t kinds[15] = {[1]=OMEGA_GPU_DGRAPH_K_NORM,[9]=OMEGA_GPU_DGRAPH_K_ADD,
392 [12]=OMEGA_GPU_DGRAPH_K_SWIGLU,[14]=OMEGA_GPU_DGRAPH_K_COMBINE};
393 rc=add_glue(g,kinds[node[0]],&g->k[kernels[node[1]]],node[2],node[3],node[4],words,node[5]);break;
394 }
395 case 2:rc=after_op(g,OMEGA_GPU_DGRAPH_K_QKV,omega_gpu_fp8_gemv_grouped_record(g->cl,g->layer[l].qkv,
396 node[7],node[10],node[11],node[8],node[12]));break;
397 case 6:rc=after_op(g,OMEGA_GPU_DGRAPH_K_ATTN,omega_gpu_decode_attn_record(g->cl,g->cfg.attn,l,node[13],node[11],eps,
398 g->mutant==3?1.0f:SCALE_2P64,node[12]));break;
399 case 8:
400 rc=after_op(g,OMEGA_GPU_DGRAPH_K_O,omega_gpu_fp8_gemv_grouped_record(g->cl,g->layer[l].o,
401 node[7],node[10],node[11],node[8],node[12]));
402 if(!rc&&g_prefetch_red&&!g->flush_each&&omega_gpu_cmdlist_mark_independent(g->cl))rc=OMEGA_GPU_DGRAPH_CHIP_FAIL;
403 break;
404 case 10:
405 rc=after_op(g,OMEGA_GPU_DGRAPH_K_ROUTER,omega_gpu_router_record(g->cl,g->cfg.router,l,node[11],eps,g->cfg.norm_topk,SCALE_2P64,&router));
406 values[16]=router.tab_gu_va;values[17]=router.tab_dn_va;values[18]=router.xout_va;values[19]=router.wts_va;break;
407 case 11:rc=after_op(g,OMEGA_GPU_DGRAPH_K_GATE_UP,omega_gpu_fp8_gemv_grouped_record(g->cl,g->layer[l].gu,
408 node[7],node[10],node[11],node[8],node[12]));break;
409 case 13:rc=after_op(g,OMEGA_GPU_DGRAPH_K_DOWN,omega_gpu_fp8_gemv_grouped_record(g->cl,g->layer[l].dn,
410 node[7],node[10],node[11],node[8],node[12]));break;
411 case 15: {
412 int prc=omega_gpu_fp8_gemv_grouped_record(g->cl,g->layer[l].o,node[7],node[10],node[11],node[8],node[12]);
413 if(!prc&&omega_gpu_cmdlist_mark_independent(g->cl))prc=-1;
414 rc=after_op(g,OMEGA_GPU_DGRAPH_K_ATTN,prc);break;
415 }
416 default:return fail(g,NULL,OMEGA_GPU_DGRAPH_CHIP_FAIL);
417 }
418 }
419 if(rc==OMEGA_GPU_DGRAPH_OK){if(l+1==g->cfg.layers)g->hidden_ready=1;else g->xq_layer=(int)l+1;}
420#else
421 int rc = OMEGA_GPU_DGRAPH_OK;
422 if (g->xq_layer != (int)l) rc = omega_gpu_decode_graph_record_res(g, OMEGA_DEC_RES_NORM, v.x, 0, 0, norm_l, v.xq, eps, SCALE_2P64);
423 if (rc == OMEGA_GPU_DGRAPH_OK) rc = after_op(g, OMEGA_GPU_DGRAPH_K_QKV, omega_gpu_fp8_gemv_grouped_record(g->cl, g->layer[l].qkv, 1, tab, v.xq, 1, v.qkv));
424 if (rc == OMEGA_GPU_DGRAPH_OK) rc = after_op(g, OMEGA_GPU_DGRAPH_K_ATTN, omega_gpu_decode_attn_record(g->cl, g->cfg.attn, l, pos, v.qkv, eps, g->mutant == 3 ? 1.0f : SCALE_2P64, v.attn));
425 if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_decode_graph_prefetch()) { /* tagged attn: its time is the overlapped window */
426 int prc = omega_gpu_fp8_gemv_grouped_record(g->cl, g->layer[l].o, 1, tab + 16u, va_of(g, g->lo.pf_x), 1, va_of(g, g->lo.pf_y));
427 if (prc == 0 && omega_gpu_cmdlist_mark_independent(g->cl) != 0) prc = -1;
428 rc = after_op(g, OMEGA_GPU_DGRAPH_K_ATTN, prc);
429 }
430 if (rc == OMEGA_GPU_DGRAPH_OK) rc = after_op(g, OMEGA_GPU_DGRAPH_K_O, omega_gpu_fp8_gemv_grouped_record(g->cl, g->layer[l].o, 1, tab + 16u, v.attn, 1, v.o));
431 if (rc == OMEGA_GPU_DGRAPH_OK && g_prefetch_red && !g->flush_each && omega_gpu_cmdlist_mark_independent(g->cl) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; /* TEST ONLY red: o_proj itself unordered after the attention */
432 if (rc == OMEGA_GPU_DGRAPH_OK) rc = omega_gpu_decode_graph_record_res(g, OMEGA_DEC_RES_ADD, v.x, v.o, 0, 0, 0, eps, 1.0f);
433 OmegaGpuRouterDevOut r; memset(&r, 0, sizeof r);
434 if (rc == OMEGA_GPU_DGRAPH_OK) rc = after_op(g, OMEGA_GPU_DGRAPH_K_ROUTER, omega_gpu_router_record(g->cl, g->cfg.router, l, v.x, eps, g->cfg.norm_topk, SCALE_2P64, &r));
435 if (rc == OMEGA_GPU_DGRAPH_OK) rc = after_op(g, OMEGA_GPU_DGRAPH_K_GATE_UP, omega_gpu_fp8_gemv_grouped_record(g->cl, g->layer[l].gu, OMEGA_GPU_DGRAPH_TOPK, r.tab_gu_va, r.xout_va, 1, v.gu));
436 if (rc == OMEGA_GPU_DGRAPH_OK) rc = omega_gpu_decode_graph_record_swiglu(g, v.gu, v.act, SCALE_2P64);
437 if (rc == OMEGA_GPU_DGRAPH_OK) rc = after_op(g, OMEGA_GPU_DGRAPH_K_DOWN, omega_gpu_fp8_gemv_grouped_record(g->cl, g->layer[l].dn, OMEGA_GPU_DGRAPH_TOPK, r.tab_dn_va, v.act, OMEGA_GPU_DGRAPH_TOPK, v.dn));
438 if (rc == OMEGA_GPU_DGRAPH_OK) {
439 const int last = l + 1 == g->cfg.layers;
440 const uint64_t nw = last ? va_of(g, g->lo.fnorm) : norm_l + OMEGA_GPU_DGRAPH_HIDDEN * 4u;
441 rc = omega_gpu_decode_graph_record_res(g, OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM, v.x, v.dn, r.wts_va, nw, last ? v.hidden : v.xq, eps, last ? 1.0f : SCALE_2P64);
442 if (rc == OMEGA_GPU_DGRAPH_OK) { if (last) g->hidden_ready = 1; else g->xq_layer = (int)l + 1; }
443 }
444#endif
445 return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc);
446}
447
448int omega_gpu_decode_graph_record_final(OmegaGpuDecodeGraph *g) {
449 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
450 if (g->failed) return OMEGA_GPU_DGRAPH_CHIP_FAIL;
451 if (!g->final_set) return fail(g, "decode graph: final norm not set", OMEGA_GPU_DGRAPH_BAD_ARGS);
452 OmegaGpuDecodeGraphVas v; omega_gpu_decode_graph_vas(g, &v);
453 int rc = OMEGA_GPU_DGRAPH_OK;
454#ifndef CUDAPP_MIGRATION_PENDING_C_GRAPHS
455 const uint64_t cfg[16]={g->hidden_ready,0,0,0,g->cfg.vocab,g->am_ctas,OMEGA_GPU_DGRAPH_HIDDEN,
456 omega_graph_fbits(g->cfg.eps),omega_graph_fbits(1.0f),omega_graph_fbits(1.0f)};
457 const uint64_t values[16]={v.x,v.hidden,v.logits,v.token,va_of(g,g->lo.fnorm),0,va_of(g,g->lo.am_v),va_of(g,g->lo.am_i)};
458 for(uint64_t phase=0;rc==OMEGA_GPU_DGRAPH_OK;phase++){
459 uint64_t node[64],result=0;
460 if(omega_mg_final(&result,cfg,16,values,16,node,64,phase)!=OMEGA_OK||result!=1)return fail(g,NULL,OMEGA_GPU_DGRAPH_CHIP_FAIL);
461 if(node[0]==255)break;
462 uint32_t words[32];omega_graph_args(node,words);
463 switch(node[0]){
464 case 0:break;
465 case 1:rc=add_glue(g,OMEGA_GPU_DGRAPH_K_NORM,&g->k[K_NORM],node[2],node[3],node[4],words,node[5]);break;
466 case 7:rc=after_op(g,OMEGA_GPU_DGRAPH_K_LM_HEAD,omega_gpu_bf16_gemv_record(g->cl,g->cfg.lm_head,node[11],node[12]));break;
467 case 8:rc=add_glue(g,OMEGA_GPU_DGRAPH_K_ARGMAX,&g->k[node[1]==5?K_AM0:K_AM1],node[2],node[3],node[4],words,node[5]);break;
468 default:return fail(g,NULL,OMEGA_GPU_DGRAPH_CHIP_FAIL);
469 }
470 }
471#else
472 if (!g->hidden_ready) rc = omega_gpu_decode_graph_record_res(g, OMEGA_DEC_RES_NORM, v.x, 0, 0, va_of(g, g->lo.fnorm), v.hidden, g->cfg.eps, 1.0f);
473 if (rc == OMEGA_GPU_DGRAPH_OK) rc = after_op(g, OMEGA_GPU_DGRAPH_K_LM_HEAD, omega_gpu_bf16_gemv_record(g->cl, g->cfg.lm_head, v.hidden, v.logits));
474 uint32_t a0[OMEGA_DEC_AM_WORDS], a1[OMEGA_DEC_AM_WORDS]; memset(a0, 0, sizeof a0); memset(a1, 0, sizeof a1);
475 const uint64_t amv = va_of(g, g->lo.am_v), ami = va_of(g, g->lo.am_i);
476 put64(a0, OMEGA_DEC_AM_V, v.logits); put64(a0, OMEGA_DEC_AM_I, 0); put64(a0, OMEGA_DEC_AM_OV, amv); put64(a0, OMEGA_DEC_AM_OI, ami);
477 put64(a1, OMEGA_DEC_AM_V, amv); put64(a1, OMEGA_DEC_AM_I, ami); put64(a1, OMEGA_DEC_AM_OV, v.token + 4u); put64(a1, OMEGA_DEC_AM_OI, v.token);
478 if (rc == OMEGA_GPU_DGRAPH_OK) rc = add_glue(g, OMEGA_GPU_DGRAPH_K_ARGMAX, &g->k[K_AM0], g->am_ctas, 1, (size_t)g->am_ctas * OMEGA_DEC_ARGMAX_SPAN, a0, OMEGA_DEC_AM_WORDS);
479 if (rc == OMEGA_GPU_DGRAPH_OK) rc = add_glue(g, OMEGA_GPU_DGRAPH_K_ARGMAX, &g->k[K_AM1], 1, 1, OMEGA_DEC_ARGMAX_SPAN, a1, OMEGA_DEC_AM_WORDS);
480#endif
481 return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc);
482}
483
484int omega_gpu_decode_graph_end_token(OmegaGpuDecodeGraph *g, uint32_t *token, OmegaGpuDecodeGraphStats *st) {
485 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
486 int rc = g->failed ? OMEGA_GPU_DGRAPH_CHIP_FAIL : OMEGA_GPU_DGRAPH_OK;
487 const uint64_t t_rec = omega_gpu_session_now_ns();
488 if (rc == OMEGA_GPU_DGRAPH_OK) rc = submit_now(g);
489 if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_decode_attn_counters_check(g->cfg.attn) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL);
490 if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_session_sync_to_cpu(&g->buf) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL);
491 if (rc == OMEGA_GPU_DGRAPH_OK) {
492 const uint32_t t = ((const uint32_t *)cpu_of(g, g->lo.token))[0];
493 if (t == TOKEN_POISON || t >= g->cfg.vocab) {
494 char e[96]; omega_gpu_fmt(e, sizeof e, "decode graph: token output %u not written or out of range", t);
495 rc = fail(g, e, OMEGA_GPU_DGRAPH_CHIP_FAIL);
496 } else if (token) *token = t;
497 }
498 omega_gpu_cmdlist_reset(g->cl);
499 g->in_token = 0;
500 const uint64_t t_end = omega_gpu_session_now_ns();
501 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; }
502 omega_gpu_session_unlock();
503 return rc;
504}
505
506int omega_gpu_decode_graph_abort_token(OmegaGpuDecodeGraph *g) {
507 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
508 omega_gpu_cmdlist_reset(g->cl);
509 g->in_token = 0; g->tagged_in_list = 0; g->n_stamp = 0; /* no timeline for an abandoned token */
510 omega_gpu_session_unlock();
511 return OMEGA_GPU_DGRAPH_OK;
512}
513
514int omega_gpu_decode_graph_token(OmegaGpuDecodeGraph *g, const float *x_embed, uint32_t pos, uint32_t *token, OmegaGpuDecodeGraphStats *st) {
515 if (!g) return OMEGA_GPU_DGRAPH_BAD_ARGS;
516 const int brc = omega_gpu_decode_graph_begin_token(g, x_embed);
517 if (brc != OMEGA_GPU_DGRAPH_OK) { if (st) memset(st, 0, sizeof *st); return brc; } /* nothing begun, lock free */
518 for (uint32_t l = 0; l < g->cfg.layers && !g->failed; l++) (void)omega_gpu_decode_graph_record_layer(g, l, pos);
519 if (!g->failed) (void)omega_gpu_decode_graph_record_final(g);
520 return omega_gpu_decode_graph_end_token(g, token, st);
521}
522
523int omega_gpu_decode_graph_read(OmegaGpuDecodeGraph *g, float *x, float *hidden, float *logits) {
524 if (!g || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
525 omega_gpu_session_lock();
526 int rc = omega_gpu_session_sync_to_cpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL;
527 if (rc == OMEGA_GPU_DGRAPH_OK) {
528 if (x) memcpy(x, cpu_of(g, g->lo.x), OMEGA_GPU_DGRAPH_HIDDEN * 4u);
529 if (hidden) memcpy(hidden, cpu_of(g, g->lo.hidden), OMEGA_GPU_DGRAPH_HIDDEN * 4u);
530 if (logits) memcpy(logits, cpu_of(g, g->lo.logits), (size_t)g->cfg.vocab * 4u);
531 }
532 omega_gpu_session_unlock();
533 return rc;
534}
535
536/* ---- timeline ---- */
537void omega_gpu_decode_graph_set_stamps(OmegaGpuDecodeGraph *g, int on) { if (g && !g->in_token) g->stamps = on ? 1 : 0; }
538
539int omega_gpu_decode_graph_stamps(const OmegaGpuDecodeGraph *g, uint64_t *stamps, uint8_t *kinds, uint32_t cap) {
540 if (!g || g->in_token || !stamps || !kinds) return OMEGA_GPU_DGRAPH_BAD_ARGS;
541 if (!g->stamps || g->failed || g->n_stamp != g->n_kind + 1u) { omega_gpu_session_set_error("decode graph: no timeline for the last token (stamps off or it failed)"); return OMEGA_GPU_DGRAPH_BAD_ARGS; }
542 if (cap < g->n_stamp) { omega_gpu_session_set_error("decode graph: timeline buffers too small"); return OMEGA_GPU_DGRAPH_BAD_ARGS; }
543 memcpy(stamps, g->stamp, (size_t)g->n_stamp * sizeof stamps[0]);
544 memcpy(kinds, g->kind, (size_t)g->n_kind);
545 return (int)g->n_kind;
546}
547
548const char *omega_gpu_decode_graph_kind_name(uint8_t kind) {
549 static const char *const names[OMEGA_GPU_DGRAPH_K_COUNT] = { "norm", "qkv", "attn", "o_proj", "res_add", "router", "gate_up", "swiglu", "down", "combine_norm", "lm_head", "argmax" };
550 return kind < OMEGA_GPU_DGRAPH_K_COUNT ? names[kind] : "?";
551}