interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / omega_gpu_decode_graph.c

454 lines · 27898 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * omega_gpu_decode_graph.c: one decode token as one command list (see the header). Records the four decode APIs'
19 * *_record launches and the omega_blackwell_codegen_dec glue kernels into an omega_gpu_cmdlist. Buffers:
20 * omega_gpu_session_alloc only (GPU-uncached, nvos.h:1117); the host touches x (before), token and logits (after).
21 */
22#include "omega_gpu_decode_graph.h"
23#include "omega_gpu_cmdlist.h"
24#include "omega_blackwell_codegen_dec.h"
25#include "omega_gpu_session.h"
26#include "omega_gpu_fmt.h"
27#include <stdlib.h>
28#include <string.h>
29
30#define TIMEOUT_MS 20000ull
31#define SCALE_2P64 18446744073709551616.0f
32#define NEG_PAD (-1e30f)
33#define TOKEN_POISON 0xffffffffu
34
35/* glue kernels */
36enum { K_NORM, K_ADD, K_CN, K_SW, K_AM0, K_AM1, K_SW_M1, K_CN_M2, K_COUNT };
37
38typedef struct { OmegaGpuMem code; uint32_t gpr; } Kern;
39
40/* the one buffer: byte offsets, every region 256-byte aligned (RES/SWIGLU load 16-byte vectors) */
41typedef struct {
42 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;
43} Layout;
44
45struct OmegaGpuDecodeGraph {
46 OmegaGpuDecodeGraphConfig cfg;
47 uint32_t am_ctas; /* stage-1 ARGMAX CTAs = ceil(vocab / 2048) */
48 Layout lo;
49 OmegaGpuMem buf;
50 Kern k[K_COUNT];
51 OmegaGpuCmdList *cl;
52 struct { const OmegaGpuFp8Tensor *qkv, *o, *gu, *dn; int set; } layer[OMEGA_GPU_DGRAPH_MAX_LAYERS];
53 int final_set;
54 /* token state */
55 int in_token, failed;
56 int xq_layer; /* layer whose input-norm x' sits in xq, -1 none */
57 int hidden_ready;
58 uint64_t t_begin;
59 OmegaGpuDecodeGraphStats acc;
60 int flush_each, mutant;
61 /* timeline (set_stamps): one kind per recorded launch and the GPU timestamps around every launch of the last token */
62 int stamps;
63 uint32_t n_kind, tagged_in_list, n_stamp;
64 uint8_t kind[OMEGA_GPU_CMDLIST_MAX_LAUNCHES];
65 uint64_t stamp[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1];
66};
67
68static size_t al(size_t v) { return (v + 255u) & ~(size_t)255u; }
69static uint64_t va_of(const OmegaGpuDecodeGraph *g, size_t off) { return g->buf.va + off; }
70static void *cpu_of(const OmegaGpuDecodeGraph *g, size_t off) { return (uint8_t *)g->buf.cpu + off; }
71static 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); }
72static void putf(uint32_t *a, unsigned off, float f) { memcpy(&a[(off - 0x380) / 4], &f, 4); }
73static int fail(OmegaGpuDecodeGraph *g, const char *what, int rc) {
74 if (g) g->failed = 1;
75 if (what) omega_gpu_session_set_error(what);
76 return rc;
77}
78
79/* lock held */
80static int kern_build(Kern *k, OmegaDecKind kind, uint32_t mode, int mutant) {
81 OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = kind; s.mode = mode; s.mutant = mutant;
82 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog);
83 if (!prog) return OMEGA_GPU_DGRAPH_CODEGEN_FAIL;
84 int rc = OMEGA_GPU_DGRAPH_OK;
85 size_t need = 0;
86 if (omega_blackwell_codegen_dec_prog(&s, prog) != 0 || (need = omega_bw_program_code_bytes(prog)) == 0) rc = OMEGA_GPU_DGRAPH_CODEGEN_FAIL;
87 else if (omega_gpu_session_alloc_code(need, &k->code) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL;
88 else {
89 OmegaBlackwellKernel kk;
90 if (omega_blackwell_kernel_encode_into(prog, k->code.cpu, need, &kk) != 0 || kk.code_size != need) rc = OMEGA_GPU_DGRAPH_CODEGEN_FAIL;
91 else if (omega_gpu_session_sync_to_gpu(&k->code) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL;
92 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) */
93 if (rc != OMEGA_GPU_DGRAPH_OK) { omega_gpu_session_free(&k->code); memset(k, 0, sizeof *k); }
94 }
95 omega_gpu_host_free(prog);
96 return rc;
97}
98
99void omega_gpu_decode_graph_free(OmegaGpuDecodeGraph *g) {
100 if (!g) return;
101 if (g->cl) omega_gpu_cmdlist_free(g->cl);
102 omega_gpu_session_lock();
103 for (unsigned i = 0; i < K_COUNT; i++) if (g->k[i].code.cpu) omega_gpu_session_free(&g->k[i].code);
104 if (g->buf.cpu) omega_gpu_session_free(&g->buf);
105 omega_gpu_session_unlock();
106 omega_gpu_host_free(g);
107}
108
109int omega_gpu_decode_graph_create(const OmegaGpuDecodeGraphConfig *cfg, OmegaGpuDecodeGraph **out) {
110 if (out) *out = NULL;
111 if (!cfg || !out || cfg->layers == 0 || cfg->layers > OMEGA_GPU_DGRAPH_MAX_LAYERS || !cfg->router || !cfg->attn || !cfg->lm_head ||
112 cfg->vocab == 0 || cfg->vocab > OMEGA_DEC_ARGMAX_SPAN * OMEGA_DEC_ARGMAX_SPAN) return OMEGA_GPU_DGRAPH_BAD_ARGS;
113 uint32_t n = 0, k = 0;
114 omega_gpu_bf16_tensor_shape(cfg->lm_head, &n, &k);
115 if (n != cfg->vocab || k != OMEGA_GPU_DGRAPH_HIDDEN) return OMEGA_GPU_DGRAPH_BAD_ARGS;
116 OmegaGpuDecodeGraph *g = omega_gpu_host_calloc(1, sizeof *g);
117 if (!g) return OMEGA_GPU_DGRAPH_BAD_ARGS;
118 g->cfg = *cfg;
119 g->am_ctas = (cfg->vocab + OMEGA_DEC_ARGMAX_SPAN - 1) / OMEGA_DEC_ARGMAX_SPAN;
120 g->xq_layer = -1;
121 const size_t H = OMEGA_GPU_DGRAPH_HIDDEN, TK = OMEGA_GPU_DGRAPH_TOPK, I = OMEGA_GPU_DGRAPH_INTER;
122 Layout *L = &g->lo; size_t o = 0;
123#define REG(f, bytes) do { L->f = o; o = al(o + (bytes)); } while (0)
124 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);
125 REG(gu, TK * 2 * I * 4); REG(act, TK * I * 4); REG(dn, TK * H * 4); REG(hidden, H * 4);
126 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);
127 REG(token, 16); REG(norms, (size_t)cfg->layers * H * 4); REG(fnorm, H * 4); REG(tabs, (size_t)cfg->layers * 2 * 16);
128 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 */
129#undef REG
130 L->bytes = o;
131 int rc = OMEGA_GPU_DGRAPH_OK;
132 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; }
133 omega_gpu_session_lock();
134 if (!omega_gpu_session_open() || omega_gpu_session_alloc(L->bytes, &g->buf) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL;
135 static const struct { OmegaDecKind kind; uint32_t mode; int mutant; } spec[K_COUNT] = {
136 [K_NORM] = { OMEGA_DEC_RES, OMEGA_DEC_RES_NORM, 0 },
137 [K_ADD] = { OMEGA_DEC_RES, OMEGA_DEC_RES_ADD, 0 },
138 [K_CN] = { OMEGA_DEC_RES, OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM, 0 },
139 [K_SW] = { OMEGA_DEC_SWIGLU, 0, 0 },
140 [K_AM0] = { OMEGA_DEC_ARGMAX, 0, 0 },
141 [K_AM1] = { OMEGA_DEC_ARGMAX, 1, 0 },
142 [K_SW_M1] = { OMEGA_DEC_SWIGLU, 0, 1 },
143 [K_CN_M2] = { OMEGA_DEC_RES, OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM, 2 },
144 };
145 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);
146 if (rc == OMEGA_GPU_DGRAPH_OK) {
147 memset(g->buf.cpu, 0, L->bytes);
148 float *lg = cpu_of(g, L->logits), *amv = cpu_of(g, L->am_v);
149 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 */
150 for (size_t i = 0; i < OMEGA_DEC_ARGMAX_SPAN; i++) amv[i] = NEG_PAD; /* stage 1 writes [0, ctas) only */
151 if (omega_gpu_session_sync_to_gpu(&g->buf) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL;
152 }
153 omega_gpu_session_unlock();
154 if (rc != OMEGA_GPU_DGRAPH_OK) { omega_gpu_decode_graph_free(g); return rc; }
155 *out = g;
156 return OMEGA_GPU_DGRAPH_OK;
157}
158
159static int shape_is(const OmegaGpuFp8Tensor *t, uint32_t e, uint32_t n, uint32_t k) {
160 uint32_t te = 0, tn = 0, tk = 0; omega_gpu_fp8_tensor_shape(t, &te, &tn, &tk);
161 return t && te == e && tn == n && tk == k;
162}
163
164int omega_gpu_decode_graph_set_layer(OmegaGpuDecodeGraph *g, uint32_t layer, const OmegaGpuFp8Tensor *qkv, const OmegaGpuFp8Tensor *o,
165 const OmegaGpuFp8Tensor *gate_up, const OmegaGpuFp8Tensor *down, const float *input_norm) {
166 if (!g || layer >= g->cfg.layers || !input_norm || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
167 const uint32_t H = OMEGA_GPU_DGRAPH_HIDDEN, I = OMEGA_GPU_DGRAPH_INTER;
168 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))
169 return OMEGA_GPU_DGRAPH_BAD_ARGS;
170 uint64_t t[4];
171 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;
172 omega_gpu_session_lock();
173 memcpy((uint8_t *)cpu_of(g, g->lo.tabs) + (size_t)layer * 32u, t, sizeof t);
174 memcpy((float *)cpu_of(g, g->lo.norms) + (size_t)layer * H, input_norm, H * 4u);
175 int rc = omega_gpu_session_sync_to_gpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL;
176 omega_gpu_session_unlock();
177 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; }
178 return rc;
179}
180
181int omega_gpu_decode_graph_set_final_norm(OmegaGpuDecodeGraph *g, const float *final_norm) {
182 if (!g || !final_norm || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
183 omega_gpu_session_lock();
184 memcpy(cpu_of(g, g->lo.fnorm), final_norm, OMEGA_GPU_DGRAPH_HIDDEN * 4u);
185 int rc = omega_gpu_session_sync_to_gpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL;
186 omega_gpu_session_unlock();
187 if (rc == OMEGA_GPU_DGRAPH_OK) g->final_set = 1;
188 return rc;
189}
190
191OmegaGpuCmdList *omega_gpu_decode_graph_list(OmegaGpuDecodeGraph *g) { return g ? g->cl : NULL; }
192void omega_gpu_decode_graph_set_flush_each(OmegaGpuDecodeGraph *g, int on) { if (g) g->flush_each = on ? 1 : 0; }
193void omega_gpu_decode_graph_test_set_mutant(OmegaGpuDecodeGraph *g, int mutant) { if (g) g->mutant = mutant; }
194
195void omega_gpu_decode_graph_vas(const OmegaGpuDecodeGraph *g, OmegaGpuDecodeGraphVas *v) {
196 if (!v) return;
197 memset(v, 0, sizeof *v);
198 if (!g) return;
199 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);
200 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);
201 v->logits = va_of(g, g->lo.logits); v->token = va_of(g, g->lo.token);
202}
203
204/* ---- the list ---- */
205
206/* barrier between the launches of a token (W-L2c launch-cost study): omega_gpu_decode_graph_set_barrier, else env
207 * AIEN_OMEGA_DGRAPH_BARRIER (0 wfi+inval_all = default, 2 wfi_only, 3 wfi+inval_data, 4 flush_data; 1 none is refused: the
208 * token is a dependency chain). Read once. */
209static int g_barrier = -1;
210void omega_gpu_decode_graph_set_barrier(int b) { g_barrier = (b >= 0 && b <= 4 && b != 1) ? b : 0; }
211int omega_gpu_decode_graph_barrier(void) {
212 if (g_barrier < 0) { const char *e = getenv("AIEN_OMEGA_DGRAPH_BARRIER"); omega_gpu_decode_graph_set_barrier(e ? atoi(e) : 0); }
213 return g_barrier;
214}
215void omega_gpu_decode_graph_test_set_barrier_none(void) { g_barrier = 1; }
216/* weight prefetch (W-L2d): 1 = beside each layer's attention, an independent launch (no barrier before it) runs the o_proj GEMV
217 * 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
218 * o_proj then reads them from L2 (needs GPU-cached weights: an uncached mapping is never kept in L2, gpu_l2_reuse_test r2).
219 * omega_gpu_decode_graph_set_prefetch, else env AIEN_OMEGA_DGRAPH_PREFETCH ("1"). Read once. Default 0. */
220static int g_prefetch = -1, g_prefetch_red;
221void omega_gpu_decode_graph_set_prefetch(int on) { g_prefetch = on ? 1 : 0; }
222int omega_gpu_decode_graph_prefetch(void) {
223 if (g_prefetch < 0) { const char *e = getenv("AIEN_OMEGA_DGRAPH_PREFETCH"); g_prefetch = (e && e[0] == '1' && e[1] == 0) ? 1 : 0; }
224 return g_prefetch;
225}
226void omega_gpu_decode_graph_test_set_prefetch_red(int on) { g_prefetch_red = on ? 1 : 0; }
227static OmegaGpuCmdListBarrier barrier_now(void) { return (OmegaGpuCmdListBarrier)omega_gpu_decode_graph_barrier(); }
228
229static OmegaGpuCmdListOpts list_opts(const OmegaGpuDecodeGraph *g) {
230 OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o);
231 o.mode = OMEGA_GPU_CMDLIST_PCAS; o.barrier = barrier_now(); o.timeout_ms = TIMEOUT_MS; o.stamps = g->stamps;
232 return o;
233}
234
235static int submit_now(OmegaGpuDecodeGraph *g) {
236 if (omega_gpu_cmdlist_count(g->cl) == 0) return OMEGA_GPU_DGRAPH_OK;
237 OmegaGpuCmdListOpts o = list_opts(g);
238 OmegaGpuCmdListStats st; memset(&st, 0, sizeof st);
239 int rc = omega_gpu_cmdlist_submit(g->cl, &o, &st);
240 g->acc.launches += st.launches; g->acc.submits += st.submits; g->acc.chip_ns += st.elapsed_ns; g->acc.sems_done += st.sems_done;
241 if (rc == 0 && g->stamps) { /* before reset: the accessor sizes by the list; later submits (flush_each) drop their start slot */
242 uint64_t tmp[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1];
243 int cnt = omega_gpu_cmdlist_stamps(g->cl, tmp, OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1);
244 if (cnt < 0) rc = -1;
245 else {
246 const int from = g->n_stamp == 0 ? 0 : 1;
247 const uint32_t room = (uint32_t)(sizeof g->stamp / sizeof g->stamp[0]) - g->n_stamp;
248 if (cnt > from && (uint32_t)(cnt - from) > room) { /* more launches this token than the timeline holds */
249 omega_gpu_session_set_error("decode graph: timeline full (more launches per token than OMEGA_GPU_CMDLIST_MAX_LAUNCHES)");
250 rc = -1;
251 } else {
252 for (int i = from; i < cnt; i++) g->stamp[g->n_stamp++] = tmp[i];
253 }
254 }
255 }
256 g->tagged_in_list = 0;
257 omega_gpu_cmdlist_reset(g->cl);
258 if (rc != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL);
259 if (st.sems_done != st.launches) {
260 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);
261 return fail(g, e, OMEGA_GPU_DGRAPH_CHIP_FAIL);
262 }
263 return OMEGA_GPU_DGRAPH_OK;
264}
265
266/* kinds for the launches added since the last tag (the timeline) */
267static void tag(OmegaGpuDecodeGraph *g, uint8_t kind) {
268 const uint32_t n = omega_gpu_cmdlist_count(g->cl);
269 for (; g->tagged_in_list < n && g->n_kind < OMEGA_GPU_CMDLIST_MAX_LAUNCHES; g->tagged_in_list++) g->kind[g->n_kind++] = kind;
270}
271
272/* after every recorded launch: flush_each submits it on its own (the comparison path) */
273static int after_op(OmegaGpuDecodeGraph *g, uint8_t kind, int api_rc) {
274 if (api_rc != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL);
275 tag(g, kind);
276 return g->flush_each ? submit_now(g) : OMEGA_GPU_DGRAPH_OK;
277}
278
279static 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) {
280 OmegaGpuLaunch L; memset(&L, 0, sizeof L);
281 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;
282 L.num_elements = elems; L.args = args; L.n_args = n_args; L.timeout_ms = TIMEOUT_MS;
283 return after_op(g, kind, omega_gpu_cmdlist_add(g->cl, &L) < 0 ? -1 : 0);
284}
285
286static int res_kernel(const OmegaGpuDecodeGraph *g, uint32_t mode) {
287 if (mode == OMEGA_DEC_RES_NORM) return K_NORM;
288 if (mode == OMEGA_DEC_RES_ADD) return K_ADD;
289 if (mode == (OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM)) return g->mutant == 2 ? K_CN_M2 : K_CN;
290 return -1;
291}
292
293int omega_gpu_decode_graph_record_res(OmegaGpuDecodeGraph *g, uint32_t mode, uint64_t x_va, uint64_t a_va, uint64_t w_va,
294 uint64_t nw_va, uint64_t y_va, float eps, float scale) {
295 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
296 int ki = res_kernel(g, mode);
297 if (ki < 0) return fail(g, "decode graph: RES mode not built (NORM, ADD, COMBINE|NORM)", OMEGA_GPU_DGRAPH_BAD_ARGS);
298 uint32_t a[OMEGA_DEC_RES_WORDS]; memset(a, 0, sizeof a);
299 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);
300 put64(a, OMEGA_DEC_RES_Y, y_va); putf(a, OMEGA_DEC_RES_EPS, eps); putf(a, OMEGA_DEC_RES_SCALE, scale);
301 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;
302 return add_glue(g, kind, &g->k[ki], 1, 1, OMEGA_GPU_DGRAPH_HIDDEN, a, OMEGA_DEC_RES_WORDS);
303}
304
305int omega_gpu_decode_graph_record_swiglu(OmegaGpuDecodeGraph *g, uint64_t gu_va, uint64_t act_va, float scale) {
306 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
307 uint32_t a[OMEGA_DEC_SW_WORDS]; memset(a, 0, sizeof a);
308 put64(a, OMEGA_DEC_SW_GU, gu_va); put64(a, OMEGA_DEC_SW_ACT, act_va); putf(a, OMEGA_DEC_SW_SCALE, scale);
309 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);
310}
311
312/* A failed begin leaves no token behind: list emptied, out of the token, lock released (no end_token owed, omega #518). */
313static int begin_fail(OmegaGpuDecodeGraph *g) {
314 omega_gpu_cmdlist_reset(g->cl);
315 g->in_token = 0; g->failed = 1;
316 omega_gpu_session_unlock(); /* begin_fail releases */
317 return OMEGA_GPU_DGRAPH_CHIP_FAIL;
318}
319
320int omega_gpu_decode_graph_begin_token(OmegaGpuDecodeGraph *g, const float *x_embed) {
321 if (!g || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
322 omega_gpu_session_lock();
323 g->t_begin = omega_gpu_session_now_ns();
324 g->in_token = 1; g->failed = 0; g->xq_layer = -1; g->hidden_ready = 0;
325 memset(&g->acc, 0, sizeof g->acc);
326 g->n_kind = 0; g->tagged_in_list = 0; g->n_stamp = 0;
327 omega_gpu_cmdlist_reset(g->cl);
328 if (!omega_gpu_session_open()) return begin_fail(g);
329 uint32_t *tok = cpu_of(g, g->lo.token);
330 tok[0] = TOKEN_POISON; tok[1] = TOKEN_POISON;
331 if (x_embed) memcpy(cpu_of(g, g->lo.x), x_embed, OMEGA_GPU_DGRAPH_HIDDEN * 4u);
332 if (omega_gpu_session_sync_to_gpu(&g->buf) != 0) return begin_fail(g);
333 return OMEGA_GPU_DGRAPH_OK;
334}
335
336int omega_gpu_decode_graph_record_layer(OmegaGpuDecodeGraph *g, uint32_t l, uint32_t pos) {
337 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
338 if (g->failed) return OMEGA_GPU_DGRAPH_CHIP_FAIL;
339 if (l >= g->cfg.layers || !g->layer[l].set || g->hidden_ready || (l + 1 == g->cfg.layers && !g->final_set))
340 return fail(g, "decode graph: layer not set, out of order, or final norm missing", OMEGA_GPU_DGRAPH_BAD_ARGS);
341 OmegaGpuDecodeGraphVas v; omega_gpu_decode_graph_vas(g, &v);
342 const float eps = g->cfg.eps;
343 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;
344 int rc = OMEGA_GPU_DGRAPH_OK;
345 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);
346 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));
347 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));
348 if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_decode_graph_prefetch()) { /* tagged attn: its time is the overlapped window */
349 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));
350 if (prc == 0 && omega_gpu_cmdlist_mark_independent(g->cl) != 0) prc = -1;
351 rc = after_op(g, OMEGA_GPU_DGRAPH_K_ATTN, prc);
352 }
353 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));
354 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 */
355 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);
356 OmegaGpuRouterDevOut r; memset(&r, 0, sizeof r);
357 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));
358 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));
359 if (rc == OMEGA_GPU_DGRAPH_OK) rc = omega_gpu_decode_graph_record_swiglu(g, v.gu, v.act, SCALE_2P64);
360 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));
361 if (rc == OMEGA_GPU_DGRAPH_OK) {
362 const int last = l + 1 == g->cfg.layers;
363 const uint64_t nw = last ? va_of(g, g->lo.fnorm) : norm_l + OMEGA_GPU_DGRAPH_HIDDEN * 4u;
364 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);
365 if (rc == OMEGA_GPU_DGRAPH_OK) { if (last) g->hidden_ready = 1; else g->xq_layer = (int)l + 1; }
366 }
367 return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc);
368}
369
370int omega_gpu_decode_graph_record_final(OmegaGpuDecodeGraph *g) {
371 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
372 if (g->failed) return OMEGA_GPU_DGRAPH_CHIP_FAIL;
373 if (!g->final_set) return fail(g, "decode graph: final norm not set", OMEGA_GPU_DGRAPH_BAD_ARGS);
374 OmegaGpuDecodeGraphVas v; omega_gpu_decode_graph_vas(g, &v);
375 int rc = OMEGA_GPU_DGRAPH_OK;
376 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);
377 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));
378 uint32_t a0[OMEGA_DEC_AM_WORDS], a1[OMEGA_DEC_AM_WORDS]; memset(a0, 0, sizeof a0); memset(a1, 0, sizeof a1);
379 const uint64_t amv = va_of(g, g->lo.am_v), ami = va_of(g, g->lo.am_i);
380 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);
381 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);
382 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);
383 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);
384 return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc);
385}
386
387int omega_gpu_decode_graph_end_token(OmegaGpuDecodeGraph *g, uint32_t *token, OmegaGpuDecodeGraphStats *st) {
388 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
389 int rc = g->failed ? OMEGA_GPU_DGRAPH_CHIP_FAIL : OMEGA_GPU_DGRAPH_OK;
390 const uint64_t t_rec = omega_gpu_session_now_ns();
391 if (rc == OMEGA_GPU_DGRAPH_OK) rc = submit_now(g);
392 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);
393 if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_session_sync_to_cpu(&g->buf) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL);
394 if (rc == OMEGA_GPU_DGRAPH_OK) {
395 const uint32_t t = ((const uint32_t *)cpu_of(g, g->lo.token))[0];
396 if (t == TOKEN_POISON || t >= g->cfg.vocab) {
397 char e[96]; omega_gpu_fmt(e, sizeof e, "decode graph: token output %u not written or out of range", t);
398 rc = fail(g, e, OMEGA_GPU_DGRAPH_CHIP_FAIL);
399 } else if (token) *token = t;
400 }
401 omega_gpu_cmdlist_reset(g->cl);
402 g->in_token = 0;
403 const uint64_t t_end = omega_gpu_session_now_ns();
404 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; }
405 omega_gpu_session_unlock();
406 return rc;
407}
408
409int omega_gpu_decode_graph_abort_token(OmegaGpuDecodeGraph *g) {
410 if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
411 omega_gpu_cmdlist_reset(g->cl);
412 g->in_token = 0; g->tagged_in_list = 0; g->n_stamp = 0; /* no timeline for an abandoned token */
413 omega_gpu_session_unlock();
414 return OMEGA_GPU_DGRAPH_OK;
415}
416
417int omega_gpu_decode_graph_token(OmegaGpuDecodeGraph *g, const float *x_embed, uint32_t pos, uint32_t *token, OmegaGpuDecodeGraphStats *st) {
418 if (!g) return OMEGA_GPU_DGRAPH_BAD_ARGS;
419 const int brc = omega_gpu_decode_graph_begin_token(g, x_embed);
420 if (brc != OMEGA_GPU_DGRAPH_OK) { if (st) memset(st, 0, sizeof *st); return brc; } /* nothing begun, lock free */
421 for (uint32_t l = 0; l < g->cfg.layers && !g->failed; l++) (void)omega_gpu_decode_graph_record_layer(g, l, pos);
422 if (!g->failed) (void)omega_gpu_decode_graph_record_final(g);
423 return omega_gpu_decode_graph_end_token(g, token, st);
424}
425
426int omega_gpu_decode_graph_read(OmegaGpuDecodeGraph *g, float *x, float *hidden, float *logits) {
427 if (!g || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS;
428 omega_gpu_session_lock();
429 int rc = omega_gpu_session_sync_to_cpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL;
430 if (rc == OMEGA_GPU_DGRAPH_OK) {
431 if (x) memcpy(x, cpu_of(g, g->lo.x), OMEGA_GPU_DGRAPH_HIDDEN * 4u);
432 if (hidden) memcpy(hidden, cpu_of(g, g->lo.hidden), OMEGA_GPU_DGRAPH_HIDDEN * 4u);
433 if (logits) memcpy(logits, cpu_of(g, g->lo.logits), (size_t)g->cfg.vocab * 4u);
434 }
435 omega_gpu_session_unlock();
436 return rc;
437}
438
439/* ---- timeline ---- */
440void omega_gpu_decode_graph_set_stamps(OmegaGpuDecodeGraph *g, int on) { if (g && !g->in_token) g->stamps = on ? 1 : 0; }
441
442int omega_gpu_decode_graph_stamps(const OmegaGpuDecodeGraph *g, uint64_t *stamps, uint8_t *kinds, uint32_t cap) {
443 if (!g || g->in_token || !stamps || !kinds) return OMEGA_GPU_DGRAPH_BAD_ARGS;
444 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; }
445 if (cap < g->n_stamp) { omega_gpu_session_set_error("decode graph: timeline buffers too small"); return OMEGA_GPU_DGRAPH_BAD_ARGS; }
446 memcpy(stamps, g->stamp, (size_t)g->n_stamp * sizeof stamps[0]);
447 memcpy(kinds, g->kind, (size_t)g->n_kind);
448 return (int)g->n_kind;
449}
450
451const char *omega_gpu_decode_graph_kind_name(uint8_t kind) {
452 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" };
453 return kind < OMEGA_GPU_DGRAPH_K_COUNT ? names[kind] : "?";
454}